check_services_nodemanagement.c 11 KB

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