check_services_view.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include <server/ua_server_internal.h>
  8. #include "check.h"
  9. #include "ua_server.h"
  10. #include "ua_config_standard.h"
  11. #include "ua_network_tcp.h"
  12. UA_Server *server_translate_browse;
  13. UA_Boolean *running_translate_browse;
  14. UA_ServerNetworkLayer nl_translate_browse;
  15. pthread_t server_thread_translate_browse;
  16. static void *serverloop_register(void *_) {
  17. while (*running_translate_browse)
  18. UA_Server_run_iterate(server_translate_browse, true);
  19. return NULL;
  20. }
  21. static void setup_server(void) {
  22. // start server
  23. running_translate_browse = UA_Boolean_new();
  24. *running_translate_browse = true;
  25. UA_ServerConfig config_register = UA_ServerConfig_standard;
  26. config_register.applicationDescription.applicationUri = UA_String_fromChars("urn:open62541.test.server_translate_browse");
  27. nl_translate_browse = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  28. config_register.networkLayers = &nl_translate_browse;
  29. config_register.networkLayersSize = 1;
  30. server_translate_browse = UA_Server_new(config_register);
  31. UA_Server_run_startup(server_translate_browse);
  32. pthread_create(&server_thread_translate_browse, NULL, serverloop_register, NULL);
  33. }
  34. static void teardown_server(void) {
  35. *running_translate_browse = false;
  36. pthread_join(server_thread_translate_browse, NULL);
  37. UA_Server_run_shutdown(server_translate_browse);
  38. UA_Boolean_delete(running_translate_browse);
  39. UA_String_deleteMembers(&server_translate_browse->config.applicationDescription.applicationUri);
  40. UA_Server_delete(server_translate_browse);
  41. nl_translate_browse.deleteMembers(&nl_translate_browse);
  42. }
  43. START_TEST(Service_Browse_WithBrowseName)
  44. {
  45. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  46. UA_BrowseDescription bd;
  47. UA_BrowseDescription_init(&bd);
  48. bd.resultMask = UA_BROWSERESULTMASK_BROWSENAME;
  49. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  50. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  51. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  52. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  53. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  54. ck_assert(br.referencesSize > 0);
  55. ck_assert(!UA_String_equal(&br.references[0].browseName.name, &UA_STRING_NULL));
  56. UA_BrowseResult_deleteMembers(&br);
  57. UA_Server_delete(server);
  58. }
  59. END_TEST
  60. #define BROWSE_PATHS_SIZE 3
  61. START_TEST(Service_TranslateBrowsePathsToNodeIds)
  62. {
  63. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  64. UA_StatusCode retVal = UA_Client_connect(client, "opc.tcp://localhost:16664");
  65. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  66. // Just for testing we want to translate the following path to its corresponding node id
  67. // /Objects/Server/ServerStatus/State
  68. // Equals the following node IDs:
  69. // /85/2253/2256/2259
  70. char *paths[BROWSE_PATHS_SIZE] = {"Server", "ServerStatus", "State"};
  71. UA_UInt32 ids[BROWSE_PATHS_SIZE] = {2253, 2256, 2259};
  72. UA_BrowsePath *browsePaths = UA_Array_new(BROWSE_PATHS_SIZE, &UA_TYPES[UA_TYPES_BROWSEPATH]);
  73. for (unsigned int i = 0; i < BROWSE_PATHS_SIZE; i++) {
  74. UA_BrowsePath_init(&browsePaths[i]);
  75. browsePaths[i].startingNode = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  76. browsePaths[i].relativePath.elementsSize = i + 1;
  77. browsePaths[i].relativePath.elements = UA_Array_new(i + 1, &UA_TYPES[UA_TYPES_RELATIVEPATHELEMENT]);
  78. for (unsigned int j = 0; j <= i; j++) {
  79. UA_RelativePathElement_init(&browsePaths[i].relativePath.elements[j]);
  80. browsePaths[i].relativePath.elements[j].isInverse = UA_TRUE;
  81. browsePaths[i].relativePath.elements[j].targetName = UA_QUALIFIEDNAME_ALLOC(0, paths[j]);
  82. }
  83. }
  84. UA_TranslateBrowsePathsToNodeIdsRequest request;
  85. UA_TranslateBrowsePathsToNodeIdsRequest_init(&request);
  86. request.browsePaths = browsePaths;
  87. request.browsePathsSize = BROWSE_PATHS_SIZE;
  88. {
  89. UA_TranslateBrowsePathsToNodeIdsResponse response = UA_Client_Service_translateBrowsePathsToNodeIds(client, request);
  90. ck_assert_int_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  91. ck_assert_int_eq(response.resultsSize, BROWSE_PATHS_SIZE);
  92. for (int i = 0; i < BROWSE_PATHS_SIZE; i++) {
  93. ck_assert_int_eq(response.results[i].targetsSize, 1);
  94. ck_assert_int_eq(response.results[i].targets[0].targetId.nodeId.identifierType, UA_NODEIDTYPE_NUMERIC);
  95. ck_assert_int_eq(response.results[i].targets[0].targetId.nodeId.identifier.numeric, ids[i]);
  96. }
  97. UA_TranslateBrowsePathsToNodeIdsRequest_deleteMembers(&request);
  98. UA_TranslateBrowsePathsToNodeIdsResponse_deleteMembers(&response);
  99. retVal = UA_Client_disconnect(client);
  100. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  101. UA_Client_delete(client);
  102. }
  103. }
  104. END_TEST
  105. static Suite *testSuite_Service_TranslateBrowsePathsToNodeIds(void) {
  106. Suite *s = suite_create("Service_TranslateBrowsePathsToNodeIds");
  107. TCase *tc_browse = tcase_create("Browse Service");
  108. tcase_add_test(tc_browse, Service_Browse_WithBrowseName);
  109. suite_add_tcase(s, tc_browse);
  110. TCase *tc_translate = tcase_create("TranslateBrowsePathsToNodeIds");
  111. tcase_add_unchecked_fixture(tc_translate, setup_server, teardown_server);
  112. tcase_add_test(tc_translate, Service_TranslateBrowsePathsToNodeIds);
  113. suite_add_tcase(s, tc_translate);
  114. return s;
  115. }
  116. int main(void) {
  117. int number_failed = 0;
  118. Suite *s;
  119. SRunner *sr;
  120. s = testSuite_Service_TranslateBrowsePathsToNodeIds();
  121. sr = srunner_create(s);
  122. srunner_set_fork_status(sr, CK_NOFORK);
  123. srunner_run_all(sr, CK_NORMAL);
  124. number_failed += srunner_ntests_failed(sr);
  125. srunner_free(sr);
  126. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  127. }