check_discovery.c 23 KB

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