check_client_subscriptions.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. #ifdef _MSC_VER
  5. #pragma warning(disable:4996) // warning C4996: 'UA_Client_Subscriptions_manuallySendPublishRequest': was declared deprecated
  6. #endif
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "ua_types.h"
  10. #include "ua_server.h"
  11. #include "ua_client.h"
  12. #include "client/ua_client_internal.h"
  13. #include "ua_client_highlevel.h"
  14. #include "ua_config_default.h"
  15. #include "ua_network_tcp.h"
  16. #include "check.h"
  17. #include "testing_clock.h"
  18. #include "testing_networklayers.h"
  19. #include "thread_wrapper.h"
  20. UA_Server *server;
  21. UA_ServerConfig *config;
  22. UA_Boolean *running;
  23. UA_ServerNetworkLayer nl;
  24. THREAD_HANDLE server_thread;
  25. THREAD_CALLBACK(serverloop) {
  26. while(*running)
  27. UA_Server_run_iterate(server, true);
  28. return 0;
  29. }
  30. static void setup(void) {
  31. running = UA_Boolean_new();
  32. *running = true;
  33. config = UA_ServerConfig_new_default();
  34. config->maxPublishReqPerSession = 8;
  35. server = UA_Server_new(config);
  36. UA_Server_run_startup(server);
  37. THREAD_CREATE(server_thread, serverloop);
  38. }
  39. static void teardown(void) {
  40. *running = false;
  41. THREAD_JOIN(server_thread);
  42. UA_Server_run_shutdown(server);
  43. UA_Boolean_delete(running);
  44. UA_Server_delete(server);
  45. UA_ServerConfig_delete(config);
  46. }
  47. #ifdef UA_ENABLE_SUBSCRIPTIONS
  48. UA_Boolean notificationReceived = false;
  49. UA_UInt32 countNotificationReceived = 0;
  50. static void
  51. monitoredItemHandler(UA_Client *client, UA_UInt32 monId, UA_DataValue *value, void *context) {
  52. notificationReceived = true;
  53. countNotificationReceived++;
  54. }
  55. static void
  56. monitoredItemHandlerSubSleep(UA_Client *client, UA_UInt32 monId, UA_DataValue *value, void *context) {
  57. notificationReceived = true;
  58. countNotificationReceived++;
  59. UA_fakeSleep((UA_UInt32)(UA_SubscriptionSettings_default.requestedPublishingInterval + 2));
  60. }
  61. START_TEST(Client_subscription) {
  62. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  63. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  64. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  65. UA_UInt32 subId;
  66. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  67. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  68. /* monitor the server state */
  69. UA_UInt32 monId;
  70. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  71. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  72. NULL, &monId, 250);
  73. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  74. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  75. notificationReceived = false;
  76. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  77. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  78. ck_assert_uint_eq(notificationReceived, true);
  79. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, monId);
  80. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  81. retval = UA_Client_Subscriptions_remove(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_addMonitoredItems) {
  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_UInt32 subId;
  92. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  93. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  94. UA_MonitoredItemCreateRequest items[3];
  95. UA_MonitoredItemHandlingFunction hfs[3];
  96. void *hfContexts[3];
  97. UA_StatusCode itemResults[3];
  98. UA_UInt32 newMonitoredItemIds[3];
  99. /* monitor the server state */
  100. UA_MonitoredItemCreateRequest_init(&items[0]);
  101. items[0].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2259);
  102. items[0].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  103. items[0].monitoringMode = UA_MONITORINGMODE_REPORTING;
  104. items[0].requestedParameters.samplingInterval = 250;
  105. items[0].requestedParameters.discardOldest = true;
  106. items[0].requestedParameters.queueSize = 1;
  107. hfs[0] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  108. hfContexts[0] = NULL;
  109. /* monitor current time */
  110. UA_MonitoredItemCreateRequest_init(&items[1]);
  111. items[1].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  112. items[1].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  113. items[1].monitoringMode = UA_MONITORINGMODE_REPORTING;
  114. items[1].requestedParameters.samplingInterval = 250;
  115. items[1].requestedParameters.discardOldest = true;
  116. items[1].requestedParameters.queueSize = 1;
  117. hfs[1] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  118. hfContexts[1] = NULL;
  119. /* monitor invalid node */
  120. UA_MonitoredItemCreateRequest_init(&items[2]);
  121. items[2].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 99999999);
  122. items[2].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  123. items[2].monitoringMode = UA_MONITORINGMODE_REPORTING;
  124. items[2].requestedParameters.samplingInterval = 250;
  125. items[2].requestedParameters.discardOldest = true;
  126. items[2].requestedParameters.queueSize = 1;
  127. hfs[2] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  128. hfContexts[2] = NULL;
  129. retval = UA_Client_Subscriptions_addMonitoredItems(client, subId, items, 3,
  130. hfs, hfContexts, itemResults,
  131. newMonitoredItemIds);
  132. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  133. ck_assert_uint_eq(itemResults[0], UA_STATUSCODE_GOOD);
  134. ck_assert_uint_eq(itemResults[1], UA_STATUSCODE_GOOD);
  135. ck_assert_uint_eq(itemResults[2], UA_STATUSCODE_BADNODEIDUNKNOWN);
  136. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  137. notificationReceived = false;
  138. countNotificationReceived = 0;
  139. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  140. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  141. ck_assert_uint_eq(notificationReceived, true);
  142. ck_assert_uint_eq(countNotificationReceived, 2);
  143. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  144. notificationReceived = false;
  145. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  146. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  147. ck_assert_uint_eq(notificationReceived, true);
  148. ck_assert_uint_eq(countNotificationReceived, 3);
  149. retval = UA_Client_Subscriptions_removeMonitoredItems(client, subId, newMonitoredItemIds,
  150. 3, itemResults);
  151. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  152. ck_assert_uint_eq(itemResults[0], UA_STATUSCODE_GOOD);
  153. ck_assert_uint_eq(itemResults[1], UA_STATUSCODE_GOOD);
  154. ck_assert_uint_eq(itemResults[2], UA_STATUSCODE_BADMONITOREDITEMIDINVALID);
  155. retval = UA_Client_Subscriptions_remove(client, subId);
  156. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  157. UA_Client_disconnect(client);
  158. UA_Client_delete(client);
  159. }
  160. END_TEST
  161. START_TEST(Client_subscription_keepAlive) {
  162. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  163. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  164. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  165. UA_UInt32 subId;
  166. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  167. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  168. UA_MonitoredItemCreateRequest items[1];
  169. UA_MonitoredItemHandlingFunction hfs[1];
  170. void *hfContexts[1];
  171. UA_StatusCode itemResults[1];
  172. UA_UInt32 newMonitoredItemIds[1];
  173. /* monitor the server state */
  174. UA_MonitoredItemCreateRequest_init(&items[0]);
  175. items[0].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2259);
  176. items[0].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  177. items[0].monitoringMode = UA_MONITORINGMODE_REPORTING;
  178. items[0].requestedParameters.samplingInterval = 250;
  179. items[0].requestedParameters.discardOldest = true;
  180. items[0].requestedParameters.queueSize = 1;
  181. hfs[0] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  182. hfContexts[0] = NULL;
  183. retval = UA_Client_Subscriptions_addMonitoredItems(client, subId, items, 1,
  184. hfs, hfContexts, itemResults,
  185. newMonitoredItemIds);
  186. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  187. ck_assert_uint_eq(itemResults[0], UA_STATUSCODE_GOOD);
  188. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  189. UA_PublishRequest request;
  190. UA_PublishRequest_init(&request);
  191. request.subscriptionAcknowledgementsSize = 0;
  192. UA_PublishResponse response = UA_Client_Service_publish(client, request);
  193. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  194. ck_assert_uint_eq(response.notificationMessage.notificationDataSize, 1);
  195. UA_PublishResponse_deleteMembers(&response);
  196. UA_PublishRequest_deleteMembers(&request);
  197. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  198. /* by default maxKeepAlive is set to 1 we must receive a response without notification message */
  199. UA_PublishRequest_init(&request);
  200. request.subscriptionAcknowledgementsSize = 0;
  201. response = UA_Client_Service_publish(client, request);
  202. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  203. ck_assert_uint_eq(response.notificationMessage.notificationDataSize, 0);
  204. UA_PublishResponse_deleteMembers(&response);
  205. UA_PublishRequest_deleteMembers(&request);
  206. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, newMonitoredItemIds[0]);
  207. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  208. retval = UA_Client_Subscriptions_remove(client, subId);
  209. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  210. UA_Client_disconnect(client);
  211. UA_Client_delete(client);
  212. }
  213. END_TEST
  214. START_TEST(Client_subscription_connectionClose) {
  215. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  216. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  217. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  218. UA_UInt32 subId;
  219. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  220. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  221. /* monitor the server state */
  222. UA_UInt32 monId;
  223. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  224. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  225. NULL, &monId, 250);
  226. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  227. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  228. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  229. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  230. UA_Client_recv = client->connection.recv;
  231. client->connection.recv = UA_Client_recvTesting;
  232. /* Simulate BADCONNECTIONCLOSE */
  233. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  234. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  235. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSERVERNOTCONNECTED);
  236. UA_Client_disconnect(client);
  237. UA_Client_delete(client);
  238. }
  239. END_TEST
  240. START_TEST(Client_subscription_without_notification) {
  241. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  242. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  243. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  244. UA_UInt32 subId;
  245. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  246. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  247. /* monitor the server state */
  248. UA_UInt32 monId;
  249. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  250. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  251. NULL, &monId, 9999999);
  252. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  253. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  254. notificationReceived = false;
  255. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  256. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  257. ck_assert_uint_eq(notificationReceived, true);
  258. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  259. notificationReceived = false;
  260. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  261. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  262. ck_assert_uint_eq(notificationReceived, false);
  263. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, monId);
  264. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  265. retval = UA_Client_Subscriptions_remove(client, subId);
  266. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  267. UA_Client_disconnect(client);
  268. UA_Client_delete(client);
  269. }
  270. END_TEST
  271. static UA_ClientState callbackClientState;
  272. static void
  273. stateCallback (UA_Client *client, UA_ClientState clientState){
  274. callbackClientState = clientState;
  275. if (clientState == UA_CLIENTSTATE_SESSION){
  276. /* A new session was created. We need to create the subscription. */
  277. /* Create a subscription */
  278. UA_UInt32 subId = 0;
  279. UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  280. ck_assert_uint_ne(subId, 0);
  281. /* Add a MonitoredItem */
  282. UA_UInt32 monId = 0;
  283. UA_NodeId monitorThis = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  284. UA_Client_Subscriptions_addMonitoredItem(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE,
  285. &monitoredItemHandlerSubSleep, NULL, &monId, 250);
  286. ck_assert_uint_ne(monId, 0);
  287. }
  288. }
  289. START_TEST(Client_subscription_async_sub) {
  290. UA_ClientConfig clientConfig = UA_ClientConfig_default;
  291. /* Set stateCallback */
  292. clientConfig.stateCallback = stateCallback;
  293. /* Activate background publish request */
  294. clientConfig.outStandingPublishRequests = 10;
  295. UA_Client *client = UA_Client_new(clientConfig);
  296. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_DISCONNECTED);
  297. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  298. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  299. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  300. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  301. countNotificationReceived = 0;
  302. notificationReceived = false;
  303. UA_Client_runAsync(client, (UA_UInt16)(UA_SubscriptionSettings_default.requestedPublishingInterval + 1));
  304. ck_assert_uint_eq(notificationReceived, true);
  305. ck_assert_uint_eq(countNotificationReceived, 1);
  306. notificationReceived = false;
  307. UA_Client_runAsync(client, (UA_UInt16)(UA_SubscriptionSettings_default.requestedPublishingInterval + 1));
  308. ck_assert_uint_eq(notificationReceived, true);
  309. ck_assert_uint_eq(countNotificationReceived, 2);
  310. notificationReceived = false;
  311. UA_Client_runAsync(client, (UA_UInt16)(UA_SubscriptionSettings_default.requestedPublishingInterval + 1));
  312. ck_assert_uint_eq(notificationReceived, true);
  313. ck_assert_uint_eq(countNotificationReceived, 3);
  314. notificationReceived = false;
  315. UA_Client_runAsync(client, (UA_UInt16)(UA_SubscriptionSettings_default.requestedPublishingInterval + 1));
  316. ck_assert_uint_eq(notificationReceived, true);
  317. ck_assert_uint_eq(countNotificationReceived, 4);
  318. notificationReceived = false;
  319. UA_Client_runAsync(client, (UA_UInt16)(UA_SubscriptionSettings_default.requestedPublishingInterval + 1));
  320. ck_assert_uint_eq(notificationReceived, true);
  321. ck_assert_uint_eq(countNotificationReceived, 5);
  322. ck_assert_uint_lt(client->config.outStandingPublishRequests, 10);
  323. UA_Client_recv = client->connection.recv;
  324. client->connection.recv = UA_Client_recvTesting;
  325. notificationReceived = false;
  326. /* Simulate network cable unplugged (no response from server) */
  327. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  328. UA_Client_runAsync(client, (UA_UInt16)(UA_SubscriptionSettings_default.requestedPublishingInterval + 1));
  329. ck_assert_uint_eq(notificationReceived, false);
  330. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  331. /* Simulate network cable unplugged (no response from server) */
  332. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  333. UA_Client_runAsync(client, (UA_UInt16)clientConfig.timeout);
  334. ck_assert_uint_eq(notificationReceived, false);
  335. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_SESSION);
  336. /* Simulate network cable unplugged (no response from server) */
  337. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  338. UA_Client_runAsync(client, (UA_UInt16)clientConfig.timeout);
  339. ck_assert_uint_eq(notificationReceived, false);
  340. ck_assert_uint_eq(callbackClientState, UA_CLIENTSTATE_DISCONNECTED);
  341. UA_Client_delete(client);
  342. }
  343. END_TEST
  344. START_TEST(Client_methodcall) {
  345. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  346. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  347. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  348. UA_UInt32 subId;
  349. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  350. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  351. /* monitor the server state */
  352. UA_UInt32 monId;
  353. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  354. UA_ATTRIBUTEID_VALUE, NULL, NULL, &monId, 250);
  355. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  356. /* call a method to get monitored item id */
  357. UA_Variant input;
  358. UA_Variant_init(&input);
  359. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  360. size_t outputSize;
  361. UA_Variant *output;
  362. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  363. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  364. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  365. ck_assert_uint_eq(outputSize, 2);
  366. ck_assert_uint_eq(output[0].arrayLength, 1);
  367. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  368. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  369. UA_Variant_deleteMembers(&input);
  370. /* call with invalid subscription id */
  371. UA_Variant_init(&input);
  372. subId = 0;
  373. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  374. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  375. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  376. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  377. UA_Variant_deleteMembers(&input);
  378. UA_Client_disconnect(client);
  379. UA_Client_delete(client);
  380. }
  381. END_TEST
  382. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  383. static Suite* testSuite_Client(void) {
  384. TCase *tc_client = tcase_create("Client Subscription Basic");
  385. tcase_add_checked_fixture(tc_client, setup, teardown);
  386. #ifdef UA_ENABLE_SUBSCRIPTIONS
  387. tcase_add_test(tc_client, Client_subscription);
  388. tcase_add_test(tc_client, Client_subscription_connectionClose);
  389. tcase_add_test(tc_client, Client_subscription_addMonitoredItems);
  390. tcase_add_test(tc_client, Client_subscription_keepAlive);
  391. tcase_add_test(tc_client, Client_subscription_without_notification);
  392. tcase_add_test(tc_client, Client_subscription_async_sub);
  393. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  394. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  395. tcase_add_checked_fixture(tc_client2, setup, teardown);
  396. #ifdef UA_ENABLE_SUBSCRIPTIONS
  397. tcase_add_test(tc_client2, Client_methodcall);
  398. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  399. Suite *s = suite_create("Client Subscription");
  400. suite_add_tcase(s,tc_client);
  401. suite_add_tcase(s,tc_client2);
  402. return s;
  403. }
  404. int main(void) {
  405. Suite *s = testSuite_Client();
  406. SRunner *sr = srunner_create(s);
  407. srunner_set_fork_status(sr, CK_NOFORK);
  408. srunner_run_all(sr,CK_NORMAL);
  409. int number_failed = srunner_ntests_failed(sr);
  410. srunner_free(sr);
  411. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  412. }