check_discovery.c 22 KB

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