Debian Codegraph @

tests/test_parse_url.c

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