91bd21c5e3213a444279ef695fa1ea7c2fcc715a
[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
193 s_uri1 = "net://:8999";
194 size = uri_parse(s_uri1, &uri);
195 ok(size == -1, "Bad URI set to net://:8999");
196 }
197
198 void test_uri_cmp()
199 {
200 struct lttng_uri *uri1, *uri2;
201 const char *s_uri1 = "net://localhost";
202 const char *s_uri2 = "net://localhost:8989:4242";
203 ssize_t size1, size2;
204 int res;
205
206 size1 = uri_parse(s_uri1, &uri1);
207
208 /* Sanity checks */
209 assert(size1 == 2);
210 assert(uri1[0].dtype == LTTNG_DST_IPV4);
211 assert(uri1[0].utype == LTTNG_URI_DST);
212 assert(uri1[0].stype == 0);
213 assert(uri1[0].port == 0);
214 assert(strlen(uri1[0].subdir) == 0);
215 assert(strcmp(uri1[0].dst.ipv4, "127.0.0.1") == 0);
216 assert(uri1[1].dtype == LTTNG_DST_IPV4);
217 assert(uri1[1].utype == LTTNG_URI_DST);
218 assert(uri1[1].stype == 0);
219 assert(uri1[1].port == 0);
220 assert(strlen(uri1[1].subdir) == 0);
221 assert(strcmp(uri1[1].dst.ipv4, "127.0.0.1") == 0);
222
223 size2 = uri_parse(s_uri2, &uri2);
224
225 assert(size2 == 2);
226 assert(uri2[0].dtype == LTTNG_DST_IPV4);
227 assert(uri2[0].utype == LTTNG_URI_DST);
228 assert(uri2[0].stype == 0);
229 assert(uri2[0].port == 8989);
230 assert(strlen(uri2[1].subdir) == 0);
231 assert(strcmp(uri2[0].dst.ipv4, "127.0.0.1") == 0);
232 assert(uri2[1].dtype == LTTNG_DST_IPV4);
233 assert(uri2[1].utype == LTTNG_URI_DST);
234 assert(uri2[1].stype == 0);
235 assert(uri2[1].port == 4242);
236 assert(strlen(uri2[1].subdir) == 0);
237 assert(strcmp(uri2[1].dst.ipv4, "127.0.0.1") == 0);
238
239 res = uri_compare(uri1, uri1);
240
241 ok(res == 0,
242 "URI compare net://localhost == net://localhost");
243
244 res = uri_compare(uri1, uri2);
245
246 ok(res != 0,
247 "URI compare net://localhost != net://localhost:8989:4242");
248
249 uri_free(uri1);
250 uri_free(uri2);
251 }
252
253 int main(int argc, char **argv)
254 {
255 diag("URI unit tests");
256
257 plan_tests(NUM_TESTS);
258
259 test_uri_parsing();
260
261 test_uri_cmp();
262
263 return exit_status();
264 }
This page took 0.050717 seconds and 4 git commands to generate.