/* * Copyright (C) 2014, 2016 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #include #include #include #ifdef MSWIN_API #include #endif #include "razor.h" #include "razor-internal.h" #ifdef MSWIN_API static int is_ascii_letter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } static int is_slash(char c) { return c == '/' || c == '\\'; } static const char *mswin_path(const char *path) { if (path == NULL) return NULL; if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' && path[3] == '/') path++; return path; } #endif static int path_cmp(const char *p1, const char *p2) { #ifdef MSWIN_API while(*p1 && *p2) { if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) { p1++; p2++; } else break; } return *p1 || *p2; #else return strcmp(p1, p2); #endif } static int test_pfu(const char *uri, const char *path) { char *s; int r; struct razor_error *error = NULL; s = razor_path_from_uri(uri, &error); #ifdef MSWIN_API path = mswin_path(path); #endif if (s && path) r = path_cmp(s, path); else r = (s != path); if (r) { fprintf(stderr, "Fail: razor_path_from_uri(\"%s\")", uri); if (s) fprintf(stderr, " returns \"%s\", expected", s); else fprintf(stderr, " fails (%s), expected", razor_error_get_msg(error)); if (path) fprintf(stderr, " \"%s\"\n", path); else fprintf(stderr, " failure\n"); } free(s); if (error) razor_error_free(error); return r; } static int test_abs(const char *abspath, const char *path) { char *s; int r; s = razor_abspath(path); if (s && abspath) r = strcmp(s, abspath); else r = (s != abspath); if (r) { fprintf(stderr, "Fail: razor_abspath(\"%s\")", path); if (s) fprintf(stderr, " returns \"%s\", expected", s); else fprintf(stderr, " fails, expected"); if (abspath) fprintf(stderr, " \"%s\"\n", abspath); else fprintf(stderr, " failure\n"); } free(s); return r; } static int test_ptu(const char *uri, const char *path) { char *s; int r; s = razor_path_to_uri(path); if (s && uri) r = strcmp(s, uri); else r = (s != uri); if (r) { fprintf(stderr, "Fail: razor_path_to_uri(\"%s\")", path); if (s) fprintf(stderr, " returns \"%s\", expected", s); else fprintf(stderr, " fails, expected"); if (uri) fprintf(stderr, " \"%s\"\n", uri); else fprintf(stderr, " failure\n"); } free(s); return r; } #ifdef MSWIN_API UINT saved_cp; static void cleanup_on_exit(void) { SetConsoleOutputCP(saved_cp); } #endif int main(int argc, char *argv[]) { int r = 0; const char *tmpdir; char *tempdir, *s; tmpdir = getenv("TMPDIR"); if (!tmpdir || !*tmpdir) tmpdir = "/tmp"; tempdir = razor_concat(tmpdir, "/test-pfu-XXXXXX", NULL); if (!mkdtemp(tempdir) || chdir(tempdir) < 0) { perror(tempdir); free(tempdir); exit(1); } #ifdef MSWIN_API atexit(cleanup_on_exit); saved_cp = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); #endif r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab"); r |= test_pfu("file:///etc/fstab", "/etc/fstab"); r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi", "/c:/WINDOWS/clock.avi"); r |= test_pfu("file:///c:/WINDOWS/clock.avi", "/c:/WINDOWS/clock.avi"); r |= test_pfu("file:///path/to/the%20file.txt", "/path/to/the file.txt"); r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien"); r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš"); 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", "/var/log/22 สิงหาคม 2014"); s = razor_concat(tempdir, "/file.txt", NULL); r |= test_abs(s, "file.txt"); free(s); s = razor_concat(tempdir, "/dir/../file.txt", NULL); r |= test_abs(s, "dir/../file.txt"); free(s); s = razor_concat(tempdir, "/../file.txt", NULL); r |= test_abs(s, "../file.txt"); free(s); s = razor_concat(tempdir, "/dir/../../file.txt", NULL); r |= test_abs(s, "dir/../../file.txt"); free(s); r |= test_ptu("file:file.txt", "file.txt"); r |= test_ptu("file:file.txt", "dir/../file.txt"); s = razor_concat("file:", tmpdir, "/file.txt", NULL); r |= test_ptu(s, "../file.txt"); r |= test_ptu(s, "dir/../../file.txt"); free(s); free(tempdir); exit(r ? 1 : 0); }