check_utils.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdlib.h>
  2. #include "ua_types.h"
  3. #include "ua_client.h"
  4. #include "check.h"
  5. START_TEST(EndpointUrl_split) {
  6. // check for null
  7. ck_assert_uint_eq(UA_EndpointUrl_split(NULL, NULL, NULL, NULL), UA_STATUSCODE_BADINVALIDARGUMENT);
  8. char hostname[256];
  9. UA_UInt16 port;
  10. const char* path;
  11. // check for max url length
  12. // this string has 256 chars
  13. char *overlength = "wEgfH2Sqe8AtFcUqX6VnyvZz6A4AZtbKRvGwQWvtPLrt7aaLb6wtqFzqQ2dLYLhTwJpAuVbsRTGfjvP2kvsVSYQLLeGuPjJyYnMt5e8TqtmYuPTb78uuAx7KyQB9ce95eacs3Jp32KMNtb7BTuKjQ236MnMX3mFWYAkALcj5axpQnFaGyU3HvpYrX24FTEztuZ3zpNnqBWQyHPVa6efGTzmUXMADxjw3AbG5sTGzDca7rucsfQRAZby8ZWKm66pV";
  14. ck_assert_uint_eq(UA_EndpointUrl_split(overlength, hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
  15. // check for too short url
  16. ck_assert_uint_eq(UA_EndpointUrl_split("inv.ali:/", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
  17. // check for opc.tcp:// protocol
  18. ck_assert_uint_eq(UA_EndpointUrl_split("inv.ali://", hostname, &port, &path), UA_STATUSCODE_BADATTRIBUTEIDINVALID);
  19. // empty url
  20. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://", hostname, &port, &path), UA_STATUSCODE_GOOD);
  21. ck_assert_uint_eq(strlen(hostname), 0);
  22. ck_assert_uint_eq(port, 0);
  23. ck_assert_ptr_eq(path, NULL);
  24. // only hostname
  25. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname", hostname, &port, &path), UA_STATUSCODE_GOOD);
  26. ck_assert_str_eq(hostname,"hostname");
  27. ck_assert_uint_eq(port, 0);
  28. ck_assert_ptr_eq(path, NULL);
  29. // empty port
  30. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:", hostname, &port, &path), UA_STATUSCODE_GOOD);
  31. ck_assert_str_eq(hostname,"hostname");
  32. ck_assert_uint_eq(port, 0);
  33. ck_assert_ptr_eq(path, NULL);
  34. // specific port
  35. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234", hostname, &port, &path), UA_STATUSCODE_GOOD);
  36. ck_assert_str_eq(hostname,"hostname");
  37. ck_assert_uint_eq(port, 1234);
  38. ck_assert_ptr_eq(path, NULL);
  39. // IPv6
  40. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path", hostname, &port, &path), UA_STATUSCODE_GOOD);
  41. ck_assert_str_eq(hostname,"[2001:0db8:85a3::8a2e:0370:7334]");
  42. ck_assert_uint_eq(port, 1234);
  43. ck_assert_str_eq(path, "path");
  44. // empty hostname
  45. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://:", hostname, &port, &path), UA_STATUSCODE_GOOD);
  46. ck_assert_uint_eq(strlen(hostname),0);
  47. ck_assert_uint_eq(port, 0);
  48. ck_assert_ptr_eq(path, NULL);
  49. // empty hostname and no port
  50. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp:///", hostname, &port, &path), UA_STATUSCODE_GOOD);
  51. ck_assert_uint_eq(strlen(hostname),0);
  52. ck_assert_uint_eq(port, 0);
  53. ck_assert_ptr_eq(path,0);
  54. // overlength port
  55. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:12345678", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
  56. // too high port
  57. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:65536", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
  58. // port not a number
  59. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:6x6", hostname, &port, &path), UA_STATUSCODE_BADOUTOFRANGE);
  60. // no port but path
  61. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname/", hostname, &port, &path), UA_STATUSCODE_GOOD);
  62. ck_assert_str_eq(hostname,"hostname");
  63. ck_assert_uint_eq(port, 0);
  64. ck_assert_ptr_eq(path, 0);
  65. // port and path
  66. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234/path", hostname, &port, &path), UA_STATUSCODE_GOOD);
  67. ck_assert_str_eq(hostname,"hostname");
  68. ck_assert_uint_eq(port, 1234);
  69. ck_assert_str_eq(path, "path");
  70. // full url, but only hostname required
  71. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234/path", hostname, NULL, NULL), UA_STATUSCODE_GOOD);
  72. ck_assert_str_eq(hostname,"hostname");
  73. // full url, but only hostname and port required
  74. ck_assert_uint_eq(UA_EndpointUrl_split("opc.tcp://hostname:1234/path", hostname, &port, NULL), UA_STATUSCODE_GOOD);
  75. ck_assert_str_eq(hostname,"hostname");
  76. ck_assert_uint_eq(port, 1234);
  77. }
  78. END_TEST
  79. START_TEST(readNumber) {
  80. UA_UInt32 result;
  81. ck_assert_uint_eq(UA_readNumber(NULL, 0, NULL), 0);
  82. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"x", 1, &result), 0);
  83. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"1x", 2, &result), 1);
  84. ck_assert_uint_eq(result, 1);
  85. ck_assert_uint_eq(UA_readNumber((UA_Byte*)"123456789", 9, &result), 9);
  86. ck_assert_uint_eq(result, 123456789);
  87. }
  88. END_TEST
  89. static Suite* testSuite_Utils(void) {
  90. Suite *s = suite_create("Utils");
  91. TCase *tc_endpointUrl_split = tcase_create("EndpointUrl_split");
  92. tcase_add_test(tc_endpointUrl_split, EndpointUrl_split);
  93. suite_add_tcase(s,tc_endpointUrl_split);
  94. TCase *tc_utils = tcase_create("Utils");
  95. tcase_add_test(tc_utils, readNumber);
  96. suite_add_tcase(s,tc_utils);
  97. return s;
  98. }
  99. int main(void) {
  100. Suite *s = testSuite_Utils();
  101. SRunner *sr = srunner_create(s);
  102. srunner_set_fork_status(sr, CK_NOFORK);
  103. srunner_run_all(sr,CK_NORMAL);
  104. int number_failed = srunner_ntests_failed(sr);
  105. srunner_free(sr);
  106. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  107. }