check_node_inheritance.c 9.3 KB

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