1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
4 *
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 *
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
10 *
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
23 */
24
25 #include <glib/glib.h>
26 #include <gio/gio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 struct TestPathsWithOper {
31 const char *path1;
32 gboolean equal;
33 gboolean use_uri;
34 const char *path2;
35 const char *path3;
36 };
37
38
39
40 /* TODO:
41 * - test on Windows
42 *
43 **/
44
45 static void
46 test_g_file_new_null (void)
47 {
48 const char *paths[] = {"/",
49 "/tmp///",
50 "/non-existent-file",
51 "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
52 NULL
53 };
54 const char *uris[] = {"file:///",
55 "file:///tmp///",
56 "non-existent-uri:///some-dir/",
57 "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88",
58 NULL
59 };
60
61 GFile *file = NULL;
62
63 int i = 0;
64 while (paths[i])
65 {
66 file = g_file_new_for_path (paths[i++]);
67 g_assert (file != NULL);
68 g_object_unref (file);
69 }
70
71 i = 0;
72 while (uris[i])
73 {
74 file = g_file_new_for_uri (uris[i++]);
75 g_assert (file != NULL);
76 g_object_unref(file);
77 }
78 }
79
80
81
82 static gboolean
83 compare_two_files (const gboolean use_uri, const char *path1, const char *path2)
84 {
85 GFile *file1 = NULL;
86 GFile *file2 = NULL;
87 gboolean equal;
88
89 if (use_uri)
90 {
91 file1 = g_file_new_for_uri (path1);
92 file2 = g_file_new_for_uri (path2);
93 }
94 else
95 {
96 file1 = g_file_new_for_path (path1);
97 file2 = g_file_new_for_path (path2);
98 }
99
100 g_assert (file1 != NULL);
101 g_assert (file2 != NULL);
102
103 equal = g_file_equal (file1, file2);
104
105 g_object_unref (file1);
106 g_object_unref (file2);
107
108 return equal;
109 }
110
111 static void
112 test_g_file_new_for_path (void)
113 {
114 const struct TestPathsWithOper cmp_paths[] =
115 {
116 {"/", TRUE, 0, "/./", NULL },
117 {"//", TRUE, 0, "//", NULL },
118 {"//", TRUE, 0, "//./", NULL },
119 {"/", TRUE, 0, "/.//", NULL },
120 {"/", TRUE, 0, "/././", NULL },
121 {"/tmp", TRUE, 0, "/tmp/d/../", NULL },
122 {"/", TRUE, 0, "/somedir/../", NULL },
123 {"/", FALSE, 0, "/somedir/.../", NULL },
124 {"//tmp/dir1", TRUE, 0, "//tmp/dir1", NULL },
125 {"/tmp/dir1", TRUE, 0, "///tmp/dir1", NULL },
126 {"/tmp/dir1", TRUE, 0, "////tmp/dir1", NULL },
127 {"/tmp/dir1", TRUE, 0, "/tmp/./dir1", NULL },
128 {"/tmp/dir1", TRUE, 0, "/tmp//dir1", NULL },
129 {"/tmp/dir1", TRUE, 0, "/tmp///dir1///", NULL },
130 {"/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/", NULL }
131 };
132
133 guint i;
134 for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++)
135 {
136 gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2);
137 g_assert_cmpint (equal, ==, cmp_paths[i].equal);
138 }
139 }
140
141
142
143 static void
144 test_g_file_new_for_uri (void)
145 {
146 const struct TestPathsWithOper cmp_uris[] = {
147 {"file:///", TRUE, 0, "file:///./", NULL },
148 {"file:////", TRUE, 0, "file:////", NULL },
149 {"file:////", TRUE, 0, "file:////./", NULL },
150 {"file:///", TRUE, 0, "file:///.//", NULL },
151 {"file:///", TRUE, 0, "file:///././", NULL },
152 {"file:///tmp", TRUE, 0, "file:///tmp/d/../", NULL },
153 {"file:///", TRUE, 0, "file:///somedir/../", NULL },
154 {"file:///", FALSE, 0, "file:///somedir/.../", NULL },
155 {"file:////tmp/dir1", TRUE, 0, "file:////tmp/dir1", NULL },
156 {"file:///tmp/dir1", TRUE, 0, "file:///tmp/./dir1", NULL },
157 {"file:///tmp/dir1", TRUE, 0, "file:///tmp//dir1", NULL },
158 {"file:///tmp/dir1", TRUE, 0, "file:///tmp///dir1///", NULL },
159 {"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/", NULL }
160 };
161
162 guint i;
163 for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++)
164 {
165 gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2);
166 g_assert_cmpint (equal, ==, cmp_uris[i].equal);
167 }
168 }
169
170
171
172 static gboolean
173 dup_equals (const gboolean use_uri, const char *path)
174 {
175 GFile *file1 = NULL;
176 GFile *file2 = NULL;
177 gboolean equal;
178
179 if (use_uri)
180 file1 = g_file_new_for_uri (path);
181 else
182 file1 = g_file_new_for_path (path);
183
184 g_assert (file1 != NULL);
185
186 file2 = g_file_dup (file1);
187
188 g_assert (file2 != NULL);
189
190 equal = g_file_equal (file1, file2);
191
192 g_object_unref (file1);
193 g_object_unref (file2);
194
195 return equal;
196 }
197
198 static void
199 test_g_file_dup (void)
200 {
201 const struct TestPathsWithOper dup_paths[] =
202 {
203 {"/", 0, FALSE, "", NULL },
204 {"file:///", 0, TRUE, "", NULL },
205 {"totalnonsense", 0, FALSE, "", NULL },
206 {"/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 },
207 {"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, "", NULL },
208 };
209
210 guint i;
211 for (i = 0; i < G_N_ELEMENTS (dup_paths); i++)
212 {
213 gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1);
214 g_assert (equal == TRUE);
215 }
216 }
217
218
219
220 static gboolean
221 parse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name)
222 {
223 GFile *file1 = NULL;
224 GFile *file2 = NULL;
225 char *parsed_name;
226 gboolean is_utf8_valid;
227 gboolean equal;
228
229 if (use_uri)
230 file1 = g_file_new_for_uri (path);
231 else
232 file1 = g_file_new_for_path (path);
233
234 g_assert (file1 != NULL);
235
236 parsed_name = g_file_get_parse_name (file1);
237
238 g_assert (parsed_name != NULL);
239
240 /* UTF-8 validation */
241 is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL);
242 g_assert (is_utf8_valid == TRUE);
243
244 if (result_parse_name)
245 g_assert_cmpstr (parsed_name, ==, result_parse_name);
246
247 file2 = g_file_parse_name (parsed_name);
248
249 g_assert (file2 != NULL);
250
251 equal = g_file_equal (file1, file2);
252
253 g_object_unref (file1);
254 g_object_unref (file2);
255
256 g_free (parsed_name);
257
258 return equal;
259 }
260
261 static void
262 test_g_file_get_parse_name_utf8 (void)
263 {
264 const struct TestPathsWithOper strings[] =
265 {
266 {G_DIR_SEPARATOR_S, 0, FALSE, G_DIR_SEPARATOR_S, NULL },
267 {"file:///", 0, TRUE, G_DIR_SEPARATOR_S, NULL },
268 {"totalnonsense", 0, FALSE, NULL, NULL },
269 {"/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, NULL /* Depends on local file encoding */},
270 {"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", NULL },
271 };
272
273 guint i;
274 for (i = 0; i < G_N_ELEMENTS (strings); i++)
275 {
276 gboolean equal = parse_check_utf8 (strings[i].use_uri, strings[i].path1, strings[i].path2);
277 g_assert (equal == TRUE);
278 }
279 }
280
281 static char *
282 resolve_arg (const gboolean is_uri_only, const char *arg)
283 {
284 GFile *file1 = NULL;
285 char *uri = NULL;
286 char *path = NULL;
287 char *s = NULL;
288
289 file1 = g_file_new_for_commandline_arg (arg);
290 g_assert (file1 != NULL);
291
292 /* Test if we get URI string */
293 uri = g_file_get_uri (file1);
294 g_assert_cmpstr (uri, !=, NULL);
295 g_printerr ("%s\n",uri);
296
297 /* Test if we get correct value of the local path */
298 path = g_file_get_path (file1);
299 if (is_uri_only)
300 g_assert_cmpstr (path, ==, NULL);
301 else
302 g_assert (g_path_is_absolute (path) == TRUE);
303
304 /* Get the URI scheme and compare it with expected one */
305 s = g_file_get_uri_scheme (file1);
306
307 g_object_unref (file1);
308 g_free (uri);
309 g_free (path);
310
311 return s;
312 }
313
314 static void
315 test_g_file_new_for_commandline_arg (void)
316 {
317 /* TestPathsWithOper.use_uri represents IsURIOnly here */
318 const struct TestPathsWithOper arg_data[] =
319 {
320 {"./", 0, FALSE, "file", NULL },
321 {"../", 0, FALSE, "file", NULL },
322 {"/tmp", 0, FALSE, "file", NULL },
323 {"//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", NULL },
324 {"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", NULL },
325 #if 0
326 {"http://www.gtk.org/", 0, TRUE, "http", NULL },
327 {"ftp://user:pass@ftp.gimp.org/", 0, TRUE, "ftp", NULL },
328 #endif
329 };
330 GFile *file;
331 char *resolved;
332 char *cwd;
333 guint i;
334
335 for (i = 0; i < G_N_ELEMENTS (arg_data); i++)
336 {
337 char *s = resolve_arg (arg_data[i].use_uri, arg_data[i].path1);
338 g_assert_cmpstr (s, ==, arg_data[i].path2);
339 g_free (s);
340 }
341
342 /* Manual test for getting correct cwd */
343 file = g_file_new_for_commandline_arg ("./");
344 resolved = g_file_get_path (file);
345 cwd = g_get_current_dir ();
346 g_assert_cmpstr (resolved, ==, cwd);
347 g_object_unref (file);
348 g_free (resolved);
349 g_free (cwd);
350 }
351
352 static char*
353 get_relative_path (const gboolean use_uri, const gboolean should_have_prefix, const char *dir1, const char *dir2)
354 {
355 GFile *file1 = NULL;
356 GFile *file2 = NULL;
357 GFile *file3 = NULL;
358 gboolean has_prefix = FALSE;
359 char *relative_path = NULL;
360
361 if (use_uri)
362 {
363 file1 = g_file_new_for_uri (dir1);
364 file2 = g_file_new_for_uri (dir2);
365 }
366 else
367 {
368 file1 = g_file_new_for_path (dir1);
369 file2 = g_file_new_for_path (dir2);
370 }
371
372 g_assert (file1 != NULL);
373 g_assert (file2 != NULL);
374
375 has_prefix = g_file_has_prefix (file2, file1);
376 g_printerr ("%s %s\n", dir1, dir2);
377 g_assert (has_prefix == should_have_prefix);
378
379 relative_path = g_file_get_relative_path (file1, file2);
380 if (should_have_prefix)
381 {
382 g_assert (relative_path != NULL);
383
384 file3 = g_file_resolve_relative_path (file1, relative_path);
385 g_assert (g_file_equal (file2, file3) == TRUE);
386 }
387
388 if (file1)
389 g_object_unref (file1);
390 if (file2)
391 g_object_unref (file2);
392 if (file3)
393 g_object_unref (file3);
394
395 return relative_path;
396 }
397
398 static void
399 test_g_file_has_prefix (void)
400 {
401 /* TestPathsWithOper.equal represents here if the dir belongs to the directory structure */
402 const struct TestPathsWithOper dirs[] =
403 {
404 /* path1 equal uri path2 path3 */
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 {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
408 {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
409 {"/tmp/", FALSE, FALSE, "/something/", NULL},
410 {"/dir1/dir2", FALSE, FALSE, "/dir1/", NULL},
411 {"//dir1/new", TRUE, FALSE, "//dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
412 {"/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"},
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:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
416 {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
417 {"file:///tmp/", FALSE, TRUE, "file:///something/", NULL},
418 {"file:///dir1/dir2", FALSE, TRUE, "file:///dir1/", NULL},
419 {"file:////dir1/new", TRUE, TRUE, "file:////dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
420 {"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"},
421 #if 0
422 {"dav://www.gtk.org/plan/", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "meetings/20071218.txt"},
423 {"dav://www.gtk.org/plan/meetings", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "20071218.txt"},
424 #endif
425 };
426
427 guint i;
428 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
429 {
430 char *s = get_relative_path (dirs[i].use_uri, dirs[i].equal, dirs[i].path1, dirs[i].path2);
431 if (dirs[i].equal)
432 g_assert_cmpstr (s, ==, dirs[i].path3);
433 g_free (s);
434 }
435 }
436
437 static void
438 roundtrip_parent_child (const gboolean use_uri, const gboolean under_root_descending,
439 const char *path, const char *dir_holder)
440 {
441 GFile *files[6] = {NULL};
442 guint i;
443
444 if (use_uri)
445 {
446 files[0] = g_file_new_for_uri (path);
447 files[1] = g_file_new_for_uri (path);
448 }
449 else
450 {
451 files[0] = g_file_new_for_path (path);
452 files[1] = g_file_new_for_path (path);
453 }
454
455 g_assert (files[0] != NULL);
456 g_assert (files[1] != NULL);
457
458 files[2] = g_file_get_child (files[1], dir_holder);
459 g_assert (files[2] != NULL);
460
461 files[3] = g_file_get_parent (files[2]);
462 g_assert (files[3] != NULL);
463 g_assert (g_file_equal (files[3], files[0]) == TRUE);
464
465 files[4] = g_file_get_parent (files[3]);
466 /* Don't go lower beyond the root */
467 if (under_root_descending)
468 g_assert (files[4] == NULL);
469 else
470 {
471 g_assert (files[4] != NULL);
472
473 files[5] = g_file_get_child (files[4], dir_holder);
474 g_assert (files[5] != NULL);
475 g_assert (g_file_equal (files[5], files[0]) == TRUE);
476 }
477
478 for (i = 0; i < G_N_ELEMENTS (files); i++)
479 {
480 if (files[i])
481 g_object_unref (files[i]);
482 }
483 }
484
485 static void
486 test_g_file_get_parent_child (void)
487 {
488 const struct TestPathsWithOper paths[] =
489 {
490 /* path root_desc uri dir_holder */
491 {"/dir1/dir", FALSE, FALSE, "dir", NULL },
492 {"/dir", FALSE, FALSE, "dir", NULL },
493 {"/", TRUE, FALSE, "dir", NULL },
494 {"/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", NULL },
495 {"file:///dir1/dir", FALSE, TRUE, "dir", NULL },
496 {"file:///dir", FALSE, TRUE, "dir", NULL },
497 {"file:///", TRUE, TRUE, "dir", NULL },
498 {"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", NULL },
499 {"dav://www.gtk.org/plan/meetings", FALSE, TRUE, "meetings", NULL },
500 };
501
502 guint i;
503 for (i = 0; i < G_N_ELEMENTS (paths); i++)
504 roundtrip_parent_child (paths[i].use_uri, paths[i].equal, paths[i].path1, paths[i].path2);
505 }
506
507 int
508 main (int argc,
509 char *argv[])
510 {
511 g_test_init (&argc, &argv, NULL);
512
513
514 /* Testing whether g_file_new_for_path() or g_file_new_for_uri() always returns non-NULL result */
515 g_test_add_func ("/g-file/test_g_file_new_null", test_g_file_new_null);
516
517 /* Testing whether the g_file_new_for_path() correctly canonicalizes strings and two files equals (g_file_equal()) */
518 g_test_add_func ("/g-file/test_g_file_new_for_path", test_g_file_new_for_path);
519
520 /* Testing whether the g_file_new_for_uri() correctly canonicalizes strings and two files equals (g_file_equal()) */
521 g_test_add_func ("/g-file/test_g_file_new_for_uri", test_g_file_new_for_uri);
522
523 /* Testing g_file_dup() equals original file via g_file_equal() */
524 g_test_add_func ("/g-file/test_g_file_dup", test_g_file_dup);
525
526 /* Testing g_file_get_parse_name() to return correct UTF-8 string */
527 g_test_add_func ("/g-file/test_g_file_get_parse_name_utf8", test_g_file_get_parse_name_utf8);
528
529 /* Testing g_file_new_for_commandline_arg() for correct relavive path resolution and correct path/URI guess */
530 g_test_add_func ("/g-file/test_g_file_new_for_commandline_arg", test_g_file_new_for_commandline_arg);
531
532 /* Testing g_file_has_prefix(), g_file_get_relative_path() and g_file_resolve_relative_path() to return and process correct relative paths */
533 g_test_add_func ("/g-file/test_g_file_has_prefix", test_g_file_has_prefix);
534
535 /* Testing g_file_get_parent() and g_file_get_child() */
536 g_test_add_func ("/g-file/test_g_file_get_parent_child", test_g_file_get_parent_child);
537
538 return g_test_run();
539 }