Added a couple of tests for SetCurrentDirectoryA.
[wine] / dlls / kernel / tests / directory.c
1 /*
2  * Unit test suite for directory functions.
3  *
4  * Copyright 2002 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27
28 /* If you change something in these tests, please do the same
29  * for GetSystemDirectory tests.
30  */
31 static void test_GetWindowsDirectoryA(void)
32 {
33     UINT len, len_with_null;
34     char buf[MAX_PATH];
35
36     len_with_null = GetWindowsDirectoryA(NULL, 0);
37     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
38
39     lstrcpyA(buf, "foo");
40     len_with_null = GetWindowsDirectoryA(buf, 1);
41     ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
42
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);
48
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);
55 }
56
57 static void test_GetWindowsDirectoryW(void)
58 {
59     UINT len, len_with_null;
60     WCHAR buf[MAX_PATH];
61     static const WCHAR fooW[] = {'f','o','o',0};
62
63     len_with_null = GetWindowsDirectoryW(NULL, 0);
64     if (len_with_null==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
65         return;
66     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
67
68     lstrcpyW(buf, fooW);
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",
72        len, len_with_null);
73
74     lstrcpyW(buf, fooW);
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",
78        len, len_with_null);
79
80     lstrcpyW(buf, fooW);
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);
86 }
87
88
89 /* If you change something in these tests, please do the same
90  * for GetWindowsDirectory tests.
91  */
92 static void test_GetSystemDirectoryA(void)
93 {
94     UINT len, len_with_null;
95     char buf[MAX_PATH];
96
97     len_with_null = GetSystemDirectoryA(NULL, 0);
98     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
99
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",
104        len, len_with_null);
105
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",
110        len, len_with_null);
111
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);
118 }
119
120 static void test_GetSystemDirectoryW(void)
121 {
122     UINT len, len_with_null;
123     WCHAR buf[MAX_PATH];
124     static const WCHAR fooW[] = {'f','o','o',0};
125
126     len_with_null = GetSystemDirectoryW(NULL, 0);
127     if (len_with_null==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
128         return;
129     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
130
131     lstrcpyW(buf, fooW);
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",
135        len, len_with_null);
136
137     lstrcpyW(buf, fooW);
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",
141        len, len_with_null);
142
143     lstrcpyW(buf, fooW);
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);
149 }
150
151 static void test_CreateDirectoryA(void)
152 {
153     char tmpdir[MAX_PATH];
154     BOOL ret;
155
156     ret = CreateDirectoryA(NULL, NULL);
157     ok(ret == FALSE && (GetLastError() == ERROR_PATH_NOT_FOUND ||
158                         GetLastError() == ERROR_INVALID_PARAMETER),
159        "CreateDirectoryA(NULL,NULL): ret=%d error=%ld\n",ret,GetLastError());
160
161     ret = CreateDirectoryA("", NULL);
162     ok(ret == FALSE && (GetLastError() == ERROR_BAD_PATHNAME ||
163                         GetLastError() == ERROR_PATH_NOT_FOUND),
164        "CreateDirectoryA(\"\",NULL): ret=%d error=%ld\n",ret,GetLastError());
165
166     ret = GetSystemDirectoryA(tmpdir, MAX_PATH);
167     ok(ret < MAX_PATH, "System directory should fit into MAX_PATH\n");
168
169     ret = SetCurrentDirectoryA(tmpdir);
170     ok(ret == TRUE, "could not chdir to the System directory\n");
171
172     ret = CreateDirectoryA(".", NULL);
173     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path\n");
174
175     ret = CreateDirectoryA("..", NULL);
176     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path\n");
177
178     GetTempPathA(MAX_PATH, tmpdir);
179     tmpdir[3] = 0; /* truncate the path */
180     ret = CreateDirectoryA(tmpdir, NULL);
181     ok(ret == FALSE && (GetLastError() == ERROR_ALREADY_EXISTS ||
182                         GetLastError() == ERROR_ACCESS_DENIED),
183        "CreateDirectoryA(drive_root): ret=%d error=%ld\n",ret,GetLastError());
184
185     GetTempPathA(MAX_PATH, tmpdir);
186     lstrcatA(tmpdir, "Please Remove Me");
187     ret = CreateDirectoryA(tmpdir, NULL);
188     ok(ret == TRUE, "CreateDirectoryA should always succeed\n");
189
190     ret = CreateDirectoryA(tmpdir, NULL);
191     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path\n");
192
193     ret = RemoveDirectoryA(tmpdir);
194     ok(ret == TRUE, "RemoveDirectoryA should always succeed\n");
195
196     lstrcatA(tmpdir, "?");
197     ret = CreateDirectoryA(tmpdir, NULL);
198     ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
199                         GetLastError() == ERROR_PATH_NOT_FOUND),
200        "CreateDirectoryA with ? wildcard name should fail, ret=%s error=%ld\n",
201        ret ? " True" : "False", GetLastError());
202     ret = RemoveDirectoryA(tmpdir);
203
204     tmpdir[lstrlenA(tmpdir) - 1] = '*';
205     ret = CreateDirectoryA(tmpdir, NULL);
206     ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
207                         GetLastError() == ERROR_PATH_NOT_FOUND),
208        "CreateDirectoryA with * wildcard name should fail, ret=%s error=%ld\n",
209        ret ? " True" : "False", GetLastError());
210     ret = RemoveDirectoryA(tmpdir);
211 }
212
213 static void test_CreateDirectoryW(void)
214 {
215     WCHAR tmpdir[MAX_PATH];
216     BOOL ret;
217     static const WCHAR empty_strW[] = { 0 };
218     static const WCHAR tmp_dir_name[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
219     static const WCHAR dotW[] = {'.',0};
220     static const WCHAR dotdotW[] = {'.','.',0};
221     static const WCHAR questionW[] = {'?',0};
222
223     ret = CreateDirectoryW(NULL, NULL);
224     if (!ret && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
225         return;
226     ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND, "should not create NULL path\n");
227
228     ret = CreateDirectoryW(empty_strW, NULL);
229     ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND, "should not create empty path\n");
230
231     ret = GetSystemDirectoryW(tmpdir, MAX_PATH);
232     ok(ret < MAX_PATH, "System directory should fit into MAX_PATH\n");
233
234     ret = SetCurrentDirectoryW(tmpdir);
235     ok(ret == TRUE, "could not chdir to the System directory\n");
236
237     ret = CreateDirectoryW(dotW, NULL);
238     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path\n");
239
240     ret = CreateDirectoryW(dotdotW, NULL);
241     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path\n");
242
243     GetTempPathW(MAX_PATH, tmpdir);
244     tmpdir[3] = 0; /* truncate the path */
245     ret = CreateDirectoryW(tmpdir, NULL);
246     ok(ret == FALSE && GetLastError() == ERROR_ACCESS_DENIED, "should deny access to the drive root\n");
247
248     GetTempPathW(MAX_PATH, tmpdir);
249     lstrcatW(tmpdir, tmp_dir_name);
250     ret = CreateDirectoryW(tmpdir, NULL);
251     ok(ret == TRUE, "CreateDirectoryW should always succeed\n");
252
253     ret = CreateDirectoryW(tmpdir, NULL);
254     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path\n");
255
256     ret = RemoveDirectoryW(tmpdir);
257     ok(ret == TRUE, "RemoveDirectoryW should always succeed\n");
258
259     lstrcatW(tmpdir, questionW);
260     ret = CreateDirectoryW(tmpdir, NULL);
261     ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
262        "CreateDirectoryW with ? wildcard name should fail with error 183, ret=%s error=%ld\n",
263        ret ? " True" : "False", GetLastError());
264     ret = RemoveDirectoryW(tmpdir);
265
266     tmpdir[lstrlenW(tmpdir) - 1] = '*';
267     ret = CreateDirectoryW(tmpdir, NULL);
268     ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
269        "CreateDirectoryW with * wildcard name should fail with error 183, ret=%s error=%ld\n",
270        ret ? " True" : "False", GetLastError());
271     ret = RemoveDirectoryW(tmpdir);
272 }
273
274 static void test_RemoveDirectoryA(void)
275 {
276     char tmpdir[MAX_PATH];
277     BOOL ret;
278
279     GetTempPathA(MAX_PATH, tmpdir);
280     lstrcatA(tmpdir, "Please Remove Me");
281     ret = CreateDirectoryA(tmpdir, NULL);
282     ok(ret == TRUE, "CreateDirectoryA should always succeed\n");
283
284     ret = RemoveDirectoryA(tmpdir);
285     ok(ret == TRUE, "RemoveDirectoryA should always succeed\n");
286
287     lstrcatA(tmpdir, "?");
288     ret = RemoveDirectoryA(tmpdir);
289     ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
290                         GetLastError() == ERROR_PATH_NOT_FOUND),
291        "RemoveDirectoryA with ? wildcard name should fail, ret=%s error=%ld\n",
292        ret ? " True" : "False", GetLastError());
293
294     tmpdir[lstrlenA(tmpdir) - 1] = '*';
295     ret = RemoveDirectoryA(tmpdir);
296     ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
297                         GetLastError() == ERROR_PATH_NOT_FOUND),
298        "RemoveDirectoryA with * wildcard name should fail, ret=%s error=%ld\n",
299        ret ? " True" : "False", GetLastError());
300 }
301
302 static void test_RemoveDirectoryW(void)
303 {
304     WCHAR tmpdir[MAX_PATH];
305     BOOL ret;
306     static const WCHAR tmp_dir_name[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
307     static const WCHAR questionW[] = {'?',0};
308
309     GetTempPathW(MAX_PATH, tmpdir);
310     lstrcatW(tmpdir, tmp_dir_name);
311     ret = CreateDirectoryW(tmpdir, NULL);
312     if (!ret && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
313       return;
314
315     ok(ret == TRUE, "CreateDirectoryW should always succeed\n");
316
317     ret = RemoveDirectoryW(tmpdir);
318     ok(ret == TRUE, "RemoveDirectoryW should always succeed\n");
319
320     lstrcatW(tmpdir, questionW);
321     ret = RemoveDirectoryW(tmpdir);
322     ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
323        "RemoveDirectoryW with wildcard should fail with error 183, ret=%s error=%ld\n",
324        ret ? " True" : "False", GetLastError());
325
326     tmpdir[lstrlenW(tmpdir) - 1] = '*';
327     ret = RemoveDirectoryW(tmpdir);
328     ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
329        "RemoveDirectoryW with * wildcard name should fail with error 183, ret=%s error=%ld\n",
330        ret ? " True" : "False", GetLastError());
331 }
332
333 static void test_SetCurrentDirectoryA(void)
334 {
335     SetLastError(0);
336     ok( !SetCurrentDirectoryA( "\\some_dummy_dir" ), "SetCurrentDirectoryA succeeded\n" );
337     ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %ld\n", GetLastError() );
338     ok( !SetCurrentDirectoryA( "\\some_dummy\\subdir" ), "SetCurrentDirectoryA succeeded\n" );
339     ok( GetLastError() == ERROR_PATH_NOT_FOUND, "wrong error %ld\n", GetLastError() );
340 }
341
342 START_TEST(directory)
343 {
344     test_GetWindowsDirectoryA();
345     test_GetWindowsDirectoryW();
346
347     test_GetSystemDirectoryA();
348     test_GetSystemDirectoryW();
349
350     test_CreateDirectoryA();
351     test_CreateDirectoryW();
352
353     test_RemoveDirectoryA();
354     test_RemoveDirectoryW();
355
356     test_SetCurrentDirectoryA();
357 }