check_services_nodemanagement.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 <open62541/server_config_default.h>
  5. #include "server/ua_server_internal.h"
  6. #include "server/ua_services.h"
  7. #include <check.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. static UA_Server *server = NULL;
  12. static UA_Int32 handleCalled = 0;
  13. static UA_StatusCode
  14. globalInstantiationMethod(UA_Server *server_,
  15. const UA_NodeId *sessionId, void *sessionContext,
  16. const UA_NodeId *nodeId, void **nodeContext) {
  17. handleCalled++;
  18. return UA_STATUSCODE_GOOD;
  19. }
  20. static void setup(void) {
  21. server = UA_Server_new();
  22. UA_ServerConfig *config = UA_Server_getConfig(server);
  23. UA_ServerConfig_setDefault(config);
  24. UA_GlobalNodeLifecycle lifecycle;
  25. lifecycle.constructor = globalInstantiationMethod;
  26. lifecycle.destructor = NULL;
  27. lifecycle.createOptionalChild = NULL;
  28. lifecycle.generateChildNodeId = NULL;
  29. config->nodeLifecycle = lifecycle;
  30. }
  31. static void teardown(void) {
  32. UA_Server_delete(server);
  33. }
  34. START_TEST(AddVariableNode) {
  35. /* Add a variable node to the address space */
  36. UA_VariableAttributes attr = UA_VariableAttributes_default;
  37. UA_Int32 myInteger = 42;
  38. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  39. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  40. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  41. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  42. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  43. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  44. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  45. UA_StatusCode res =
  46. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  47. parentReferenceNodeId, myIntegerName,
  48. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  49. attr, NULL, NULL);
  50. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  51. } END_TEST
  52. START_TEST(AddVariableNode_ExtensionObject) {
  53. /* Add a variable node to the address space */
  54. UA_VariableAttributes attr = UA_VariableAttributes_default;
  55. attr.displayName = UA_LOCALIZEDTEXT("en-US","the extensionobject");
  56. /* Set an ExtensionObject with an unknown binary encoding */
  57. UA_ExtensionObject myExtensionObject;
  58. UA_ExtensionObject_init(&myExtensionObject);
  59. myExtensionObject.encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
  60. myExtensionObject.content.encoded.typeId = UA_NODEID_NUMERIC(5, 1234);
  61. UA_ByteString byteString = UA_BYTESTRING("String Payload as a ByteString extension");
  62. myExtensionObject.content.encoded.body = byteString;
  63. UA_Variant_setScalar(&attr.value, &myExtensionObject, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
  64. UA_NodeId myEONodeId = UA_NODEID_STRING(1, "the.extensionobject");
  65. UA_QualifiedName myEOName = UA_QUALIFIEDNAME(1, "the extensionobject");
  66. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  67. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  68. UA_StatusCode res =
  69. UA_Server_addVariableNode(server, myEONodeId, parentNodeId,
  70. parentReferenceNodeId, myEOName,
  71. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  72. attr, NULL, NULL);
  73. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  74. } END_TEST
  75. static UA_NodeId pointTypeId;
  76. static void
  77. addVariableTypeNode(void) {
  78. UA_VariableTypeAttributes vtAttr = UA_VariableTypeAttributes_default;
  79. vtAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  80. vtAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  81. UA_UInt32 arrayDims[1] = {2};
  82. vtAttr.arrayDimensions = arrayDims;
  83. vtAttr.arrayDimensionsSize = 1;
  84. vtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Type");
  85. /* a matching default value is required */
  86. UA_Double zero[2] = {0.0, 0.0};
  87. UA_Variant_setArray(&vtAttr.value, zero, 2, &UA_TYPES[UA_TYPES_DOUBLE]);
  88. UA_StatusCode res =
  89. UA_Server_addVariableTypeNode(server, UA_NODEID_NULL,
  90. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  91. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  92. UA_QUALIFIEDNAME(1, "2DPoint Type"), UA_NODEID_NULL,
  93. vtAttr, NULL, &pointTypeId);
  94. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  95. }
  96. START_TEST(InstantiateVariableTypeNode) {
  97. addVariableTypeNode();
  98. /* Prepare the node attributes */
  99. UA_UInt32 arrayDims[1] = {2};
  100. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  101. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  102. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  103. vAttr.arrayDimensions = arrayDims;
  104. vAttr.arrayDimensionsSize = 1;
  105. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  106. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  107. /* vAttr.value is left empty, the server instantiates with the default value */
  108. /* Add the node */
  109. UA_NodeId pointVariableId;
  110. UA_StatusCode res =
  111. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  112. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  113. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  114. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  115. vAttr, NULL, &pointVariableId);
  116. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  117. /* Was the value instantiated? */
  118. UA_Variant val;
  119. UA_Server_readValue(server, pointVariableId, &val);
  120. ck_assert(val.type != NULL);
  121. UA_Variant_deleteMembers(&val);
  122. } END_TEST
  123. START_TEST(InstantiateVariableTypeNodeWrongDims) {
  124. addVariableTypeNode();
  125. /* Prepare the node attributes */
  126. UA_UInt32 arrayDims[1] = {3}; /* This will fail as the dimensions are too big */
  127. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  128. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  129. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  130. vAttr.arrayDimensions = arrayDims;
  131. vAttr.arrayDimensionsSize = 1;
  132. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  133. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  134. /* vAttr.value is left empty, the server instantiates with the default value */
  135. /* Add the node */
  136. UA_StatusCode res =
  137. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  138. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  139. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  140. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  141. vAttr, NULL, NULL);
  142. ck_assert_int_eq(UA_STATUSCODE_BADTYPEMISMATCH, res);
  143. } END_TEST
  144. START_TEST(InstantiateVariableTypeNodeLessDims) {
  145. addVariableTypeNode();
  146. /* Prepare the node attributes */
  147. UA_UInt32 arrayDims[1] = {1}; /* This will match as the dimension constraints are an upper bound */
  148. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  149. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  150. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  151. vAttr.arrayDimensions = arrayDims;
  152. vAttr.arrayDimensionsSize = 1;
  153. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  154. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  155. /* vAttr.value is left empty, the server instantiates with the default value */
  156. /* Add the node */
  157. UA_StatusCode res =
  158. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  159. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  160. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  161. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  162. vAttr, NULL, NULL);
  163. ck_assert_int_eq(UA_STATUSCODE_BADTYPEMISMATCH, res);
  164. } END_TEST
  165. START_TEST(AddComplexTypeWithInheritance) {
  166. /* add a variable node to the address space */
  167. /* Node UA_NS0ID_SERVERTYPE is not available in the minimal NS0 */
  168. #ifdef UA_GENERATED_NAMESPACE_ZERO
  169. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  170. attr.description = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  171. attr.displayName = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  172. UA_NodeId myObjectNodeId = UA_NODEID_STRING(1, "the.fake.Server.Struct");
  173. UA_QualifiedName myObjectName = UA_QUALIFIEDNAME(1, "the.fake.Server.Struct");
  174. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  175. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  176. UA_StatusCode res =
  177. UA_Server_addObjectNode(server, myObjectNodeId, parentNodeId,
  178. parentReferenceNodeId, myObjectName,
  179. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVERTYPE), attr,
  180. &handleCalled, NULL);
  181. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  182. ck_assert_int_gt(handleCalled, 0); // Should be 58, but may depend on NS0 XML detail
  183. #endif
  184. } END_TEST
  185. START_TEST(AddNodeTwiceGivesError) {
  186. /* add a variable node to the address space */
  187. UA_VariableAttributes attr = UA_VariableAttributes_default;
  188. UA_Int32 myInteger = 42;
  189. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  190. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  191. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  192. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  193. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  194. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  195. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  196. UA_StatusCode res =
  197. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  198. parentReferenceNodeId, myIntegerName,
  199. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  200. attr, NULL, NULL);
  201. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  202. res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  203. parentReferenceNodeId, myIntegerName,
  204. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  205. attr, NULL, NULL);
  206. ck_assert_int_eq(res, UA_STATUSCODE_BADNODEIDEXISTS);
  207. } END_TEST
  208. static UA_Boolean constructorCalled = false;
  209. static UA_StatusCode
  210. objectConstructor(UA_Server *server_,
  211. const UA_NodeId *sessionId, void *sessionContext,
  212. const UA_NodeId *typeId, void *typeContext,
  213. const UA_NodeId *nodeId, void **nodeContext) {
  214. constructorCalled = true;
  215. return UA_STATUSCODE_GOOD;
  216. }
  217. START_TEST(AddObjectWithConstructor) {
  218. /* Add an object type */
  219. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  220. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  221. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  222. UA_StatusCode res =
  223. UA_Server_addObjectTypeNode(server, objecttypeid,
  224. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  225. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  226. UA_QUALIFIEDNAME(0, "myobjecttype"), attr,
  227. NULL, NULL);
  228. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  229. /* Add a constructor to the object type */
  230. UA_NodeTypeLifecycle lifecycle;
  231. lifecycle.constructor = objectConstructor;
  232. lifecycle.destructor = NULL;
  233. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  234. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  235. /* Add an object of the type */
  236. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  237. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  238. res = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  239. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  240. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  241. UA_QUALIFIEDNAME(0, "MyObjectNode"), objecttypeid,
  242. attr2, NULL, NULL);
  243. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  244. /* Verify that the constructor was called */
  245. ck_assert_int_eq(constructorCalled, true);
  246. } END_TEST
  247. static UA_Boolean destructorCalled = false;
  248. static void
  249. objectDestructor(UA_Server *server_,
  250. const UA_NodeId *sessionId, void *sessionContext,
  251. const UA_NodeId *typeId, void *typeContext,
  252. const UA_NodeId *nodeId, void **nodeContext) {
  253. destructorCalled = true;
  254. }
  255. START_TEST(DeleteObjectWithDestructor) {
  256. /* Add an object type */
  257. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  258. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  259. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  260. UA_StatusCode res =
  261. UA_Server_addObjectTypeNode(server, objecttypeid,
  262. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  263. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  264. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  265. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  266. /* Add a constructor to the object type */
  267. UA_NodeTypeLifecycle lifecycle;
  268. lifecycle.constructor = NULL;
  269. lifecycle.destructor = objectDestructor;
  270. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  271. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  272. /* Add an object of the type */
  273. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  274. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  275. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  276. res = UA_Server_addObjectNode(server, objectid,
  277. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  278. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  279. UA_QUALIFIEDNAME(0, "MyObject"), objecttypeid,
  280. attr2, NULL, NULL);
  281. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  282. /* Delete the object */
  283. UA_Server_deleteNode(server, objectid, true);
  284. /* Verify that the destructor was called */
  285. ck_assert_int_eq(destructorCalled, true);
  286. } END_TEST
  287. START_TEST(DeleteObjectAndReferences) {
  288. /* Add an object of the type */
  289. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  290. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  291. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  292. UA_StatusCode res;
  293. res = UA_Server_addObjectNode(server, objectid,
  294. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  295. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  296. UA_QUALIFIEDNAME(0, "MyObject"),
  297. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  298. attr, NULL, NULL);
  299. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  300. /* Verify that we have a reference to the node from the objects folder */
  301. UA_BrowseDescription bd;
  302. UA_BrowseDescription_init(&bd);
  303. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  304. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  305. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  306. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  307. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  308. size_t refCount = 0;
  309. for(size_t i = 0; i < br.referencesSize; ++i) {
  310. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  311. refCount++;
  312. }
  313. ck_assert_int_eq(refCount, 1);
  314. UA_BrowseResult_deleteMembers(&br);
  315. /* Delete the object */
  316. UA_Server_deleteNode(server, objectid, true);
  317. /* Browse again, this time we expect that no reference is found */
  318. br = UA_Server_browse(server, 0, &bd);
  319. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  320. refCount = 0;
  321. for(size_t i = 0; i < br.referencesSize; ++i) {
  322. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  323. refCount++;
  324. }
  325. ck_assert_int_eq(refCount, 0);
  326. UA_BrowseResult_deleteMembers(&br);
  327. /* Add an object the second time */
  328. attr = UA_ObjectAttributes_default;
  329. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  330. objectid = UA_NODEID_NUMERIC(0, 23372337);
  331. res = UA_Server_addObjectNode(server, objectid,
  332. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  333. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  334. UA_QUALIFIEDNAME(0, "MyObject"),
  335. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  336. attr, NULL, NULL);
  337. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  338. /* Browse again, this time we expect that a single reference to the node is found */
  339. refCount = 0;
  340. br = UA_Server_browse(server, 0, &bd);
  341. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  342. for(size_t i = 0; i < br.referencesSize; ++i) {
  343. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  344. refCount++;
  345. }
  346. ck_assert_int_eq(refCount, 1);
  347. UA_BrowseResult_deleteMembers(&br);
  348. } END_TEST
  349. /* Example taken from tutorial_server_object.c */
  350. START_TEST(InstantiateObjectType) {
  351. /* Define the object type */
  352. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  353. UA_StatusCode retval;
  354. /* Define the object type for "Device" */
  355. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  356. UA_ObjectTypeAttributes dtAttr = UA_ObjectTypeAttributes_default;
  357. dtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DeviceType");
  358. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  359. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  360. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  361. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  362. NULL, &deviceTypeId);
  363. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  364. UA_VariableAttributes mnAttr = UA_VariableAttributes_default;
  365. mnAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ManufacturerName");
  366. UA_NodeId manufacturerNameId;
  367. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  368. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  369. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  370. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  371. mnAttr, NULL, &manufacturerNameId);
  372. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  373. /* UA_NS0ID_MODELLINGRULE_MANDATORY is not available in Minimal Nodeset */
  374. #ifdef UA_GENERATED_NAMESPACE_ZERO
  375. /* Make the manufacturer name mandatory */
  376. retval = UA_Server_addReference(server, manufacturerNameId,
  377. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  378. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  379. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  380. #endif
  381. UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
  382. modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
  383. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  384. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  385. UA_QUALIFIEDNAME(1, "ModelName"),
  386. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  387. modelAttr, NULL, NULL);
  388. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  389. /* Define the object type for "Pump" */
  390. UA_ObjectTypeAttributes ptAttr = UA_ObjectTypeAttributes_default;
  391. ptAttr.displayName = UA_LOCALIZEDTEXT("en-US", "PumpType");
  392. retval = UA_Server_addObjectTypeNode(server, pumpTypeId, deviceTypeId,
  393. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  394. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  395. NULL, NULL);
  396. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  397. UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
  398. statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
  399. statusAttr.valueRank = UA_VALUERANK_SCALAR;
  400. UA_NodeId statusId;
  401. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  402. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  403. UA_QUALIFIEDNAME(1, "Status"),
  404. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  405. statusAttr, NULL, &statusId);
  406. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  407. /* UA_NS0ID_MODELLINGRULE_MANDATORY is not available in Minimal Nodeset */
  408. #ifdef UA_GENERATED_NAMESPACE_ZERO
  409. /* Make the status variable mandatory */
  410. retval = UA_Server_addReference(server, statusId,
  411. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  412. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  413. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  414. #endif
  415. UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
  416. rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
  417. rpmAttr.valueRank = UA_VALUERANK_SCALAR;
  418. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  419. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  420. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  421. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  422. rpmAttr, NULL, NULL);
  423. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  424. /* Instantiate the variable */
  425. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  426. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MyPump");
  427. retval = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  428. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  429. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  430. UA_QUALIFIEDNAME(1, "MyPump"),
  431. pumpTypeId, /* this refers to the object type
  432. identifier */
  433. oAttr, NULL, NULL);
  434. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  435. } END_TEST
  436. static UA_NodeId
  437. findReference(const UA_NodeId sourceId, const UA_NodeId refTypeId) {
  438. UA_BrowseDescription * bDesc = UA_BrowseDescription_new();
  439. UA_NodeId_copy(&sourceId, &bDesc->nodeId);
  440. bDesc->browseDirection = UA_BROWSEDIRECTION_FORWARD;
  441. bDesc->includeSubtypes = true;
  442. bDesc->resultMask = UA_BROWSERESULTMASK_REFERENCETYPEID;
  443. UA_BrowseResult bRes = UA_Server_browse(server, 0, bDesc);
  444. ck_assert(bRes.statusCode == UA_STATUSCODE_GOOD);
  445. UA_NodeId outNodeId = UA_NODEID_NULL;
  446. for(size_t i = 0; i < bRes.referencesSize; i++) {
  447. UA_ReferenceDescription rDesc = bRes.references[i];
  448. if(UA_NodeId_equal(&rDesc.referenceTypeId, &refTypeId)) {
  449. UA_NodeId_copy(&rDesc.nodeId.nodeId, &outNodeId);
  450. break;
  451. }
  452. }
  453. UA_BrowseDescription_deleteMembers(bDesc);
  454. UA_BrowseDescription_delete(bDesc);
  455. UA_BrowseResult_deleteMembers(&bRes);
  456. return outNodeId;
  457. }
  458. static UA_NodeId
  459. registerRefType(char *forwName, char *invName) {
  460. UA_NodeId outNodeId;
  461. UA_ReferenceTypeAttributes refattr = UA_ReferenceTypeAttributes_default;
  462. refattr.displayName = UA_LOCALIZEDTEXT(NULL, forwName);
  463. refattr.inverseName = UA_LOCALIZEDTEXT(NULL, invName );
  464. UA_QualifiedName browseName = UA_QUALIFIEDNAME(1, forwName);
  465. UA_StatusCode st =
  466. UA_Server_addReferenceTypeNode(server, UA_NODEID_NULL,
  467. UA_NODEID_NUMERIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  468. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  469. browseName, refattr, NULL, &outNodeId);
  470. ck_assert(st == UA_STATUSCODE_GOOD);
  471. return outNodeId;
  472. }
  473. static UA_NodeId
  474. addObjInstance(const UA_NodeId parentNodeId, char *dispName) {
  475. UA_NodeId outNodeId;
  476. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  477. oAttr.displayName = UA_LOCALIZEDTEXT(NULL, dispName);
  478. UA_QualifiedName browseName = UA_QUALIFIEDNAME(1, dispName);
  479. UA_StatusCode st =
  480. UA_Server_addObjectNode(server, UA_NODEID_NULL,
  481. parentNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  482. browseName, UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  483. oAttr, NULL, &outNodeId);
  484. ck_assert(st == UA_STATUSCODE_GOOD);
  485. return outNodeId;
  486. }
  487. START_TEST(AddDoubleReference) {
  488. // create two different reference types
  489. UA_NodeId ref1TypeId = registerRefType("HasRef1", "IsRefOf1");
  490. UA_NodeId ref2TypeId = registerRefType("HasRef2", "IsRefOf2");
  491. // create two different object instances
  492. UA_NodeId objectsNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  493. UA_NodeId sourceId = addObjInstance(objectsNodeId, "obj1");
  494. UA_NodeId targetId = addObjInstance(objectsNodeId, "obj2");
  495. // connect them twice, one time per reference type
  496. UA_ExpandedNodeId targetExpId;
  497. targetExpId.nodeId = targetId;
  498. targetExpId.namespaceUri = UA_STRING_NULL;
  499. targetExpId.serverIndex = 0;
  500. UA_StatusCode st;
  501. st = UA_Server_addReference(server, sourceId, ref1TypeId, targetExpId, true);
  502. ck_assert(st == UA_STATUSCODE_GOOD);
  503. st = UA_Server_addReference(server, sourceId, ref2TypeId, targetExpId, true);
  504. ck_assert(st == UA_STATUSCODE_GOOD);
  505. /* repetition fails */
  506. st = UA_Server_addReference(server, sourceId, ref2TypeId, targetExpId, true);
  507. ck_assert(st != UA_STATUSCODE_GOOD);
  508. // check references where added
  509. UA_NodeId targetCheckId;
  510. targetCheckId = findReference(sourceId, ref1TypeId);
  511. ck_assert(UA_NodeId_equal(&targetCheckId, &targetId));
  512. targetCheckId = findReference(sourceId, ref2TypeId);
  513. ck_assert(UA_NodeId_equal(&targetCheckId, &targetId));
  514. } END_TEST
  515. int main(void) {
  516. Suite *s = suite_create("services_nodemanagement");
  517. TCase *tc_addnodes = tcase_create("addnodes");
  518. tcase_add_checked_fixture(tc_addnodes, setup, teardown);
  519. tcase_add_test(tc_addnodes, AddVariableNode);
  520. tcase_add_test(tc_addnodes, AddVariableNode_ExtensionObject);
  521. tcase_add_test(tc_addnodes, InstantiateVariableTypeNode);
  522. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeWrongDims);
  523. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeLessDims);
  524. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  525. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  526. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  527. tcase_add_test(tc_addnodes, InstantiateObjectType);
  528. suite_add_tcase(s, tc_addnodes);
  529. TCase *tc_deletenodes = tcase_create("deletenodes");
  530. tcase_add_checked_fixture(tc_deletenodes, setup, teardown);
  531. tcase_add_test(tc_deletenodes, DeleteObjectWithDestructor);
  532. tcase_add_test(tc_deletenodes, DeleteObjectAndReferences);
  533. suite_add_tcase(s, tc_deletenodes);
  534. TCase *tc_addreferences = tcase_create("addreferences");
  535. tcase_add_checked_fixture(tc_addreferences, setup, teardown);
  536. tcase_add_test(tc_addreferences, AddDoubleReference);
  537. suite_add_tcase(s, tc_addreferences);
  538. SRunner *sr = srunner_create(s);
  539. srunner_set_fork_status(sr, CK_NOFORK);
  540. srunner_run_all(sr, CK_NORMAL);
  541. int number_failed = srunner_ntests_failed(sr);
  542. srunner_free(sr);
  543. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  544. }