check_services_nodemanagement.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /* Example taken from tutorial_server_object.c */
  210. START_TEST(InstantiateObjectType) {
  211. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  212. /* Define the object type */
  213. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  214. UA_StatusCode retval;
  215. /* Define the object type for "Device" */
  216. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  217. UA_ObjectTypeAttributes dtAttr;
  218. UA_ObjectTypeAttributes_init(&dtAttr);
  219. dtAttr.displayName = UA_LOCALIZEDTEXT("en_US", "DeviceType");
  220. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  221. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  222. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  223. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  224. NULL, &deviceTypeId);
  225. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  226. UA_VariableAttributes mnAttr;
  227. UA_VariableAttributes_init(&mnAttr);
  228. mnAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ManufacturerName");
  229. UA_NodeId manufacturerNameId;
  230. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  231. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  232. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  233. UA_NODEID_NULL, mnAttr, NULL, &manufacturerNameId);
  234. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  235. /* Make the manufacturer name mandatory */
  236. retval = UA_Server_addReference(server, manufacturerNameId,
  237. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  238. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  239. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  240. UA_VariableAttributes modelAttr;
  241. UA_VariableAttributes_init(&modelAttr);
  242. modelAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ModelName");
  243. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  244. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  245. UA_QUALIFIEDNAME(1, "ModelName"),
  246. UA_NODEID_NULL, modelAttr, NULL, NULL);
  247. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  248. /* Define the object type for "Pump" */
  249. UA_ObjectTypeAttributes ptAttr;
  250. UA_ObjectTypeAttributes_init(&ptAttr);
  251. ptAttr.displayName = UA_LOCALIZEDTEXT("en_US", "PumpType");
  252. retval = UA_Server_addObjectTypeNode(server, pumpTypeId,
  253. deviceTypeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  254. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  255. NULL, NULL);
  256. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  257. UA_VariableAttributes statusAttr;
  258. UA_VariableAttributes_init(&statusAttr);
  259. statusAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Status");
  260. statusAttr.valueRank = -1;
  261. UA_NodeId statusId;
  262. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  263. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  264. UA_QUALIFIEDNAME(1, "Status"),
  265. UA_NODEID_NULL, statusAttr, NULL, &statusId);
  266. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  267. /* Make the status variable mandatory */
  268. retval = UA_Server_addReference(server, statusId,
  269. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  270. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  271. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  272. UA_VariableAttributes rpmAttr;
  273. UA_VariableAttributes_init(&rpmAttr);
  274. rpmAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MotorRPM");
  275. rpmAttr.valueRank = -1;
  276. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  277. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  278. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  279. UA_NODEID_NULL, rpmAttr, NULL, NULL);
  280. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  281. /* Instantiate the variable */
  282. UA_ObjectAttributes oAttr;
  283. UA_ObjectAttributes_init(&oAttr);
  284. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MyPump");
  285. retval = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  286. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  287. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  288. UA_QUALIFIEDNAME(1, "MyPump"),
  289. pumpTypeId, /* this refers to the object type
  290. identifier */
  291. oAttr, NULL, NULL);
  292. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  293. UA_Server_delete(server);
  294. } END_TEST
  295. int main(void) {
  296. Suite *s = suite_create("services_nodemanagement");
  297. TCase *tc_addnodes = tcase_create("addnodes");
  298. tcase_add_test(tc_addnodes, AddVariableNode);
  299. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  300. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  301. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  302. tcase_add_test(tc_addnodes, InstantiateObjectType);
  303. TCase *tc_deletenodes = tcase_create("deletenodes");
  304. tcase_add_test(tc_deletenodes, DeleteObjectWithDestructor);
  305. tcase_add_test(tc_deletenodes, DeleteObjectAndReferences);
  306. suite_add_tcase(s, tc_addnodes);
  307. suite_add_tcase(s, tc_deletenodes);
  308. SRunner *sr = srunner_create(s);
  309. srunner_set_fork_status(sr, CK_NOFORK);
  310. srunner_run_all(sr, CK_NORMAL);
  311. int number_failed = srunner_ntests_failed(sr);
  312. srunner_free(sr);
  313. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  314. }