librazor/test-pfu.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jul 14 11:43:16 2016 +0100 (2016-07-14)
changeset 492 644b648173b0
parent 475 008c75a5e08d
permissions -rw-r--r--
Release 0.6.3.106
ali@459
     1
/*
ali@475
     2
 * Copyright (C) 2014, 2016  J. Ali Harlow <ali@juiblex.co.uk>
ali@459
     3
 *
ali@459
     4
 * This program is free software; you can redistribute it and/or modify
ali@459
     5
 * it under the terms of the GNU General Public License as published by
ali@459
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@459
     7
 * (at your option) any later version.
ali@459
     8
 *
ali@459
     9
 * This program is distributed in the hope that it will be useful,
ali@459
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@459
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@459
    12
 * GNU General Public License for more details.
ali@459
    13
 *
ali@459
    14
 * You should have received a copy of the GNU General Public License along
ali@459
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@459
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@459
    17
 */
ali@459
    18
ali@459
    19
#include "config.h"
ali@459
    20
ali@459
    21
#include <stdlib.h>
ali@459
    22
#include <stdio.h>
ali@459
    23
#include <string.h>
ali@459
    24
#ifdef MSWIN_API
ali@459
    25
#include <windows.h>
ali@459
    26
#endif
ali@459
    27
#include "razor.h"
ali@491
    28
#include "razor-internal.h"
ali@459
    29
ali@475
    30
#ifdef MSWIN_API
ali@475
    31
static int is_ascii_letter(char c)
ali@459
    32
{
ali@459
    33
	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
ali@459
    34
}
ali@459
    35
ali@475
    36
static int is_slash(char c)
ali@459
    37
{
ali@459
    38
	return c == '/' || c == '\\';
ali@459
    39
}
ali@459
    40
ali@475
    41
static const char *mswin_path(const char *path)
ali@459
    42
{
ali@459
    43
	if (path == NULL)
ali@459
    44
		return NULL;
ali@459
    45
ali@459
    46
	if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' &&
ali@459
    47
	    path[3] == '/')
ali@459
    48
		path++;
ali@459
    49
ali@459
    50
	return path;
ali@459
    51
}
ali@475
    52
#endif
ali@459
    53
ali@475
    54
static int path_cmp(const char *p1, const char *p2)
ali@459
    55
{
ali@459
    56
#ifdef MSWIN_API
ali@459
    57
	while(*p1 && *p2) {
ali@459
    58
		if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) {
ali@459
    59
			p1++;
ali@459
    60
			p2++;
ali@459
    61
		} else
ali@459
    62
			break;
ali@459
    63
	}
ali@459
    64
ali@459
    65
	return *p1 || *p2;
ali@459
    66
#else
ali@459
    67
	return strcmp(p1, p2);
ali@459
    68
#endif
ali@459
    69
}
ali@459
    70
ali@475
    71
static int test_pfu(const char *uri, const char *path)
ali@459
    72
{
ali@459
    73
	char *s;
ali@459
    74
	int r;
ali@475
    75
	struct razor_error *error = NULL;
ali@459
    76
ali@475
    77
	s = razor_path_from_uri(uri, &error);
ali@459
    78
ali@459
    79
#ifdef MSWIN_API
ali@459
    80
	path = mswin_path(path);
ali@459
    81
#endif
ali@459
    82
ali@459
    83
	if (s && path)
ali@459
    84
		r = path_cmp(s, path);
ali@459
    85
	else
ali@459
    86
		r = (s != path);
ali@459
    87
ali@459
    88
	if (r) {
ali@475
    89
		fprintf(stderr, "Fail: razor_path_from_uri(\"%s\")", uri);
ali@459
    90
		if (s)
ali@459
    91
			fprintf(stderr, " returns \"%s\", expected", s);
ali@459
    92
		else
ali@475
    93
			fprintf(stderr, " fails (%s), expected",
ali@475
    94
				razor_error_get_msg(error));
ali@459
    95
		if (path)
ali@459
    96
			fprintf(stderr, " \"%s\"\n", path);
ali@459
    97
		else
ali@475
    98
			fprintf(stderr, " failure\n");
ali@459
    99
	}
ali@459
   100
ali@459
   101
	free(s);
ali@475
   102
	if (error)
ali@475
   103
		razor_error_free(error);
ali@459
   104
ali@459
   105
	return r;
ali@459
   106
}
ali@459
   107
ali@491
   108
static int test_abs(const char *abspath, const char *path)
ali@491
   109
{
ali@491
   110
	char *s;
ali@491
   111
	int r;
ali@491
   112
ali@491
   113
	s = razor_abspath(path);
ali@491
   114
ali@491
   115
	if (s && abspath)
ali@491
   116
		r = strcmp(s, abspath);
ali@491
   117
	else
ali@491
   118
		r = (s != abspath);
ali@491
   119
ali@491
   120
	if (r) {
ali@491
   121
		fprintf(stderr, "Fail: razor_abspath(\"%s\")", path);
ali@491
   122
		if (s)
ali@491
   123
			fprintf(stderr, " returns \"%s\", expected", s);
ali@491
   124
		else
ali@491
   125
			fprintf(stderr, " fails, expected");
ali@491
   126
		if (abspath)
ali@491
   127
			fprintf(stderr, " \"%s\"\n", abspath);
ali@491
   128
		else
ali@491
   129
			fprintf(stderr, " failure\n");
ali@491
   130
	}
ali@491
   131
ali@491
   132
	free(s);
ali@491
   133
ali@491
   134
	return r;
ali@491
   135
}
ali@491
   136
ali@491
   137
static int test_ptu(const char *uri, const char *path)
ali@491
   138
{
ali@491
   139
	char *s;
ali@491
   140
	int r;
ali@491
   141
ali@491
   142
	s = razor_path_to_uri(path);
ali@491
   143
ali@491
   144
	if (s && uri)
ali@491
   145
		r = strcmp(s, uri);
ali@491
   146
	else
ali@491
   147
		r = (s != uri);
ali@491
   148
ali@491
   149
	if (r) {
ali@491
   150
		fprintf(stderr, "Fail: razor_path_to_uri(\"%s\")", path);
ali@491
   151
		if (s)
ali@491
   152
			fprintf(stderr, " returns \"%s\", expected", s);
ali@491
   153
		else
ali@491
   154
			fprintf(stderr, " fails, expected");
ali@491
   155
		if (uri)
ali@491
   156
			fprintf(stderr, " \"%s\"\n", uri);
ali@491
   157
		else
ali@491
   158
			fprintf(stderr, " failure\n");
ali@491
   159
	}
ali@491
   160
ali@491
   161
	free(s);
ali@491
   162
ali@491
   163
	return r;
ali@491
   164
}
ali@491
   165
ali@459
   166
#ifdef MSWIN_API
ali@459
   167
UINT saved_cp;
ali@459
   168
ali@475
   169
static void cleanup_on_exit(void)
ali@459
   170
{
ali@459
   171
    SetConsoleOutputCP(saved_cp);
ali@459
   172
}
ali@459
   173
#endif
ali@459
   174
ali@459
   175
int main(int argc, char *argv[])
ali@459
   176
{
ali@459
   177
	int r = 0;
ali@491
   178
	const char *tmpdir;
ali@491
   179
	char *tempdir, *s;
ali@491
   180
ali@491
   181
	tmpdir = getenv("TMPDIR");
ali@491
   182
	if (!tmpdir || !*tmpdir)
ali@491
   183
		tmpdir = "/tmp";
ali@491
   184
ali@491
   185
	tempdir = razor_concat(tmpdir, "/test-pfu-XXXXXX", NULL);
ali@491
   186
ali@491
   187
	if (!mkdtemp(tempdir) || chdir(tempdir) < 0) {
ali@491
   188
		perror(tempdir);
ali@491
   189
		free(tempdir);
ali@491
   190
		exit(1);
ali@491
   191
	}
ali@459
   192
ali@459
   193
#ifdef MSWIN_API
ali@459
   194
	atexit(cleanup_on_exit);
ali@459
   195
	saved_cp = GetConsoleOutputCP();
ali@459
   196
	SetConsoleOutputCP(CP_UTF8);
ali@459
   197
#endif
ali@459
   198
ali@459
   199
	r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab");
ali@459
   200
	r |= test_pfu("file:///etc/fstab", "/etc/fstab");
ali@459
   201
	r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi",
ali@459
   202
		      "/c:/WINDOWS/clock.avi");
ali@459
   203
	r |= test_pfu("file:///c:/WINDOWS/clock.avi",
ali@459
   204
		      "/c:/WINDOWS/clock.avi");
ali@459
   205
	r |= test_pfu("file:///path/to/the%20file.txt",
ali@459
   206
		      "/path/to/the file.txt");
ali@459
   207
	r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien");
ali@459
   208
	r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš");
ali@459
   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",
ali@459
   210
		      "/var/log/22 สิงหาคม 2014");
ali@459
   211
ali@491
   212
	s = razor_concat(tempdir, "/file.txt", NULL);
ali@491
   213
	r |= test_abs(s, "file.txt");
ali@491
   214
	free(s);
ali@491
   215
ali@491
   216
	s = razor_concat(tempdir, "/dir/../file.txt", NULL);
ali@491
   217
	r |= test_abs(s, "dir/../file.txt");
ali@491
   218
	free(s);
ali@491
   219
ali@491
   220
	s = razor_concat(tempdir, "/../file.txt", NULL);
ali@491
   221
	r |= test_abs(s, "../file.txt");
ali@491
   222
	free(s);
ali@491
   223
ali@491
   224
	s = razor_concat(tempdir, "/dir/../../file.txt", NULL);
ali@491
   225
	r |= test_abs(s, "dir/../../file.txt");
ali@491
   226
	free(s);
ali@491
   227
ali@491
   228
	r |= test_ptu("file:file.txt", "file.txt");
ali@491
   229
	r |= test_ptu("file:file.txt", "dir/../file.txt");
ali@491
   230
ali@491
   231
	s = razor_concat("file:", tmpdir, "/file.txt", NULL);
ali@491
   232
	r |= test_ptu(s, "../file.txt");
ali@491
   233
	r |= test_ptu(s, "dir/../../file.txt");
ali@491
   234
	free(s);
ali@491
   235
ali@491
   236
	free(tempdir);
ali@491
   237
ali@459
   238
	exit(r ? 1 : 0);
ali@459
   239
}