diff -r 000000000000 -r 4b7270fd3201 librazor/test-pfu.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/librazor/test-pfu.c Thu Apr 14 11:59:56 2016 +0100 @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2014 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" + +int is_ascii_letter(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +int is_slash(char c) +{ + return c == '/' || c == '\\'; +} + +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; +} + +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 +} + +int test_pfu(const char *url, const char *path) +{ + char *s; + int r; + + s = razor_path_from_url(url); + +#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_url(\"%s\")", url); + if (s) + fprintf(stderr, " returns \"%s\", expected", s); + else + fprintf(stderr, " returns NULL, expected"); + if (path) + fprintf(stderr, " \"%s\"\n", path); + else + fprintf(stderr, " NULL\n"); + } + + free(s); + + return r; +} + +#ifdef MSWIN_API +UINT saved_cp; + +void cleanup_on_exit(void) +{ + SetConsoleOutputCP(saved_cp); +} +#endif + +int main(int argc, char *argv[]) +{ + int r = 0; + +#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"); + + exit(r ? 1 : 0); +}