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.
26 /* We are testing some deprecated APIs here */
27 #define GLIB_DISABLE_DEPRECATION_WARNINGS
31 /* Test our stdio wrappers here */
32 #define G_STDIO_NO_WRAP_ON_UNIX
33 #include <glib/gstdio.h>
37 #include <sys/types.h>
44 #include <sys/utime.h>
47 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
54 #define S G_DIR_SEPARATOR_S
57 check_string (gchar *str, const gchar *expected)
59 g_assert_nonnull (str);
60 g_assert_cmpstr (str, ==, expected);
65 test_build_path (void)
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");
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::");
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::");
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::::");
129 test_build_pathv (void)
133 g_assert_null (g_build_pathv ("", 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");
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::");
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::::");
250 test_build_filename (void)
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);
278 /* Test also using the slash as file name separator */
280 /* check_string (g_build_filename (NULL), ""); */
281 check_string (g_build_filename (U, NULL), U);
282 check_string (g_build_filename (U"x", NULL), U"x");
283 check_string (g_build_filename ("x"U, NULL), "x"U);
284 check_string (g_build_filename ("", U"x", NULL), U"x");
285 check_string (g_build_filename ("", U"x", NULL), U"x");
286 check_string (g_build_filename (U, "x", NULL), U"x");
287 check_string (g_build_filename (U U, "x", NULL), U U"x");
288 check_string (g_build_filename (U S, "x", NULL), U S"x");
289 check_string (g_build_filename ("x"U, "", NULL), "x"U);
290 check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
291 check_string (g_build_filename ("x", U, NULL), "x"U);
292 check_string (g_build_filename ("x", U U, NULL), "x"U U);
293 check_string (g_build_filename ("x", S U, NULL), "x"S U);
294 check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
295 check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
296 check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
297 check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
298 check_string (g_build_filename ("x", U, "y", NULL), "x"U"y");
299 check_string (g_build_filename ("x", U U, "y", NULL), "x"U"y");
300 check_string (g_build_filename ("x", U S, "y", NULL), "x"S"y");
301 check_string (g_build_filename ("x", S U, "y", NULL), "x"U"y");
302 check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"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", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
305 check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
306 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);
310 #endif /* G_OS_WIN32 */
315 test_build_filenamev (void)
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);
368 /* Test also using the slash as file name separator */
371 check_string (g_build_filenamev (args), "");
372 args[0] = U; args[1] = NULL;
373 check_string (g_build_filenamev (args), U);
374 args[0] = U"x"; args[1] = NULL;
375 check_string (g_build_filenamev (args), U"x");
376 args[0] = "x"U; args[1] = NULL;
377 check_string (g_build_filenamev (args), "x"U);
378 args[0] = ""; args[1] = U"x"; args[2] = NULL;
379 check_string (g_build_filenamev (args), U"x");
380 args[0] = ""; args[1] = U"x"; args[2] = NULL;
381 check_string (g_build_filenamev (args), U"x");
382 args[0] = U; args[1] = "x"; args[2] = NULL;
383 check_string (g_build_filenamev (args), U"x");
384 args[0] = U U; args[1] = "x"; args[2] = NULL;
385 check_string (g_build_filenamev (args), U U"x");
386 args[0] = U S; args[1] = "x"; args[2] = NULL;
387 check_string (g_build_filenamev (args), U S"x");
388 args[0] = "x"U; args[1] = ""; args[2] = NULL;
389 check_string (g_build_filenamev (args), "x"U);
390 args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
391 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
392 args[0] = "x"; args[1] = U; args[2] = NULL;
393 check_string (g_build_filenamev (args), "x"U);
394 args[0] = "x"; args[1] = U U; args[2] = NULL;
395 check_string (g_build_filenamev (args), "x"U U);
396 args[0] = "x"; args[1] = S U; args[2] = NULL;
397 check_string (g_build_filenamev (args), "x"S U);
398 args[0] = U"x"; args[1] = "y"; args[2] = NULL;
399 check_string (g_build_filenamev (args), U"x"U"y");
400 args[0] = "x"; args[1] = "y"U; args[2] = NULL;
401 check_string (g_build_filenamev (args), "x"U"y"U);
402 args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
403 check_string (g_build_filenamev (args), U"x"U"y"U);
404 args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
405 check_string (g_build_filenamev (args), U"x"U"y"U);
406 args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
407 check_string (g_build_filenamev (args), "x"U"y");
408 args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
409 check_string (g_build_filenamev (args), "x"U"y");
410 args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
411 check_string (g_build_filenamev (args), "x"S"y");
412 args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
413 check_string (g_build_filenamev (args), "x"U"y");
414 args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
415 check_string (g_build_filenamev (args), "x"U"y"U"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] = U;
419 args[4] = "a"; args[5] = "b"; args[6] = NULL;
420 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
421 args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
422 check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
423 args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
424 check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
428 #endif /* G_OS_WIN32 */
434 test_mkdir_with_parents_1 (const gchar *base)
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);
445 if (g_file_test (p0, G_FILE_TEST_EXISTS))
446 g_error ("failed, %s exists, cannot test g_mkdir_with_parents", p0);
448 if (g_file_test (p1, G_FILE_TEST_EXISTS))
449 g_error ("failed, %s exists, cannot test g_mkdir_with_parents", p1);
451 if (g_file_test (p2, G_FILE_TEST_EXISTS))
452 g_error ("failed, %s exists, cannot test g_mkdir_with_parents", p2);
454 if (g_mkdir_with_parents (p2, 0777) == -1)
457 g_error ("failed, g_mkdir_with_parents(%s) failed: %s", p2, g_strerror (errsv));
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);
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);
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);
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);
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);
477 f = g_fopen (p1, "w");
479 g_error ("failed, couldn't create file %s", p1);
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);
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);
498 test_mkdir_with_parents (void)
501 if (g_test_verbose())
502 g_printerr ("checking g_mkdir_with_parents() in subdir ./hum/");
503 test_mkdir_with_parents_1 ("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");
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);
517 g_assert_cmpint (g_mkdir_with_parents (NULL, 0), ==, -1);
518 g_assert_cmpint (errno, ==, EINVAL);
522 test_format_size_for_display (void)
525 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
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");
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");
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");
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");
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)");
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");
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");
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)");
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");
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");
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)");
595 test_file_errors (void)
598 g_assert_cmpint (g_file_error_from_errno (EEXIST), ==, G_FILE_ERROR_EXIST);
601 g_assert_cmpint (g_file_error_from_errno (EISDIR), ==, G_FILE_ERROR_ISDIR);
604 g_assert_cmpint (g_file_error_from_errno (EACCES), ==, G_FILE_ERROR_ACCES);
607 g_assert_cmpint (g_file_error_from_errno (ENAMETOOLONG), ==, G_FILE_ERROR_NAMETOOLONG);
610 g_assert_cmpint (g_file_error_from_errno (ENOENT), ==, G_FILE_ERROR_NOENT);
613 g_assert_cmpint (g_file_error_from_errno (ENOTDIR), ==, G_FILE_ERROR_NOTDIR);
616 g_assert_cmpint (g_file_error_from_errno (ENXIO), ==, G_FILE_ERROR_NXIO);
619 g_assert_cmpint (g_file_error_from_errno (ENODEV), ==, G_FILE_ERROR_NODEV);
622 g_assert_cmpint (g_file_error_from_errno (EROFS), ==, G_FILE_ERROR_ROFS);
625 g_assert_cmpint (g_file_error_from_errno (ETXTBSY), ==, G_FILE_ERROR_TXTBSY);
628 g_assert_cmpint (g_file_error_from_errno (EFAULT), ==, G_FILE_ERROR_FAULT);
631 g_assert_cmpint (g_file_error_from_errno (ELOOP), ==, G_FILE_ERROR_LOOP);
634 g_assert_cmpint (g_file_error_from_errno (ENOSPC), ==, G_FILE_ERROR_NOSPC);
637 g_assert_cmpint (g_file_error_from_errno (ENOMEM), ==, G_FILE_ERROR_NOMEM);
640 g_assert_cmpint (g_file_error_from_errno (EMFILE), ==, G_FILE_ERROR_MFILE);
643 g_assert_cmpint (g_file_error_from_errno (ENFILE), ==, G_FILE_ERROR_NFILE);
646 g_assert_cmpint (g_file_error_from_errno (EBADF), ==, G_FILE_ERROR_BADF);
649 g_assert_cmpint (g_file_error_from_errno (EINVAL), ==, G_FILE_ERROR_INVAL);
652 g_assert_cmpint (g_file_error_from_errno (EPIPE), ==, G_FILE_ERROR_PIPE);
655 g_assert_cmpint (g_file_error_from_errno (EAGAIN), ==, G_FILE_ERROR_AGAIN);
658 g_assert_cmpint (g_file_error_from_errno (EINTR), ==, G_FILE_ERROR_INTR);
661 g_assert_cmpint (g_file_error_from_errno (EIO), ==, G_FILE_ERROR_IO);
664 g_assert_cmpint (g_file_error_from_errno (EPERM), ==, G_FILE_ERROR_PERM);
667 g_assert_cmpint (g_file_error_from_errno (ENOSYS), ==, G_FILE_ERROR_NOSYS);
676 b = g_path_get_basename ("");
677 g_assert_cmpstr (b, ==, ".");
680 b = g_path_get_basename ("///");
681 g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
684 b = g_path_get_basename ("/a/b/c/d");
685 g_assert_cmpstr (b, ==, "d");
690 test_dir_make_tmp (void)
693 GError *error = NULL;
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);
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);
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);
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);
722 test_file_open_tmp (void)
725 GError *error = NULL;
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);
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);
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);
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);
764 name = g_strdup ("testXXXXXXtest"),
765 fd = g_mkstemp (name);
766 g_assert_cmpint (fd, !=, -1);
767 g_assert_null (strstr (name, "XXXXXX"));
772 name = g_strdup ("testYYYYYYtest"),
773 fd = g_mkstemp (name);
774 g_assert_cmpint (fd, ==, -1);
784 name = g_strdup ("testXXXXXXtest"),
785 ret = g_mkdtemp (name);
786 g_assert (ret == name);
787 g_assert_null (strstr (name, "XXXXXX"));
791 name = g_strdup ("testYYYYYYtest"),
792 ret = g_mkdtemp (name);
798 test_set_contents (void)
800 GError *error = NULL;
807 fd = g_file_open_tmp (NULL, &name, &error);
808 g_assert_no_error (error);
812 ret = g_file_get_contents (name, &buf, &len, &error);
814 g_assert_no_error (error);
815 g_assert_cmpstr (buf, ==, "a");
818 ret = g_file_set_contents (name, "b", 1, &error);
820 g_assert_no_error (error);
822 ret = g_file_get_contents (name, &buf, &len, &error);
824 g_assert_no_error (error);
825 g_assert_cmpstr (buf, ==, "b");
833 test_read_link (void)
838 const gchar *oldpath;
843 GError *error = NULL;
845 cwd = g_get_current_dir ();
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);
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);
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);
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);
878 g_test_skip ("Symbolic links not supported");
883 test_stdio_wrappers (void)
889 GError *error = NULL;
890 GStatBuf path_statbuf, cwd_statbuf;
892 /* The permissions tests here don’t work when running as root. */
894 if (getuid () == 0 || geteuid () == 0)
896 g_test_skip ("File permissions tests cannot be run as root");
901 g_remove ("mkdir-test/test-create");
902 ret = g_rmdir ("mkdir-test");
903 g_assert (ret == 0 || errno == ENOENT);
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);
913 cwd = g_get_current_dir ();
914 path = g_build_filename (cwd, "mkdir-test", NULL);
916 ret = g_chdir (path);
917 g_assert_cmpint (errno, ==, EACCES);
918 g_assert_cmpint (ret, ==, -1);
919 ret = g_chmod (path, 0777);
920 g_assert_cmpint (ret, ==, 0);
921 ret = g_chdir (path);
922 g_assert_cmpint (ret, ==, 0);
923 cwd = g_get_current_dir ();
924 /* We essentially want to check that cwd == path, but we can’t compare the
925 * paths directly since the tests might be running under a symlink (for
926 * example, /tmp is sometimes a symlink). Compare the inode numbers instead. */
927 g_assert_cmpint (g_stat (cwd, &cwd_statbuf), ==, 0);
928 g_assert_cmpint (g_stat (path, &path_statbuf), ==, 0);
929 g_assert_true (cwd_statbuf.st_dev == path_statbuf.st_dev &&
930 cwd_statbuf.st_ino == path_statbuf.st_ino);
934 ret = g_creat ("test-creat", 0555);
935 g_close (ret, &error);
936 g_assert_no_error (error);
938 ret = g_access ("test-creat", F_OK);
939 g_assert_cmpint (ret, ==, 0);
941 ret = g_rename ("test-creat", "test-create");
942 g_assert_cmpint (ret, ==, 0);
944 ret = g_open ("test-create", O_RDONLY, 0666);
945 g_close (ret, &error);
946 g_assert_no_error (error);
948 ut.actime = ut.modtime = (time_t)0;
949 ret = g_utime ("test-create", &ut);
950 g_assert_cmpint (ret, ==, 0);
952 ret = g_lstat ("test-create", &buf);
953 g_assert_cmpint (ret, ==, 0);
954 g_assert_cmpint (buf.st_atime, ==, (time_t)0);
955 g_assert_cmpint (buf.st_mtime, ==, (time_t)0);
958 g_remove ("mkdir-test/test-create");
959 g_rmdir ("mkdir-test");
962 /* Win32 does not support "wb+", but g_fopen() should automatically
963 * translate this mode to its alias "w+b".
964 * Also check various other file open modes for correct support accross
966 * See: https://gitlab.gnome.org/GNOME/glib/merge_requests/119
969 test_fopen_modes (void)
971 char *path = g_build_filename ("temp-fopen", NULL);
973 const gchar *modes[] =
994 if (g_file_test (path, G_FILE_TEST_EXISTS))
995 g_error ("failed, %s exists, cannot test g_fopen()", path);
997 for (i = 0; i < G_N_ELEMENTS (modes); i++)
1001 g_test_message ("Testing fopen() mode '%s'", modes[i]);
1003 f = g_fopen (path, modes[i]);
1004 g_assert_nonnull (f);
1016 g_setenv ("LC_ALL", "C", TRUE);
1017 g_test_init (&argc, &argv, NULL);
1019 g_test_bug_base ("https://gitlab.gnome.org/GNOME/glib/merge_requests/");
1021 g_test_add_func ("/fileutils/build-path", test_build_path);
1022 g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
1023 g_test_add_func ("/fileutils/build-filename", test_build_filename);
1024 g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
1025 g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
1026 g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
1027 g_test_add_func ("/fileutils/errors", test_file_errors);
1028 g_test_add_func ("/fileutils/basename", test_basename);
1029 g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
1030 g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
1031 g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
1032 g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
1033 g_test_add_func ("/fileutils/set-contents", test_set_contents);
1034 g_test_add_func ("/fileutils/read-link", test_read_link);
1035 g_test_add_func ("/fileutils/stdio-wrappers", test_stdio_wrappers);
1036 g_test_add_func ("/fileutils/fopen-modes", test_fopen_modes);
1038 return g_test_run ();