check_services_nodemanagement.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 denstructor was called */
  144. ck_assert_int_eq(destructorCalled, true);
  145. UA_Server_delete(server);
  146. } END_TEST
  147. static Suite * testSuite_services_nodemanagement(void) {
  148. Suite *s = suite_create("services_nodemanagement");
  149. TCase *tc_addnodes = tcase_create("addnodes");
  150. tcase_add_test(tc_addnodes, AddVariableNode);
  151. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  152. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  153. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  154. TCase *tc_deletenodes = tcase_create("deletenodes");
  155. tcase_add_test(tc_addnodes, DeleteObjectWithDestructor);
  156. suite_add_tcase(s, tc_addnodes);
  157. suite_add_tcase(s, tc_deletenodes);
  158. return s;
  159. }
  160. int main(void) {
  161. int number_failed = 0;
  162. Suite *s;
  163. s = testSuite_services_nodemanagement();
  164. SRunner *sr = srunner_create(s);
  165. srunner_set_fork_status(sr, CK_NOFORK);
  166. srunner_run_all(sr, CK_NORMAL);
  167. number_failed += srunner_ntests_failed(sr);
  168. srunner_free(sr);
  169. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  170. }