corpus_generator.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /**
  5. * This code is used to generate a binary file for every request type
  6. * which can be sent from a client to the server.
  7. * These files form the basic corpus for fuzzing the server.
  8. * This script is intended to be executed manually and then commit the new
  9. * corpus to the repository.
  10. */
  11. #ifdef _MSC_VER
  12. #pragma warning(disable:4996) // warning C4996: 'UA_Client_Subscriptions_manuallySendPublishRequest': was declared deprecated
  13. #endif
  14. #ifndef UA_DEBUG_DUMP_PKGS_FILE
  15. #error UA_DEBUG_DUMP_PKGS_FILE must be defined
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <pthread.h>
  20. #include <ua_types.h>
  21. #include <sys/stat.h>
  22. #include <server/ua_server_internal.h>
  23. #include <dirent.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <ua_client_highlevel.h>
  27. #include <ua_client_subscriptions.h>
  28. #include "ua_config_default.h"
  29. UA_Server *server;
  30. UA_ServerConfig *config;
  31. UA_Boolean *running;
  32. pthread_t server_thread;
  33. static void * serverloop(void *_) {
  34. while(*running)
  35. UA_Server_run_iterate(server, true);
  36. return NULL;
  37. }
  38. static void start_server(void) {
  39. running = UA_Boolean_new();
  40. *running = true;
  41. config = UA_ServerConfig_new_default();
  42. server = UA_Server_new(config);
  43. UA_Server_run_startup(server);
  44. pthread_create(&server_thread, NULL, serverloop, NULL);
  45. }
  46. static void teardown_server(void) {
  47. *running = false;
  48. pthread_join(server_thread, NULL);
  49. UA_Server_run_shutdown(server);
  50. UA_Boolean_delete(running);
  51. UA_Server_delete(server);
  52. UA_ServerConfig_delete(config);
  53. }
  54. static void emptyCorpusDir(void) {
  55. DIR *theFolder = opendir(UA_CORPUS_OUTPUT_DIR);
  56. struct dirent *next_file;
  57. char filepath[400];
  58. while ( (next_file = readdir(theFolder)) != NULL ) {
  59. // build the path for each file in the folder
  60. sprintf(filepath, "%s/%s", UA_CORPUS_OUTPUT_DIR, next_file->d_name);
  61. remove(filepath);
  62. }
  63. closedir(theFolder);
  64. }
  65. #define ASSERT_GOOD(X) if (X != UA_STATUSCODE_GOOD) return X;
  66. /*************************************************
  67. * The following list of client requests is based
  68. * on ua_server_binary.c:getServicePointers to
  69. * cover all possible services and their inputs
  70. ************************************************/
  71. static UA_StatusCode
  72. findServersRequest(UA_Client *client) {
  73. UA_ApplicationDescription* applicationDescriptionArray = NULL;
  74. size_t applicationDescriptionArraySize = 0;
  75. size_t serverUrisSize = 1;
  76. UA_String *serverUris = UA_String_new();
  77. serverUris[0] = UA_String_fromChars("urn:some:server:uri");
  78. size_t localeIdsSize = 1;
  79. UA_String *localeIds = UA_String_new();
  80. localeIds[0] = UA_String_fromChars("en");
  81. ASSERT_GOOD(UA_Client_findServers(client, "opc.tcp://localhost:4840",
  82. serverUrisSize, serverUris, localeIdsSize, localeIds,
  83. &applicationDescriptionArraySize, &applicationDescriptionArray));
  84. UA_Array_delete(serverUris, serverUrisSize, &UA_TYPES[UA_TYPES_STRING]);
  85. UA_Array_delete(localeIds, localeIdsSize, &UA_TYPES[UA_TYPES_STRING]);
  86. UA_Array_delete(applicationDescriptionArray, applicationDescriptionArraySize,
  87. &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]);
  88. return UA_STATUSCODE_GOOD;
  89. }
  90. static UA_StatusCode
  91. findServersOnNetworkRequest(UA_Client *client) {
  92. UA_ServerOnNetwork* serverOnNetwork = NULL;
  93. size_t serverOnNetworkSize = 0;
  94. size_t serverCapabilityFilterSize = 2;
  95. UA_String *serverCapabilityFilter = (UA_String*)UA_malloc(sizeof(UA_String) * serverCapabilityFilterSize);
  96. serverCapabilityFilter[0] = UA_String_fromChars("LDS");
  97. serverCapabilityFilter[1] = UA_String_fromChars("NA");
  98. ASSERT_GOOD(UA_Client_findServersOnNetwork(client, "opc.tcp://localhost:4840", 0, 0,
  99. serverCapabilityFilterSize, serverCapabilityFilter,
  100. &serverOnNetworkSize, &serverOnNetwork));
  101. UA_Array_delete(serverCapabilityFilter, serverCapabilityFilterSize,
  102. &UA_TYPES[UA_TYPES_STRING]);
  103. UA_Array_delete(serverOnNetwork, serverOnNetworkSize, &UA_TYPES[UA_TYPES_SERVERONNETWORK]);
  104. return UA_STATUSCODE_GOOD;
  105. }
  106. static void
  107. initUaRegisterServer(UA_RegisteredServer *requestServer) {
  108. requestServer->isOnline = UA_TRUE;
  109. requestServer->serverUri = server->config.applicationDescription.applicationUri;
  110. requestServer->productUri = server->config.applicationDescription.productUri;
  111. requestServer->serverType = server->config.applicationDescription.applicationType;
  112. requestServer->gatewayServerUri = server->config.applicationDescription.gatewayServerUri;
  113. // create the semaphore
  114. int fd = open("/tmp/open62541-corpus-semaphore", O_RDWR|O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
  115. close(fd);
  116. requestServer->semaphoreFilePath = UA_STRING("/tmp/open62541-corpus-semaphore");
  117. requestServer->serverNames = &server->config.applicationDescription.applicationName;
  118. requestServer->serverNamesSize = 1;
  119. size_t nl_discurls = server->config.networkLayersSize;
  120. requestServer->discoveryUrls = (UA_String*)UA_malloc(sizeof(UA_String) * nl_discurls);
  121. requestServer->discoveryUrlsSize = nl_discurls;
  122. for(size_t i = 0; i < nl_discurls; ++i) {
  123. UA_ServerNetworkLayer *nl = &server->config.networkLayers[i];
  124. requestServer->discoveryUrls[i] = nl->discoveryUrl;
  125. }
  126. }
  127. static UA_StatusCode
  128. registerServerRequest(UA_Client *client) {
  129. /* Prepare the request. Do not cleanup the request after the service call,
  130. * as the members are stack-allocated or point into the server config. */
  131. UA_RegisterServerRequest request;
  132. UA_RegisterServerRequest_init(&request);
  133. /* Copy from RegisterServer2 request */
  134. request.requestHeader.timestamp = UA_DateTime_now();
  135. request.requestHeader.timeoutHint = 10000;
  136. initUaRegisterServer(&request.server);
  137. UA_RegisterServerResponse response;
  138. UA_RegisterServerResponse_init(&response);
  139. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERSERVERREQUEST],
  140. &response, &UA_TYPES[UA_TYPES_REGISTERSERVERRESPONSE]);
  141. UA_free(request.server.discoveryUrls);
  142. ASSERT_GOOD(response.responseHeader.serviceResult);
  143. return UA_STATUSCODE_GOOD;
  144. }
  145. static UA_StatusCode
  146. registerServer2Request(UA_Client *client) {
  147. /* Prepare the request. Do not cleanup the request after the service call,
  148. * as the members are stack-allocated or point into the server config. */
  149. UA_RegisterServer2Request request;
  150. UA_RegisterServer2Request_init(&request);
  151. request.requestHeader.timestamp = UA_DateTime_now();
  152. request.requestHeader.timeoutHint = 10000;
  153. initUaRegisterServer(&request.server);
  154. UA_MdnsDiscoveryConfiguration mdnsConfig;
  155. UA_MdnsDiscoveryConfiguration_init(&mdnsConfig);
  156. request.discoveryConfigurationSize = 1;
  157. request.discoveryConfiguration = UA_ExtensionObject_new();
  158. UA_ExtensionObject_init(&request.discoveryConfiguration[0]);
  159. request.discoveryConfiguration[0].encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
  160. request.discoveryConfiguration[0].content.decoded.type = &UA_TYPES[UA_TYPES_MDNSDISCOVERYCONFIGURATION];
  161. request.discoveryConfiguration[0].content.decoded.data = &mdnsConfig;
  162. mdnsConfig.mdnsServerName = server->config.mdnsServerName;
  163. mdnsConfig.serverCapabilities = server->config.serverCapabilities;
  164. mdnsConfig.serverCapabilitiesSize = server->config.serverCapabilitiesSize;
  165. // First try with RegisterServer2, if that isn't implemented, use RegisterServer
  166. UA_RegisterServer2Response response;
  167. UA_RegisterServer2Response_init(&response);
  168. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERSERVER2REQUEST],
  169. &response, &UA_TYPES[UA_TYPES_REGISTERSERVER2RESPONSE]);
  170. ASSERT_GOOD(response.responseHeader.serviceResult);
  171. UA_free(request.server.discoveryUrls);
  172. UA_ExtensionObject_delete(request.discoveryConfiguration);
  173. UA_RegisterServer2Response_deleteMembers(&response);
  174. return UA_STATUSCODE_GOOD;
  175. }
  176. static UA_StatusCode
  177. readValueRequest(UA_Client *client) {
  178. UA_ReadValueId rvi;
  179. UA_ReadValueId_init(&rvi);
  180. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STARTTIME);
  181. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  182. UA_DataValue resp = UA_Server_read(server, &rvi, UA_TIMESTAMPSTORETURN_BOTH);
  183. ASSERT_GOOD(resp.status);
  184. UA_DataValue_deleteMembers(&resp);
  185. return UA_STATUSCODE_GOOD;
  186. }
  187. static UA_StatusCode
  188. writeValueRequest(UA_Client *client) {
  189. UA_WriteValue wValue;
  190. UA_WriteValue_init(&wValue);
  191. UA_LocalizedText testValue = UA_LOCALIZEDTEXT("en-EN", "MyServer");
  192. UA_Variant_setScalar(&wValue.value.value, &testValue, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  193. wValue.value.hasValue = true;
  194. wValue.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  195. wValue.attributeId = UA_ATTRIBUTEID_DISPLAYNAME;
  196. ASSERT_GOOD(UA_Server_write(server, &wValue));
  197. return UA_STATUSCODE_GOOD;
  198. }
  199. static UA_StatusCode
  200. browseAndBrowseNextRequest(UA_Client *client) {
  201. // Browse node in server folder
  202. UA_BrowseRequest bReq;
  203. UA_BrowseRequest_init(&bReq);
  204. // normally is set to 0, to get all the nodes, but we want to test browse next
  205. bReq.requestedMaxReferencesPerNode = 1;
  206. bReq.nodesToBrowse = UA_BrowseDescription_new();
  207. bReq.nodesToBrowseSize = 1;
  208. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  209. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL;
  210. UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
  211. ASSERT_GOOD(bResp.responseHeader.serviceResult);
  212. // browse next
  213. UA_BrowseNextRequest bNextReq;
  214. UA_BrowseNextRequest_init(&bNextReq);
  215. // normally is set to 0, to get all the nodes, but we want to test browse next
  216. bNextReq.releaseContinuationPoints = UA_FALSE;
  217. bNextReq.continuationPoints = &bResp.results[0].continuationPoint;
  218. bNextReq.continuationPointsSize = 1;
  219. UA_BrowseNextResponse bNextResp = UA_Client_Service_browseNext(client, bNextReq);
  220. ASSERT_GOOD(bNextResp.responseHeader.serviceResult);
  221. UA_BrowseNextResponse_deleteMembers(&bNextResp);
  222. bNextResp = UA_Client_Service_browseNext(client, bNextReq);
  223. ASSERT_GOOD(bNextResp.responseHeader.serviceResult);
  224. UA_BrowseNextResponse_deleteMembers(&bNextResp);
  225. // release continuation point. Result is then empty
  226. bNextReq.releaseContinuationPoints = UA_TRUE;
  227. bNextResp = UA_Client_Service_browseNext(client, bNextReq);
  228. UA_BrowseNextResponse_deleteMembers(&bNextResp);
  229. ASSERT_GOOD(bNextResp.responseHeader.serviceResult);
  230. UA_BrowseRequest_deleteMembers(&bReq);
  231. UA_BrowseResponse_deleteMembers(&bResp);
  232. // already deleted by browse request
  233. bNextReq.continuationPoints = NULL;
  234. bNextReq.continuationPointsSize = 0;
  235. UA_BrowseNextRequest_deleteMembers(&bNextReq);
  236. return UA_STATUSCODE_GOOD;
  237. }
  238. static UA_StatusCode
  239. registerUnregisterNodesRequest(UA_Client *client) {
  240. UA_RegisterNodesRequest req;
  241. UA_RegisterNodesRequest_init(&req);
  242. req.nodesToRegister = UA_NodeId_new();
  243. req.nodesToRegister[0] = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  244. req.nodesToRegisterSize = 1;
  245. UA_RegisterNodesResponse res = UA_Client_Service_registerNodes(client, req);
  246. ASSERT_GOOD(res.responseHeader.serviceResult);
  247. UA_UnregisterNodesRequest reqUn;
  248. UA_UnregisterNodesRequest_init(&reqUn);
  249. reqUn.nodesToUnregister = UA_NodeId_new();
  250. reqUn.nodesToUnregister[0] = res.registeredNodeIds[0];
  251. reqUn.nodesToUnregisterSize = 1;
  252. UA_UnregisterNodesResponse resUn = UA_Client_Service_unregisterNodes(client, reqUn);
  253. ASSERT_GOOD(resUn.responseHeader.serviceResult);
  254. UA_UnregisterNodesRequest_deleteMembers(&reqUn);
  255. UA_UnregisterNodesResponse_deleteMembers(&resUn);
  256. UA_RegisterNodesRequest_deleteMembers(&req);
  257. UA_RegisterNodesResponse_deleteMembers(&res);
  258. return UA_STATUSCODE_GOOD;
  259. }
  260. static UA_StatusCode
  261. translateBrowsePathsToNodeIdsRequest(UA_Client *client) {
  262. // Just for testing we want to translate the following path to its corresponding node id
  263. // /Objects/Server/ServerStatus/State
  264. // Equals the following node IDs:
  265. // /85/2253/2256/2259
  266. #define BROWSE_PATHS_SIZE 3
  267. char *paths[BROWSE_PATHS_SIZE] = {"Server", "ServerStatus", "State"};
  268. UA_UInt32 ids[BROWSE_PATHS_SIZE] = {UA_NS0ID_ORGANIZES, UA_NS0ID_HASCOMPONENT, UA_NS0ID_HASCOMPONENT};
  269. UA_BrowsePath browsePath;
  270. UA_BrowsePath_init(&browsePath);
  271. browsePath.startingNode = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  272. browsePath.relativePath.elements = (UA_RelativePathElement*)UA_Array_new(BROWSE_PATHS_SIZE, &UA_TYPES[UA_TYPES_RELATIVEPATHELEMENT]);
  273. browsePath.relativePath.elementsSize = BROWSE_PATHS_SIZE;
  274. for(size_t i = 0; i < BROWSE_PATHS_SIZE; i++) {
  275. UA_RelativePathElement *elem = &browsePath.relativePath.elements[i];
  276. elem->referenceTypeId = UA_NODEID_NUMERIC(0, ids[i]);
  277. elem->targetName = UA_QUALIFIEDNAME_ALLOC(0, paths[i]);
  278. }
  279. UA_TranslateBrowsePathsToNodeIdsRequest request;
  280. UA_TranslateBrowsePathsToNodeIdsRequest_init(&request);
  281. request.browsePaths = &browsePath;
  282. request.browsePathsSize = 1;
  283. UA_TranslateBrowsePathsToNodeIdsResponse response = UA_Client_Service_translateBrowsePathsToNodeIds(client, request);
  284. ASSERT_GOOD(response.responseHeader.serviceResult);
  285. UA_BrowsePath_deleteMembers(&browsePath);
  286. UA_TranslateBrowsePathsToNodeIdsResponse_deleteMembers(&response);
  287. return UA_STATUSCODE_GOOD;
  288. }
  289. static void
  290. monitoredItemHandler(UA_Client *client, UA_UInt32 subId, void *subContext,
  291. UA_UInt32 monId, void *monContext, UA_DataValue *value) {
  292. }
  293. static UA_StatusCode
  294. subscriptionRequests(UA_Client *client) {
  295. UA_UInt32 subId;
  296. // createSubscriptionRequest
  297. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  298. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  299. NULL, NULL, NULL);
  300. ASSERT_GOOD(response.responseHeader.serviceResult);
  301. subId = response.subscriptionId;
  302. // modifySubscription
  303. UA_ModifySubscriptionRequest modifySubscriptionRequest;
  304. UA_ModifySubscriptionRequest_init(&modifySubscriptionRequest);
  305. modifySubscriptionRequest.subscriptionId = subId;
  306. modifySubscriptionRequest.maxNotificationsPerPublish = request.maxNotificationsPerPublish;
  307. modifySubscriptionRequest.priority = request.priority;
  308. modifySubscriptionRequest.requestedLifetimeCount = request.requestedLifetimeCount;
  309. modifySubscriptionRequest.requestedMaxKeepAliveCount = request.requestedMaxKeepAliveCount;
  310. modifySubscriptionRequest.requestedPublishingInterval = request.requestedPublishingInterval;
  311. UA_ModifySubscriptionResponse modifySubscriptionResponse;
  312. __UA_Client_Service(client, &modifySubscriptionRequest, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONREQUEST],
  313. &modifySubscriptionResponse, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONRESPONSE]);
  314. ASSERT_GOOD(modifySubscriptionResponse.responseHeader.serviceResult);
  315. UA_ModifySubscriptionRequest_deleteMembers(&modifySubscriptionRequest);
  316. UA_ModifySubscriptionResponse_deleteMembers(&modifySubscriptionResponse);
  317. // setPublishingMode
  318. UA_SetPublishingModeRequest setPublishingModeRequest;
  319. UA_SetPublishingModeRequest_init(&setPublishingModeRequest);
  320. setPublishingModeRequest.subscriptionIdsSize = 1;
  321. setPublishingModeRequest.subscriptionIds = UA_UInt32_new();
  322. setPublishingModeRequest.subscriptionIds[0] = subId;
  323. setPublishingModeRequest.publishingEnabled = UA_TRUE;
  324. UA_SetPublishingModeResponse setPublishingModeResponse;
  325. __UA_Client_Service(client, &setPublishingModeRequest, &UA_TYPES[UA_TYPES_SETPUBLISHINGMODEREQUEST],
  326. &setPublishingModeResponse, &UA_TYPES[UA_TYPES_SETPUBLISHINGMODERESPONSE]);
  327. ASSERT_GOOD(setPublishingModeResponse.responseHeader.serviceResult);
  328. UA_SetPublishingModeRequest_deleteMembers(&setPublishingModeRequest);
  329. UA_SetPublishingModeResponse_deleteMembers(&setPublishingModeResponse);
  330. // createMonitoredItemsRequest
  331. UA_UInt32 monId;
  332. UA_MonitoredItemCreateRequest monRequest =
  333. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  334. UA_MonitoredItemCreateResult monResponse =
  335. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  336. UA_TIMESTAMPSTORETURN_BOTH,
  337. monRequest, NULL, monitoredItemHandler, NULL);
  338. ASSERT_GOOD(monResponse.statusCode);
  339. monId = monResponse.monitoredItemId;
  340. // publishRequest
  341. ASSERT_GOOD(UA_Client_Subscriptions_manuallySendPublishRequest(client));
  342. // republishRequest
  343. UA_RepublishRequest republishRequest;
  344. UA_RepublishRequest_init(&republishRequest);
  345. republishRequest.retransmitSequenceNumber = 0;
  346. republishRequest.subscriptionId = subId;
  347. UA_RepublishResponse republishResponse;
  348. __UA_Client_Service(client, &republishRequest, &UA_TYPES[UA_TYPES_REPUBLISHREQUEST],
  349. &republishResponse, &UA_TYPES[UA_TYPES_REPUBLISHRESPONSE]);
  350. // here we don't care about the return value since it may be UA_STATUSCODE_BADMESSAGENOTAVAILABLE
  351. // ASSERT_GOOD(republishResponse.responseHeader.serviceResult);
  352. UA_RepublishRequest_deleteMembers(&republishRequest);
  353. UA_RepublishResponse_deleteMembers(&republishResponse);
  354. // modifyMonitoredItems
  355. UA_ModifyMonitoredItemsRequest modifyMonitoredItemsRequest;
  356. UA_ModifyMonitoredItemsRequest_init(&modifyMonitoredItemsRequest);
  357. modifyMonitoredItemsRequest.subscriptionId = subId;
  358. modifyMonitoredItemsRequest.itemsToModifySize = 1;
  359. modifyMonitoredItemsRequest.itemsToModify = UA_MonitoredItemModifyRequest_new();
  360. modifyMonitoredItemsRequest.itemsToModify[0].monitoredItemId = monId;
  361. UA_MonitoringParameters_init(&modifyMonitoredItemsRequest.itemsToModify[0].requestedParameters);
  362. UA_ModifyMonitoredItemsResponse modifyMonitoredItemsResponse;
  363. __UA_Client_Service(client, &modifyMonitoredItemsRequest, &UA_TYPES[UA_TYPES_MODIFYMONITOREDITEMSREQUEST],
  364. &modifyMonitoredItemsResponse, &UA_TYPES[UA_TYPES_MODIFYMONITOREDITEMSRESPONSE]);
  365. ASSERT_GOOD(modifyMonitoredItemsResponse.responseHeader.serviceResult);
  366. UA_ModifyMonitoredItemsRequest_deleteMembers(&modifyMonitoredItemsRequest);
  367. UA_ModifyMonitoredItemsResponse_deleteMembers(&modifyMonitoredItemsResponse);
  368. // setMonitoringMode
  369. UA_SetMonitoringModeRequest setMonitoringModeRequest;
  370. UA_SetMonitoringModeRequest_init(&setMonitoringModeRequest);
  371. setMonitoringModeRequest.subscriptionId = subId;
  372. setMonitoringModeRequest.monitoredItemIdsSize = 1;
  373. setMonitoringModeRequest.monitoredItemIds = UA_UInt32_new();
  374. setMonitoringModeRequest.monitoredItemIds[0] = monId;
  375. setMonitoringModeRequest.monitoringMode = UA_MONITORINGMODE_REPORTING;
  376. UA_SetMonitoringModeResponse setMonitoringModeResponse;
  377. __UA_Client_Service(client, &setMonitoringModeRequest, &UA_TYPES[UA_TYPES_SETMONITORINGMODEREQUEST],
  378. &setMonitoringModeResponse, &UA_TYPES[UA_TYPES_SETMONITORINGMODERESPONSE]);
  379. ASSERT_GOOD(setMonitoringModeResponse.responseHeader.serviceResult);
  380. UA_SetMonitoringModeRequest_deleteMembers(&setMonitoringModeRequest);
  381. UA_SetMonitoringModeResponse_deleteMembers(&setMonitoringModeResponse);
  382. // deleteMonitoredItemsRequest
  383. ASSERT_GOOD(UA_Client_MonitoredItems_deleteSingle(client, subId, monId));
  384. // deleteSubscriptionRequest
  385. ASSERT_GOOD(UA_Client_Subscriptions_deleteSingle(client, subId));
  386. return UA_STATUSCODE_GOOD;
  387. }
  388. static UA_StatusCode
  389. callRequest(UA_Client *client) {
  390. /* Set up the request */
  391. UA_CallRequest request;
  392. UA_CallRequest_init(&request);
  393. UA_CallMethodRequest item;
  394. UA_CallMethodRequest_init(&item);
  395. item.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS);
  396. item.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  397. UA_Variant input;
  398. UA_UInt32 subId = 12345;
  399. UA_Variant_init(&input);
  400. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  401. item.inputArgumentsSize = 1;
  402. item.inputArguments = &input;
  403. request.methodsToCall = &item;
  404. request.methodsToCallSize = 1;
  405. /* Call the service */
  406. UA_CallResponse response = UA_Client_Service_call(client, request);
  407. ASSERT_GOOD(response.responseHeader.serviceResult);
  408. UA_CallResponse_deleteMembers(&response);
  409. UA_Variant_deleteMembers(&input);
  410. return UA_STATUSCODE_GOOD;
  411. }
  412. static UA_StatusCode
  413. nodemanagementRequests(UA_Client *client) {
  414. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  415. attr.description = UA_LOCALIZEDTEXT("en-US", "Some Coordinates");
  416. attr.displayName = UA_LOCALIZEDTEXT("en-US", "Coordinates");
  417. UA_NodeId newObjectId;
  418. ASSERT_GOOD(UA_Client_addObjectNode(client, UA_NODEID_NULL,
  419. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  420. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  421. UA_QUALIFIEDNAME(1, "Coordinates"),
  422. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), attr, &newObjectId));
  423. UA_ExpandedNodeId target = UA_EXPANDEDNODEID_NULL;
  424. target.nodeId = newObjectId;
  425. ASSERT_GOOD(UA_Client_addReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_ROOTFOLDER),
  426. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  427. UA_TRUE, UA_STRING_NULL, target, UA_NODECLASS_OBJECT));
  428. ASSERT_GOOD(UA_Client_deleteReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_ROOTFOLDER),
  429. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  430. true, target, true));
  431. ASSERT_GOOD(UA_Client_deleteNode(client, newObjectId, UA_TRUE));
  432. return UA_STATUSCODE_GOOD;
  433. }
  434. static UA_StatusCode
  435. executeClientServices(UA_Client *client) {
  436. ASSERT_GOOD(findServersRequest(client));
  437. ASSERT_GOOD(findServersOnNetworkRequest(client));
  438. ASSERT_GOOD(registerServerRequest(client));
  439. ASSERT_GOOD(registerServer2Request(client));
  440. ASSERT_GOOD(readValueRequest(client));
  441. ASSERT_GOOD(writeValueRequest(client));
  442. ASSERT_GOOD(browseAndBrowseNextRequest(client));
  443. ASSERT_GOOD(registerUnregisterNodesRequest(client));
  444. ASSERT_GOOD(translateBrowsePathsToNodeIdsRequest(client));
  445. ASSERT_GOOD(subscriptionRequests(client));
  446. ASSERT_GOOD(callRequest(client));
  447. ASSERT_GOOD(nodemanagementRequests(client));
  448. return UA_STATUSCODE_GOOD;
  449. }
  450. int main(void) {
  451. emptyCorpusDir();
  452. start_server();
  453. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  454. // this will also call getEndpointsRequest
  455. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  456. if(retval == UA_STATUSCODE_GOOD)
  457. retval = executeClientServices(client);
  458. UA_Client_disconnect(client);
  459. UA_Client_delete(client);
  460. if(retval == UA_STATUSCODE_GOOD) {
  461. // now also connect with user/pass so that fuzzer also knows how to do that
  462. client = UA_Client_new(UA_ClientConfig_default);
  463. retval = UA_Client_connect_username(client, "opc.tcp://localhost:4840", "user", "password");
  464. retval = retval == UA_STATUSCODE_BADUSERACCESSDENIED ? UA_STATUSCODE_GOOD : retval;
  465. UA_Client_disconnect(client);
  466. UA_Client_delete(client);
  467. }
  468. teardown_server();
  469. if(retval != UA_STATUSCODE_GOOD) {
  470. printf("\n--------- AN ERROR OCCURRED ----------\nStatus = %s\n", UA_StatusCode_name(retval));
  471. exit(1);
  472. } else {
  473. printf("\n--------- SUCCESS -------\nThe corpus is stored in %s", UA_CORPUS_OUTPUT_DIR);
  474. }
  475. return 0;
  476. }