1 /*****************************************************************************
2 * url.c: Test for url encoding/decoding stuff
3 *****************************************************************************
4 * Copyright (C) 2006 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
30 #include <vlc_common.h>
32 #include <vlc_strings.h>
34 static int exitcode
= 0;
36 static void test_compare(const char *in
, const char *exp
, const char *res
)
41 fprintf(stderr
, "\"%s\" returned NULL, expected \"%s\"", in
, exp
);
48 fprintf(stderr
, "\"%s\" returned \"%s\", expected NULL", in
, res
);
51 fprintf(stderr
, "\"%s\" returned \"%s\", expected \"%s\"\n", in
,
59 typedef char * (*conv_t
) (const char *);
61 static void test (conv_t f
, const char *in
, const char *out
)
64 test_compare(in
, out
, res
);
68 static inline void test_decode (const char *in
, const char *out
)
70 test (vlc_uri_decode_duplicate
, in
, out
);
73 static inline void test_b64 (const char *in
, const char *out
)
75 test (vlc_b64_encode
, in
, out
);
78 static char *make_URI_def (const char *in
)
80 return vlc_path2uri (in
, NULL
);
83 static inline void test_path (const char *in
, const char *out
)
85 test (make_URI_def
, in
, out
);
88 static inline void test_current_directory_path (const char *in
, const char *cwd
, const char *out
)
90 char *expected_result
;
91 int val
= asprintf (&expected_result
, "file://%s/%s", cwd
, out
);
95 test (make_URI_def
, in
, expected_result
);
96 free(expected_result
);
99 static void test_url_parse(const char *in
, const char *protocol
,
100 const char *user
, const char *pass
,
101 const char *host
, unsigned port
,
102 const char *path
, const char *option
)
105 int ret
= vlc_UrlParse(&url
, in
);
107 /* XXX: only checking that the port-part is parsed correctly, and
108 * equal to 0, is currently not supported due to the below. */
109 if (protocol
== NULL
&& user
== NULL
&& pass
== NULL
&& host
== NULL
110 && port
== 0 && path
== NULL
&& option
== NULL
)
116 fprintf(stderr
, "\"%s\" accepted, expected rejection\n", in
);
122 test_compare(in
, url
.psz_protocol
, protocol
);
123 test_compare(in
, url
.psz_username
, user
);
124 test_compare(in
, url
.psz_password
, pass
);
126 if (ret
!= 0 && errno
== ENOSYS
)
128 test_compare(in
, url
.psz_host
, NULL
);
132 test_compare(in
, url
.psz_host
, host
);
134 if (url
.i_port
!= port
)
136 fprintf(stderr
, "\"%s\" returned %u, expected %u\n", in
, url
.i_port
,
141 test_compare(in
, url
.psz_path
, path
);
142 test_compare(in
, url
.psz_option
, option
);
146 static char *vlc_uri_resolve_rfc3986_test(const char *in
)
148 return vlc_uri_resolve("http://a/b/c/d;p?q", in
);
151 static void test_rfc3986(const char *reference
, const char *expected
)
153 test(vlc_uri_resolve_rfc3986_test
, reference
, expected
);
156 static void test_fixup_noop(const char *expected
)
158 test(vlc_uri_fixup
, expected
, expected
);
163 (void)setvbuf (stdout
, NULL
, _IONBF
, 0);
164 test_decode ("this_should_not_be_modified_1234",
165 "this_should_not_be_modified_1234");
167 test_decode ("This%20should%20be%20modified%201234!",
168 "This should be modified 1234!");
170 test_decode ("%7E", "~");
172 /* tests with invalid input */
173 test_decode ("%", NULL
);
174 test_decode ("%2", NULL
);
175 test_decode ("%0000", "");
177 /* Non-ASCII tests */
178 test_decode ("T%C3%a9l%c3%A9vision %e2%82%Ac", "Télévision €");
179 test_decode ("T%E9l%E9vision", "T\xe9l\xe9vision");
183 test_b64 ("f", "Zg==");
184 test_b64 ("fo", "Zm8=");
185 test_b64 ("foo", "Zm9v");
186 test_b64 ("foob", "Zm9vYg==");
187 test_b64 ("fooba", "Zm9vYmE=");
188 test_b64 ("foobar", "Zm9vYmFy");
192 test_path ("/", "file:///");
193 test_path ("/home/john/", "file:///home/john/");
194 test_path ("/home/john//too///many//slashes",
195 "file:///home/john//too///many//slashes");
196 test_path ("/home/john/music.ogg", "file:///home/john/music.ogg");
198 test_path ("C:\\", "file:///C:/");
199 test_path ("C:\\Users\\john\\", "file:///C:/Users/john/");
200 test_path ("C:\\Users\\john\\music.ogg",
201 "file:///C:/Users/john/music.ogg");
202 test_path ("\\\\server\\share\\dir\\file.ext",
203 "file://server/share/dir/file.ext");
206 /*int fd = open (".", O_RDONLY);
209 #ifndef _WIN32 /* FIXME: deal with anti-slashes */
217 char *tmpdir
= getcwd(buf
, sizeof (buf
) / sizeof (*buf
));
224 test_current_directory_path ("movie.ogg", tmpdir
, "movie.ogg");
225 test_current_directory_path (".", tmpdir
, ".");
226 test_current_directory_path ("", tmpdir
, "");
230 assert (val != -1);*/
232 /* URI to path tests */
233 #define test( a, b ) test (vlc_uri2path, a, b)
234 test ("mailto:john@example.com", NULL
);
235 test ("http://www.example.com/file.html#ref", NULL
);
236 test ("file://", NULL
);
238 test ("file:///", "/");
239 test ("file://localhost/home/john/music%2Eogg", "/home/john/music.ogg");
240 test ("file://localhost/home/john/text#ref", "/home/john/text");
241 test ("file://localhost/home/john/text?name=value", "/home/john/text");
242 test ("file://localhost/home/john/text?name=value#ref", "/home/john/text");
243 test ("file://?name=value", NULL
);
244 test ("file:///?name=value", "/");
245 test ("fd://0foobar", NULL
);
246 test ("fd://0#ref", "/dev/stdin");
247 test ("fd://1", "/dev/stdout");
248 test ("fd://12345", "/dev/fd/12345");
250 test ("file:///C:", "C:");
251 test ("file:///C:/Users/john/music%2Eogg", "C:\\Users\\john\\music.ogg");
252 test ("file://server/share/dir/file%2Eext",
253 "\\\\server\\share\\dir\\file.ext");
254 test ("file:///C:/Users/john/text#ref", "C:\\Users\\john\\text");
255 test ("file:///C:/Users/john/text?name=value", "C:\\Users\\john\\text");
256 test ("file:///C:/Users/john/text?name=value#ref",
257 "C:\\Users\\john\\text");
258 test ("file://?name=value", NULL
);
259 test ("file:///C:?name=value", "C:");
260 test ("fd://0foobar", NULL
);
261 test ("fd://0#ref", "CON");
262 test ("fd://1", "CON");
263 test ("fd://12345", NULL
);
267 test_url_parse("http://example.com", "http", NULL
, NULL
, "example.com", 0,
269 test_url_parse("http://example.com/", "http", NULL
, NULL
, "example.com", 0,
271 test_url_parse("http://[2001:db8::1]", "http", NULL
, NULL
, "2001:db8::1",
273 test_url_parse("http://example.com:", "http", NULL
, NULL
, "example.com", 0,
275 test_url_parse("protocol://john:doe@1.2.3.4:567", "protocol", "john", "doe", "1.2.3.4", 567, NULL
, NULL
);
276 test_url_parse("http://a.b/?opt=val", "http", NULL
, NULL
, "a.b", 0, "/", "opt=val");
277 test_url_parse("p://u:p@host:123/a/b/c?o=v", "p", "u", "p", "host", 123, "/a/b/c", "o=v");
278 test_url_parse("p://?o=v", "p", NULL
, NULL
, "", 0, NULL
, "o=v");
279 test_url_parse("p://h?o=v", "p", NULL
, NULL
, "h", 0, NULL
, "o=v");
280 test_url_parse("p://h:123?o=v", "p", NULL
, NULL
, "h", 123, NULL
, "o=v");
281 test_url_parse("p://u:p@h:123?o=v", "p", "u", "p", "h", 123, NULL
, "o=v");
282 test_url_parse("p://caf\xc3\xa9.example.com", "p", NULL
, NULL
,
283 "xn--caf-dma.example.com", 0, NULL
, NULL
);
284 test_url_parse("p://caf%C3%A9.example.com", "p", NULL
, NULL
,
285 "xn--caf-dma.example.com", 0, NULL
, NULL
);
286 test_url_parse("p://www.example.com/caf\xc3\xa9/", "p", NULL
, NULL
,
287 "www.example.com", 0, "/caf%C3%A9/", NULL
);
288 test_url_parse("p://h/white%20spaced", "p", NULL
, NULL
, "h", 0,
289 "/white%20spaced", NULL
);
291 test_url_parse("//example.com", NULL
, NULL
, NULL
, "example.com", 0,
293 test_url_parse("/file", NULL
, NULL
, NULL
, NULL
, 0, "/file", NULL
);
294 test_url_parse("?opt=val", NULL
, NULL
, NULL
, NULL
, 0, "", "opt=val");
295 test_url_parse("?o1=v1&o2=v2", NULL
, NULL
, NULL
, NULL
, 0, "",
297 test_url_parse("/f?o=v", NULL
, NULL
, NULL
, NULL
, 0, "/f", "o=v");
298 test_url_parse("//example.com/file", NULL
, NULL
, NULL
, "example.com", 0,
300 test_url_parse("//example.com?opt=val", NULL
, NULL
, NULL
, "example.com", 0,
302 test_url_parse("//example.com/f?o=v", NULL
, NULL
, NULL
, "example.com", 0,
305 test_url_parse("p://G a r b a g e", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
306 test_url_parse("p://h/G a r b a g e", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
307 test_url_parse("http://example.com:123xyz", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
308 test_url_parse("http://example.com: 123", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
309 test_url_parse("http://example.com:+123", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
310 test_url_parse("http://example.com:-123", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
311 test_url_parse("http://example.com:-4294967298", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
312 test_url_parse("http://example.com:-18446744073709551615", NULL
, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
314 /* Reference test cases for reference URI resolution */
315 static const char *rfc3986_cases
[] =
318 "g", "http://a/b/c/g",
319 "./g", "http://a/b/c/g",
320 "g/", "http://a/b/c/g/",
323 "?y", "http://a/b/c/d;p?y",
324 "g?y", "http://a/b/c/g?y",
325 //"#s", "http://a/b/c/d;p?q#s",
326 //"g#s", "http://a/b/c/g#s",
327 //"g?y#s", "http://a/b/c/g?y#s",
328 ";x", "http://a/b/c/;x",
329 "g;x", "http://a/b/c/g;x",
330 //"g;x?y#s", "http://a/b/c/g;x?y#s",
331 "", "http://a/b/c/d;p?q",
332 ".", "http://a/b/c/",
333 "./", "http://a/b/c/",
335 "../", "http://a/b/",
336 "../g", "http://a/b/g",
337 "../..", "http://a/",
338 "../../", "http://a/",
339 "../../g", "http://a/g",
341 "../../../g", "http://a/g",
342 "../../../../g", "http://a/g",
344 "/./g", "http://a/g",
345 "/../g", "http://a/g",
346 "g.", "http://a/b/c/g.",
347 ".g", "http://a/b/c/.g",
348 "g..", "http://a/b/c/g..",
349 "..g", "http://a/b/c/..g",
351 "./../g", "http://a/b/g",
352 "./g/.", "http://a/b/c/g/",
353 "g/./h", "http://a/b/c/g/h",
354 "g/../h", "http://a/b/c/h",
355 "g;x=1/./y", "http://a/b/c/g;x=1/y",
356 "g;x=1/../y", "http://a/b/c/y",
358 "g?y/./x", "http://a/b/c/g?y/./x",
359 "g?y/../x", "http://a/b/c/g?y/../x",
360 //"g#s/./x", "http://a/b/c/g#s/./x",
361 //"g#s/../x", "http://a/b/c/g#s/../x",
364 for (size_t i
= 0; i
< ARRAY_SIZE(rfc3986_cases
); i
+= 2)
365 test_rfc3986(rfc3986_cases
[i
], rfc3986_cases
[i
+ 1]);
367 /* Check that fixup does not mangle valid URIs */
368 static const char *valid_uris
[] =
371 ".", "..", "/", "../../dir/subdir/subsubdir/file.ext",
372 "//example.com?q=info",
373 "//192.0.2.1/index.html",
374 "//[2001:db8::1]/index.html",
375 "https://www.example.com:8443/?opt1=val1&opt2=val2",
376 "https://192.0.2.1:8443/#foobar",
377 "https://[2001:db8::1]:8443/file?opt=val#foobar",
378 "https://[v9.abcd:efgh]:8443/welcome?to=the#future",
379 "mailto:john@example.com",
380 "mailto:mailman@example.com?subject=help",
381 "mailto:mailman@example.com?body=subscribe%20news-flash",
382 "mailto:literal@[192.0.2.1],literal@[IPv6:2001:db8::1]",
385 for (size_t i
= 0; i
< ARRAY_SIZE(valid_uris
); i
++)
386 test_fixup_noop(valid_uris
[i
]);