Tests: Add runner script for tests
[lttng-tools.git] / tests / unit / test_uri.c
CommitLineData
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
DG
24
25/* For lttngerr.h */
26int lttng_opt_quiet = 1;
a4b92340 27int lttng_opt_verbose = 3;
de9a8b41 28
c291fb24
CB
29/* Number of TAP tests in this file */
30#define NUM_TESTS 11
31
3cb37009 32void 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");
de9a8b41
DG
192
193 s_uri1 = "net://:8999";
de9a8b41 194 size = uri_parse(s_uri1, &uri);
c291fb24 195 ok(size == -1, "Bad URI set to net://:8999");
3cb37009 196}
de9a8b41 197
3cb37009
CB
198void 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
3cb37009 239 res = uri_compare(uri1, uri1);
c291fb24
CB
240
241 ok(res == 0,
242 "URI compare net://localhost == net://localhost");
3cb37009
CB
243
244 res = uri_compare(uri1, uri2);
c291fb24
CB
245
246 ok(res != 0,
247 "URI compare net://localhost != net://localhost:8989:4242");
3cb37009
CB
248
249 uri_free(uri1);
250 uri_free(uri2);
de9a8b41
DG
251}
252
253int main(int argc, char **argv)
254{
c291fb24 255 diag("URI unit tests");
de9a8b41 256
c291fb24 257 plan_tests(NUM_TESTS);
de9a8b41 258
3cb37009 259 test_uri_parsing();
c291fb24 260
3cb37009 261 test_uri_cmp();
de9a8b41 262
c291fb24 263 return exit_status();
de9a8b41 264}
This page took 0.051638 seconds and 4 git commands to generate.