check_client_subscriptions.c 23 KB

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