1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/librazor/test-pfu.c Thu Nov 13 10:44:53 2014 +0000
1.3 @@ -0,0 +1,134 @@
1.4 +/*
1.5 + * Copyright (C) 2014 J. Ali Harlow <ali@juiblex.co.uk>
1.6 + *
1.7 + * This program is free software; you can redistribute it and/or modify
1.8 + * it under the terms of the GNU General Public License as published by
1.9 + * the Free Software Foundation; either version 2 of the License, or
1.10 + * (at your option) any later version.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License along
1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.20 + */
1.21 +
1.22 +#include "config.h"
1.23 +
1.24 +#include <stdlib.h>
1.25 +#include <stdio.h>
1.26 +#include <string.h>
1.27 +#ifdef MSWIN_API
1.28 +#include <windows.h>
1.29 +#endif
1.30 +#include "razor.h"
1.31 +
1.32 +int is_ascii_letter(char c)
1.33 +{
1.34 + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
1.35 +}
1.36 +
1.37 +int is_slash(char c)
1.38 +{
1.39 + return c == '/' || c == '\\';
1.40 +}
1.41 +
1.42 +const char *mswin_path(const char *path)
1.43 +{
1.44 + if (path == NULL)
1.45 + return NULL;
1.46 +
1.47 + if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' &&
1.48 + path[3] == '/')
1.49 + path++;
1.50 +
1.51 + return path;
1.52 +}
1.53 +
1.54 +int path_cmp(const char *p1, const char *p2)
1.55 +{
1.56 +#ifdef MSWIN_API
1.57 + while(*p1 && *p2) {
1.58 + if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) {
1.59 + p1++;
1.60 + p2++;
1.61 + } else
1.62 + break;
1.63 + }
1.64 +
1.65 + return *p1 || *p2;
1.66 +#else
1.67 + return strcmp(p1, p2);
1.68 +#endif
1.69 +}
1.70 +
1.71 +int test_pfu(const char *url, const char *path)
1.72 +{
1.73 + char *s;
1.74 + int r;
1.75 +
1.76 + s = razor_path_from_url(url);
1.77 +
1.78 +#ifdef MSWIN_API
1.79 + path = mswin_path(path);
1.80 +#endif
1.81 +
1.82 + if (s && path)
1.83 + r = path_cmp(s, path);
1.84 + else
1.85 + r = (s != path);
1.86 +
1.87 + if (r) {
1.88 + fprintf(stderr, "Fail: razor_path_from_url(\"%s\")", url);
1.89 + if (s)
1.90 + fprintf(stderr, " returns \"%s\", expected", s);
1.91 + else
1.92 + fprintf(stderr, " returns NULL, expected");
1.93 + if (path)
1.94 + fprintf(stderr, " \"%s\"\n", path);
1.95 + else
1.96 + fprintf(stderr, " NULL\n");
1.97 + }
1.98 +
1.99 + free(s);
1.100 +
1.101 + return r;
1.102 +}
1.103 +
1.104 +#ifdef MSWIN_API
1.105 +UINT saved_cp;
1.106 +
1.107 +void cleanup_on_exit(void)
1.108 +{
1.109 + SetConsoleOutputCP(saved_cp);
1.110 +}
1.111 +#endif
1.112 +
1.113 +int main(int argc, char *argv[])
1.114 +{
1.115 + int r = 0;
1.116 +
1.117 +#ifdef MSWIN_API
1.118 + atexit(cleanup_on_exit);
1.119 + saved_cp = GetConsoleOutputCP();
1.120 + SetConsoleOutputCP(CP_UTF8);
1.121 +#endif
1.122 +
1.123 + r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab");
1.124 + r |= test_pfu("file:///etc/fstab", "/etc/fstab");
1.125 + r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi",
1.126 + "/c:/WINDOWS/clock.avi");
1.127 + r |= test_pfu("file:///c:/WINDOWS/clock.avi",
1.128 + "/c:/WINDOWS/clock.avi");
1.129 + r |= test_pfu("file:///path/to/the%20file.txt",
1.130 + "/path/to/the file.txt");
1.131 + r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien");
1.132 + r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš");
1.133 + 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",
1.134 + "/var/log/22 สิงหาคม 2014");
1.135 +
1.136 + exit(r ? 1 : 0);
1.137 +}