librazor/test-pfu.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jul 08 15:54:09 2016 +0100 (2016-07-08)
changeset 481 c3722fff46c7
parent 459 5576a85e174a
child 491 b18e0bf48a91
permissions -rw-r--r--
Windows tweaks for archives
     1 /*
     2  * Copyright (C) 2014, 2016  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 #ifdef MSWIN_API
    30 static int is_ascii_letter(char c)
    31 {
    32 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    33 }
    34 
    35 static int is_slash(char c)
    36 {
    37 	return c == '/' || c == '\\';
    38 }
    39 
    40 static const char *mswin_path(const char *path)
    41 {
    42 	if (path == NULL)
    43 		return NULL;
    44 
    45 	if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' &&
    46 	    path[3] == '/')
    47 		path++;
    48 
    49 	return path;
    50 }
    51 #endif
    52 
    53 static int path_cmp(const char *p1, const char *p2)
    54 {
    55 #ifdef MSWIN_API
    56 	while(*p1 && *p2) {
    57 		if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) {
    58 			p1++;
    59 			p2++;
    60 		} else
    61 			break;
    62 	}
    63 
    64 	return *p1 || *p2;
    65 #else
    66 	return strcmp(p1, p2);
    67 #endif
    68 }
    69 
    70 static int test_pfu(const char *uri, const char *path)
    71 {
    72 	char *s;
    73 	int r;
    74 	struct razor_error *error = NULL;
    75 
    76 	s = razor_path_from_uri(uri, &error);
    77 
    78 #ifdef MSWIN_API
    79 	path = mswin_path(path);
    80 #endif
    81 
    82 	if (s && path)
    83 		r = path_cmp(s, path);
    84 	else
    85 		r = (s != path);
    86 
    87 	if (r) {
    88 		fprintf(stderr, "Fail: razor_path_from_uri(\"%s\")", uri);
    89 		if (s)
    90 			fprintf(stderr, " returns \"%s\", expected", s);
    91 		else
    92 			fprintf(stderr, " fails (%s), expected",
    93 				razor_error_get_msg(error));
    94 		if (path)
    95 			fprintf(stderr, " \"%s\"\n", path);
    96 		else
    97 			fprintf(stderr, " failure\n");
    98 	}
    99 
   100 	free(s);
   101 	if (error)
   102 		razor_error_free(error);
   103 
   104 	return r;
   105 }
   106 
   107 #ifdef MSWIN_API
   108 UINT saved_cp;
   109 
   110 static void cleanup_on_exit(void)
   111 {
   112     SetConsoleOutputCP(saved_cp);
   113 }
   114 #endif
   115 
   116 int main(int argc, char *argv[])
   117 {
   118 	int r = 0;
   119 
   120 #ifdef MSWIN_API
   121 	atexit(cleanup_on_exit);
   122 	saved_cp = GetConsoleOutputCP();
   123 	SetConsoleOutputCP(CP_UTF8);
   124 #endif
   125 
   126 	r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab");
   127 	r |= test_pfu("file:///etc/fstab", "/etc/fstab");
   128 	r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi",
   129 		      "/c:/WINDOWS/clock.avi");
   130 	r |= test_pfu("file:///c:/WINDOWS/clock.avi",
   131 		      "/c:/WINDOWS/clock.avi");
   132 	r |= test_pfu("file:///path/to/the%20file.txt",
   133 		      "/path/to/the file.txt");
   134 	r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien");
   135 	r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš");
   136 	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",
   137 		      "/var/log/22 สิงหาคม 2014");
   138 
   139 	exit(r ? 1 : 0);
   140 }