Commit | Line | Data |
---|---|---|
de9a8b41 DG |
1 | /* |
2 | * Copyright (C) - 2012 David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License as published by as | |
6 | * published by the Free Software Foundation; only version 2 of the License. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
de9a8b41 | 18 | #include <assert.h> |
de9a8b41 | 19 | #include <string.h> |
de9a8b41 | 20 | |
c291fb24 | 21 | #include <tap/tap.h> |
de9a8b41 | 22 | |
c291fb24 | 23 | #include <common/uri.h> |
de9a8b41 | 24 | |
ad7c9c18 | 25 | /* For error.h */ |
de9a8b41 | 26 | int lttng_opt_quiet = 1; |
a4b92340 | 27 | int lttng_opt_verbose = 3; |
de9a8b41 | 28 | |
c291fb24 CB |
29 | /* Number of TAP tests in this file */ |
30 | #define NUM_TESTS 11 | |
31 | ||
3cb37009 | 32 | void test_uri_parsing(void) |
de9a8b41 DG |
33 | { |
34 | ssize_t size; | |
35 | const char *s_uri1; | |
c291fb24 | 36 | struct lttng_uri *uri = NULL; |
de9a8b41 DG |
37 | |
38 | s_uri1 = "net://localhost"; | |
c291fb24 | 39 | |
de9a8b41 | 40 | size = uri_parse(s_uri1, &uri); |
c291fb24 CB |
41 | |
42 | ok(size == 2 && | |
43 | uri[0].dtype == LTTNG_DST_IPV4 && | |
44 | uri[0].utype == LTTNG_URI_DST && | |
45 | uri[0].stype == 0 && | |
46 | uri[0].port == 0 && | |
47 | strlen(uri[0].subdir) == 0 && | |
48 | strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0 && | |
49 | uri[1].dtype == LTTNG_DST_IPV4 && | |
50 | uri[1].utype == LTTNG_URI_DST && | |
51 | uri[1].stype == 0 && | |
52 | uri[1].port == 0 && | |
53 | strlen(uri[1].subdir) == 0 && | |
54 | strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0, | |
55 | "URI set to net://localhost"); | |
56 | ||
57 | if (uri) { | |
58 | uri_free(uri); | |
59 | uri = NULL; | |
60 | } | |
de9a8b41 DG |
61 | |
62 | s_uri1 = "net://localhost:8989:4242/my/test/path"; | |
c291fb24 | 63 | |
de9a8b41 | 64 | size = uri_parse(s_uri1, &uri); |
c291fb24 CB |
65 | |
66 | ok(size == 2 && | |
67 | uri[0].dtype == LTTNG_DST_IPV4 && | |
68 | uri[0].utype == LTTNG_URI_DST && | |
69 | uri[0].stype == 0 && | |
70 | uri[0].port == 8989 && | |
71 | strcmp(uri[0].subdir, "my/test/path") == 0 && | |
72 | strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0 && | |
73 | uri[1].dtype == LTTNG_DST_IPV4 && | |
74 | uri[1].utype == LTTNG_URI_DST && | |
75 | uri[1].stype == 0 && | |
76 | uri[1].port == 4242 && | |
77 | strcmp(uri[0].subdir, "my/test/path") == 0 && | |
78 | strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0, | |
79 | "URI set to net://localhost:8989:4242/my/test/path"); | |
80 | ||
81 | if (uri) { | |
82 | uri_free(uri); | |
83 | uri = NULL; | |
84 | } | |
de9a8b41 DG |
85 | |
86 | s_uri1 = "net://localhost:8989:4242"; | |
c291fb24 | 87 | |
de9a8b41 | 88 | size = uri_parse(s_uri1, &uri); |
c291fb24 CB |
89 | |
90 | ok(size == 2 && | |
91 | uri[0].dtype == LTTNG_DST_IPV4 && | |
92 | uri[0].utype == LTTNG_URI_DST && | |
93 | uri[0].stype == 0 && | |
94 | uri[0].port == 8989 && | |
95 | strlen(uri[1].subdir) == 0 && | |
96 | strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0 && | |
97 | uri[1].dtype == LTTNG_DST_IPV4 && | |
98 | uri[1].utype == LTTNG_URI_DST && | |
99 | uri[1].stype == 0 && | |
100 | uri[1].port == 4242 && | |
101 | strlen(uri[1].subdir) == 0 && | |
102 | strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0, | |
103 | "URI set to net://localhost:8989:4242"); | |
104 | ||
105 | if (uri) { | |
106 | uri_free(uri); | |
107 | uri = NULL; | |
108 | } | |
de9a8b41 DG |
109 | |
110 | s_uri1 = "net6://localhost:8989"; | |
c291fb24 | 111 | |
de9a8b41 | 112 | size = uri_parse(s_uri1, &uri); |
c291fb24 CB |
113 | |
114 | ok(size == 2 && | |
115 | uri[0].dtype == LTTNG_DST_IPV6 && | |
116 | uri[0].utype == LTTNG_URI_DST && | |
117 | uri[0].stype == 0 && | |
118 | uri[0].port == 8989 && | |
119 | strlen(uri[1].subdir) == 0 && | |
120 | strcmp(uri[0].dst.ipv6, "::1") == 0 && | |
121 | uri[1].dtype == LTTNG_DST_IPV6 && | |
122 | uri[1].utype == LTTNG_URI_DST && | |
123 | uri[1].stype == 0 && | |
124 | uri[1].port == 0 && | |
125 | strlen(uri[1].subdir) == 0 && | |
126 | strcmp(uri[0].dst.ipv6, "::1") == 0, | |
127 | "URI set to net6://localhost:8989"); | |
128 | ||
129 | if (uri) { | |
130 | uri_free(uri); | |
131 | uri = NULL; | |
132 | } | |
de9a8b41 DG |
133 | |
134 | s_uri1 = "tcp://42.42.42.42/my/test/path"; | |
c291fb24 | 135 | |
de9a8b41 | 136 | size = uri_parse(s_uri1, &uri); |
c291fb24 CB |
137 | |
138 | ok(size == 1 && | |
139 | uri[0].dtype == LTTNG_DST_IPV4 && | |
140 | uri[0].utype == LTTNG_URI_DST && | |
141 | uri[0].stype == 0 && | |
142 | uri[0].port == 0 && | |
143 | strcmp(uri[0].subdir, "my/test/path") == 0 && | |
144 | strcmp(uri[0].dst.ipv4, "42.42.42.42") == 0, | |
145 | "URI set to tcp://42.42.42.42/my/test/path"); | |
146 | ||
147 | if (uri) { | |
148 | uri_free(uri); | |
149 | uri = NULL; | |
150 | } | |
de9a8b41 | 151 | |
a4b92340 | 152 | s_uri1 = "tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path"; |
c291fb24 | 153 | |
de9a8b41 | 154 | size = uri_parse(s_uri1, &uri); |
c291fb24 CB |
155 | |
156 | ok(size == 1 && | |
157 | uri[0].dtype == LTTNG_DST_IPV6 && | |
158 | uri[0].utype == LTTNG_URI_DST && | |
159 | uri[0].stype == 0 && | |
160 | uri[0].port == 0 && | |
161 | strcmp(uri[0].subdir, "my/test/path") == 0 && | |
162 | strcmp(uri[0].dst.ipv6, "fe80::f66d:4ff:fe53:d220") == 0, | |
163 | "URI set to tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path"); | |
164 | ||
165 | if (uri) { | |
166 | uri_free(uri); | |
167 | uri = NULL; | |
168 | } | |
de9a8b41 DG |
169 | |
170 | s_uri1 = "file:///my/test/path"; | |
c291fb24 | 171 | |
de9a8b41 | 172 | size = uri_parse(s_uri1, &uri); |
de9a8b41 | 173 | |
c291fb24 CB |
174 | ok(size == 1 && |
175 | uri[0].dtype == LTTNG_DST_PATH && | |
176 | uri[0].utype == LTTNG_URI_DST && | |
177 | uri[0].stype == 0 && | |
178 | uri[0].port == 0 && | |
179 | strlen(uri[0].subdir) == 0 && | |
180 | strcmp(uri[0].dst.path, "/my/test/path") == 0, | |
181 | "URI set to file:///my/test/path"); | |
182 | ||
183 | if (uri) { | |
184 | uri_free(uri); | |
185 | uri = NULL; | |
186 | } | |
187 | ||
188 | /* FIXME: Noisy on stdout */ | |
de9a8b41 | 189 | s_uri1 = "file/my/test/path"; |
de9a8b41 | 190 | size = uri_parse(s_uri1, &uri); |
c291fb24 | 191 | ok(size == -1, "Bad URI set to file/my/test/path"); |
2e67d81d | 192 | assert(!uri); |
de9a8b41 DG |
193 | |
194 | s_uri1 = "net://:8999"; | |
de9a8b41 | 195 | size = uri_parse(s_uri1, &uri); |
c291fb24 | 196 | ok(size == -1, "Bad URI set to net://:8999"); |
2e67d81d | 197 | assert(!uri); |
3cb37009 | 198 | } |
de9a8b41 | 199 | |
3cb37009 CB |
200 | void test_uri_cmp() |
201 | { | |
202 | struct lttng_uri *uri1, *uri2; | |
203 | const char *s_uri1 = "net://localhost"; | |
204 | const char *s_uri2 = "net://localhost:8989:4242"; | |
205 | ssize_t size1, size2; | |
206 | int res; | |
207 | ||
208 | size1 = uri_parse(s_uri1, &uri1); | |
209 | ||
210 | /* Sanity checks */ | |
211 | assert(size1 == 2); | |
212 | assert(uri1[0].dtype == LTTNG_DST_IPV4); | |
213 | assert(uri1[0].utype == LTTNG_URI_DST); | |
214 | assert(uri1[0].stype == 0); | |
215 | assert(uri1[0].port == 0); | |
216 | assert(strlen(uri1[0].subdir) == 0); | |
217 | assert(strcmp(uri1[0].dst.ipv4, "127.0.0.1") == 0); | |
218 | assert(uri1[1].dtype == LTTNG_DST_IPV4); | |
219 | assert(uri1[1].utype == LTTNG_URI_DST); | |
220 | assert(uri1[1].stype == 0); | |
221 | assert(uri1[1].port == 0); | |
222 | assert(strlen(uri1[1].subdir) == 0); | |
223 | assert(strcmp(uri1[1].dst.ipv4, "127.0.0.1") == 0); | |
224 | ||
225 | size2 = uri_parse(s_uri2, &uri2); | |
226 | ||
227 | assert(size2 == 2); | |
228 | assert(uri2[0].dtype == LTTNG_DST_IPV4); | |
229 | assert(uri2[0].utype == LTTNG_URI_DST); | |
230 | assert(uri2[0].stype == 0); | |
231 | assert(uri2[0].port == 8989); | |
232 | assert(strlen(uri2[1].subdir) == 0); | |
233 | assert(strcmp(uri2[0].dst.ipv4, "127.0.0.1") == 0); | |
234 | assert(uri2[1].dtype == LTTNG_DST_IPV4); | |
235 | assert(uri2[1].utype == LTTNG_URI_DST); | |
236 | assert(uri2[1].stype == 0); | |
237 | assert(uri2[1].port == 4242); | |
238 | assert(strlen(uri2[1].subdir) == 0); | |
239 | assert(strcmp(uri2[1].dst.ipv4, "127.0.0.1") == 0); | |
240 | ||
3cb37009 | 241 | res = uri_compare(uri1, uri1); |
c291fb24 CB |
242 | |
243 | ok(res == 0, | |
244 | "URI compare net://localhost == net://localhost"); | |
3cb37009 CB |
245 | |
246 | res = uri_compare(uri1, uri2); | |
c291fb24 CB |
247 | |
248 | ok(res != 0, | |
249 | "URI compare net://localhost != net://localhost:8989:4242"); | |
3cb37009 CB |
250 | |
251 | uri_free(uri1); | |
252 | uri_free(uri2); | |
de9a8b41 DG |
253 | } |
254 | ||
255 | int main(int argc, char **argv) | |
256 | { | |
c291fb24 | 257 | plan_tests(NUM_TESTS); |
de9a8b41 | 258 | |
e3bef725 CB |
259 | diag("URI unit tests"); |
260 | ||
3cb37009 | 261 | test_uri_parsing(); |
c291fb24 | 262 | |
3cb37009 | 263 | test_uri_cmp(); |
de9a8b41 | 264 | |
c291fb24 | 265 | return exit_status(); |
de9a8b41 | 266 | } |