check_services_nodemanagement.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <time.h>
  7. #include "check.h"
  8. #include "server/ua_nodestore.h"
  9. #include "server/ua_services.h"
  10. #include "ua_client.h"
  11. #include "ua_nodeids.h"
  12. #include "ua_types.h"
  13. #include "ua_config_standard.h"
  14. #include "server/ua_server_internal.h"
  15. #ifdef UA_ENABLE_MULTITHREADING
  16. #include <pthread.h>
  17. #include <urcu.h>
  18. #endif
  19. static UA_StatusCode
  20. instantiationMethod(UA_NodeId newNodeId, UA_NodeId templateId, void *handle ) {
  21. *((UA_Int32 *) handle) += 1;
  22. return UA_STATUSCODE_GOOD;
  23. }
  24. START_TEST(AddVariableNode) {
  25. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  26. /* add a variable node to the address space */
  27. UA_VariableAttributes attr;
  28. UA_VariableAttributes_init(&attr);
  29. UA_Int32 myInteger = 42;
  30. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  31. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  32. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  33. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  34. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  35. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  36. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  37. UA_StatusCode res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  38. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  39. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  40. UA_Server_delete(server);
  41. } END_TEST
  42. START_TEST(AddComplexTypeWithInheritance) {
  43. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  44. /* add a variable node to the address space */
  45. UA_ObjectAttributes attr;
  46. UA_ObjectAttributes_init(&attr);
  47. attr.description = UA_LOCALIZEDTEXT("en_US","fakeServerStruct");
  48. attr.displayName = UA_LOCALIZEDTEXT("en_US","fakeServerStruct");
  49. UA_NodeId myObjectNodeId = UA_NODEID_STRING(1, "the.fake.Server.Struct");
  50. UA_QualifiedName myObjectName = UA_QUALIFIEDNAME(1, "the.fake.Server.Struct");
  51. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  52. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  53. UA_Int32 handleCalled = 0;
  54. UA_InstantiationCallback iCallback = {.method=instantiationMethod, .handle = (void *) &handleCalled};
  55. UA_StatusCode res = UA_Server_addObjectNode(server, myObjectNodeId, parentNodeId, parentReferenceNodeId,
  56. myObjectName, UA_NODEID_NUMERIC(0, 2004), attr, &iCallback, NULL);
  57. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  58. ck_assert_int_gt(handleCalled, 0); // Should be 58, but may depend on NS0 XML detail
  59. UA_Server_delete(server);
  60. } END_TEST
  61. START_TEST(AddNodeTwiceGivesError) {
  62. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  63. /* add a variable node to the address space */
  64. UA_VariableAttributes attr;
  65. UA_VariableAttributes_init(&attr);
  66. UA_Int32 myInteger = 42;
  67. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  68. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  69. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  70. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  71. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  72. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  73. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  74. UA_StatusCode res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  75. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  76. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  77. res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  78. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  79. ck_assert_int_eq(res, UA_STATUSCODE_BADNODEIDEXISTS);
  80. UA_Server_delete(server);
  81. } END_TEST
  82. static UA_Boolean constructorCalled = false;
  83. static void * objectConstructor(const UA_NodeId instance) {
  84. constructorCalled = true;
  85. return NULL;
  86. }
  87. START_TEST(AddObjectWithConstructor) {
  88. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  89. /* Add an object type */
  90. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  91. UA_ObjectTypeAttributes attr;
  92. UA_ObjectTypeAttributes_init(&attr);
  93. attr.displayName = UA_LOCALIZEDTEXT("en_US","my objecttype");
  94. UA_StatusCode res = UA_Server_addObjectTypeNode(server, objecttypeid,
  95. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  96. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  97. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  98. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  99. /* Add a constructor to the object type */
  100. UA_ObjectLifecycleManagement olm = {objectConstructor, NULL};
  101. res = UA_Server_setObjectTypeNode_lifecycleManagement(server, objecttypeid, olm);
  102. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  103. /* Add an object of the type */
  104. UA_ObjectAttributes attr2;
  105. UA_ObjectAttributes_init(&attr2);
  106. attr2.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  107. res = UA_Server_addObjectNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  108. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  109. objecttypeid, attr2, NULL, NULL);
  110. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  111. /* Verify that the constructor was called */
  112. ck_assert_int_eq(constructorCalled, true);
  113. UA_Server_delete(server);
  114. } END_TEST
  115. static UA_Boolean destructorCalled = false;
  116. static void objectDestructor(const UA_NodeId instance, void *handle) {
  117. destructorCalled = true;
  118. }
  119. START_TEST(DeleteObjectWithDestructor) {
  120. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  121. /* Add an object type */
  122. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  123. UA_ObjectTypeAttributes attr;
  124. UA_ObjectTypeAttributes_init(&attr);
  125. attr.displayName = UA_LOCALIZEDTEXT("en_US","my objecttype");
  126. UA_StatusCode res = UA_Server_addObjectTypeNode(server, objecttypeid,
  127. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  128. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  129. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  130. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  131. /* Add a constructor to the object type */
  132. UA_ObjectLifecycleManagement olm = {NULL, objectDestructor};
  133. res = UA_Server_setObjectTypeNode_lifecycleManagement(server, objecttypeid, olm);
  134. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  135. /* Add an object of the type */
  136. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  137. UA_ObjectAttributes attr2;
  138. UA_ObjectAttributes_init(&attr2);
  139. attr2.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  140. res = UA_Server_addObjectNode(server, objectid, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  141. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  142. objecttypeid, attr2, NULL, NULL);
  143. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  144. /* Delete the object */
  145. UA_Server_deleteNode(server, objectid, true);
  146. /* Verify that the destructor was called */
  147. ck_assert_int_eq(destructorCalled, true);
  148. UA_Server_delete(server);
  149. } END_TEST
  150. START_TEST(DeleteObjectAndReferences) {
  151. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  152. /* Add an object of the type */
  153. UA_ObjectAttributes attr;
  154. UA_ObjectAttributes_init(&attr);
  155. attr.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  156. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  157. UA_StatusCode res;
  158. res = UA_Server_addObjectNode(server, objectid, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  159. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  160. UA_NODEID_NULL, attr, NULL, NULL);
  161. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  162. /* Verify that we have a reference to the node from the objects folder */
  163. UA_BrowseDescription bd;
  164. UA_BrowseDescription_init(&bd);
  165. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  166. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  167. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  168. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  169. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  170. size_t refCount = 0;
  171. for(size_t i = 0; i < br.referencesSize; ++i) {
  172. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  173. refCount++;
  174. }
  175. ck_assert_int_eq(refCount, 1);
  176. UA_BrowseResult_deleteMembers(&br);
  177. /* Delete the object */
  178. UA_Server_deleteNode(server, objectid, true);
  179. /* Browse again, this time we expect that no reference is found */
  180. br = UA_Server_browse(server, 0, &bd);
  181. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  182. refCount = 0;
  183. for(size_t i = 0; i < br.referencesSize; ++i) {
  184. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  185. refCount++;
  186. }
  187. ck_assert_int_eq(refCount, 0);
  188. UA_BrowseResult_deleteMembers(&br);
  189. /* Add an object the second time */
  190. UA_ObjectAttributes_init(&attr);
  191. attr.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  192. objectid = UA_NODEID_NUMERIC(0, 23372337);
  193. res = UA_Server_addObjectNode(server, objectid, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  194. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  195. UA_NODEID_NULL, attr, NULL, NULL);
  196. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  197. /* Browse again, this time we expect that a single reference to the node is found */
  198. refCount = 0;
  199. br = UA_Server_browse(server, 0, &bd);
  200. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  201. for(size_t i = 0; i < br.referencesSize; ++i) {
  202. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  203. refCount++;
  204. }
  205. ck_assert_int_eq(refCount, 1);
  206. UA_BrowseResult_deleteMembers(&br);
  207. UA_Server_delete(server);
  208. } END_TEST
  209. static Suite * testSuite_services_nodemanagement(void) {
  210. Suite *s = suite_create("services_nodemanagement");
  211. TCase *tc_addnodes = tcase_create("addnodes");
  212. tcase_add_test(tc_addnodes, AddVariableNode);
  213. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  214. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  215. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  216. TCase *tc_deletenodes = tcase_create("deletenodes");
  217. tcase_add_test(tc_addnodes, DeleteObjectWithDestructor);
  218. tcase_add_test(tc_addnodes, DeleteObjectAndReferences);
  219. suite_add_tcase(s, tc_addnodes);
  220. suite_add_tcase(s, tc_deletenodes);
  221. return s;
  222. }
  223. int main(void) {
  224. int number_failed = 0;
  225. Suite *s;
  226. s = testSuite_services_nodemanagement();
  227. SRunner *sr = srunner_create(s);
  228. srunner_set_fork_status(sr, CK_NOFORK);
  229. srunner_run_all(sr, CK_NORMAL);
  230. number_failed += srunner_ntests_failed(sr);
  231. srunner_free(sr);
  232. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  233. }