check_services_nodemanagement.c 30 KB

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