2 * Copyright (C) 2014, 2016 J. Ali Harlow <ali@juiblex.co.uk>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "razor-internal.h"
31 static int is_ascii_letter(char c)
33 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
36 static int is_slash(char c)
38 return c == '/' || c == '\\';
41 static const char *mswin_path(const char *path)
46 if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' &&
54 static int path_cmp(const char *p1, const char *p2)
58 if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) {
67 return strcmp(p1, p2);
71 static int test_pfu(const char *uri, const char *path)
75 struct razor_error *error = NULL;
77 s = razor_path_from_uri(uri, &error);
80 path = mswin_path(path);
84 r = path_cmp(s, path);
89 fprintf(stderr, "Fail: razor_path_from_uri(\"%s\")", uri);
91 fprintf(stderr, " returns \"%s\", expected", s);
93 fprintf(stderr, " fails (%s), expected",
94 razor_error_get_msg(error));
96 fprintf(stderr, " \"%s\"\n", path);
98 fprintf(stderr, " failure\n");
103 razor_error_free(error);
108 static int test_abs(const char *abspath, const char *path)
113 s = razor_abspath(path);
116 r = strcmp(s, abspath);
121 fprintf(stderr, "Fail: razor_abspath(\"%s\")", path);
123 fprintf(stderr, " returns \"%s\", expected", s);
125 fprintf(stderr, " fails, expected");
127 fprintf(stderr, " \"%s\"\n", abspath);
129 fprintf(stderr, " failure\n");
137 static int test_ptu(const char *uri, const char *path)
142 s = razor_path_to_uri(path);
150 fprintf(stderr, "Fail: razor_path_to_uri(\"%s\")", path);
152 fprintf(stderr, " returns \"%s\", expected", s);
154 fprintf(stderr, " fails, expected");
156 fprintf(stderr, " \"%s\"\n", uri);
158 fprintf(stderr, " failure\n");
169 static void cleanup_on_exit(void)
171 SetConsoleOutputCP(saved_cp);
175 int main(int argc, char *argv[])
181 tmpdir = getenv("TMPDIR");
182 if (!tmpdir || !*tmpdir)
185 tempdir = razor_concat(tmpdir, "/test-pfu-XXXXXX", NULL);
187 if (!mkdtemp(tempdir) || chdir(tempdir) < 0) {
194 atexit(cleanup_on_exit);
195 saved_cp = GetConsoleOutputCP();
196 SetConsoleOutputCP(CP_UTF8);
199 r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab");
200 r |= test_pfu("file:///etc/fstab", "/etc/fstab");
201 r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi",
202 "/c:/WINDOWS/clock.avi");
203 r |= test_pfu("file:///c:/WINDOWS/clock.avi",
204 "/c:/WINDOWS/clock.avi");
205 r |= test_pfu("file:///path/to/the%20file.txt",
206 "/path/to/the file.txt");
207 r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien");
208 r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš");
209 r |= test_pfu("file:///var/log/22%20%e0%b8%aa%e0%b8%b4%e0%b8%87%e0%b8%ab%e0%b8%b2%e0%b8%84%e0%b8%a1%202014",
210 "/var/log/22 สิงหาคม 2014");
212 s = razor_concat(tempdir, "/file.txt", NULL);
213 r |= test_abs(s, "file.txt");
216 s = razor_concat(tempdir, "/dir/../file.txt", NULL);
217 r |= test_abs(s, "dir/../file.txt");
220 s = razor_concat(tempdir, "/../file.txt", NULL);
221 r |= test_abs(s, "../file.txt");
224 s = razor_concat(tempdir, "/dir/../../file.txt", NULL);
225 r |= test_abs(s, "dir/../../file.txt");
228 r |= test_ptu("file:file.txt", "file.txt");
229 r |= test_ptu("file:file.txt", "dir/../file.txt");
231 s = razor_concat("file:", tmpdir, "/file.txt", NULL);
232 r |= test_ptu(s, "../file.txt");
233 r |= test_ptu(s, "dir/../../file.txt");