check_client_subscriptions.c 23 KB

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