check_services_nodemanagement.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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_ServerConfig *config = NULL;
  13. static UA_Int32 handleCalled = 0;
  14. static UA_StatusCode
  15. globalInstantiationMethod(UA_Server *server_,
  16. const UA_NodeId *sessionId, void *sessionContext,
  17. const UA_NodeId *nodeId, void **nodeContext) {
  18. handleCalled++;
  19. return UA_STATUSCODE_GOOD;
  20. }
  21. static void setup(void) {
  22. config = UA_ServerConfig_new_default();
  23. UA_GlobalNodeLifecycle lifecycle;
  24. lifecycle.constructor = globalInstantiationMethod;
  25. lifecycle.destructor = NULL;
  26. config->nodeLifecycle = lifecycle;
  27. server = UA_Server_new(config);
  28. }
  29. static void teardown(void) {
  30. UA_Server_delete(server);
  31. UA_ServerConfig_delete(config);
  32. }
  33. START_TEST(AddVariableNode) {
  34. /* add a variable node to the address space */
  35. UA_VariableAttributes attr = UA_VariableAttributes_default;
  36. UA_Int32 myInteger = 42;
  37. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  38. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  39. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  40. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  41. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  42. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  43. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  44. UA_StatusCode res =
  45. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  46. parentReferenceNodeId, myIntegerName,
  47. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  48. attr, NULL, NULL);
  49. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  50. } END_TEST
  51. static UA_NodeId pointTypeId;
  52. static void
  53. addVariableTypeNode(void) {
  54. UA_VariableTypeAttributes vtAttr = UA_VariableTypeAttributes_default;
  55. vtAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  56. vtAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  57. UA_UInt32 arrayDims[1] = {2};
  58. vtAttr.arrayDimensions = arrayDims;
  59. vtAttr.arrayDimensionsSize = 1;
  60. vtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Type");
  61. /* a matching default value is required */
  62. UA_Double zero[2] = {0.0, 0.0};
  63. UA_Variant_setArray(&vtAttr.value, zero, 2, &UA_TYPES[UA_TYPES_DOUBLE]);
  64. UA_StatusCode res =
  65. UA_Server_addVariableTypeNode(server, UA_NODEID_NULL,
  66. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  67. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  68. UA_QUALIFIEDNAME(1, "2DPoint Type"), UA_NODEID_NULL,
  69. vtAttr, NULL, &pointTypeId);
  70. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  71. }
  72. START_TEST(InstantiateVariableTypeNode) {
  73. addVariableTypeNode();
  74. /* Prepare the node attributes */
  75. UA_UInt32 arrayDims[1] = {2};
  76. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  77. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  78. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  79. vAttr.arrayDimensions = arrayDims;
  80. vAttr.arrayDimensionsSize = 1;
  81. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  82. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  83. /* vAttr.value is left empty, the server instantiates with the default value */
  84. /* Add the node */
  85. UA_NodeId pointVariableId;
  86. UA_StatusCode res =
  87. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  88. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  89. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  90. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  91. vAttr, NULL, &pointVariableId);
  92. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  93. /* Was the value instantiated? */
  94. UA_Variant val;
  95. UA_Server_readValue(server, pointVariableId, &val);
  96. ck_assert(val.type != NULL);
  97. UA_Variant_deleteMembers(&val);
  98. } END_TEST
  99. START_TEST(InstantiateVariableTypeNodeWrongDims) {
  100. addVariableTypeNode();
  101. /* Prepare the node attributes */
  102. UA_UInt32 arrayDims[1] = {3}; /* This will fail as the dimensions are too big */
  103. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  104. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  105. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  106. vAttr.arrayDimensions = arrayDims;
  107. vAttr.arrayDimensionsSize = 1;
  108. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  109. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  110. /* vAttr.value is left empty, the server instantiates with the default value */
  111. /* Add the node */
  112. UA_StatusCode res =
  113. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  114. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  115. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  116. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  117. vAttr, NULL, NULL);
  118. ck_assert_int_eq(UA_STATUSCODE_BADTYPEMISMATCH, res);
  119. } END_TEST
  120. START_TEST(InstantiateVariableTypeNodeLessDims) {
  121. addVariableTypeNode();
  122. /* Prepare the node attributes */
  123. UA_UInt32 arrayDims[1] = {1}; /* This will match as the dimension constraints are an upper bound */
  124. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  125. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  126. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  127. vAttr.arrayDimensions = arrayDims;
  128. vAttr.arrayDimensionsSize = 1;
  129. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  130. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  131. /* vAttr.value is left empty, the server instantiates with the default value */
  132. /* Add the node */
  133. UA_StatusCode res =
  134. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  135. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  136. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  137. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  138. vAttr, NULL, NULL);
  139. ck_assert_int_eq(UA_STATUSCODE_BADTYPEMISMATCH, res);
  140. } END_TEST
  141. START_TEST(AddComplexTypeWithInheritance) {
  142. /* add a variable node to the address space */
  143. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  144. attr.description = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  145. attr.displayName = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  146. UA_NodeId myObjectNodeId = UA_NODEID_STRING(1, "the.fake.Server.Struct");
  147. UA_QualifiedName myObjectName = UA_QUALIFIEDNAME(1, "the.fake.Server.Struct");
  148. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  149. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  150. UA_StatusCode res =
  151. UA_Server_addObjectNode(server, myObjectNodeId, parentNodeId,
  152. parentReferenceNodeId, myObjectName,
  153. UA_NODEID_NUMERIC(0, 2004), attr,
  154. &handleCalled, NULL);
  155. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  156. ck_assert_int_gt(handleCalled, 0); // Should be 58, but may depend on NS0 XML detail
  157. } END_TEST
  158. START_TEST(AddNodeTwiceGivesError) {
  159. /* add a variable node to the address space */
  160. UA_VariableAttributes attr = UA_VariableAttributes_default;
  161. UA_Int32 myInteger = 42;
  162. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  163. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  164. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  165. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  166. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  167. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  168. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  169. UA_StatusCode res =
  170. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  171. parentReferenceNodeId, myIntegerName,
  172. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  173. attr, NULL, NULL);
  174. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  175. res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  176. parentReferenceNodeId, myIntegerName,
  177. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  178. attr, NULL, NULL);
  179. ck_assert_int_eq(res, UA_STATUSCODE_BADNODEIDEXISTS);
  180. } END_TEST
  181. static UA_Boolean constructorCalled = false;
  182. static UA_StatusCode
  183. objectConstructor(UA_Server *server_,
  184. const UA_NodeId *sessionId, void *sessionContext,
  185. const UA_NodeId *typeId, void *typeContext,
  186. const UA_NodeId *nodeId, void **nodeContext) {
  187. constructorCalled = true;
  188. return UA_STATUSCODE_GOOD;
  189. }
  190. START_TEST(AddObjectWithConstructor) {
  191. /* Add an object type */
  192. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  193. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  194. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  195. UA_StatusCode res =
  196. UA_Server_addObjectTypeNode(server, objecttypeid,
  197. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  198. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  199. UA_QUALIFIEDNAME(0, "myobjecttype"), attr,
  200. NULL, NULL);
  201. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  202. /* Add a constructor to the object type */
  203. UA_NodeTypeLifecycle lifecycle;
  204. lifecycle.constructor = objectConstructor;
  205. lifecycle.destructor = NULL;
  206. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  207. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  208. /* Add an object of the type */
  209. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  210. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  211. res = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  212. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  213. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  214. UA_QUALIFIEDNAME(0, ""), objecttypeid,
  215. attr2, NULL, NULL);
  216. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  217. /* Verify that the constructor was called */
  218. ck_assert_int_eq(constructorCalled, true);
  219. } END_TEST
  220. static UA_Boolean destructorCalled = false;
  221. static void
  222. objectDestructor(UA_Server *server_,
  223. const UA_NodeId *sessionId, void *sessionContext,
  224. const UA_NodeId *typeId, void *typeContext,
  225. const UA_NodeId *nodeId, void **nodeContext) {
  226. destructorCalled = true;
  227. }
  228. START_TEST(DeleteObjectWithDestructor) {
  229. /* Add an object type */
  230. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  231. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  232. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  233. UA_StatusCode res =
  234. UA_Server_addObjectTypeNode(server, objecttypeid,
  235. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  236. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  237. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  238. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  239. /* Add a constructor to the object type */
  240. UA_NodeTypeLifecycle lifecycle;
  241. lifecycle.constructor = NULL;
  242. lifecycle.destructor = objectDestructor;
  243. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  244. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  245. /* Add an object of the type */
  246. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  247. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  248. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  249. res = UA_Server_addObjectNode(server, objectid,
  250. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  251. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  252. UA_QUALIFIEDNAME(0, ""), objecttypeid,
  253. attr2, NULL, NULL);
  254. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  255. /* Delete the object */
  256. UA_Server_deleteNode(server, objectid, true);
  257. /* Verify that the destructor was called */
  258. ck_assert_int_eq(destructorCalled, true);
  259. } END_TEST
  260. START_TEST(DeleteObjectAndReferences) {
  261. /* Add an object of the type */
  262. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  263. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  264. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  265. UA_StatusCode res;
  266. res = UA_Server_addObjectNode(server, objectid,
  267. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  268. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  269. UA_QUALIFIEDNAME(0, ""),
  270. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  271. attr, NULL, NULL);
  272. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  273. /* Verify that we have a reference to the node from the objects folder */
  274. UA_BrowseDescription bd;
  275. UA_BrowseDescription_init(&bd);
  276. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  277. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  278. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  279. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  280. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  281. size_t refCount = 0;
  282. for(size_t i = 0; i < br.referencesSize; ++i) {
  283. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  284. refCount++;
  285. }
  286. ck_assert_int_eq(refCount, 1);
  287. UA_BrowseResult_deleteMembers(&br);
  288. /* Delete the object */
  289. UA_Server_deleteNode(server, objectid, true);
  290. /* Browse again, this time we expect that no reference is found */
  291. br = UA_Server_browse(server, 0, &bd);
  292. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  293. refCount = 0;
  294. for(size_t i = 0; i < br.referencesSize; ++i) {
  295. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  296. refCount++;
  297. }
  298. ck_assert_int_eq(refCount, 0);
  299. UA_BrowseResult_deleteMembers(&br);
  300. /* Add an object the second time */
  301. attr = UA_ObjectAttributes_default;
  302. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  303. objectid = UA_NODEID_NUMERIC(0, 23372337);
  304. res = UA_Server_addObjectNode(server, objectid,
  305. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  306. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  307. UA_QUALIFIEDNAME(0, ""),
  308. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  309. attr, NULL, NULL);
  310. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  311. /* Browse again, this time we expect that a single reference to the node is found */
  312. refCount = 0;
  313. br = UA_Server_browse(server, 0, &bd);
  314. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  315. for(size_t i = 0; i < br.referencesSize; ++i) {
  316. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  317. refCount++;
  318. }
  319. ck_assert_int_eq(refCount, 1);
  320. UA_BrowseResult_deleteMembers(&br);
  321. } END_TEST
  322. /* Example taken from tutorial_server_object.c */
  323. START_TEST(InstantiateObjectType) {
  324. /* Define the object type */
  325. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  326. UA_StatusCode retval;
  327. /* Define the object type for "Device" */
  328. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  329. UA_ObjectTypeAttributes dtAttr = UA_ObjectTypeAttributes_default;
  330. dtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DeviceType");
  331. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  332. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  333. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  334. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  335. NULL, &deviceTypeId);
  336. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  337. UA_VariableAttributes mnAttr = UA_VariableAttributes_default;
  338. mnAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ManufacturerName");
  339. UA_NodeId manufacturerNameId;
  340. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  341. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  342. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  343. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  344. mnAttr, NULL, &manufacturerNameId);
  345. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  346. /* Make the manufacturer name mandatory */
  347. retval = UA_Server_addReference(server, manufacturerNameId,
  348. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  349. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  350. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  351. UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
  352. modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
  353. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  354. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  355. UA_QUALIFIEDNAME(1, "ModelName"),
  356. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  357. modelAttr, NULL, NULL);
  358. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  359. /* Define the object type for "Pump" */
  360. UA_ObjectTypeAttributes ptAttr = UA_ObjectTypeAttributes_default;
  361. ptAttr.displayName = UA_LOCALIZEDTEXT("en-US", "PumpType");
  362. retval = UA_Server_addObjectTypeNode(server, pumpTypeId, deviceTypeId,
  363. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  364. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  365. NULL, NULL);
  366. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  367. UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
  368. statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
  369. statusAttr.valueRank = UA_VALUERANK_SCALAR;
  370. UA_NodeId statusId;
  371. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  372. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  373. UA_QUALIFIEDNAME(1, "Status"),
  374. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  375. statusAttr, NULL, &statusId);
  376. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  377. /* Make the status variable mandatory */
  378. retval = UA_Server_addReference(server, statusId,
  379. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  380. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  381. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  382. UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
  383. rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
  384. rpmAttr.valueRank = UA_VALUERANK_SCALAR;
  385. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  386. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  387. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  388. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  389. rpmAttr, NULL, NULL);
  390. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  391. /* Instantiate the variable */
  392. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  393. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MyPump");
  394. retval = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  395. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  396. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  397. UA_QUALIFIEDNAME(1, "MyPump"),
  398. pumpTypeId, /* this refers to the object type
  399. identifier */
  400. oAttr, NULL, NULL);
  401. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  402. } END_TEST
  403. int main(void) {
  404. Suite *s = suite_create("services_nodemanagement");
  405. TCase *tc_addnodes = tcase_create("addnodes");
  406. tcase_add_checked_fixture(tc_addnodes, setup, teardown);
  407. tcase_add_test(tc_addnodes, AddVariableNode);
  408. tcase_add_test(tc_addnodes, InstantiateVariableTypeNode);
  409. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeWrongDims);
  410. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeLessDims);
  411. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  412. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  413. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  414. tcase_add_test(tc_addnodes, InstantiateObjectType);
  415. suite_add_tcase(s, tc_addnodes);
  416. TCase *tc_deletenodes = tcase_create("deletenodes");
  417. tcase_add_checked_fixture(tc_deletenodes, setup, teardown);
  418. tcase_add_test(tc_deletenodes, DeleteObjectWithDestructor);
  419. tcase_add_test(tc_deletenodes, DeleteObjectAndReferences);
  420. suite_add_tcase(s, tc_deletenodes);
  421. SRunner *sr = srunner_create(s);
  422. srunner_set_fork_status(sr, CK_NOFORK);
  423. srunner_run_all(sr, CK_NORMAL);
  424. int number_failed = srunner_ntests_failed(sr);
  425. srunner_free(sr);
  426. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  427. }