check_server_userspace.c 6.6 KB

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