corpus_generator.c 24 KB

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