check_utils.c 6.9 KB

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