check_services_nodemanagement.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. /* Node UA_NS0ID_SERVERTYPE is not available in the minimal NS0 */
  144. #ifdef UA_GENERATED_NAMESPACE_ZERO
  145. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  146. attr.description = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  147. attr.displayName = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  148. UA_NodeId myObjectNodeId = UA_NODEID_STRING(1, "the.fake.Server.Struct");
  149. UA_QualifiedName myObjectName = UA_QUALIFIEDNAME(1, "the.fake.Server.Struct");
  150. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  151. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  152. UA_StatusCode res =
  153. UA_Server_addObjectNode(server, myObjectNodeId, parentNodeId,
  154. parentReferenceNodeId, myObjectName,
  155. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVERTYPE), attr,
  156. &handleCalled, NULL);
  157. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  158. ck_assert_int_gt(handleCalled, 0); // Should be 58, but may depend on NS0 XML detail
  159. #endif
  160. } END_TEST
  161. START_TEST(AddNodeTwiceGivesError) {
  162. /* add a variable node to the address space */
  163. UA_VariableAttributes attr = UA_VariableAttributes_default;
  164. UA_Int32 myInteger = 42;
  165. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  166. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  167. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  168. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  169. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  170. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  171. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  172. UA_StatusCode res =
  173. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  174. parentReferenceNodeId, myIntegerName,
  175. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  176. attr, NULL, NULL);
  177. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  178. res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  179. parentReferenceNodeId, myIntegerName,
  180. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  181. attr, NULL, NULL);
  182. ck_assert_int_eq(res, UA_STATUSCODE_BADNODEIDEXISTS);
  183. } END_TEST
  184. static UA_Boolean constructorCalled = false;
  185. static UA_StatusCode
  186. objectConstructor(UA_Server *server_,
  187. const UA_NodeId *sessionId, void *sessionContext,
  188. const UA_NodeId *typeId, void *typeContext,
  189. const UA_NodeId *nodeId, void **nodeContext) {
  190. constructorCalled = true;
  191. return UA_STATUSCODE_GOOD;
  192. }
  193. START_TEST(AddObjectWithConstructor) {
  194. /* Add an object type */
  195. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  196. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  197. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  198. UA_StatusCode res =
  199. UA_Server_addObjectTypeNode(server, objecttypeid,
  200. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  201. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  202. UA_QUALIFIEDNAME(0, "myobjecttype"), attr,
  203. NULL, NULL);
  204. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  205. /* Add a constructor to the object type */
  206. UA_NodeTypeLifecycle lifecycle;
  207. lifecycle.constructor = objectConstructor;
  208. lifecycle.destructor = NULL;
  209. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  210. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  211. /* Add an object of the type */
  212. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  213. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  214. res = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  215. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  216. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  217. UA_QUALIFIEDNAME(0, ""), objecttypeid,
  218. attr2, NULL, NULL);
  219. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  220. /* Verify that the constructor was called */
  221. ck_assert_int_eq(constructorCalled, true);
  222. } END_TEST
  223. static UA_Boolean destructorCalled = false;
  224. static void
  225. objectDestructor(UA_Server *server_,
  226. const UA_NodeId *sessionId, void *sessionContext,
  227. const UA_NodeId *typeId, void *typeContext,
  228. const UA_NodeId *nodeId, void **nodeContext) {
  229. destructorCalled = true;
  230. }
  231. START_TEST(DeleteObjectWithDestructor) {
  232. /* Add an object type */
  233. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  234. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  235. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  236. UA_StatusCode res =
  237. UA_Server_addObjectTypeNode(server, objecttypeid,
  238. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  239. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  240. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  241. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  242. /* Add a constructor to the object type */
  243. UA_NodeTypeLifecycle lifecycle;
  244. lifecycle.constructor = NULL;
  245. lifecycle.destructor = objectDestructor;
  246. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  247. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  248. /* Add an object of the type */
  249. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  250. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  251. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  252. res = UA_Server_addObjectNode(server, objectid,
  253. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  254. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  255. UA_QUALIFIEDNAME(0, ""), objecttypeid,
  256. attr2, NULL, NULL);
  257. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  258. /* Delete the object */
  259. UA_Server_deleteNode(server, objectid, true);
  260. /* Verify that the destructor was called */
  261. ck_assert_int_eq(destructorCalled, true);
  262. } END_TEST
  263. START_TEST(DeleteObjectAndReferences) {
  264. /* Add an object of the type */
  265. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  266. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  267. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  268. UA_StatusCode res;
  269. res = UA_Server_addObjectNode(server, objectid,
  270. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  271. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  272. UA_QUALIFIEDNAME(0, ""),
  273. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  274. attr, NULL, NULL);
  275. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  276. /* Verify that we have a reference to the node from the objects folder */
  277. UA_BrowseDescription bd;
  278. UA_BrowseDescription_init(&bd);
  279. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  280. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  281. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  282. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  283. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  284. size_t refCount = 0;
  285. for(size_t i = 0; i < br.referencesSize; ++i) {
  286. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  287. refCount++;
  288. }
  289. ck_assert_int_eq(refCount, 1);
  290. UA_BrowseResult_deleteMembers(&br);
  291. /* Delete the object */
  292. UA_Server_deleteNode(server, objectid, true);
  293. /* Browse again, this time we expect that no reference is found */
  294. br = UA_Server_browse(server, 0, &bd);
  295. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  296. refCount = 0;
  297. for(size_t i = 0; i < br.referencesSize; ++i) {
  298. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  299. refCount++;
  300. }
  301. ck_assert_int_eq(refCount, 0);
  302. UA_BrowseResult_deleteMembers(&br);
  303. /* Add an object the second time */
  304. attr = UA_ObjectAttributes_default;
  305. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  306. objectid = UA_NODEID_NUMERIC(0, 23372337);
  307. res = UA_Server_addObjectNode(server, objectid,
  308. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  309. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  310. UA_QUALIFIEDNAME(0, ""),
  311. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  312. attr, NULL, NULL);
  313. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  314. /* Browse again, this time we expect that a single reference to the node is found */
  315. refCount = 0;
  316. br = UA_Server_browse(server, 0, &bd);
  317. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  318. for(size_t i = 0; i < br.referencesSize; ++i) {
  319. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  320. refCount++;
  321. }
  322. ck_assert_int_eq(refCount, 1);
  323. UA_BrowseResult_deleteMembers(&br);
  324. } END_TEST
  325. /* Example taken from tutorial_server_object.c */
  326. START_TEST(InstantiateObjectType) {
  327. /* Define the object type */
  328. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  329. UA_StatusCode retval;
  330. /* Define the object type for "Device" */
  331. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  332. UA_ObjectTypeAttributes dtAttr = UA_ObjectTypeAttributes_default;
  333. dtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DeviceType");
  334. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  335. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  336. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  337. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  338. NULL, &deviceTypeId);
  339. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  340. UA_VariableAttributes mnAttr = UA_VariableAttributes_default;
  341. mnAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ManufacturerName");
  342. UA_NodeId manufacturerNameId;
  343. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  344. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  345. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  346. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  347. mnAttr, NULL, &manufacturerNameId);
  348. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  349. /* UA_NS0ID_MODELLINGRULE_MANDATORY is not available in Minimal Nodeset */
  350. #ifdef UA_GENERATED_NAMESPACE_ZERO
  351. /* Make the manufacturer name mandatory */
  352. retval = UA_Server_addReference(server, manufacturerNameId,
  353. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  354. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  355. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  356. #endif
  357. UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
  358. modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
  359. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  360. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  361. UA_QUALIFIEDNAME(1, "ModelName"),
  362. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  363. modelAttr, NULL, NULL);
  364. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  365. /* Define the object type for "Pump" */
  366. UA_ObjectTypeAttributes ptAttr = UA_ObjectTypeAttributes_default;
  367. ptAttr.displayName = UA_LOCALIZEDTEXT("en-US", "PumpType");
  368. retval = UA_Server_addObjectTypeNode(server, pumpTypeId, deviceTypeId,
  369. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  370. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  371. NULL, NULL);
  372. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  373. UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
  374. statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
  375. statusAttr.valueRank = UA_VALUERANK_SCALAR;
  376. UA_NodeId statusId;
  377. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  378. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  379. UA_QUALIFIEDNAME(1, "Status"),
  380. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  381. statusAttr, NULL, &statusId);
  382. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  383. /* UA_NS0ID_MODELLINGRULE_MANDATORY is not available in Minimal Nodeset */
  384. #ifdef UA_GENERATED_NAMESPACE_ZERO
  385. /* Make the status variable mandatory */
  386. retval = UA_Server_addReference(server, statusId,
  387. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  388. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  389. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  390. #endif
  391. UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
  392. rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
  393. rpmAttr.valueRank = UA_VALUERANK_SCALAR;
  394. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  395. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  396. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  397. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  398. rpmAttr, NULL, NULL);
  399. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  400. /* Instantiate the variable */
  401. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  402. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MyPump");
  403. retval = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  404. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  405. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  406. UA_QUALIFIEDNAME(1, "MyPump"),
  407. pumpTypeId, /* this refers to the object type
  408. identifier */
  409. oAttr, NULL, NULL);
  410. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  411. } END_TEST
  412. int main(void) {
  413. Suite *s = suite_create("services_nodemanagement");
  414. TCase *tc_addnodes = tcase_create("addnodes");
  415. tcase_add_checked_fixture(tc_addnodes, setup, teardown);
  416. tcase_add_test(tc_addnodes, AddVariableNode);
  417. tcase_add_test(tc_addnodes, InstantiateVariableTypeNode);
  418. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeWrongDims);
  419. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeLessDims);
  420. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  421. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  422. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  423. tcase_add_test(tc_addnodes, InstantiateObjectType);
  424. suite_add_tcase(s, tc_addnodes);
  425. TCase *tc_deletenodes = tcase_create("deletenodes");
  426. tcase_add_checked_fixture(tc_deletenodes, setup, teardown);
  427. tcase_add_test(tc_deletenodes, DeleteObjectWithDestructor);
  428. tcase_add_test(tc_deletenodes, DeleteObjectAndReferences);
  429. suite_add_tcase(s, tc_deletenodes);
  430. SRunner *sr = srunner_create(s);
  431. srunner_set_fork_status(sr, CK_NOFORK);
  432. srunner_run_all(sr, CK_NORMAL);
  433. int number_failed = srunner_ntests_failed(sr);
  434. srunner_free(sr);
  435. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  436. }