check_client_subscriptions_deprecated.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. server = UA_Server_new(config);
  32. UA_Server_run_startup(server);
  33. THREAD_CREATE(server_thread, serverloop);
  34. }
  35. static void teardown(void) {
  36. *running = false;
  37. THREAD_JOIN(server_thread);
  38. UA_Server_run_shutdown(server);
  39. UA_Boolean_delete(running);
  40. UA_Server_delete(server);
  41. UA_ServerConfig_delete(config);
  42. }
  43. #ifdef UA_ENABLE_SUBSCRIPTIONS
  44. UA_Boolean notificationReceived;
  45. UA_UInt32 countNotificationReceived = 0;
  46. static void monitoredItemHandler(UA_Client *client, UA_UInt32 monId, UA_DataValue *value, void *context) {
  47. notificationReceived = true;
  48. countNotificationReceived++;
  49. }
  50. START_TEST(Client_subscription) {
  51. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  52. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  53. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  54. UA_UInt32 subId;
  55. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  56. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  57. /* monitor the server state */
  58. UA_UInt32 monId;
  59. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  60. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  61. NULL, &monId, 250);
  62. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  63. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  64. notificationReceived = false;
  65. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  66. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  67. ck_assert_uint_eq(notificationReceived, true);
  68. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, monId);
  69. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  70. retval = UA_Client_Subscriptions_remove(client, subId);
  71. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  72. UA_Client_disconnect(client);
  73. UA_Client_delete(client);
  74. }
  75. END_TEST
  76. START_TEST(Client_subscription_addMonitoredItems) {
  77. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  78. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  79. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  80. UA_UInt32 subId;
  81. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  82. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  83. UA_MonitoredItemCreateRequest items[3];
  84. UA_MonitoredItemHandlingFunction hfs[3];
  85. void *hfContexts[3];
  86. UA_StatusCode itemResults[3];
  87. UA_UInt32 newMonitoredItemIds[3];
  88. /* monitor the server state */
  89. UA_MonitoredItemCreateRequest_init(&items[0]);
  90. items[0].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2259);
  91. items[0].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  92. items[0].monitoringMode = UA_MONITORINGMODE_REPORTING;
  93. items[0].requestedParameters.samplingInterval = 250;
  94. items[0].requestedParameters.discardOldest = true;
  95. items[0].requestedParameters.queueSize = 1;
  96. hfs[0] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  97. hfContexts[0] = NULL;
  98. /* monitor current time */
  99. UA_MonitoredItemCreateRequest_init(&items[1]);
  100. items[1].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  101. items[1].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  102. items[1].monitoringMode = UA_MONITORINGMODE_REPORTING;
  103. items[1].requestedParameters.samplingInterval = 250;
  104. items[1].requestedParameters.discardOldest = true;
  105. items[1].requestedParameters.queueSize = 1;
  106. hfs[1] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  107. hfContexts[1] = NULL;
  108. /* monitor invalid node */
  109. UA_MonitoredItemCreateRequest_init(&items[2]);
  110. items[2].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 99999999);
  111. items[2].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  112. items[2].monitoringMode = UA_MONITORINGMODE_REPORTING;
  113. items[2].requestedParameters.samplingInterval = 250;
  114. items[2].requestedParameters.discardOldest = true;
  115. items[2].requestedParameters.queueSize = 1;
  116. hfs[2] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  117. hfContexts[2] = NULL;
  118. retval = UA_Client_Subscriptions_addMonitoredItems(client, subId, items, 3,
  119. hfs, hfContexts, itemResults,
  120. newMonitoredItemIds);
  121. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  122. ck_assert_uint_eq(itemResults[0], UA_STATUSCODE_GOOD);
  123. ck_assert_uint_eq(itemResults[1], UA_STATUSCODE_GOOD);
  124. ck_assert_uint_eq(itemResults[2], UA_STATUSCODE_BADNODEIDUNKNOWN);
  125. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  126. notificationReceived = false;
  127. countNotificationReceived = 0;
  128. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  129. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  130. ck_assert_uint_eq(notificationReceived, true);
  131. ck_assert_uint_eq(countNotificationReceived, 2);
  132. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  133. notificationReceived = false;
  134. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  135. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  136. ck_assert_uint_eq(notificationReceived, true);
  137. ck_assert_uint_eq(countNotificationReceived, 3);
  138. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, newMonitoredItemIds[0]);
  139. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  140. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, newMonitoredItemIds[1]);
  141. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  142. retval = UA_Client_Subscriptions_remove(client, subId);
  143. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  144. UA_Client_disconnect(client);
  145. UA_Client_delete(client);
  146. }
  147. END_TEST
  148. START_TEST(Client_subscription_keepAlive) {
  149. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  150. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  151. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  152. UA_UInt32 subId;
  153. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  154. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  155. UA_MonitoredItemCreateRequest items[1];
  156. UA_MonitoredItemHandlingFunction hfs[1];
  157. void *hfContexts[1];
  158. UA_StatusCode itemResults[1];
  159. UA_UInt32 newMonitoredItemIds[1];
  160. /* monitor the server state */
  161. UA_MonitoredItemCreateRequest_init(&items[0]);
  162. items[0].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2259);
  163. items[0].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  164. items[0].monitoringMode = UA_MONITORINGMODE_REPORTING;
  165. items[0].requestedParameters.samplingInterval = 250;
  166. items[0].requestedParameters.discardOldest = true;
  167. items[0].requestedParameters.queueSize = 1;
  168. hfs[0] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
  169. hfContexts[0] = NULL;
  170. retval = UA_Client_Subscriptions_addMonitoredItems(client, subId, items, 1,
  171. hfs, hfContexts, itemResults,
  172. newMonitoredItemIds);
  173. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  174. ck_assert_uint_eq(itemResults[0], UA_STATUSCODE_GOOD);
  175. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  176. UA_PublishRequest request;
  177. UA_PublishRequest_init(&request);
  178. request.subscriptionAcknowledgementsSize = 0;
  179. UA_PublishResponse response;
  180. UA_PublishResponse_init(&response);
  181. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  182. &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  183. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  184. ck_assert_uint_eq(response.notificationMessage.notificationDataSize, 1);
  185. UA_PublishResponse_deleteMembers(&response);
  186. UA_PublishRequest_deleteMembers(&request);
  187. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  188. /* by default maxKeepAlive is set to 1 we must receive a response without notification message */
  189. UA_PublishRequest_init(&request);
  190. request.subscriptionAcknowledgementsSize = 0;
  191. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  192. &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  193. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  194. ck_assert_uint_eq(response.notificationMessage.notificationDataSize, 0);
  195. UA_PublishResponse_deleteMembers(&response);
  196. UA_PublishRequest_deleteMembers(&request);
  197. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, newMonitoredItemIds[0]);
  198. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  199. retval = UA_Client_Subscriptions_remove(client, subId);
  200. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  201. UA_Client_disconnect(client);
  202. UA_Client_delete(client);
  203. }
  204. END_TEST
  205. START_TEST(Client_subscription_connectionClose) {
  206. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  207. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  208. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  209. UA_UInt32 subId;
  210. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  211. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  212. /* monitor the server state */
  213. UA_UInt32 monId;
  214. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  215. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  216. NULL, &monId, 250);
  217. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  218. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  219. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  220. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  221. UA_Client_recv = client->connection.recv;
  222. client->connection.recv = UA_Client_recvTesting;
  223. /* Simulate BADCONNECTIONCLOSE */
  224. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  225. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  226. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSERVERNOTCONNECTED);
  227. UA_Client_disconnect(client);
  228. UA_Client_delete(client);
  229. }
  230. END_TEST
  231. START_TEST(Client_methodcall) {
  232. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  233. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  234. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  235. UA_UInt32 subId;
  236. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  237. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  238. /* monitor the server state */
  239. UA_UInt32 monId;
  240. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  241. UA_ATTRIBUTEID_VALUE, NULL, NULL, &monId, 250);
  242. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  243. /* call a method to get monitored item id */
  244. UA_Variant input;
  245. UA_Variant_init(&input);
  246. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  247. size_t outputSize;
  248. UA_Variant *output;
  249. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  250. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  251. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  252. ck_assert_uint_eq(outputSize, 2);
  253. ck_assert_uint_eq(output[0].arrayLength, 1);
  254. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  255. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  256. UA_Variant_deleteMembers(&input);
  257. /* call with invalid subscription id */
  258. UA_Variant_init(&input);
  259. subId = 0;
  260. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  261. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  262. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  263. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  264. UA_Variant_deleteMembers(&input);
  265. UA_Client_disconnect(client);
  266. UA_Client_delete(client);
  267. }
  268. END_TEST
  269. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  270. static Suite* testSuite_Client(void) {
  271. TCase *tc_client = tcase_create("Client Subscription Basic");
  272. tcase_add_checked_fixture(tc_client, setup, teardown);
  273. #ifdef UA_ENABLE_SUBSCRIPTIONS
  274. tcase_add_test(tc_client, Client_subscription);
  275. tcase_add_test(tc_client, Client_subscription_connectionClose);
  276. tcase_add_test(tc_client, Client_subscription_addMonitoredItems);
  277. tcase_add_test(tc_client, Client_subscription_keepAlive);
  278. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  279. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  280. tcase_add_checked_fixture(tc_client2, setup, teardown);
  281. #ifdef UA_ENABLE_SUBSCRIPTIONS
  282. tcase_add_test(tc_client2, Client_methodcall);
  283. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  284. Suite *s = suite_create("Client Subscription");
  285. suite_add_tcase(s,tc_client);
  286. suite_add_tcase(s,tc_client2);
  287. return s;
  288. }
  289. int main(void) {
  290. Suite *s = testSuite_Client();
  291. SRunner *sr = srunner_create(s);
  292. srunner_set_fork_status(sr, CK_NOFORK);
  293. srunner_run_all(sr,CK_NORMAL);
  294. int number_failed = srunner_ntests_failed(sr);
  295. srunner_free(sr);
  296. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  297. }