check_services_nodemanagement.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_types.h"
  12. #include "ua_config_standard.h"
  13. #include "server/ua_server_internal.h"
  14. #ifdef UA_ENABLE_MULTITHREADING
  15. #include <pthread.h>
  16. #include <urcu.h>
  17. #endif
  18. static UA_StatusCode
  19. instantiationMethod(UA_NodeId newNodeId, UA_NodeId templateId, void *handle ) {
  20. *((UA_Int32 *) handle) += 1;
  21. return UA_STATUSCODE_GOOD;
  22. }
  23. START_TEST(AddVariableNode) {
  24. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  25. /* add a variable node to the address space */
  26. UA_VariableAttributes attr;
  27. UA_VariableAttributes_init(&attr);
  28. UA_Int32 myInteger = 42;
  29. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  30. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  31. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  32. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  33. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  34. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  35. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  36. UA_StatusCode res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  37. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  38. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  39. UA_Server_delete(server);
  40. } END_TEST
  41. START_TEST(AddComplexTypeWithInheritance) {
  42. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  43. /* add a variable node to the address space */
  44. UA_ObjectAttributes attr;
  45. UA_ObjectAttributes_init(&attr);
  46. attr.description = UA_LOCALIZEDTEXT("en_US","fakeServerStruct");
  47. attr.displayName = UA_LOCALIZEDTEXT("en_US","fakeServerStruct");
  48. UA_NodeId myObjectNodeId = UA_NODEID_STRING(1, "the.fake.Server.Struct");
  49. UA_QualifiedName myObjectName = UA_QUALIFIEDNAME(1, "the.fake.Server.Struct");
  50. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  51. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  52. UA_Int32 handleCalled = 0;
  53. UA_InstantiationCallback iCallback = {.method=instantiationMethod, .handle = (void *) &handleCalled};
  54. UA_StatusCode res = UA_Server_addObjectNode(server, myObjectNodeId, parentNodeId, parentReferenceNodeId,
  55. myObjectName, UA_NODEID_NUMERIC(0, 2004), attr, &iCallback, NULL);
  56. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  57. ck_assert_int_gt(handleCalled, 0); // Should be 58, but may depend on NS0 XML detail
  58. UA_Server_delete(server);
  59. } END_TEST
  60. START_TEST(AddNodeTwiceGivesError) {
  61. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  62. /* add a variable node to the address space */
  63. UA_VariableAttributes attr;
  64. UA_VariableAttributes_init(&attr);
  65. UA_Int32 myInteger = 42;
  66. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  67. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  68. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  69. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  70. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  71. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  72. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  73. UA_StatusCode res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  74. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  75. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  76. res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  77. myIntegerName, UA_NODEID_NULL, attr, NULL, NULL);
  78. ck_assert_int_eq(res, UA_STATUSCODE_BADNODEIDEXISTS);
  79. UA_Server_delete(server);
  80. } END_TEST
  81. static UA_Boolean constructorCalled = false;
  82. static void * objectConstructor(const UA_NodeId instance) {
  83. constructorCalled = true;
  84. return NULL;
  85. }
  86. START_TEST(AddObjectWithConstructor) {
  87. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  88. /* Add an object type */
  89. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  90. UA_ObjectTypeAttributes attr;
  91. UA_ObjectTypeAttributes_init(&attr);
  92. attr.displayName = UA_LOCALIZEDTEXT("en_US","my objecttype");
  93. UA_StatusCode res = UA_Server_addObjectTypeNode(server, objecttypeid,
  94. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  95. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  96. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  97. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  98. /* Add a constructor to the object type */
  99. UA_ObjectLifecycleManagement olm = {objectConstructor, NULL};
  100. res = UA_Server_setObjectTypeNode_lifecycleManagement(server, objecttypeid, olm);
  101. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  102. /* Add an object of the type */
  103. UA_ObjectAttributes attr2;
  104. UA_ObjectAttributes_init(&attr2);
  105. attr2.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  106. res = UA_Server_addObjectNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  107. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  108. objecttypeid, attr2, NULL, NULL);
  109. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  110. /* Verify that the constructor was called */
  111. ck_assert_int_eq(constructorCalled, true);
  112. UA_Server_delete(server);
  113. } END_TEST
  114. static UA_Boolean destructorCalled = false;
  115. static void objectDestructor(const UA_NodeId instance, void *handle) {
  116. destructorCalled = true;
  117. }
  118. START_TEST(DeleteObjectWithDestructor) {
  119. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  120. /* Add an object type */
  121. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  122. UA_ObjectTypeAttributes attr;
  123. UA_ObjectTypeAttributes_init(&attr);
  124. attr.displayName = UA_LOCALIZEDTEXT("en_US","my objecttype");
  125. UA_StatusCode res = UA_Server_addObjectTypeNode(server, objecttypeid,
  126. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  127. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  128. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  129. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  130. /* Add a constructor to the object type */
  131. UA_ObjectLifecycleManagement olm = {NULL, objectDestructor};
  132. res = UA_Server_setObjectTypeNode_lifecycleManagement(server, objecttypeid, olm);
  133. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  134. /* Add an object of the type */
  135. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  136. UA_ObjectAttributes attr2;
  137. UA_ObjectAttributes_init(&attr2);
  138. attr2.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  139. res = UA_Server_addObjectNode(server, objectid, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  140. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  141. objecttypeid, attr2, NULL, NULL);
  142. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  143. /* Delete the object */
  144. UA_Server_deleteNode(server, objectid, true);
  145. /* Verify that the destructor was called */
  146. ck_assert_int_eq(destructorCalled, true);
  147. UA_Server_delete(server);
  148. } END_TEST
  149. START_TEST(DeleteObjectAndReferences) {
  150. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  151. /* Add an object of the type */
  152. UA_ObjectAttributes attr;
  153. UA_ObjectAttributes_init(&attr);
  154. attr.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  155. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  156. UA_StatusCode res;
  157. res = UA_Server_addObjectNode(server, objectid, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  158. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  159. UA_NODEID_NULL, attr, NULL, NULL);
  160. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  161. /* Verify that we have a reference to the node from the objects folder */
  162. UA_BrowseDescription bd;
  163. UA_BrowseDescription_init(&bd);
  164. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  165. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  166. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  167. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  168. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  169. size_t refCount = 0;
  170. for(size_t i = 0; i < br.referencesSize; ++i) {
  171. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  172. refCount++;
  173. }
  174. ck_assert_int_eq(refCount, 1);
  175. UA_BrowseResult_deleteMembers(&br);
  176. /* Delete the object */
  177. UA_Server_deleteNode(server, objectid, true);
  178. /* Browse again, this time we expect that no reference is found */
  179. br = UA_Server_browse(server, 0, &bd);
  180. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  181. refCount = 0;
  182. for(size_t i = 0; i < br.referencesSize; ++i) {
  183. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  184. refCount++;
  185. }
  186. ck_assert_int_eq(refCount, 0);
  187. UA_BrowseResult_deleteMembers(&br);
  188. /* Add an object the second time */
  189. UA_ObjectAttributes_init(&attr);
  190. attr.displayName = UA_LOCALIZEDTEXT("en_US","my object");
  191. objectid = UA_NODEID_NUMERIC(0, 23372337);
  192. res = UA_Server_addObjectNode(server, objectid, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  193. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_QUALIFIEDNAME(0, ""),
  194. UA_NODEID_NULL, attr, NULL, NULL);
  195. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  196. /* Browse again, this time we expect that a single reference to the node is found */
  197. refCount = 0;
  198. br = UA_Server_browse(server, 0, &bd);
  199. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  200. for(size_t i = 0; i < br.referencesSize; ++i) {
  201. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  202. refCount++;
  203. }
  204. ck_assert_int_eq(refCount, 1);
  205. UA_BrowseResult_deleteMembers(&br);
  206. UA_Server_delete(server);
  207. } END_TEST
  208. /* Example taken from tutorial_server_object.c */
  209. START_TEST(InstantiateObjectType) {
  210. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  211. /* Define the object type */
  212. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  213. UA_StatusCode retval;
  214. /* Define the object type for "Device" */
  215. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  216. UA_ObjectTypeAttributes dtAttr;
  217. UA_ObjectTypeAttributes_init(&dtAttr);
  218. dtAttr.displayName = UA_LOCALIZEDTEXT("en_US", "DeviceType");
  219. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  220. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  221. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  222. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  223. NULL, &deviceTypeId);
  224. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  225. UA_VariableAttributes mnAttr;
  226. UA_VariableAttributes_init(&mnAttr);
  227. mnAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ManufacturerName");
  228. UA_NodeId manufacturerNameId;
  229. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  230. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  231. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  232. UA_NODEID_NULL, mnAttr, NULL, &manufacturerNameId);
  233. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  234. /* Make the manufacturer name mandatory */
  235. retval = UA_Server_addReference(server, manufacturerNameId,
  236. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  237. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  238. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  239. UA_VariableAttributes modelAttr;
  240. UA_VariableAttributes_init(&modelAttr);
  241. modelAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ModelName");
  242. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  243. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  244. UA_QUALIFIEDNAME(1, "ModelName"),
  245. UA_NODEID_NULL, modelAttr, NULL, NULL);
  246. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  247. /* Define the object type for "Pump" */
  248. UA_ObjectTypeAttributes ptAttr;
  249. UA_ObjectTypeAttributes_init(&ptAttr);
  250. ptAttr.displayName = UA_LOCALIZEDTEXT("en_US", "PumpType");
  251. retval = UA_Server_addObjectTypeNode(server, pumpTypeId,
  252. deviceTypeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  253. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  254. NULL, NULL);
  255. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  256. UA_VariableAttributes statusAttr;
  257. UA_VariableAttributes_init(&statusAttr);
  258. statusAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Status");
  259. statusAttr.valueRank = -1;
  260. UA_NodeId statusId;
  261. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  262. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  263. UA_QUALIFIEDNAME(1, "Status"),
  264. UA_NODEID_NULL, statusAttr, NULL, &statusId);
  265. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  266. /* Make the status variable mandatory */
  267. retval = UA_Server_addReference(server, statusId,
  268. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  269. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  270. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  271. UA_VariableAttributes rpmAttr;
  272. UA_VariableAttributes_init(&rpmAttr);
  273. rpmAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MotorRPM");
  274. rpmAttr.valueRank = -1;
  275. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  276. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  277. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  278. UA_NODEID_NULL, rpmAttr, NULL, NULL);
  279. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  280. /* Instantiate the variable */
  281. UA_ObjectAttributes oAttr;
  282. UA_ObjectAttributes_init(&oAttr);
  283. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MyPump");
  284. retval = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  285. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  286. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  287. UA_QUALIFIEDNAME(1, "MyPump"),
  288. pumpTypeId, /* this refers to the object type
  289. identifier */
  290. oAttr, NULL, NULL);
  291. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  292. UA_Server_delete(server);
  293. } END_TEST
  294. int main(void) {
  295. Suite *s = suite_create("services_nodemanagement");
  296. TCase *tc_addnodes = tcase_create("addnodes");
  297. tcase_add_test(tc_addnodes, AddVariableNode);
  298. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  299. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  300. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  301. tcase_add_test(tc_addnodes, InstantiateObjectType);
  302. TCase *tc_deletenodes = tcase_create("deletenodes");
  303. tcase_add_test(tc_deletenodes, DeleteObjectWithDestructor);
  304. tcase_add_test(tc_deletenodes, DeleteObjectAndReferences);
  305. suite_add_tcase(s, tc_addnodes);
  306. suite_add_tcase(s, tc_deletenodes);
  307. SRunner *sr = srunner_create(s);
  308. srunner_set_fork_status(sr, CK_NOFORK);
  309. srunner_run_all(sr, CK_NORMAL);
  310. int number_failed = srunner_ntests_failed(sr);
  311. srunner_free(sr);
  312. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  313. }