check_discovery.c 23 KB

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