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 ok(len_with_null
<= MAX_PATH
, "should fit into MAX_PATH\n");
69 len
= GetWindowsDirectoryW(buf
, 1);
70 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
71 ok(len
== len_with_null
, "GetWindowsDirectoryW returned %d, expected %d\n",
75 len
= GetWindowsDirectoryW(buf
, len_with_null
- 1);
76 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
77 ok(len
== len_with_null
, "GetWindowsDirectoryW returned %d, expected %d\n",
81 len
= GetWindowsDirectoryW(buf
, len_with_null
);
82 ok(lstrcmpW(buf
, fooW
) != 0, "should touch the buffer\n");
83 ok(len
== lstrlenW(buf
), "returned length should be equal to the length of string\n");
84 ok(len
== len_with_null
-1, "GetWindowsDirectoryW returned %d, expected %d\n",
85 len
, len_with_null
-1);
89 /* If you change something in these tests, please do the same
90 * for GetWindowsDirectory tests.
92 static void test_GetSystemDirectoryA(void)
94 UINT len
, len_with_null
;
97 len_with_null
= GetSystemDirectoryA(NULL
, 0);
98 ok(len_with_null
<= MAX_PATH
, "should fit into MAX_PATH\n");
100 lstrcpyA(buf
, "foo");
101 len
= GetSystemDirectoryA(buf
, 1);
102 ok(lstrcmpA(buf
, "foo") == 0, "should not touch the buffer\n");
103 ok(len
== len_with_null
, "GetSystemDirectoryA returned %d, expected %d\n",
106 lstrcpyA(buf
, "foo");
107 len
= GetSystemDirectoryA(buf
, len_with_null
- 1);
108 ok(lstrcmpA(buf
, "foo") == 0, "should not touch the buffer\n");
109 ok(len
== len_with_null
, "GetSystemDirectoryA returned %d, expected %d\n",
112 lstrcpyA(buf
, "foo");
113 len
= GetSystemDirectoryA(buf
, len_with_null
);
114 ok(lstrcmpA(buf
, "foo") != 0, "should touch the buffer\n");
115 ok(len
== strlen(buf
), "returned length should be equal to the length of string\n");
116 ok(len
== len_with_null
-1, "GetSystemDirectoryW returned %d, expected %d\n",
117 len
, len_with_null
-1);
120 static void test_GetSystemDirectoryW(void)
122 UINT len
, len_with_null
;
124 static const WCHAR fooW
[] = {'f','o','o',0};
126 len_with_null
= GetSystemDirectoryW(NULL
, 0);
127 if (len_with_null
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
129 ok(len_with_null
<= MAX_PATH
, "should fit into MAX_PATH\n");
132 len
= GetSystemDirectoryW(buf
, 1);
133 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
134 ok(len
== len_with_null
, "GetSystemDirectoryW returned %d, expected %d\n",
138 len
= GetSystemDirectoryW(buf
, len_with_null
- 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
);
145 ok(lstrcmpW(buf
, fooW
) != 0, "should touch the buffer\n");
146 ok(len
== lstrlenW(buf
), "returned length should be equal to the length of string\n");
147 ok(len
== len_with_null
-1, "GetSystemDirectoryW returned %d, expected %d\n",
148 len
, len_with_null
-1);
151 static void test_CreateDirectoryA(void)
153 char tmpdir
[MAX_PATH
];
156 ret
= CreateDirectoryA(NULL
, NULL
);
157 ok(ret
== FALSE
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
158 GetLastError() == ERROR_INVALID_PARAMETER
),
159 "CreateDirectoryA(NULL): ret=%d err=%ld\n", ret
, GetLastError());
161 ret
= CreateDirectoryA("", NULL
);
162 ok(ret
== FALSE
&& (GetLastError() == ERROR_BAD_PATHNAME
||
163 GetLastError() == ERROR_PATH_NOT_FOUND
),
164 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
166 ret
= GetSystemDirectoryA(tmpdir
, MAX_PATH
);
167 ok(ret
< MAX_PATH
, "System directory should fit into MAX_PATH\n");
169 ret
= SetCurrentDirectoryA(tmpdir
);
170 ok(ret
== TRUE
, "could not chdir to the System directory\n");
172 ret
= CreateDirectoryA(".", NULL
);
173 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
174 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
177 ret
= CreateDirectoryA("..", NULL
);
178 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
179 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
181 GetTempPathA(MAX_PATH
, tmpdir
);
182 tmpdir
[3] = 0; /* truncate the path */
183 ret
= CreateDirectoryA(tmpdir
, NULL
);
184 ok(ret
== FALSE
&& (GetLastError() == ERROR_ALREADY_EXISTS
||
185 GetLastError() == ERROR_ACCESS_DENIED
),
186 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
188 GetTempPathA(MAX_PATH
, tmpdir
);
189 lstrcatA(tmpdir
, "Please Remove Me");
190 ret
= CreateDirectoryA(tmpdir
, NULL
);
191 ok(ret
== TRUE
, "CreateDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
193 ret
= CreateDirectoryA(tmpdir
, NULL
);
194 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
195 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
197 ret
= RemoveDirectoryA(tmpdir
);
199 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
202 lstrcatA(tmpdir
, "?");
203 ret
= CreateDirectoryA(tmpdir
, NULL
);
204 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
205 GetLastError() == ERROR_PATH_NOT_FOUND
),
206 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
207 RemoveDirectoryA(tmpdir
);
209 tmpdir
[lstrlenA(tmpdir
) - 1] = '*';
210 ret
= CreateDirectoryA(tmpdir
, NULL
);
211 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
212 GetLastError() == ERROR_PATH_NOT_FOUND
),
213 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
214 RemoveDirectoryA(tmpdir
);
216 GetTempPathA(MAX_PATH
, tmpdir
);
217 lstrcatA(tmpdir
, "Please Remove Me/Please Remove Me");
218 ret
= CreateDirectoryA(tmpdir
, NULL
);
219 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
220 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
221 RemoveDirectoryA(tmpdir
);
223 /* Test behavior with a trailing dot.
224 * The directory should be created without the dot.
226 GetTempPathA(MAX_PATH
, tmpdir
);
227 lstrcatA(tmpdir
, "Please Remove Me.");
228 ret
= CreateDirectoryA(tmpdir
, NULL
);
230 "CreateDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
232 lstrcatA(tmpdir
, "/Please Remove Me");
233 ret
= CreateDirectoryA(tmpdir
, NULL
);
235 "CreateDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
236 ret
= RemoveDirectoryA(tmpdir
);
238 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
240 GetTempPathA(MAX_PATH
, tmpdir
);
241 lstrcatA(tmpdir
, "Please Remove Me");
242 ret
= RemoveDirectoryA(tmpdir
);
244 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
246 /* Test behavior with two trailing dots.
247 * The directory should be created without the trailing dots.
249 GetTempPathA(MAX_PATH
, tmpdir
);
250 lstrcatA(tmpdir
, "Please Remove Me..");
251 ret
= CreateDirectoryA(tmpdir
, NULL
);
253 "CreateDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
255 lstrcatA(tmpdir
, "/Please Remove Me");
256 ret
= CreateDirectoryA(tmpdir
, NULL
);
257 ok(ret
== TRUE
|| /* On Win98 */
258 (ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
), /* On NT! */
259 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
262 ret
= RemoveDirectoryA(tmpdir
);
264 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
267 GetTempPathA(MAX_PATH
, tmpdir
);
268 lstrcatA(tmpdir
, "Please Remove Me");
269 ret
= RemoveDirectoryA(tmpdir
);
271 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
273 /* Test behavior with a trailing space.
274 * The directory should be created without the trailing space.
276 GetTempPathA(MAX_PATH
, tmpdir
);
277 lstrcatA(tmpdir
, "Please Remove Me ");
278 ret
= CreateDirectoryA(tmpdir
, NULL
);
280 "CreateDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
282 lstrcatA(tmpdir
, "/Please Remove Me");
283 ret
= CreateDirectoryA(tmpdir
, NULL
);
284 ok(ret
== TRUE
|| /* On Win98 */
285 (ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
), /* On NT! */
286 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
289 ret
= RemoveDirectoryA(tmpdir
);
291 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
294 GetTempPathA(MAX_PATH
, tmpdir
);
295 lstrcatA(tmpdir
, "Please Remove Me");
296 ret
= RemoveDirectoryA(tmpdir
);
298 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
300 /* Test behavior with a trailing space.
301 * The directory should be created without the trailing spaces.
303 GetTempPathA(MAX_PATH
, tmpdir
);
304 lstrcatA(tmpdir
, "Please Remove Me ");
305 ret
= CreateDirectoryA(tmpdir
, NULL
);
307 "CreateDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
309 lstrcatA(tmpdir
, "/Please Remove Me");
310 ret
= CreateDirectoryA(tmpdir
, NULL
);
311 ok(ret
== TRUE
|| /* On Win98 */
312 (ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
), /* On NT! */
313 "CreateDirectoryA(%s): ret=%d err=%ld\n", tmpdir
, ret
, GetLastError());
316 ret
= RemoveDirectoryA(tmpdir
);
318 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
321 GetTempPathA(MAX_PATH
, tmpdir
);
322 lstrcatA(tmpdir
, "Please Remove Me");
323 ret
= RemoveDirectoryA(tmpdir
);
325 "RemoveDirectoryA(%s) failed err=%ld\n", tmpdir
, GetLastError());
328 static void test_CreateDirectoryW(void)
330 WCHAR tmpdir
[MAX_PATH
];
332 static const WCHAR empty_strW
[] = { 0 };
333 static const WCHAR tmp_dir_name
[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
334 static const WCHAR dotW
[] = {'.',0};
335 static const WCHAR slashW
[] = {'/',0};
336 static const WCHAR dotdotW
[] = {'.','.',0};
337 static const WCHAR questionW
[] = {'?',0};
339 ret
= CreateDirectoryW(NULL
, NULL
);
340 if (!ret
&& GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
342 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
, "should not create NULL path\n");
344 ret
= CreateDirectoryW(empty_strW
, NULL
);
345 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
, "should not create empty path\n");
347 ret
= GetSystemDirectoryW(tmpdir
, MAX_PATH
);
348 ok(ret
< MAX_PATH
, "System directory should fit into MAX_PATH\n");
350 ret
= SetCurrentDirectoryW(tmpdir
);
351 ok(ret
== TRUE
, "could not chdir to the System directory\n");
353 ret
= CreateDirectoryW(dotW
, NULL
);
354 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
, "should not create existing path\n");
356 ret
= CreateDirectoryW(dotdotW
, NULL
);
357 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
, "should not create existing path\n");
359 GetTempPathW(MAX_PATH
, tmpdir
);
360 tmpdir
[3] = 0; /* truncate the path */
361 ret
= CreateDirectoryW(tmpdir
, NULL
);
362 ok(ret
== FALSE
&& GetLastError() == ERROR_ACCESS_DENIED
, "should deny access to the drive root\n");
364 GetTempPathW(MAX_PATH
, tmpdir
);
365 lstrcatW(tmpdir
, tmp_dir_name
);
366 ret
= CreateDirectoryW(tmpdir
, NULL
);
367 ok(ret
== TRUE
, "CreateDirectoryW should always succeed\n");
369 ret
= CreateDirectoryW(tmpdir
, NULL
);
370 ok(ret
== FALSE
&& GetLastError() == ERROR_ALREADY_EXISTS
, "should not create existing path\n");
372 ret
= RemoveDirectoryW(tmpdir
);
373 ok(ret
== TRUE
, "RemoveDirectoryW should always succeed\n");
375 lstrcatW(tmpdir
, questionW
);
376 ret
= CreateDirectoryW(tmpdir
, NULL
);
377 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
378 "CreateDirectoryW with ? wildcard name should fail with error 183, ret=%s error=%ld\n",
379 ret
? " True" : "False", GetLastError());
380 ret
= RemoveDirectoryW(tmpdir
);
382 tmpdir
[lstrlenW(tmpdir
) - 1] = '*';
383 ret
= CreateDirectoryW(tmpdir
, NULL
);
384 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
385 "CreateDirectoryW with * wildcard name should fail with error 183, ret=%s error=%ld\n",
386 ret
? " True" : "False", GetLastError());
387 ret
= RemoveDirectoryW(tmpdir
);
389 GetTempPathW(MAX_PATH
, tmpdir
);
390 lstrcatW(tmpdir
, tmp_dir_name
);
391 lstrcatW(tmpdir
, slashW
);
392 lstrcatW(tmpdir
, tmp_dir_name
);
393 ret
= CreateDirectoryW(tmpdir
, NULL
);
394 ok(ret
== FALSE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
395 "CreateDirectoryW with multiple nonexistent directories in path should fail\n");
396 ret
= RemoveDirectoryW(tmpdir
);
399 static void test_RemoveDirectoryA(void)
401 char tmpdir
[MAX_PATH
];
404 GetTempPathA(MAX_PATH
, tmpdir
);
405 lstrcatA(tmpdir
, "Please Remove Me");
406 ret
= CreateDirectoryA(tmpdir
, NULL
);
407 ok(ret
== TRUE
, "CreateDirectoryA should always succeed\n");
409 ret
= RemoveDirectoryA(tmpdir
);
410 ok(ret
== TRUE
, "RemoveDirectoryA should always succeed\n");
412 lstrcatA(tmpdir
, "?");
413 ret
= RemoveDirectoryA(tmpdir
);
414 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
415 GetLastError() == ERROR_PATH_NOT_FOUND
),
416 "RemoveDirectoryA with ? wildcard name should fail, ret=%s error=%ld\n",
417 ret
? " True" : "False", GetLastError());
419 tmpdir
[lstrlenA(tmpdir
) - 1] = '*';
420 ret
= RemoveDirectoryA(tmpdir
);
421 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_NAME
||
422 GetLastError() == ERROR_PATH_NOT_FOUND
),
423 "RemoveDirectoryA with * wildcard name should fail, ret=%s error=%ld\n",
424 ret
? " True" : "False", GetLastError());
427 static void test_RemoveDirectoryW(void)
429 WCHAR tmpdir
[MAX_PATH
];
431 static const WCHAR tmp_dir_name
[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
432 static const WCHAR questionW
[] = {'?',0};
434 GetTempPathW(MAX_PATH
, tmpdir
);
435 lstrcatW(tmpdir
, tmp_dir_name
);
436 ret
= CreateDirectoryW(tmpdir
, NULL
);
437 if (!ret
&& GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
440 ok(ret
== TRUE
, "CreateDirectoryW should always succeed\n");
442 ret
= RemoveDirectoryW(tmpdir
);
443 ok(ret
== TRUE
, "RemoveDirectoryW should always succeed\n");
445 lstrcatW(tmpdir
, questionW
);
446 ret
= RemoveDirectoryW(tmpdir
);
447 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
448 "RemoveDirectoryW with wildcard should fail with error 183, ret=%s error=%ld\n",
449 ret
? " True" : "False", GetLastError());
451 tmpdir
[lstrlenW(tmpdir
) - 1] = '*';
452 ret
= RemoveDirectoryW(tmpdir
);
453 ok(ret
== FALSE
&& GetLastError() == ERROR_INVALID_NAME
,
454 "RemoveDirectoryW with * wildcard name should fail with error 183, ret=%s error=%ld\n",
455 ret
? " True" : "False", GetLastError());
458 static void test_SetCurrentDirectoryA(void)
461 ok( !SetCurrentDirectoryA( "\\some_dummy_dir" ), "SetCurrentDirectoryA succeeded\n" );
462 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %ld\n", GetLastError() );
463 ok( !SetCurrentDirectoryA( "\\some_dummy\\subdir" ), "SetCurrentDirectoryA succeeded\n" );
464 ok( GetLastError() == ERROR_PATH_NOT_FOUND
, "wrong error %ld\n", GetLastError() );
467 START_TEST(directory
)
469 test_GetWindowsDirectoryA();
470 test_GetWindowsDirectoryW();
472 test_GetSystemDirectoryA();
473 test_GetSystemDirectoryW();
475 test_CreateDirectoryA();
476 test_CreateDirectoryW();
478 test_RemoveDirectoryA();
479 test_RemoveDirectoryW();
481 test_SetCurrentDirectoryA();