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