2 * Unit test suite for directory functions.
4 * Copyright 2002 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/test.h"
28 /* If you change something in these tests, please do the same
29 * for GetSystemDirectory tests.
31 static void test_GetWindowsDirectoryA(void)
33 UINT len
, len_with_null
;
36 len_with_null
= GetWindowsDirectoryA(NULL
, 0);
37 ok(len_with_null
<= MAX_PATH
, "should fit into MAX_PATH\n");
40 len_with_null
= GetWindowsDirectoryA(buf
, 1);
41 ok(lstrcmpA(buf
, "foo") == 0, "should not touch the buffer\n");
44 len
= GetWindowsDirectoryA(buf
, len_with_null
- 1);
45 ok(lstrcmpA(buf
, "foo") == 0, "should not touch the buffer\n");
46 ok(len
== len_with_null
, "GetWindowsDirectoryW returned %d, expected %d\n",
50 len
= GetWindowsDirectoryA(buf
, len_with_null
);
51 ok(lstrcmpA(buf
, "foo") != 0, "should touch the buffer\n");
52 ok(len
== strlen(buf
), "returned length should be equal to the length of string\n");
53 ok(len
== len_with_null
-1, "GetWindowsDirectoryA returned %d, expected %d\n",
54 len
, len_with_null
-1);
57 static void test_GetWindowsDirectoryW(void)
59 UINT len
, len_with_null
;
61 static const WCHAR fooW
[] = {'f','o','o',0};
63 len_with_null
= GetWindowsDirectoryW(NULL
, 0);
64 if (len_with_null
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
66 win_skip("GetWindowsDirectoryW is not implemented\n");
69 ok(len_with_null
<= MAX_PATH
, "should fit into MAX_PATH\n");
72 len
= GetWindowsDirectoryW(buf
, 1);
73 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
74 ok(len
== len_with_null
, "GetWindowsDirectoryW returned %d, expected %d\n",
78 len
= GetWindowsDirectoryW(buf
, len_with_null
- 1);
79 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
80 ok(len
== len_with_null
, "GetWindowsDirectoryW returned %d, expected %d\n",
84 len
= GetWindowsDirectoryW(buf
, len_with_null
);
85 ok(lstrcmpW(buf
, fooW
) != 0, "should touch the buffer\n");
86 ok(len
== lstrlenW(buf
), "returned length should be equal to the length of string\n");
87 ok(len
== len_with_null
-1, "GetWindowsDirectoryW returned %d, expected %d\n",
88 len
, len_with_null
-1);
92 /* If you change something in these tests, please do the same
93 * for GetWindowsDirectory tests.
95 static void test_GetSystemDirectoryA(void)
97 UINT len
, len_with_null
;
100 len_with_null
= GetSystemDirectoryA(NULL
, 0);
101 ok(len_with_null
<= MAX_PATH
, "should fit into MAX_PATH\n");
103 lstrcpyA(buf
, "foo");
104 len
= GetSystemDirectoryA(buf
, 1);
105 ok(lstrcmpA(buf
, "foo") == 0, "should not touch the buffer\n");
106 ok(len
== len_with_null
, "GetSystemDirectoryA returned %d, expected %d\n",
109 lstrcpyA(buf
, "foo");
110 len
= GetSystemDirectoryA(buf
, len_with_null
- 1);
111 ok(lstrcmpA(buf
, "foo") == 0, "should not touch the buffer\n");
112 ok(len
== len_with_null
, "GetSystemDirectoryA returned %d, expected %d\n",
115 lstrcpyA(buf
, "foo");
116 len
= GetSystemDirectoryA(buf
, len_with_null
);
117 ok(lstrcmpA(buf
, "foo") != 0, "should touch the buffer\n");
118 ok(len
== strlen(buf
), "returned length should be equal to the length of string\n");
119 ok(len
== len_with_null
-1, "GetSystemDirectoryW returned %d, expected %d\n",
120 len
, len_with_null
-1);
123 static void test_GetSystemDirectoryW(void)
125 UINT len
, len_with_null
;
127 static const WCHAR fooW
[] = {'f','o','o',0};
129 len_with_null
= GetSystemDirectoryW(NULL
, 0);
130 if (len_with_null
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
132 win_skip("GetSystemDirectoryW is not available\n");
135 ok(len_with_null
<= MAX_PATH
, "should fit into MAX_PATH\n");
138 len
= GetSystemDirectoryW(buf
, 1);
139 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
140 ok(len
== len_with_null
, "GetSystemDirectoryW returned %d, expected %d\n",
144 len
= GetSystemDirectoryW(buf
, len_with_null
- 1);
145 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
146 ok(len
== len_with_null
, "GetSystemDirectoryW returned %d, expected %d\n",
150 len
= GetSystemDirectoryW(buf
, len_with_null
);
151 ok(lstrcmpW(buf
, fooW
) != 0, "should touch the buffer\n");
152 ok(len
== lstrlenW(buf
), "returned length should be equal to the length of string\n");
153 ok(len
== len_with_null
-1, "GetSystemDirectoryW returned %d, expected %d\n",
154 len
, len_with_null
-1);
157 static void test_CreateDirectoryA(void)
159 char tmpdir
[MAX_PATH
];
160 WCHAR curdir
[MAX_PATH
];
163 ret
= CreateDirectoryA(NULL
, NULL
);
164 ok(ret
== FALSE
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
165 GetLastError() == ERROR_INVALID_PARAMETER
),
166 "CreateDirectoryA(NULL): ret=%d err=%d\n", ret
, GetLastError());
168 ret
= CreateDirectoryA("", NULL
);
169 ok(ret
== FALSE
&& (GetLastError() == ERROR_BAD_PATHNAME
||
170 GetLastError() == ERROR_PATH_NOT_FOUND
),
171 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
173 ret
= GetSystemDirectoryA(tmpdir
, MAX_PATH
);
174 ok(ret
< MAX_PATH
, "System directory should fit into MAX_PATH\n");
176 GetCurrentDirectoryW(MAX_PATH
, curdir
);
177 ret
= SetCurrentDirectoryA(tmpdir
);
178 ok(ret
== TRUE
, "could not chdir to the System directory\n");
180 ret
= CreateDirectoryA(".", NULL
);
181 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
182 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
185 ret
= CreateDirectoryA("..", NULL
);
186 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
187 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
189 GetTempPathA(MAX_PATH
, tmpdir
);
190 tmpdir
[3] = 0; /* truncate the path */
191 ret
= CreateDirectoryA(tmpdir
, NULL
);
192 ok(ret
== FALSE
&& (GetLastError() == ERROR_ALREADY_EXISTS
||
193 GetLastError() == ERROR_ACCESS_DENIED
),
194 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
196 GetTempPathA(MAX_PATH
, tmpdir
);
197 lstrcatA(tmpdir
, "Please Remove Me");
198 ret
= CreateDirectoryA(tmpdir
, NULL
);
199 ok(ret
== TRUE
, "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
201 ret
= CreateDirectoryA(tmpdir
, NULL
);
202 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
203 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
205 ret
= RemoveDirectoryA(tmpdir
);
207 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
210 lstrcatA(tmpdir
, "?");
211 ret
= CreateDirectoryA(tmpdir
, NULL
);
212 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
213 GetLastError() == ERROR_PATH_NOT_FOUND
),
214 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
215 RemoveDirectoryA(tmpdir
);
217 tmpdir
[lstrlenA(tmpdir
) - 1] = '*';
218 ret
= CreateDirectoryA(tmpdir
, NULL
);
219 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
220 GetLastError() == ERROR_PATH_NOT_FOUND
),
221 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
222 RemoveDirectoryA(tmpdir
);
224 GetTempPathA(MAX_PATH
, tmpdir
);
225 lstrcatA(tmpdir
, "Please Remove Me/Please Remove Me");
226 ret
= CreateDirectoryA(tmpdir
, NULL
);
227 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
228 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
229 RemoveDirectoryA(tmpdir
);
231 /* Test behavior with a trailing dot.
232 * The directory should be created without the dot.
234 GetTempPathA(MAX_PATH
, tmpdir
);
235 lstrcatA(tmpdir
, "Please Remove Me.");
236 ret
= CreateDirectoryA(tmpdir
, NULL
);
238 "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
240 lstrcatA(tmpdir
, "/Please Remove Me");
241 ret
= CreateDirectoryA(tmpdir
, NULL
);
243 "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
244 ret
= RemoveDirectoryA(tmpdir
);
246 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
248 GetTempPathA(MAX_PATH
, tmpdir
);
249 lstrcatA(tmpdir
, "Please Remove Me");
250 ret
= RemoveDirectoryA(tmpdir
);
252 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
254 /* Test behavior with two trailing dots.
255 * The directory should be created without the trailing dots.
257 GetTempPathA(MAX_PATH
, tmpdir
);
258 lstrcatA(tmpdir
, "Please Remove Me..");
259 ret
= CreateDirectoryA(tmpdir
, NULL
);
261 "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
263 lstrcatA(tmpdir
, "/Please Remove Me");
264 ret
= CreateDirectoryA(tmpdir
, NULL
);
265 ok(ret
== TRUE
|| /* On Win98 */
266 (ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
), /* On NT! */
267 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
270 ret
= RemoveDirectoryA(tmpdir
);
272 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
275 GetTempPathA(MAX_PATH
, tmpdir
);
276 lstrcatA(tmpdir
, "Please Remove Me");
277 ret
= RemoveDirectoryA(tmpdir
);
279 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
281 /* Test behavior with a trailing space.
282 * The directory should be created without the trailing space.
284 GetTempPathA(MAX_PATH
, tmpdir
);
285 lstrcatA(tmpdir
, "Please Remove Me ");
286 ret
= CreateDirectoryA(tmpdir
, NULL
);
288 "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
290 lstrcatA(tmpdir
, "/Please Remove Me");
291 ret
= CreateDirectoryA(tmpdir
, NULL
);
292 ok(ret
== TRUE
|| /* On Win98 */
293 (ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
), /* On NT! */
294 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
297 ret
= RemoveDirectoryA(tmpdir
);
299 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
302 GetTempPathA(MAX_PATH
, tmpdir
);
303 lstrcatA(tmpdir
, "Please Remove Me");
304 ret
= RemoveDirectoryA(tmpdir
);
306 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
308 /* Test behavior with a trailing space.
309 * The directory should be created without the trailing spaces.
311 GetTempPathA(MAX_PATH
, tmpdir
);
312 lstrcatA(tmpdir
, "Please Remove Me ");
313 ret
= CreateDirectoryA(tmpdir
, NULL
);
315 "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
317 lstrcatA(tmpdir
, "/Please Remove Me");
318 ret
= CreateDirectoryA(tmpdir
, NULL
);
319 ok(ret
== TRUE
|| /* On Win98 */
320 (ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
), /* On NT! */
321 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir
, ret
, GetLastError());
324 ret
= RemoveDirectoryA(tmpdir
);
326 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
329 GetTempPathA(MAX_PATH
, tmpdir
);
330 lstrcatA(tmpdir
, "Please Remove Me");
331 ret
= RemoveDirectoryA(tmpdir
);
333 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
334 SetCurrentDirectoryW(curdir
);
337 static void test_CreateDirectoryW(void)
339 WCHAR tmpdir
[MAX_PATH
];
341 static const WCHAR empty_strW
[] = { 0 };
342 static const WCHAR tmp_dir_name
[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
343 static const WCHAR dotW
[] = {'.',0};
344 static const WCHAR slashW
[] = {'/',0};
345 static const WCHAR dotdotW
[] = {'.','.',0};
346 static const WCHAR questionW
[] = {'?',0};
347 WCHAR curdir
[MAX_PATH
];
349 ret
= CreateDirectoryW(NULL
, NULL
);
350 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
352 win_skip("CreateDirectoryW is not available\n");
355 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
356 "should not create NULL path ret %u err %u\n", ret
, GetLastError());
358 ret
= CreateDirectoryW(empty_strW
, NULL
);
359 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
360 "should not create empty path ret %u err %u\n", ret
, GetLastError());
362 ret
= GetSystemDirectoryW(tmpdir
, MAX_PATH
);
363 ok(ret
< MAX_PATH
, "System directory should fit into MAX_PATH\n");
365 GetCurrentDirectoryW(MAX_PATH
, curdir
);
366 ret
= SetCurrentDirectoryW(tmpdir
);
367 ok(ret
== TRUE
, "could not chdir to the System directory ret %u err %u\n", ret
, GetLastError());
369 ret
= CreateDirectoryW(dotW
, NULL
);
370 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
371 "should not create existing path ret %u err %u\n", ret
, GetLastError());
373 ret
= CreateDirectoryW(dotdotW
, NULL
);
374 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
375 "should not create existing path ret %u err %u\n", ret
, GetLastError());
377 GetTempPathW(MAX_PATH
, tmpdir
);
378 tmpdir
[3] = 0; /* truncate the path */
379 ret
= CreateDirectoryW(tmpdir
, NULL
);
380 ok(ret
== FALSE
&& (GetLastError() == ERROR_ACCESS_DENIED
|| GetLastError() == ERROR_ALREADY_EXISTS
),
381 "should deny access to the drive root ret %u err %u\n", ret
, GetLastError());
383 GetTempPathW(MAX_PATH
, tmpdir
);
384 lstrcatW(tmpdir
, tmp_dir_name
);
385 ret
= CreateDirectoryW(tmpdir
, NULL
);
386 ok(ret
== TRUE
, "CreateDirectoryW should always succeed\n");
388 ret
= CreateDirectoryW(tmpdir
, NULL
);
389 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
390 "should not create existing path ret %u err %u\n", ret
, GetLastError());
392 ret
= RemoveDirectoryW(tmpdir
);
393 ok(ret
== TRUE
, "RemoveDirectoryW should always succeed\n");
395 lstrcatW(tmpdir
, questionW
);
396 ret
= CreateDirectoryW(tmpdir
, NULL
);
397 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
398 "CreateDirectoryW with ? wildcard name should fail with error 183, ret=%s error=%d\n",
399 ret
? " True" : "False", GetLastError());
400 ret
= RemoveDirectoryW(tmpdir
);
401 ok(ret
== FALSE
, "RemoveDirectoryW should have failed\n");
403 tmpdir
[lstrlenW(tmpdir
) - 1] = '*';
404 ret
= CreateDirectoryW(tmpdir
, NULL
);
405 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
406 "CreateDirectoryW with * wildcard name should fail with error 183, ret=%s error=%d\n",
407 ret
? " True" : "False", GetLastError());
408 ret
= RemoveDirectoryW(tmpdir
);
409 ok(ret
== FALSE
, "RemoveDirectoryW should have failed\n");
411 GetTempPathW(MAX_PATH
, tmpdir
);
412 lstrcatW(tmpdir
, tmp_dir_name
);
413 lstrcatW(tmpdir
, slashW
);
414 lstrcatW(tmpdir
, tmp_dir_name
);
415 ret
= CreateDirectoryW(tmpdir
, NULL
);
416 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
417 "CreateDirectoryW with multiple nonexistent directories in path should fail ret %u err %u\n",
418 ret
, GetLastError());
419 ret
= RemoveDirectoryW(tmpdir
);
420 ok(ret
== FALSE
, "RemoveDirectoryW should have failed\n");
422 SetCurrentDirectoryW(curdir
);
425 static void test_RemoveDirectoryA(void)
427 char tmpdir
[MAX_PATH
];
430 GetTempPathA(MAX_PATH
, tmpdir
);
431 lstrcatA(tmpdir
, "Please Remove Me");
432 ret
= CreateDirectoryA(tmpdir
, NULL
);
433 ok(ret
== TRUE
, "CreateDirectoryA should always succeed\n");
435 ret
= RemoveDirectoryA(tmpdir
);
436 ok(ret
== TRUE
, "RemoveDirectoryA should always succeed\n");
438 lstrcatA(tmpdir
, "?");
439 ret
= RemoveDirectoryA(tmpdir
);
440 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
441 GetLastError() == ERROR_PATH_NOT_FOUND
),
442 "RemoveDirectoryA with ? wildcard name should fail, ret=%s error=%d\n",
443 ret
? " True" : "False", GetLastError());
445 tmpdir
[lstrlenA(tmpdir
) - 1] = '*';
446 ret
= RemoveDirectoryA(tmpdir
);
447 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
448 GetLastError() == ERROR_PATH_NOT_FOUND
),
449 "RemoveDirectoryA with * wildcard name should fail, ret=%s error=%d\n",
450 ret
? " True" : "False", GetLastError());
453 static void test_RemoveDirectoryW(void)
455 WCHAR tmpdir
[MAX_PATH
];
457 static const WCHAR tmp_dir_name
[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
458 static const WCHAR questionW
[] = {'?',0};
460 GetTempPathW(MAX_PATH
, tmpdir
);
461 lstrcatW(tmpdir
, tmp_dir_name
);
462 ret
= CreateDirectoryW(tmpdir
, NULL
);
463 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
465 win_skip("CreateDirectoryW is not available\n");
469 ok(ret
== TRUE
, "CreateDirectoryW should always succeed\n");
471 ret
= RemoveDirectoryW(tmpdir
);
472 ok(ret
== TRUE
, "RemoveDirectoryW should always succeed\n");
474 lstrcatW(tmpdir
, questionW
);
475 ret
= RemoveDirectoryW(tmpdir
);
476 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
477 "RemoveDirectoryW with wildcard should fail with error 183, ret=%s error=%d\n",
478 ret
? " True" : "False", GetLastError());
480 tmpdir
[lstrlenW(tmpdir
) - 1] = '*';
481 ret
= RemoveDirectoryW(tmpdir
);
482 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
483 "RemoveDirectoryW with * wildcard name should fail with error 183, ret=%s error=%d\n",
484 ret
? " True" : "False", GetLastError());
487 static void test_SetCurrentDirectoryA(void)
490 ok( !SetCurrentDirectoryA( "\\some_dummy_dir" ), "SetCurrentDirectoryA succeeded\n" );
491 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %d\n", GetLastError() );
492 ok( !SetCurrentDirectoryA( "\\some_dummy\\subdir" ), "SetCurrentDirectoryA succeeded\n" );
493 ok( GetLastError() == ERROR_PATH_NOT_FOUND
, "wrong error %d\n", GetLastError() );
496 START_TEST(directory
)
498 test_GetWindowsDirectoryA();
499 test_GetWindowsDirectoryW();
501 test_GetSystemDirectoryA();
502 test_GetSystemDirectoryW();
504 test_CreateDirectoryA();
505 test_CreateDirectoryW();
507 test_RemoveDirectoryA();
508 test_RemoveDirectoryW();
510 test_SetCurrentDirectoryA();