check_node_inheritance.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_ServerConfig *config = NULL;
  9. UA_UInt32 valueToBeInherited = 42;
  10. static void setup(void) {
  11. config = UA_ServerConfig_new_default();
  12. server = UA_Server_new(config);
  13. UA_Server_run_startup(server);
  14. }
  15. static void teardown(void) {
  16. UA_Server_run_shutdown(server);
  17. UA_Server_delete(server);
  18. UA_ServerConfig_delete(config);
  19. }
  20. /* finds the NodeId of a StateNumber child of a given node id */
  21. static void
  22. findChildId(UA_NodeId stateId, UA_NodeId referenceType,
  23. const UA_QualifiedName targetName, UA_NodeId *result) {
  24. UA_RelativePathElement rpe;
  25. UA_RelativePathElement_init(&rpe);
  26. rpe.referenceTypeId = referenceType;
  27. rpe.isInverse = false;
  28. rpe.includeSubtypes = false;
  29. rpe.targetName = targetName;
  30. UA_BrowsePath bp;
  31. UA_BrowsePath_init(&bp);
  32. bp.startingNode = stateId;
  33. bp.relativePath.elementsSize = 1;
  34. bp.relativePath.elements = &rpe; //clion complains but is ok
  35. UA_BrowsePathResult bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  36. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  37. ck_assert(bpr.targetsSize > 0);
  38. UA_NodeId_copy(&bpr.targets[0].targetId.nodeId, result);
  39. UA_BrowsePathResult_deleteMembers(&bpr);
  40. }
  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. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6001),
  80. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  81. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY),
  82. true);
  83. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  84. }
  85. END_TEST
  86. START_TEST(Nodes_createCustomObjectType) {
  87. /* Create a custom object type "CustomDemoType" which has a
  88. * "CustomStateType" component */
  89. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  90. /* create new object type node which has a subcomponent of the type StateType */
  91. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  92. otAttr.displayName = UA_LOCALIZEDTEXT("", "CustomDemoType");
  93. otAttr.description = UA_LOCALIZEDTEXT("", "");
  94. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 6010),
  95. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  96. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  97. UA_QUALIFIEDNAME(1, "CustomDemoType"),
  98. otAttr, NULL, NULL);
  99. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  100. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  101. oAttr.displayName = UA_LOCALIZEDTEXT("", "State");
  102. oAttr.description = UA_LOCALIZEDTEXT("", "");
  103. retval = UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(1, 6010),
  104. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  105. UA_QUALIFIEDNAME(1, "State"),
  106. UA_NODEID_NUMERIC(1, 6000),
  107. oAttr, NULL, NULL);
  108. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  109. /* modelling rule is mandatory so it will be inherited for the object
  110. * created from CustomDemoType */
  111. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6011),
  112. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  113. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY),
  114. true);
  115. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  116. /* assign a default value to the attribute "StateNumber" inside the state
  117. * attribute (part of the MyDemoType) */
  118. UA_Variant stateNum;
  119. UA_Variant_init(&stateNum);
  120. UA_Variant_setScalar(&stateNum, &valueToBeInherited, &UA_TYPES[UA_TYPES_UINT32]);
  121. UA_NodeId childID;
  122. findChildId(UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  123. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childID);
  124. ck_assert(!UA_NodeId_isNull(&childID));
  125. retval = UA_Server_writeValue(server, childID, stateNum);
  126. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  127. }
  128. END_TEST
  129. START_TEST(Nodes_createInheritedObject) {
  130. /* create an object/instance of the demo type */
  131. UA_ObjectAttributes oAttr2 = UA_ObjectAttributes_default;
  132. oAttr2.displayName = UA_LOCALIZEDTEXT("", "Demo");
  133. oAttr2.description = UA_LOCALIZEDTEXT("", "");
  134. UA_StatusCode retval =
  135. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6020),
  136. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  137. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  138. UA_QUALIFIEDNAME(1, "Demo"), UA_NODEID_NUMERIC(1, 6010),
  139. oAttr2, NULL, NULL);
  140. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  141. }
  142. END_TEST
  143. START_TEST(Nodes_checkInheritedValue) {
  144. UA_NodeId childState;
  145. findChildId(UA_NODEID_NUMERIC(1, 6020),
  146. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  147. UA_QUALIFIEDNAME(1, "State"), &childState);
  148. ck_assert(!UA_NodeId_isNull(&childState));
  149. UA_NodeId childNumber;
  150. findChildId(childState, UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  151. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childNumber);
  152. ck_assert(!UA_NodeId_isNull(&childNumber));
  153. UA_Variant inheritedValue;
  154. UA_Variant_init(&inheritedValue);
  155. UA_Server_readValue(server, childNumber, &inheritedValue);
  156. ck_assert(inheritedValue.type == &UA_TYPES[UA_TYPES_UINT32]);
  157. UA_UInt32 *value = (UA_UInt32 *) inheritedValue.data;
  158. ck_assert_int_eq(*value, valueToBeInherited);
  159. UA_Variant_deleteMembers(&inheritedValue);
  160. }
  161. END_TEST
  162. static Suite *testSuite_Client(void) {
  163. Suite *s = suite_create("Node inheritance");
  164. TCase *tc_inherit_subtype = tcase_create("Inherit subtype value");
  165. tcase_add_unchecked_fixture(tc_inherit_subtype, setup, teardown);
  166. tcase_add_test(tc_inherit_subtype, Nodes_createCustomStateType);
  167. tcase_add_test(tc_inherit_subtype, Nodes_createCustomObjectType);
  168. tcase_add_test(tc_inherit_subtype, Nodes_createInheritedObject);
  169. tcase_add_test(tc_inherit_subtype, Nodes_checkInheritedValue);
  170. suite_add_tcase(s, tc_inherit_subtype);
  171. return s;
  172. }
  173. int main(void) {
  174. Suite *s = testSuite_Client();
  175. SRunner *sr = srunner_create(s);
  176. srunner_set_fork_status(sr, CK_NOFORK);
  177. srunner_run_all(sr, CK_NORMAL);
  178. int number_failed = srunner_ntests_failed(sr);
  179. srunner_free(sr);
  180. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  181. }