1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include "config.h"
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char *filename;
char *hostname;
char *expected_result;
GConvertError expected_error; /* If failed */
} ToUriTest;
ToUriTest
to_uri_tests[] = {
{ "/etc", NULL, "file:///etc"},
{ "/etc", "", "file:///etc"},
{ "/etc", "otherhost", "file://otherhost/etc"},
#ifdef G_OS_WIN32
{ "/etc", "localhost", "file:///etc"},
{ "c:\\windows", NULL, "file:///c:/windows"},
{ "c:\\windows", "localhost", "file:///c:/windows"},
{ "c:\\windows", "otherhost", "file://otherhost/c:/windows"},
{ "\\\\server\\share\\dir", NULL, "file:////server/share/dir"},
{ "\\\\server\\share\\dir", "localhost", "file:////server/share/dir"},
#else
{ "/etc", "localhost", "file://localhost/etc"},
{ "c:\\windows", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, /* it's important to get this error on Unix */
{ "c:\\windows", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
{ "c:\\windows", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
#endif
{ "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
#ifndef G_PLATFORM_WIN32
{ "/etc/\xE5\xE4\xF6", NULL, "file:///etc/%E5%E4%F6" },
{ "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
#endif
{ "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/etc", "\xE5\xE4\xF6", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
{ "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
{ "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
{ "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
{ "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
{ "/0123456789", NULL, "file:///0123456789"},
{ "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
{ "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"},
{ "/-_.!~*'()", NULL, "file:///-_.!~*'()"},
#ifdef G_OS_WIN32
/* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
{ "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"},
#else
/* On Unix, '\\' is a normal character in the file name */
{ "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
#endif
{ "/;@+$,", NULL, "file:///%3B@+$,"},
/* This and some of the following are of course as such illegal file names on Windows,
* and would not occur in real life.
*/
{ "/:", NULL, "file:///:"},
{ "/?&=", NULL, "file:///%3F&="},
{ "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
{ "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
{ "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", "\xC3\x80\xC3\xBF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
};
typedef struct
{
char *uri;
char *expected_filename;
char *expected_hostname;
GConvertError expected_error; /* If failed */
} FromUriTest;
FromUriTest
from_uri_tests[] = {
{ "file:///etc", "/etc"},
{ "file:/etc", "/etc"},
#ifdef G_OS_WIN32
/* On Win32 we don't return "localhost" hostames, just in case
* it isn't recognized anyway.
*/
{ "file://localhost/etc", "/etc", NULL},
{ "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL},
{ "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", NULL},
{ "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", NULL},
#else
{ "file://localhost/etc", "/etc", "localhost"},
{ "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
{ "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", "localhost"},
{ "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", "localhost"},
#endif
{ "file://otherhost/etc", "/etc", "otherhost"},
{ "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
{ "file://%C3%B6%C3%A4%C3%A5/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file:////etc/%C3%B6%C3%C3%C3%A5", "//etc/\xc3\xb6\xc3\xc3\xc3\xa5", NULL},
{ "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file:////etc", "//etc"},
{ "file://///etc", "///etc"},
#ifdef G_OS_WIN32
/* URIs with backslashes come from some nonstandard application, but accept them anyhow */
{ "file:///c:\\foo", "c:\\foo"},
{ "file:///c:/foo\\bar", "c:\\foo\\bar"},
/* Accept also the old Netscape drive-letter-and-vertical bar convention */
{ "file:///c|/foo", "c:\\foo"},
{ "file:////server/share/dir", "\\\\server\\share\\dir"},
{ "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
{ "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
#else
{ "file:///c:\\foo", "/c:\\foo"},
{ "file:///c:/foo", "/c:/foo"},
{ "file:////c:/foo", "//c:/foo"},
#endif
{ "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
{ "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
{ "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://%C3%80%C3%BF/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://@/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://:/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
};
static gboolean any_failed = FALSE;
static void
run_to_uri_tests (void)
{
int i;
gchar *res;
GError *error;
for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
{
error = NULL;
res = g_filename_to_uri (to_uri_tests[i].filename,
to_uri_tests[i].hostname,
&error);
if (to_uri_tests[i].expected_result == NULL)
{
if (res != NULL)
{
g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
any_failed = TRUE;
}
else
{
if (error == NULL)
{
g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
any_failed = TRUE;
}
else if (error->domain != G_CONVERT_ERROR)
{
g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
any_failed = TRUE;
}
else if (error->code != to_uri_tests[i].expected_error)
{
g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
i, error->code, to_uri_tests[i].expected_error);
any_failed = TRUE;
}
}
}
else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
{
g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
if (error)
g_print ("Error message: %s\n", error->message);
any_failed = TRUE;
}
g_free (res);
}
}
static void
run_from_uri_tests (void)
{
int i;
gchar *res;
gchar *hostname;
GError *error;
for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
{
error = NULL;
res = g_filename_from_uri (from_uri_tests[i].uri,
&hostname,
&error);
if (from_uri_tests[i].expected_filename == NULL)
{
if (res != NULL)
{
g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
any_failed = TRUE;
}
else
{
if (error == NULL)
{
g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
any_failed = TRUE;
}
else if (error->domain != G_CONVERT_ERROR)
{
g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
any_failed = TRUE;
}
else if (error->code != from_uri_tests[i].expected_error)
{
g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
i, error->code, from_uri_tests[i].expected_error);
any_failed = TRUE;
}
}
}
else
{
#ifdef G_OS_WIN32
gchar *slash, *p;
p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
while ((slash = strchr (p, '/')) != NULL)
{
*slash = '\\';
p = slash + 1;
}
#endif
if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
{
g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
any_failed = TRUE;
}
if (from_uri_tests[i].expected_hostname == NULL)
{
if (hostname != NULL)
{
g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
i, hostname);
any_failed = TRUE;
}
}
else if (hostname == NULL ||
strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
{
g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
any_failed = TRUE;
}
}
}
}
static gint
safe_strcmp (const gchar *a, const gchar *b)
{
return strcmp (a ? a : "", b ? b : "");
}
static gint
safe_strcmp_filename (const gchar *a, const gchar *b)
{
#ifndef G_OS_WIN32
return safe_strcmp (a, b);
#else
if (!a || !b)
return safe_strcmp (a, b);
else
{
while (*a && *b)
{
if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
*a == *b)
a++, b++;
else
return (*a - *b);
}
return (*a - *b);
}
#endif
}
static gint
safe_strcmp_hostname (const gchar *a, const gchar *b)
{
#ifndef G_OS_WIN32
return safe_strcmp (a, b);
#else
if (safe_strcmp (a, "localhost") == 0 && b == NULL)
return 0;
else
return safe_strcmp (a, b);
#endif
}
static void
run_roundtrip_tests (void)
{
int i;
gchar *uri, *hostname, *res;
GError *error;
for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
{
if (to_uri_tests[i].expected_error != 0)
continue;
error = NULL;
uri = g_filename_to_uri (to_uri_tests[i].filename,
to_uri_tests[i].hostname,
&error);
if (error != NULL)
{
g_print ("g_filename_to_uri failed unexpectedly: %s\n",
error->message);
any_failed = TRUE;
continue;
}
error = NULL;
res = g_filename_from_uri (uri, &hostname, &error);
if (error != NULL)
{
g_print ("g_filename_from_uri failed unexpectedly: %s\n",
error->message);
any_failed = TRUE;
continue;
}
if (safe_strcmp_filename (to_uri_tests[i].filename, res))
{
g_print ("roundtrip test %d failed, filename modified: "
" expected \"%s\", but got \"%s\"\n",
i, to_uri_tests[i].filename, res);
any_failed = TRUE;
}
if (safe_strcmp_hostname (to_uri_tests[i].hostname, hostname))
{
g_print ("roundtrip test %d failed, hostname modified: "
" expected \"%s\", but got \"%s\"\n",
i, to_uri_tests[i].hostname, hostname);
any_failed = TRUE;
}
}
}
static void
run_uri_list_tests (void)
{
/* straight from the RFC */
gchar *list =
"# urn:isbn:0-201-08372-8\r\n"
"http://www.huh.org/books/foo.html\r\n"
"http://www.huh.org/books/foo.pdf \r\n"
" ftp://ftp.foo.org/books/foo.txt\r\n";
gchar *expected_uris[] = {
"http://www.huh.org/books/foo.html",
"http://www.huh.org/books/foo.pdf",
"ftp://ftp.foo.org/books/foo.txt"
};
gchar **uris;
gint j;
uris = g_uri_list_extract_uris (list);
if (g_strv_length (uris) != 3)
{
g_print ("uri list test failed: "
" expected %d uris, but got %d\n",
3, g_strv_length (uris));
any_failed = TRUE;
}
for (j = 0; j < 3; j++)
{
if (safe_strcmp (uris[j], expected_uris[j]))
{
g_print ("uri list test failed: "
" expected \"%s\", but got \"%s\"\n",
expected_uris[j], uris[j]);
any_failed = TRUE;
}
}
g_strfreev (uris);
uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air");
if (g_strv_length (uris) != 0)
{
g_print ("uri list test 2 failed: "
" expected %d uris, but got %d (first is \"%s\")\n",
0, g_strv_length (uris), uris[0]);
any_failed = TRUE;
}
}
int
main (int argc,
char *argv[])
{
#ifdef G_OS_UNIX
# ifdef HAVE_UNSETENV
unsetenv ("G_BROKEN_FILENAMES");
# else
/* putenv with no = isn't standard, but works to unset the variable
* on some systems
*/
putenv ("G_BROKEN_FILENAMES");
# endif
#endif
run_to_uri_tests ();
run_from_uri_tests ();
run_roundtrip_tests ();
run_uri_list_tests ();
return any_failed ? 1 : 0;
}
|