ok() does not support '%S'. Store the Ansi version, convert to Unicode
[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 "wine/test.h"
22 #include "winbase.h"
23 #include "winerror.h"
24
25 /* If you change something in these tests, please do the same
26  * for GetSystemDirectory tests.
27  */
28 static void test_GetWindowsDirectoryA(void)
29 {
30     UINT len, len_with_null;
31     char buf[MAX_PATH];
32
33     len_with_null = GetWindowsDirectoryA(NULL, 0);
34     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH");
35
36     lstrcpyA(buf, "foo");
37     len_with_null = GetWindowsDirectoryA(buf, 1);
38     ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
39
40     lstrcpyA(buf, "foo");
41     len = GetWindowsDirectoryA(buf, len_with_null - 1);
42     ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
43     ok(len == len_with_null, "GetWindowsDirectoryW returned %d, expected %d",
44        len, len_with_null);
45
46     lstrcpyA(buf, "foo");
47     len = GetWindowsDirectoryA(buf, len_with_null);
48     ok(lstrcmpA(buf, "foo") != 0, "should touch the buffer");
49     ok(len == strlen(buf), "returned length should be equal to the length of string");
50     ok(len == len_with_null-1, "GetWindowsDirectoryA returned %d, expected %d",
51        len, len_with_null-1);
52 }
53
54 static void test_GetWindowsDirectoryW(void)
55 {
56     UINT len, len_with_null;
57     WCHAR buf[MAX_PATH];
58     static const WCHAR fooW[] = {'f','o','o',0};
59
60     len_with_null = GetWindowsDirectoryW(NULL, 0);
61     if (len_with_null==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
62         return;
63     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH");
64
65     lstrcpyW(buf, fooW);
66     len = GetWindowsDirectoryW(buf, 1);
67     ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
68     ok(len == len_with_null, "GetWindowsDirectoryW returned %d, expected %d",
69        len, len_with_null);
70
71     lstrcpyW(buf, fooW);
72     len = GetWindowsDirectoryW(buf, len_with_null - 1);
73     ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
74     ok(len == len_with_null, "GetWindowsDirectoryW returned %d, expected %d",
75        len, len_with_null);
76
77     lstrcpyW(buf, fooW);
78     len = GetWindowsDirectoryW(buf, len_with_null);
79     ok(lstrcmpW(buf, fooW) != 0, "should touch the buffer");
80     ok(len == lstrlenW(buf), "returned length should be equal to the length of string");
81     ok(len == len_with_null-1, "GetWindowsDirectoryW returned %d, expected %d",
82        len, len_with_null-1);
83 }
84
85
86 /* If you change something in these tests, please do the same
87  * for GetWindowsDirectory tests.
88  */
89 static void test_GetSystemDirectoryA(void)
90 {
91     UINT len, len_with_null;
92     char buf[MAX_PATH];
93
94     len_with_null = GetSystemDirectoryA(NULL, 0);
95     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH");
96
97     lstrcpyA(buf, "foo");
98     len = GetSystemDirectoryA(buf, 1);
99     ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
100     ok(len == len_with_null, "GetSystemDirectoryA returned %d, expected %d",
101        len, len_with_null);
102
103     lstrcpyA(buf, "foo");
104     len = GetSystemDirectoryA(buf, len_with_null - 1);
105     ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
106     ok(len == len_with_null, "GetSystemDirectoryA returned %d, expected %d",
107        len, len_with_null);
108
109     lstrcpyA(buf, "foo");
110     len = GetSystemDirectoryA(buf, len_with_null);
111     ok(lstrcmpA(buf, "foo") != 0, "should touch the buffer");
112     ok(len == strlen(buf), "returned length should be equal to the length of string");
113     ok(len == len_with_null-1, "GetSystemDirectoryW returned %d, expected %d",
114        len, len_with_null-1);
115 }
116
117 static void test_GetSystemDirectoryW(void)
118 {
119     UINT len, len_with_null;
120     WCHAR buf[MAX_PATH];
121     static const WCHAR fooW[] = {'f','o','o',0};
122
123     len_with_null = GetSystemDirectoryW(NULL, 0);
124     if (len_with_null==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
125         return;
126     ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH");
127
128     lstrcpyW(buf, fooW);
129     len = GetSystemDirectoryW(buf, 1);
130     ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
131     ok(len == len_with_null, "GetSystemDirectoryW returned %d, expected %d",
132        len, len_with_null);
133
134     lstrcpyW(buf, fooW);
135     len = GetSystemDirectoryW(buf, len_with_null - 1);
136     ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
137     ok(len == len_with_null, "GetSystemDirectoryW returned %d, expected %d",
138        len, len_with_null);
139
140     lstrcpyW(buf, fooW);
141     len = GetSystemDirectoryW(buf, len_with_null);
142     ok(lstrcmpW(buf, fooW) != 0, "should touch the buffer");
143     ok(len == lstrlenW(buf), "returned length should be equal to the length of string");
144     ok(len == len_with_null-1, "GetSystemDirectoryW returned %d, expected %d",
145        len, len_with_null-1);
146 }
147
148 static void test_CreateDirectoryA(void)
149 {
150     char tmpdir[MAX_PATH];
151     BOOL ret;
152
153     ret = CreateDirectoryA(NULL, NULL);
154     ok(ret == FALSE && (GetLastError() == ERROR_PATH_NOT_FOUND ||
155                         GetLastError() == ERROR_INVALID_PARAMETER),
156        "CreateDirectoryA(NULL,NULL): ret=%d error=%ld",ret,GetLastError());
157
158     ret = CreateDirectoryA("", NULL);
159     ok(ret == FALSE && (GetLastError() == ERROR_BAD_PATHNAME ||
160                         GetLastError() == ERROR_PATH_NOT_FOUND),
161        "CreateDirectoryA(\"\",NULL): ret=%d error=%ld",ret,GetLastError());
162
163     ret = GetSystemDirectoryA(tmpdir, MAX_PATH);
164     ok(ret < MAX_PATH, "System directory should fit into MAX_PATH");
165
166     ret = SetCurrentDirectoryA(tmpdir);
167     ok(ret == TRUE, "could not chdir to the System directory");
168
169     ret = CreateDirectoryA(".", NULL);
170     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path");
171
172     ret = CreateDirectoryA("..", NULL);
173     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path");
174
175     GetTempPathA(MAX_PATH, tmpdir);
176     tmpdir[3] = 0; /* truncate the path */
177     ret = CreateDirectoryA(tmpdir, NULL);
178     ok(ret == FALSE && (GetLastError() == ERROR_ALREADY_EXISTS ||
179                         GetLastError() == ERROR_ACCESS_DENIED),
180        "CreateDirectoryA(drive_root): ret=%d error=%ld",ret,GetLastError());
181
182     GetTempPathA(MAX_PATH, tmpdir);
183     lstrcatA(tmpdir, "Please Remove Me");
184     ret = CreateDirectoryA(tmpdir, NULL);
185     ok(ret == TRUE, "CreateDirectoryA should always succeed");
186
187     ret = CreateDirectoryA(tmpdir, NULL);
188     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path");
189
190     ret = RemoveDirectoryA(tmpdir);
191     ok(ret == TRUE, "RemoveDirectoryA should always succeed");
192 }
193
194 static void test_CreateDirectoryW(void)
195 {
196     WCHAR tmpdir[MAX_PATH];
197     BOOL ret;
198     static const WCHAR empty_strW[] = { 0 };
199     static const WCHAR tmp_dir_name[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
200     static const WCHAR dotW[] = {'.',0};
201     static const WCHAR dotdotW[] = {'.','.',0};
202
203     ret = CreateDirectoryW(NULL, NULL);
204     if (!ret && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
205         return;
206     ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND, "should not create NULL path");
207
208     ret = CreateDirectoryW(empty_strW, NULL);
209     ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND, "should not create empty path");
210
211     ret = GetSystemDirectoryW(tmpdir, MAX_PATH);
212     ok(ret < MAX_PATH, "System directory should fit into MAX_PATH");
213
214     ret = SetCurrentDirectoryW(tmpdir);
215     ok(ret == TRUE, "could not chdir to the System directory");
216
217     ret = CreateDirectoryW(dotW, NULL);
218     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path");
219
220     ret = CreateDirectoryW(dotdotW, NULL);
221     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path");
222
223     GetTempPathW(MAX_PATH, tmpdir);
224     tmpdir[3] = 0; /* truncate the path */
225     ret = CreateDirectoryW(tmpdir, NULL);
226     ok(ret == FALSE && GetLastError() == ERROR_ACCESS_DENIED, "should deny access to the drive root");
227
228     GetTempPathW(MAX_PATH, tmpdir);
229     lstrcatW(tmpdir, tmp_dir_name);
230     ret = CreateDirectoryW(tmpdir, NULL);
231     ok(ret == TRUE, "CreateDirectoryW should always succeed");
232
233     ret = CreateDirectoryW(tmpdir, NULL);
234     ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "should not create existing path");
235
236     ret = RemoveDirectoryW(tmpdir);
237     ok(ret == TRUE, "RemoveDirectoryW should always succeed");
238 }
239
240 START_TEST(directory)
241 {
242     test_GetWindowsDirectoryA();
243     test_GetWindowsDirectoryW();
244
245     test_GetSystemDirectoryA();
246     test_GetSystemDirectoryW();
247
248     test_CreateDirectoryA();
249     test_CreateDirectoryW();
250 }