check_services_nodemanagement.c 16 KB

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