check_client_subscriptions.c 24 KB

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