check_discovery.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <pthread.h>
  7. #include <ua_util.h>
  8. #include <ua_types_generated.h>
  9. #include <server/ua_server_internal.h>
  10. #include <unistd.h>
  11. #include "ua_client.h"
  12. #include "ua_config_standard.h"
  13. #include "ua_network_tcp.h"
  14. #include "check.h"
  15. // set register timeout to 1 second so we are able to test it.
  16. #define registerTimeout 1
  17. // cleanup is only triggered every 10 seconds, thus wait a bit longer to check
  18. #define checkWait registerTimeout + 11
  19. UA_Server *server_lds;
  20. UA_Boolean *running_lds;
  21. UA_ServerNetworkLayer nl_lds;
  22. pthread_t server_thread_lds;
  23. static void * serverloop_lds(void *_) {
  24. while(*running_lds)
  25. UA_Server_run_iterate(server_lds, true);
  26. return NULL;
  27. }
  28. static void setup_lds(void) {
  29. // start LDS server
  30. running_lds = UA_Boolean_new();
  31. *running_lds = true;
  32. UA_ServerConfig config_lds = UA_ServerConfig_standard;
  33. config_lds.applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
  34. config_lds.applicationDescription.applicationUri = UA_String_fromChars("open62541.test.local_discovery_server");
  35. config_lds.discoveryCleanupTimeout = registerTimeout;
  36. nl_lds = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 4840);
  37. config_lds.networkLayers = &nl_lds;
  38. config_lds.networkLayersSize = 1;
  39. server_lds = UA_Server_new(config_lds);
  40. UA_Server_run_startup(server_lds);
  41. pthread_create(&server_thread_lds, NULL, serverloop_lds, NULL);
  42. }
  43. static void teardown_lds(void) {
  44. *running_lds = false;
  45. pthread_join(server_thread_lds, NULL);
  46. UA_Server_run_shutdown(server_lds);
  47. UA_Boolean_delete(running_lds);
  48. UA_String_deleteMembers(&server_lds->config.applicationDescription.applicationUri);
  49. UA_Server_delete(server_lds);
  50. nl_lds.deleteMembers(&nl_lds);
  51. }
  52. UA_Server *server_register;
  53. UA_Boolean *running_register;
  54. UA_ServerNetworkLayer nl_register;
  55. pthread_t server_thread_register;
  56. static void * serverloop_register(void *_) {
  57. while(*running_register)
  58. UA_Server_run_iterate(server_register, true);
  59. return NULL;
  60. }
  61. static void setup_register(void) {
  62. // start LDS server
  63. running_register = UA_Boolean_new();
  64. *running_register = true;
  65. UA_ServerConfig config_register = UA_ServerConfig_standard;
  66. config_register.applicationDescription.applicationUri = UA_String_fromChars("open62541.test.server_register");
  67. nl_register = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  68. config_register.networkLayers = &nl_register;
  69. config_register.networkLayersSize = 1;
  70. server_register = UA_Server_new(config_register);
  71. UA_Server_run_startup(server_register);
  72. pthread_create(&server_thread_register, NULL, serverloop_register, NULL);
  73. }
  74. static void teardown_register(void) {
  75. *running_register = false;
  76. pthread_join(server_thread_register, NULL);
  77. UA_Server_run_shutdown(server_register);
  78. UA_Boolean_delete(running_register);
  79. UA_String_deleteMembers(&server_register->config.applicationDescription.applicationUri);
  80. UA_Server_delete(server_register);
  81. nl_register.deleteMembers(&nl_register);
  82. }
  83. START_TEST(Server_register) {
  84. UA_StatusCode retval = UA_Server_register_discovery(server_register, "opc.tcp://localhost:4840", NULL);
  85. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  86. }
  87. END_TEST
  88. START_TEST(Server_unregister) {
  89. UA_StatusCode retval = UA_Server_unregister_discovery(server_register, "opc.tcp://localhost:4840");
  90. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  91. }
  92. END_TEST
  93. static UA_StatusCode FindServers(const char* discoveryServerUrl, size_t* registeredServerSize, UA_ApplicationDescription** registeredServers, const char* filterUri, const char* filterLocale) {
  94. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  95. UA_StatusCode retval = UA_Client_connect(client, discoveryServerUrl);
  96. if(retval != UA_STATUSCODE_GOOD) {
  97. UA_Client_delete(client);
  98. return retval;
  99. }
  100. UA_FindServersRequest request;
  101. UA_FindServersRequest_init(&request);
  102. if (filterUri) {
  103. request.serverUrisSize = 1;
  104. request.serverUris = UA_malloc(sizeof(UA_String));
  105. request.serverUris[0] = UA_String_fromChars(filterUri);
  106. }
  107. if (filterLocale) {
  108. request.localeIdsSize = 1;
  109. request.localeIds = UA_malloc(sizeof(UA_String));
  110. request.localeIds[0] = UA_String_fromChars(filterLocale);
  111. }
  112. // now send the request
  113. UA_FindServersResponse response;
  114. UA_FindServersResponse_init(&response);
  115. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_FINDSERVERSREQUEST],
  116. &response, &UA_TYPES[UA_TYPES_FINDSERVERSRESPONSE]);
  117. if (filterUri) {
  118. UA_Array_delete(request.serverUris, request.serverUrisSize, &UA_TYPES[UA_TYPES_STRING]);
  119. }
  120. if (filterLocale) {
  121. UA_Array_delete(request.localeIds, request.localeIdsSize, &UA_TYPES[UA_TYPES_STRING]);
  122. }
  123. if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  124. UA_FindServersResponse_deleteMembers(&response);
  125. UA_Client_disconnect(client);
  126. UA_Client_delete(client);
  127. ck_abort_msg("FindServers failed with statuscode 0x%08x", response.responseHeader.serviceResult);
  128. }
  129. *registeredServerSize = response.serversSize;
  130. *registeredServers = (UA_ApplicationDescription*)UA_Array_new(response.serversSize, &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]);
  131. for(size_t i=0;i<response.serversSize;i++)
  132. UA_ApplicationDescription_copy(&response.servers[i], &(*registeredServers)[i]);
  133. UA_FindServersResponse_deleteMembers(&response);
  134. UA_Client_disconnect(client);
  135. UA_Client_delete(client);
  136. return (int) UA_STATUSCODE_GOOD;
  137. }
  138. static void FindAndCheck(const char* expectedUris[], size_t expectedUrisSize, const char *filterUri, const char *filterLocale) {
  139. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  140. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  141. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  142. UA_ApplicationDescription* applicationDescriptionArray = NULL;
  143. size_t applicationDescriptionArraySize = 0;
  144. retval = FindServers("opc.tcp://localhost:4840", &applicationDescriptionArraySize, &applicationDescriptionArray, filterUri, filterLocale);
  145. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  146. // only the discovery server is expected
  147. ck_assert_uint_eq(applicationDescriptionArraySize, expectedUrisSize);
  148. assert(applicationDescriptionArray != NULL);
  149. for(size_t i=0; i < expectedUrisSize; ++i) {
  150. char* serverUri = malloc(sizeof(char)*applicationDescriptionArray[i].applicationUri.length+1);
  151. memcpy(serverUri, applicationDescriptionArray[i].applicationUri.data, applicationDescriptionArray[i].applicationUri.length);
  152. serverUri[applicationDescriptionArray[i].applicationUri.length] = '\0';
  153. ck_assert_str_eq(serverUri, expectedUris[i]);
  154. free(serverUri);
  155. }
  156. UA_Array_delete(applicationDescriptionArray, applicationDescriptionArraySize, &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]);
  157. UA_Client_disconnect(client);
  158. UA_Client_delete(client);
  159. }
  160. // Test if discovery server lists himself as registered server, before any other registration.
  161. START_TEST(Client_find_discovery) {
  162. const char* expectedUris[] ={"open62541.test.local_discovery_server"};
  163. FindAndCheck(expectedUris, 1, NULL, NULL);
  164. }
  165. END_TEST
  166. // Test if discovery server lists himself as registered server if it is filtered by his uri
  167. START_TEST(Client_filter_discovery) {
  168. const char* expectedUris[] ={"open62541.test.local_discovery_server"};
  169. FindAndCheck(expectedUris, 1, "open62541.test.local_discovery_server", "en");
  170. }
  171. END_TEST
  172. // Test if registered server is returned from LDS
  173. START_TEST(Client_find_registered) {
  174. const char* expectedUris[] ={"open62541.test.local_discovery_server", "open62541.test.server_register"};
  175. FindAndCheck(expectedUris, 2, NULL, NULL);
  176. }
  177. END_TEST
  178. // Test if filtering with uris works
  179. START_TEST(Client_find_filter) {
  180. const char* expectedUris[] ={"open62541.test.server_register"};
  181. FindAndCheck(expectedUris, 1, "open62541.test.server_register", NULL);
  182. }
  183. END_TEST
  184. START_TEST(Util_wait_timeout) {
  185. // wait until server is removed by timeout. Additionally wait a few seconds more to be sure.
  186. sleep(checkWait);
  187. }
  188. END_TEST
  189. static Suite* testSuite_Client(void) {
  190. Suite *s = suite_create("Register Server and Client");
  191. TCase *tc_register = tcase_create("RegisterServer");
  192. tcase_add_unchecked_fixture(tc_register, setup_lds, teardown_lds);
  193. tcase_add_unchecked_fixture(tc_register, setup_register, teardown_register);
  194. tcase_add_test(tc_register, Server_register);
  195. // register two times
  196. tcase_add_test(tc_register, Server_register);
  197. tcase_add_test(tc_register, Server_unregister);
  198. suite_add_tcase(s,tc_register);
  199. TCase *tc_register_find = tcase_create("RegisterServer and FindServers");
  200. tcase_add_unchecked_fixture(tc_register_find, setup_lds, teardown_lds);
  201. tcase_add_unchecked_fixture(tc_register_find, setup_register, teardown_register);
  202. tcase_add_test(tc_register_find, Client_find_discovery);
  203. tcase_add_test(tc_register_find, Server_register);
  204. tcase_add_test(tc_register_find, Client_find_registered);
  205. tcase_add_test(tc_register_find, Client_find_filter);
  206. tcase_add_test(tc_register_find, Server_unregister);
  207. tcase_add_test(tc_register_find, Client_find_discovery);
  208. tcase_add_test(tc_register_find, Client_filter_discovery);
  209. suite_add_tcase(s,tc_register_find);
  210. // register server again, then wait for timeout and auto unregister
  211. TCase *tc_register_timeout = tcase_create("RegisterServer timeout");
  212. tcase_add_unchecked_fixture(tc_register_timeout, setup_lds, teardown_lds);
  213. tcase_add_unchecked_fixture(tc_register_timeout, setup_register, teardown_register);
  214. tcase_set_timeout(tc_register_timeout, checkWait+2);
  215. tcase_add_test(tc_register_timeout, Server_register);
  216. tcase_add_test(tc_register_timeout, Client_find_registered);
  217. tcase_add_test(tc_register_timeout, Util_wait_timeout);
  218. tcase_add_test(tc_register_timeout, Client_find_discovery);
  219. suite_add_tcase(s,tc_register_timeout);
  220. return s;
  221. }
  222. int main(void) {
  223. Suite *s = testSuite_Client();
  224. SRunner *sr = srunner_create(s);
  225. srunner_set_fork_status(sr, CK_NOFORK);
  226. srunner_run_all(sr,CK_NORMAL);
  227. int number_failed = srunner_ntests_failed(sr);
  228. srunner_free(sr);
  229. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  230. }