Add some more cases to the app-id unit tests
[glib.git] / glib / tests / fileutils.c
blobd218e78a0848eb030e9935728853ae5f7f52f527
1 /* Unit tests for gfileutils
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
22 #include "config.h"
23 #include <string.h>
24 #include <errno.h>
26 /* We are testing some deprecated APIs here */
27 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 #include <glib.h>
31 /* Test our stdio wrappers here */
32 #define G_STDIO_NO_WRAP_ON_UNIX
33 #include <glib/gstdio.h>
35 #ifdef G_OS_UNIX
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #endif
40 #include <fcntl.h>
41 #include <utime.h>
42 #ifdef G_OS_WIN32
43 #include <windows.h>
44 #endif
46 #define S G_DIR_SEPARATOR_S
48 static void
49 check_string (gchar *str, const gchar *expected)
51 g_assert (str != NULL);
52 g_assert_cmpstr (str, ==, expected);
53 g_free (str);
56 static void
57 test_build_path (void)
59 /* check_string (g_build_path ("", NULL), "");*/
60 check_string (g_build_path ("", "", NULL), "");
61 check_string (g_build_path ("", "x", NULL), "x");
62 check_string (g_build_path ("", "x", "y", NULL), "xy");
63 check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
65 /* check_string (g_build_path (":", NULL), "");*/
66 check_string (g_build_path (":", ":", NULL), ":");
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", NULL), ":x");
71 check_string (g_build_path (":", ":", "x", NULL), ":x");
72 check_string (g_build_path (":", "::", "x", NULL), "::x");
73 check_string (g_build_path (":", "x", "", NULL), "x");
74 check_string (g_build_path (":", "x:", "", NULL), "x:");
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", "y", NULL), "x:y");
78 check_string (g_build_path (":", ":x", "y", NULL), ":x:y");
79 check_string (g_build_path (":", "x", "y:", NULL), "x:y:");
80 check_string (g_build_path (":", ":x:", ":y:", NULL), ":x:y:");
81 check_string (g_build_path (":", ":x::", "::y:", NULL), ":x:y:");
82 check_string (g_build_path (":", "x", "","y", NULL), "x:y");
83 check_string (g_build_path (":", "x", ":", "y", NULL), "x:y");
84 check_string (g_build_path (":", "x", "::", "y", NULL), "x:y");
85 check_string (g_build_path (":", "x", "y", "z", NULL), "x:y:z");
86 check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
87 check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
89 /* check_string (g_build_path ("::", NULL), "");*/
90 check_string (g_build_path ("::", "::", NULL), "::");
91 check_string (g_build_path ("::", ":::", NULL), ":::");
92 check_string (g_build_path ("::", "::x", NULL), "::x");
93 check_string (g_build_path ("::", "x::", NULL), "x::");
94 check_string (g_build_path ("::", "", "x", NULL), "x");
95 check_string (g_build_path ("::", "", "::x", NULL), "::x");
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", "", NULL), "x");
99 check_string (g_build_path ("::", "x::", "", NULL), "x::");
100 check_string (g_build_path ("::", "x", "::", NULL), "x::");
102 /* This following is weird, but keeps the definition simple */
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", "y", NULL), "x::y");
106 check_string (g_build_path ("::", "::x", "y", NULL), "::x::y");
107 check_string (g_build_path ("::", "x", "y::", NULL), "x::y::");
108 check_string (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::");
109 check_string (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::");
110 check_string (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::");
111 check_string (g_build_path ("::", "x", "", "y", NULL), "x::y");
112 check_string (g_build_path ("::", "x", "::", "y", NULL), "x::y");
113 check_string (g_build_path ("::", "x", "::::", "y", NULL), "x::y");
114 check_string (g_build_path ("::", "x", "y", "z", NULL), "x::y::z");
115 check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
116 check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
117 check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
120 static void
121 test_build_pathv (void)
123 gchar *args[10];
125 g_assert (g_build_pathv ("", NULL) == NULL);
126 args[0] = NULL;
127 check_string (g_build_pathv ("", args), "");
128 args[0] = ""; args[1] = NULL;
129 check_string (g_build_pathv ("", args), "");
130 args[0] = "x"; args[1] = NULL;
131 check_string (g_build_pathv ("", args), "x");
132 args[0] = "x"; args[1] = "y"; args[2] = NULL;
133 check_string (g_build_pathv ("", args), "xy");
134 args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
135 check_string (g_build_pathv ("", args), "xyz");
137 args[0] = NULL;
138 check_string (g_build_pathv (":", args), "");
139 args[0] = ":"; args[1] = NULL;
140 check_string (g_build_pathv (":", args), ":");
141 args[0] = ":x"; args[1] = NULL;
142 check_string (g_build_pathv (":", args), ":x");
143 args[0] = "x:"; args[1] = NULL;
144 check_string (g_build_pathv (":", args), "x:");
145 args[0] = ""; args[1] = "x"; args[2] = NULL;
146 check_string (g_build_pathv (":", args), "x");
147 args[0] = ""; args[1] = ":x"; args[2] = NULL;
148 check_string (g_build_pathv (":", args), ":x");
149 args[0] = ":"; args[1] = "x"; args[2] = NULL;
150 check_string (g_build_pathv (":", args), ":x");
151 args[0] = "::"; args[1] = "x"; args[2] = NULL;
152 check_string (g_build_pathv (":", args), "::x");
153 args[0] = "x"; args[1] = ""; args[2] = NULL;
154 check_string (g_build_pathv (":", args), "x");
155 args[0] = "x:"; args[1] = ""; args[2] = NULL;
156 check_string (g_build_pathv (":", args), "x:");
157 args[0] = "x"; args[1] = ":"; args[2] = NULL;
158 check_string (g_build_pathv (":", args), "x:");
159 args[0] = "x"; args[1] = "::"; args[2] = NULL;
160 check_string (g_build_pathv (":", args), "x::");
161 args[0] = "x"; args[1] = "y"; args[2] = NULL;
162 check_string (g_build_pathv (":", args), "x:y");
163 args[0] = ":x"; args[1] = "y"; args[2] = NULL;
164 check_string (g_build_pathv (":", args), ":x:y");
165 args[0] = "x"; args[1] = "y:"; args[2] = NULL;
166 check_string (g_build_pathv (":", args), "x:y:");
167 args[0] = ":x:"; args[1] = ":y:"; args[2] = NULL;
168 check_string (g_build_pathv (":", args), ":x:y:");
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] = ""; args[2] = "y"; args[3] = NULL;
172 check_string (g_build_pathv (":", args), "x:y");
173 args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
174 check_string (g_build_pathv (":", args), "x:y");
175 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
176 check_string (g_build_pathv (":", args), "x:y");
177 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
178 check_string (g_build_pathv (":", args), "x:y:z");
179 args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
180 check_string (g_build_pathv (":", args), ":x:y:z:");
181 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
182 check_string (g_build_pathv (":", args), "::x:y:z::");
184 args[0] = NULL;
185 check_string (g_build_pathv ("::", args), "");
186 args[0] = "::"; args[1] = NULL;
187 check_string (g_build_pathv ("::", args), "::");
188 args[0] = ":::"; args[1] = NULL;
189 check_string (g_build_pathv ("::", args), ":::");
190 args[0] = "::x"; args[1] = NULL;
191 check_string (g_build_pathv ("::", args), "::x");
192 args[0] = "x::"; args[1] = NULL;
193 check_string (g_build_pathv ("::", args), "x::");
194 args[0] = ""; args[1] = "x"; args[2] = NULL;
195 check_string (g_build_pathv ("::", args), "x");
196 args[0] = ""; args[1] = "::x"; args[2] = NULL;
197 check_string (g_build_pathv ("::", args), "::x");
198 args[0] = "::"; args[1] = "x"; args[2] = NULL;
199 check_string (g_build_pathv ("::", args), "::x");
200 args[0] = "::::"; args[1] = "x"; args[2] = NULL;
201 check_string (g_build_pathv ("::", args), "::::x");
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] = "::"; args[2] = NULL;
207 check_string (g_build_pathv ("::", args), "x::");
208 /* This following is weird, but keeps the definition simple */
209 args[0] = "x"; args[1] = ":::"; args[2] = NULL;
210 check_string (g_build_pathv ("::", args), "x:::::");
211 args[0] = "x"; args[1] = "::::"; args[2] = NULL;
212 check_string (g_build_pathv ("::", args), "x::::");
213 args[0] = "x"; args[1] = "y"; args[2] = NULL;
214 check_string (g_build_pathv ("::", args), "x::y");
215 args[0] = "::x"; args[1] = "y"; args[2] = NULL;
216 check_string (g_build_pathv ("::", args), "::x::y");
217 args[0] = "x"; args[1] = "y::"; args[2] = NULL;
218 check_string (g_build_pathv ("::", args), "x::y::");
219 args[0] = "::x::"; args[1] = "::y::"; args[2] = NULL;
220 check_string (g_build_pathv ("::", args), "::x::y::");
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] = ""; args[2] = "y"; args[3] = NULL;
226 check_string (g_build_pathv ("::", args), "x::y");
227 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
228 check_string (g_build_pathv ("::", args), "x::y");
229 args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
230 check_string (g_build_pathv ("::", args), "x::y");
231 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
232 check_string (g_build_pathv ("::", args), "x::y::z");
233 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
234 check_string (g_build_pathv ("::", args), "::x::y::z::");
235 args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
236 check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
237 args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
238 check_string (g_build_pathv ("::", args), "::::x::y::z::::");
241 static void
242 test_build_filename (void)
244 /* check_string (g_build_filename (NULL), "");*/
245 check_string (g_build_filename (S, NULL), S);
246 check_string (g_build_filename (S"x", NULL), S"x");
247 check_string (g_build_filename ("x"S, NULL), "x"S);
248 check_string (g_build_filename ("", "x", NULL), "x");
249 check_string (g_build_filename ("", S"x", NULL), S"x");
250 check_string (g_build_filename (S, "x", NULL), S"x");
251 check_string (g_build_filename (S S, "x", NULL), S S"x");
252 check_string (g_build_filename ("x", "", NULL), "x");
253 check_string (g_build_filename ("x"S, "", NULL), "x"S);
254 check_string (g_build_filename ("x", S, NULL), "x"S);
255 check_string (g_build_filename ("x", S S, NULL), "x"S S);
256 check_string (g_build_filename ("x", "y", NULL), "x"S"y");
257 check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
258 check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
259 check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
260 check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
261 check_string (g_build_filename ("x", "", "y", NULL), "x"S"y");
262 check_string (g_build_filename ("x", S, "y", NULL), "x"S"y");
263 check_string (g_build_filename ("x", S S, "y", NULL), "x"S"y");
264 check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
265 check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
266 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);
268 #ifdef G_OS_WIN32
270 /* Test also using the slash as file name separator */
271 #define U "/"
272 /* check_string (g_build_filename (NULL), ""); */
273 check_string (g_build_filename (U, NULL), U);
274 check_string (g_build_filename (U"x", NULL), U"x");
275 check_string (g_build_filename ("x"U, NULL), "x"U);
276 check_string (g_build_filename ("", U"x", NULL), U"x");
277 check_string (g_build_filename ("", U"x", NULL), U"x");
278 check_string (g_build_filename (U, "x", NULL), U"x");
279 check_string (g_build_filename (U U, "x", NULL), U U"x");
280 check_string (g_build_filename (U S, "x", NULL), U S"x");
281 check_string (g_build_filename ("x"U, "", NULL), "x"U);
282 check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
283 check_string (g_build_filename ("x", U, NULL), "x"U);
284 check_string (g_build_filename ("x", U U, NULL), "x"U U);
285 check_string (g_build_filename ("x", S U, NULL), "x"S U);
286 check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
287 check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
288 check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
289 check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
290 check_string (g_build_filename ("x", U, "y", NULL), "x"U"y");
291 check_string (g_build_filename ("x", U U, "y", NULL), "x"U"y");
292 check_string (g_build_filename ("x", U S, "y", NULL), "x"S"y");
293 check_string (g_build_filename ("x", S U, "y", NULL), "x"U"y");
294 check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"z");
295 check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
296 check_string (g_build_filename ("x", S "y", "z", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
297 check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
298 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);
300 #undef U
302 #endif /* G_OS_WIN32 */
306 static void
307 test_build_filenamev (void)
309 gchar *args[10];
311 args[0] = NULL;
312 check_string (g_build_filenamev (args), "");
313 args[0] = S; args[1] = NULL;
314 check_string (g_build_filenamev (args), S);
315 args[0] = S"x"; args[1] = NULL;
316 check_string (g_build_filenamev (args), S"x");
317 args[0] = "x"S; args[1] = NULL;
318 check_string (g_build_filenamev (args), "x"S);
319 args[0] = ""; args[1] = "x"; args[2] = NULL;
320 check_string (g_build_filenamev (args), "x");
321 args[0] = ""; args[1] = S"x"; args[2] = NULL;
322 check_string (g_build_filenamev (args), S"x");
323 args[0] = S; args[1] = "x"; args[2] = NULL;
324 check_string (g_build_filenamev (args), S"x");
325 args[0] = S S; args[1] = "x"; args[2] = NULL;
326 check_string (g_build_filenamev (args), S S"x");
327 args[0] = "x"; args[1] = ""; args[2] = NULL;
328 check_string (g_build_filenamev (args), "x");
329 args[0] = "x"S; args[1] = ""; args[2] = NULL;
330 check_string (g_build_filenamev (args), "x"S);
331 args[0] = "x"; args[1] = S; args[2] = NULL;
332 check_string (g_build_filenamev (args), "x"S);
333 args[0] = "x"; args[1] = S S; args[2] = NULL;
334 check_string (g_build_filenamev (args), "x"S S);
335 args[0] = "x"; args[1] = "y"; args[2] = NULL;
336 check_string (g_build_filenamev (args), "x"S"y");
337 args[0] = S"x"; args[1] = "y"; args[2] = NULL;
338 check_string (g_build_filenamev (args), S"x"S"y");
339 args[0] = "x"; args[1] = "y"S; args[2] = NULL;
340 check_string (g_build_filenamev (args), "x"S"y"S);
341 args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
342 check_string (g_build_filenamev (args), S"x"S"y"S);
343 args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
344 check_string (g_build_filenamev (args), S"x"S"y"S);
345 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
346 check_string (g_build_filenamev (args), "x"S"y");
347 args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
348 check_string (g_build_filenamev (args), "x"S"y");
349 args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
350 check_string (g_build_filenamev (args), "x"S"y");
351 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
352 check_string (g_build_filenamev (args), "x"S"y"S"z");
353 args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
354 check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
355 args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
356 check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
358 #ifdef G_OS_WIN32
360 /* Test also using the slash as file name separator */
361 #define U "/"
362 args[0] = NULL;
363 check_string (g_build_filenamev (args), "");
364 args[0] = U; args[1] = NULL;
365 check_string (g_build_filenamev (args), U);
366 args[0] = U"x"; args[1] = NULL;
367 check_string (g_build_filenamev (args), U"x");
368 args[0] = "x"U; args[1] = NULL;
369 check_string (g_build_filenamev (args), "x"U);
370 args[0] = ""; args[1] = U"x"; args[2] = NULL;
371 check_string (g_build_filenamev (args), U"x");
372 args[0] = ""; args[1] = U"x"; args[2] = NULL;
373 check_string (g_build_filenamev (args), U"x");
374 args[0] = U; args[1] = "x"; args[2] = NULL;
375 check_string (g_build_filenamev (args), U"x");
376 args[0] = U U; args[1] = "x"; args[2] = NULL;
377 check_string (g_build_filenamev (args), U U"x");
378 args[0] = U S; args[1] = "x"; args[2] = NULL;
379 check_string (g_build_filenamev (args), U S"x");
380 args[0] = "x"U; args[1] = ""; args[2] = NULL;
381 check_string (g_build_filenamev (args), "x"U);
382 args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
383 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
384 args[0] = "x"; args[1] = U; args[2] = NULL;
385 check_string (g_build_filenamev (args), "x"U);
386 args[0] = "x"; args[1] = U U; args[2] = NULL;
387 check_string (g_build_filenamev (args), "x"U U);
388 args[0] = "x"; args[1] = S U; args[2] = NULL;
389 check_string (g_build_filenamev (args), "x"S U);
390 args[0] = U"x"; args[1] = "y"; args[2] = NULL;
391 check_string (g_build_filenamev (args), U"x"U"y");
392 args[0] = "x"; args[1] = "y"U; args[2] = NULL;
393 check_string (g_build_filenamev (args), "x"U"y"U);
394 args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
395 check_string (g_build_filenamev (args), U"x"U"y"U);
396 args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
397 check_string (g_build_filenamev (args), U"x"U"y"U);
398 args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
399 check_string (g_build_filenamev (args), "x"U"y");
400 args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
401 check_string (g_build_filenamev (args), "x"U"y");
402 args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
403 check_string (g_build_filenamev (args), "x"S"y");
404 args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
405 check_string (g_build_filenamev (args), "x"U"y");
406 args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
407 check_string (g_build_filenamev (args), "x"U"y"U"z");
408 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
409 check_string (g_build_filenamev (args), "x"S"y"S"z");
410 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = U;
411 args[4] = "a"; args[5] = "b"; args[6] = NULL;
412 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
413 args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
414 check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
415 args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
416 check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
418 #undef U
420 #endif /* G_OS_WIN32 */
423 #undef S
425 static void
426 test_mkdir_with_parents_1 (const gchar *base)
428 char *p0 = g_build_filename (base, "fum", NULL);
429 char *p1 = g_build_filename (p0, "tem", NULL);
430 char *p2 = g_build_filename (p1, "zap", NULL);
431 FILE *f;
433 g_remove (p2);
434 g_remove (p1);
435 g_remove (p0);
437 if (g_file_test (p0, G_FILE_TEST_EXISTS))
438 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
440 if (g_file_test (p1, G_FILE_TEST_EXISTS))
441 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
443 if (g_file_test (p2, G_FILE_TEST_EXISTS))
444 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
446 if (g_mkdir_with_parents (p2, 0777) == -1)
447 g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
449 if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
450 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
452 if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
453 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
455 if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
456 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
458 g_rmdir (p2);
459 if (g_file_test (p2, G_FILE_TEST_EXISTS))
460 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
462 g_rmdir (p1);
463 if (g_file_test (p1, G_FILE_TEST_EXISTS))
464 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
466 f = g_fopen (p1, "w");
467 if (f == NULL)
468 g_error ("failed, couldn't create file %s\n", p1);
469 fclose (f);
471 if (g_mkdir_with_parents (p1, 0666) == 0)
472 g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
474 if (g_mkdir_with_parents (p2, 0666) == 0)
475 g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
477 g_remove (p2);
478 g_remove (p1);
479 g_remove (p0);
481 g_free (p2);
482 g_free (p1);
483 g_free (p0);
486 static void
487 test_mkdir_with_parents (void)
489 gchar *cwd;
490 if (g_test_verbose())
491 g_printerr ("checking g_mkdir_with_parents() in subdir ./hum/");
492 test_mkdir_with_parents_1 ("hum");
493 g_remove ("hum");
494 if (g_test_verbose())
495 g_printerr ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
496 test_mkdir_with_parents_1 ("hii///haa/hee");
497 g_remove ("hii/haa/hee");
498 g_remove ("hii/haa");
499 g_remove ("hii");
500 cwd = g_get_current_dir ();
501 if (g_test_verbose())
502 g_printerr ("checking g_mkdir_with_parents() in cwd: %s", cwd);
503 test_mkdir_with_parents_1 (cwd);
504 g_free (cwd);
506 g_assert (g_mkdir_with_parents (NULL, 0) == -1);
507 g_assert (errno == EINVAL);
510 static void
511 test_format_size_for_display (void)
513 #ifdef G_OS_WIN32
514 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
515 #endif
516 /* nobody called setlocale(), so we should get "C" behaviour... */
517 check_string (g_format_size_for_display (0), "0 bytes");
518 check_string (g_format_size_for_display (1), "1 byte");
519 check_string (g_format_size_for_display (2), "2 bytes");
520 check_string (g_format_size_for_display (1024), "1.0 KB");
521 check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
522 check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
523 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
524 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
525 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
527 check_string (g_format_size (0), "0 bytes");
528 check_string (g_format_size (1), "1 byte");
529 check_string (g_format_size (2), "2 bytes");
530 check_string (g_format_size (1000ULL), "1.0 kB");
531 check_string (g_format_size (1000ULL * 1000), "1.0 MB");
532 check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
533 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
534 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
535 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
537 check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
538 check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
539 check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
541 check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
542 check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
543 check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
544 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
545 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
546 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
548 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
549 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
550 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
553 static void
554 test_file_errors (void)
556 #ifdef EEXIST
557 g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
558 #endif
559 #ifdef EISDIR
560 g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
561 #endif
562 #ifdef EACCES
563 g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
564 #endif
565 #ifdef ENAMETOOLONG
566 g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
567 #endif
568 #ifdef ENOENT
569 g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
570 #endif
571 #ifdef ENOTDIR
572 g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
573 #endif
574 #ifdef ENXIO
575 g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
576 #endif
577 #ifdef ENODEV
578 g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
579 #endif
580 #ifdef EROFS
581 g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
582 #endif
583 #ifdef ETXTBSY
584 g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
585 #endif
586 #ifdef EFAULT
587 g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
588 #endif
589 #ifdef ELOOP
590 g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
591 #endif
592 #ifdef ENOSPC
593 g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
594 #endif
595 #ifdef ENOMEM
596 g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
597 #endif
598 #ifdef EMFILE
599 g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
600 #endif
601 #ifdef ENFILE
602 g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
603 #endif
604 #ifdef EBADF
605 g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
606 #endif
607 #ifdef EINVAL
608 g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
609 #endif
610 #ifdef EPIPE
611 g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
612 #endif
613 #ifdef EAGAIN
614 g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
615 #endif
616 #ifdef EINTR
617 g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
618 #endif
619 #ifdef EIO
620 g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
621 #endif
622 #ifdef EPERM
623 g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
624 #endif
625 #ifdef ENOSYS
626 g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
627 #endif
630 static void
631 test_basename (void)
633 gchar *b;
635 b = g_path_get_basename ("");
636 g_assert_cmpstr (b, ==, ".");
637 g_free (b);
639 b = g_path_get_basename ("///");
640 g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
641 g_free (b);
643 b = g_path_get_basename ("/a/b/c/d");
644 g_assert_cmpstr (b, ==, "d");
645 g_free (b);
648 static void
649 test_dir_make_tmp (void)
651 gchar *name;
652 GError *error = NULL;
653 gint ret;
655 name = g_dir_make_tmp ("testXXXXXXtest", &error);
656 g_assert_no_error (error);
657 g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
658 ret = g_rmdir (name);
659 g_assert (ret == 0);
660 g_free (name);
662 name = g_dir_make_tmp (NULL, &error);
663 g_assert_no_error (error);
664 g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
665 ret = g_rmdir (name);
666 g_assert (ret == 0);
667 g_free (name);
669 name = g_dir_make_tmp ("test/XXXXXX", &error);
670 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
671 g_clear_error (&error);
672 g_assert (name == NULL);
674 name = g_dir_make_tmp ("XXXXxX", &error);
675 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
676 g_clear_error (&error);
677 g_assert (name == NULL);
680 static void
681 test_file_open_tmp (void)
683 gchar *name = NULL;
684 GError *error = NULL;
685 gint fd;
687 fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
688 g_assert (fd != -1);
689 g_assert_no_error (error);
690 g_assert (name != NULL);
691 unlink (name);
692 g_free (name);
693 close (fd);
695 fd = g_file_open_tmp (NULL, &name, &error);
696 g_assert (fd != -1);
697 g_assert_no_error (error);
698 g_assert (name != NULL);
699 g_unlink (name);
700 g_free (name);
701 close (fd);
703 name = NULL;
704 fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
705 g_assert (fd == -1);
706 g_assert (name == NULL);
707 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
708 g_clear_error (&error);
710 fd = g_file_open_tmp ("XXXXxX", &name, &error);
711 g_assert (fd == -1);
712 g_assert (name == NULL);
713 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
714 g_clear_error (&error);
717 static void
718 test_mkstemp (void)
720 gchar *name;
721 gint fd;
723 name = g_strdup ("testXXXXXXtest"),
724 fd = g_mkstemp (name);
725 g_assert (fd != -1);
726 g_assert (strstr (name, "XXXXXX") == NULL);
727 unlink (name);
728 close (fd);
729 g_free (name);
731 name = g_strdup ("testYYYYYYtest"),
732 fd = g_mkstemp (name);
733 g_assert (fd == -1);
734 g_free (name);
737 static void
738 test_mkdtemp (void)
740 gchar *name;
741 gchar *ret;
743 name = g_strdup ("testXXXXXXtest"),
744 ret = g_mkdtemp (name);
745 g_assert (ret == name);
746 g_assert (strstr (name, "XXXXXX") == NULL);
747 g_rmdir (name);
748 g_free (name);
750 name = g_strdup ("testYYYYYYtest"),
751 ret = g_mkdtemp (name);
752 g_assert (ret == NULL);
753 g_free (name);
756 static void
757 test_set_contents (void)
759 GError *error = NULL;
760 gint fd;
761 gchar *name;
762 gchar *buf;
763 gsize len;
764 gboolean ret;
766 fd = g_file_open_tmp (NULL, &name, &error);
767 g_assert_no_error (error);
768 write (fd, "a", 1);
769 close (fd);
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, ==, "a");
775 g_free (buf);
777 ret = g_file_set_contents (name, "b", 1, &error);
778 g_assert (ret);
779 g_assert_no_error (error);
781 ret = g_file_get_contents (name, &buf, &len, &error);
782 g_assert (ret);
783 g_assert_no_error (error);
784 g_assert_cmpstr (buf, ==, "b");
785 g_free (buf);
787 g_remove (name);
788 g_free (name);
791 static void
792 test_read_link (void)
794 #ifdef HAVE_READLINK
795 #ifdef G_OS_UNIX
796 int ret;
797 const gchar *oldpath;
798 gchar *cwd;
799 gchar *newpath;
800 gchar *badpath;
801 gchar *path;
802 GError *error = NULL;
804 cwd = g_get_current_dir ();
806 oldpath = g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL);
807 newpath = g_build_filename (cwd, "page-of-junk", NULL);
808 badpath = g_build_filename (cwd, "4097-random-bytes", NULL);
809 remove (newpath);
810 ret = symlink (oldpath, newpath);
811 g_assert (ret == 0);
812 path = g_file_read_link (newpath, &error);
813 g_assert_no_error (error);
814 g_assert_cmpstr (path, ==, oldpath);
815 g_free (path);
817 remove (newpath);
818 ret = symlink (badpath, newpath);
819 g_assert (ret == 0);
820 path = g_file_read_link (newpath, &error);
821 g_assert_no_error (error);
822 g_assert_cmpstr (path, ==, badpath);
823 g_free (path);
825 path = g_file_read_link (oldpath, &error);
826 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
827 g_assert_null (path);
828 g_error_free (error);
830 remove (newpath);
831 g_free (cwd);
832 g_free (newpath);
833 g_free (badpath);
835 #endif
836 #else
837 g_test_skip ("Symbolic links not supported");
838 #endif
841 static void
842 test_stdio_wrappers (void)
844 GStatBuf buf;
845 gchar *cwd, *path;
846 gint ret;
847 struct utimbuf ut;
848 GError *error = NULL;
850 g_remove ("mkdir-test/test-create");
851 g_rmdir ("mkdir-test");
853 ret = g_stat ("mkdir-test", &buf);
854 g_assert (ret == -1);
855 ret = g_mkdir ("mkdir-test", 0666);
856 g_assert (ret == 0);
857 ret = g_stat ("mkdir-test", &buf);
858 g_assert (ret == 0);
859 g_assert (S_ISDIR (buf.st_mode));
861 cwd = g_get_current_dir ();
862 path = g_build_filename (cwd, "mkdir-test", NULL);
863 g_free (cwd);
864 ret = g_chdir (path);
865 g_assert (errno == EACCES);
866 g_assert (ret == -1);
867 ret = g_chmod (path, 0777);
868 g_assert (ret == 0);
869 ret = g_chdir (path);
870 g_assert (ret == 0);
871 cwd = g_get_current_dir ();
872 g_assert (g_str_equal (cwd, path));
873 g_free (cwd);
874 g_free (path);
876 ret = g_creat ("test-creat", 0555);
877 g_close (ret, &error);
878 g_assert_no_error (error);
880 ret = g_access ("test-creat", F_OK);
881 g_assert (ret == 0);
883 ret = g_rename ("test-creat", "test-create");
884 g_assert (ret == 0);
886 ret = g_open ("test-create", O_RDONLY, 0666);
887 g_close (ret, &error);
888 g_assert_no_error (error);
890 ut.actime = ut.modtime = (time_t)0;
891 ret = g_utime ("test-create", &ut);
892 g_assert (ret == 0);
894 ret = g_lstat ("test-create", &buf);
895 g_assert (ret == 0);
896 g_assert (buf.st_atime == (time_t)0);
897 g_assert (buf.st_mtime == (time_t)0);
899 g_chdir ("..");
900 g_remove ("mkdir-test/test-create");
901 g_rmdir ("mkdir-test");
905 main (int argc,
906 char *argv[])
908 g_setenv ("LC_ALL", "C", TRUE);
909 g_test_init (&argc, &argv, NULL);
911 g_test_add_func ("/fileutils/build-path", test_build_path);
912 g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
913 g_test_add_func ("/fileutils/build-filename", test_build_filename);
914 g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
915 g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
916 g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
917 g_test_add_func ("/fileutils/errors", test_file_errors);
918 g_test_add_func ("/fileutils/basename", test_basename);
919 g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
920 g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
921 g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
922 g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
923 g_test_add_func ("/fileutils/set-contents", test_set_contents);
924 g_test_add_func ("/fileutils/read-link", test_read_link);
925 g_test_add_func ("/fileutils/stdio-wrappers", test_stdio_wrappers);
927 return g_test_run ();