check_discovery.c 24 KB

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