check_node_inheritance.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "ua_server.h"
  5. #include "server/ua_server_internal.h"
  6. #include "ua_config_default.h"
  7. #include "check.h"
  8. UA_Server *server = NULL;
  9. UA_ServerConfig *config = NULL;
  10. UA_UInt32 valueToBeInherited = 42;
  11. static void setup(void) {
  12. config = UA_ServerConfig_new_default();
  13. server = UA_Server_new(config);
  14. UA_Server_run_startup(server);
  15. }
  16. static void teardown(void) {
  17. UA_Server_run_shutdown(server);
  18. UA_Server_delete(server);
  19. UA_ServerConfig_delete(config);
  20. }
  21. /* finds the NodeId of a StateNumber child of a given node id */
  22. static void findChildId(UA_NodeId stateId, UA_NodeId referenceType, 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. UA_NodeId_copy(&bpr.targets[0].targetId.nodeId, result);
  37. UA_BrowsePathResult_deleteMembers(&bpr);
  38. }
  39. START_TEST(Nodes_createCustomStateType)
  40. {
  41. // Create a type "CustomStateType" with a variable "CustomStateNumber" as property
  42. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  43. UA_ObjectTypeAttributes attrObject = UA_ObjectTypeAttributes_default;
  44. attrObject.displayName = UA_LOCALIZEDTEXT("", "CustomStateType");
  45. attrObject.description = UA_LOCALIZEDTEXT("", "");
  46. attrObject.writeMask = 0;
  47. attrObject.userWriteMask = 0;
  48. retval = UA_Server_addObjectTypeNode(server,
  49. UA_NODEID_NUMERIC(1, 6000),
  50. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  51. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  52. UA_QUALIFIEDNAME(1, "CustomStateType"),
  53. attrObject,
  54. NULL, NULL);
  55. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  56. // Now add a property "StateNumber"
  57. UA_VariableAttributes attr = UA_VariableAttributes_default;
  58. attr.minimumSamplingInterval = 0.000000;
  59. attr.userAccessLevel = 1;
  60. attr.accessLevel = 1;
  61. attr.valueRank = -2;
  62. attr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_UINT32);
  63. UA_UInt32 val = 0;
  64. UA_Variant_setScalar(&attr.value, &val, &UA_TYPES[UA_TYPES_UINT32]);
  65. attr.displayName = UA_LOCALIZEDTEXT("", "CustomStateNumber");
  66. attr.description = UA_LOCALIZEDTEXT("", "");
  67. attr.writeMask = 0;
  68. attr.userWriteMask = 0;
  69. retval = UA_Server_addVariableNode(server,
  70. UA_NODEID_NUMERIC(1, 6001),
  71. UA_NODEID_NUMERIC(1, 6000),
  72. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  73. UA_QUALIFIEDNAME(1, "CustomStateNumber"),
  74. UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE),
  75. attr,
  76. NULL, NULL);
  77. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  78. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6001), UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  79. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  80. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  81. }
  82. END_TEST
  83. START_TEST(Nodes_createCustomObjectType)
  84. {
  85. // Create a custom object type "CustomDemoType" which has a "CustomStateType" component
  86. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  87. /* create new object type node which has a subcomponent of the type StateType */
  88. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  89. otAttr.displayName = UA_LOCALIZEDTEXT("", "CustomDemoType");
  90. otAttr.description = UA_LOCALIZEDTEXT("", "");
  91. retval = UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 6010),
  92. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  93. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  94. UA_QUALIFIEDNAME(1, "CustomDemoType"),
  95. otAttr, NULL, NULL);
  96. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  97. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  98. oAttr.displayName = UA_LOCALIZEDTEXT("", "State");
  99. oAttr.description = UA_LOCALIZEDTEXT("", "");
  100. retval = UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(1, 6010),
  101. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  102. UA_QUALIFIEDNAME(1, "State"),
  103. UA_NODEID_NUMERIC(1, 6000),
  104. oAttr, NULL, NULL);
  105. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  106. /* modelling rule is mandatory so it will be inherited for the object created from CustomDemoType */
  107. retval = UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  108. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  109. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  110. /* assign a default value to the attribute "StateNumber" inside the state attribute (part of the MyDemoType) */
  111. UA_Variant stateNum;
  112. UA_Variant_init(&stateNum);
  113. UA_Variant_setScalar(&stateNum, &valueToBeInherited, &UA_TYPES[UA_TYPES_UINT32]);
  114. UA_NodeId childID;
  115. findChildId(UA_NODEID_NUMERIC(1, 6011), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  116. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childID);
  117. ck_assert(!UA_NodeId_isNull(&childID));
  118. retval = UA_Server_writeValue(server, childID, stateNum);
  119. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  120. }
  121. END_TEST
  122. START_TEST(Nodes_createInheritedObject)
  123. {
  124. /* create an object/instance of the demo type */
  125. UA_ObjectAttributes oAttr2 = UA_ObjectAttributes_default;
  126. oAttr2.displayName = UA_LOCALIZEDTEXT("", "Demo");
  127. oAttr2.description = UA_LOCALIZEDTEXT("", "");
  128. UA_StatusCode retval = UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 6020),
  129. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  130. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  131. UA_QUALIFIEDNAME(1, "Demo"), UA_NODEID_NUMERIC(1, 6010),
  132. oAttr2, NULL, NULL);
  133. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  134. }
  135. END_TEST
  136. START_TEST(Nodes_checkInheritedValue)
  137. {
  138. UA_NodeId childState;
  139. findChildId(UA_NODEID_NUMERIC(1, 6020), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  140. UA_QUALIFIEDNAME(1, "State"), &childState);
  141. ck_assert(!UA_NodeId_isNull(&childState));
  142. UA_NodeId childNumber;
  143. findChildId(childState, UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  144. UA_QUALIFIEDNAME(1, "CustomStateNumber"), &childNumber);
  145. ck_assert(!UA_NodeId_isNull(&childNumber));
  146. UA_Variant inheritedValue;
  147. UA_Variant_init(&inheritedValue);
  148. UA_Server_readValue(server, childNumber, &inheritedValue);
  149. ck_assert(inheritedValue.type == &UA_TYPES[UA_TYPES_UINT32]);
  150. UA_UInt32 *value = (UA_UInt32 *) inheritedValue.data;
  151. ck_assert_int_eq(*value, valueToBeInherited);
  152. UA_Variant_deleteMembers(&inheritedValue);
  153. }
  154. END_TEST
  155. static Suite *testSuite_Client(void) {
  156. Suite *s = suite_create("Node inheritance");
  157. TCase *tc_inherit_subtype = tcase_create("Inherit subtype value");
  158. tcase_add_unchecked_fixture(tc_inherit_subtype, setup, teardown);
  159. tcase_add_test(tc_inherit_subtype, Nodes_createCustomStateType);
  160. tcase_add_test(tc_inherit_subtype, Nodes_createCustomObjectType);
  161. tcase_add_test(tc_inherit_subtype, Nodes_createInheritedObject);
  162. tcase_add_test(tc_inherit_subtype, Nodes_checkInheritedValue);
  163. suite_add_tcase(s, tc_inherit_subtype);
  164. return s;
  165. }
  166. int main(void) {
  167. Suite *s = testSuite_Client();
  168. SRunner *sr = srunner_create(s);
  169. srunner_set_fork_status(sr, CK_NOFORK);
  170. srunner_run_all(sr, CK_NORMAL);
  171. int number_failed = srunner_ntests_failed(sr);
  172. srunner_free(sr);
  173. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  174. }