check_client_subscriptions.c 24 KB

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