ef852c03b5ba218a02bbc2574f78c75601bef9a3
[lttng-tools.git] / tests / unit / test_uri.c
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
18 #include <assert.h>
19 #include <string.h>
20
21 #include <tap/tap.h>
22
23 #include <common/uri.h>
24
25 /* For lttngerr.h */
26 int lttng_opt_quiet = 1;
27 int lttng_opt_verbose = 3;
28
29 /* Number of TAP tests in this file */
30 #define NUM_TESTS 11
31
32 void test_uri_parsing(void)
33 {
34 ssize_t size;
35 const char *s_uri1;
36 struct lttng_uri *uri = NULL;
37
38 s_uri1 = "net://localhost";
39
40 size = uri_parse(s_uri1, &uri);
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 }
61
62 s_uri1 = "net://localhost:8989:4242/my/test/path";
63
64 size = uri_parse(s_uri1, &uri);
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 }
85
86 s_uri1 = "net://localhost:8989:4242";
87
88 size = uri_parse(s_uri1, &uri);
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 }
109
110 s_uri1 = "net6://localhost:8989";
111
112 size = uri_parse(s_uri1, &uri);
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 }
133
134 s_uri1 = "tcp://42.42.42.42/my/test/path";
135
136 size = uri_parse(s_uri1, &uri);
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 }
151
152 s_uri1 = "tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path";
153
154 size = uri_parse(s_uri1, &uri);
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 }
169
170 s_uri1 = "file:///my/test/path";
171
172 size = uri_parse(s_uri1, &uri);
173
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 */
189 s_uri1 = "file/my/test/path";
190 size = uri_parse(s_uri1, &uri);
191 ok(size == -1, "Bad URI set to file/my/test/path");
192 assert(!uri);
193
194 s_uri1 = "net://:8999";
195 size = uri_parse(s_uri1, &uri);
196 ok(size == -1, "Bad URI set to net://:8999");
197 assert(!uri);
198 }
199
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
241 res = uri_compare(uri1, uri1);
242
243 ok(res == 0,
244 "URI compare net://localhost == net://localhost");
245
246 res = uri_compare(uri1, uri2);
247
248 ok(res != 0,
249 "URI compare net://localhost != net://localhost:8989:4242");
250
251 uri_free(uri1);
252 uri_free(uri2);
253 }
254
255 int main(int argc, char **argv)
256 {
257 plan_tests(NUM_TESTS);
258
259 diag("URI unit tests");
260
261 test_uri_parsing();
262
263 test_uri_cmp();
264
265 return exit_status();
266 }
This page took 0.049577 seconds and 3 git commands to generate.