check_services_nodemanagement.c 18 KB

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