check_client_subscriptions.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "ua_types.h"
  7. #include "ua_server.h"
  8. #include "ua_client.h"
  9. #include "client/ua_client_internal.h"
  10. #include "ua_client_highlevel.h"
  11. #include "ua_config_default.h"
  12. #include "ua_network_tcp.h"
  13. #include "check.h"
  14. #include "testing_clock.h"
  15. #include "testing_networklayers.h"
  16. #include "thread_wrapper.h"
  17. UA_Server *server;
  18. UA_ServerConfig *config;
  19. UA_Boolean *running;
  20. UA_ServerNetworkLayer nl;
  21. THREAD_HANDLE server_thread;
  22. THREAD_CALLBACK(serverloop) {
  23. while(*running)
  24. UA_Server_run_iterate(server, true);
  25. return 0;
  26. }
  27. static void setup(void) {
  28. running = UA_Boolean_new();
  29. *running = true;
  30. config = UA_ServerConfig_new_default();
  31. config->maxPublishReqPerSession = 8;
  32. server = UA_Server_new(config);
  33. UA_Server_run_startup(server);
  34. THREAD_CREATE(server_thread, serverloop);
  35. }
  36. static void teardown(void) {
  37. *running = false;
  38. THREAD_JOIN(server_thread);
  39. UA_Server_run_shutdown(server);
  40. UA_Boolean_delete(running);
  41. UA_Server_delete(server);
  42. UA_ServerConfig_delete(config);
  43. }
  44. #ifdef UA_ENABLE_SUBSCRIPTIONS
  45. UA_Boolean notificationReceived = false;
  46. UA_UInt32 countNotificationReceived = 0;
  47. UA_Double publishingInterval = 500.0;
  48. static void
  49. dataChangeHandler(UA_Client *client, UA_UInt32 subId, void *subContext,
  50. UA_UInt32 monId, void *monContext, UA_DataValue *value) {
  51. notificationReceived = true;
  52. countNotificationReceived++;
  53. }
  54. static void
  55. dataChangeHandlerSubSleep(UA_Client *client, UA_UInt32 subId, void *subContext,
  56. UA_UInt32 monId, void *monContext, UA_DataValue *value) {
  57. notificationReceived = true;
  58. countNotificationReceived++;
  59. UA_fakeSleep((UA_UInt32)(publishingInterval + 2));
  60. }
  61. START_TEST(Client_subscription) {
  62. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  63. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  64. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  65. UA_Client_recv = client->connection.recv;
  66. client->connection.recv = UA_Client_recvTesting;
  67. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  68. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  69. NULL, NULL, NULL);
  70. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  71. UA_UInt32 subId = response.subscriptionId;
  72. /* monitor the server state */
  73. UA_MonitoredItemCreateRequest monRequest =
  74. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  75. UA_MonitoredItemCreateResult monResponse =
  76. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  77. UA_TIMESTAMPSTORETURN_BOTH,
  78. monRequest, NULL, dataChangeHandler, NULL);
  79. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  80. UA_UInt32 monId = monResponse.monitoredItemId;
  81. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  82. notificationReceived = false;
  83. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  84. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  85. ck_assert_uint_eq(notificationReceived, true);
  86. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  87. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  88. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  89. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  90. UA_Client_disconnect(client);
  91. UA_Client_delete(client);
  92. }
  93. END_TEST
  94. START_TEST(Client_subscription_createDataChanges) {
  95. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  96. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  97. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  98. UA_Client_recv = client->connection.recv;
  99. client->connection.recv = UA_Client_recvTesting;
  100. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  101. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  102. NULL, NULL, NULL);
  103. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  104. UA_UInt32 subId = response.subscriptionId;
  105. UA_MonitoredItemCreateRequest items[3];
  106. UA_UInt32 newMonitoredItemIds[3];
  107. UA_Client_DataChangeNotificationCallback callbacks[3];
  108. UA_Client_DeleteMonitoredItemCallback deleteCallbacks[3];
  109. void *contexts[3];
  110. /* monitor the server state */
  111. items[0] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  112. callbacks[0] = dataChangeHandler;
  113. contexts[0] = NULL;
  114. deleteCallbacks[0] = NULL;
  115. /* monitor invalid node */
  116. items[1] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, 999999));
  117. callbacks[1] = dataChangeHandler;
  118. contexts[1] = NULL;
  119. deleteCallbacks[1] = NULL;
  120. /* monitor current time */
  121. items[2] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
  122. callbacks[2] = dataChangeHandler;
  123. contexts[2] = NULL;
  124. deleteCallbacks[2] = NULL;
  125. UA_CreateMonitoredItemsRequest createRequest;
  126. UA_CreateMonitoredItemsRequest_init(&createRequest);
  127. createRequest.subscriptionId = subId;
  128. createRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_BOTH;
  129. createRequest.itemsToCreate = items;
  130. createRequest.itemsToCreateSize = 3;
  131. UA_CreateMonitoredItemsResponse createResponse =
  132. UA_Client_MonitoredItems_createDataChanges(client, createRequest, contexts,
  133. callbacks, deleteCallbacks);
  134. ck_assert_uint_eq(createResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  135. ck_assert_uint_eq(createResponse.resultsSize, 3);
  136. ck_assert_uint_eq(createResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  137. newMonitoredItemIds[0] = createResponse.results[0].monitoredItemId;
  138. ck_assert_uint_eq(createResponse.results[1].statusCode, UA_STATUSCODE_BADNODEIDUNKNOWN);
  139. newMonitoredItemIds[1] = createResponse.results[1].monitoredItemId;
  140. ck_assert_uint_eq(newMonitoredItemIds[1], 0);
  141. ck_assert_uint_eq(createResponse.results[2].statusCode, UA_STATUSCODE_GOOD);
  142. newMonitoredItemIds[2] = createResponse.results[2].monitoredItemId;
  143. ck_assert_uint_eq(createResponse.results[2].statusCode, UA_STATUSCODE_GOOD);
  144. UA_CreateMonitoredItemsResponse_deleteMembers(&createResponse);
  145. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  146. notificationReceived = false;
  147. countNotificationReceived = 0;
  148. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  149. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  150. ck_assert_uint_eq(notificationReceived, true);
  151. ck_assert_uint_eq(countNotificationReceived, 2);
  152. notificationReceived = false;
  153. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  154. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  155. ck_assert_uint_eq(notificationReceived, true);
  156. ck_assert_uint_eq(countNotificationReceived, 3);
  157. UA_DeleteMonitoredItemsRequest deleteRequest;
  158. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  159. deleteRequest.subscriptionId = subId;
  160. deleteRequest.monitoredItemIds = newMonitoredItemIds;
  161. deleteRequest.monitoredItemIdsSize = 3;
  162. UA_DeleteMonitoredItemsResponse deleteResponse =
  163. UA_Client_MonitoredItems_delete(client, deleteRequest);
  164. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  165. ck_assert_uint_eq(deleteResponse.resultsSize, 3);
  166. ck_assert_uint_eq(deleteResponse.results[0], UA_STATUSCODE_GOOD);
  167. ck_assert_uint_eq(deleteResponse.results[1], UA_STATUSCODE_BADMONITOREDITEMIDINVALID);
  168. ck_assert_uint_eq(deleteResponse.results[2], UA_STATUSCODE_GOOD);
  169. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  170. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  171. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  172. UA_Client_disconnect(client);
  173. UA_Client_delete(client);
  174. }
  175. END_TEST
  176. START_TEST(Client_subscription_keepAlive) {
  177. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  178. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  179. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  180. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  181. request.requestedMaxKeepAliveCount = 1;
  182. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  183. NULL, NULL, NULL);
  184. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  185. UA_UInt32 subId = response.subscriptionId;
  186. /* monitor the server state */
  187. UA_MonitoredItemCreateRequest monRequest =
  188. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  189. UA_MonitoredItemCreateResult monResponse =
  190. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  191. UA_TIMESTAMPSTORETURN_BOTH,
  192. monRequest, NULL, dataChangeHandler, NULL);
  193. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  194. UA_UInt32 monId = monResponse.monitoredItemId;
  195. UA_fakeSleep((UA_UInt32)(publishingInterval + 1));
  196. /* Manually send a publish request */
  197. UA_PublishRequest pr;
  198. UA_PublishRequest_init(&pr);
  199. pr.subscriptionAcknowledgementsSize = 0;
  200. UA_PublishResponse presponse;
  201. UA_PublishResponse_init(&presponse);
  202. __UA_Client_Service(client, &pr, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  203. &presponse, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  204. ck_assert_uint_eq(presponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  205. ck_assert_uint_eq(presponse.notificationMessage.notificationDataSize, 1);
  206. UA_PublishResponse_deleteMembers(&presponse);
  207. UA_PublishRequest_deleteMembers(&pr);
  208. UA_fakeSleep((UA_UInt32)(publishingInterval + 1));
  209. UA_PublishRequest_init(&pr);
  210. pr.subscriptionAcknowledgementsSize = 0;
  211. UA_PublishResponse_init(&presponse);
  212. __UA_Client_Service(client, &pr, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  213. &presponse, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  214. ck_assert_uint_eq(presponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  215. ck_assert_uint_eq(presponse.notificationMessage.notificationDataSize, 0);
  216. UA_PublishResponse_deleteMembers(&presponse);
  217. UA_PublishRequest_deleteMembers(&pr);
  218. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  219. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  220. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  221. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  222. UA_Client_disconnect(client);
  223. UA_Client_delete(client);
  224. }
  225. END_TEST
  226. START_TEST(Client_subscription_connectionClose) {
  227. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  228. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  229. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  230. UA_Client_recv = client->connection.recv;
  231. client->connection.recv = UA_Client_recvTesting;
  232. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  233. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  234. NULL, NULL, NULL);
  235. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  236. /* monitor the server state */
  237. UA_MonitoredItemCreateRequest monRequest =
  238. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  239. UA_MonitoredItemCreateResult monResponse =
  240. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  241. UA_TIMESTAMPSTORETURN_BOTH,
  242. monRequest, NULL, dataChangeHandler, NULL);
  243. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  244. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  245. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 60));
  246. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  247. /* Simulate BADCONNECTIONCLOSE */
  248. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  249. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 60));
  250. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  251. UA_Client_disconnect(client);
  252. UA_Client_delete(client);
  253. }
  254. END_TEST
  255. START_TEST(Client_subscription_without_notification) {
  256. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  257. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  258. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  259. UA_Client_recv = client->connection.recv;
  260. client->connection.recv = UA_Client_recvTesting;
  261. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  262. request.requestedMaxKeepAliveCount = 1;
  263. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  264. NULL, NULL, NULL);
  265. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  266. UA_UInt32 subId = response.subscriptionId;
  267. /* monitor the server state */
  268. UA_MonitoredItemCreateRequest monRequest =
  269. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  270. monRequest.requestedParameters.samplingInterval = 99999999.0;
  271. UA_MonitoredItemCreateResult monResponse =
  272. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  273. UA_TIMESTAMPSTORETURN_BOTH,
  274. monRequest, NULL, dataChangeHandler, NULL);
  275. UA_UInt32 monId = monResponse.monitoredItemId;
  276. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  277. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  278. notificationReceived = false;
  279. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  280. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  281. ck_assert_uint_eq(notificationReceived, true);
  282. notificationReceived = false;
  283. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  284. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  285. ck_assert_uint_eq(notificationReceived, false);
  286. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  287. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  288. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  289. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  290. UA_Client_disconnect(client);
  291. UA_Client_delete(client);
  292. }
  293. END_TEST
  294. static UA_ClientState callbackClientState;
  295. static void
  296. stateCallback (UA_Client *client, UA_ClientState clientState){
  297. callbackClientState = clientState;
  298. if (clientState == UA_CLIENTSTATE_SESSION){
  299. /* A new session was created. We need to create the subscription. */
  300. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  301. request.requestedMaxKeepAliveCount = 1;
  302. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  303. NULL, NULL, NULL);
  304. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  305. UA_UInt32 subId = response.subscriptionId;
  306. ck_assert_uint_ne(subId, 0);
  307. /* Add a MonitoredItem */
  308. UA_MonitoredItemCreateRequest monRequest =
  309. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
  310. UA_MonitoredItemCreateResult monResponse =
  311. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  312. UA_TIMESTAMPSTORETURN_BOTH,
  313. monRequest, NULL, dataChangeHandlerSubSleep, NULL);
  314. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  315. UA_UInt32 monId = monResponse.monitoredItemId;
  316. ck_assert_uint_ne(monId, 0);
  317. }
  318. }
  319. START_TEST(Client_subscription_async_sub) {
  320. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  321. /* Set stateCallback */
  322. clientConfig.stateCallback = stateCallback;
  323. /* Activate background publish request */
  324. clientConfig.outStandingPublishRequests = 10;
  325. UA_Client *client = UA_Client_new(clientConfig);
  326. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_DISCONNECTED);
  327. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  328. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  329. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  330. UA_Client_recv = client->connection.recv;
  331. client->connection.recv = UA_Client_recvTesting;
  332. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  333. countNotificationReceived = 0;
  334. notificationReceived = false;
  335. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  336. ck_assert_uint_eq(notificationReceived, true);
  337. ck_assert_uint_eq(countNotificationReceived, 1);
  338. notificationReceived = false;
  339. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  340. ck_assert_uint_eq(notificationReceived, true);
  341. ck_assert_uint_eq(countNotificationReceived, 2);
  342. notificationReceived = false;
  343. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  344. ck_assert_uint_eq(notificationReceived, true);
  345. ck_assert_uint_eq(countNotificationReceived, 3);
  346. notificationReceived = false;
  347. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  348. ck_assert_uint_eq(notificationReceived, true);
  349. ck_assert_uint_eq(countNotificationReceived, 4);
  350. notificationReceived = false;
  351. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  352. ck_assert_uint_eq(notificationReceived, true);
  353. ck_assert_uint_eq(countNotificationReceived, 5);
  354. ck_assert_uint_lt(client->config.outStandingPublishRequests, 10);
  355. notificationReceived = false;
  356. /* Simulate network cable unplugged (no response from server) */
  357. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  358. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  359. ck_assert_uint_eq(notificationReceived, false);
  360. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  361. /* Simulate network cable unplugged (no response from server) */
  362. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  363. UA_Client_runAsync(client, (UA_UInt16)clientConfig.timeout);
  364. ck_assert_uint_eq(notificationReceived, false);
  365. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_DISCONNECTED);
  366. UA_Client_delete(client);
  367. }
  368. END_TEST
  369. START_TEST(Client_methodcall) {
  370. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  371. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  372. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  373. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  374. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  375. NULL, NULL, NULL);
  376. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  377. UA_UInt32 subId = response.subscriptionId;
  378. /* monitor the server state */
  379. UA_MonitoredItemCreateRequest monRequest =
  380. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  381. UA_MonitoredItemCreateResult monResponse =
  382. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  383. UA_TIMESTAMPSTORETURN_BOTH,
  384. monRequest, NULL, NULL, NULL);
  385. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  386. UA_UInt32 monId = monResponse.monitoredItemId;
  387. /* call a method to get monitored item id */
  388. UA_Variant input;
  389. UA_Variant_init(&input);
  390. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  391. size_t outputSize;
  392. UA_Variant *output;
  393. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  394. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  395. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  396. ck_assert_uint_eq(outputSize, 2);
  397. ck_assert_uint_eq(output[0].arrayLength, 1);
  398. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  399. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  400. UA_Variant_deleteMembers(&input);
  401. /* call with invalid subscription id */
  402. UA_Variant_init(&input);
  403. subId = 0;
  404. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  405. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  406. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  407. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  408. UA_Variant_deleteMembers(&input);
  409. UA_Client_disconnect(client);
  410. UA_Client_delete(client);
  411. }
  412. END_TEST
  413. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  414. static Suite* testSuite_Client(void) {
  415. TCase *tc_client = tcase_create("Client Subscription Basic");
  416. tcase_add_checked_fixture(tc_client, setup, teardown);
  417. #ifdef UA_ENABLE_SUBSCRIPTIONS
  418. tcase_add_test(tc_client, Client_subscription);
  419. tcase_add_test(tc_client, Client_subscription_connectionClose);
  420. tcase_add_test(tc_client, Client_subscription_createDataChanges);
  421. tcase_add_test(tc_client, Client_subscription_keepAlive);
  422. tcase_add_test(tc_client, Client_subscription_without_notification);
  423. tcase_add_test(tc_client, Client_subscription_async_sub);
  424. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  425. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  426. tcase_add_checked_fixture(tc_client2, setup, teardown);
  427. #ifdef UA_ENABLE_SUBSCRIPTIONS
  428. tcase_add_test(tc_client2, Client_methodcall);
  429. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  430. Suite *s = suite_create("Client Subscription");
  431. suite_add_tcase(s,tc_client);
  432. suite_add_tcase(s,tc_client2);
  433. return s;
  434. }
  435. int main(void) {
  436. Suite *s = testSuite_Client();
  437. SRunner *sr = srunner_create(s);
  438. srunner_set_fork_status(sr, CK_NOFORK);
  439. srunner_run_all(sr,CK_NORMAL);
  440. int number_failed = srunner_ntests_failed(sr);
  441. srunner_free(sr);
  442. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  443. }