]> git.sesse.net Git - vlc/blob - src/test/url.c
Add vlc_UrlParse tests
[vlc] / src / test / url.c
1 /*****************************************************************************
2  * url.c: Test for url encoding/decoding stuff
3  *****************************************************************************
4  * Copyright (C) 2006 Rémi Denis-Courmont
5  * $Id$
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include "vlc_url.h"
28 #include "vlc_strings.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <assert.h>
33
34 typedef char * (*conv_t) (const char *);
35
36 static void test (conv_t f, const char *in, const char *out)
37 {
38     char *res;
39
40     if (out != NULL)
41        printf ("\"%s\" -> \"%s\" ?\n", in, out);
42     else
43        printf ("\"%s\" -> NULL ?\n", in);
44     res = f (in);
45     if (res == NULL)
46     {
47         if (out == NULL)
48             return; /* good: NULL -> NULL */
49         puts (" ERROR: got NULL");
50         exit (2);
51     }
52     if (out == NULL || strcmp (res, out))
53     {
54         printf (" ERROR: got \"%s\"\n", res);
55         exit (2);
56     }
57
58     free (res);
59 }
60
61 static inline void test_decode (const char *in, const char *out)
62 {
63     test (decode_URI_duplicate, in, out);
64 }
65
66 static inline void test_b64 (const char *in, const char *out)
67 {
68     test (vlc_b64_encode, in, out);
69 }
70
71 static char *make_URI_def (const char *in)
72 {
73     return vlc_path2uri (in, NULL);
74 }
75
76 static inline void test_path (const char *in, const char *out)
77 {
78     test (make_URI_def, in, out);
79 }
80
81 static inline void test_current_directory_path (const char *in, const char *cwd, const char *out)
82 {
83     char * expected_result = NULL;
84     int val = asprintf(&expected_result, "file://%s/%s", cwd, out);
85     assert (val != -1);
86
87     test (make_URI_def, in, expected_result);
88 }
89
90 static void test_url_parse(const char* in, const char* protocol, const char* user,
91                            const char* pass, const char* host, unsigned i_port,
92                            const char* path, const char* option )
93 {
94 #define CHECK( a, b ) assert(((a == NULL) && (b == NULL)) || !strcmp((a), (b)))
95     vlc_url_t url;
96     vlc_UrlParse( &url, in, '?' );
97     CHECK( url.psz_protocol, protocol );
98     CHECK( url.psz_username, user );
99     CHECK( url.psz_password, pass );
100     CHECK( url.psz_host, host );
101     CHECK( url.psz_path, path );
102     assert( url.i_port == i_port );
103     CHECK( url.psz_option, option );
104
105     vlc_UrlClean( &url );
106
107 #undef CHECK
108 }
109
110 int main (void)
111 {
112     int val;
113
114     (void)setvbuf (stdout, NULL, _IONBF, 0);
115     test_decode ("this_should_not_be_modified_1234",
116                  "this_should_not_be_modified_1234");
117
118     test_decode ("This%20should%20be%20modified%201234!",
119                  "This should be modified 1234!");
120
121     test_decode ("%7E", "~");
122
123     /* tests with invalid input */
124     test_decode ("%", NULL);
125     test_decode ("%2", NULL);
126     test_decode ("%0000", "");
127
128     /* Non-ASCII tests */
129     test_decode ("T%C3%a9l%c3%A9vision %e2%82%Ac", "Télévision €");
130     test_decode ("T%E9l%E9vision", "T\xe9l\xe9vision");
131
132     /* Base 64 tests */
133     test_b64 ("", "");
134     test_b64 ("f", "Zg==");
135     test_b64 ("fo", "Zm8=");
136     test_b64 ("foo", "Zm9v");
137     test_b64 ("foob", "Zm9vYg==");
138     test_b64 ("fooba", "Zm9vYmE=");
139     test_b64 ("foobar", "Zm9vYmFy");
140
141     /* Path test */
142     test_path ("/", "file:///");
143     test_path ("/home/john/", "file:///home/john/");
144     test_path ("/home/john//too///many//slashes",
145                "file:///home/john//too///many//slashes");
146     test_path ("/home/john/music.ogg", "file:///home/john/music.ogg");
147
148     /*int fd = open (".", O_RDONLY);
149     assert (fd != -1);*/
150     val = chdir ("/tmp");
151     assert (val != -1);
152
153     char buf[256];
154     char * tmpdir;
155     tmpdir = getcwd(buf, sizeof(buf)/sizeof(*buf));
156     assert (tmpdir);
157
158     test_current_directory_path ("movie.ogg", tmpdir, "movie.ogg");
159     test_current_directory_path (".", tmpdir, ".");
160     test_current_directory_path ("", tmpdir, "");
161
162     /*val = fchdir (fd);
163     assert (val != -1);*/
164
165     /* URI to path tests */
166 #define test( a, b ) test (make_path, a, b)
167     test ("mailto:john@example.com", NULL);
168     test ("http://www.example.com/file.html#ref", NULL);
169     test ("file://", NULL);
170     test ("file:///", "/");
171     test ("file://localhost/home/john/music%2Eogg", "/home/john/music.ogg");
172     test ("file://localhost/home/john/text#ref", "/home/john/text");
173     test ("fd://0foobar", NULL);
174     test ("fd://0#ref", "/dev/stdin");
175     test ("fd://1", "/dev/stdout");
176     test ("fd://12345", "/dev/fd/12345");
177 #undef test
178
179     test_url_parse("http://test.com", "http", NULL, NULL, "test.com", 0, NULL, NULL);
180     test_url_parse("http://test.com/", "http", NULL, NULL, "test.com", 0, "/", NULL);
181     test_url_parse("protocol://john:doe@1.2.3.4:567", "protocol", "john", "doe", "1.2.3.4", 567, NULL, NULL);
182     test_url_parse("http://a.b/?opt=val", "http", NULL, NULL, "a.b", 0, "/", "opt=val");
183     test_url_parse("p://u:p@host:123/a/b/c?o=v", "p", "u", "p", "host", 123, "/a/b/c", "o=v");
184
185     return 0;
186 }