1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #undef G_DISABLE_ASSERT
39 char *expected_result;
40 GConvertError expected_error; /* If failed */
45 { "/etc", NULL, "file:///etc"},
46 { "/etc", "", "file:///etc"},
47 { "/etc", "localhost", "file://localhost/etc"},
49 { "c:\\windows", NULL, "file:///c:\\windows"},
50 { "c:\\windows", "localhost", "file://localhost/c:\\windows"},
52 { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
53 { "/etc/öäå", NULL, NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
54 { "/etc/öäå", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
55 { "/etc", "öäå", "file://%C3%B6%C3%A4%C3%A5/etc"},
56 { "/etc", "åäö", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
57 { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
64 char *expected_filename;
65 char *expected_hostname;
66 GConvertError expected_error; /* If failed */
71 { "file:///etc", "/etc", NULL},
72 { "file:/etc", "/etc", NULL},
73 { "file://localhost/etc", "/etc", "localhost", },
74 { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost", },
75 { "file://%C3%B6%C3%A4%C3%A5/etc", "/etc", "öäå", },
76 { "file:////etc/%C3%B6%C3%C3%C3%A5", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
77 { "file://localhost/åäö", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
78 { "file://åäö/etc", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
79 { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
80 { "file://some", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
84 static gboolean any_failed = FALSE;
87 run_to_uri_tests (void)
93 for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
96 res = g_filename_to_uri (to_uri_tests[i].filename,
97 to_uri_tests[i].hostname,
100 if (to_uri_tests[i].expected_result == NULL)
104 g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
111 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
114 else if (error->domain != G_CONVERT_ERROR)
116 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
119 else if (error->code != to_uri_tests[i].expected_error)
121 g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
122 i, error->code, to_uri_tests[i].expected_error);
127 else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
129 g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
130 i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
132 g_print ("Error message: %s\n", error->message);
136 /* Give some output */
142 run_from_uri_tests (void)
149 for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
152 res = g_filename_from_uri (from_uri_tests[i].uri,
156 if (from_uri_tests[i].expected_filename == NULL)
160 g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
167 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
170 else if (error->domain != G_CONVERT_ERROR)
172 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
175 else if (error->code != from_uri_tests[i].expected_error)
177 g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
178 i, error->code, from_uri_tests[i].expected_error);
185 if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
187 g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
188 i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
192 if (from_uri_tests[i].expected_hostname == NULL)
194 if (hostname != NULL)
196 g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
201 else if (hostname == NULL ||
202 strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
204 g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
205 i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
210 /* Give some output */
221 run_from_uri_tests ();
223 return any_failed ? 1 : 0;