check_client_subscriptions.c 24 KB

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