check_server_userspace.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "ua_types.h"
  7. #include "ua_config_default.h"
  8. #include "check.h"
  9. #ifdef __clang__
  10. //required for ck_assert_ptr_eq and const casting
  11. #pragma clang diagnostic push
  12. #pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
  13. #endif
  14. START_TEST(Server_addNamespace_ShallWork) {
  15. UA_ServerConfig *config = UA_ServerConfig_new_default();
  16. UA_Server *server = UA_Server_new(config);
  17. UA_UInt16 a = UA_Server_addNamespace(server, "http://nameOfNamespace");
  18. UA_UInt16 b = UA_Server_addNamespace(server, "http://nameOfNamespace");
  19. UA_UInt16 c = UA_Server_addNamespace(server, "http://nameOfNamespace2");
  20. ck_assert_uint_gt(a, 0);
  21. ck_assert_uint_eq(a,b);
  22. ck_assert_uint_ne(a,c);
  23. UA_Server_delete(server);
  24. UA_ServerConfig_delete(config);
  25. }
  26. END_TEST
  27. START_TEST(Server_addNamespace_writeService) {
  28. UA_ServerConfig *config = UA_ServerConfig_new_default();
  29. UA_Server *server = UA_Server_new(config);
  30. UA_Variant namespaces;
  31. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  32. UA_Server_readValue(server, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACEARRAY),
  33. &namespaces);
  34. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  35. ck_assert_ptr_eq(namespaces.type, &UA_TYPES[UA_TYPES_STRING]);
  36. namespaces.data = UA_realloc(namespaces.data, (namespaces.arrayLength + 1) * sizeof(UA_String));
  37. ++namespaces.arrayLength;
  38. UA_String *ns = (UA_String*)namespaces.data;
  39. ns[namespaces.arrayLength-1] = UA_STRING_ALLOC("test");
  40. size_t nsSize = namespaces.arrayLength;
  41. retval = UA_Server_writeValue(server, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACEARRAY),
  42. namespaces);
  43. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  44. UA_Variant_deleteMembers(&namespaces);
  45. /* Now read again */
  46. UA_Server_readValue(server, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACEARRAY),
  47. &namespaces);
  48. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  49. ck_assert_uint_eq(namespaces.arrayLength, nsSize);
  50. UA_Variant_deleteMembers(&namespaces);
  51. UA_Server_delete(server);
  52. UA_ServerConfig_delete(config);
  53. }
  54. END_TEST
  55. struct nodeIterData {
  56. UA_NodeId id;
  57. UA_Boolean isInverse;
  58. UA_NodeId referenceTypeID;
  59. UA_Boolean hit;
  60. };
  61. #define NODE_ITER_DATA_SIZE 3
  62. static UA_StatusCode
  63. nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void *handle) {
  64. struct nodeIterData* objectsFolderChildren = ( struct nodeIterData*)handle;
  65. ck_assert_int_eq(childId.namespaceIndex, 0);
  66. ck_assert(childId.identifierType == UA_NODEIDTYPE_NUMERIC);
  67. int i;
  68. for (i=0; i<NODE_ITER_DATA_SIZE; i++) {
  69. if (UA_NodeId_equal(&childId, &objectsFolderChildren[i].id)) {
  70. break;
  71. }
  72. }
  73. ck_assert_int_lt(i, NODE_ITER_DATA_SIZE);
  74. ck_assert(objectsFolderChildren[i].isInverse == isInverse);
  75. ck_assert(!objectsFolderChildren[i].hit);
  76. objectsFolderChildren[i].hit = UA_TRUE;
  77. ck_assert(UA_NodeId_equal(&referenceTypeId, &objectsFolderChildren[i].referenceTypeID));
  78. return UA_STATUSCODE_GOOD;
  79. }
  80. START_TEST(Server_forEachChildNodeCall) {
  81. UA_ServerConfig *config = UA_ServerConfig_new_default();
  82. UA_Server *server = UA_Server_new(config);
  83. /* List all the children/references of the objects folder
  84. * The forEachChildNodeCall has to hit all of them */
  85. struct nodeIterData objectsFolderChildren[3];
  86. objectsFolderChildren[0].id = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  87. objectsFolderChildren[0].isInverse = UA_FALSE;
  88. objectsFolderChildren[0].referenceTypeID = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  89. objectsFolderChildren[0].hit = UA_FALSE;
  90. objectsFolderChildren[1].id = UA_NODEID_NUMERIC(0, UA_NS0ID_ROOTFOLDER);
  91. objectsFolderChildren[1].isInverse = UA_TRUE;
  92. objectsFolderChildren[1].referenceTypeID = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  93. objectsFolderChildren[1].hit = UA_FALSE;
  94. objectsFolderChildren[2].id = UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE);
  95. objectsFolderChildren[2].isInverse = UA_FALSE;
  96. objectsFolderChildren[2].referenceTypeID = UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION);
  97. objectsFolderChildren[2].hit = UA_FALSE;
  98. UA_StatusCode retval = UA_Server_forEachChildNodeCall(server, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), nodeIter, &objectsFolderChildren);
  99. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  100. /* Check if all nodes are hit */
  101. for (int i=0; i<NODE_ITER_DATA_SIZE; i++) {
  102. ck_assert(objectsFolderChildren[i].hit);
  103. }
  104. UA_Server_delete(server);
  105. UA_ServerConfig_delete(config);
  106. } END_TEST
  107. START_TEST(Server_set_customHostname) {
  108. UA_String customHost = UA_STRING("fancy-host");
  109. UA_UInt16 port = 10042;
  110. UA_ServerConfig *config = UA_ServerConfig_new_minimal(port, NULL);
  111. UA_ServerConfig_set_customHostname(config, customHost);
  112. UA_Server *server = UA_Server_new(config);
  113. UA_StatusCode retval = UA_Server_run_startup(server);
  114. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  115. // TODO when we have more network layers, extend this
  116. ck_assert_uint_ge(config->networkLayersSize, 1);
  117. for (size_t i=0; i<config->networkLayersSize; i++) {
  118. const UA_ServerNetworkLayer *nl = &config->networkLayers[i];
  119. char discoveryUrl[256];
  120. int len = snprintf(discoveryUrl, 255, "opc.tcp://%.*s:%d/", (int)customHost.length, customHost.data, port);
  121. ck_assert_int_eq(nl->discoveryUrl.length, len);
  122. ck_assert(strncmp(discoveryUrl, (char*)nl->discoveryUrl.data, len)==0);
  123. }
  124. UA_Server_run_shutdown(server);
  125. UA_Server_delete(server);
  126. UA_ServerConfig_delete(config);
  127. }
  128. END_TEST
  129. static Suite* testSuite_ServerUserspace(void) {
  130. Suite *s = suite_create("ServerUserspace");
  131. TCase *tc_core = tcase_create("Core");
  132. tcase_add_test(tc_core, Server_addNamespace_ShallWork);
  133. tcase_add_test(tc_core, Server_addNamespace_writeService);
  134. tcase_add_test(tc_core, Server_forEachChildNodeCall);
  135. tcase_add_test(tc_core, Server_set_customHostname);
  136. suite_add_tcase(s,tc_core);
  137. return s;
  138. }
  139. int main(void) {
  140. int number_failed = 0;
  141. Suite *s;
  142. SRunner *sr;
  143. s = testSuite_ServerUserspace();
  144. sr = srunner_create(s);
  145. srunner_set_fork_status(sr, CK_NOFORK);
  146. srunner_run_all(sr,CK_NORMAL);
  147. number_failed += srunner_ntests_failed(sr);
  148. srunner_free(sr);
  149. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  150. }
  151. #ifdef __clang__
  152. #pragma clang diagnostic pop
  153. #endif