check_client_subscriptions.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. UA_fakeSleep((UA_UInt32)(publishingInterval + 1));
  189. /* Manually send a publish request */
  190. UA_PublishRequest pr;
  191. UA_PublishRequest_init(&pr);
  192. pr.subscriptionAcknowledgementsSize = 0;
  193. UA_PublishResponse presponse;
  194. UA_PublishResponse_init(&presponse);
  195. __UA_Client_Service(client, &pr, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  196. &presponse, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  197. ck_assert_uint_eq(presponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  198. ck_assert_uint_eq(presponse.notificationMessage.notificationDataSize, 1);
  199. UA_PublishResponse_deleteMembers(&presponse);
  200. UA_PublishRequest_deleteMembers(&pr);
  201. UA_fakeSleep((UA_UInt32)(publishingInterval + 1));
  202. UA_PublishRequest_init(&pr);
  203. pr.subscriptionAcknowledgementsSize = 0;
  204. UA_PublishResponse_init(&presponse);
  205. __UA_Client_Service(client, &pr, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  206. &presponse, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  207. ck_assert_uint_eq(presponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  208. ck_assert_uint_eq(presponse.notificationMessage.notificationDataSize, 0);
  209. UA_PublishResponse_deleteMembers(&presponse);
  210. UA_PublishRequest_deleteMembers(&pr);
  211. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  212. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  213. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  214. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  215. UA_Client_disconnect(client);
  216. UA_Client_delete(client);
  217. }
  218. END_TEST
  219. START_TEST(Client_subscription_connectionClose) {
  220. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  221. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  222. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  223. UA_Client_recv = client->connection.recv;
  224. client->connection.recv = UA_Client_recvTesting;
  225. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  226. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  227. NULL, NULL, NULL);
  228. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  229. /* monitor the server state */
  230. UA_MonitoredItemCreateRequest monRequest =
  231. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  232. UA_MonitoredItemCreateResult monResponse =
  233. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  234. UA_TIMESTAMPSTORETURN_BOTH,
  235. monRequest, NULL, dataChangeHandler, NULL);
  236. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  237. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  238. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 60));
  239. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  240. /* Simulate BADCONNECTIONCLOSE */
  241. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  242. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 60));
  243. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  244. UA_Client_disconnect(client);
  245. UA_Client_delete(client);
  246. }
  247. END_TEST
  248. START_TEST(Client_subscription_without_notification) {
  249. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  250. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  251. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  252. UA_Client_recv = client->connection.recv;
  253. client->connection.recv = UA_Client_recvTesting;
  254. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  255. request.requestedMaxKeepAliveCount = 1;
  256. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  257. NULL, NULL, NULL);
  258. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  259. UA_UInt32 subId = response.subscriptionId;
  260. /* monitor the server state */
  261. UA_MonitoredItemCreateRequest monRequest =
  262. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  263. monRequest.requestedParameters.samplingInterval = 99999999.0;
  264. UA_MonitoredItemCreateResult monResponse =
  265. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  266. UA_TIMESTAMPSTORETURN_BOTH,
  267. monRequest, NULL, dataChangeHandler, NULL);
  268. UA_UInt32 monId = monResponse.monitoredItemId;
  269. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  270. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  271. notificationReceived = false;
  272. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  273. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  274. ck_assert_uint_eq(notificationReceived, true);
  275. notificationReceived = false;
  276. retval = UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  277. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  278. ck_assert_uint_eq(notificationReceived, false);
  279. retval = UA_Client_MonitoredItems_deleteSingle(client, subId, monId);
  280. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  281. retval = UA_Client_Subscriptions_deleteSingle(client, subId);
  282. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  283. UA_Client_disconnect(client);
  284. UA_Client_delete(client);
  285. }
  286. END_TEST
  287. static UA_ClientState callbackClientState;
  288. static void
  289. stateCallback (UA_Client *client, UA_ClientState clientState){
  290. callbackClientState = clientState;
  291. if (clientState == UA_CLIENTSTATE_SESSION){
  292. /* A new session was created. We need to create the subscription. */
  293. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  294. request.requestedMaxKeepAliveCount = 1;
  295. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  296. NULL, NULL, NULL);
  297. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  298. UA_UInt32 subId = response.subscriptionId;
  299. ck_assert_uint_ne(subId, 0);
  300. /* Add a MonitoredItem */
  301. UA_MonitoredItemCreateRequest monRequest =
  302. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
  303. UA_MonitoredItemCreateResult monResponse =
  304. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  305. UA_TIMESTAMPSTORETURN_BOTH,
  306. monRequest, NULL, dataChangeHandler, NULL);
  307. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  308. UA_UInt32 monId = monResponse.monitoredItemId;
  309. ck_assert_uint_ne(monId, 0);
  310. }
  311. }
  312. static UA_Boolean inactivityCallbackCalled = false;
  313. static void
  314. subscriptionInactivityCallback (UA_Client *client, UA_UInt32 subId, void *subContext) {
  315. inactivityCallbackCalled = true;
  316. }
  317. START_TEST(Client_subscription_async_sub) {
  318. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  319. /* Set stateCallback */
  320. clientConfig.stateCallback = stateCallback;
  321. clientConfig.subscriptionInactivityCallback = subscriptionInactivityCallback;
  322. inactivityCallbackCalled = false;
  323. /* Activate background publish request */
  324. clientConfig.outStandingPublishRequests = 10;
  325. UA_Client *client = UA_Client_new(clientConfig);
  326. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_DISCONNECTED);
  327. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  328. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  329. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  330. UA_Client_recv = client->connection.recv;
  331. client->connection.recv = UA_Client_recvTesting;
  332. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  333. countNotificationReceived = 0;
  334. notificationReceived = false;
  335. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  336. ck_assert_uint_eq(notificationReceived, true);
  337. ck_assert_uint_eq(countNotificationReceived, 1);
  338. notificationReceived = false;
  339. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  340. ck_assert_uint_eq(notificationReceived, true);
  341. ck_assert_uint_eq(countNotificationReceived, 2);
  342. notificationReceived = false;
  343. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  344. ck_assert_uint_eq(notificationReceived, true);
  345. ck_assert_uint_eq(countNotificationReceived, 3);
  346. notificationReceived = false;
  347. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  348. ck_assert_uint_eq(notificationReceived, true);
  349. ck_assert_uint_eq(countNotificationReceived, 4);
  350. notificationReceived = false;
  351. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  352. ck_assert_uint_eq(notificationReceived, true);
  353. ck_assert_uint_eq(countNotificationReceived, 5);
  354. ck_assert_uint_lt(client->config.outStandingPublishRequests, 10);
  355. notificationReceived = false;
  356. /* Simulate network cable unplugged (no response from server) */
  357. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  358. UA_Client_runAsync(client, (UA_UInt16)(publishingInterval + 1));
  359. ck_assert_uint_eq(notificationReceived, false);
  360. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  361. /* Simulate network cable unplugged (no response from server) */
  362. ck_assert_uint_eq(inactivityCallbackCalled, false);
  363. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  364. UA_Client_runAsync(client, (UA_UInt16)clientConfig.timeout);
  365. ck_assert_uint_eq(inactivityCallbackCalled, true);
  366. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  367. UA_Client_delete(client);
  368. }
  369. END_TEST
  370. START_TEST(Client_methodcall) {
  371. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  372. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  373. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  374. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  375. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  376. NULL, NULL, NULL);
  377. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  378. UA_UInt32 subId = response.subscriptionId;
  379. /* monitor the server state */
  380. UA_MonitoredItemCreateRequest monRequest =
  381. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  382. UA_MonitoredItemCreateResult monResponse =
  383. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  384. UA_TIMESTAMPSTORETURN_BOTH,
  385. monRequest, NULL, NULL, NULL);
  386. ck_assert_uint_eq(monResponse.statusCode, UA_STATUSCODE_GOOD);
  387. UA_UInt32 monId = monResponse.monitoredItemId;
  388. /* call a method to get monitored item id */
  389. UA_Variant input;
  390. UA_Variant_init(&input);
  391. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  392. size_t outputSize;
  393. UA_Variant *output;
  394. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  395. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  396. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  397. ck_assert_uint_eq(outputSize, 2);
  398. ck_assert_uint_eq(output[0].arrayLength, 1);
  399. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  400. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  401. UA_Variant_deleteMembers(&input);
  402. /* call with invalid subscription id */
  403. UA_Variant_init(&input);
  404. subId = 0;
  405. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  406. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  407. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  408. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  409. UA_Variant_deleteMembers(&input);
  410. UA_Client_disconnect(client);
  411. UA_Client_delete(client);
  412. }
  413. END_TEST
  414. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  415. static Suite* testSuite_Client(void) {
  416. TCase *tc_client = tcase_create("Client Subscription Basic");
  417. tcase_add_checked_fixture(tc_client, setup, teardown);
  418. #ifdef UA_ENABLE_SUBSCRIPTIONS
  419. tcase_add_test(tc_client, Client_subscription);
  420. tcase_add_test(tc_client, Client_subscription_connectionClose);
  421. tcase_add_test(tc_client, Client_subscription_createDataChanges);
  422. tcase_add_test(tc_client, Client_subscription_keepAlive);
  423. tcase_add_test(tc_client, Client_subscription_without_notification);
  424. tcase_add_test(tc_client, Client_subscription_async_sub);
  425. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  426. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  427. tcase_add_checked_fixture(tc_client2, setup, teardown);
  428. #ifdef UA_ENABLE_SUBSCRIPTIONS
  429. tcase_add_test(tc_client2, Client_methodcall);
  430. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  431. Suite *s = suite_create("Client Subscription");
  432. suite_add_tcase(s,tc_client);
  433. suite_add_tcase(s,tc_client2);
  434. return s;
  435. }
  436. int main(void) {
  437. Suite *s = testSuite_Client();
  438. SRunner *sr = srunner_create(s);
  439. srunner_set_fork_status(sr, CK_NOFORK);
  440. srunner_run_all(sr,CK_NORMAL);
  441. int number_failed = srunner_ntests_failed(sr);
  442. srunner_free(sr);
  443. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  444. }