librazor/test-pfu.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jun 09 17:37:09 2016 +0100 (2016-06-09)
changeset 471 a3e5e3eaf224
child 475 008c75a5e08d
permissions -rw-r--r--
Improve an error message
     1 /*
     2  * Copyright (C) 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #include <stdlib.h>
    22 #include <stdio.h>
    23 #include <string.h>
    24 #ifdef MSWIN_API
    25 #include <windows.h>
    26 #endif
    27 #include "razor.h"
    28 
    29 int is_ascii_letter(char c)
    30 {
    31 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    32 }
    33 
    34 int is_slash(char c)
    35 {
    36 	return c == '/' || c == '\\';
    37 }
    38 
    39 const char *mswin_path(const char *path)
    40 {
    41 	if (path == NULL)
    42 		return NULL;
    43 
    44 	if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' &&
    45 	    path[3] == '/')
    46 		path++;
    47 
    48 	return path;
    49 }
    50 
    51 int path_cmp(const char *p1, const char *p2)
    52 {
    53 #ifdef MSWIN_API
    54 	while(*p1 && *p2) {
    55 		if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) {
    56 			p1++;
    57 			p2++;
    58 		} else
    59 			break;
    60 	}
    61 
    62 	return *p1 || *p2;
    63 #else
    64 	return strcmp(p1, p2);
    65 #endif
    66 }
    67 
    68 int test_pfu(const char *url, const char *path)
    69 {
    70 	char *s;
    71 	int r;
    72 
    73 	s = razor_path_from_url(url);
    74 
    75 #ifdef MSWIN_API
    76 	path = mswin_path(path);
    77 #endif
    78 
    79 	if (s && path)
    80 		r = path_cmp(s, path);
    81 	else
    82 		r = (s != path);
    83 
    84 	if (r) {
    85 		fprintf(stderr, "Fail: razor_path_from_url(\"%s\")", url);
    86 		if (s)
    87 			fprintf(stderr, " returns \"%s\", expected", s);
    88 		else
    89 			fprintf(stderr, " returns NULL, expected");
    90 		if (path)
    91 			fprintf(stderr, " \"%s\"\n", path);
    92 		else
    93 			fprintf(stderr, " NULL\n");
    94 	}
    95 
    96 	free(s);
    97 
    98 	return r;
    99 }
   100 
   101 #ifdef MSWIN_API
   102 UINT saved_cp;
   103 
   104 void cleanup_on_exit(void)
   105 {
   106     SetConsoleOutputCP(saved_cp);
   107 }
   108 #endif
   109 
   110 int main(int argc, char *argv[])
   111 {
   112 	int r = 0;
   113 
   114 #ifdef MSWIN_API
   115 	atexit(cleanup_on_exit);
   116 	saved_cp = GetConsoleOutputCP();
   117 	SetConsoleOutputCP(CP_UTF8);
   118 #endif
   119 
   120 	r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab");
   121 	r |= test_pfu("file:///etc/fstab", "/etc/fstab");
   122 	r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi",
   123 		      "/c:/WINDOWS/clock.avi");
   124 	r |= test_pfu("file:///c:/WINDOWS/clock.avi",
   125 		      "/c:/WINDOWS/clock.avi");
   126 	r |= test_pfu("file:///path/to/the%20file.txt",
   127 		      "/path/to/the file.txt");
   128 	r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien");
   129 	r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš");
   130 	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",
   131 		      "/var/log/22 สิงหาคม 2014");
   132 
   133 	exit(r ? 1 : 0);
   134 }