check_node_inheritance.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <check.h>
  7. UA_Server *server = NULL;
  8. UA_UInt32 valueToBeInherited = 42;
  9. static void setup(void) {
  10. server = UA_Server_new();
  11. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  12. UA_Server_run_startup(server);
  13. }
  14. static void teardown(void) {
  15. UA_Server_run_shutdown(server);
  16. UA_Server_delete(server);
  17. }
  18. #ifdef UA_GENERATED_NAMESPACE_ZERO
  19. /* finds the NodeId of a StateNumber child of a given node id */
  20. static void
  21. findChildId(UA_NodeId stateId, UA_NodeId referenceType,
  22. const UA_QualifiedName targetName, UA_NodeId *result) {
  23. UA_RelativePathElement rpe;
  24. UA_RelativePathElement_init(&rpe);
  25. rpe.referenceTypeId = referenceType;
  26. rpe.isInverse = false;
  27. rpe.includeSubtypes = false;
  28. rpe.targetName = targetName;
  29. UA_BrowsePath bp;
  30. UA_BrowsePath_init(&bp);
  31. bp.startingNode = stateId;
  32. bp.relativePath.elementsSize = 1;
  33. bp.relativePath.elements = &rpe; //clion complains but is ok
  34. UA_BrowsePathResult bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  35. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  36. ck_assert(bpr.targetsSize > 0);
  37. UA_NodeId_copy(&bpr.targets[0].targetId.nodeId, result);
  38. UA_BrowsePathResult_deleteMembers(&bpr);
  39. }
  40. #endif
  41. START_TEST(Nodes_createCustomBrowseNameObjectType)
  42. {
  43. /* Create a custom object type "CustomBrowseNameType" which has a
  44. * "DefaultInstanceBrowseName" property. */
  45. /* create new object type node which has a subcomponent of the type StateType */
  46. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  47. otAttr.displayName = UA_LOCALIZEDTEXT("", "CustomBrowseNameType");
  48. otAttr.description = UA_LOCALIZEDTEXT("", "");
  49. UA_StatusCode retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 7010),
  50. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  51. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  52. UA_QUALIFIEDNAME(1, "CustomBrowseNameType"),
  53. otAttr, NULL, NULL);
  54. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  55. // Now add a property "DefaultInstanceBrowseName"
  56. UA_VariableAttributes attr = UA_VariableAttributes_default;
  57. attr.minimumSamplingInterval = 0.000000;
  58. attr.userAccessLevel = 1;
  59. attr.accessLevel = 1;
  60. attr.valueRank = UA_VALUERANK_ANY;
  61. attr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_QUALIFIEDNAME);
  62. UA_QualifiedName defaultInstanceBrowseName = UA_QUALIFIEDNAME(1, "MyCustomBrowseName");
  63. UA_Variant_setScalar(&attr.value, &defaultInstanceBrowseName, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  64. attr.displayName = UA_LOCALIZEDTEXT("", "DefaultInstanceBrowseName");
  65. attr.description = UA_LOCALIZEDTEXT("", "");
  66. attr.writeMask = 0;
  67. attr.userWriteMask = 0;
  68. retval = UA_Server_addVariableNode(server,
  69. UA_NODEID_NUMERIC(1, 7011),
  70. UA_NODEID_NUMERIC(1, 7010),
  71. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  72. UA_QUALIFIEDNAME(0, "DefaultInstanceBrowseName"),
  73. UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE),
  74. attr,
  75. NULL, NULL);
  76. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  77. } END_TEST
  78. START_TEST(Nodes_checkDefaultInstanceBrowseName) {
  79. /* create an object/instance of the CustomDemoType.
  80. * This should fail if we do not specifiy a browse name.
  81. * CustomDemoType does not have a DefaultInstanceBrowseName
  82. * */
  83. UA_ObjectAttributes oAttr2 = UA_ObjectAttributes_default;
  84. oAttr2.displayName = UA_LOCALIZEDTEXT("", "DemoCustomBrowseNameFail");
  85. oAttr2.description = UA_LOCALIZEDTEXT("", "");
  86. UA_QualifiedName nullName;
  87. UA_QualifiedName_init(&nullName);
  88. UA_StatusCode retval =
  89. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 7020),
  90. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  91. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  92. nullName, UA_NODEID_NUMERIC(1, 6010),
  93. oAttr2, NULL, NULL);
  94. ck_assert_uint_eq(retval, UA_STATUSCODE_BADBROWSENAMEINVALID);
  95. /* create an object/instance of the CustomBrowseNameType and set the default browse name */
  96. oAttr2 = UA_ObjectAttributes_default;
  97. oAttr2.displayName = UA_LOCALIZEDTEXT("", "DemoCustomBrowseName");
  98. oAttr2.description = UA_LOCALIZEDTEXT("", "");
  99. UA_QualifiedName_init(&nullName);
  100. retval =
  101. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 7021),
  102. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  103. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  104. nullName, UA_NODEID_NUMERIC(1, 7010),
  105. oAttr2, NULL, NULL);
  106. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  107. UA_QualifiedName receivedBrowseName;
  108. UA_QualifiedName_init(&receivedBrowseName);
  109. UA_QualifiedName defaultInstanceBrowseName = UA_QUALIFIEDNAME(1, "MyCustomBrowseName");
  110. retval = UA_Server_readBrowseName(server, UA_NODEID_NUMERIC(1, 7021), &receivedBrowseName);
  111. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  112. ck_assert(UA_QualifiedName_equal(&receivedBrowseName, &defaultInstanceBrowseName) == true);
  113. UA_QualifiedName_clear(&receivedBrowseName);
  114. /* create an object/instance of the CustomBrowseNameType and set a custom browse name */
  115. oAttr2 = UA_ObjectAttributes_default;
  116. oAttr2.displayName = UA_LOCALIZEDTEXT("", "DemoCustomBrowseName");
  117. oAttr2.description = UA_LOCALIZEDTEXT("", "");
  118. UA_QualifiedName overriddenBrowseName = UA_QUALIFIEDNAME(1, "MyOverriddenBrowseName");
  119. retval =
  120. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 7022),
  121. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  122. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  123. overriddenBrowseName, UA_NODEID_NUMERIC(1, 7010),
  124. oAttr2, NULL, NULL);
  125. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  126. UA_QualifiedName_init(&receivedBrowseName);
  127. retval = UA_Server_readBrowseName(server, UA_NODEID_NUMERIC(1, 7022), &receivedBrowseName);
  128. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  129. ck_assert(UA_QualifiedName_equal(&receivedBrowseName, &overriddenBrowseName) == true);
  130. UA_QualifiedName_clear(&receivedBrowseName);
  131. }
  132. END_TEST
  133. START_TEST(Nodes_createCustomStateType) {
  134. // Create a type "CustomStateType" with a variable "CustomStateNumber" as property
  135. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  136. UA_ObjectTypeAttributes attrObject = UA_ObjectTypeAttributes_default;
  137. attrObject.displayName = UA_LOCALIZEDTEXT("", "CustomStateType");
  138. attrObject.description = UA_LOCALIZEDTEXT("", "");
  139. attrObject.writeMask = 0;
  140. attrObject.userWriteMask = 0;
  141. retval = UA_Server_addObjectTypeNode(server,
  142. UA_NODEID_NUMERIC(1, 6000),
  143. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  144. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  145. UA_QUALIFIEDNAME(1, "CustomStateType"),
  146. attrObject,
  147. NULL, NULL);
  148. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  149. // Now add a property "StateNumber"
  150. UA_VariableAttributes attr = UA_VariableAttributes_default;
  151. attr.minimumSamplingInterval = 0.000000;
  152. attr.userAccessLevel = 1;
  153. attr.accessLevel = 1;
  154. attr.valueRank = UA_VALUERANK_ANY;
  155. attr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_UINT32);
  156. UA_UInt32 val = 0;
  157. UA_Variant_setScalar(&attr.value, &val, &UA_TYPES[UA_TYPES_UINT32]);
  158. attr.displayName = UA_LOCALIZEDTEXT("", "CustomStateNumber");
  159. attr.description = UA_LOCALIZEDTEXT("", "");
  160. attr.writeMask = 0;
  161. attr.userWriteMask = 0;
  162. retval = UA_Server_addVariableNode(server,
  163. UA_NODEID_NUMERIC(1, 6001),
  164. UA_NODEID_NUMERIC(1, 6000),
  165. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  166. UA_QUALIFIEDNAME(1, "CustomStateNumber"),
  167. UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE),
  168. attr,
  169. NULL, NULL);
  170. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  171. /* Minimal nodeset does not contain the modelling rule mandatory */
  172. #ifdef UA_GENERATED_NAMESPACE_ZERO
  173. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6001),
  174. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  175. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY),
  176. true);
  177. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  178. #endif
  179. }
  180. END_TEST
  181. START_TEST(Nodes_createCustomObjectType) {
  182. /* Create a custom object type "CustomDemoType" which has a
  183. * "CustomStateType" component */
  184. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  185. /* create new object type node which has a subcomponent of the type StateType */
  186. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  187. otAttr.displayName = UA_LOCALIZEDTEXT("", "CustomDemoType");
  188. otAttr.description = UA_LOCALIZEDTEXT("", "");
  189. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 6010),
  190. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  191. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  192. UA_QUALIFIEDNAME(1, "CustomDemoType"),
  193. otAttr, NULL, NULL);
  194. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  195. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  196. oAttr.displayName = UA_LOCALIZEDTEXT("", "State");
  197. oAttr.description = UA_LOCALIZEDTEXT("", "");
  198. retval = UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(1, 6010),
  199. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  200. UA_QUALIFIEDNAME(1, "State"),
  201. UA_NODEID_NUMERIC(1, 6000),
  202. oAttr, NULL, NULL);
  203. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  204. /* Minimal nodeset does not contain the modelling rule mandatory */
  205. #ifdef UA_GENERATED_NAMESPACE_ZERO
  206. /* modelling rule is mandatory so it will be inherited for the object
  207. * created from CustomDemoType */
  208. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6011),
  209. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  210. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY),
  211. true);
  212. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  213. /* assign a default value to the attribute "StateNumber" inside the state
  214. * attribute (part of the MyDemoType) */
  215. UA_Variant stateNum;
  216. UA_Variant_init(&stateNum);
  217. UA_Variant_setScalar(&stateNum, &valueToBeInherited, &UA_TYPES[UA_TYPES_UINT32]);
  218. UA_NodeId childID;
  219. findChildId(UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  220. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childID);
  221. ck_assert(!UA_NodeId_isNull(&childID));
  222. retval = UA_Server_writeValue(server, childID, stateNum);
  223. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  224. #endif
  225. }
  226. END_TEST
  227. START_TEST(Nodes_createInheritedObject) {
  228. /* create an object/instance of the demo type */
  229. UA_ObjectAttributes oAttr2 = UA_ObjectAttributes_default;
  230. oAttr2.displayName = UA_LOCALIZEDTEXT("", "Demo");
  231. oAttr2.description = UA_LOCALIZEDTEXT("", "");
  232. UA_StatusCode retval =
  233. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6020),
  234. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  235. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  236. UA_QUALIFIEDNAME(1, "Demo"), UA_NODEID_NUMERIC(1, 6010),
  237. oAttr2, NULL, NULL);
  238. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  239. }
  240. END_TEST
  241. START_TEST(Nodes_checkInheritedValue) {
  242. /* Minimal nodeset does not contain the modelling rule mandatory, therefore there is no CustomStateNumber */
  243. #ifdef UA_GENERATED_NAMESPACE_ZERO
  244. UA_NodeId childState;
  245. findChildId(UA_NODEID_NUMERIC(1, 6020),
  246. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  247. UA_QUALIFIEDNAME(1, "State"), &childState);
  248. ck_assert(!UA_NodeId_isNull(&childState));
  249. UA_NodeId childNumber;
  250. findChildId(childState, UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  251. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childNumber);
  252. ck_assert(!UA_NodeId_isNull(&childNumber));
  253. UA_Variant inheritedValue;
  254. UA_Variant_init(&inheritedValue);
  255. UA_Server_readValue(server, childNumber, &inheritedValue);
  256. ck_assert(inheritedValue.type == &UA_TYPES[UA_TYPES_UINT32]);
  257. UA_UInt32 *value = (UA_UInt32 *) inheritedValue.data;
  258. ck_assert_int_eq(*value, valueToBeInherited);
  259. UA_Variant_deleteMembers(&inheritedValue);
  260. #endif
  261. }
  262. END_TEST
  263. static Suite *testSuite_Client(void) {
  264. Suite *s = suite_create("Node inheritance");
  265. TCase *tc_inherit_subtype = tcase_create("Inherit subtype value");
  266. tcase_add_unchecked_fixture(tc_inherit_subtype, setup, teardown);
  267. tcase_add_test(tc_inherit_subtype, Nodes_createCustomStateType);
  268. tcase_add_test(tc_inherit_subtype, Nodes_createCustomObjectType);
  269. tcase_add_test(tc_inherit_subtype, Nodes_createInheritedObject);
  270. tcase_add_test(tc_inherit_subtype, Nodes_checkInheritedValue);
  271. tcase_add_test(tc_inherit_subtype, Nodes_createCustomBrowseNameObjectType);
  272. tcase_add_test(tc_inherit_subtype, Nodes_checkDefaultInstanceBrowseName);
  273. suite_add_tcase(s, tc_inherit_subtype);
  274. return s;
  275. }
  276. int main(void) {
  277. Suite *s = testSuite_Client();
  278. SRunner *sr = srunner_create(s);
  279. srunner_set_fork_status(sr, CK_NOFORK);
  280. srunner_run_all(sr, CK_NORMAL);
  281. int number_failed = srunner_ntests_failed(sr);
  282. srunner_free(sr);
  283. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  284. }