corpus_generator.c 24 KB

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