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