]> git.sesse.net Git - vlc/blob - src/test/url.c
Allow tested functions to return NULL
[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
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 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 General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, 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 inline void test_path (const char *in, const char *out)
72 {
73     test (make_URI, in, out);
74 }
75
76 static inline void test_current_directory_path (const char *in, const char *cwd, const char *out)
77 {
78     char * expected_result = NULL;
79     int val = asprintf(&expected_result, "file://%s/%s", cwd, out);
80     assert (val != -1);
81     
82     test (make_URI, in, expected_result);
83 }
84
85 int main (void)
86 {
87     int val;
88
89     (void)setvbuf (stdout, NULL, _IONBF, 0);
90     test_decode ("this_should_not_be_modified_1234",
91                  "this_should_not_be_modified_1234");
92
93     test_decode ("This+should+be+modified+1234!",
94                  "This should be modified 1234!");
95
96     test_decode ("This%20should%20be%20modified%201234!",
97                  "This should be modified 1234!");
98
99     test_decode ("%7E", "~");
100
101     /* tests with invalid input */
102     test_decode ("%", "%");
103     test_decode ("%2", "%2");
104     test_decode ("%0000", "");
105
106     /* UTF-8 tests */
107     test_decode ("T%C3%a9l%c3%A9vision+%e2%82%Ac", "Télévision €");
108     test_decode ("T%E9l%E9vision", "T?l?vision");
109     test_decode ("%C1%94%C3%a9l%c3%A9vision", "??élévision"); /* overlong */
110
111     /* Base 64 tests */
112     test_b64 ("", "");
113     test_b64 ("f", "Zg==");
114     test_b64 ("fo", "Zm8=");
115     test_b64 ("foo", "Zm9v");
116     test_b64 ("foob", "Zm9vYg==");
117     test_b64 ("fooba", "Zm9vYmE=");
118     test_b64 ("foobar", "Zm9vYmFy");
119
120     /* Path test */
121     test_path ("file:///", "file:///");
122     test_path ("http://www.example.com/%7Ejohn/",
123                "http://www.example.com/%7Ejohn/");
124     test_path ("/", "file:///");
125     test_path ("/home/john/", "file:///home/john/");
126     test_path ("/home/john/music.ogg", "file:///home/john/music.ogg");
127     test_path ("\\\\server/pub/music.ogg", "smb://server/pub/music.ogg");
128     test_path ("\\\\server\\pub\\music.ogg", "smb://server/pub/music.ogg");
129
130     /*int fd = open (".", O_RDONLY);
131     assert (fd != -1);*/
132     val = chdir ("/tmp");
133     assert (val != -1);
134
135     char buf[256];
136     char * tmpdir;
137     tmpdir = getcwd(buf, sizeof(buf)/sizeof(*buf));
138     assert (tmpdir);
139
140     test_current_directory_path ("movie.ogg", tmpdir, "movie.ogg");
141     test_current_directory_path (".", tmpdir, ".");
142     test_current_directory_path ("", tmpdir, "");
143
144     /*val = fchdir (fd);
145     assert (val != -1);*/
146
147     return 0;
148 }