check_services_nodemanagement.c 28 KB

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