check_client_subscriptions.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 <stdio.h>
  10. #include <stdlib.h>
  11. #include "check.h"
  12. #include "testing_clock.h"
  13. #include "testing_networklayers.h"
  14. #include "thread_wrapper.h"
  15. UA_Server *server;
  16. UA_Boolean running;
  17. UA_ServerNetworkLayer nl;
  18. THREAD_HANDLE server_thread;
  19. THREAD_CALLBACK(serverloop) {
  20. while(running)
  21. UA_Server_run_iterate(server, true);
  22. return 0;
  23. }
  24. static void setup(void) {
  25. running = true;
  26. server = UA_Server_new();
  27. UA_ServerConfig *config = UA_Server_getConfig(server);
  28. UA_ServerConfig_setDefault(config);
  29. config->maxPublishReqPerSession = 5;
  30. UA_Server_run_startup(server);
  31. THREAD_CREATE(server_thread, serverloop);
  32. }
  33. static void teardown(void) {
  34. running = false;
  35. THREAD_JOIN(server_thread);
  36. UA_Server_run_shutdown(server);
  37. UA_Server_delete(server);
  38. }
  39. #ifdef UA_ENABLE_SUBSCRIPTIONS
  40. UA_Boolean notificationReceived = false;
  41. UA_UInt32 countNotificationReceived = 0;
  42. UA_Double publishingInterval = 500.0;
  43. static void
  44. dataChangeHandler(UA_Client *client, UA_UInt32 subId, void *subContext,
  45. UA_UInt32 monId, void *monContext, UA_DataValue *value) {
  46. notificationReceived = true;
  47. countNotificationReceived++;
  48. }
  49. START_TEST(Client_subscription) {
  50. UA_Client *client = UA_Client_new();
  51. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  52. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  53. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  54. UA_Client_recv = client->connection.recv;
  55. client->connection.recv = UA_Client_recvTesting;
  56. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  57. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  58. NULL, NULL, NULL);
  59. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  60. UA_UInt32 subId = response.subscriptionId;
  61. // a valid UA_Client_Subscriptions_modify
  62. UA_ModifySubscriptionRequest modifySubscriptionRequest;
  63. UA_ModifySubscriptionRequest_init(&modifySubscriptionRequest);
  64. modifySubscriptionRequest.subscriptionId = response.subscriptionId;
  65. modifySubscriptionRequest.requestedPublishingInterval = response.revisedPublishingInterval;
  66. modifySubscriptionRequest.requestedLifetimeCount = response.revisedLifetimeCount;
  67. modifySubscriptionRequest.requestedMaxKeepAliveCount = response.revisedMaxKeepAliveCount;
  68. UA_ModifySubscriptionResponse modifySubscriptionResponse = UA_Client_Subscriptions_modify(client,modifySubscriptionRequest);
  69. ck_assert_int_eq(modifySubscriptionResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  70. // an invalid UA_Client_Subscriptions_modify
  71. modifySubscriptionRequest.subscriptionId = 99999; // invalid
  72. modifySubscriptionResponse = UA_Client_Subscriptions_modify(client,modifySubscriptionRequest);
  73. ck_assert_int_eq(modifySubscriptionResponse.responseHeader.serviceResult, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  74. /* monitor the server state */
  75. UA_MonitoredItemCreateRequest monRequest =
  76. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  77. UA_MonitoredItemCreateResult monResponse =
  78. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  79. UA_TIMESTAMPSTORETURN_BOTH,
  80. monRequest, NULL, dataChangeHandler, NULL);
  81. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  82. UA_UInt32 monId = monResponse.monitoredItemId;
  83. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  84. notificationReceived = false;
  85. retval = UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  86. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  87. ck_assert_uint_eq(notificationReceived, true);
  88. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  89. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  90. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  91. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  92. UA_Client_disconnect(client);
  93. UA_Client_delete(client);
  94. }
  95. END_TEST
  96. START_TEST(Client_subscription_createDataChanges) {
  97. UA_Client *client = UA_Client_new();
  98. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  99. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  100. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  101. UA_Client_recv = client->connection.recv;
  102. client->connection.recv = UA_Client_recvTesting;
  103. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  104. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  105. NULL, NULL, NULL);
  106. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  107. UA_UInt32 subId = response.subscriptionId;
  108. UA_MonitoredItemCreateRequest items[3];
  109. UA_UInt32 newMonitoredItemIds[3];
  110. UA_Client_DataChangeNotificationCallback callbacks[3];
  111. UA_Client_DeleteMonitoredItemCallback deleteCallbacks[3];
  112. void *contexts[3];
  113. /* monitor the server state */
  114. items[0] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  115. callbacks[0] = dataChangeHandler;
  116. contexts[0] = NULL;
  117. deleteCallbacks[0] = NULL;
  118. /* monitor invalid node */
  119. items[1] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, 999999));
  120. callbacks[1] = dataChangeHandler;
  121. contexts[1] = NULL;
  122. deleteCallbacks[1] = NULL;
  123. /* monitor current time */
  124. items[2] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
  125. callbacks[2] = dataChangeHandler;
  126. contexts[2] = NULL;
  127. deleteCallbacks[2] = NULL;
  128. UA_CreateMonitoredItemsRequest createRequest;
  129. UA_CreateMonitoredItemsRequest_init(&createRequest);
  130. createRequest.subscriptionId = subId;
  131. createRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_BOTH;
  132. createRequest.itemsToCreate = items;
  133. createRequest.itemsToCreateSize = 3;
  134. UA_CreateMonitoredItemsResponse createResponse =
  135. UA_Client_MonitoredItems_createDataChanges(client, createRequest, contexts,
  136. callbacks, deleteCallbacks);
  137. ck_assert_uint_eq(createResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  138. ck_assert_uint_eq(createResponse.resultsSize, 3);
  139. ck_assert_uint_eq(createResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  140. newMonitoredItemIds[0] = createResponse.results[0].monitoredItemId;
  141. ck_assert_uint_eq(createResponse.results[1].statusCode, UA_STATUSCODE_BADNODEIDUNKNOWN);
  142. newMonitoredItemIds[1] = createResponse.results[1].monitoredItemId;
  143. ck_assert_uint_eq(newMonitoredItemIds[1], 0);
  144. ck_assert_uint_eq(createResponse.results[2].statusCode, UA_STATUSCODE_GOOD);
  145. newMonitoredItemIds[2] = createResponse.results[2].monitoredItemId;
  146. ck_assert_uint_eq(createResponse.results[2].statusCode, UA_STATUSCODE_GOOD);
  147. UA_CreateMonitoredItemsResponse_deleteMembers(&createResponse);
  148. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  149. notificationReceived = false;
  150. countNotificationReceived = 0;
  151. retval = UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  152. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  153. ck_assert_uint_eq(notificationReceived, true);
  154. ck_assert_uint_eq(countNotificationReceived, 2);
  155. notificationReceived = false;
  156. retval = UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  157. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  158. ck_assert_uint_eq(notificationReceived, true);
  159. ck_assert_uint_eq(countNotificationReceived, 3);
  160. UA_DeleteMonitoredItemsRequest deleteRequest;
  161. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  162. deleteRequest.subscriptionId = subId;
  163. deleteRequest.monitoredItemIds = newMonitoredItemIds;
  164. deleteRequest.monitoredItemIdsSize = 3;
  165. UA_DeleteMonitoredItemsResponse deleteResponse =
  166. UA_Client_MonitoredItems_delete(client, deleteRequest);
  167. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  168. ck_assert_uint_eq(deleteResponse.resultsSize, 3);
  169. ck_assert_uint_eq(deleteResponse.results[0], UA_STATUSCODE_GOOD);
  170. ck_assert_uint_eq(deleteResponse.results[1], UA_STATUSCODE_BADMONITOREDITEMIDINVALID);
  171. ck_assert_uint_eq(deleteResponse.results[2], UA_STATUSCODE_GOOD);
  172. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  173. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  174. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  175. UA_Client_disconnect(client);
  176. UA_Client_delete(client);
  177. }
  178. END_TEST
  179. START_TEST(Client_subscription_keepAlive) {
  180. UA_Client *client = UA_Client_new();
  181. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  182. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  183. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  184. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  185. request.requestedMaxKeepAliveCount = 1;
  186. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  187. NULL, NULL, NULL);
  188. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  189. UA_UInt32 subId = response.subscriptionId;
  190. /* monitor the server state */
  191. UA_MonitoredItemCreateRequest monRequest =
  192. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  193. UA_MonitoredItemCreateResult monResponse =
  194. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  195. UA_TIMESTAMPSTORETURN_BOTH,
  196. monRequest, NULL, dataChangeHandler, NULL);
  197. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  198. UA_UInt32 monId = monResponse.monitoredItemId;
  199. /* Ensure that the subscription is late */
  200. UA_fakeSleep((UA_UInt32)(publishingInterval + 1));
  201. /* Manually send a publish request */
  202. UA_PublishRequest pr;
  203. UA_PublishRequest_init(&pr);
  204. pr.subscriptionAcknowledgementsSize = 0;
  205. UA_PublishResponse presponse;
  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, 1);
  211. UA_PublishResponse_deleteMembers(&presponse);
  212. UA_PublishRequest_deleteMembers(&pr);
  213. UA_fakeSleep((UA_UInt32)(publishingInterval + 1));
  214. UA_PublishRequest_init(&pr);
  215. pr.subscriptionAcknowledgementsSize = 0;
  216. UA_PublishResponse_init(&presponse);
  217. __UA_Client_Service(client, &pr, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  218. &presponse, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  219. ck_assert_uint_eq(presponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  220. ck_assert_uint_eq(presponse.notificationMessage.notificationDataSize, 0);
  221. UA_PublishResponse_deleteMembers(&presponse);
  222. UA_PublishRequest_deleteMembers(&pr);
  223. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  224. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  225. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  226. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  227. UA_Client_disconnect(client);
  228. UA_Client_delete(client);
  229. }
  230. END_TEST
  231. START_TEST(Client_subscription_connectionClose) {
  232. UA_Client *client = UA_Client_new();
  233. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  234. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  235. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  236. UA_Client_recv = client->connection.recv;
  237. client->connection.recv = UA_Client_recvTesting;
  238. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  239. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  240. NULL, NULL, NULL);
  241. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  242. /* monitor the server state */
  243. UA_MonitoredItemCreateRequest monRequest =
  244. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  245. UA_MonitoredItemCreateResult monResponse =
  246. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  247. UA_TIMESTAMPSTORETURN_BOTH,
  248. monRequest, NULL, dataChangeHandler, NULL);
  249. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  250. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  251. retval = UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 60));
  252. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  253. /* Simulate BADCONNECTIONCLOSE */
  254. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  255. retval = UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 60));
  256. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  257. UA_Client_disconnect(client);
  258. UA_Client_delete(client);
  259. }
  260. END_TEST
  261. START_TEST(Client_subscription_without_notification) {
  262. UA_Client *client = UA_Client_new();
  263. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  264. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  265. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  266. UA_Client_recv = client->connection.recv;
  267. client->connection.recv = UA_Client_recvTesting;
  268. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  269. request.requestedMaxKeepAliveCount = 1;
  270. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  271. NULL, NULL, NULL);
  272. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  273. UA_UInt32 subId = response.subscriptionId;
  274. /* monitor the server state */
  275. UA_MonitoredItemCreateRequest monRequest =
  276. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  277. monRequest.requestedParameters.samplingInterval = 99999999.0;
  278. UA_MonitoredItemCreateResult monResponse =
  279. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  280. UA_TIMESTAMPSTORETURN_BOTH,
  281. monRequest, NULL, dataChangeHandler, NULL);
  282. UA_UInt32 monId = monResponse.monitoredItemId;
  283. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  284. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  285. notificationReceived = false;
  286. retval = UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  287. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  288. ck_assert_uint_eq(notificationReceived, true);
  289. notificationReceived = false;
  290. retval = UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  291. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  292. ck_assert_uint_eq(notificationReceived, false);
  293. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  294. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  295. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  296. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  297. UA_Client_disconnect(client);
  298. UA_Client_delete(client);
  299. }
  300. END_TEST
  301. static UA_ClientState callbackClientState;
  302. static void
  303. stateCallback (UA_Client *client, UA_ClientState clientState){
  304. callbackClientState = clientState;
  305. if (clientState == UA_CLIENTSTATE_SESSION){
  306. /* A new session was created. We need to create the subscription. */
  307. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  308. request.requestedMaxKeepAliveCount = 1;
  309. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  310. NULL, NULL, NULL);
  311. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  312. UA_UInt32 subId = response.subscriptionId;
  313. ck_assert_uint_ne(subId, 0);
  314. /* Add a MonitoredItem */
  315. UA_MonitoredItemCreateRequest monRequest =
  316. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
  317. UA_MonitoredItemCreateResult monResponse =
  318. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  319. UA_TIMESTAMPSTORETURN_BOTH,
  320. monRequest, NULL, dataChangeHandler, NULL);
  321. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  322. UA_UInt32 monId = monResponse.monitoredItemId;
  323. ck_assert_uint_ne(monId, 0);
  324. }
  325. }
  326. static UA_Boolean inactivityCallbackCalled = false;
  327. static void
  328. subscriptionInactivityCallback (UA_Client *client, UA_UInt32 subId, void *subContext) {
  329. inactivityCallbackCalled = true;
  330. }
  331. START_TEST(Client_subscription_async_sub) {
  332. UA_Client *client = UA_Client_new();
  333. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  334. /* Set stateCallback */
  335. UA_ClientConfig *cc = UA_Client_getConfig(client);
  336. cc->stateCallback = stateCallback;
  337. cc->subscriptionInactivityCallback = subscriptionInactivityCallback;
  338. inactivityCallbackCalled = false;
  339. /* Activate background publish request */
  340. cc->outStandingPublishRequests = 10;
  341. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_DISCONNECTED);
  342. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  343. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  344. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  345. UA_Client_recv = client->connection.recv;
  346. client->connection.recv = UA_Client_recvTesting;
  347. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  348. countNotificationReceived = 0;
  349. notificationReceived = false;
  350. UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  351. ck_assert_uint_eq(notificationReceived, true);
  352. ck_assert_uint_eq(countNotificationReceived, 1);
  353. notificationReceived = false;
  354. UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  355. ck_assert_uint_eq(notificationReceived, true);
  356. ck_assert_uint_eq(countNotificationReceived, 2);
  357. notificationReceived = false;
  358. UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  359. ck_assert_uint_eq(notificationReceived, true);
  360. ck_assert_uint_eq(countNotificationReceived, 3);
  361. notificationReceived = false;
  362. UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  363. ck_assert_uint_eq(notificationReceived, true);
  364. ck_assert_uint_eq(countNotificationReceived, 4);
  365. notificationReceived = false;
  366. UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  367. ck_assert_uint_eq(notificationReceived, true);
  368. ck_assert_uint_eq(countNotificationReceived, 5);
  369. ck_assert_uint_lt(client->config.outStandingPublishRequests, 10);
  370. notificationReceived = false;
  371. /* Simulate network cable unplugged (no response from server) */
  372. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  373. UA_Client_run_iterate(client, (UA_UInt16)(publishingInterval + 1));
  374. ck_assert_uint_eq(notificationReceived, false);
  375. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  376. /* Simulate network cable unplugged (no response from server) */
  377. ck_assert_uint_eq(inactivityCallbackCalled, false);
  378. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  379. UA_Client_run_iterate(client, (UA_UInt16)cc->timeout);
  380. ck_assert_uint_eq(inactivityCallbackCalled, true);
  381. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  382. UA_Client_delete(client);
  383. }
  384. END_TEST
  385. #ifdef UA_ENABLE_METHODCALLS
  386. START_TEST(Client_methodcall) {
  387. UA_Client *client = UA_Client_new();
  388. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  389. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  390. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  391. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  392. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  393. NULL, NULL, NULL);
  394. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  395. /* monitor the server state */
  396. UA_MonitoredItemCreateRequest monRequest =
  397. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  398. UA_MonitoredItemCreateResult monResponse =
  399. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  400. UA_TIMESTAMPSTORETURN_BOTH,
  401. monRequest, NULL, NULL, NULL);
  402. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  403. /* Minimal nodeset does not contain any method we can call here */
  404. #ifdef UA_GENERATED_NAMESPACE_ZERO
  405. UA_UInt32 monId = monResponse.monitoredItemId;
  406. UA_UInt32 subId = response.subscriptionId;
  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. #endif
  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. }