Improve file utils test coverage
[platform/upstream/glib.git] / glib / tests / fileutils.c
1 /* Unit tests for gfileutils
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This work is provided "as is"; redistribution and modification
5  * in whole or in part, in any medium, physical or electronic is
6  * permitted without restriction.
7  *
8  * This work is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * In no event shall the authors or contributors be liable for any
13  * direct, indirect, incidental, special, exemplary, or consequential
14  * damages (including, but not limited to, procurement of substitute
15  * goods or services; loss of use, data, or profits; or business
16  * interruption) however caused and on any theory of liability, whether
17  * in contract, strict liability, or tort (including negligence or
18  * otherwise) arising in any way out of the use of this software, even
19  * if advised of the possibility of such damage.
20  */
21
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25
26 /* We are testing some deprecated APIs here */
27 #define GLIB_DISABLE_DEPRECATION_WARNINGS
28
29 #include <glib.h>
30 #include <glib/gstdio.h>
31
32 #define S G_DIR_SEPARATOR_S
33
34 static void
35 check_string (gchar *str, gchar *expected)
36 {
37   g_assert (str != NULL);
38   g_assert_cmpstr (str, ==, expected);
39   g_free (str);
40 }
41
42 static void
43 test_build_path (void)
44 {
45 /*  check_string (g_build_path ("", NULL), "");*/
46   check_string (g_build_path ("", "", NULL), "");
47   check_string (g_build_path ("", "x", NULL), "x");
48   check_string (g_build_path ("", "x", "y",  NULL), "xy");
49   check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
50
51 /*  check_string (g_build_path (":", NULL), "");*/
52   check_string (g_build_path (":", ":", NULL), ":");
53   check_string (g_build_path (":", ":x", NULL), ":x");
54   check_string (g_build_path (":", "x:", NULL), "x:");
55   check_string (g_build_path (":", "", "x", NULL), "x");
56   check_string (g_build_path (":", "", ":x", NULL), ":x");
57   check_string (g_build_path (":", ":", "x", NULL), ":x");
58   check_string (g_build_path (":", "::", "x", NULL), "::x");
59   check_string (g_build_path (":", "x", "", NULL), "x");
60   check_string (g_build_path (":", "x:", "", NULL), "x:");
61   check_string (g_build_path (":", "x", ":", NULL), "x:");
62   check_string (g_build_path (":", "x", "::", NULL), "x::");
63   check_string (g_build_path (":", "x", "y",  NULL), "x:y");
64   check_string (g_build_path (":", ":x", "y", NULL), ":x:y");
65   check_string (g_build_path (":", "x", "y:", NULL), "x:y:");
66   check_string (g_build_path (":", ":x:", ":y:", NULL), ":x:y:");
67   check_string (g_build_path (":", ":x::", "::y:", NULL), ":x:y:");
68   check_string (g_build_path (":", "x", "","y",  NULL), "x:y");
69   check_string (g_build_path (":", "x", ":", "y",  NULL), "x:y");
70   check_string (g_build_path (":", "x", "::", "y",  NULL), "x:y");
71   check_string (g_build_path (":", "x", "y", "z", NULL), "x:y:z");
72   check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
73   check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
74
75 /*  check_string (g_build_path ("::", NULL), "");*/
76   check_string (g_build_path ("::", "::", NULL), "::");
77   check_string (g_build_path ("::", ":::", NULL), ":::");
78   check_string (g_build_path ("::", "::x", NULL), "::x");
79   check_string (g_build_path ("::", "x::", NULL), "x::");
80   check_string (g_build_path ("::", "", "x", NULL), "x");
81   check_string (g_build_path ("::", "", "::x", NULL), "::x");
82   check_string (g_build_path ("::", "::", "x", NULL), "::x");
83   check_string (g_build_path ("::", "::::", "x", NULL), "::::x");
84   check_string (g_build_path ("::", "x", "", NULL), "x");
85   check_string (g_build_path ("::", "x::", "", NULL), "x::");
86   check_string (g_build_path ("::", "x", "::", NULL), "x::");
87
88   /* This following is weird, but keeps the definition simple */
89   check_string (g_build_path ("::", "x", ":::", NULL), "x:::::");
90   check_string (g_build_path ("::", "x", "::::", NULL), "x::::");
91   check_string (g_build_path ("::", "x", "y",  NULL), "x::y");
92   check_string (g_build_path ("::", "::x", "y", NULL), "::x::y");
93   check_string (g_build_path ("::", "x", "y::", NULL), "x::y::");
94   check_string (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::");
95   check_string (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::");
96   check_string (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::");
97   check_string (g_build_path ("::", "x", "", "y",  NULL), "x::y");
98   check_string (g_build_path ("::", "x", "::", "y",  NULL), "x::y");
99   check_string (g_build_path ("::", "x", "::::", "y",  NULL), "x::y");
100   check_string (g_build_path ("::", "x", "y", "z", NULL), "x::y::z");
101   check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
102   check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
103   check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
104 }
105
106 static void
107 test_build_pathv (void)
108 {
109   gchar *args[10];
110
111   g_assert (g_build_pathv ("", NULL) == NULL);
112   args[0] = NULL;
113   check_string (g_build_pathv ("", args), "");
114   args[0] = ""; args[1] = NULL;
115   check_string (g_build_pathv ("", args), "");
116   args[0] = "x"; args[1] = NULL;
117   check_string (g_build_pathv ("", args), "x");
118   args[0] = "x"; args[1] = "y"; args[2] = NULL;
119   check_string (g_build_pathv ("", args), "xy");
120   args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
121   check_string (g_build_pathv ("", args), "xyz");
122
123   args[0] = NULL;
124   check_string (g_build_pathv (":", args), "");
125   args[0] = ":"; args[1] = NULL;
126   check_string (g_build_pathv (":", args), ":");
127   args[0] = ":x"; args[1] = NULL;
128   check_string (g_build_pathv (":", args), ":x");
129   args[0] = "x:"; args[1] = NULL;
130   check_string (g_build_pathv (":", args), "x:");
131   args[0] = ""; args[1] = "x"; args[2] = NULL;
132   check_string (g_build_pathv (":", args), "x");
133   args[0] = ""; args[1] = ":x"; args[2] = NULL;
134   check_string (g_build_pathv (":", args), ":x");
135   args[0] = ":"; args[1] = "x"; args[2] = NULL;
136   check_string (g_build_pathv (":", args), ":x");
137   args[0] = "::"; args[1] = "x"; args[2] = NULL;
138   check_string (g_build_pathv (":", args), "::x");
139   args[0] = "x"; args[1] = ""; args[2] = NULL;
140   check_string (g_build_pathv (":", args), "x");
141   args[0] = "x:"; args[1] = ""; args[2] = NULL;
142   check_string (g_build_pathv (":", args), "x:");
143   args[0] = "x"; args[1] = ":"; args[2] = NULL;
144   check_string (g_build_pathv (":", args), "x:");
145   args[0] = "x"; args[1] = "::"; args[2] = NULL;
146   check_string (g_build_pathv (":", args), "x::");
147   args[0] = "x"; args[1] = "y"; args[2] = NULL;
148   check_string (g_build_pathv (":", args), "x:y");
149   args[0] = ":x"; args[1] = "y"; args[2] = NULL;
150   check_string (g_build_pathv (":", args), ":x:y");
151   args[0] = "x"; args[1] = "y:"; args[2] = NULL;
152   check_string (g_build_pathv (":", args), "x:y:");
153   args[0] = ":x:"; args[1] = ":y:"; args[2] = NULL;
154   check_string (g_build_pathv (":", args), ":x:y:");
155   args[0] = ":x::"; args[1] = "::y:"; args[2] = NULL;
156   check_string (g_build_pathv (":", args), ":x:y:");
157   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
158   check_string (g_build_pathv (":", args), "x:y");
159   args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
160   check_string (g_build_pathv (":", args), "x:y");
161   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
162   check_string (g_build_pathv (":", args), "x:y");
163   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
164   check_string (g_build_pathv (":", args), "x:y:z");
165   args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
166   check_string (g_build_pathv (":", args), ":x:y:z:");
167   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
168   check_string (g_build_pathv (":", args), "::x:y:z::");
169
170   args[0] = NULL;
171   check_string (g_build_pathv ("::", args), "");
172   args[0] = "::"; args[1] = NULL;
173   check_string (g_build_pathv ("::", args), "::");
174   args[0] = ":::"; args[1] = NULL;
175   check_string (g_build_pathv ("::", args), ":::");
176   args[0] = "::x"; args[1] = NULL;
177   check_string (g_build_pathv ("::", args), "::x");
178   args[0] = "x::"; args[1] = NULL;
179   check_string (g_build_pathv ("::", args), "x::");
180   args[0] = ""; args[1] = "x"; args[2] = NULL;
181   check_string (g_build_pathv ("::", args), "x");
182   args[0] = ""; args[1] = "::x"; args[2] = NULL;
183   check_string (g_build_pathv ("::", args), "::x");
184   args[0] = "::"; args[1] = "x"; args[2] = NULL;
185   check_string (g_build_pathv ("::", args), "::x");
186   args[0] = "::::"; args[1] = "x"; args[2] = NULL;
187   check_string (g_build_pathv ("::", args), "::::x");
188   args[0] = "x"; args[1] = ""; args[2] = NULL;
189   check_string (g_build_pathv ("::", args), "x");
190   args[0] = "x::"; args[1] = ""; args[2] = NULL;
191   check_string (g_build_pathv ("::", args), "x::");
192   args[0] = "x"; args[1] = "::"; args[2] = NULL;
193   check_string (g_build_pathv ("::", args), "x::");
194   /* This following is weird, but keeps the definition simple */
195   args[0] = "x"; args[1] = ":::"; args[2] = NULL;
196   check_string (g_build_pathv ("::", args), "x:::::");
197   args[0] = "x"; args[1] = "::::"; args[2] = NULL;
198   check_string (g_build_pathv ("::", args), "x::::");
199   args[0] = "x"; args[1] = "y"; args[2] = NULL;
200   check_string (g_build_pathv ("::", args), "x::y");
201   args[0] = "::x"; args[1] = "y"; args[2] = NULL;
202   check_string (g_build_pathv ("::", args), "::x::y");
203   args[0] = "x"; args[1] = "y::"; args[2] = NULL;
204   check_string (g_build_pathv ("::", args), "x::y::");
205   args[0] = "::x::"; args[1] = "::y::"; args[2] = NULL;
206   check_string (g_build_pathv ("::", args), "::x::y::");
207   args[0] = "::x:::"; args[1] = ":::y::"; args[2] = NULL;
208   check_string (g_build_pathv ("::", args), "::x::::y::");
209   args[0] = "::x::::"; args[1] = "::::y::"; args[2] = NULL;
210   check_string (g_build_pathv ("::", args), "::x::y::");
211   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
212   check_string (g_build_pathv ("::", args), "x::y");
213   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
214   check_string (g_build_pathv ("::", args), "x::y");
215   args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
216   check_string (g_build_pathv ("::", args), "x::y");
217   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
218   check_string (g_build_pathv ("::", args), "x::y::z");
219   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
220   check_string (g_build_pathv ("::", args), "::x::y::z::");
221   args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
222   check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
223   args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
224   check_string (g_build_pathv ("::", args), "::::x::y::z::::");
225 }
226
227 static void
228 test_build_filename (void)
229 {
230 /*  check_string (g_build_filename (NULL), "");*/
231   check_string (g_build_filename (S, NULL), S);
232   check_string (g_build_filename (S"x", NULL), S"x");
233   check_string (g_build_filename ("x"S, NULL), "x"S);
234   check_string (g_build_filename ("", "x", NULL), "x");
235   check_string (g_build_filename ("", S"x", NULL), S"x");
236   check_string (g_build_filename (S, "x", NULL), S"x");
237   check_string (g_build_filename (S S, "x", NULL), S S"x");
238   check_string (g_build_filename ("x", "", NULL), "x");
239   check_string (g_build_filename ("x"S, "", NULL), "x"S);
240   check_string (g_build_filename ("x", S, NULL), "x"S);
241   check_string (g_build_filename ("x", S S, NULL), "x"S S);
242   check_string (g_build_filename ("x", "y",  NULL), "x"S"y");
243   check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
244   check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
245   check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
246   check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
247   check_string (g_build_filename ("x", "", "y",  NULL), "x"S"y");
248   check_string (g_build_filename ("x", S, "y",  NULL), "x"S"y");
249   check_string (g_build_filename ("x", S S, "y",  NULL), "x"S"y");
250   check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
251   check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
252   check_string (g_build_filename (S S"x"S S, S S"y"S S, S S"z"S S, NULL), S S"x"S"y"S"z"S S);
253
254 #ifdef G_OS_WIN32
255
256   /* Test also using the slash as file name separator */
257 #define U "/"
258   check_string (g_build_filename (NULL), "");
259   check_string (g_build_filename (U, NULL), U);
260   check_string (g_build_filename (U"x", NULL), U"x");
261   check_string (g_build_filename ("x"U, NULL), "x"U);
262   check_string (g_build_filename ("", U"x", NULL), U"x");
263   check_string (g_build_filename ("", U"x", NULL), U"x");
264   check_string (g_build_filename (U, "x", NULL), U"x");
265   check_string (g_build_filename (U U, "x", NULL), U U"x");
266   check_string (g_build_filename (U S, "x", NULL), U S"x");
267   check_string (g_build_filename ("x"U, "", NULL), "x"U);
268   check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
269   check_string (g_build_filename ("x", U, NULL), "x"U);
270   check_string (g_build_filename ("x", U U, NULL), "x"U U);
271   check_string (g_build_filename ("x", S U, NULL), "x"S U);
272   check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
273   check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
274   check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
275   check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
276   check_string (g_build_filename ("x", U, "y",  NULL), "x"U"y");
277   check_string (g_build_filename ("x", U U, "y",  NULL), "x"U"y");
278   check_string (g_build_filename ("x", U S, "y",  NULL), "x"S"y");
279   check_string (g_build_filename ("x", S U, "y",  NULL), "x"U"y");
280   check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"z");
281   check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
282   check_string (g_build_filename ("x", S "y", "z", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
283   check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
284   check_string (g_build_filename (U U"x"U U, U U"y"U U, U U"z"U U, NULL), U U"x"U"y"U"z"U U);
285
286 #undef U
287
288 #endif /* G_OS_WIN32 */
289
290 }
291
292 static void
293 test_build_filenamev (void)
294 {
295   gchar *args[10];
296
297   args[0] = NULL;
298   check_string (g_build_filenamev (args), "");
299   args[0] = S; args[1] = NULL;
300   check_string (g_build_filenamev (args), S);
301   args[0] = S"x"; args[1] = NULL;
302   check_string (g_build_filenamev (args), S"x");
303   args[0] = "x"S; args[1] = NULL;
304   check_string (g_build_filenamev (args), "x"S);
305   args[0] = ""; args[1] = "x"; args[2] = NULL;
306   check_string (g_build_filenamev (args), "x");
307   args[0] = ""; args[1] = S"x"; args[2] = NULL;
308   check_string (g_build_filenamev (args), S"x");
309   args[0] = S; args[1] = "x"; args[2] = NULL;
310   check_string (g_build_filenamev (args), S"x");
311   args[0] = S S; args[1] = "x"; args[2] = NULL;
312   check_string (g_build_filenamev (args), S S"x");
313   args[0] = "x"; args[1] = ""; args[2] = NULL;
314   check_string (g_build_filenamev (args), "x");
315   args[0] = "x"S; args[1] = ""; args[2] = NULL;
316   check_string (g_build_filenamev (args), "x"S);
317   args[0] = "x"; args[1] = S; args[2] = NULL;
318   check_string (g_build_filenamev (args), "x"S);
319   args[0] = "x"; args[1] = S S; args[2] = NULL;
320   check_string (g_build_filenamev (args), "x"S S);
321   args[0] = "x"; args[1] = "y"; args[2] = NULL;
322   check_string (g_build_filenamev (args), "x"S"y");
323   args[0] = S"x"; args[1] = "y"; args[2] = NULL;
324   check_string (g_build_filenamev (args), S"x"S"y");
325   args[0] = "x"; args[1] = "y"S; args[2] = NULL;
326   check_string (g_build_filenamev (args), "x"S"y"S);
327   args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
328   check_string (g_build_filenamev (args), S"x"S"y"S);
329   args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
330   check_string (g_build_filenamev (args), S"x"S"y"S);
331   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
332   check_string (g_build_filenamev (args), "x"S"y");
333   args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
334   check_string (g_build_filenamev (args), "x"S"y");
335   args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
336   check_string (g_build_filenamev (args), "x"S"y");
337   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
338   check_string (g_build_filenamev (args), "x"S"y"S"z");
339   args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
340   check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
341   args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
342   check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
343
344 #ifdef G_OS_WIN32
345
346   /* Test also using the slash as file name separator */
347 #define U "/"
348   args[0] = NULL;
349   check_string (g_build_filenamev (args), "");
350   args[0] = U; args[1] = NULL;
351   check_string (g_build_filenamev (args), U);
352   args[0] = U"x"; args[1] = NULL;
353   check_string (g_build_filenamev (args), U"x");
354   args[0] = "x"U; args[1] = NULL;
355   check_string (g_build_filenamev (args), "x"U);
356   args[0] = ""; args[1] = U"x"; args[2] = NULL;
357   check_string (g_build_filenamev (args), U"x");
358   args[0] = ""; args[1] = U"x"; args[2] = NULL;
359   check_string (g_build_filenamev (args), U"x");
360   args[0] = U; args[1] = "x"; args[2] = NULL;
361   check_string (g_build_filenamev (args), U"x");
362   args[0] = U U; args[1] = "x"; args[2] = NULL;
363   check_string (g_build_filenamev (args), U U"x");
364   args[0] = U S; args[1] = "x"; args[2] = NULL;
365   check_string (g_build_filenamev (args), U S"x");
366   args[0] = "x"U; args[1] = ""; args[2] = NULL;
367   check_string (g_build_filenamev (args), "x"U);
368   args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
369   check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
370   args[0] = "x"; args[1] = U; args[2] = NULL;
371   check_string (g_build_filenamev (args), "x"U);
372   args[0] = "x"; args[1] = U U; args[2] = NULL;
373   check_string (g_build_filenamev (args), "x"U U);
374   args[0] = "x"; args[1] = S U; args[2] = NULL;
375   check_string (g_build_filenamev (args), "x"S U);
376   args[0] = U"x"; args[1] = "y"; args[2] = NULL;
377   check_string (g_build_filenamev (args), U"x"U"y");
378   args[0] = "x"; args[1] = "y"U; args[2] = NULL;
379   check_string (g_build_filenamev (args), "x"U"y"U);
380   args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
381   check_string (g_build_filenamev (args), U"x"U"y"U);
382   args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
383   check_string (g_build_filenamev (args), U"x"U"y"U);
384   args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
385   check_string (g_build_filenamev (args), "x"U"y");
386   args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
387   check_string (g_build_filenamev (args), "x"U"y");
388   args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
389   check_string (g_build_filenamev (args), "x"S"y");
390   args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
391   check_string (g_build_filenamev (args), "x"U"y");
392   args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
393   check_string (g_build_filenamev (args), "x"U"y"U"z");
394   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
395   check_string (g_build_filenamev (args), "x"S"y"S"z");
396   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = U;
397   args[4] = "a"; args[5] = "b"; args[6] = NULL;
398   check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
399   args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
400   check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
401   args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
402   check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
403
404 #undef U
405
406 #endif /* G_OS_WIN32 */
407 }
408
409 #undef S
410
411 static void
412 test_mkdir_with_parents_1 (const gchar *base)
413 {
414   char *p0 = g_build_filename (base, "fum", NULL);
415   char *p1 = g_build_filename (p0, "tem", NULL);
416   char *p2 = g_build_filename (p1, "zap", NULL);
417   FILE *f;
418
419   g_remove (p2);
420   g_remove (p1);
421   g_remove (p0);
422
423   if (g_file_test (p0, G_FILE_TEST_EXISTS))
424     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
425
426   if (g_file_test (p1, G_FILE_TEST_EXISTS))
427     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
428
429   if (g_file_test (p2, G_FILE_TEST_EXISTS))
430     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
431
432   if (g_mkdir_with_parents (p2, 0777) == -1)
433     g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
434
435   if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
436     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
437
438   if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
439     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
440
441   if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
442     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
443
444   g_rmdir (p2);
445   if (g_file_test (p2, G_FILE_TEST_EXISTS))
446     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
447
448   g_rmdir (p1);
449   if (g_file_test (p1, G_FILE_TEST_EXISTS))
450     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
451
452   f = g_fopen (p1, "w");
453   if (f == NULL)
454     g_error ("failed, couldn't create file %s\n", p1);
455   fclose (f);
456
457   if (g_mkdir_with_parents (p1, 0666) == 0)
458     g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
459
460   if (g_mkdir_with_parents (p2, 0666) == 0)
461     g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
462
463   g_remove (p2);
464   g_remove (p1);
465   g_remove (p0);
466
467   g_free (p2);
468   g_free (p1);
469   g_free (p0);
470 }
471
472 static void
473 test_mkdir_with_parents (void)
474 {
475   gchar *cwd;
476   if (g_test_verbose())
477     g_print ("checking g_mkdir_with_parents() in subdir ./hum/");
478   test_mkdir_with_parents_1 ("hum");
479   g_remove ("hum");
480   if (g_test_verbose())
481     g_print ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
482   test_mkdir_with_parents_1 ("hii///haa/hee");
483   g_remove ("hii/haa/hee");
484   g_remove ("hii/haa");
485   g_remove ("hii");
486   cwd = g_get_current_dir ();
487   if (g_test_verbose())
488     g_print ("checking g_mkdir_with_parents() in cwd: %s", cwd);
489   test_mkdir_with_parents_1 (cwd);
490   g_free (cwd);
491
492   g_assert (g_mkdir_with_parents (NULL, 0) == -1);
493   g_assert (errno == EINVAL);
494 }
495
496 static void
497 test_format_size_for_display (void)
498 {
499   /* nobody called setlocale(), so we should get "C" behaviour... */
500   check_string (g_format_size_for_display (0), "0 bytes");
501   check_string (g_format_size_for_display (1), "1 byte");
502   check_string (g_format_size_for_display (2), "2 bytes");
503   check_string (g_format_size_for_display (1024), "1.0 KB");
504   check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
505   check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
506   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
507   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
508   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
509
510   check_string (g_format_size (0), "0 bytes");
511   check_string (g_format_size (1), "1 byte");
512   check_string (g_format_size (2), "2 bytes");
513   check_string (g_format_size (1000ULL), "1.0 kB");
514   check_string (g_format_size (1000ULL * 1000), "1.0 MB");
515   check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
516   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
517   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
518   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
519
520   check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
521   check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
522   check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
523
524   check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
525   check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
526   check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
527   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
528   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
529   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
530
531   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
532   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
533   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
534 }
535
536 static void
537 test_file_errors (void)
538 {
539 #ifdef EEXIST
540   g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
541 #endif
542 #ifdef EISDIR
543   g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
544 #endif
545 #ifdef EACCES
546   g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
547 #endif
548 #ifdef ENAMETOOLONG
549   g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
550 #endif
551 #ifdef ENOENT
552   g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
553 #endif
554 #ifdef ENOTDIR
555   g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
556 #endif
557 #ifdef ENXIO
558   g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
559 #endif
560 #ifdef ENODEV
561   g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
562 #endif
563 #ifdef EROFS
564   g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
565 #endif
566 #ifdef ETXTBSY
567   g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
568 #endif
569 #ifdef EFAULT
570   g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
571 #endif
572 #ifdef ELOOP
573   g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
574 #endif
575 #ifdef ENOSPC
576   g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
577 #endif
578 #ifdef ENOMEM
579   g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
580 #endif
581 #ifdef EMFILE
582   g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
583 #endif
584 #ifdef ENFILE
585   g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
586 #endif
587 #ifdef EBADF
588   g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
589 #endif
590 #ifdef EINVAL
591   g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
592 #endif
593 #ifdef EPIPE
594   g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
595 #endif
596 #ifdef EAGAIN
597   g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
598 #endif
599 #ifdef EINTR
600   g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
601 #endif
602 #ifdef EIO
603   g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
604 #endif
605 #ifdef EPERM
606   g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
607 #endif
608 #ifdef ENOSYS
609   g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
610 #endif
611 }
612
613 static void
614 test_basename (void)
615 {
616   gchar *b;
617
618   b = g_path_get_basename ("");
619   g_assert_cmpstr (b, ==, ".");
620   g_free (b);
621
622   b = g_path_get_basename ("///");
623   g_assert_cmpstr (b, ==, "/");
624   g_free (b);
625
626   b = g_path_get_basename ("/a/b/c/d");
627   g_assert_cmpstr (b, ==, "d");
628   g_free (b);
629 }
630
631 static void
632 test_dir_make_tmp (void)
633 {
634   gchar *name;
635   GError *error = NULL;
636
637   name = g_dir_make_tmp ("testXXXXXXtest", &error);
638   g_assert_no_error (error);
639   g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
640   g_assert (g_rmdir (name) == 0);
641   g_free (name);
642
643   name = g_dir_make_tmp (NULL, &error);
644   g_assert_no_error (error);
645   g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
646   g_assert (g_rmdir (name) == 0);
647   g_free (name);
648
649   name = g_dir_make_tmp ("test/XXXXXX", &error);
650   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
651   g_clear_error (&error);
652   g_assert (name == NULL);
653
654   name = g_dir_make_tmp ("XXXXxX", &error);
655   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
656   g_clear_error (&error);
657   g_assert (name == NULL);
658 }
659
660 static void
661 test_file_open_tmp (void)
662 {
663   gchar *name = NULL;
664   GError *error = NULL;
665   gint fd;
666
667   fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
668   g_assert (fd != -1);
669   g_assert_no_error (error);
670   g_assert (name != NULL);
671   unlink (name);
672   close (fd);
673
674   fd = g_file_open_tmp (NULL, &name, &error);
675   g_assert (fd != -1);
676   g_assert_no_error (error);
677   g_assert (name != NULL);
678   g_unlink (name);
679   close (fd);
680
681   name = NULL;
682   fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
683   g_assert (fd == -1);
684   g_assert (name == NULL);
685   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
686   g_clear_error (&error);
687
688   fd = g_file_open_tmp ("XXXXxX", &name, &error);
689   g_assert (fd == -1);
690   g_assert (name == NULL);
691   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
692   g_clear_error (&error);
693 }
694
695 static void
696 test_mkstemp (void)
697 {
698   gchar *name;
699   gint fd;
700
701   name = g_strdup ("testXXXXXXtest"),
702   fd = g_mkstemp (name);
703   g_assert (fd != -1);
704   g_assert (strstr (name, "XXXXXX") == NULL);
705   unlink (name);
706   close (fd);
707   g_free (name);
708
709   name = g_strdup ("testYYYYYYtest"),
710   fd = g_mkstemp (name);
711   g_assert (fd == -1);
712   g_free (name);
713 }
714
715 static void
716 test_mkdtemp (void)
717 {
718   gchar *name;
719   gchar *ret;
720
721   name = g_strdup ("testXXXXXXtest"),
722   ret = g_mkdtemp (name);
723   g_assert (ret == name);
724   g_assert (strstr (name, "XXXXXX") == NULL);
725   g_rmdir (name);
726   g_free (name);
727
728   name = g_strdup ("testYYYYYYtest"),
729   ret = g_mkdtemp (name);
730   g_assert (ret == NULL);
731   g_free (name);
732 }
733
734 static void
735 test_set_contents (void)
736 {
737   GError *error = NULL;
738   gint fd;
739   gchar *name;
740   gchar *buf;
741   gsize len;
742   gboolean ret;
743
744   fd = g_file_open_tmp (NULL, &name, &error);
745   g_assert_no_error (error);
746   write (fd, "a", 1);
747   close (fd);
748
749   ret = g_file_get_contents (name, &buf, &len, &error);
750   g_assert (ret);
751   g_assert_no_error (error);
752   g_assert_cmpstr (buf, ==, "a");
753   g_free (buf);
754
755   ret = g_file_set_contents (name, "b", 1, &error);
756   g_assert (ret);
757   g_assert_no_error (error);
758
759   ret = g_file_get_contents (name, &buf, &len, &error);
760   g_assert (ret);
761   g_assert_no_error (error);
762   g_assert_cmpstr (buf, ==, "b");
763   g_free (buf);
764
765   g_remove (name);
766   g_free (name);
767 }
768
769 int
770 main (int   argc,
771       char *argv[])
772 {
773   g_test_init (&argc, &argv, NULL);
774
775   g_test_add_func ("/fileutils/build-path", test_build_path);
776   g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
777   g_test_add_func ("/fileutils/build-filename", test_build_filename);
778   g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
779   g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
780   g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
781   g_test_add_func ("/fileutils/errors", test_file_errors);
782   g_test_add_func ("/fileutils/basename", test_basename);
783   g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
784   g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
785   g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
786   g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
787   g_test_add_func ("/fileutils/set-contents", test_set_contents);
788
789   return g_test_run ();
790 }