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.
22 #include <glib/gtestutils.h>
23 #include <glib/glib.h>
28 struct TestPathsWithOper {
44 test_g_file_new_null (void)
46 const char *paths[] = {"/",
49 "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
52 const char *uris[] = {"file:///",
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",
64 file = g_file_new_for_path (paths[i++]);
65 g_assert (file != NULL);
66 g_object_unref (file);
72 file = g_file_new_for_uri (paths[i++]);
73 g_assert (file != NULL);
81 compare_two_files (const gboolean use_uri, const char *path1, const char *path2)
89 file1 = g_file_new_for_uri (path1);
90 file2 = g_file_new_for_uri (path2);
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);
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 /* Should not fail: {"/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 {"/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 for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++)
133 gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2);
134 g_assert_cmpint (equal, ==, cmp_paths[i].equal);
141 test_g_file_new_for_uri (void)
143 const struct TestPathsWithOper cmp_uris[] = {
144 {"file:///", TRUE, 0, "file:///./"},
145 {"file:////", TRUE, 0, "file:////"},
146 {"file:////", TRUE, 0, "file:////./"},
147 {"file:///", TRUE, 0, "file:///.//"},
148 {"file:///", TRUE, 0, "file:///././"},
149 {"file:///tmp", TRUE, 0, "file:///tmp/d/../"},
150 {"file:///", TRUE, 0, "file:///somedir/../"},
151 {"file:///", FALSE, 0, "file:///somedir/.../"},
152 {"file:////tmp/dir1", TRUE, 0, "file:////tmp/dir1"},
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:///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 for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++)
162 gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2);
163 g_assert_cmpint (equal, ==, cmp_uris[i].equal);
170 dup_equals (const gboolean use_uri, const char *path)
177 file1 = g_file_new_for_uri (path);
179 file1 = g_file_new_for_path (path);
181 g_assert (file1 != NULL);
183 file2 = g_file_dup (file1);
185 g_assert (file2 != NULL);
187 equal = g_file_equal (file1, file2);
189 g_object_unref (file1);
190 g_object_unref (file2);
196 test_g_file_dup (void)
198 const struct TestPathsWithOper dup_paths[] =
201 {"file:///", 0, TRUE, ""},
202 {"totalnonsense", 0, FALSE, ""},
203 {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, ""},
204 {"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 for (i = 0; i < G_N_ELEMENTS (dup_paths); i++)
210 gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1);
211 g_assert (equal == TRUE);
218 parse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name)
223 gboolean is_utf8_valid;
227 file1 = g_file_new_for_uri (path);
229 file1 = g_file_new_for_path (path);
231 g_assert (file1 != NULL);
233 parsed_name = g_file_get_parse_name (file1);
235 g_assert (parsed_name != NULL);
237 /* UTF-8 validation */
238 is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL);
239 g_assert (is_utf8_valid == TRUE);
241 if (result_parse_name)
242 g_assert_cmpstr (parsed_name, ==, result_parse_name);
244 file2 = g_file_parse_name (parsed_name);
246 g_assert (file2 != NULL);
248 equal = g_file_equal (file1, file2);
250 g_object_unref (file1);
251 g_object_unref (file2);
253 g_free (parsed_name);
259 test_g_file_get_parse_name_utf8 (void)
261 const struct TestPathsWithOper strings[] =
263 {"/", 0, FALSE, "/"},
264 {"file:///", 0, TRUE, "/"},
265 {"totalnonsense", 0, FALSE, NULL},
266 {"/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 */},
267 {"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 for (i = 0; i < G_N_ELEMENTS (strings); i++)
273 gboolean equal = parse_check_utf8 (strings[i].use_uri, strings[i].path1, strings[i].path2);
274 g_assert (equal == TRUE);
279 resolve_arg (const gboolean is_uri_only, const char *arg)
286 file1 = g_file_new_for_commandline_arg (arg);
287 g_assert (file1 != NULL);
289 /* Test if we get URI string */
290 uri = g_file_get_uri (file1);
291 g_assert (uri != NULL);
293 /* Test if we get correct value of the local path */
294 path = g_file_get_path (file1);
296 g_assert (path == NULL);
298 g_assert (g_path_is_absolute (path) == TRUE);
300 /* Get the URI scheme and compare it with expected one */
301 s = g_file_get_uri_scheme (file1);
303 g_object_unref (file1);
311 test_g_file_new_for_commandline_arg (void)
313 /* TestPathsWithOper.use_uri represents IsURIOnly here */
314 const struct TestPathsWithOper arg_data[] =
316 {"./", 0, FALSE, "file"},
317 {"../", 0, FALSE, "file"},
318 {"/tmp", 0, FALSE, "file"},
319 {"//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"},
320 {"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"},
321 {"http://www.gtk.org/", 0, TRUE, "http"},
322 {"ftp://user:pass@ftp.gimp.org/", 0, TRUE, "ftp"},
329 for (i = 0; i < G_N_ELEMENTS (arg_data); i++)
331 char *s = resolve_arg (arg_data[i].use_uri, arg_data[i].path1);
332 g_assert_cmpstr (s, ==, arg_data[i].path2);
336 /* Manual test for getting correct cwd */
337 file = g_file_new_for_commandline_arg ("./");
338 resolved = g_file_get_path (file);
339 cwd = g_get_current_dir ();
340 g_assert_cmpstr (resolved, ==, cwd);
341 g_object_unref (file);
347 get_relative_path (const gboolean use_uri, const gboolean should_contain_file, const char *dir1, const char *dir2)
352 gboolean contains_file = FALSE;
353 char *relative_path = NULL;
357 file1 = g_file_new_for_uri (dir1);
358 file2 = g_file_new_for_uri (dir2);
362 file1 = g_file_new_for_path (dir1);
363 file2 = g_file_new_for_path (dir2);
366 g_assert (file1 != NULL);
367 g_assert (file2 != NULL);
369 contains_file = g_file_contains_file (file1, file2);
370 g_assert (contains_file == should_contain_file);
372 relative_path = g_file_get_relative_path (file1, file2);
373 if (should_contain_file)
375 g_assert (relative_path != NULL);
377 file3 = g_file_resolve_relative_path (file1, relative_path);
378 g_assert (g_file_equal (file2, file3) == TRUE);
382 g_object_unref (file1);
384 g_object_unref (file2);
386 g_object_unref (file3);
388 return relative_path;
392 test_g_file_contains_file (void)
394 /* TestPathsWithOper.equal represents here if the dir belongs to the directory structure */
395 const struct TestPathsWithOper dirs[] =
397 /* path1 equal uri path2 path3 */
398 {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2/dir3"},
399 {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2/dir3"},
400 {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3", "dir2/dir3"},
401 {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3", "dir2/dir3"},
402 {"/tmp/", FALSE, FALSE, "/something/", NULL},
403 {"/dir1/dir2", FALSE, FALSE, "/dir1/", NULL},
404 {"//dir1/new", TRUE, FALSE, "//dir1/new/dir2/dir3", "dir2/dir3"},
405 {"/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"},
406 {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2/dir3"},
407 {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2/dir3"},
408 {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2/dir3"},
409 {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2/dir3"},
410 {"file:///tmp/", FALSE, TRUE, "file:///something/", NULL},
411 {"file:///dir1/dir2", FALSE, TRUE, "file:///dir1/", NULL},
412 {"file:////dir1/new", TRUE, TRUE, "file:////dir1/new/dir2/dir3", "dir2/dir3"},
413 {"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"},
414 {"dav://www.gtk.org/plan/", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "meetings/20071218.txt"},
415 {"dav://www.gtk.org/plan/meetings", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "20071218.txt"},
419 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
421 char *s = get_relative_path (dirs[i].use_uri, dirs[i].equal, dirs[i].path1, dirs[i].path2);
423 g_assert_cmpstr (s, ==, dirs[i].path3);
429 roundtrip_parent_child (const gboolean use_uri, const gboolean under_root_descending,
430 const char *path, const char *dir_holder)
432 GFile *files[6] = {0};
437 files[0] = g_file_new_for_uri (path);
438 files[1] = g_file_new_for_uri (path);
442 files[0] = g_file_new_for_path (path);
443 files[1] = g_file_new_for_path (path);
446 g_assert (files[0] != NULL);
447 g_assert (files[1] != NULL);
449 files[2] = g_file_get_child (files[1], dir_holder);
450 g_assert (files[2] != NULL);
452 files[3] = g_file_get_parent (files[2]);
453 g_assert (files[3] != NULL);
454 g_assert (g_file_equal (files[3], files[0]) == TRUE);
456 files[4] = g_file_get_parent (files[3]);
457 /* Don't go lower beyond the root */
458 if (under_root_descending)
459 g_assert (files[4] == NULL);
462 g_assert (files[4] != NULL);
464 files[5] = g_file_get_child (files[4], dir_holder);
465 g_assert (files[5] != NULL);
466 g_assert (g_file_equal (files[5], files[0]) == TRUE);
469 for (i = 0; i < G_N_ELEMENTS (files); i++)
472 g_object_unref (files[i]);
477 test_g_file_get_parent_child (void)
479 const struct TestPathsWithOper paths[] =
481 /* path root_desc uri dir_holder */
482 {"/dir1/dir", FALSE, FALSE, "dir"},
483 {"/dir", FALSE, FALSE, "dir"},
484 {"/", TRUE, FALSE, "dir"},
485 {"/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"},
486 {"file:///dir1/dir", FALSE, TRUE, "dir"},
487 {"file:///dir", FALSE, TRUE, "dir"},
488 {"file:///", TRUE, TRUE, "dir"},
489 {"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"},
490 {"dav://www.gtk.org/plan/meetings", FALSE, TRUE, "meetings"},
494 for (i = 0; i < G_N_ELEMENTS (paths); i++)
495 roundtrip_parent_child (paths[i].use_uri, paths[i].equal, paths[i].path1, paths[i].path2);
503 g_test_init (&argc, &argv, NULL);
506 /* Testing whether g_file_new_for_path() or g_file_new_for_uri() always returns non-NULL result */
507 g_test_add_func ("/g-file/test_g_file_new_null", test_g_file_new_null);
509 /* Testing whether the g_file_new_for_path() correctly canonicalizes strings and two files equals (g_file_equal()) */
510 g_test_add_func ("/g-file/test_g_file_new_for_path", test_g_file_new_for_path);
512 /* Testing whether the g_file_new_for_uri() correctly canonicalizes strings and two files equals (g_file_equal()) */
513 g_test_add_func ("/g-file/test_g_file_new_for_uri", test_g_file_new_for_uri);
515 /* Testing g_file_dup() equals original file via g_file_equal() */
516 g_test_add_func ("/g-file/test_g_file_dup", test_g_file_dup);
518 /* Testing g_file_get_parse_name() to return correct UTF-8 string */
519 g_test_add_func ("/g-file/test_g_file_get_parse_name_utf8", test_g_file_get_parse_name_utf8);
521 /* Testing g_file_new_for_commandline_arg() for correct relavive path resolution and correct path/URI guess */
522 g_test_add_func ("/g-file/test_g_file_new_for_commandline_arg", test_g_file_new_for_commandline_arg);
524 /* Testing g_file_contains_file(), g_file_get_relative_path() and g_file_resolve_relative_path() to return and process correct relative paths */
525 g_test_add_func ("/g-file/test_g_file_contains_file", test_g_file_contains_file);
527 /* Testing g_file_get_parent() and g_file_get_child() */
528 g_test_add_func ("/g-file/test_g_file_get_parent_child", test_g_file_get_parent_child);