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 *****************************************************************************/
26 #include <vlc_common.h>
28 #include "vlc_strings.h"
34 typedef char * (*conv_t) (const char *);
36 static void test (conv_t f, const char *in, const char *out)
41 printf ("\"%s\" -> \"%s\" ?\n", in, out);
43 printf ("\"%s\" -> NULL ?\n", in);
48 return; /* good: NULL -> NULL */
49 puts (" ERROR: got NULL");
52 if (out == NULL || strcmp (res, out))
54 printf (" ERROR: got \"%s\"\n", res);
61 static inline void test_decode (const char *in, const char *out)
63 test (decode_URI_duplicate, in, out);
66 static inline void test_b64 (const char *in, const char *out)
68 test (vlc_b64_encode, in, out);
71 static char *make_URI_def (const char *in)
73 return vlc_path2uri (in, NULL);
76 static inline void test_path (const char *in, const char *out)
78 test (make_URI_def, in, out);
81 static inline void test_current_directory_path (const char *in, const char *cwd, const char *out)
83 char *expected_result;
84 int val = asprintf (&expected_result, "file://%s/%s", cwd, out);
87 test (make_URI_def, in, expected_result);
88 free(expected_result);
91 static void test_url_parse(const char* in, const char* protocol, const char* user,
92 const char* pass, const char* host, unsigned i_port,
93 const char* path, const char* option )
95 #define CHECK( a, b ) \
99 assert(b != NULL && !strcmp((a), (b)))
102 vlc_UrlParse( &url, in );
103 CHECK( url.psz_protocol, protocol );
104 CHECK( url.psz_username, user );
105 CHECK( url.psz_password, pass );
106 CHECK( url.psz_host, host );
107 CHECK( url.psz_path, path );
108 assert( url.i_port == i_port );
109 CHECK( url.psz_option, option );
111 vlc_UrlClean( &url );
120 (void)setvbuf (stdout, NULL, _IONBF, 0);
121 test_decode ("this_should_not_be_modified_1234",
122 "this_should_not_be_modified_1234");
124 test_decode ("This%20should%20be%20modified%201234!",
125 "This should be modified 1234!");
127 test_decode ("%7E", "~");
129 /* tests with invalid input */
130 test_decode ("%", NULL);
131 test_decode ("%2", NULL);
132 test_decode ("%0000", "");
134 /* Non-ASCII tests */
135 test_decode ("T%C3%a9l%c3%A9vision %e2%82%Ac", "Télévision €");
136 test_decode ("T%E9l%E9vision", "T\xe9l\xe9vision");
140 test_b64 ("f", "Zg==");
141 test_b64 ("fo", "Zm8=");
142 test_b64 ("foo", "Zm9v");
143 test_b64 ("foob", "Zm9vYg==");
144 test_b64 ("fooba", "Zm9vYmE=");
145 test_b64 ("foobar", "Zm9vYmFy");
148 test_path ("/", "file:///");
149 test_path ("/home/john/", "file:///home/john/");
150 test_path ("/home/john//too///many//slashes",
151 "file:///home/john//too///many//slashes");
152 test_path ("/home/john/music.ogg", "file:///home/john/music.ogg");
154 /*int fd = open (".", O_RDONLY);
156 val = chdir ("/tmp");
161 tmpdir = getcwd(buf, sizeof(buf)/sizeof(*buf));
164 test_current_directory_path ("movie.ogg", tmpdir, "movie.ogg");
165 test_current_directory_path (".", tmpdir, ".");
166 test_current_directory_path ("", tmpdir, "");
169 assert (val != -1);*/
171 /* URI to path tests */
172 #define test( a, b ) test (make_path, a, b)
173 test ("mailto:john@example.com", NULL);
174 test ("http://www.example.com/file.html#ref", NULL);
175 test ("file://", NULL);
176 test ("file:///", "/");
177 test ("file://localhost/home/john/music%2Eogg", "/home/john/music.ogg");
178 test ("file://localhost/home/john/text#ref", "/home/john/text");
179 test ("file://localhost/home/john/text?name=value", "/home/john/text");
180 test ("file://localhost/home/john/text?name=value#ref", "/home/john/text");
181 test ("file://?name=value", NULL);
182 test ("file:///?name=value", "/");
183 test ("fd://0foobar", NULL);
184 test ("fd://0#ref", "/dev/stdin");
185 test ("fd://1", "/dev/stdout");
186 test ("fd://12345", "/dev/fd/12345");
189 test_url_parse("http://test.com", "http", NULL, NULL, "test.com", 0, NULL, NULL);
190 test_url_parse("http://test.com/", "http", NULL, NULL, "test.com", 0, "/", NULL);
191 test_url_parse("protocol://john:doe@1.2.3.4:567", "protocol", "john", "doe", "1.2.3.4", 567, NULL, NULL);
192 test_url_parse("http://a.b/?opt=val", "http", NULL, NULL, "a.b", 0, "/", "opt=val");
193 test_url_parse("p://u:p@host:123/a/b/c?o=v", "p", "u", "p", "host", 123, "/a/b/c", "o=v");
194 test_url_parse("p://?o=v", "p", NULL, NULL, "", 0, NULL, "o=v");
195 test_url_parse("p://h?o=v", "p", NULL, NULL, "h", 0, NULL, "o=v");
196 test_url_parse("p://h:123?o=v", "p", NULL, NULL, "h", 123, NULL, "o=v");
197 test_url_parse("p://u:p@h:123?o=v", "p", "u", "p", "h", 123, NULL, "o=v");