check_discovery.c 24 KB

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