check_client_subscriptions.c 23 KB

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