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