check_node_inheritance.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_createCustomStateType) {
  42. // Create a type "CustomStateType" with a variable "CustomStateNumber" as property
  43. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  44. UA_ObjectTypeAttributes attrObject = UA_ObjectTypeAttributes_default;
  45. attrObject.displayName = UA_LOCALIZEDTEXT("", "CustomStateType");
  46. attrObject.description = UA_LOCALIZEDTEXT("", "");
  47. attrObject.writeMask = 0;
  48. attrObject.userWriteMask = 0;
  49. retval = UA_Server_addObjectTypeNode(server,
  50. UA_NODEID_NUMERIC(1, 6000),
  51. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  52. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  53. UA_QUALIFIEDNAME(1, "CustomStateType"),
  54. attrObject,
  55. NULL, NULL);
  56. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  57. // Now add a property "StateNumber"
  58. UA_VariableAttributes attr = UA_VariableAttributes_default;
  59. attr.minimumSamplingInterval = 0.000000;
  60. attr.userAccessLevel = 1;
  61. attr.accessLevel = 1;
  62. attr.valueRank = UA_VALUERANK_ANY;
  63. attr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_UINT32);
  64. UA_UInt32 val = 0;
  65. UA_Variant_setScalar(&attr.value, &val, &UA_TYPES[UA_TYPES_UINT32]);
  66. attr.displayName = UA_LOCALIZEDTEXT("", "CustomStateNumber");
  67. attr.description = UA_LOCALIZEDTEXT("", "");
  68. attr.writeMask = 0;
  69. attr.userWriteMask = 0;
  70. retval = UA_Server_addVariableNode(server,
  71. UA_NODEID_NUMERIC(1, 6001),
  72. UA_NODEID_NUMERIC(1, 6000),
  73. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  74. UA_QUALIFIEDNAME(1, "CustomStateNumber"),
  75. UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE),
  76. attr,
  77. NULL, NULL);
  78. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  79. /* Minimal nodeset does not contain the modelling rule mandatory */
  80. #ifdef UA_GENERATED_NAMESPACE_ZERO
  81. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6001),
  82. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  83. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY),
  84. true);
  85. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  86. #endif
  87. }
  88. END_TEST
  89. START_TEST(Nodes_createCustomObjectType) {
  90. /* Create a custom object type "CustomDemoType" which has a
  91. * "CustomStateType" component */
  92. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  93. /* create new object type node which has a subcomponent of the type StateType */
  94. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  95. otAttr.displayName = UA_LOCALIZEDTEXT("", "CustomDemoType");
  96. otAttr.description = UA_LOCALIZEDTEXT("", "");
  97. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 6010),
  98. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  99. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  100. UA_QUALIFIEDNAME(1, "CustomDemoType"),
  101. otAttr, NULL, NULL);
  102. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  103. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  104. oAttr.displayName = UA_LOCALIZEDTEXT("", "State");
  105. oAttr.description = UA_LOCALIZEDTEXT("", "");
  106. retval = UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(1, 6010),
  107. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  108. UA_QUALIFIEDNAME(1, "State"),
  109. UA_NODEID_NUMERIC(1, 6000),
  110. oAttr, NULL, NULL);
  111. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  112. /* Minimal nodeset does not contain the modelling rule mandatory */
  113. #ifdef UA_GENERATED_NAMESPACE_ZERO
  114. /* modelling rule is mandatory so it will be inherited for the object
  115. * created from CustomDemoType */
  116. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6011),
  117. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  118. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY),
  119. true);
  120. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  121. /* assign a default value to the attribute "StateNumber" inside the state
  122. * attribute (part of the MyDemoType) */
  123. UA_Variant stateNum;
  124. UA_Variant_init(&stateNum);
  125. UA_Variant_setScalar(&stateNum, &valueToBeInherited, &UA_TYPES[UA_TYPES_UINT32]);
  126. UA_NodeId childID;
  127. findChildId(UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  128. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childID);
  129. ck_assert(!UA_NodeId_isNull(&childID));
  130. retval = UA_Server_writeValue(server, childID, stateNum);
  131. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  132. #endif
  133. }
  134. END_TEST
  135. START_TEST(Nodes_createInheritedObject) {
  136. /* create an object/instance of the demo type */
  137. UA_ObjectAttributes oAttr2 = UA_ObjectAttributes_default;
  138. oAttr2.displayName = UA_LOCALIZEDTEXT("", "Demo");
  139. oAttr2.description = UA_LOCALIZEDTEXT("", "");
  140. UA_StatusCode retval =
  141. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6020),
  142. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  143. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  144. UA_QUALIFIEDNAME(1, "Demo"), UA_NODEID_NUMERIC(1, 6010),
  145. oAttr2, NULL, NULL);
  146. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  147. }
  148. END_TEST
  149. START_TEST(Nodes_checkInheritedValue) {
  150. /* Minimal nodeset does not contain the modelling rule mandatory, therefore there is no CustomStateNumber */
  151. #ifdef UA_GENERATED_NAMESPACE_ZERO
  152. UA_NodeId childState;
  153. findChildId(UA_NODEID_NUMERIC(1, 6020),
  154. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  155. UA_QUALIFIEDNAME(1, "State"), &childState);
  156. ck_assert(!UA_NodeId_isNull(&childState));
  157. UA_NodeId childNumber;
  158. findChildId(childState, UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  159. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childNumber);
  160. ck_assert(!UA_NodeId_isNull(&childNumber));
  161. UA_Variant inheritedValue;
  162. UA_Variant_init(&inheritedValue);
  163. UA_Server_readValue(server, childNumber, &inheritedValue);
  164. ck_assert(inheritedValue.type == &UA_TYPES[UA_TYPES_UINT32]);
  165. UA_UInt32 *value = (UA_UInt32 *) inheritedValue.data;
  166. ck_assert_int_eq(*value, valueToBeInherited);
  167. UA_Variant_deleteMembers(&inheritedValue);
  168. #endif
  169. }
  170. END_TEST
  171. static Suite *testSuite_Client(void) {
  172. Suite *s = suite_create("Node inheritance");
  173. TCase *tc_inherit_subtype = tcase_create("Inherit subtype value");
  174. tcase_add_unchecked_fixture(tc_inherit_subtype, setup, teardown);
  175. tcase_add_test(tc_inherit_subtype, Nodes_createCustomStateType);
  176. tcase_add_test(tc_inherit_subtype, Nodes_createCustomObjectType);
  177. tcase_add_test(tc_inherit_subtype, Nodes_createInheritedObject);
  178. tcase_add_test(tc_inherit_subtype, Nodes_checkInheritedValue);
  179. suite_add_tcase(s, tc_inherit_subtype);
  180. return s;
  181. }
  182. int main(void) {
  183. Suite *s = testSuite_Client();
  184. SRunner *sr = srunner_create(s);
  185. srunner_set_fork_status(sr, CK_NOFORK);
  186. srunner_run_all(sr, CK_NORMAL);
  187. int number_failed = srunner_ntests_failed(sr);
  188. srunner_free(sr);
  189. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  190. }