librazor/test-pfu.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jul 14 12:49:48 2016 +0100 (2016-07-14)
changeset 491 b18e0bf48a91
parent 475 008c75a5e08d
permissions -rw-r--r--
razor_path_to_uri() should cope with relative paths better
     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 #include "razor-internal.h"
    29 
    30 #ifdef MSWIN_API
    31 static int is_ascii_letter(char c)
    32 {
    33 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    34 }
    35 
    36 static int is_slash(char c)
    37 {
    38 	return c == '/' || c == '\\';
    39 }
    40 
    41 static const char *mswin_path(const char *path)
    42 {
    43 	if (path == NULL)
    44 		return NULL;
    45 
    46 	if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' &&
    47 	    path[3] == '/')
    48 		path++;
    49 
    50 	return path;
    51 }
    52 #endif
    53 
    54 static int path_cmp(const char *p1, const char *p2)
    55 {
    56 #ifdef MSWIN_API
    57 	while(*p1 && *p2) {
    58 		if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) {
    59 			p1++;
    60 			p2++;
    61 		} else
    62 			break;
    63 	}
    64 
    65 	return *p1 || *p2;
    66 #else
    67 	return strcmp(p1, p2);
    68 #endif
    69 }
    70 
    71 static int test_pfu(const char *uri, const char *path)
    72 {
    73 	char *s;
    74 	int r;
    75 	struct razor_error *error = NULL;
    76 
    77 	s = razor_path_from_uri(uri, &error);
    78 
    79 #ifdef MSWIN_API
    80 	path = mswin_path(path);
    81 #endif
    82 
    83 	if (s && path)
    84 		r = path_cmp(s, path);
    85 	else
    86 		r = (s != path);
    87 
    88 	if (r) {
    89 		fprintf(stderr, "Fail: razor_path_from_uri(\"%s\")", uri);
    90 		if (s)
    91 			fprintf(stderr, " returns \"%s\", expected", s);
    92 		else
    93 			fprintf(stderr, " fails (%s), expected",
    94 				razor_error_get_msg(error));
    95 		if (path)
    96 			fprintf(stderr, " \"%s\"\n", path);
    97 		else
    98 			fprintf(stderr, " failure\n");
    99 	}
   100 
   101 	free(s);
   102 	if (error)
   103 		razor_error_free(error);
   104 
   105 	return r;
   106 }
   107 
   108 static int test_abs(const char *abspath, const char *path)
   109 {
   110 	char *s;
   111 	int r;
   112 
   113 	s = razor_abspath(path);
   114 
   115 	if (s && abspath)
   116 		r = strcmp(s, abspath);
   117 	else
   118 		r = (s != abspath);
   119 
   120 	if (r) {
   121 		fprintf(stderr, "Fail: razor_abspath(\"%s\")", path);
   122 		if (s)
   123 			fprintf(stderr, " returns \"%s\", expected", s);
   124 		else
   125 			fprintf(stderr, " fails, expected");
   126 		if (abspath)
   127 			fprintf(stderr, " \"%s\"\n", abspath);
   128 		else
   129 			fprintf(stderr, " failure\n");
   130 	}
   131 
   132 	free(s);
   133 
   134 	return r;
   135 }
   136 
   137 static int test_ptu(const char *uri, const char *path)
   138 {
   139 	char *s;
   140 	int r;
   141 
   142 	s = razor_path_to_uri(path);
   143 
   144 	if (s && uri)
   145 		r = strcmp(s, uri);
   146 	else
   147 		r = (s != uri);
   148 
   149 	if (r) {
   150 		fprintf(stderr, "Fail: razor_path_to_uri(\"%s\")", path);
   151 		if (s)
   152 			fprintf(stderr, " returns \"%s\", expected", s);
   153 		else
   154 			fprintf(stderr, " fails, expected");
   155 		if (uri)
   156 			fprintf(stderr, " \"%s\"\n", uri);
   157 		else
   158 			fprintf(stderr, " failure\n");
   159 	}
   160 
   161 	free(s);
   162 
   163 	return r;
   164 }
   165 
   166 #ifdef MSWIN_API
   167 UINT saved_cp;
   168 
   169 static void cleanup_on_exit(void)
   170 {
   171     SetConsoleOutputCP(saved_cp);
   172 }
   173 #endif
   174 
   175 int main(int argc, char *argv[])
   176 {
   177 	int r = 0;
   178 	const char *tmpdir;
   179 	char *tempdir, *s;
   180 
   181 	tmpdir = getenv("TMPDIR");
   182 	if (!tmpdir || !*tmpdir)
   183 		tmpdir = "/tmp";
   184 
   185 	tempdir = razor_concat(tmpdir, "/test-pfu-XXXXXX", NULL);
   186 
   187 	if (!mkdtemp(tempdir) || chdir(tempdir) < 0) {
   188 		perror(tempdir);
   189 		free(tempdir);
   190 		exit(1);
   191 	}
   192 
   193 #ifdef MSWIN_API
   194 	atexit(cleanup_on_exit);
   195 	saved_cp = GetConsoleOutputCP();
   196 	SetConsoleOutputCP(CP_UTF8);
   197 #endif
   198 
   199 	r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab");
   200 	r |= test_pfu("file:///etc/fstab", "/etc/fstab");
   201 	r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi",
   202 		      "/c:/WINDOWS/clock.avi");
   203 	r |= test_pfu("file:///c:/WINDOWS/clock.avi",
   204 		      "/c:/WINDOWS/clock.avi");
   205 	r |= test_pfu("file:///path/to/the%20file.txt",
   206 		      "/path/to/the file.txt");
   207 	r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien");
   208 	r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš");
   209 	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",
   210 		      "/var/log/22 สิงหาคม 2014");
   211 
   212 	s = razor_concat(tempdir, "/file.txt", NULL);
   213 	r |= test_abs(s, "file.txt");
   214 	free(s);
   215 
   216 	s = razor_concat(tempdir, "/dir/../file.txt", NULL);
   217 	r |= test_abs(s, "dir/../file.txt");
   218 	free(s);
   219 
   220 	s = razor_concat(tempdir, "/../file.txt", NULL);
   221 	r |= test_abs(s, "../file.txt");
   222 	free(s);
   223 
   224 	s = razor_concat(tempdir, "/dir/../../file.txt", NULL);
   225 	r |= test_abs(s, "dir/../../file.txt");
   226 	free(s);
   227 
   228 	r |= test_ptu("file:file.txt", "file.txt");
   229 	r |= test_ptu("file:file.txt", "dir/../file.txt");
   230 
   231 	s = razor_concat("file:", tmpdir, "/file.txt", NULL);
   232 	r |= test_ptu(s, "../file.txt");
   233 	r |= test_ptu(s, "dir/../../file.txt");
   234 	free(s);
   235 
   236 	free(tempdir);
   237 
   238 	exit(r ? 1 : 0);
   239 }