corpus_generator.c 23 KB

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