check_client_subscriptions.c 24 KB

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