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