check_discovery.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 <open62541/client.h>
  5. #include <open62541/client_config_default.h>
  6. #include <open62541/server_config_default.h>
  7. #include "server/ua_server_internal.h"
  8. #include <fcntl.h>
  9. #include "testing_clock.h"
  10. #include "thread_wrapper.h"
  11. #ifndef WIN32
  12. #include <sys/stat.h>
  13. #endif
  14. #include <check.h>
  15. #ifndef _WIN32
  16. #include <sys/stat.h>
  17. #endif
  18. // set register timeout to 1 second so we are able to test it.
  19. #define registerTimeout 1
  20. // cleanup is only triggered every 10 seconds, thus wait a bit longer to check
  21. #define checkWait registerTimeout + 11
  22. UA_Server *server_lds;
  23. UA_Boolean *running_lds;
  24. THREAD_HANDLE server_thread_lds;
  25. UA_Client *clientRegisterRepeated;
  26. THREAD_CALLBACK(serverloop_lds) {
  27. while(*running_lds)
  28. UA_Server_run_iterate(server_lds, true);
  29. return 0;
  30. }
  31. static void setup_lds(void) {
  32. // start LDS server
  33. running_lds = UA_Boolean_new();
  34. *running_lds = true;
  35. server_lds = UA_Server_new();
  36. UA_ServerConfig *config_lds = UA_Server_getConfig(server_lds);
  37. UA_ServerConfig_setDefault(config_lds);
  38. config_lds->applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
  39. UA_String_deleteMembers(&config_lds->applicationDescription.applicationUri);
  40. config_lds->applicationDescription.applicationUri =
  41. UA_STRING_ALLOC("urn:open62541.test.local_discovery_server");
  42. UA_LocalizedText_deleteMembers(&config_lds->applicationDescription.applicationName);
  43. config_lds->applicationDescription.applicationName
  44. = UA_LOCALIZEDTEXT_ALLOC("en", "LDS Server");
  45. config_lds->discovery.mdnsEnable = true;
  46. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  47. config_lds->discovery.mdns.mdnsServerName = UA_String_fromChars("LDS_test");
  48. config_lds->discovery.mdns.serverCapabilitiesSize = 1;
  49. UA_String *caps = UA_String_new();
  50. *caps = UA_String_fromChars("LDS");
  51. config_lds->discovery.mdns.serverCapabilities = caps;
  52. #endif
  53. config_lds->discovery.cleanupTimeout = registerTimeout;
  54. UA_Server_run_startup(server_lds);
  55. THREAD_CREATE(server_thread_lds, serverloop_lds);
  56. // wait until LDS started
  57. UA_fakeSleep(1000);
  58. UA_realSleep(1000);
  59. }
  60. static void teardown_lds(void) {
  61. *running_lds = false;
  62. THREAD_JOIN(server_thread_lds);
  63. UA_Server_run_shutdown(server_lds);
  64. UA_Boolean_delete(running_lds);
  65. UA_Server_delete(server_lds);
  66. }
  67. UA_Server *server_register;
  68. UA_Boolean *running_register;
  69. THREAD_HANDLE server_thread_register;
  70. UA_UInt64 periodicRegisterCallbackId;
  71. THREAD_CALLBACK(serverloop_register) {
  72. while(*running_register)
  73. UA_Server_run_iterate(server_register, true);
  74. return 0;
  75. }
  76. static void setup_register(void) {
  77. // start register server
  78. running_register = UA_Boolean_new();
  79. *running_register = true;
  80. server_register = UA_Server_new();
  81. UA_ServerConfig *config_register = UA_Server_getConfig(server_register);
  82. UA_ServerConfig_setMinimal(config_register, 16664, NULL);
  83. UA_String_deleteMembers(&config_register->applicationDescription.applicationUri);
  84. config_register->applicationDescription.applicationUri =
  85. UA_String_fromChars("urn:open62541.test.server_register");
  86. UA_LocalizedText_deleteMembers(&config_register->applicationDescription.applicationName);
  87. config_register->applicationDescription.applicationName =
  88. UA_LOCALIZEDTEXT_ALLOC("de", "Anmeldungsserver");
  89. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  90. config_register->discovery.mdns.mdnsServerName = UA_String_fromChars("Register_test");
  91. #endif
  92. UA_Server_run_startup(server_register);
  93. THREAD_CREATE(server_thread_register, serverloop_register);
  94. }
  95. static void teardown_register(void) {
  96. *running_register = false;
  97. THREAD_JOIN(server_thread_register);
  98. UA_Server_run_shutdown(server_register);
  99. UA_Boolean_delete(running_register);
  100. UA_Server_delete(server_register);
  101. }
  102. START_TEST(Server_register) {
  103. UA_Client *clientRegister = UA_Client_new();
  104. UA_ClientConfig_setDefault(UA_Client_getConfig(clientRegister));
  105. UA_StatusCode retval = UA_Client_connect_noSession(clientRegister, "opc.tcp://localhost:4840");
  106. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  107. retval = UA_Server_register_discovery(server_register, clientRegister , NULL);
  108. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  109. UA_Client_disconnect(clientRegister);
  110. UA_Client_delete(clientRegister);
  111. }
  112. END_TEST
  113. START_TEST(Server_unregister) {
  114. UA_Client *clientRegister = UA_Client_new();
  115. UA_ClientConfig_setDefault(UA_Client_getConfig(clientRegister));
  116. UA_StatusCode retval = UA_Client_connect_noSession(clientRegister, "opc.tcp://localhost:4840");
  117. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  118. retval = UA_Server_unregister_discovery(server_register, clientRegister);
  119. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  120. UA_Client_disconnect(clientRegister);
  121. UA_Client_delete(clientRegister);
  122. }
  123. END_TEST
  124. #ifdef UA_ENABLE_DISCOVERY_SEMAPHORE
  125. #ifndef WIN32
  126. #define SEMAPHORE_PATH "/tmp/open62541-unit-test-semaphore"
  127. #else
  128. #define SEMAPHORE_PATH ".\\open62541-unit-test-semaphore"
  129. #endif
  130. START_TEST(Server_register_semaphore) {
  131. // create the semaphore
  132. #ifndef WIN32
  133. int fd = open(SEMAPHORE_PATH, O_RDWR|O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
  134. ck_assert_int_ne(fd, -1);
  135. close(fd);
  136. #else
  137. FILE *fp;
  138. fopen_s(&fp, SEMAPHORE_PATH, "ab+");
  139. ck_assert_ptr_ne(fp, NULL);
  140. fclose(fp);
  141. #endif
  142. UA_Client *clientRegister = UA_Client_new();
  143. UA_ClientConfig_setDefault(UA_Client_getConfig(clientRegister));
  144. UA_StatusCode retval = UA_Client_connect_noSession(clientRegister, "opc.tcp://localhost:4840");
  145. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  146. retval = UA_Server_register_discovery(server_register, clientRegister, SEMAPHORE_PATH);
  147. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  148. UA_Client_disconnect(clientRegister);
  149. UA_Client_delete(clientRegister);
  150. }
  151. END_TEST
  152. START_TEST(Server_unregister_semaphore) {
  153. // delete the semaphore, this should remove the registration automatically on next check
  154. ck_assert_int_eq(remove(SEMAPHORE_PATH), 0);
  155. }
  156. END_TEST
  157. #endif /* UA_ENABLE_DISCOVERY_SEMAPHORE */
  158. START_TEST(Server_register_periodic) {
  159. ck_assert(clientRegisterRepeated == NULL);
  160. clientRegisterRepeated = UA_Client_new();
  161. UA_ClientConfig_setDefault(UA_Client_getConfig(clientRegisterRepeated));
  162. ck_assert(clientRegisterRepeated != NULL);
  163. // periodic register every minute, first register immediately
  164. UA_StatusCode retval = UA_Server_addPeriodicServerRegisterCallback(server_register, clientRegisterRepeated, "opc.tcp://localhost:4840",
  165. 60*1000, 100, &periodicRegisterCallbackId);
  166. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  167. }
  168. END_TEST
  169. START_TEST(Server_unregister_periodic) {
  170. // wait for first register delay
  171. UA_fakeSleep(1000);
  172. UA_realSleep(1000);
  173. UA_Server_removeRepeatedCallback(server_register, periodicRegisterCallbackId);
  174. UA_StatusCode retval = UA_Server_unregister_discovery(server_register, clientRegisterRepeated);
  175. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  176. UA_Client_disconnect(clientRegisterRepeated);
  177. UA_Client_delete(clientRegisterRepeated);
  178. clientRegisterRepeated=NULL;
  179. }
  180. END_TEST
  181. static void
  182. FindAndCheck(const UA_String expectedUris[], size_t expectedUrisSize,
  183. const UA_String expectedLocales[],
  184. const UA_String expectedNames[],
  185. const char *filterUri,
  186. const char *filterLocale) {
  187. UA_Client *client = UA_Client_new();
  188. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  189. UA_ApplicationDescription* applicationDescriptionArray = NULL;
  190. size_t applicationDescriptionArraySize = 0;
  191. size_t serverUrisSize = 0;
  192. UA_String *serverUris = NULL;
  193. if(filterUri) {
  194. serverUrisSize = 1;
  195. serverUris = UA_String_new();
  196. serverUris[0] = UA_String_fromChars(filterUri);
  197. }
  198. size_t localeIdsSize = 0;
  199. UA_String *localeIds = NULL;
  200. if(filterLocale) {
  201. localeIdsSize = 1;
  202. localeIds = UA_String_new();
  203. localeIds[0] = UA_String_fromChars(filterLocale);
  204. }
  205. UA_StatusCode retval =
  206. UA_Client_findServers(client, "opc.tcp://localhost:4840",
  207. serverUrisSize, serverUris, localeIdsSize, localeIds,
  208. &applicationDescriptionArraySize, &applicationDescriptionArray);
  209. if(filterUri)
  210. UA_Array_delete(serverUris, serverUrisSize, &UA_TYPES[UA_TYPES_STRING]);
  211. if(filterLocale)
  212. UA_Array_delete(localeIds, localeIdsSize, &UA_TYPES[UA_TYPES_STRING]);
  213. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  214. // only the discovery server is expected
  215. ck_assert_uint_eq(applicationDescriptionArraySize, expectedUrisSize);
  216. ck_assert(applicationDescriptionArray != NULL);
  217. for(size_t i = 0; i < expectedUrisSize; ++i) {
  218. ck_assert(UA_String_equal(&applicationDescriptionArray[i].applicationUri,
  219. &expectedUris[i]));
  220. if(expectedNames)
  221. ck_assert(UA_String_equal(&applicationDescriptionArray[i].applicationName.text,
  222. &expectedNames[i]));
  223. if (expectedLocales)
  224. ck_assert(UA_String_equal(&applicationDescriptionArray[i].applicationName.locale,
  225. &expectedLocales[i]));
  226. }
  227. UA_Array_delete(applicationDescriptionArray, applicationDescriptionArraySize,
  228. &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]);
  229. UA_Client_disconnect(client);
  230. UA_Client_delete(client);
  231. }
  232. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  233. static void
  234. FindOnNetworkAndCheck(UA_String expectedServerNames[], size_t expectedServerNamesSize,
  235. const char *filterUri, const char *filterLocale,
  236. const char** filterCapabilities, size_t filterCapabilitiesSize) {
  237. UA_Client *client = UA_Client_new();
  238. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  239. UA_ServerOnNetwork* serverOnNetwork = NULL;
  240. size_t serverOnNetworkSize = 0;
  241. size_t serverCapabilityFilterSize = 0;
  242. UA_String *serverCapabilityFilter = NULL;
  243. if(filterCapabilitiesSize) {
  244. serverCapabilityFilterSize = filterCapabilitiesSize;
  245. serverCapabilityFilter = (UA_String*)UA_malloc(sizeof(UA_String) * filterCapabilitiesSize);
  246. for(size_t i = 0; i < filterCapabilitiesSize; i++)
  247. serverCapabilityFilter[i] = UA_String_fromChars(filterCapabilities[i]);
  248. }
  249. UA_StatusCode retval =
  250. UA_Client_findServersOnNetwork(client, "opc.tcp://localhost:4840", 0, 0,
  251. serverCapabilityFilterSize, serverCapabilityFilter,
  252. &serverOnNetworkSize, &serverOnNetwork);
  253. if(serverCapabilityFilterSize)
  254. UA_Array_delete(serverCapabilityFilter, serverCapabilityFilterSize,
  255. &UA_TYPES[UA_TYPES_STRING]);
  256. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  257. // only the discovery server is expected
  258. ck_assert_uint_eq(serverOnNetworkSize , expectedServerNamesSize);
  259. if(expectedServerNamesSize > 0)
  260. ck_assert_ptr_ne(serverOnNetwork, NULL);
  261. if(serverOnNetwork != NULL) {
  262. for(size_t i = 0; i < expectedServerNamesSize; i++) {
  263. UA_Boolean expectedServerNameInServerOnNetwork = false;
  264. for(size_t j = 0; j < expectedServerNamesSize && !expectedServerNameInServerOnNetwork; j++) {
  265. expectedServerNameInServerOnNetwork = UA_String_equal(&serverOnNetwork[j].serverName,
  266. &expectedServerNames[i]);
  267. }
  268. ck_assert_msg(expectedServerNameInServerOnNetwork, "Expected %.*s in serverOnNetwork list, but not found",
  269. expectedServerNames[i].length, expectedServerNames[i].data);
  270. }
  271. }
  272. UA_Array_delete(serverOnNetwork, serverOnNetworkSize, &UA_TYPES[UA_TYPES_SERVERONNETWORK]);
  273. UA_Client_disconnect(client);
  274. UA_Client_delete(client);
  275. }
  276. static UA_StatusCode
  277. GetEndpoints(UA_Client *client, const UA_String* endpointUrl,
  278. size_t* endpointDescriptionsSize,
  279. UA_EndpointDescription** endpointDescriptions,
  280. const char* filterTransportProfileUri) {
  281. UA_GetEndpointsRequest request;
  282. UA_GetEndpointsRequest_init(&request);
  283. //request.requestHeader.authenticationToken = client->authenticationToken;
  284. request.requestHeader.timestamp = UA_DateTime_now();
  285. request.requestHeader.timeoutHint = 10000;
  286. request.endpointUrl = *endpointUrl; // assume the endpointurl outlives the service call
  287. if (filterTransportProfileUri) {
  288. request.profileUrisSize = 1;
  289. request.profileUris = (UA_String*)UA_malloc(sizeof(UA_String));
  290. request.profileUris[0] = UA_String_fromChars(filterTransportProfileUri);
  291. }
  292. UA_GetEndpointsResponse response;
  293. UA_GetEndpointsResponse_init(&response);
  294. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST],
  295. &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]);
  296. if (filterTransportProfileUri) {
  297. UA_Array_delete(request.profileUris, request.profileUrisSize, &UA_TYPES[UA_TYPES_STRING]);
  298. }
  299. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  300. *endpointDescriptionsSize = response.endpointsSize;
  301. *endpointDescriptions =
  302. (UA_EndpointDescription*)UA_Array_new(response.endpointsSize,
  303. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  304. for(size_t i=0;i<response.endpointsSize;i++) {
  305. UA_EndpointDescription_init(&(*endpointDescriptions)[i]);
  306. UA_EndpointDescription_copy(&response.endpoints[i], &(*endpointDescriptions)[i]);
  307. }
  308. UA_GetEndpointsResponse_deleteMembers(&response);
  309. return UA_STATUSCODE_GOOD;
  310. }
  311. static void
  312. GetEndpointsAndCheck(const char* discoveryUrl, const char* filterTransportProfileUri,
  313. const UA_String expectedEndpointUrls[], size_t expectedEndpointUrlsSize) {
  314. UA_Client *client = UA_Client_new();
  315. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  316. ck_assert_uint_eq(UA_Client_connect(client, discoveryUrl), UA_STATUSCODE_GOOD);
  317. UA_EndpointDescription* endpointArray = NULL;
  318. size_t endpointArraySize = 0;
  319. UA_String discoveryUrlUA = UA_String_fromChars(discoveryUrl);
  320. UA_StatusCode retval = GetEndpoints(client, &discoveryUrlUA, &endpointArraySize,
  321. &endpointArray, filterTransportProfileUri);
  322. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  323. UA_String_deleteMembers(&discoveryUrlUA);
  324. ck_assert_uint_eq(endpointArraySize , expectedEndpointUrlsSize);
  325. for(size_t j = 0; j < endpointArraySize && j < expectedEndpointUrlsSize; j++) {
  326. UA_EndpointDescription* endpoint = &endpointArray[j];
  327. ck_assert(UA_String_equal(&endpoint->endpointUrl, &expectedEndpointUrls[j]));
  328. }
  329. UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  330. UA_Client_delete(client);
  331. }
  332. // Test if discovery server lists himself as registered server if it is filtered by his uri
  333. START_TEST(Client_filter_discovery) {
  334. UA_String expectedUris[1];
  335. expectedUris[0] = UA_STRING("urn:open62541.test.local_discovery_server");
  336. FindAndCheck(expectedUris, 1, NULL, NULL, "urn:open62541.test.local_discovery_server", NULL);
  337. }
  338. END_TEST
  339. // Test if server filters locale
  340. START_TEST(Client_filter_locale) {
  341. UA_String expectedUris[2];
  342. expectedUris[0] = UA_STRING("urn:open62541.test.local_discovery_server"),
  343. expectedUris[1] = UA_STRING("urn:open62541.test.server_register");
  344. UA_String expectedNames[2];
  345. expectedNames[0]= UA_STRING("LDS Server");
  346. expectedNames[1]= UA_STRING("Anmeldungsserver");
  347. UA_String expectedLocales[2];
  348. expectedLocales[0] = UA_STRING("en");
  349. expectedLocales[1] = UA_STRING("de");
  350. // even if we request en-US, the server will return de-DE because it only has that name.
  351. FindAndCheck(expectedUris, 2, expectedLocales, expectedNames, NULL, "en");
  352. }
  353. END_TEST
  354. // Test if registered server is returned from LDS using FindServersOnNetwork
  355. START_TEST(Client_find_on_network_registered) {
  356. char urls[2][64];
  357. UA_String expectedUris[2];
  358. char hostname[256];
  359. ck_assert_uint_eq(gethostname(hostname, 255), 0);
  360. //DNS limits name to max 63 chars (+ \0)
  361. //We need this ugly casting, otherwise gcc >7.2 will complain about format-truncation, but we want it here
  362. void *hostnameVoid = (void*)hostname;
  363. snprintf(urls[0], 64, "LDS_test-%s", (char*)hostnameVoid);
  364. snprintf(urls[1], 64, "Register_test-%s", (char*)hostnameVoid);
  365. expectedUris[0] = UA_STRING(urls[0]);
  366. expectedUris[1] = UA_STRING(urls[1]);
  367. FindOnNetworkAndCheck(expectedUris, 2, NULL, NULL, NULL, 0);
  368. // filter by Capabilities
  369. const char* capsLDS[] = {"LDS"};
  370. const char* capsNA[] = {"NA"};
  371. const char* capsMultiple[] = {"LDS", "NA"};
  372. // only LDS expected
  373. FindOnNetworkAndCheck(expectedUris, 1, NULL, NULL, capsLDS, 1);
  374. // only register server expected
  375. FindOnNetworkAndCheck(&expectedUris[1], 1, NULL, NULL, capsNA, 1);
  376. // no server expected
  377. FindOnNetworkAndCheck(NULL, 0, NULL, NULL, capsMultiple, 2);
  378. }
  379. END_TEST
  380. // Test if filtering with uris works
  381. START_TEST(Client_find_filter) {
  382. UA_String expectedUris[1];
  383. expectedUris[0] = UA_STRING("urn:open62541.test.server_register");
  384. FindAndCheck(expectedUris, 1, NULL, NULL, "urn:open62541.test.server_register", NULL);
  385. }
  386. END_TEST
  387. START_TEST(Client_get_endpoints) {
  388. UA_String expectedEndpoints[1];
  389. expectedEndpoints[0] = UA_STRING("opc.tcp://localhost:4840");
  390. // general check if expected endpoints are returned
  391. GetEndpointsAndCheck("opc.tcp://localhost:4840", NULL,expectedEndpoints, 1);
  392. // check if filtering transport profile still returns the endpoint
  393. GetEndpointsAndCheck("opc.tcp://localhost:4840",
  394. "http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary",
  395. expectedEndpoints, 1);
  396. // filter transport profily by HTTPS, which should return no endpoint
  397. GetEndpointsAndCheck("opc.tcp://localhost:4840",
  398. "http://opcfoundation.org/UA-Profile/Transport/https-uabinary", NULL, 0);
  399. }
  400. END_TEST
  401. #endif
  402. // Test if discovery server lists himself as registered server, before any other registration.
  403. START_TEST(Client_find_discovery) {
  404. UA_String expectedUris[1];
  405. expectedUris[0] = UA_STRING("urn:open62541.test.local_discovery_server");
  406. FindAndCheck(expectedUris, 1, NULL, NULL, NULL, NULL);
  407. }
  408. END_TEST
  409. // Test if registered server is returned from LDS
  410. START_TEST(Client_find_registered) {
  411. UA_String expectedUris[2];
  412. expectedUris[0] = UA_STRING("urn:open62541.test.local_discovery_server");
  413. expectedUris[1] = UA_STRING("urn:open62541.test.server_register");
  414. FindAndCheck(expectedUris, 2, NULL, NULL, NULL, NULL);
  415. }
  416. END_TEST
  417. START_TEST(Util_start_lds) {
  418. setup_lds();
  419. }
  420. END_TEST
  421. START_TEST(Util_stop_lds) {
  422. teardown_lds();
  423. }
  424. END_TEST
  425. START_TEST(Util_wait_timeout) {
  426. // wait until server is removed by timeout. Additionally wait a few seconds more to be sure.
  427. UA_fakeSleep(100000 * checkWait);
  428. UA_realSleep(1000);
  429. }
  430. END_TEST
  431. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  432. START_TEST(Util_wait_mdns) {
  433. UA_fakeSleep(5000);
  434. UA_realSleep(5000);
  435. }
  436. END_TEST
  437. #endif
  438. START_TEST(Util_wait_startup) {
  439. UA_fakeSleep(1000);
  440. UA_realSleep(1000);
  441. }
  442. END_TEST
  443. START_TEST(Util_wait_retry) {
  444. // first retry is after 2 seconds, then 4, so it should be enough to wait 3 seconds
  445. UA_fakeSleep(3000);
  446. UA_realSleep(3000);
  447. }
  448. END_TEST
  449. static Suite* testSuite_Client(void) {
  450. Suite *s = suite_create("Register Server and Client");
  451. TCase *tc_register = tcase_create("RegisterServer");
  452. tcase_add_unchecked_fixture(tc_register, setup_lds, teardown_lds);
  453. tcase_add_unchecked_fixture(tc_register, setup_register, teardown_register);
  454. tcase_add_test(tc_register, Server_register);
  455. // register two times, just for fun
  456. tcase_add_test(tc_register, Server_register);
  457. tcase_add_test(tc_register, Server_unregister);
  458. tcase_add_test(tc_register, Server_register_periodic);
  459. tcase_add_test(tc_register, Server_unregister_periodic);
  460. suite_add_tcase(s,tc_register);
  461. TCase *tc_register_retry = tcase_create("RegisterServer Retry");
  462. //tcase_add_unchecked_fixture(tc_register, setup_lds, teardown_lds);
  463. tcase_add_unchecked_fixture(tc_register_retry, setup_register, teardown_register);
  464. tcase_add_test(tc_register_retry, Server_register_periodic);
  465. tcase_add_test(tc_register_retry, Util_wait_startup); // wait a bit to let first try run through
  466. // now start LDS
  467. tcase_add_test(tc_register_retry, Util_start_lds);
  468. tcase_add_test(tc_register_retry, Util_wait_retry);
  469. // check if there
  470. tcase_add_test(tc_register_retry, Client_find_registered);
  471. tcase_add_test(tc_register_retry, Server_unregister_periodic);
  472. tcase_add_test(tc_register_retry, Client_find_discovery);
  473. tcase_add_test(tc_register_retry, Util_stop_lds);
  474. suite_add_tcase(s,tc_register_retry);
  475. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  476. TCase *tc_register_find = tcase_create("RegisterServer and FindServers");
  477. tcase_add_unchecked_fixture(tc_register_find, setup_lds, teardown_lds);
  478. tcase_add_unchecked_fixture(tc_register_find, setup_register, teardown_register);
  479. tcase_add_test(tc_register_find, Client_find_discovery);
  480. tcase_add_test(tc_register_find, Server_register);
  481. tcase_add_test(tc_register_find, Client_find_registered);
  482. tcase_add_test(tc_register_find, Util_wait_mdns);
  483. tcase_add_test(tc_register_find, Client_find_on_network_registered);
  484. tcase_add_test(tc_register_find, Client_find_filter);
  485. tcase_add_test(tc_register_find, Client_get_endpoints);
  486. tcase_add_test(tc_register_find, Client_filter_locale);
  487. tcase_add_test(tc_register_find, Server_unregister);
  488. tcase_add_test(tc_register_find, Client_find_discovery);
  489. tcase_add_test(tc_register_find, Client_filter_discovery);
  490. suite_add_tcase(s,tc_register_find);
  491. #endif
  492. // register server again, then wait for timeout and auto unregister
  493. TCase *tc_register_timeout = tcase_create("RegisterServer timeout");
  494. tcase_add_unchecked_fixture(tc_register_timeout, setup_lds, teardown_lds);
  495. tcase_add_unchecked_fixture(tc_register_timeout, setup_register, teardown_register);
  496. tcase_set_timeout(tc_register_timeout, checkWait+2);
  497. tcase_add_test(tc_register_timeout, Server_register);
  498. tcase_add_test(tc_register_timeout, Client_find_registered);
  499. tcase_add_test(tc_register_timeout, Util_wait_timeout);
  500. tcase_add_test(tc_register_timeout, Client_find_discovery);
  501. #ifdef UA_ENABLE_DISCOVERY_SEMAPHORE
  502. // now check if semaphore file works
  503. tcase_add_test(tc_register_timeout, Server_register_semaphore);
  504. tcase_add_test(tc_register_timeout, Client_find_registered);
  505. tcase_add_test(tc_register_timeout, Server_unregister_semaphore);
  506. tcase_add_test(tc_register_timeout, Util_wait_timeout);
  507. tcase_add_test(tc_register_timeout, Client_find_discovery);
  508. #endif
  509. suite_add_tcase(s,tc_register_timeout);
  510. return s;
  511. }
  512. int main(void) {
  513. Suite *s = testSuite_Client();
  514. SRunner *sr = srunner_create(s);
  515. srunner_set_fork_status(sr, CK_NOFORK);
  516. srunner_run_all(sr,CK_NORMAL);
  517. int number_failed = srunner_ntests_failed(sr);
  518. srunner_free(sr);
  519. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  520. }