check_services_nodemanagement.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. START_TEST(AddComplexTypeWithInheritance) {
  54. /* add a variable node to the address space */
  55. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  56. attr.description = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  57. attr.displayName = UA_LOCALIZEDTEXT("en-US","fakeServerStruct");
  58. UA_NodeId myObjectNodeId = UA_NODEID_STRING(1, "the.fake.Server.Struct");
  59. UA_QualifiedName myObjectName = UA_QUALIFIEDNAME(1, "the.fake.Server.Struct");
  60. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  61. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  62. UA_StatusCode res =
  63. UA_Server_addObjectNode(server, myObjectNodeId, parentNodeId,
  64. parentReferenceNodeId, myObjectName,
  65. UA_NODEID_NUMERIC(0, 2004), attr,
  66. &handleCalled, NULL);
  67. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  68. ck_assert_int_gt(handleCalled, 0); // Should be 58, but may depend on NS0 XML detail
  69. } END_TEST
  70. START_TEST(AddNodeTwiceGivesError) {
  71. /* add a variable node to the address space */
  72. UA_VariableAttributes attr = UA_VariableAttributes_default;
  73. UA_Int32 myInteger = 42;
  74. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  75. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  76. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  77. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  78. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  79. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  80. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  81. UA_StatusCode res =
  82. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  83. parentReferenceNodeId, myIntegerName,
  84. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  85. attr, NULL, NULL);
  86. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  87. res = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  88. parentReferenceNodeId, myIntegerName,
  89. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  90. attr, NULL, NULL);
  91. ck_assert_int_eq(res, UA_STATUSCODE_BADNODEIDEXISTS);
  92. } END_TEST
  93. static UA_Boolean constructorCalled = false;
  94. static UA_StatusCode
  95. objectConstructor(UA_Server *server_,
  96. const UA_NodeId *sessionId, void *sessionContext,
  97. const UA_NodeId *typeId, void *typeContext,
  98. const UA_NodeId *nodeId, void **nodeContext) {
  99. constructorCalled = true;
  100. return UA_STATUSCODE_GOOD;
  101. }
  102. START_TEST(AddObjectWithConstructor) {
  103. /* Add an object type */
  104. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  105. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  106. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  107. UA_StatusCode res =
  108. UA_Server_addObjectTypeNode(server, objecttypeid,
  109. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  110. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  111. UA_QUALIFIEDNAME(0, "myobjecttype"), attr,
  112. NULL, NULL);
  113. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  114. /* Add a constructor to the object type */
  115. UA_NodeTypeLifecycle lifecycle;
  116. lifecycle.constructor = objectConstructor;
  117. lifecycle.destructor = NULL;
  118. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  119. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  120. /* Add an object of the type */
  121. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  122. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  123. res = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  124. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  125. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  126. UA_QUALIFIEDNAME(0, ""), objecttypeid,
  127. attr2, NULL, NULL);
  128. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  129. /* Verify that the constructor was called */
  130. ck_assert_int_eq(constructorCalled, true);
  131. } END_TEST
  132. static UA_Boolean destructorCalled = false;
  133. static void
  134. objectDestructor(UA_Server *server_,
  135. const UA_NodeId *sessionId, void *sessionContext,
  136. const UA_NodeId *typeId, void *typeContext,
  137. const UA_NodeId *nodeId, void **nodeContext) {
  138. destructorCalled = true;
  139. }
  140. START_TEST(DeleteObjectWithDestructor) {
  141. /* Add an object type */
  142. UA_NodeId objecttypeid = UA_NODEID_NUMERIC(0, 13371337);
  143. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  144. attr.displayName = UA_LOCALIZEDTEXT("en-US","my objecttype");
  145. UA_StatusCode res =
  146. UA_Server_addObjectTypeNode(server, objecttypeid,
  147. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  148. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  149. UA_QUALIFIEDNAME(0, "myobjecttype"), attr, NULL, NULL);
  150. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  151. /* Add a constructor to the object type */
  152. UA_NodeTypeLifecycle lifecycle;
  153. lifecycle.constructor = NULL;
  154. lifecycle.destructor = objectDestructor;
  155. res = UA_Server_setNodeTypeLifecycle(server, objecttypeid, lifecycle);
  156. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  157. /* Add an object of the type */
  158. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  159. UA_ObjectAttributes attr2 = UA_ObjectAttributes_default;
  160. attr2.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  161. res = UA_Server_addObjectNode(server, objectid,
  162. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  163. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  164. UA_QUALIFIEDNAME(0, ""), objecttypeid,
  165. attr2, NULL, NULL);
  166. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  167. /* Delete the object */
  168. UA_Server_deleteNode(server, objectid, true);
  169. /* Verify that the destructor was called */
  170. ck_assert_int_eq(destructorCalled, true);
  171. } END_TEST
  172. START_TEST(DeleteObjectAndReferences) {
  173. /* Add an object of the type */
  174. UA_ObjectAttributes attr = UA_ObjectAttributes_default;
  175. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  176. UA_NodeId objectid = UA_NODEID_NUMERIC(0, 23372337);
  177. UA_StatusCode res;
  178. res = UA_Server_addObjectNode(server, objectid,
  179. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  180. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  181. UA_QUALIFIEDNAME(0, ""),
  182. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  183. attr, NULL, NULL);
  184. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  185. /* Verify that we have a reference to the node from the objects folder */
  186. UA_BrowseDescription bd;
  187. UA_BrowseDescription_init(&bd);
  188. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  189. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  190. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  191. UA_BrowseResult br = UA_Server_browse(server, 0, &bd);
  192. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  193. size_t refCount = 0;
  194. for(size_t i = 0; i < br.referencesSize; ++i) {
  195. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  196. refCount++;
  197. }
  198. ck_assert_int_eq(refCount, 1);
  199. UA_BrowseResult_deleteMembers(&br);
  200. /* Delete the object */
  201. UA_Server_deleteNode(server, objectid, true);
  202. /* Browse again, this time we expect that no reference is found */
  203. br = UA_Server_browse(server, 0, &bd);
  204. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  205. refCount = 0;
  206. for(size_t i = 0; i < br.referencesSize; ++i) {
  207. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  208. refCount++;
  209. }
  210. ck_assert_int_eq(refCount, 0);
  211. UA_BrowseResult_deleteMembers(&br);
  212. /* Add an object the second time */
  213. attr = UA_ObjectAttributes_default;
  214. attr.displayName = UA_LOCALIZEDTEXT("en-US","my object");
  215. objectid = UA_NODEID_NUMERIC(0, 23372337);
  216. res = UA_Server_addObjectNode(server, objectid,
  217. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  218. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  219. UA_QUALIFIEDNAME(0, ""),
  220. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  221. attr, NULL, NULL);
  222. ck_assert_int_eq(res, UA_STATUSCODE_GOOD);
  223. /* Browse again, this time we expect that a single reference to the node is found */
  224. refCount = 0;
  225. br = UA_Server_browse(server, 0, &bd);
  226. ck_assert_int_eq(br.statusCode, UA_STATUSCODE_GOOD);
  227. for(size_t i = 0; i < br.referencesSize; ++i) {
  228. if(UA_NodeId_equal(&br.references[i].nodeId.nodeId, &objectid))
  229. refCount++;
  230. }
  231. ck_assert_int_eq(refCount, 1);
  232. UA_BrowseResult_deleteMembers(&br);
  233. } END_TEST
  234. /* Example taken from tutorial_server_object.c */
  235. START_TEST(InstantiateObjectType) {
  236. /* Define the object type */
  237. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  238. UA_StatusCode retval;
  239. /* Define the object type for "Device" */
  240. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  241. UA_ObjectTypeAttributes dtAttr = UA_ObjectTypeAttributes_default;
  242. dtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DeviceType");
  243. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  244. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  245. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  246. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  247. NULL, &deviceTypeId);
  248. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  249. UA_VariableAttributes mnAttr = UA_VariableAttributes_default;
  250. mnAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ManufacturerName");
  251. UA_NodeId manufacturerNameId;
  252. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  253. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  254. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  255. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  256. mnAttr, NULL, &manufacturerNameId);
  257. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  258. /* Make the manufacturer name mandatory */
  259. retval = UA_Server_addReference(server, manufacturerNameId,
  260. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  261. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  262. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  263. UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
  264. modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
  265. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  266. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  267. UA_QUALIFIEDNAME(1, "ModelName"),
  268. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  269. modelAttr, NULL, NULL);
  270. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  271. /* Define the object type for "Pump" */
  272. UA_ObjectTypeAttributes ptAttr = UA_ObjectTypeAttributes_default;
  273. ptAttr.displayName = UA_LOCALIZEDTEXT("en-US", "PumpType");
  274. retval = UA_Server_addObjectTypeNode(server, pumpTypeId, deviceTypeId,
  275. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  276. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  277. NULL, NULL);
  278. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  279. UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
  280. statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
  281. statusAttr.valueRank = -1;
  282. UA_NodeId statusId;
  283. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  284. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  285. UA_QUALIFIEDNAME(1, "Status"),
  286. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  287. statusAttr, NULL, &statusId);
  288. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  289. /* Make the status variable mandatory */
  290. retval = UA_Server_addReference(server, statusId,
  291. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  292. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  293. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  294. UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
  295. rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
  296. rpmAttr.valueRank = -1;
  297. retval = UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  298. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  299. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  300. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  301. rpmAttr, NULL, NULL);
  302. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  303. /* Instantiate the variable */
  304. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  305. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MyPump");
  306. retval = UA_Server_addObjectNode(server, UA_NODEID_NULL,
  307. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  308. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  309. UA_QUALIFIEDNAME(1, "MyPump"),
  310. pumpTypeId, /* this refers to the object type
  311. identifier */
  312. oAttr, NULL, NULL);
  313. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  314. } END_TEST
  315. int main(void) {
  316. Suite *s = suite_create("services_nodemanagement");
  317. TCase *tc_addnodes = tcase_create("addnodes");
  318. tcase_add_checked_fixture(tc_addnodes, setup, teardown);
  319. tcase_add_test(tc_addnodes, AddVariableNode);
  320. tcase_add_test(tc_addnodes, AddComplexTypeWithInheritance);
  321. tcase_add_test(tc_addnodes, AddNodeTwiceGivesError);
  322. tcase_add_test(tc_addnodes, AddObjectWithConstructor);
  323. tcase_add_test(tc_addnodes, InstantiateObjectType);
  324. suite_add_tcase(s, tc_addnodes);
  325. TCase *tc_deletenodes = tcase_create("deletenodes");
  326. tcase_add_checked_fixture(tc_deletenodes, setup, teardown);
  327. tcase_add_test(tc_deletenodes, DeleteObjectWithDestructor);
  328. tcase_add_test(tc_deletenodes, DeleteObjectAndReferences);
  329. suite_add_tcase(s, tc_deletenodes);
  330. SRunner *sr = srunner_create(s);
  331. srunner_set_fork_status(sr, CK_NOFORK);
  332. srunner_run_all(sr, CK_NORMAL);
  333. int number_failed = srunner_ntests_failed(sr);
  334. srunner_free(sr);
  335. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  336. }