gwin32: Remove old win32 codepage ABI compat code
[glib.git] / gio / tests / g-file.c
blobf87c1b1d89aa2e66a49aa191f11afde3cd7a4657
1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work 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.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 struct TestPathsWithOper {
29 const char *path1;
30 gboolean equal;
31 gboolean use_uri;
32 const char *path2;
33 const char *path3;
38 /* TODO:
39 * - test on Windows
41 **/
43 static void
44 test_g_file_new_null (void)
46 const char *paths[] = {"/",
47 "/tmp///",
48 "/non-existent-file",
49 "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
50 NULL
52 const char *uris[] = {"file:///",
53 "file:///tmp///",
54 "non-existent-uri:///some-dir/",
55 "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88",
56 NULL
59 GFile *file = NULL;
61 int i = 0;
62 while (paths[i])
64 file = g_file_new_for_path (paths[i++]);
65 g_assert (file != NULL);
66 g_object_unref (file);
69 i = 0;
70 while (uris[i])
72 file = g_file_new_for_uri (uris[i++]);
73 g_assert (file != NULL);
74 g_object_unref(file);
80 static gboolean
81 compare_two_files (const gboolean use_uri, const char *path1, const char *path2)
83 GFile *file1 = NULL;
84 GFile *file2 = NULL;
85 gboolean equal;
87 if (use_uri)
89 file1 = g_file_new_for_uri (path1);
90 file2 = g_file_new_for_uri (path2);
92 else
94 file1 = g_file_new_for_path (path1);
95 file2 = g_file_new_for_path (path2);
98 g_assert (file1 != NULL);
99 g_assert (file2 != NULL);
101 equal = g_file_equal (file1, file2);
103 g_object_unref (file1);
104 g_object_unref (file2);
106 return equal;
109 static void
110 test_g_file_new_for_path (void)
112 const struct TestPathsWithOper cmp_paths[] =
114 {"/", TRUE, 0, "/./"},
115 {"//", TRUE, 0, "//"},
116 {"//", TRUE, 0, "//./"},
117 {"/", TRUE, 0, "/.//"},
118 {"/", TRUE, 0, "/././"},
119 {"/tmp", TRUE, 0, "/tmp/d/../"},
120 {"/", TRUE, 0, "/somedir/../"},
121 {"/", FALSE, 0, "/somedir/.../"},
122 {"//tmp/dir1", TRUE, 0, "//tmp/dir1"},
123 {"/tmp/dir1", TRUE, 0, "///tmp/dir1"},
124 {"/tmp/dir1", TRUE, 0, "////tmp/dir1"},
125 {"/tmp/dir1", TRUE, 0, "/tmp/./dir1"},
126 {"/tmp/dir1", TRUE, 0, "/tmp//dir1"},
127 {"/tmp/dir1", TRUE, 0, "/tmp///dir1///"},
128 {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, 0, "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/"}
131 guint i;
132 for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++)
134 gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2);
135 g_assert_cmpint (equal, ==, cmp_paths[i].equal);
141 static void
142 test_g_file_new_for_uri (void)
144 const struct TestPathsWithOper cmp_uris[] = {
145 {"file:///", TRUE, 0, "file:///./"},
146 {"file:////", TRUE, 0, "file:////"},
147 {"file:////", TRUE, 0, "file:////./"},
148 {"file:///", TRUE, 0, "file:///.//"},
149 {"file:///", TRUE, 0, "file:///././"},
150 {"file:///tmp", TRUE, 0, "file:///tmp/d/../"},
151 {"file:///", TRUE, 0, "file:///somedir/../"},
152 {"file:///", FALSE, 0, "file:///somedir/.../"},
153 {"file:////tmp/dir1", TRUE, 0, "file:////tmp/dir1"},
154 {"file:///tmp/dir1", TRUE, 0, "file:///tmp/./dir1"},
155 {"file:///tmp/dir1", TRUE, 0, "file:///tmp//dir1"},
156 {"file:///tmp/dir1", TRUE, 0, "file:///tmp///dir1///"},
157 {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, 0, "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/"}
160 guint i;
161 for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++)
163 gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2);
164 g_assert_cmpint (equal, ==, cmp_uris[i].equal);
170 static gboolean
171 dup_equals (const gboolean use_uri, const char *path)
173 GFile *file1 = NULL;
174 GFile *file2 = NULL;
175 gboolean equal;
177 if (use_uri)
178 file1 = g_file_new_for_uri (path);
179 else
180 file1 = g_file_new_for_path (path);
182 g_assert (file1 != NULL);
184 file2 = g_file_dup (file1);
186 g_assert (file2 != NULL);
188 equal = g_file_equal (file1, file2);
190 g_object_unref (file1);
191 g_object_unref (file2);
193 return equal;
196 static void
197 test_g_file_dup (void)
199 const struct TestPathsWithOper dup_paths[] =
201 {"/", 0, FALSE, ""},
202 {"file:///", 0, TRUE, ""},
203 {"totalnonsense", 0, FALSE, ""},
204 {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, ""},
205 {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", 0, TRUE, ""},
208 guint i;
209 for (i = 0; i < G_N_ELEMENTS (dup_paths); i++)
211 gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1);
212 g_assert (equal == TRUE);
218 static gboolean
219 parse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name)
221 GFile *file1 = NULL;
222 GFile *file2 = NULL;
223 char *parsed_name;
224 gboolean is_utf8_valid;
225 gboolean equal;
227 if (use_uri)
228 file1 = g_file_new_for_uri (path);
229 else
230 file1 = g_file_new_for_path (path);
232 g_assert (file1 != NULL);
234 parsed_name = g_file_get_parse_name (file1);
236 g_assert (parsed_name != NULL);
238 /* UTF-8 validation */
239 is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL);
240 g_assert (is_utf8_valid == TRUE);
242 if (result_parse_name)
243 g_assert_cmpstr (parsed_name, ==, result_parse_name);
245 file2 = g_file_parse_name (parsed_name);
247 g_assert (file2 != NULL);
249 equal = g_file_equal (file1, file2);
251 g_object_unref (file1);
252 g_object_unref (file2);
254 g_free (parsed_name);
256 return equal;
259 static void
260 test_g_file_get_parse_name_utf8 (void)
262 const struct TestPathsWithOper strings[] =
264 {G_DIR_SEPARATOR_S, 0, FALSE, G_DIR_SEPARATOR_S},
265 {"file:///", 0, TRUE, G_DIR_SEPARATOR_S},
266 {"totalnonsense", 0, FALSE, NULL},
267 {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, NULL /* Depends on local file encoding */},
268 {"file:///invalid%08/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, TRUE, "file:///invalid%08/UTF-8%20p\xc5\x99\xc3\xadli\xc5\xa1%20\xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd%20k\xc5\xaf\xc5\x88"},
271 guint i;
272 for (i = 0; i < G_N_ELEMENTS (strings); i++)
274 gboolean equal = parse_check_utf8 (strings[i].use_uri, strings[i].path1, strings[i].path2);
275 g_assert (equal == TRUE);
279 static char *
280 resolve_arg (const gboolean is_uri_only, const char *arg)
282 GFile *file1 = NULL;
283 char *uri = NULL;
284 char *path = NULL;
285 char *s = NULL;
287 file1 = g_file_new_for_commandline_arg (arg);
288 g_assert (file1 != NULL);
290 /* Test if we get URI string */
291 uri = g_file_get_uri (file1);
292 g_assert_cmpstr (uri, !=, NULL);
293 g_printerr ("%s\n",uri);
295 /* Test if we get correct value of the local path */
296 path = g_file_get_path (file1);
297 if (is_uri_only)
298 g_assert_cmpstr (path, ==, NULL);
299 else
300 g_assert (g_path_is_absolute (path) == TRUE);
302 /* Get the URI scheme and compare it with expected one */
303 s = g_file_get_uri_scheme (file1);
305 g_object_unref (file1);
306 g_free (uri);
307 g_free (path);
309 return s;
312 static void
313 test_g_file_new_for_commandline_arg (void)
315 /* TestPathsWithOper.use_uri represents IsURIOnly here */
316 const struct TestPathsWithOper arg_data[] =
318 {"./", 0, FALSE, "file"},
319 {"../", 0, FALSE, "file"},
320 {"/tmp", 0, FALSE, "file"},
321 {"//UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, "file"},
322 {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, FALSE, "file"},
323 #if 0
324 {"http://www.gtk.org/", 0, TRUE, "http"},
325 {"ftp://user:pass@ftp.gimp.org/", 0, TRUE, "ftp"},
326 #endif
328 GFile *file;
329 char *resolved;
330 char *cwd;
331 guint i;
333 for (i = 0; i < G_N_ELEMENTS (arg_data); i++)
335 char *s = resolve_arg (arg_data[i].use_uri, arg_data[i].path1);
336 g_assert_cmpstr (s, ==, arg_data[i].path2);
337 g_free (s);
340 /* Manual test for getting correct cwd */
341 file = g_file_new_for_commandline_arg ("./");
342 resolved = g_file_get_path (file);
343 cwd = g_get_current_dir ();
344 g_assert_cmpstr (resolved, ==, cwd);
345 g_object_unref (file);
346 g_free (resolved);
347 g_free (cwd);
350 static char*
351 get_relative_path (const gboolean use_uri, const gboolean should_have_prefix, const char *dir1, const char *dir2)
353 GFile *file1 = NULL;
354 GFile *file2 = NULL;
355 GFile *file3 = NULL;
356 gboolean has_prefix = FALSE;
357 char *relative_path = NULL;
359 if (use_uri)
361 file1 = g_file_new_for_uri (dir1);
362 file2 = g_file_new_for_uri (dir2);
364 else
366 file1 = g_file_new_for_path (dir1);
367 file2 = g_file_new_for_path (dir2);
370 g_assert (file1 != NULL);
371 g_assert (file2 != NULL);
373 has_prefix = g_file_has_prefix (file2, file1);
374 g_printerr ("%s %s\n", dir1, dir2);
375 g_assert (has_prefix == should_have_prefix);
377 relative_path = g_file_get_relative_path (file1, file2);
378 if (should_have_prefix)
380 g_assert (relative_path != NULL);
382 file3 = g_file_resolve_relative_path (file1, relative_path);
383 g_assert (g_file_equal (file2, file3) == TRUE);
386 if (file1)
387 g_object_unref (file1);
388 if (file2)
389 g_object_unref (file2);
390 if (file3)
391 g_object_unref (file3);
393 return relative_path;
396 static void
397 test_g_file_has_prefix (void)
399 /* TestPathsWithOper.equal represents here if the dir belongs to the directory structure */
400 const struct TestPathsWithOper dirs[] =
402 /* path1 equal uri path2 path3 */
403 {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
404 {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
405 {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
406 {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
407 {"/tmp/", FALSE, FALSE, "/something/", NULL},
408 {"/dir1/dir2", FALSE, FALSE, "/dir1/", NULL},
409 {"//dir1/new", TRUE, FALSE, "//dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
410 {"/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, FALSE, "/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/dir2", "dir2"},
411 {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
412 {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
413 {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
414 {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
415 {"file:///tmp/", FALSE, TRUE, "file:///something/", NULL},
416 {"file:///dir1/dir2", FALSE, TRUE, "file:///dir1/", NULL},
417 {"file:////dir1/new", TRUE, TRUE, "file:////dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
418 {"file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, TRUE, "file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/dir2", "dir2"},
419 #if 0
420 {"dav://www.gtk.org/plan/", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "meetings/20071218.txt"},
421 {"dav://www.gtk.org/plan/meetings", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "20071218.txt"},
422 #endif
425 guint i;
426 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
428 char *s = get_relative_path (dirs[i].use_uri, dirs[i].equal, dirs[i].path1, dirs[i].path2);
429 if (dirs[i].equal)
430 g_assert_cmpstr (s, ==, dirs[i].path3);
431 g_free (s);
435 static void
436 roundtrip_parent_child (const gboolean use_uri, const gboolean under_root_descending,
437 const char *path, const char *dir_holder)
439 GFile *files[6] = {NULL};
440 guint i;
442 if (use_uri)
444 files[0] = g_file_new_for_uri (path);
445 files[1] = g_file_new_for_uri (path);
447 else
449 files[0] = g_file_new_for_path (path);
450 files[1] = g_file_new_for_path (path);
453 g_assert (files[0] != NULL);
454 g_assert (files[1] != NULL);
456 files[2] = g_file_get_child (files[1], dir_holder);
457 g_assert (files[2] != NULL);
459 files[3] = g_file_get_parent (files[2]);
460 g_assert (files[3] != NULL);
461 g_assert (g_file_equal (files[3], files[0]) == TRUE);
463 files[4] = g_file_get_parent (files[3]);
464 /* Don't go lower beyond the root */
465 if (under_root_descending)
466 g_assert (files[4] == NULL);
467 else
469 g_assert (files[4] != NULL);
471 files[5] = g_file_get_child (files[4], dir_holder);
472 g_assert (files[5] != NULL);
473 g_assert (g_file_equal (files[5], files[0]) == TRUE);
476 for (i = 0; i < G_N_ELEMENTS (files); i++)
478 if (files[i])
479 g_object_unref (files[i]);
483 static void
484 test_g_file_get_parent_child (void)
486 const struct TestPathsWithOper paths[] =
488 /* path root_desc uri dir_holder */
489 {"/dir1/dir", FALSE, FALSE, "dir"},
490 {"/dir", FALSE, FALSE, "dir"},
491 {"/", TRUE, FALSE, "dir"},
492 {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/", FALSE, FALSE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"},
493 {"file:///dir1/dir", FALSE, TRUE, "dir"},
494 {"file:///dir", FALSE, TRUE, "dir"},
495 {"file:///", TRUE, TRUE, "dir"},
496 {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", FALSE, TRUE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"},
497 {"dav://www.gtk.org/plan/meetings", FALSE, TRUE, "meetings"},
500 guint i;
501 for (i = 0; i < G_N_ELEMENTS (paths); i++)
502 roundtrip_parent_child (paths[i].use_uri, paths[i].equal, paths[i].path1, paths[i].path2);
506 main (int argc,
507 char *argv[])
509 g_test_init (&argc, &argv, NULL);
512 /* Testing whether g_file_new_for_path() or g_file_new_for_uri() always returns non-NULL result */
513 g_test_add_func ("/g-file/test_g_file_new_null", test_g_file_new_null);
515 /* Testing whether the g_file_new_for_path() correctly canonicalizes strings and two files equals (g_file_equal()) */
516 g_test_add_func ("/g-file/test_g_file_new_for_path", test_g_file_new_for_path);
518 /* Testing whether the g_file_new_for_uri() correctly canonicalizes strings and two files equals (g_file_equal()) */
519 g_test_add_func ("/g-file/test_g_file_new_for_uri", test_g_file_new_for_uri);
521 /* Testing g_file_dup() equals original file via g_file_equal() */
522 g_test_add_func ("/g-file/test_g_file_dup", test_g_file_dup);
524 /* Testing g_file_get_parse_name() to return correct UTF-8 string */
525 g_test_add_func ("/g-file/test_g_file_get_parse_name_utf8", test_g_file_get_parse_name_utf8);
527 /* Testing g_file_new_for_commandline_arg() for correct relavive path resolution and correct path/URI guess */
528 g_test_add_func ("/g-file/test_g_file_new_for_commandline_arg", test_g_file_new_for_commandline_arg);
530 /* Testing g_file_has_prefix(), g_file_get_relative_path() and g_file_resolve_relative_path() to return and process correct relative paths */
531 g_test_add_func ("/g-file/test_g_file_has_prefix", test_g_file_has_prefix);
533 /* Testing g_file_get_parent() and g_file_get_child() */
534 g_test_add_func ("/g-file/test_g_file_get_parent_child", test_g_file_get_parent_child);
536 return g_test_run();