check_client_subscriptions.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. static void monitoredItemHandler(UA_UInt32 monId, UA_DataValue *value, void *context) {
  46. notificationReceived = true;
  47. }
  48. START_TEST(Client_subscription) {
  49. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  50. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  51. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  52. UA_UInt32 subId;
  53. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  54. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  55. /* monitor the server state */
  56. UA_UInt32 monId;
  57. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  58. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  59. NULL, &monId, 250);
  60. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  61. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  62. notificationReceived = false;
  63. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  64. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  65. ck_assert_uint_eq(notificationReceived, true);
  66. retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, monId);
  67. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  68. retval = UA_Client_Subscriptions_remove(client, subId);
  69. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  70. UA_Client_disconnect(client);
  71. UA_Client_delete(client);
  72. }
  73. END_TEST
  74. START_TEST(Client_subscription_connectionClose) {
  75. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  76. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  77. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  78. UA_UInt32 subId;
  79. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  80. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  81. /* monitor the server state */
  82. UA_UInt32 monId;
  83. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  84. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  85. NULL, &monId, 250);
  86. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  87. UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  88. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  89. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  90. UA_Client_recv = client->connection.recv;
  91. client->connection.recv = UA_Client_recvTesting;
  92. /* Simulate BADCONNECTIONCLOSE */
  93. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  94. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  95. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSERVERNOTCONNECTED);
  96. UA_Client_disconnect(client);
  97. UA_Client_delete(client);
  98. }
  99. END_TEST
  100. START_TEST(Client_methodcall) {
  101. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  102. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  103. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  104. UA_UInt32 subId;
  105. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  106. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  107. /* monitor the server state */
  108. UA_UInt32 monId;
  109. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  110. UA_ATTRIBUTEID_VALUE, NULL, NULL, &monId, 250);
  111. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  112. /* call a method to get monitored item id */
  113. UA_Variant input;
  114. UA_Variant_init(&input);
  115. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  116. size_t outputSize;
  117. UA_Variant *output;
  118. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  119. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  120. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  121. ck_assert_uint_eq(outputSize, 2);
  122. ck_assert_uint_eq(output[0].arrayLength, 1);
  123. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  124. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  125. UA_Variant_deleteMembers(&input);
  126. /* call with invalid subscription id */
  127. UA_Variant_init(&input);
  128. subId = 0;
  129. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  130. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  131. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  132. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  133. UA_Variant_deleteMembers(&input);
  134. UA_Client_disconnect(client);
  135. UA_Client_delete(client);
  136. }
  137. END_TEST
  138. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  139. static Suite* testSuite_Client(void) {
  140. TCase *tc_client = tcase_create("Client Subscription Basic");
  141. tcase_add_checked_fixture(tc_client, setup, teardown);
  142. #ifdef UA_ENABLE_SUBSCRIPTIONS
  143. tcase_add_test(tc_client, Client_subscription);
  144. tcase_add_test(tc_client, Client_subscription_connectionClose);
  145. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  146. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  147. tcase_add_checked_fixture(tc_client2, setup, teardown);
  148. #ifdef UA_ENABLE_SUBSCRIPTIONS
  149. tcase_add_test(tc_client2, Client_methodcall);
  150. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  151. Suite *s = suite_create("Client Subscription");
  152. suite_add_tcase(s,tc_client);
  153. suite_add_tcase(s,tc_client2);
  154. return s;
  155. }
  156. int main(void) {
  157. Suite *s = testSuite_Client();
  158. SRunner *sr = srunner_create(s);
  159. srunner_set_fork_status(sr, CK_NOFORK);
  160. srunner_run_all(sr,CK_NORMAL);
  161. int number_failed = srunner_ntests_failed(sr);
  162. srunner_free(sr);
  163. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  164. }