check_utils.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <stdlib.h>
  5. #include "ua_types.h"
  6. #include "ua_client.h"
  7. #include "ua_util.h"
  8. #include "check.h"
  9. START_TEST(EndpointUrl_split) {
  10. UA_String hostname = UA_STRING_NULL;
  11. UA_String path = UA_STRING_NULL;
  12. UA_UInt16 port = 0;
  13. // check for too short url
  14. UA_String endPointUrl = UA_STRING("inv.ali:/");
  15. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  16. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  17. // check for opc.tcp:// protocol
  18. endPointUrl = UA_STRING("inv.ali://");
  19. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  20. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  21. // empty url
  22. endPointUrl = UA_STRING("opc.tcp://");
  23. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  24. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  25. ck_assert(UA_String_equal(&hostname, &UA_STRING_NULL));
  26. ck_assert_uint_eq(port, 0);
  27. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  28. // only hostname
  29. endPointUrl = UA_STRING("opc.tcp://hostname");
  30. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  31. UA_String expected = UA_STRING("hostname");
  32. ck_assert(UA_String_equal(&hostname, &expected));
  33. ck_assert_uint_eq(port, 0);
  34. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  35. // empty port
  36. endPointUrl = UA_STRING("opc.tcp://hostname:");
  37. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  38. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  39. ck_assert(UA_String_equal(&hostname, &expected));
  40. ck_assert_uint_eq(port, 0);
  41. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  42. // specific port
  43. endPointUrl = UA_STRING("opc.tcp://hostname:1234");
  44. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  45. UA_STATUSCODE_GOOD);
  46. ck_assert(UA_String_equal(&hostname, &expected));
  47. ck_assert_uint_eq(port, 1234);
  48. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  49. // IPv6
  50. endPointUrl = UA_STRING("opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path");
  51. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  52. expected = UA_STRING("[2001:0db8:85a3::8a2e:0370:7334]");
  53. UA_String expectedPath = UA_STRING("path");
  54. ck_assert(UA_String_equal(&hostname, &expected));
  55. ck_assert_uint_eq(port, 1234);
  56. ck_assert(UA_String_equal(&path, &expectedPath));
  57. // empty hostname
  58. endPointUrl = UA_STRING("opc.tcp://:");
  59. port = 0;
  60. path = UA_STRING_NULL;
  61. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  62. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  63. ck_assert(UA_String_equal(&hostname, &UA_STRING_NULL));
  64. ck_assert_uint_eq(port, 0);
  65. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  66. // empty hostname and no port
  67. endPointUrl = UA_STRING("opc.tcp:///");
  68. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  69. ck_assert(UA_String_equal(&hostname, &UA_STRING_NULL));
  70. ck_assert_uint_eq(port, 0);
  71. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  72. // overlength port
  73. endPointUrl = UA_STRING("opc.tcp://hostname:12345678");
  74. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  75. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  76. // port not a number
  77. endPointUrl = UA_STRING("opc.tcp://hostname:6x6");
  78. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path),
  79. UA_STATUSCODE_BADTCPENDPOINTURLINVALID);
  80. expected = UA_STRING("hostname");
  81. ck_assert(UA_String_equal(&hostname, &expected));
  82. ck_assert_uint_eq(port, 0);
  83. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  84. // no port but path
  85. endPointUrl = UA_STRING("opc.tcp://hostname/");
  86. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  87. ck_assert(UA_String_equal(&hostname, &expected));
  88. ck_assert_uint_eq(port, 0);
  89. ck_assert(UA_String_equal(&path, &UA_STRING_NULL));
  90. // port and path
  91. endPointUrl = UA_STRING("opc.tcp://hostname:1234/path");
  92. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  93. ck_assert(UA_String_equal(&hostname, &expected));
  94. ck_assert_uint_eq(port, 1234);
  95. ck_assert(UA_String_equal(&path, &expectedPath));
  96. // port and path with a slash
  97. endPointUrl = UA_STRING("opc.tcp://hostname:1234/path/");
  98. ck_assert_uint_eq(UA_parseEndpointUrl(&endPointUrl, &hostname, &port, &path), UA_STATUSCODE_GOOD);
  99. ck_assert(UA_String_equal(&hostname, &expected));
  100. ck_assert_uint_eq(port, 1234);
  101. ck_assert(UA_String_equal(&path, &expectedPath));
  102. }
  103. END_TEST
  104. START_TEST(readNumber) {
  105. UA_UInt32 result;
  106. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"x", 1, &result), 0);
  107. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"1x", 2, &result), 1);
  108. ck_assert_uint_eq(result, 1);
  109. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"123456789", 9, &result), 9);
  110. ck_assert_uint_eq(result, 123456789);
  111. }
  112. END_TEST
  113. START_TEST(StatusCode_msg) {
  114. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  115. ck_assert_str_eq(UA_StatusCode_msg(UA_STATUSCODE_GOOD), "StatusCode descriptions not available");
  116. return;
  117. #endif
  118. // first element in table
  119. ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_GOOD), "Success / No error");
  120. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOOD), "Good");
  121. // just some randomly picked status codes
  122. ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_BADNOCOMMUNICATION),
  123. "Communication with the data source is defined");
  124. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADNOCOMMUNICATION),
  125. "BadNoCommunication");
  126. ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_GOODNODATA),
  127. "No data exists for the requested time range or event filter.");
  128. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_GOODNODATA), "GoodNoData");
  129. // last element in table
  130. ck_assert_str_eq(UA_StatusCode_explanation(UA_STATUSCODE_BADMAXCONNECTIONSREACHED),
  131. "The operation could not be finished because all available connections are in use.");
  132. ck_assert_str_eq(UA_StatusCode_name(UA_STATUSCODE_BADMAXCONNECTIONSREACHED),
  133. "BadMaxConnectionsReached");
  134. // an invalid status code
  135. ck_assert_str_eq(UA_StatusCode_explanation(0x80123456), "Unknown StatusCode");
  136. ck_assert_str_eq(UA_StatusCode_name(0x80123456), "Unknown");
  137. }
  138. END_TEST
  139. static Suite* testSuite_Utils(void) {
  140. Suite *s = suite_create("Utils");
  141. TCase *tc_endpointUrl_split = tcase_create("EndpointUrl_split");
  142. tcase_add_test(tc_endpointUrl_split, EndpointUrl_split);
  143. suite_add_tcase(s,tc_endpointUrl_split);
  144. TCase *tc_utils = tcase_create("Utils");
  145. tcase_add_test(tc_utils, readNumber);
  146. tcase_add_test(tc_utils, StatusCode_msg);
  147. suite_add_tcase(s,tc_utils);
  148. return s;
  149. }
  150. int main(void) {
  151. Suite *s = testSuite_Utils();
  152. SRunner *sr = srunner_create(s);
  153. srunner_set_fork_status(sr, CK_NOFORK);
  154. srunner_run_all(sr,CK_NORMAL);
  155. int number_failed = srunner_ntests_failed(sr);
  156. srunner_free(sr);
  157. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  158. }