check_client_subscriptions.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_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_connectionClose) {
  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. /* monitor the server state */
  156. UA_UInt32 monId;
  157. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  158. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  159. NULL, &monId, 250);
  160. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  161. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  162. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  163. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  164. UA_Client_recv = client->connection.recv;
  165. client->connection.recv = UA_Client_recvTesting;
  166. /* Simulate BADCONNECTIONCLOSE */
  167. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  168. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  169. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSERVERNOTCONNECTED);
  170. UA_Client_disconnect(client);
  171. UA_Client_delete(client);
  172. }
  173. END_TEST
  174. START_TEST(Client_methodcall) {
  175. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  176. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  177. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  178. UA_UInt32 subId;
  179. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  180. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  181. /* monitor the server state */
  182. UA_UInt32 monId;
  183. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  184. UA_ATTRIBUTEID_VALUE, NULL, NULL, &monId, 250);
  185. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  186. /* call a method to get monitored item id */
  187. UA_Variant input;
  188. UA_Variant_init(&input);
  189. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  190. size_t outputSize;
  191. UA_Variant *output;
  192. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  193. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  194. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  195. ck_assert_uint_eq(outputSize, 2);
  196. ck_assert_uint_eq(output[0].arrayLength, 1);
  197. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  198. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  199. UA_Variant_deleteMembers(&input);
  200. /* call with invalid subscription id */
  201. UA_Variant_init(&input);
  202. subId = 0;
  203. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  204. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  205. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  206. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  207. UA_Variant_deleteMembers(&input);
  208. UA_Client_disconnect(client);
  209. UA_Client_delete(client);
  210. }
  211. END_TEST
  212. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  213. static Suite* testSuite_Client(void) {
  214. TCase *tc_client = tcase_create("Client Subscription Basic");
  215. tcase_add_checked_fixture(tc_client, setup, teardown);
  216. #ifdef UA_ENABLE_SUBSCRIPTIONS
  217. tcase_add_test(tc_client, Client_subscription);
  218. tcase_add_test(tc_client, Client_subscription_connectionClose);
  219. tcase_add_test(tc_client, Client_subscription_addMonitoredItems);
  220. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  221. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  222. tcase_add_checked_fixture(tc_client2, setup, teardown);
  223. #ifdef UA_ENABLE_SUBSCRIPTIONS
  224. tcase_add_test(tc_client2, Client_methodcall);
  225. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  226. Suite *s = suite_create("Client Subscription");
  227. suite_add_tcase(s,tc_client);
  228. suite_add_tcase(s,tc_client2);
  229. return s;
  230. }
  231. int main(void) {
  232. Suite *s = testSuite_Client();
  233. SRunner *sr = srunner_create(s);
  234. srunner_set_fork_status(sr, CK_NOFORK);
  235. srunner_run_all(sr,CK_NORMAL);
  236. int number_failed = srunner_ntests_failed(sr);
  237. srunner_free(sr);
  238. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  239. }