check_services_view.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <server/ua_server_internal.h>
  7. #include "check.h"
  8. #include "ua_server.h"
  9. #include "ua_config_default.h"
  10. #include "ua_network_tcp.h"
  11. #include "thread_wrapper.h"
  12. UA_Server *server_translate_browse;
  13. UA_ServerConfig *server_translate_config;
  14. UA_Boolean *running_translate_browse;
  15. THREAD_HANDLE server_thread_translate_browse;
  16. THREAD_CALLBACK(serverloop_register) {
  17. while (*running_translate_browse)
  18. UA_Server_run_iterate(server_translate_browse, true);
  19. return 0;
  20. }
  21. static void setup_server(void) {
  22. // start server
  23. running_translate_browse = UA_Boolean_new();
  24. *running_translate_browse = true;
  25. server_translate_config = UA_ServerConfig_new_default();
  26. UA_String_deleteMembers(&server_translate_config->applicationDescription.applicationUri);
  27. server_translate_config->applicationDescription.applicationUri =
  28. UA_String_fromChars("urn:open62541.test.server_translate_browse");
  29. server_translate_browse = UA_Server_new(server_translate_config);
  30. UA_Server_run_startup(server_translate_browse);
  31. THREAD_CREATE(server_thread_translate_browse, serverloop_register);
  32. }
  33. static void teardown_server(void) {
  34. *running_translate_browse = false;
  35. THREAD_JOIN(server_thread_translate_browse);
  36. UA_Server_run_shutdown(server_translate_browse);
  37. UA_Boolean_delete(running_translate_browse);
  38. UA_Server_delete(server_translate_browse);
  39. UA_ServerConfig_delete(server_translate_config);
  40. }
  41. static size_t
  42. browseWithMaxResults(UA_Server *server, UA_NodeId nodeId, UA_UInt32 maxResults) {
  43. UA_BrowseDescription bd;
  44. UA_BrowseDescription_init(&bd);
  45. bd.nodeId = nodeId;
  46. bd.resultMask = UA_BROWSERESULTMASK_ALL;
  47. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  48. UA_BrowseResult br = UA_Server_browse(server, maxResults, &bd);
  49. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  50. ck_assert(br.referencesSize > 0);
  51. size_t total = br.referencesSize;
  52. UA_ByteString cp = br.continuationPoint;
  53. br.continuationPoint = UA_BYTESTRING_NULL;
  54. UA_BrowseResult_deleteMembers(&br);
  55. while(cp.length > 0) {
  56. br = UA_Server_browseNext(server, false, &cp);
  57. ck_assert(br.referencesSize > 0);
  58. UA_ByteString_deleteMembers(&cp);
  59. cp = br.continuationPoint;
  60. br.continuationPoint = UA_BYTESTRING_NULL;
  61. total += br.referencesSize;
  62. UA_BrowseResult_deleteMembers(&br);
  63. }
  64. return total;
  65. }
  66. START_TEST(Service_Browse_WithMaxResults) {
  67. UA_ServerConfig *config = UA_ServerConfig_new_default();
  68. UA_Server *server = UA_Server_new(config);
  69. UA_BrowseDescription bd;
  70. UA_BrowseDescription_init(&bd);
  71. bd.resultMask = UA_BROWSERESULTMASK_ALL;
  72. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  73. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  74. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  75. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  76. ck_assert(br.referencesSize > 0);
  77. size_t total = br.referencesSize;
  78. UA_BrowseResult_deleteMembers(&br);
  79. for(UA_UInt32 i = 1; i <= total; i++) {
  80. size_t sum_total =
  81. browseWithMaxResults(server, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), i);
  82. ck_assert_int_eq(total, sum_total);
  83. }
  84. UA_Server_delete(server);
  85. UA_ServerConfig_delete(config);
  86. }
  87. END_TEST
  88. START_TEST(Service_Browse_WithBrowseName) {
  89. UA_ServerConfig *config = UA_ServerConfig_new_default();
  90. UA_Server *server = UA_Server_new(config);
  91. UA_BrowseDescription bd;
  92. UA_BrowseDescription_init(&bd);
  93. bd.resultMask = UA_BROWSERESULTMASK_BROWSENAME;
  94. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  95. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  96. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  97. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  98. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  99. ck_assert(br.referencesSize > 0);
  100. ck_assert(!UA_String_equal(&br.references[0].browseName.name, &UA_STRING_NULL));
  101. UA_BrowseResult_deleteMembers(&br);
  102. UA_Server_delete(server);
  103. UA_ServerConfig_delete(config);
  104. }
  105. END_TEST
  106. START_TEST(Service_TranslateBrowsePathsToNodeIds) {
  107. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  108. UA_StatusCode retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  109. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  110. // Just for testing we want to translate the following path to its corresponding node id
  111. // /Objects/Server/ServerStatus/State
  112. // Equals the following node IDs:
  113. // /85/2253/2256/2259
  114. #define BROWSE_PATHS_SIZE 3
  115. char *paths[BROWSE_PATHS_SIZE] = {"Server", "ServerStatus", "State"};
  116. UA_UInt32 ids[BROWSE_PATHS_SIZE] = {UA_NS0ID_ORGANIZES, UA_NS0ID_HASCOMPONENT, UA_NS0ID_HASCOMPONENT};
  117. UA_BrowsePath browsePath;
  118. UA_BrowsePath_init(&browsePath);
  119. browsePath.startingNode = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  120. browsePath.relativePath.elements = (UA_RelativePathElement*)UA_Array_new(BROWSE_PATHS_SIZE, &UA_TYPES[UA_TYPES_RELATIVEPATHELEMENT]);
  121. browsePath.relativePath.elementsSize = BROWSE_PATHS_SIZE;
  122. for(size_t i = 0; i < BROWSE_PATHS_SIZE; i++) {
  123. UA_RelativePathElement *elem = &browsePath.relativePath.elements[i];
  124. elem->referenceTypeId = UA_NODEID_NUMERIC(0, ids[i]);
  125. elem->targetName = UA_QUALIFIEDNAME_ALLOC(0, paths[i]);
  126. }
  127. UA_TranslateBrowsePathsToNodeIdsRequest request;
  128. UA_TranslateBrowsePathsToNodeIdsRequest_init(&request);
  129. request.browsePaths = &browsePath;
  130. request.browsePathsSize = 1;
  131. UA_TranslateBrowsePathsToNodeIdsResponse response = UA_Client_Service_translateBrowsePathsToNodeIds(client, request);
  132. ck_assert_int_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  133. ck_assert_int_eq(response.resultsSize, 1);
  134. ck_assert_int_eq(response.results[0].targetsSize, 1);
  135. ck_assert_int_eq(response.results[0].targets[0].targetId.nodeId.identifierType, UA_NODEIDTYPE_NUMERIC);
  136. ck_assert_int_eq(response.results[0].targets[0].targetId.nodeId.identifier.numeric, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  137. UA_BrowsePath_deleteMembers(&browsePath);
  138. UA_TranslateBrowsePathsToNodeIdsResponse_deleteMembers(&response);
  139. retVal = UA_Client_disconnect(client);
  140. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  141. UA_Client_delete(client);
  142. }
  143. END_TEST
  144. static Suite *testSuite_Service_TranslateBrowsePathsToNodeIds(void) {
  145. Suite *s = suite_create("Service_TranslateBrowsePathsToNodeIds");
  146. TCase *tc_browse = tcase_create("Browse Service");
  147. tcase_add_test(tc_browse, Service_Browse_WithBrowseName);
  148. tcase_add_test(tc_browse, Service_Browse_WithMaxResults);
  149. suite_add_tcase(s, tc_browse);
  150. TCase *tc_translate = tcase_create("TranslateBrowsePathsToNodeIds");
  151. tcase_add_unchecked_fixture(tc_translate, setup_server, teardown_server);
  152. tcase_add_test(tc_translate, Service_TranslateBrowsePathsToNodeIds);
  153. suite_add_tcase(s, tc_translate);
  154. return s;
  155. }
  156. int main(void) {
  157. int number_failed = 0;
  158. Suite *s;
  159. SRunner *sr;
  160. s = testSuite_Service_TranslateBrowsePathsToNodeIds();
  161. sr = srunner_create(s);
  162. srunner_set_fork_status(sr, CK_NOFORK);
  163. srunner_run_all(sr, CK_NORMAL);
  164. number_failed += srunner_ntests_failed(sr);
  165. srunner_free(sr);
  166. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  167. }