windowscodecs/tests: Fix compilation on systems that don't support nameless structs.
[wine/multimedia.git] / dlls / kernel32 / tests / directory.c
blob9baae472ba97e124b6fd10de1c334d82f5f9cd4d
1 /*
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
21 #include <stdarg.h>
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.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;
34 char buf[MAX_PATH];
36 len_with_null = GetWindowsDirectoryA(NULL, 0);
37 ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
39 lstrcpyA(buf, "foo");
40 len_with_null = GetWindowsDirectoryA(buf, 1);
41 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
43 lstrcpyA(buf, "foo");
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",
47 len, len_with_null);
49 lstrcpyA(buf, "foo");
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;
60 WCHAR buf[MAX_PATH];
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");
67 return;
69 ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
71 lstrcpyW(buf, fooW);
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",
75 len, len_with_null);
77 lstrcpyW(buf, fooW);
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",
81 len, len_with_null);
83 lstrcpyW(buf, fooW);
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;
98 char buf[MAX_PATH];
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",
107 len, len_with_null);
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",
113 len, len_with_null);
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;
126 WCHAR buf[MAX_PATH];
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");
133 return;
135 ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
137 lstrcpyW(buf, fooW);
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",
141 len, len_with_null);
143 lstrcpyW(buf, fooW);
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",
147 len, len_with_null);
149 lstrcpyW(buf, fooW);
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 BOOL ret;
162 ret = CreateDirectoryA(NULL, NULL);
163 ok(ret == FALSE && (GetLastError() == ERROR_PATH_NOT_FOUND ||
164 GetLastError() == ERROR_INVALID_PARAMETER),
165 "CreateDirectoryA(NULL): ret=%d err=%d\n", ret, GetLastError());
167 ret = CreateDirectoryA("", NULL);
168 ok(ret == FALSE && (GetLastError() == ERROR_BAD_PATHNAME ||
169 GetLastError() == ERROR_PATH_NOT_FOUND),
170 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
172 ret = GetSystemDirectoryA(tmpdir, MAX_PATH);
173 ok(ret < MAX_PATH, "System directory should fit into MAX_PATH\n");
175 ret = SetCurrentDirectoryA(tmpdir);
176 ok(ret == TRUE, "could not chdir to the System directory\n");
178 ret = CreateDirectoryA(".", NULL);
179 ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
180 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
183 ret = CreateDirectoryA("..", NULL);
184 ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
185 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
187 GetTempPathA(MAX_PATH, tmpdir);
188 tmpdir[3] = 0; /* truncate the path */
189 ret = CreateDirectoryA(tmpdir, NULL);
190 ok(ret == FALSE && (GetLastError() == ERROR_ALREADY_EXISTS ||
191 GetLastError() == ERROR_ACCESS_DENIED),
192 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
194 GetTempPathA(MAX_PATH, tmpdir);
195 lstrcatA(tmpdir, "Please Remove Me");
196 ret = CreateDirectoryA(tmpdir, NULL);
197 ok(ret == TRUE, "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
199 ret = CreateDirectoryA(tmpdir, NULL);
200 ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
201 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
203 ret = RemoveDirectoryA(tmpdir);
204 ok(ret == TRUE,
205 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
208 lstrcatA(tmpdir, "?");
209 ret = CreateDirectoryA(tmpdir, NULL);
210 ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
211 GetLastError() == ERROR_PATH_NOT_FOUND),
212 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
213 RemoveDirectoryA(tmpdir);
215 tmpdir[lstrlenA(tmpdir) - 1] = '*';
216 ret = CreateDirectoryA(tmpdir, NULL);
217 ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
218 GetLastError() == ERROR_PATH_NOT_FOUND),
219 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
220 RemoveDirectoryA(tmpdir);
222 GetTempPathA(MAX_PATH, tmpdir);
223 lstrcatA(tmpdir, "Please Remove Me/Please Remove Me");
224 ret = CreateDirectoryA(tmpdir, NULL);
225 ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
226 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
227 RemoveDirectoryA(tmpdir);
229 /* Test behavior with a trailing dot.
230 * The directory should be created without the dot.
232 GetTempPathA(MAX_PATH, tmpdir);
233 lstrcatA(tmpdir, "Please Remove Me.");
234 ret = CreateDirectoryA(tmpdir, NULL);
235 ok(ret == TRUE,
236 "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
238 lstrcatA(tmpdir, "/Please Remove Me");
239 ret = CreateDirectoryA(tmpdir, NULL);
240 ok(ret == TRUE,
241 "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
242 ret = RemoveDirectoryA(tmpdir);
243 ok(ret == TRUE,
244 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
246 GetTempPathA(MAX_PATH, tmpdir);
247 lstrcatA(tmpdir, "Please Remove Me");
248 ret = RemoveDirectoryA(tmpdir);
249 ok(ret == TRUE,
250 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
252 /* Test behavior with two trailing dots.
253 * The directory should be created without the trailing dots.
255 GetTempPathA(MAX_PATH, tmpdir);
256 lstrcatA(tmpdir, "Please Remove Me..");
257 ret = CreateDirectoryA(tmpdir, NULL);
258 ok(ret == TRUE,
259 "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
261 lstrcatA(tmpdir, "/Please Remove Me");
262 ret = CreateDirectoryA(tmpdir, NULL);
263 ok(ret == TRUE || /* On Win98 */
264 (ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND), /* On NT! */
265 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
266 if (ret == TRUE)
268 ret = RemoveDirectoryA(tmpdir);
269 ok(ret == TRUE,
270 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
273 GetTempPathA(MAX_PATH, tmpdir);
274 lstrcatA(tmpdir, "Please Remove Me");
275 ret = RemoveDirectoryA(tmpdir);
276 ok(ret == TRUE,
277 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
279 /* Test behavior with a trailing space.
280 * The directory should be created without the trailing space.
282 GetTempPathA(MAX_PATH, tmpdir);
283 lstrcatA(tmpdir, "Please Remove Me ");
284 ret = CreateDirectoryA(tmpdir, NULL);
285 ok(ret == TRUE,
286 "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
288 lstrcatA(tmpdir, "/Please Remove Me");
289 ret = CreateDirectoryA(tmpdir, NULL);
290 ok(ret == TRUE || /* On Win98 */
291 (ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND), /* On NT! */
292 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
293 if (ret == TRUE)
295 ret = RemoveDirectoryA(tmpdir);
296 ok(ret == TRUE,
297 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
300 GetTempPathA(MAX_PATH, tmpdir);
301 lstrcatA(tmpdir, "Please Remove Me");
302 ret = RemoveDirectoryA(tmpdir);
303 ok(ret == TRUE,
304 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
306 /* Test behavior with a trailing space.
307 * The directory should be created without the trailing spaces.
309 GetTempPathA(MAX_PATH, tmpdir);
310 lstrcatA(tmpdir, "Please Remove Me ");
311 ret = CreateDirectoryA(tmpdir, NULL);
312 ok(ret == TRUE,
313 "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
315 lstrcatA(tmpdir, "/Please Remove Me");
316 ret = CreateDirectoryA(tmpdir, NULL);
317 ok(ret == TRUE || /* On Win98 */
318 (ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND), /* On NT! */
319 "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
320 if (ret == TRUE)
322 ret = RemoveDirectoryA(tmpdir);
323 ok(ret == TRUE,
324 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
327 GetTempPathA(MAX_PATH, tmpdir);
328 lstrcatA(tmpdir, "Please Remove Me");
329 ret = RemoveDirectoryA(tmpdir);
330 ok(ret == TRUE,
331 "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
334 static void test_CreateDirectoryW(void)
336 WCHAR tmpdir[MAX_PATH];
337 BOOL ret;
338 static const WCHAR empty_strW[] = { 0 };
339 static const WCHAR tmp_dir_name[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
340 static const WCHAR dotW[] = {'.',0};
341 static const WCHAR slashW[] = {'/',0};
342 static const WCHAR dotdotW[] = {'.','.',0};
343 static const WCHAR questionW[] = {'?',0};
345 ret = CreateDirectoryW(NULL, NULL);
346 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
348 win_skip("CreateDirectoryW is not available\n");
349 return;
351 ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
352 "should not create NULL path ret %u err %u\n", ret, GetLastError());
354 ret = CreateDirectoryW(empty_strW, NULL);
355 ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
356 "should not create empty path ret %u err %u\n", ret, GetLastError());
358 ret = GetSystemDirectoryW(tmpdir, MAX_PATH);
359 ok(ret < MAX_PATH, "System directory should fit into MAX_PATH\n");
361 ret = SetCurrentDirectoryW(tmpdir);
362 ok(ret == TRUE, "could not chdir to the System directory ret %u err %u\n", ret, GetLastError());
364 ret = CreateDirectoryW(dotW, NULL);
365 ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
366 "should not create existing path ret %u err %u\n", ret, GetLastError());
368 ret = CreateDirectoryW(dotdotW, NULL);
369 ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
370 "should not create existing path ret %u err %u\n", ret, GetLastError());
372 GetTempPathW(MAX_PATH, tmpdir);
373 tmpdir[3] = 0; /* truncate the path */
374 ret = CreateDirectoryW(tmpdir, NULL);
375 ok(ret == FALSE && (GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_ALREADY_EXISTS),
376 "should deny access to the drive root ret %u err %u\n", ret, GetLastError());
378 GetTempPathW(MAX_PATH, tmpdir);
379 lstrcatW(tmpdir, tmp_dir_name);
380 ret = CreateDirectoryW(tmpdir, NULL);
381 ok(ret == TRUE, "CreateDirectoryW should always succeed\n");
383 ret = CreateDirectoryW(tmpdir, NULL);
384 ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
385 "should not create existing path ret %u err %u\n", ret, GetLastError());
387 ret = RemoveDirectoryW(tmpdir);
388 ok(ret == TRUE, "RemoveDirectoryW should always succeed\n");
390 lstrcatW(tmpdir, questionW);
391 ret = CreateDirectoryW(tmpdir, NULL);
392 ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
393 "CreateDirectoryW with ? wildcard name should fail with error 183, ret=%s error=%d\n",
394 ret ? " True" : "False", GetLastError());
395 ret = RemoveDirectoryW(tmpdir);
396 ok(ret == FALSE, "RemoveDirectoryW should have failed\n");
398 tmpdir[lstrlenW(tmpdir) - 1] = '*';
399 ret = CreateDirectoryW(tmpdir, NULL);
400 ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
401 "CreateDirectoryW with * wildcard name should fail with error 183, ret=%s error=%d\n",
402 ret ? " True" : "False", GetLastError());
403 ret = RemoveDirectoryW(tmpdir);
404 ok(ret == FALSE, "RemoveDirectoryW should have failed\n");
406 GetTempPathW(MAX_PATH, tmpdir);
407 lstrcatW(tmpdir, tmp_dir_name);
408 lstrcatW(tmpdir, slashW);
409 lstrcatW(tmpdir, tmp_dir_name);
410 ret = CreateDirectoryW(tmpdir, NULL);
411 ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
412 "CreateDirectoryW with multiple nonexistent directories in path should fail ret %u err %u\n",
413 ret, GetLastError());
414 ret = RemoveDirectoryW(tmpdir);
415 ok(ret == FALSE, "RemoveDirectoryW should have failed\n");
418 static void test_RemoveDirectoryA(void)
420 char tmpdir[MAX_PATH];
421 BOOL ret;
423 GetTempPathA(MAX_PATH, tmpdir);
424 lstrcatA(tmpdir, "Please Remove Me");
425 ret = CreateDirectoryA(tmpdir, NULL);
426 ok(ret == TRUE, "CreateDirectoryA should always succeed\n");
428 ret = RemoveDirectoryA(tmpdir);
429 ok(ret == TRUE, "RemoveDirectoryA should always succeed\n");
431 lstrcatA(tmpdir, "?");
432 ret = RemoveDirectoryA(tmpdir);
433 ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
434 GetLastError() == ERROR_PATH_NOT_FOUND),
435 "RemoveDirectoryA with ? wildcard name should fail, ret=%s error=%d\n",
436 ret ? " True" : "False", GetLastError());
438 tmpdir[lstrlenA(tmpdir) - 1] = '*';
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());
446 static void test_RemoveDirectoryW(void)
448 WCHAR tmpdir[MAX_PATH];
449 BOOL ret;
450 static const WCHAR tmp_dir_name[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
451 static const WCHAR questionW[] = {'?',0};
453 GetTempPathW(MAX_PATH, tmpdir);
454 lstrcatW(tmpdir, tmp_dir_name);
455 ret = CreateDirectoryW(tmpdir, NULL);
456 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
458 win_skip("CreateDirectoryW is not available\n");
459 return;
462 ok(ret == TRUE, "CreateDirectoryW should always succeed\n");
464 ret = RemoveDirectoryW(tmpdir);
465 ok(ret == TRUE, "RemoveDirectoryW should always succeed\n");
467 lstrcatW(tmpdir, questionW);
468 ret = RemoveDirectoryW(tmpdir);
469 ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
470 "RemoveDirectoryW with wildcard should fail with error 183, ret=%s error=%d\n",
471 ret ? " True" : "False", GetLastError());
473 tmpdir[lstrlenW(tmpdir) - 1] = '*';
474 ret = RemoveDirectoryW(tmpdir);
475 ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
476 "RemoveDirectoryW with * wildcard name should fail with error 183, ret=%s error=%d\n",
477 ret ? " True" : "False", GetLastError());
480 static void test_SetCurrentDirectoryA(void)
482 SetLastError(0);
483 ok( !SetCurrentDirectoryA( "\\some_dummy_dir" ), "SetCurrentDirectoryA succeeded\n" );
484 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %d\n", GetLastError() );
485 ok( !SetCurrentDirectoryA( "\\some_dummy\\subdir" ), "SetCurrentDirectoryA succeeded\n" );
486 ok( GetLastError() == ERROR_PATH_NOT_FOUND, "wrong error %d\n", GetLastError() );
489 START_TEST(directory)
491 test_GetWindowsDirectoryA();
492 test_GetWindowsDirectoryW();
494 test_GetSystemDirectoryA();
495 test_GetSystemDirectoryW();
497 test_CreateDirectoryA();
498 test_CreateDirectoryW();
500 test_RemoveDirectoryA();
501 test_RemoveDirectoryW();
503 test_SetCurrentDirectoryA();