69ab077f1fdb12461b50250883f387d91d474212
[platform/upstream/glib.git] / gio / tests / g-file.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2008 Red Hat, Inc.
3  * Authors: Tomas Bzatek <tbzatek@redhat.com>
4  *
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.
8  *
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.
12  *
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.
21  */
22 #include <glib/gtestutils.h>
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 struct TestPathsWithOper {
29   const char *path1;
30   gboolean equal;
31   gboolean use_uri;
32   const char *path2;
33   const char *path3;
34 };
35
36
37
38 /* TODO:
39  *   - test on Windows
40  * 
41  **/
42
43 static void
44 test_g_file_new_null (void)
45 {
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
51   };
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
57   };
58   
59   GFile *file = NULL;
60   
61   int i = 0;
62   while (paths[i])
63     {
64       file = g_file_new_for_path (paths[i++]);
65       g_assert (file != NULL);
66       g_object_unref (file);
67     }
68   
69   i = 0;
70   while (uris[i])
71     {
72       file = g_file_new_for_uri (paths[i++]);
73       g_assert (file != NULL);
74       g_object_unref(file);
75     }
76 }
77
78
79
80 static gboolean
81 compare_two_files (const gboolean use_uri, const char *path1, const char *path2)
82 {
83   GFile *file1 = NULL;
84   GFile *file2 = NULL;
85   gboolean equal;
86   
87   if (use_uri)
88     {
89       file1 = g_file_new_for_uri (path1);
90       file2 = g_file_new_for_uri (path2);
91     }
92   else
93     {
94       file1 = g_file_new_for_path (path1);
95       file2 = g_file_new_for_path (path2);
96     }
97
98   g_assert (file1 != NULL);
99   g_assert (file2 != NULL);
100   
101   equal = g_file_equal (file1, file2);
102   
103   g_object_unref (file1);
104   g_object_unref (file2);
105   
106   return equal;
107 }
108
109 static void
110 test_g_file_new_for_path (void)
111 {
112   const struct TestPathsWithOper cmp_paths[] =
113     {
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/"}
128     };
129
130   int i;
131   for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++)
132     {
133       gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2);
134       g_assert_cmpint (equal, ==, cmp_paths[i].equal);
135     }
136 }
137
138
139
140 static void
141 test_g_file_new_for_uri (void)
142 {
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/"}
157   };
158   
159   int i;
160   for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++)
161     {
162       gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2);
163       g_assert_cmpint (equal, ==, cmp_uris[i].equal);
164     }
165 }
166
167
168
169 static gboolean
170 dup_equals (const gboolean use_uri, const char *path)
171 {
172   GFile *file1 = NULL;
173   GFile *file2 = NULL;
174   gboolean equal;
175   
176   if (use_uri) 
177     file1 = g_file_new_for_uri (path);
178   else
179     file1 = g_file_new_for_path (path);
180         
181   g_assert (file1 != NULL);
182   
183   file2 = g_file_dup (file1);
184   
185   g_assert (file2 != NULL);
186   
187   equal = g_file_equal (file1, file2);
188   
189   g_object_unref (file1);
190   g_object_unref (file2);
191         
192   return equal;
193 }
194
195 static void
196 test_g_file_dup (void)
197 {
198   const struct TestPathsWithOper dup_paths[] =
199     {
200       {"/", 0, FALSE, ""},
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, ""},
205     };
206   
207   int i;
208   for (i = 0; i < G_N_ELEMENTS (dup_paths); i++)
209     {
210       gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1);
211       g_assert (equal == TRUE);
212     }
213 }
214
215
216
217 static gboolean
218 parse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name)
219 {
220   GFile *file1 = NULL;
221   GFile *file2 = NULL;
222   char *parsed_name;
223   gboolean is_utf8_valid;
224   gboolean equal;
225   
226   if (use_uri)
227     file1 = g_file_new_for_uri (path);
228   else
229     file1 = g_file_new_for_path (path);
230         
231   g_assert (file1 != NULL);
232
233   parsed_name = g_file_get_parse_name (file1);
234   
235   g_assert (parsed_name != NULL);
236   
237   /* UTF-8 validation */
238   is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL);
239   g_assert (is_utf8_valid == TRUE);
240
241   if (result_parse_name)
242     g_assert_cmpstr (parsed_name, ==, result_parse_name);
243   
244   file2 = g_file_parse_name (parsed_name);
245   
246   g_assert (file2 != NULL);
247
248   equal = g_file_equal (file1, file2);
249         
250   g_object_unref (file1);
251   g_object_unref (file2);
252   
253   g_free (parsed_name);
254   
255   return equal;
256 }
257
258 static void
259 test_g_file_get_parse_name_utf8 (void)
260 {
261   const struct TestPathsWithOper strings[] =
262     {
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"},
268     };
269
270   int i;
271   for (i = 0; i < G_N_ELEMENTS (strings); i++)
272     {
273       gboolean equal = parse_check_utf8 (strings[i].use_uri, strings[i].path1, strings[i].path2);
274       g_assert (equal == TRUE);
275     }
276 }
277
278 static char *
279 resolve_arg (const gboolean is_uri_only, const char *arg)
280 {
281   GFile *file1 = NULL;
282   char *uri = NULL;
283   char *path = NULL;
284   char *s = NULL;
285   
286   file1 = g_file_new_for_commandline_arg (arg);
287   g_assert (file1 != NULL);
288         
289   /*  Test if we get URI string */
290   uri = g_file_get_uri (file1);
291   g_assert (uri != NULL);
292         
293   /*  Test if we get correct value of the local path */
294   path = g_file_get_path (file1);
295   if (is_uri_only) 
296     g_assert (path == NULL);
297   else
298     g_assert (g_path_is_absolute (path) == TRUE);
299
300   /*  Get the URI scheme and compare it with expected one */
301   s = g_file_get_uri_scheme (file1);
302         
303   g_object_unref (file1);
304   g_free (uri);
305   g_free (path);
306   
307   return s;
308 }
309
310 static void
311 test_g_file_new_for_commandline_arg (void)
312 {
313   /*  TestPathsWithOper.use_uri represents IsURIOnly here */
314   const struct TestPathsWithOper arg_data[] =
315     {
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"},
323     };
324   GFile *file;
325   char *resolved;
326   char *cwd;
327   int i;
328   
329   for (i = 0; i < G_N_ELEMENTS (arg_data); i++)
330     {
331       char *s = resolve_arg (arg_data[i].use_uri, arg_data[i].path1);
332       g_assert_cmpstr (s, ==, arg_data[i].path2);
333       g_free (s);
334     }
335   
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);
342   g_free (resolved);
343   g_free (cwd);
344 }
345
346 static char*
347 get_relative_path (const gboolean use_uri, const gboolean should_contain_file, const char *dir1, const char *dir2)
348 {
349   GFile *file1 = NULL;
350   GFile *file2 = NULL;
351   GFile *file3 = NULL;
352   gboolean contains_file = FALSE;
353   char *relative_path = NULL;
354   
355   if (use_uri)
356     {
357       file1 = g_file_new_for_uri (dir1);
358       file2 = g_file_new_for_uri (dir2);
359     }
360   else
361     {
362       file1 = g_file_new_for_path (dir1);
363       file2 = g_file_new_for_path (dir2);
364     }
365   
366   g_assert (file1 != NULL);
367   g_assert (file2 != NULL);
368   
369   contains_file = g_file_contains_file (file1, file2);
370   g_assert (contains_file == should_contain_file);
371
372   relative_path = g_file_get_relative_path (file1, file2);
373   if (should_contain_file)
374     {
375       g_assert (relative_path != NULL);
376       
377       file3 = g_file_resolve_relative_path (file1, relative_path);
378       g_assert (g_file_equal (file2, file3) == TRUE);
379     }
380         
381   if (file1)
382     g_object_unref (file1);
383   if (file2)
384     g_object_unref (file2);
385   if (file3)
386     g_object_unref (file3);
387
388   return relative_path;
389 }
390
391 static void
392 test_g_file_contains_file (void)
393 {
394   /*  TestPathsWithOper.equal represents here if the dir belongs to the directory structure  */
395   const struct TestPathsWithOper dirs[] =
396     {
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"},
416     };
417   
418   int i;
419   for (i = 0; i < G_N_ELEMENTS (dirs); i++)
420     {
421       char *s = get_relative_path (dirs[i].use_uri, dirs[i].equal, dirs[i].path1, dirs[i].path2);
422       if (dirs[i].equal) 
423         g_assert_cmpstr (s, ==, dirs[i].path3);
424       g_free (s);
425     }
426 }
427
428 static void
429 roundtrip_parent_child (const gboolean use_uri, const gboolean under_root_descending,
430                         const char *path, const char *dir_holder)
431 {
432   GFile *files[6] = {0};
433   int i;
434   
435   if (use_uri)
436     {
437       files[0] = g_file_new_for_uri (path);
438       files[1] = g_file_new_for_uri (path);
439     }
440   else
441     {
442       files[0] = g_file_new_for_path (path);
443       files[1] = g_file_new_for_path (path);
444     }
445
446   g_assert (files[0] != NULL);
447   g_assert (files[1] != NULL);
448   
449   files[2] = g_file_get_child (files[1], dir_holder); 
450   g_assert (files[2] != NULL);
451   
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);
455   
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);
460   else
461     {
462       g_assert (files[4] != NULL);
463       
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);
467     }
468   
469   for (i = 0; i < G_N_ELEMENTS (files); i++)
470     {
471       if (files[i])
472         g_object_unref (files[i]);
473     }
474 }
475
476 static void
477 test_g_file_get_parent_child (void)
478 {
479   const struct TestPathsWithOper paths[] =
480     {
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"},
491     };
492
493   int i;
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);
496 }
497
498 int
499 main (int   argc,
500       char *argv[])
501 {
502   g_type_init ();
503   g_test_init (&argc, &argv, NULL);
504   
505   
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);
508   
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);
511
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);
514
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);
517
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);
520   
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);
523   
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);
526   
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);
529   
530   return g_test_run();
531 }