Imported Upstream version 2.59.0
[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
31 /* Test our stdio wrappers here */
32 #define G_STDIO_NO_WRAP_ON_UNIX
33 #include <glib/gstdio.h>
34
35 #ifdef G_OS_UNIX
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <utime.h>
40 #endif
41 #include <fcntl.h>
42 #ifdef G_OS_WIN32
43 #include <windows.h>
44 #include <sys/utime.h>
45 #include <io.h>
46 #ifndef S_ISDIR
47 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
48 #endif
49 #ifndef F_OK
50 #define F_OK 0
51 #endif
52 #endif
53
54 #define S G_DIR_SEPARATOR_S
55
56 static void
57 check_string (gchar *str, const gchar *expected)
58 {
59   g_assert_nonnull (str);
60   g_assert_cmpstr (str, ==, expected);
61   g_free (str);
62 }
63
64 static void
65 test_build_path (void)
66 {
67 /*  check_string (g_build_path ("", NULL), "");*/
68   check_string (g_build_path ("", "", NULL), "");
69   check_string (g_build_path ("", "x", NULL), "x");
70   check_string (g_build_path ("", "x", "y",  NULL), "xy");
71   check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
72
73 /*  check_string (g_build_path (":", NULL), "");*/
74   check_string (g_build_path (":", ":", NULL), ":");
75   check_string (g_build_path (":", ":x", NULL), ":x");
76   check_string (g_build_path (":", "x:", NULL), "x:");
77   check_string (g_build_path (":", "", "x", NULL), "x");
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", "y",  NULL), "x:y");
86   check_string (g_build_path (":", ":x", "y", NULL), ":x:y");
87   check_string (g_build_path (":", "x", "y:", NULL), "x:y:");
88   check_string (g_build_path (":", ":x:", ":y:", NULL), ":x:y:");
89   check_string (g_build_path (":", ":x::", "::y:", NULL), ":x:y:");
90   check_string (g_build_path (":", "x", "","y",  NULL), "x:y");
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", "z", NULL), "x:y:z");
94   check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
95   check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
96
97 /*  check_string (g_build_path ("::", NULL), "");*/
98   check_string (g_build_path ("::", "::", NULL), "::");
99   check_string (g_build_path ("::", ":::", NULL), ":::");
100   check_string (g_build_path ("::", "::x", NULL), "::x");
101   check_string (g_build_path ("::", "x::", NULL), "x::");
102   check_string (g_build_path ("::", "", "x", NULL), "x");
103   check_string (g_build_path ("::", "", "::x", NULL), "::x");
104   check_string (g_build_path ("::", "::", "x", NULL), "::x");
105   check_string (g_build_path ("::", "::::", "x", NULL), "::::x");
106   check_string (g_build_path ("::", "x", "", NULL), "x");
107   check_string (g_build_path ("::", "x::", "", NULL), "x::");
108   check_string (g_build_path ("::", "x", "::", NULL), "x::");
109
110   /* This following is weird, but keeps the definition simple */
111   check_string (g_build_path ("::", "x", ":::", NULL), "x:::::");
112   check_string (g_build_path ("::", "x", "::::", NULL), "x::::");
113   check_string (g_build_path ("::", "x", "y",  NULL), "x::y");
114   check_string (g_build_path ("::", "::x", "y", NULL), "::x::y");
115   check_string (g_build_path ("::", "x", "y::", NULL), "x::y::");
116   check_string (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::");
117   check_string (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::");
118   check_string (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::");
119   check_string (g_build_path ("::", "x", "", "y",  NULL), "x::y");
120   check_string (g_build_path ("::", "x", "::", "y",  NULL), "x::y");
121   check_string (g_build_path ("::", "x", "::::", "y",  NULL), "x::y");
122   check_string (g_build_path ("::", "x", "y", "z", NULL), "x::y::z");
123   check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
124   check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
125   check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
126 }
127
128 static void
129 test_build_pathv (void)
130 {
131   gchar *args[10];
132
133   g_assert_null (g_build_pathv ("", NULL));
134   args[0] = NULL;
135   check_string (g_build_pathv ("", args), "");
136   args[0] = ""; args[1] = NULL;
137   check_string (g_build_pathv ("", args), "");
138   args[0] = "x"; args[1] = NULL;
139   check_string (g_build_pathv ("", args), "x");
140   args[0] = "x"; args[1] = "y"; args[2] = NULL;
141   check_string (g_build_pathv ("", args), "xy");
142   args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
143   check_string (g_build_pathv ("", args), "xyz");
144
145   args[0] = NULL;
146   check_string (g_build_pathv (":", args), "");
147   args[0] = ":"; args[1] = NULL;
148   check_string (g_build_pathv (":", args), ":");
149   args[0] = ":x"; args[1] = NULL;
150   check_string (g_build_pathv (":", args), ":x");
151   args[0] = "x:"; args[1] = NULL;
152   check_string (g_build_pathv (":", args), "x:");
153   args[0] = ""; args[1] = "x"; args[2] = NULL;
154   check_string (g_build_pathv (":", args), "x");
155   args[0] = ""; args[1] = ":x"; args[2] = NULL;
156   check_string (g_build_pathv (":", args), ":x");
157   args[0] = ":"; args[1] = "x"; args[2] = NULL;
158   check_string (g_build_pathv (":", args), ":x");
159   args[0] = "::"; args[1] = "x"; args[2] = NULL;
160   check_string (g_build_pathv (":", args), "::x");
161   args[0] = "x"; args[1] = ""; args[2] = NULL;
162   check_string (g_build_pathv (":", args), "x");
163   args[0] = "x:"; args[1] = ""; args[2] = NULL;
164   check_string (g_build_pathv (":", args), "x:");
165   args[0] = "x"; args[1] = ":"; args[2] = NULL;
166   check_string (g_build_pathv (":", args), "x:");
167   args[0] = "x"; args[1] = "::"; args[2] = NULL;
168   check_string (g_build_pathv (":", args), "x::");
169   args[0] = "x"; args[1] = "y"; args[2] = NULL;
170   check_string (g_build_pathv (":", args), "x:y");
171   args[0] = ":x"; args[1] = "y"; args[2] = NULL;
172   check_string (g_build_pathv (":", args), ":x:y");
173   args[0] = "x"; args[1] = "y:"; args[2] = NULL;
174   check_string (g_build_pathv (":", args), "x:y:");
175   args[0] = ":x:"; args[1] = ":y:"; args[2] = NULL;
176   check_string (g_build_pathv (":", args), ":x:y:");
177   args[0] = ":x::"; args[1] = "::y:"; args[2] = NULL;
178   check_string (g_build_pathv (":", args), ":x:y:");
179   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
180   check_string (g_build_pathv (":", args), "x:y");
181   args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
182   check_string (g_build_pathv (":", args), "x:y");
183   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
184   check_string (g_build_pathv (":", args), "x:y");
185   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
186   check_string (g_build_pathv (":", args), "x:y:z");
187   args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
188   check_string (g_build_pathv (":", args), ":x:y:z:");
189   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
190   check_string (g_build_pathv (":", args), "::x:y:z::");
191
192   args[0] = NULL;
193   check_string (g_build_pathv ("::", args), "");
194   args[0] = "::"; args[1] = NULL;
195   check_string (g_build_pathv ("::", args), "::");
196   args[0] = ":::"; args[1] = NULL;
197   check_string (g_build_pathv ("::", args), ":::");
198   args[0] = "::x"; args[1] = NULL;
199   check_string (g_build_pathv ("::", args), "::x");
200   args[0] = "x::"; args[1] = NULL;
201   check_string (g_build_pathv ("::", args), "x::");
202   args[0] = ""; args[1] = "x"; args[2] = NULL;
203   check_string (g_build_pathv ("::", args), "x");
204   args[0] = ""; args[1] = "::x"; args[2] = NULL;
205   check_string (g_build_pathv ("::", args), "::x");
206   args[0] = "::"; args[1] = "x"; args[2] = NULL;
207   check_string (g_build_pathv ("::", args), "::x");
208   args[0] = "::::"; args[1] = "x"; args[2] = NULL;
209   check_string (g_build_pathv ("::", args), "::::x");
210   args[0] = "x"; args[1] = ""; args[2] = NULL;
211   check_string (g_build_pathv ("::", args), "x");
212   args[0] = "x::"; args[1] = ""; args[2] = NULL;
213   check_string (g_build_pathv ("::", args), "x::");
214   args[0] = "x"; args[1] = "::"; args[2] = NULL;
215   check_string (g_build_pathv ("::", args), "x::");
216   /* This following is weird, but keeps the definition simple */
217   args[0] = "x"; args[1] = ":::"; args[2] = NULL;
218   check_string (g_build_pathv ("::", args), "x:::::");
219   args[0] = "x"; args[1] = "::::"; args[2] = NULL;
220   check_string (g_build_pathv ("::", args), "x::::");
221   args[0] = "x"; args[1] = "y"; args[2] = NULL;
222   check_string (g_build_pathv ("::", args), "x::y");
223   args[0] = "::x"; args[1] = "y"; args[2] = NULL;
224   check_string (g_build_pathv ("::", args), "::x::y");
225   args[0] = "x"; args[1] = "y::"; args[2] = NULL;
226   check_string (g_build_pathv ("::", args), "x::y::");
227   args[0] = "::x::"; args[1] = "::y::"; args[2] = NULL;
228   check_string (g_build_pathv ("::", args), "::x::y::");
229   args[0] = "::x:::"; args[1] = ":::y::"; args[2] = NULL;
230   check_string (g_build_pathv ("::", args), "::x::::y::");
231   args[0] = "::x::::"; args[1] = "::::y::"; args[2] = NULL;
232   check_string (g_build_pathv ("::", args), "::x::y::");
233   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
234   check_string (g_build_pathv ("::", args), "x::y");
235   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
236   check_string (g_build_pathv ("::", args), "x::y");
237   args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
238   check_string (g_build_pathv ("::", args), "x::y");
239   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
240   check_string (g_build_pathv ("::", args), "x::y::z");
241   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
242   check_string (g_build_pathv ("::", args), "::x::y::z::");
243   args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
244   check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
245   args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
246   check_string (g_build_pathv ("::", args), "::::x::y::z::::");
247 }
248
249 static void
250 test_build_filename (void)
251 {
252 /*  check_string (g_build_filename (NULL), "");*/
253   check_string (g_build_filename (S, NULL), S);
254   check_string (g_build_filename (S"x", NULL), S"x");
255   check_string (g_build_filename ("x"S, NULL), "x"S);
256   check_string (g_build_filename ("", "x", NULL), "x");
257   check_string (g_build_filename ("", S"x", NULL), S"x");
258   check_string (g_build_filename (S, "x", NULL), S"x");
259   check_string (g_build_filename (S S, "x", NULL), S S"x");
260   check_string (g_build_filename ("x", "", NULL), "x");
261   check_string (g_build_filename ("x"S, "", NULL), "x"S);
262   check_string (g_build_filename ("x", S, NULL), "x"S);
263   check_string (g_build_filename ("x", S S, NULL), "x"S S);
264   check_string (g_build_filename ("x", "y",  NULL), "x"S"y");
265   check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
266   check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
267   check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
268   check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
269   check_string (g_build_filename ("x", "", "y",  NULL), "x"S"y");
270   check_string (g_build_filename ("x", S, "y",  NULL), "x"S"y");
271   check_string (g_build_filename ("x", S S, "y",  NULL), "x"S"y");
272   check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
273   check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
274   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);
275
276 #ifdef G_OS_WIN32
277
278   /* Test also using the slash as file name separator */
279 #define Z "/"
280   /* check_string (g_build_filename (NULL), ""); */
281   check_string (g_build_filename (Z, NULL), Z);
282   check_string (g_build_filename (Z"x", NULL), Z"x");
283   check_string (g_build_filename ("x"Z, NULL), "x"Z);
284   check_string (g_build_filename ("", Z"x", NULL), Z"x");
285   check_string (g_build_filename ("", Z"x", NULL), Z"x");
286   check_string (g_build_filename (Z, "x", NULL), Z"x");
287   check_string (g_build_filename (Z Z, "x", NULL), Z Z"x");
288   check_string (g_build_filename (Z S, "x", NULL), Z S"x");
289   check_string (g_build_filename ("x"Z, "", NULL), "x"Z);
290   check_string (g_build_filename ("x"S"y", "z"Z"a", NULL), "x"S"y"S"z"Z"a");
291   check_string (g_build_filename ("x", Z, NULL), "x"Z);
292   check_string (g_build_filename ("x", Z Z, NULL), "x"Z Z);
293   check_string (g_build_filename ("x", S Z, NULL), "x"S Z);
294   check_string (g_build_filename (Z"x", "y", NULL), Z"x"Z"y");
295   check_string (g_build_filename ("x", "y"Z, NULL), "x"Z"y"Z);
296   check_string (g_build_filename (Z"x"Z, Z"y"Z, NULL), Z"x"Z"y"Z);
297   check_string (g_build_filename (Z"x"Z Z, Z Z"y"Z, NULL), Z"x"Z"y"Z);
298   check_string (g_build_filename ("x", Z, "y",  NULL), "x"Z"y");
299   check_string (g_build_filename ("x", Z Z, "y",  NULL), "x"Z"y");
300   check_string (g_build_filename ("x", Z S, "y",  NULL), "x"S"y");
301   check_string (g_build_filename ("x", S Z, "y",  NULL), "x"Z"y");
302   check_string (g_build_filename ("x", Z "y", "z", NULL), "x"Z"y"Z"z");
303   check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
304   check_string (g_build_filename ("x", S "y", "z", Z, "a", "b", NULL), "x"S"y"S"z"Z"a"Z"b");
305   check_string (g_build_filename (Z"x"Z, Z"y"Z, Z"z"Z, NULL), Z"x"Z"y"Z"z"Z);
306   check_string (g_build_filename (Z Z"x"Z Z, Z Z"y"Z Z, Z Z"z"Z Z, NULL), Z Z"x"Z"y"Z"z"Z Z);
307
308 #undef Z
309
310 #endif /* G_OS_WIN32 */
311
312 }
313
314 static void
315 test_build_filenamev (void)
316 {
317   gchar *args[10];
318
319   args[0] = NULL;
320   check_string (g_build_filenamev (args), "");
321   args[0] = S; args[1] = NULL;
322   check_string (g_build_filenamev (args), S);
323   args[0] = S"x"; args[1] = NULL;
324   check_string (g_build_filenamev (args), S"x");
325   args[0] = "x"S; args[1] = NULL;
326   check_string (g_build_filenamev (args), "x"S);
327   args[0] = ""; args[1] = "x"; args[2] = NULL;
328   check_string (g_build_filenamev (args), "x");
329   args[0] = ""; args[1] = S"x"; args[2] = NULL;
330   check_string (g_build_filenamev (args), S"x");
331   args[0] = S; args[1] = "x"; args[2] = NULL;
332   check_string (g_build_filenamev (args), S"x");
333   args[0] = S S; args[1] = "x"; args[2] = NULL;
334   check_string (g_build_filenamev (args), S S"x");
335   args[0] = "x"; args[1] = ""; args[2] = NULL;
336   check_string (g_build_filenamev (args), "x");
337   args[0] = "x"S; args[1] = ""; args[2] = NULL;
338   check_string (g_build_filenamev (args), "x"S);
339   args[0] = "x"; args[1] = S; args[2] = NULL;
340   check_string (g_build_filenamev (args), "x"S);
341   args[0] = "x"; args[1] = S S; args[2] = NULL;
342   check_string (g_build_filenamev (args), "x"S S);
343   args[0] = "x"; args[1] = "y"; args[2] = NULL;
344   check_string (g_build_filenamev (args), "x"S"y");
345   args[0] = S"x"; args[1] = "y"; args[2] = NULL;
346   check_string (g_build_filenamev (args), S"x"S"y");
347   args[0] = "x"; args[1] = "y"S; args[2] = NULL;
348   check_string (g_build_filenamev (args), "x"S"y"S);
349   args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
350   check_string (g_build_filenamev (args), S"x"S"y"S);
351   args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
352   check_string (g_build_filenamev (args), S"x"S"y"S);
353   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
354   check_string (g_build_filenamev (args), "x"S"y");
355   args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
356   check_string (g_build_filenamev (args), "x"S"y");
357   args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
358   check_string (g_build_filenamev (args), "x"S"y");
359   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
360   check_string (g_build_filenamev (args), "x"S"y"S"z");
361   args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
362   check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
363   args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
364   check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
365
366 #ifdef G_OS_WIN32
367
368   /* Test also using the slash as file name separator */
369 #define Z "/"
370   args[0] = NULL;
371   check_string (g_build_filenamev (args), "");
372   args[0] = Z; args[1] = NULL;
373   check_string (g_build_filenamev (args), Z);
374   args[0] = Z"x"; args[1] = NULL;
375   check_string (g_build_filenamev (args), Z"x");
376   args[0] = "x"Z; args[1] = NULL;
377   check_string (g_build_filenamev (args), "x"Z);
378   args[0] = ""; args[1] = Z"x"; args[2] = NULL;
379   check_string (g_build_filenamev (args), Z"x");
380   args[0] = ""; args[1] = Z"x"; args[2] = NULL;
381   check_string (g_build_filenamev (args), Z"x");
382   args[0] = Z; args[1] = "x"; args[2] = NULL;
383   check_string (g_build_filenamev (args), Z"x");
384   args[0] = Z Z; args[1] = "x"; args[2] = NULL;
385   check_string (g_build_filenamev (args), Z Z"x");
386   args[0] = Z S; args[1] = "x"; args[2] = NULL;
387   check_string (g_build_filenamev (args), Z S"x");
388   args[0] = "x"Z; args[1] = ""; args[2] = NULL;
389   check_string (g_build_filenamev (args), "x"Z);
390   args[0] = "x"S"y"; args[1] = "z"Z"a"; args[2] = NULL;
391   check_string (g_build_filenamev (args), "x"S"y"S"z"Z"a");
392   args[0] = "x"; args[1] = Z; args[2] = NULL;
393   check_string (g_build_filenamev (args), "x"Z);
394   args[0] = "x"; args[1] = Z Z; args[2] = NULL;
395   check_string (g_build_filenamev (args), "x"Z Z);
396   args[0] = "x"; args[1] = S Z; args[2] = NULL;
397   check_string (g_build_filenamev (args), "x"S Z);
398   args[0] = Z"x"; args[1] = "y"; args[2] = NULL;
399   check_string (g_build_filenamev (args), Z"x"Z"y");
400   args[0] = "x"; args[1] = "y"Z; args[2] = NULL;
401   check_string (g_build_filenamev (args), "x"Z"y"Z);
402   args[0] = Z"x"Z; args[1] = Z"y"Z; args[2] = NULL;
403   check_string (g_build_filenamev (args), Z"x"Z"y"Z);
404   args[0] = Z"x"Z Z; args[1] = Z Z"y"Z; args[2] = NULL;
405   check_string (g_build_filenamev (args), Z"x"Z"y"Z);
406   args[0] = "x"; args[1] = Z; args[2] = "y", args[3] = NULL;
407   check_string (g_build_filenamev (args), "x"Z"y");
408   args[0] = "x"; args[1] = Z Z; args[2] = "y", args[3] = NULL;
409   check_string (g_build_filenamev (args), "x"Z"y");
410   args[0] = "x"; args[1] = Z S; args[2] = "y", args[3] = NULL;
411   check_string (g_build_filenamev (args), "x"S"y");
412   args[0] = "x"; args[1] = S Z; args[2] = "y", args[3] = NULL;
413   check_string (g_build_filenamev (args), "x"Z"y");
414   args[0] = "x"; args[1] = Z "y"; args[2] = "z", args[3] = NULL;
415   check_string (g_build_filenamev (args), "x"Z"y"Z"z");
416   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
417   check_string (g_build_filenamev (args), "x"S"y"S"z");
418   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = Z;
419   args[4] = "a"; args[5] = "b"; args[6] = NULL;
420   check_string (g_build_filenamev (args), "x"S"y"S"z"Z"a"Z"b");
421   args[0] = Z"x"Z; args[1] = Z"y"Z; args[2] = Z"z"Z, args[3] = NULL;
422   check_string (g_build_filenamev (args), Z"x"Z"y"Z"z"Z);
423   args[0] = Z Z"x"Z Z; args[1] = Z Z"y"Z Z; args[2] = Z Z"z"Z Z, args[3] = NULL;
424   check_string (g_build_filenamev (args), Z Z"x"Z"y"Z"z"Z Z);
425
426 #undef Z
427
428 #endif /* G_OS_WIN32 */
429 }
430
431 #undef S
432
433 static void
434 test_mkdir_with_parents_1 (const gchar *base)
435 {
436   char *p0 = g_build_filename (base, "fum", NULL);
437   char *p1 = g_build_filename (p0, "tem", NULL);
438   char *p2 = g_build_filename (p1, "zap", NULL);
439   FILE *f;
440
441   g_remove (p2);
442   g_remove (p1);
443   g_remove (p0);
444
445   if (g_file_test (p0, G_FILE_TEST_EXISTS))
446     g_error ("failed, %s exists, cannot test g_mkdir_with_parents", p0);
447
448   if (g_file_test (p1, G_FILE_TEST_EXISTS))
449     g_error ("failed, %s exists, cannot test g_mkdir_with_parents", p1);
450
451   if (g_file_test (p2, G_FILE_TEST_EXISTS))
452     g_error ("failed, %s exists, cannot test g_mkdir_with_parents", p2);
453
454   if (g_mkdir_with_parents (p2, 0777) == -1)
455     {
456       int errsv = errno;
457       g_error ("failed, g_mkdir_with_parents(%s) failed: %s", p2, g_strerror (errsv));
458     }
459
460   if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
461     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory", p2, p2);
462
463   if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
464     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory", p2, p1);
465
466   if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
467     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory", p2, p0);
468
469   g_rmdir (p2);
470   if (g_file_test (p2, G_FILE_TEST_EXISTS))
471     g_error ("failed, did g_rmdir(%s), but %s is still there", p2, p2);
472
473   g_rmdir (p1);
474   if (g_file_test (p1, G_FILE_TEST_EXISTS))
475     g_error ("failed, did g_rmdir(%s), but %s is still there", p1, p1);
476
477   f = g_fopen (p1, "w");
478   if (f == NULL)
479     g_error ("failed, couldn't create file %s", p1);
480   fclose (f);
481
482   if (g_mkdir_with_parents (p1, 0666) == 0)
483     g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file", p1, p1);
484
485   if (g_mkdir_with_parents (p2, 0666) == 0)
486     g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file", p2, p1);
487
488   g_remove (p2);
489   g_remove (p1);
490   g_remove (p0);
491
492   g_free (p2);
493   g_free (p1);
494   g_free (p0);
495 }
496
497 static void
498 test_mkdir_with_parents (void)
499 {
500   gchar *cwd;
501   if (g_test_verbose())
502     g_printerr ("checking g_mkdir_with_parents() in subdir ./hum/");
503   test_mkdir_with_parents_1 ("hum");
504   g_remove ("hum");
505   if (g_test_verbose())
506     g_printerr ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
507   test_mkdir_with_parents_1 ("hii///haa/hee");
508   g_remove ("hii/haa/hee");
509   g_remove ("hii/haa");
510   g_remove ("hii");
511   cwd = g_get_current_dir ();
512   if (g_test_verbose())
513     g_printerr ("checking g_mkdir_with_parents() in cwd: %s", cwd);
514   test_mkdir_with_parents_1 (cwd);
515   g_free (cwd);
516
517   g_assert_cmpint (g_mkdir_with_parents (NULL, 0), ==, -1);
518   g_assert_cmpint (errno, ==, EINVAL);
519 }
520
521 static void
522 test_format_size_for_display (void)
523 {
524 #ifdef G_OS_WIN32
525   SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
526 #endif
527   /* nobody called setlocale(), so we should get "C" behaviour... */
528   check_string (g_format_size_for_display (0), "0 bytes");
529   check_string (g_format_size_for_display (1), "1 byte");
530   check_string (g_format_size_for_display (2), "2 bytes");
531   check_string (g_format_size_for_display (1024), "1.0 KB");
532   check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
533   check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
534   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
535   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
536   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
537
538   check_string (g_format_size (0), "0 bytes");
539   check_string (g_format_size (1), "1 byte");
540   check_string (g_format_size (2), "2 bytes");
541   check_string (g_format_size (1000ULL), "1.0 kB");
542   check_string (g_format_size (1000ULL * 1000), "1.0 MB");
543   check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
544   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
545   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
546   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
547
548   check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
549   check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
550   check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
551
552   check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
553   check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
554   check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
555   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
556   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
557   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
558
559   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
560   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
561   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
562
563
564   check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS), "0 bits");
565   check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS), "1 bit");
566   check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS), "2 bits");
567
568   check_string (g_format_size_full (2000ULL, G_FORMAT_SIZE_BITS), "2.0 kb");
569   check_string (g_format_size_full (2000ULL * 1000, G_FORMAT_SIZE_BITS), "2.0 Mb");
570   check_string (g_format_size_full (2000ULL * 1000 * 1000, G_FORMAT_SIZE_BITS), "2.0 Gb");
571   check_string (g_format_size_full (2000ULL * 1000 * 1000 * 1000, G_FORMAT_SIZE_BITS), "2.0 Tb");
572   check_string (g_format_size_full (2000ULL * 1000 * 1000 * 1000 * 1000, G_FORMAT_SIZE_BITS), "2.0 Pb");
573   check_string (g_format_size_full (2000ULL * 1000 * 1000 * 1000 * 1000 * 1000, G_FORMAT_SIZE_BITS), "2.0 Eb");
574
575   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS), "238.5 Mb");
576   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_LONG_FORMAT), "238.5 Mb (238472938 bits)");
577
578
579   check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "0 bits");
580   check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "1 bit");
581   check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2 bits");
582
583   check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2.0 Kib");
584   check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2.0 Mib");
585   check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2.0 Gib");
586   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2.0 Tib");
587   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2.0 Pib");
588   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2.0 Eib");
589
590   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "227.4 Mib");
591   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_LONG_FORMAT), "227.4 Mib (238472938 bits)");
592 }
593
594 static void
595 test_file_errors (void)
596 {
597 #ifdef EEXIST
598   g_assert_cmpint (g_file_error_from_errno (EEXIST), ==, G_FILE_ERROR_EXIST);
599 #endif
600 #ifdef EISDIR
601   g_assert_cmpint (g_file_error_from_errno (EISDIR), ==, G_FILE_ERROR_ISDIR);
602 #endif
603 #ifdef EACCES
604   g_assert_cmpint (g_file_error_from_errno (EACCES), ==, G_FILE_ERROR_ACCES);
605 #endif
606 #ifdef ENAMETOOLONG
607   g_assert_cmpint (g_file_error_from_errno (ENAMETOOLONG), ==, G_FILE_ERROR_NAMETOOLONG);
608 #endif
609 #ifdef ENOENT
610   g_assert_cmpint (g_file_error_from_errno (ENOENT), ==, G_FILE_ERROR_NOENT);
611 #endif
612 #ifdef ENOTDIR
613   g_assert_cmpint (g_file_error_from_errno (ENOTDIR), ==, G_FILE_ERROR_NOTDIR);
614 #endif
615 #ifdef ENXIO
616   g_assert_cmpint (g_file_error_from_errno (ENXIO), ==, G_FILE_ERROR_NXIO);
617 #endif
618 #ifdef ENODEV
619   g_assert_cmpint (g_file_error_from_errno (ENODEV), ==, G_FILE_ERROR_NODEV);
620 #endif
621 #ifdef EROFS
622   g_assert_cmpint (g_file_error_from_errno (EROFS), ==, G_FILE_ERROR_ROFS);
623 #endif
624 #ifdef ETXTBSY
625   g_assert_cmpint (g_file_error_from_errno (ETXTBSY), ==, G_FILE_ERROR_TXTBSY);
626 #endif
627 #ifdef EFAULT
628   g_assert_cmpint (g_file_error_from_errno (EFAULT), ==, G_FILE_ERROR_FAULT);
629 #endif
630 #ifdef ELOOP
631   g_assert_cmpint (g_file_error_from_errno (ELOOP), ==, G_FILE_ERROR_LOOP);
632 #endif
633 #ifdef ENOSPC
634   g_assert_cmpint (g_file_error_from_errno (ENOSPC), ==, G_FILE_ERROR_NOSPC);
635 #endif
636 #ifdef ENOMEM
637   g_assert_cmpint (g_file_error_from_errno (ENOMEM), ==, G_FILE_ERROR_NOMEM);
638 #endif
639 #ifdef EMFILE
640   g_assert_cmpint (g_file_error_from_errno (EMFILE), ==, G_FILE_ERROR_MFILE);
641 #endif
642 #ifdef ENFILE
643   g_assert_cmpint (g_file_error_from_errno (ENFILE), ==, G_FILE_ERROR_NFILE);
644 #endif
645 #ifdef EBADF
646   g_assert_cmpint (g_file_error_from_errno (EBADF), ==, G_FILE_ERROR_BADF);
647 #endif
648 #ifdef EINVAL
649   g_assert_cmpint (g_file_error_from_errno (EINVAL), ==, G_FILE_ERROR_INVAL);
650 #endif
651 #ifdef EPIPE
652   g_assert_cmpint (g_file_error_from_errno (EPIPE), ==, G_FILE_ERROR_PIPE);
653 #endif
654 #ifdef EAGAIN
655   g_assert_cmpint (g_file_error_from_errno (EAGAIN), ==, G_FILE_ERROR_AGAIN);
656 #endif
657 #ifdef EINTR
658   g_assert_cmpint (g_file_error_from_errno (EINTR), ==, G_FILE_ERROR_INTR);
659 #endif
660 #ifdef EIO
661   g_assert_cmpint (g_file_error_from_errno (EIO), ==, G_FILE_ERROR_IO);
662 #endif
663 #ifdef EPERM
664   g_assert_cmpint (g_file_error_from_errno (EPERM), ==, G_FILE_ERROR_PERM);
665 #endif
666 #ifdef ENOSYS
667   g_assert_cmpint (g_file_error_from_errno (ENOSYS), ==, G_FILE_ERROR_NOSYS);
668 #endif
669 }
670
671 static void
672 test_basename (void)
673 {
674   gchar *b;
675
676   b = g_path_get_basename ("");
677   g_assert_cmpstr (b, ==, ".");
678   g_free (b);
679
680   b = g_path_get_basename ("///");
681   g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
682   g_free (b);
683
684   b = g_path_get_basename ("/a/b/c/d");
685   g_assert_cmpstr (b, ==, "d");
686   g_free (b);
687 }
688
689 static void
690 test_dir_make_tmp (void)
691 {
692   gchar *name;
693   GError *error = NULL;
694   gint ret;
695
696   name = g_dir_make_tmp ("testXXXXXXtest", &error);
697   g_assert_no_error (error);
698   g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
699   ret = g_rmdir (name);
700   g_assert_cmpint (ret, ==, 0);
701   g_free (name);
702
703   name = g_dir_make_tmp (NULL, &error);
704   g_assert_no_error (error);
705   g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
706   ret = g_rmdir (name);
707   g_assert_cmpint (ret, ==, 0);
708   g_free (name);
709
710   name = g_dir_make_tmp ("test/XXXXXX", &error);
711   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
712   g_clear_error (&error);
713   g_assert_null (name);
714
715   name = g_dir_make_tmp ("XXXXxX", &error);
716   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
717   g_clear_error (&error);
718   g_assert_null (name);
719 }
720
721 static void
722 test_file_open_tmp (void)
723 {
724   gchar *name = NULL;
725   GError *error = NULL;
726   gint fd;
727
728   fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
729   g_assert_cmpint (fd, !=, -1);
730   g_assert_no_error (error);
731   g_assert_nonnull (name);
732   unlink (name);
733   g_free (name);
734   close (fd);
735
736   fd = g_file_open_tmp (NULL, &name, &error);
737   g_assert_cmpint (fd, !=, -1);
738   g_assert_no_error (error);
739   g_assert_nonnull (name);
740   g_unlink (name);
741   g_free (name);
742   close (fd);
743
744   name = NULL;
745   fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
746   g_assert_cmpint (fd, ==, -1);
747   g_assert_null (name);
748   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
749   g_clear_error (&error);
750
751   fd = g_file_open_tmp ("XXXXxX", &name, &error);
752   g_assert_cmpint (fd, ==, -1);
753   g_assert_null (name);
754   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
755   g_clear_error (&error);
756 }
757
758 static void
759 test_mkstemp (void)
760 {
761   gchar *name;
762   gint fd;
763
764   name = g_strdup ("testXXXXXXtest"),
765   fd = g_mkstemp (name);
766   g_assert_cmpint (fd, !=, -1);
767   g_assert_null (strstr (name, "XXXXXX"));
768   unlink (name);
769   close (fd);
770   g_free (name);
771
772   name = g_strdup ("testYYYYYYtest"),
773   fd = g_mkstemp (name);
774   g_assert_cmpint (fd, ==, -1);
775   g_free (name);
776 }
777
778 static void
779 test_mkdtemp (void)
780 {
781   gchar *name;
782   gchar *ret;
783
784   name = g_strdup ("testXXXXXXtest"),
785   ret = g_mkdtemp (name);
786   g_assert (ret == name);
787   g_assert_null (strstr (name, "XXXXXX"));
788   g_rmdir (name);
789   g_free (name);
790
791   name = g_strdup ("testYYYYYYtest"),
792   ret = g_mkdtemp (name);
793   g_assert_null (ret);
794   g_free (name);
795 }
796
797 static void
798 test_set_contents (void)
799 {
800   GError *error = NULL;
801   gint fd;
802   gchar *name;
803   gchar *buf;
804   gsize len;
805   gboolean ret;
806
807   fd = g_file_open_tmp (NULL, &name, &error);
808   g_assert_no_error (error);
809   write (fd, "a", 1);
810   close (fd);
811
812   ret = g_file_get_contents (name, &buf, &len, &error);
813   g_assert_true (ret);
814   g_assert_no_error (error);
815   g_assert_cmpstr (buf, ==, "a");
816   g_free (buf);
817
818   ret = g_file_set_contents (name, "b", 1, &error);
819   g_assert_true (ret);
820   g_assert_no_error (error);
821
822   ret = g_file_get_contents (name, &buf, &len, &error);
823   g_assert_true (ret);
824   g_assert_no_error (error);
825   g_assert_cmpstr (buf, ==, "b");
826   g_free (buf);
827
828   g_remove (name);
829   g_free (name);
830 }
831
832 static void
833 test_read_link (void)
834 {
835 #ifdef HAVE_READLINK
836 #ifdef G_OS_UNIX
837   int ret;
838   const gchar *oldpath;
839   gchar *cwd;
840   gchar *newpath;
841   gchar *badpath;
842   gchar *path;
843   GError *error = NULL;
844
845   cwd = g_get_current_dir ();
846
847   oldpath = g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL);
848   newpath = g_build_filename (cwd, "page-of-junk", NULL);
849   badpath = g_build_filename (cwd, "4097-random-bytes", NULL);
850   remove (newpath);
851   ret = symlink (oldpath, newpath);
852   g_assert_cmpint (ret, ==, 0);
853   path = g_file_read_link (newpath, &error);
854   g_assert_no_error (error);
855   g_assert_cmpstr (path, ==, oldpath);
856   g_free (path);
857
858   remove (newpath);
859   ret = symlink (badpath, newpath);
860   g_assert_cmpint (ret, ==, 0);
861   path = g_file_read_link (newpath, &error);
862   g_assert_no_error (error);
863   g_assert_cmpstr (path, ==, badpath);
864   g_free (path);
865
866   path = g_file_read_link (oldpath, &error);
867   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
868   g_assert_null (path);
869   g_error_free (error);
870
871   remove (newpath);
872   g_free (cwd);
873   g_free (newpath);
874   g_free (badpath);
875
876 #endif
877 #else
878   g_test_skip ("Symbolic links not supported");
879 #endif
880 }
881
882 static void
883 test_stdio_wrappers (void)
884 {
885   GStatBuf buf;
886   gchar *cwd, *path;
887   gint ret;
888   struct utimbuf ut;
889   GError *error = NULL;
890   GStatBuf path_statbuf, cwd_statbuf;
891
892   /* The permissions tests here don’t work when running as root. */
893 #ifdef G_OS_UNIX
894   if (getuid () == 0 || geteuid () == 0)
895     {
896       g_test_skip ("File permissions tests cannot be run as root");
897       return;
898     }
899 #endif
900
901   g_remove ("mkdir-test/test-create");
902   ret = g_rmdir ("mkdir-test");
903   g_assert (ret == 0 || errno == ENOENT);
904
905   ret = g_stat ("mkdir-test", &buf);
906   g_assert_cmpint (ret, ==, -1);
907   ret = g_mkdir ("mkdir-test", 0666);
908   g_assert_cmpint (ret, ==, 0);
909   ret = g_stat ("mkdir-test", &buf);
910   g_assert_cmpint (ret, ==, 0);
911   g_assert_cmpint (S_ISDIR (buf.st_mode), !=, 0);
912
913   cwd = g_get_current_dir ();
914   path = g_build_filename (cwd, "mkdir-test", NULL);
915   g_free (cwd);
916 #ifndef G_OS_WIN32
917   /* 0666 on directories means nothing to Windows, it only obeys ACLs */
918   ret = g_chdir (path);
919   g_assert_cmpint (errno, ==, EACCES);
920   g_assert_cmpint (ret, ==, -1);
921 #endif
922   ret = g_chmod (path, 0777);
923   g_assert_cmpint (ret, ==, 0);
924   ret = g_chdir (path);
925   g_assert_cmpint (ret, ==, 0);
926   cwd = g_get_current_dir ();
927   /* We essentially want to check that cwd == path, but we can’t compare the
928    * paths directly since the tests might be running under a symlink (for
929    * example, /tmp is sometimes a symlink). Compare the inode numbers instead. */
930   g_assert_cmpint (g_stat (cwd, &cwd_statbuf), ==, 0);
931   g_assert_cmpint (g_stat (path, &path_statbuf), ==, 0);
932   g_assert_true (cwd_statbuf.st_dev == path_statbuf.st_dev &&
933                  cwd_statbuf.st_ino == path_statbuf.st_ino);
934   g_free (cwd);
935   g_free (path);
936
937   ret = g_creat ("test-creat", 0555);
938   g_close (ret, &error);
939   g_assert_no_error (error);
940
941   ret = g_access ("test-creat", F_OK);
942   g_assert_cmpint (ret, ==, 0);
943
944   ret = g_rename ("test-creat", "test-create");
945   g_assert_cmpint (ret, ==, 0);
946
947   ret = g_open ("test-create", O_RDONLY, 0666);
948   g_close (ret, &error);
949   g_assert_no_error (error);
950
951 #ifdef G_OS_WIN32
952   /* On Windows the 5 permission bit results in a read-only file
953    * that cannot be modified in any way (attribute changes included).
954    * Remove the read-only attribute via chmod().
955    */
956   ret = g_chmod ("test-create", 0666);
957   g_assert_cmpint (ret, ==, 0);
958 #endif
959
960   ut.actime = ut.modtime = (time_t)0;
961   ret = g_utime ("test-create", &ut);
962   g_assert_cmpint (ret, ==, 0);
963
964   ret = g_lstat ("test-create", &buf);
965   g_assert_cmpint (ret, ==, 0);
966   g_assert_cmpint (buf.st_atime, ==, (time_t)0);
967   g_assert_cmpint (buf.st_mtime, ==, (time_t)0);
968
969   g_chdir ("..");
970   g_remove ("mkdir-test/test-create");
971   g_rmdir ("mkdir-test");
972 }
973
974 /* Win32 does not support "wb+", but g_fopen() should automatically
975  * translate this mode to its alias "w+b".
976  * Also check various other file open modes for correct support accross
977  * platforms.
978  * See: https://gitlab.gnome.org/GNOME/glib/merge_requests/119
979  */
980 static void
981 test_fopen_modes (void)
982 {
983   char        *path = g_build_filename ("temp-fopen", NULL);
984   gsize        i;
985   const gchar *modes[] =
986     {
987       "w",
988       "r",
989       "a",
990       "w+",
991       "r+",
992       "a+",
993       "wb",
994       "rb",
995       "ab",
996       "w+b",
997       "r+b",
998       "a+b",
999       "wb+",
1000       "rb+",
1001       "ab+"
1002     };
1003
1004   g_test_bug ("119");
1005
1006   if (g_file_test (path, G_FILE_TEST_EXISTS))
1007     g_error ("failed, %s exists, cannot test g_fopen()", path);
1008
1009   for (i = 0; i < G_N_ELEMENTS (modes); i++)
1010     {
1011       FILE *f;
1012
1013       g_test_message ("Testing fopen() mode '%s'", modes[i]);
1014
1015       f = g_fopen (path, modes[i]);
1016       g_assert_nonnull (f);
1017       fclose (f);
1018     }
1019
1020   g_remove (path);
1021   g_free (path);
1022 }
1023
1024 #ifdef G_OS_WIN32
1025 #include "../gstdio-private.c"
1026
1027 static int
1028 g_wcscmp0 (const gunichar2 *str1,
1029            const gunichar2 *str2)
1030 {
1031   if (!str1)
1032     return -(str1 != str2);
1033   if (!str2)
1034     return str1 != str2;
1035   return wcscmp (str1, str2);
1036 }
1037
1038 #define g_assert_cmpwcs(s1, cmp, s2, s1u8, s2u8) \
1039 G_STMT_START { \
1040   const gunichar2 *__s1 = (s1), *__s2 = (s2); \
1041   if (g_wcscmp0 (__s1, __s2) cmp 0) ; else \
1042     g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
1043                                 #s1u8 " " #cmp " " #s2u8, s1u8, #cmp, s2u8); \
1044 } G_STMT_END
1045
1046 static void
1047 test_win32_pathstrip (void)
1048 {
1049   gunichar2 *buf;
1050   gsize i;
1051 #define IDENTITY_TEST(x) { x, x, FALSE }
1052   struct
1053   {
1054     gunichar2 *in;
1055     gunichar2 *out;
1056     gboolean   result;
1057   } testcases[] = {
1058     IDENTITY_TEST (L"\\\\?\\V"),
1059     IDENTITY_TEST (L"\\\\?\\Vo"),
1060     IDENTITY_TEST (L"\\\\?\\Volume{0700f3d3-6d24-11e3-8b2f-806e6f6e6963}\\"),
1061     IDENTITY_TEST (L"\\??\\V"),
1062     IDENTITY_TEST (L"\\??\\Vo"),
1063     IDENTITY_TEST (L"\\??\\Volume{0700f3d3-6d24-11e3-8b2f-806e6f6e6963}\\"),
1064     IDENTITY_TEST (L"\\\\?\\\x0441:\\"),
1065     IDENTITY_TEST (L"\\??\\\x0441:\\"),
1066     IDENTITY_TEST (L"a:\\"),
1067     IDENTITY_TEST (L"a:\\b\\c"),
1068     IDENTITY_TEST (L"x"),
1069 #undef IDENTITY_TEST
1070     {
1071       L"\\\\?\\c:\\",
1072              L"c:\\",
1073       TRUE,
1074     },
1075     {
1076       L"\\\\?\\C:\\",
1077              L"C:\\",
1078       TRUE,
1079     },
1080     {
1081       L"\\\\?\\c:\\",
1082              L"c:\\",
1083       TRUE,
1084     },
1085     {
1086       L"\\\\?\\C:\\",
1087              L"C:\\",
1088       TRUE,
1089     },
1090     {
1091       L"\\\\?\\C:\\",
1092              L"C:\\",
1093       TRUE,
1094     },
1095     { 0, }
1096   };
1097
1098   for (i = 0; testcases[i].in; i++)
1099     {
1100       gsize str_len = wcslen (testcases[i].in) + 1;
1101       gchar *in_u8 = g_utf16_to_utf8 (testcases[i].in, -1, NULL, NULL, NULL);
1102       gchar *out_u8 = g_utf16_to_utf8 (testcases[i].out, -1, NULL, NULL, NULL);
1103
1104       g_assert_nonnull (in_u8);
1105       g_assert_nonnull (out_u8);
1106
1107       buf = g_new0 (gunichar2, str_len);
1108       memcpy (buf, testcases[i].in, str_len * sizeof (gunichar2));
1109       _g_win32_strip_extended_ntobjm_prefix (buf, &str_len);
1110       g_assert_cmpwcs (buf, ==, testcases[i].out, in_u8, out_u8);
1111       g_free (buf);
1112       g_free (in_u8);
1113       g_free (out_u8);
1114     }
1115   /* Check for correct behaviour on non-NUL-terminated strings */
1116   for (i = 0; testcases[i].in; i++)
1117     {
1118       gsize str_len = wcslen (testcases[i].in) + 1;
1119       wchar_t old_endchar;
1120       gchar *in_u8 = g_utf16_to_utf8 (testcases[i].in, -1, NULL, NULL, NULL);
1121       gchar *out_u8 = g_utf16_to_utf8 (testcases[i].out, -1, NULL, NULL, NULL);
1122
1123       g_assert_nonnull (in_u8);
1124       g_assert_nonnull (out_u8);
1125
1126       buf = g_new0 (gunichar2, str_len);
1127       memcpy (buf, testcases[i].in, (str_len) * sizeof (gunichar2));
1128
1129       old_endchar = buf[wcslen (testcases[i].out)];
1130       str_len -= 1;
1131
1132       if (testcases[i].result)
1133         {
1134           /* Given "\\\\?\\C:\\" (len 7, unterminated),
1135            * we should get "C:\\" (len 3, unterminated).
1136            * Put a character different from "\\" (4-th character of the buffer)
1137            * at the end of the unterminated source buffer, into a position
1138            * where NUL-terminator would normally be. Then later test that 4-th character
1139            * in the buffer is still the old "\\".
1140            * After that terminate the string and use normal g_wcscmp0().
1141            */
1142           buf[str_len] = old_endchar - 1;
1143         }
1144
1145       _g_win32_strip_extended_ntobjm_prefix (buf, &str_len);
1146       g_assert_cmpuint (old_endchar, ==, buf[wcslen (testcases[i].out)]);
1147       buf[str_len] = L'\0';
1148       g_assert_cmpwcs (buf, ==, testcases[i].out, in_u8, out_u8);
1149       g_free (buf);
1150       g_free (in_u8);
1151       g_free (out_u8);
1152     }
1153 }
1154
1155 #endif
1156
1157 int
1158 main (int   argc,
1159       char *argv[])
1160 {
1161   g_setenv ("LC_ALL", "C", TRUE);
1162   g_test_init (&argc, &argv, NULL);
1163
1164   g_test_bug_base ("https://gitlab.gnome.org/GNOME/glib/merge_requests/");
1165
1166 #ifdef G_OS_WIN32
1167   g_test_add_func ("/fileutils/stdio-win32-pathstrip", test_win32_pathstrip);
1168 #endif
1169   g_test_add_func ("/fileutils/build-path", test_build_path);
1170   g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
1171   g_test_add_func ("/fileutils/build-filename", test_build_filename);
1172   g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
1173   g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
1174   g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
1175   g_test_add_func ("/fileutils/errors", test_file_errors);
1176   g_test_add_func ("/fileutils/basename", test_basename);
1177   g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
1178   g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
1179   g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
1180   g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
1181   g_test_add_func ("/fileutils/set-contents", test_set_contents);
1182   g_test_add_func ("/fileutils/read-link", test_read_link);
1183   g_test_add_func ("/fileutils/stdio-wrappers", test_stdio_wrappers);
1184   g_test_add_func ("/fileutils/fopen-modes", test_fopen_modes);
1185
1186   return g_test_run ();
1187 }