check_services_nodemanagement.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include "check.h"
  8. #include "server/ua_services.h"
  9. #include "ua_client.h"
  10. #include "ua_types.h"
  11. #include "ua_config_default.h"
  12. #include "server/ua_server_internal.h"
  13. static UA_Server *server = NULL;
  14. static UA_ServerConfig *config = NULL;
  15. static UA_Int32 handleCalled = 0;
  16. static UA_StatusCode
  17. globalInstantiationMethod(UA_Server *server_,
  18. const UA_NodeId *sessionId, void *sessionContext,
  19. const UA_NodeId *nodeId, void **nodeContext) {
  20. handleCalled++;
  21. return UA_STATUSCODE_GOOD;
  22. }
  23. static void setup(void) {
  24. config = UA_ServerConfig_new_default();
  25. UA_GlobalNodeLifecycle lifecycle;
  26. lifecycle.constructor = globalInstantiationMethod;
  27. lifecycle.destructor = NULL;
  28. config->nodeLifecycle = lifecycle;
  29. server = UA_Server_new(config);
  30. }
  31. static void teardown(void) {
  32. UA_Server_delete(server);
  33. UA_ServerConfig_delete(config);
  34. }
  35. START_TEST(AddVariableNode) {
  36. /* add a variable node to the address space */
  37. UA_VariableAttributes attr = UA_VariableAttributes_default;
  38. UA_Int32 myInteger = 42;
  39. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  40. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  41. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  42. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  43. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  44. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  45. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  46. UA_StatusCode res =
  47. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  48. parentReferenceNodeId, myIntegerName,
  49. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  50. attr, NULL, NULL);
  51. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  52. } END_TEST
  53. static UA_NodeId pointTypeId;
  54. static void
  55. addVariableTypeNode(void) {
  56. UA_VariableTypeAttributes vtAttr = UA_VariableTypeAttributes_default;
  57. vtAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  58. vtAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  59. UA_UInt32 arrayDims[1] = {2};
  60. vtAttr.arrayDimensions = arrayDims;
  61. vtAttr.arrayDimensionsSize = 1;
  62. vtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Type");
  63. /* a matching default value is required */
  64. UA_Double zero[2] = {0.0, 0.0};
  65. UA_Variant_setArray(&vtAttr.value, zero, 2, &UA_TYPES[UA_TYPES_DOUBLE]);
  66. UA_StatusCode res =
  67. UA_Server_addVariableTypeNode(server, UA_NODEID_NULL,
  68. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  69. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  70. UA_QUALIFIEDNAME(1, "2DPoint Type"), UA_NODEID_NULL,
  71. vtAttr, NULL, &pointTypeId);
  72. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  73. }
  74. START_TEST(InstantiateVariableTypeNode) {
  75. addVariableTypeNode();
  76. /* Prepare the node attributes */
  77. UA_UInt32 arrayDims[1] = {2};
  78. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  79. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  80. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  81. vAttr.arrayDimensions = arrayDims;
  82. vAttr.arrayDimensionsSize = 1;
  83. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  84. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  85. /* vAttr.value is left empty, the server instantiates with the default value */
  86. /* Add the node */
  87. UA_NodeId pointVariableId;
  88. UA_StatusCode res =
  89. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  90. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  91. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  92. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  93. vAttr, NULL, &pointVariableId);
  94. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  95. /* Was the value instantiated? */
  96. UA_Variant val;
  97. UA_Server_readValue(server, pointVariableId, &val);
  98. ck_assert(val.type != NULL);
  99. UA_Variant_deleteMembers(&val);
  100. } END_TEST
  101. START_TEST(InstantiateVariableTypeNodeWrongDims) {
  102. addVariableTypeNode();
  103. /* Prepare the node attributes */
  104. UA_UInt32 arrayDims[1] = {3}; /* This will fail as the dimensions are too big */
  105. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  106. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  107. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  108. vAttr.arrayDimensions = arrayDims;
  109. vAttr.arrayDimensionsSize = 1;
  110. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  111. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  112. /* vAttr.value is left empty, the server instantiates with the default value */
  113. /* Add the node */
  114. UA_StatusCode res =
  115. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  116. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  117. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  118. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  119. vAttr, NULL, NULL);
  120. ck_assert_int_eq(UA_STATUSCODE_BADTYPEMISMATCH, res);
  121. } END_TEST
  122. START_TEST(InstantiateVariableTypeNodeLessDims) {
  123. addVariableTypeNode();
  124. /* Prepare the node attributes */
  125. UA_UInt32 arrayDims[1] = {1}; /* This will match as the dimension constraints are an upper bound */
  126. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  127. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  128. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  129. vAttr.arrayDimensions = arrayDims;
  130. vAttr.arrayDimensionsSize = 1;
  131. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  132. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  133. /* vAttr.value is left empty, the server instantiates with the default value */
  134. /* Add the node */
  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, NULL);
  141. ck_assert_int_eq(UA_STATUSCODE_BADTYPEMISMATCH, res);
  142. } END_TEST
  143. START_TEST(AddComplexTypeWithInheritance) {
  144. /* add a variable node to the address space */
  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, 2004), 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. } 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, ""), 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, ""), 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, ""),
  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, ""),
  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. /* Make the manufacturer name mandatory */
  349. retval = UA_Server_addReference(server, manufacturerNameId,
  350. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  351. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  352. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  353. UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
  354. modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
  355. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  356. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  357. UA_QUALIFIEDNAME(1, "ModelName"),
  358. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  359. modelAttr, NULL, NULL);
  360. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  361. /* Define the object type for "Pump" */
  362. UA_ObjectTypeAttributes ptAttr = UA_ObjectTypeAttributes_default;
  363. ptAttr.displayName = UA_LOCALIZEDTEXT("en-US", "PumpType");
  364. retval = UA_Server_addObjectTypeNode(server, pumpTypeId, deviceTypeId,
  365. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  366. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  367. NULL, NULL);
  368. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  369. UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
  370. statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
  371. statusAttr.valueRank = UA_VALUERANK_SCALAR;
  372. UA_NodeId statusId;
  373. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  374. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  375. UA_QUALIFIEDNAME(1, "Status"),
  376. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  377. statusAttr, NULL, &statusId);
  378. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  379. /* Make the status variable mandatory */
  380. retval = UA_Server_addReference(server, statusId,
  381. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  382. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  383. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  384. UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
  385. rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
  386. rpmAttr.valueRank = UA_VALUERANK_SCALAR;
  387. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  388. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  389. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  390. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  391. rpmAttr, NULL, NULL);
  392. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  393. /* Instantiate the variable */
  394. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  395. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MyPump");
  396. retval = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  397. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  398. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  399. UA_QUALIFIEDNAME(1, "MyPump"),
  400. pumpTypeId, /* this refers to the object type
  401. identifier */
  402. oAttr, NULL, NULL);
  403. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  404. } END_TEST
  405. int main(void) {
  406. Suite *s = suite_create("services_nodemanagement");
  407. TCase *tc_addnodes = tcase_create("addnodes");
  408. tcase_add_checked_fixture(tc_addnodes, setup, teardown);
  409. tcase_add_test(tc_addnodes, AddVariableNode);
  410. tcase_add_test(tc_addnodes, InstantiateVariableTypeNode);
  411. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeWrongDims);
  412. tcase_add_test(tc_addnodes, InstantiateVariableTypeNodeLessDims);
  413. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  414. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  415. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  416. tcase_add_test(tc_addnodes, InstantiateObjectType);
  417. suite_add_tcase(s, tc_addnodes);
  418. TCase *tc_deletenodes = tcase_create("deletenodes");
  419. tcase_add_checked_fixture(tc_deletenodes, setup, teardown);
  420. tcase_add_test(tc_deletenodes, DeleteObjectWithDestructor);
  421. tcase_add_test(tc_deletenodes, DeleteObjectAndReferences);
  422. suite_add_tcase(s, tc_deletenodes);
  423. SRunner *sr = srunner_create(s);
  424. srunner_set_fork_status(sr, CK_NOFORK);
  425. srunner_run_all(sr, CK_NORMAL);
  426. int number_failed = srunner_ntests_failed(sr);
  427. srunner_free(sr);
  428. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  429. }