check_discovery.c 22 KB

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