check_xml.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "ua_xml.h"
  4. #include "util/ua_util.h"
  5. #include "ua_types_generated.h"
  6. #include "ua_namespace.h"
  7. #include "ua_namespace_xml.h"
  8. #include "check.h"
  9. START_TEST(parseNumericNodeIdWithoutNsIdShallYieldNs0NodeId)
  10. {
  11. // given
  12. char txt[] = "i=2";
  13. UA_NodeId nodeId = { (UA_Byte) 0, (UA_UInt16) 0, { 0 } };
  14. // when
  15. UA_Int32 retval = UA_NodeId_copycstring(txt,&nodeId,UA_NULL);
  16. // then
  17. ck_assert_int_eq(retval,UA_SUCCESS);
  18. ck_assert_int_eq(nodeId.encodingByte,UA_NODEIDTYPE_FOURBYTE);
  19. ck_assert_int_eq(nodeId.namespace,0);
  20. ck_assert_int_eq(nodeId.identifier.numeric,2);
  21. // finally
  22. }
  23. END_TEST
  24. START_TEST(parseNumericNodeIdWithNsShallYieldNodeIdWithNs)
  25. {
  26. // given
  27. char txt[] = "ns=1;i=2";
  28. UA_NodeId nodeId = { (UA_Byte) 0, (UA_UInt16) 0, { 0 } };
  29. // when
  30. UA_Int32 retval = UA_NodeId_copycstring(txt,&nodeId,UA_NULL);
  31. // then
  32. ck_assert_int_eq(retval,UA_SUCCESS);
  33. ck_assert_int_eq(nodeId.encodingByte,UA_NODEIDTYPE_FOURBYTE);
  34. ck_assert_int_eq(nodeId.namespace,1);
  35. ck_assert_int_eq(nodeId.identifier.numeric,2);
  36. // finally
  37. }
  38. END_TEST
  39. START_TEST(loadUserNamespaceWithSingleProcessVariableShallSucceed)
  40. {
  41. // given
  42. char xml[]=
  43. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  44. "<UANodeSet>"
  45. "<UAVariable NodeId=\"ns=1;i=2\" BrowseName=\"X1\" DataType=\"i=6\">"
  46. "<DisplayName>Integer Variable</DisplayName>"
  47. "<References>"
  48. "<Reference ReferenceType=\"i=40\">i=63</Reference>"
  49. "</References>"
  50. "</UAVariable>"
  51. "</UANodeSet>";
  52. Namespace *ns;
  53. UA_NodeId nodeId;
  54. UA_Int32 retval;
  55. // when
  56. retval = Namespace_loadFromString(&ns,1,"ROOT",xml);
  57. // then
  58. ck_assert_int_eq(retval,UA_SUCCESS);
  59. ck_assert_ptr_ne(ns,UA_NULL);
  60. UA_NodeId_copycstring("ns=1;i=2",&nodeId,UA_NULL);
  61. ck_assert_int_eq(Namespace_contains(ns,&nodeId),UA_TRUE);
  62. const UA_Node* nr = UA_NULL;
  63. Namespace_Entry_Lock* nl = UA_NULL;
  64. retval = Namespace_get(ns,&nodeId,&nr,&nl);
  65. ck_assert_int_eq(retval,UA_SUCCESS);
  66. ck_assert_ptr_ne(nr,UA_NULL);
  67. ck_assert_int_eq(nr->references[0].referenceTypeId.identifier.numeric,40);
  68. ck_assert_int_eq(nr->references[0].targetId.nodeId.identifier.numeric,63);
  69. UA_NodeId_copycstring("i=2",&nodeId,UA_NULL);
  70. ck_assert_int_eq(Namespace_contains(ns,&nodeId),UA_FALSE);
  71. // finally
  72. Namespace_delete(ns);
  73. }
  74. END_TEST
  75. START_TEST(loadUserNamespaceWithSingleProcessVariableAndAliasesShallSucceed)
  76. {
  77. // given
  78. char xml[]=
  79. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  80. "<UANodeSet>"
  81. "<Aliases>"
  82. "<Alias Alias=\"Int32\">i=6</Alias>"
  83. "<Alias Alias=\"HasTypeDefinition\">i=40</Alias>"
  84. "</Aliases>"
  85. "<UAVariable NodeId=\"ns=1;i=4\" BrowseName=\"X1\" DataType=\"Int32\">"
  86. "<DisplayName>Integer Variable</DisplayName>"
  87. "<References>"
  88. "<Reference ReferenceType=\"HasTypeDefinition\">i=63</Reference>"
  89. "</References>"
  90. "</UAVariable>"
  91. "</UANodeSet>";
  92. Namespace *ns;
  93. UA_NodeId nodeId;
  94. UA_Int32 retval;
  95. // when
  96. retval = Namespace_loadFromString(&ns,1,"ROOT",xml);
  97. // then
  98. ck_assert_ptr_ne(ns,UA_NULL);
  99. ck_assert_int_eq(retval,UA_SUCCESS);
  100. UA_NodeId_copycstring("ns=1;i=4",&nodeId,UA_NULL);
  101. ck_assert_int_eq(Namespace_contains(ns,&nodeId),UA_TRUE);
  102. const UA_Node* nr = UA_NULL;
  103. Namespace_Entry_Lock* nl = UA_NULL;
  104. retval = Namespace_get(ns,&nodeId,&nr,&nl);
  105. ck_assert_int_eq(retval,UA_SUCCESS);
  106. ck_assert_ptr_ne(nr,UA_NULL);
  107. ck_assert_int_eq(nr->references[0].referenceTypeId.identifier.numeric,40);
  108. ck_assert_int_eq(nr->references[0].targetId.nodeId.identifier.numeric,63);
  109. UA_NodeId_copycstring("ns=1;i=2",&nodeId,UA_NULL);
  110. ck_assert_int_eq(Namespace_contains(ns,&nodeId),UA_FALSE);
  111. UA_NodeId_copycstring("ns=2;i=4",&nodeId,UA_NULL);
  112. ck_assert_int_eq(Namespace_contains(ns,&nodeId),UA_FALSE);
  113. UA_NodeId_copycstring("i=4",&nodeId,UA_NULL);
  114. ck_assert_int_eq(Namespace_contains(ns,&nodeId),UA_FALSE);
  115. // finally
  116. Namespace_delete(ns);
  117. }
  118. END_TEST
  119. Suite* testSuite()
  120. {
  121. Suite *s = suite_create("XML Test");
  122. TCase *tc_core = tcase_create("Core");
  123. tcase_add_test(tc_core, parseNumericNodeIdWithoutNsIdShallYieldNs0NodeId);
  124. tcase_add_test(tc_core, parseNumericNodeIdWithNsShallYieldNodeIdWithNs);
  125. tcase_add_test(tc_core, loadUserNamespaceWithSingleProcessVariableShallSucceed);
  126. tcase_add_test(tc_core, loadUserNamespaceWithSingleProcessVariableAndAliasesShallSucceed);
  127. suite_add_tcase(s,tc_core);
  128. return s;
  129. }
  130. int main (void)
  131. {
  132. int number_failed = 0;
  133. Suite *s;
  134. SRunner *sr;
  135. s = testSuite();
  136. sr = srunner_create(s);
  137. srunner_set_fork_status(sr, CK_NOFORK);
  138. srunner_run_all(sr,CK_NORMAL);
  139. number_failed += srunner_ntests_failed(sr);
  140. srunner_free(sr);
  141. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  142. }