sl@0: /* GLIB - Library of useful routines for C programming sl@0: * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald sl@0: * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: /* sl@0: * Modified by the GLib Team and others 1997-2000. See the AUTHORS sl@0: * file for a list of people on the GLib Team. See the ChangeLog sl@0: * files for a list of changes. These files are distributed with sl@0: * GLib at ftp://ftp.gtk.org/pub/gtk/. sl@0: */ sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: typedef struct sl@0: { sl@0: char *filename; sl@0: char *hostname; sl@0: char *expected_result; sl@0: GConvertError expected_error; /* If failed */ sl@0: } ToUriTest; sl@0: sl@0: ToUriTest sl@0: to_uri_tests[] = { sl@0: { "/etc", NULL, "file:///etc"}, sl@0: { "/etc", "", "file:///etc"}, sl@0: { "/etc", "otherhost", "file://otherhost/etc"}, sl@0: #if defined(G_OS_WIN32) || defined (SYMBIAN) sl@0: { "/etc", "localhost", "file:///etc"}, sl@0: { "c:\\windows", NULL, "file:///c:/windows"}, sl@0: { "c:\\windows", "localhost", "file:///c:/windows"}, sl@0: { "c:\\windows", "otherhost", "file://otherhost/c:/windows"}, sl@0: { "\\\\server\\share\\dir", NULL, "file:////server/share/dir"}, sl@0: { "\\\\server\\share\\dir", "localhost", "file:////server/share/dir"}, sl@0: #else sl@0: { "/etc", "localhost", "file://localhost/etc"}, sl@0: { "c:\\windows", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, /* it's important to get this error on Unix */ sl@0: { "c:\\windows", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, sl@0: { "c:\\windows", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, sl@0: #endif sl@0: { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, sl@0: #ifndef G_PLATFORM_WIN32 sl@0: { "/etc/\xE5\xE4\xF6", NULL, "file:///etc/%E5%E4%F6" }, sl@0: { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"}, sl@0: #endif sl@0: { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/etc", "\xE5\xE4\xF6", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"}, sl@0: { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, sl@0: { "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, sl@0: { "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, sl@0: { "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, sl@0: { "/0123456789", NULL, "file:///0123456789"}, sl@0: { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, sl@0: { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"}, sl@0: { "/-_.!~*'()", NULL, "file:///-_.!~*'()"}, sl@0: #if defined(G_OS_WIN32) || defined(SYMBIAN) sl@0: /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */ sl@0: { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"}, sl@0: #else sl@0: /* On Unix, '\\' is a normal character in the file name */ sl@0: { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"}, sl@0: #endif sl@0: { "/;@+$,", NULL, "file:///%3B@+$,"}, sl@0: /* This and some of the following are of course as such illegal file names on Windows, sl@0: * and would not occur in real life. sl@0: */ sl@0: { "/:", NULL, "file:///:"}, sl@0: { "/?&=", NULL, "file:///%3F&="}, sl@0: { "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"}, sl@0: { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"}, sl@0: { "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: { "/", "\xC3\x80\xC3\xBF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE}, sl@0: }; sl@0: sl@0: sl@0: typedef struct sl@0: { sl@0: char *uri; sl@0: char *expected_filename; sl@0: char *expected_hostname; sl@0: GConvertError expected_error; /* If failed */ sl@0: } FromUriTest; sl@0: sl@0: FromUriTest sl@0: from_uri_tests[] = { sl@0: { "file:///etc", "\\etc"}, sl@0: { "file:/etc", "\\etc"}, sl@0: #if defined(G_OS_WIN32) || defined(SYMBIAN) sl@0: /* On Win32 we don't return "localhost" hostames, just in case sl@0: * it isn't recognized anyway. sl@0: */ sl@0: { "file://localhost/etc", "\\etc", NULL}, sl@0: /*{ "file://localhost/etc/%23%25%20file", "\\etc/#% file", NULL},*/ sl@0: { "file://localhost/\xE5\xE4\xF6", "\\\xe5\xe4\xf6", NULL}, sl@0: { "file://localhost/%E5%E4%F6", "\\\xe5\xe4\xf6", NULL}, sl@0: #else sl@0: { "file://localhost/etc", "\etc", "localhost"}, sl@0: { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"}, sl@0: { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", "localhost"}, sl@0: { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", "localhost"}, sl@0: #endif sl@0: { "file://otherhost/etc", "\\etc", "otherhost"}, sl@0: /*{ "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},*/ sl@0: { "file://%C3%B6%C3%A4%C3%A5/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file:////etc/%C3%B6%C3%C3%C3%A5", "\\\\etc\\\xc3\xb6\xc3\xc3\xc3\xa5", NULL}, sl@0: { "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file:////etc", "\\\\etc"}, sl@0: { "file://///etc", "\\\\\\etc"}, sl@0: #if defined(G_OS_WIN32) || defined (SYMBIAN) sl@0: /* URIs with backslashes come from some nonstandard application, but accept them anyhow */ sl@0: { "file:///c:\\foo", "c:\\foo"}, sl@0: { "file:///c:/foo\\bar", "c:\\foo\\bar"}, sl@0: /* Accept also the old Netscape drive-letter-and-vertical bar convention */ sl@0: { "file:///c|/foo", "c:\\foo"}, sl@0: { "file:////server/share/dir", "\\\\server\\share\\dir"}, sl@0: { "file://localhost//server/share/foo", "\\\\server\\share\\foo"}, sl@0: { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"}, sl@0: #else sl@0: { "file:///c:\\foo", "/c:\\foo"}, sl@0: { "file:///c:/foo", "/c:/foo"}, sl@0: { "file:////c:/foo", "//c:/foo"}, sl@0: #endif sl@0: { "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "\\", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, sl@0: { "file://abcdefghijklmnopqrstuvwxyz/", "\\", "abcdefghijklmnopqrstuvwxyz"}, sl@0: { "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://%C3%80%C3%BF/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://@/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://:/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: { "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, sl@0: }; sl@0: sl@0: sl@0: static gboolean any_failed = FALSE; sl@0: sl@0: static void sl@0: run_to_uri_tests (void) sl@0: { sl@0: int i; sl@0: gchar *res; sl@0: GError *error; sl@0: sl@0: for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++) sl@0: { sl@0: error = NULL; sl@0: res = g_filename_to_uri (to_uri_tests[i].filename, sl@0: to_uri_tests[i].hostname, sl@0: &error); sl@0: sl@0: if (to_uri_tests[i].expected_result == NULL) sl@0: { sl@0: if (res != NULL) sl@0: { sl@0: g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res); sl@0: any_failed = TRUE; sl@0: } sl@0: else sl@0: { sl@0: if (error == NULL) sl@0: { sl@0: g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i); sl@0: any_failed = TRUE; sl@0: } sl@0: else if (error->domain != G_CONVERT_ERROR) sl@0: { sl@0: g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i); sl@0: any_failed = TRUE; sl@0: } sl@0: else if (error->code != to_uri_tests[i].expected_error) sl@0: { sl@0: g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n", sl@0: i, error->code, to_uri_tests[i].expected_error); sl@0: any_failed = TRUE; sl@0: } sl@0: } sl@0: } sl@0: else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0) sl@0: { sl@0: g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n", sl@0: i, to_uri_tests[i].expected_result, (res) ? res : "NULL"); sl@0: if (error) sl@0: g_print ("Error message: %s\n", error->message); sl@0: any_failed = TRUE; sl@0: } sl@0: g_free (res); sl@0: } sl@0: } sl@0: sl@0: static void sl@0: run_from_uri_tests (void) sl@0: { sl@0: int i; sl@0: gchar *res; sl@0: gchar *hostname; sl@0: GError *error; sl@0: sl@0: for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++) sl@0: { sl@0: error = NULL; sl@0: res = g_filename_from_uri (from_uri_tests[i].uri, sl@0: &hostname, sl@0: &error); sl@0: sl@0: if (from_uri_tests[i].expected_filename == NULL) sl@0: { sl@0: if (res != NULL) sl@0: { sl@0: g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res); sl@0: any_failed = TRUE; sl@0: } sl@0: else sl@0: { sl@0: if (error == NULL) sl@0: { sl@0: g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i); sl@0: any_failed = TRUE; sl@0: } sl@0: else if (error->domain != G_CONVERT_ERROR) sl@0: { sl@0: g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i); sl@0: any_failed = TRUE; sl@0: } sl@0: else if (error->code != from_uri_tests[i].expected_error) sl@0: { sl@0: g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n", sl@0: i, error->code, from_uri_tests[i].expected_error); sl@0: any_failed = TRUE; sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: #if defined(G_OS_WIN32) sl@0: gchar *slash, *p; sl@0: sl@0: p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename); sl@0: while ((slash = strchr (p, '/')) != NULL) sl@0: { sl@0: *slash = '\\'; sl@0: p = slash + 1; sl@0: } sl@0: #endif sl@0: if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0) sl@0: { sl@0: g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n", sl@0: i, from_uri_tests[i].expected_filename, (res) ? res : "NULL"); sl@0: any_failed = TRUE; sl@0: } sl@0: sl@0: if (from_uri_tests[i].expected_hostname == NULL) sl@0: { sl@0: if (hostname != NULL) sl@0: { sl@0: g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n", sl@0: i, hostname); sl@0: any_failed = TRUE; sl@0: } sl@0: } sl@0: else if (hostname == NULL || sl@0: strcmp (hostname, from_uri_tests[i].expected_hostname) != 0) sl@0: { sl@0: g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n", sl@0: i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL"); sl@0: any_failed = TRUE; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: static gint sl@0: safe_strcmp (const gchar *a, const gchar *b) sl@0: { sl@0: return strcmp (a ? a : "", b ? b : ""); sl@0: } sl@0: sl@0: static gint sl@0: safe_strcmp_filename (const gchar *a, const gchar *b) sl@0: { sl@0: #if !defined(G_OS_WIN32) && !defined(SYMBIAN) sl@0: return safe_strcmp (a, b); sl@0: #else sl@0: if (!a || !b) sl@0: return safe_strcmp (a, b); sl@0: else sl@0: { sl@0: while (*a && *b) sl@0: { sl@0: if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) || sl@0: *a == *b) sl@0: a++, b++; sl@0: else sl@0: return (*a - *b); sl@0: } sl@0: return (*a - *b); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: static gint sl@0: safe_strcmp_hostname (const gchar *a, const gchar *b) sl@0: { sl@0: #if !defined(G_OS_WIN32) && !defined(SYMBIAN) sl@0: return safe_strcmp (a, b); sl@0: #else sl@0: if (safe_strcmp (a, "localhost") == 0 && b == NULL) sl@0: return 0; sl@0: else sl@0: return safe_strcmp (a, b); sl@0: #endif sl@0: } sl@0: sl@0: static void sl@0: run_roundtrip_tests (void) sl@0: { sl@0: int i; sl@0: gchar *uri, *hostname, *res; sl@0: GError *error; sl@0: sl@0: for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++) sl@0: { sl@0: if (to_uri_tests[i].expected_error != 0) sl@0: continue; sl@0: sl@0: error = NULL; sl@0: uri = g_filename_to_uri (to_uri_tests[i].filename, sl@0: to_uri_tests[i].hostname, sl@0: &error); sl@0: sl@0: if (error != NULL) sl@0: { sl@0: g_print ("g_filename_to_uri failed unexpectedly: %s\n", sl@0: error->message); sl@0: any_failed = TRUE; sl@0: continue; sl@0: } sl@0: sl@0: error = NULL; sl@0: res = g_filename_from_uri (uri, &hostname, &error); sl@0: if (error != NULL) sl@0: { sl@0: g_print ("g_filename_from_uri failed unexpectedly: %s\n", sl@0: error->message); sl@0: any_failed = TRUE; sl@0: continue; sl@0: } sl@0: sl@0: if (safe_strcmp_filename (to_uri_tests[i].filename, res)) sl@0: { sl@0: g_print ("roundtrip test %d failed, filename modified: " sl@0: " expected \"%s\", but got \"%s\"\n", sl@0: i, to_uri_tests[i].filename, res); sl@0: any_failed = TRUE; sl@0: } sl@0: sl@0: if (safe_strcmp_hostname (to_uri_tests[i].hostname, hostname)) sl@0: { sl@0: g_print ("roundtrip test %d failed, hostname modified: " sl@0: " expected \"%s\", but got \"%s\"\n", sl@0: i, to_uri_tests[i].hostname, hostname); sl@0: any_failed = TRUE; sl@0: } sl@0: } sl@0: } sl@0: sl@0: static void sl@0: run_uri_list_tests (void) sl@0: { sl@0: /* straight from the RFC */ sl@0: gchar *list = sl@0: "# urn:isbn:0-201-08372-8\r\n" sl@0: "http://www.huh.org/books/foo.html\r\n" sl@0: "http://www.huh.org/books/foo.pdf \r\n" sl@0: " ftp://ftp.foo.org/books/foo.txt\r\n"; sl@0: gchar *expected_uris[] = { sl@0: "http://www.huh.org/books/foo.html", sl@0: "http://www.huh.org/books/foo.pdf", sl@0: "ftp://ftp.foo.org/books/foo.txt" sl@0: }; sl@0: sl@0: gchar **uris; sl@0: gint j; sl@0: sl@0: uris = g_uri_list_extract_uris (list); sl@0: sl@0: if (g_strv_length (uris) != 3) sl@0: { sl@0: g_print ("uri list test failed: " sl@0: " expected %d uris, but got %d\n", sl@0: 3, g_strv_length (uris)); sl@0: any_failed = TRUE; sl@0: } sl@0: sl@0: for (j = 0; j < 3; j++) sl@0: { sl@0: if (safe_strcmp (uris[j], expected_uris[j])) sl@0: { sl@0: g_print ("uri list test failed: " sl@0: " expected \"%s\", but got \"%s\"\n", sl@0: expected_uris[j], uris[j]); sl@0: any_failed = TRUE; sl@0: } sl@0: } sl@0: sl@0: g_strfreev (uris); sl@0: sl@0: uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air"); sl@0: if (g_strv_length (uris) != 0) sl@0: { sl@0: g_print ("uri list test 2 failed: " sl@0: " expected %d uris, but got %d (first is \"%s\")\n", sl@0: 0, g_strv_length (uris), uris[0]); sl@0: any_failed = TRUE; sl@0: } sl@0: sl@0: } sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: #ifdef SYMBIAN sl@0: sl@0: g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*SYMBIAN*/ sl@0: #ifdef G_OS_UNIX sl@0: # ifdef HAVE_UNSETENV sl@0: unsetenv ("G_BROKEN_FILENAMES"); sl@0: # else sl@0: /* putenv with no = isn't standard, but works to unset the variable sl@0: * on some systems sl@0: */ sl@0: putenv ("G_BROKEN_FILENAMES"); sl@0: # endif sl@0: #endif sl@0: sl@0: run_to_uri_tests (); sl@0: run_from_uri_tests (); sl@0: run_roundtrip_tests (); sl@0: run_uri_list_tests (); sl@0: #ifdef SYMBIAN sl@0: assert_failed = any_failed; sl@0: testResultXml("uri-test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return any_failed ? 1 : 0; sl@0: }