check_discovery.c 23 KB

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