test_parse_url.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #include "config.h"
  4. #ifdef _MSC_VER
  5. /* MSVC complains about strdup being deprecated in favor of _strdup */
  6. #define _CRT_NONSTDC_NO_DEPRECATE
  7. #endif
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <inttypes.h>
  12. #include <rabbitmq-c/amqp.h>
  13. static void match_string(const char *what, const char *expect,
  14. const char *got) {
  15. if (strcmp(got, expect)) {
  16. fprintf(stderr, "Expected %s '%s', got '%s'\n", what, expect, got);
  17. abort();
  18. }
  19. }
  20. static void match_int(const char *what, int expect, int got) {
  21. if (got != expect) {
  22. fprintf(stderr, "Expected %s '%d', got '%d'\n", what, expect, got);
  23. abort();
  24. }
  25. }
  26. static void parse_success(const char *url, const char *user,
  27. const char *password, const char *host, int port,
  28. const char *vhost) {
  29. char *s = strdup(url);
  30. struct amqp_connection_info ci;
  31. int res;
  32. res = amqp_parse_url(s, &ci);
  33. if (res) {
  34. fprintf(stderr, "Expected to successfully parse URL, but didn't: %s (%s)\n",
  35. url, amqp_error_string2(res));
  36. abort();
  37. }
  38. match_string("user", user, ci.user);
  39. match_string("password", password, ci.password);
  40. match_string("host", host, ci.host);
  41. match_int("port", port, ci.port);
  42. match_string("vhost", vhost, ci.vhost);
  43. free(s);
  44. }
  45. static void parse_fail(const char *url) {
  46. char *s = strdup(url);
  47. struct amqp_connection_info ci;
  48. amqp_default_connection_info(&ci);
  49. if (amqp_parse_url(s, &ci) >= 0) {
  50. fprintf(stderr, "Expected to fail parsing URL, but didn't: %s\n", url);
  51. abort();
  52. }
  53. free(s);
  54. }
  55. int main(void) {
  56. /* From the spec */
  57. parse_success("amqp://user:pass@host:10000/vhost", "user", "pass", "host",
  58. 10000, "vhost");
  59. parse_success("amqps://user:pass@host:10000/vhost", "user", "pass", "host",
  60. 10000, "vhost");
  61. parse_success("amqp://user%61:%61pass@ho%61st:10000/v%2fhost", "usera",
  62. "apass", "hoast", 10000, "v/host");
  63. parse_success("amqps://user%61:%61pass@ho%61st:10000/v%2fhost", "usera",
  64. "apass", "hoast", 10000, "v/host");
  65. parse_success("amqp://", "guest", "guest", "localhost", 5672, "/");
  66. parse_success("amqps://", "guest", "guest", "localhost", 5671, "/");
  67. parse_success("amqp://:@/", "", "", "localhost", 5672, "");
  68. parse_success("amqps://:@/", "", "", "localhost", 5671, "");
  69. parse_success("amqp://user@", "user", "guest", "localhost", 5672, "/");
  70. parse_success("amqps://user@", "user", "guest", "localhost", 5671, "/");
  71. parse_success("amqp://user:pass@", "user", "pass", "localhost", 5672, "/");
  72. parse_success("amqps://user:pass@", "user", "pass", "localhost", 5671, "/");
  73. parse_success("amqp://host", "guest", "guest", "host", 5672, "/");
  74. parse_success("amqps://host", "guest", "guest", "host", 5671, "/");
  75. parse_success("amqp://:10000", "guest", "guest", "localhost", 10000, "/");
  76. parse_success("amqps://:10000", "guest", "guest", "localhost", 10000, "/");
  77. parse_success("amqp:///vhost", "guest", "guest", "localhost", 5672, "vhost");
  78. parse_success("amqps:///vhost", "guest", "guest", "localhost", 5671, "vhost");
  79. parse_success("amqp://host/", "guest", "guest", "host", 5672, "");
  80. parse_success("amqps://host/", "guest", "guest", "host", 5671, "");
  81. parse_success("amqp://host/%2f", "guest", "guest", "host", 5672, "/");
  82. parse_success("amqps://host/%2f", "guest", "guest", "host", 5671, "/");
  83. parse_success("amqp://[::1]", "guest", "guest", "::1", 5672, "/");
  84. parse_success("amqps://[::1]", "guest", "guest", "::1", 5671, "/");
  85. /* Various other success cases */
  86. parse_success("amqp://host:100", "guest", "guest", "host", 100, "/");
  87. parse_success("amqps://host:100", "guest", "guest", "host", 100, "/");
  88. parse_success("amqp://[::1]:100", "guest", "guest", "::1", 100, "/");
  89. parse_success("amqps://[::1]:100", "guest", "guest", "::1", 100, "/");
  90. parse_success("amqp://host/blah", "guest", "guest", "host", 5672, "blah");
  91. parse_success("amqps://host/blah", "guest", "guest", "host", 5671, "blah");
  92. parse_success("amqp://host:100/blah", "guest", "guest", "host", 100, "blah");
  93. parse_success("amqps://host:100/blah", "guest", "guest", "host", 100, "blah");
  94. parse_success("amqp://:100/blah", "guest", "guest", "localhost", 100, "blah");
  95. parse_success("amqps://:100/blah", "guest", "guest", "localhost", 100,
  96. "blah");
  97. parse_success("amqp://[::1]/blah", "guest", "guest", "::1", 5672, "blah");
  98. parse_success("amqps://[::1]/blah", "guest", "guest", "::1", 5671, "blah");
  99. parse_success("amqp://[::1]:100/blah", "guest", "guest", "::1", 100, "blah");
  100. parse_success("amqps://[::1]:100/blah", "guest", "guest", "::1", 100, "blah");
  101. parse_success("amqp://user:pass@host", "user", "pass", "host", 5672, "/");
  102. parse_success("amqps://user:pass@host", "user", "pass", "host", 5671, "/");
  103. parse_success("amqp://user:pass@host:100", "user", "pass", "host", 100, "/");
  104. parse_success("amqps://user:pass@host:100", "user", "pass", "host", 100, "/");
  105. parse_success("amqp://user:pass@:100", "user", "pass", "localhost", 100, "/");
  106. parse_success("amqps://user:pass@:100", "user", "pass", "localhost", 100,
  107. "/");
  108. parse_success("amqp://user:pass@[::1]", "user", "pass", "::1", 5672, "/");
  109. parse_success("amqps://user:pass@[::1]", "user", "pass", "::1", 5671, "/");
  110. parse_success("amqp://user:pass@[::1]:100", "user", "pass", "::1", 100, "/");
  111. parse_success("amqps://user:pass@[::1]:100", "user", "pass", "::1", 100, "/");
  112. /* Various failure cases */
  113. parse_fail("http://www.rabbitmq.com");
  114. parse_fail("amqp://foo:bar:baz");
  115. parse_fail("amqps://foo:bar:baz");
  116. parse_fail("amqp://foo[::1]");
  117. parse_fail("amqps://foo[::1]");
  118. parse_fail("amqp://foo[::1]");
  119. parse_fail("amqps://foo[::1]");
  120. parse_fail("amqp://foo:[::1]");
  121. parse_fail("amqps://foo:[::1]");
  122. parse_fail("amqp://[::1]foo");
  123. parse_fail("amqps://[::1]foo");
  124. parse_fail("amqp://foo:1000xyz");
  125. parse_fail("amqps://foo:1000xyz");
  126. parse_fail("amqp://foo:1000000");
  127. parse_fail("amqps://foo:1000000");
  128. parse_fail("amqp://foo/bar/baz");
  129. parse_fail("amqps://foo/bar/baz");
  130. parse_fail("amqp://foo%1");
  131. parse_fail("amqps://foo%1");
  132. parse_fail("amqp://foo%1x");
  133. parse_fail("amqps://foo%1x");
  134. parse_fail("amqp://foo%xy");
  135. parse_fail("amqps://foo%xy");
  136. return 0;
  137. }