b541f0cfe92fa5ca64fd5e1b8b8f4f2f0cfa23e3
[reactos.git] / rostests / winetests / ntdll / directory.c
1 /* Unit test suite for Ntdll directory functions
2 *
3 * Copyright 2007 Jeff Latimer
4 * Copyright 2007 Andrey Turkin
5 * Copyright 2008 Jeff Zaroyko
6 * Copyright 2009 Dan Kegel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 * NOTES
23 * We use function pointers here as there is no import library for NTDLL on
24 * windows.
25 */
26
27 #include <stdio.h>
28 #include <stdarg.h>
29
30 #include "ntstatus.h"
31 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro
32 * definition errors when we get to winnt.h
33 */
34 #define WIN32_NO_STATUS
35
36 #include "wine/test.h"
37 #include "wine/winternl.h"
38
39 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
40 static NTSTATUS (WINAPI *pNtOpenFile) ( PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK, ULONG, ULONG );
41 static NTSTATUS (WINAPI *pNtQueryDirectoryFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,
42 PVOID,ULONG,FILE_INFORMATION_CLASS,BOOLEAN,PUNICODE_STRING,BOOLEAN);
43 static BOOLEAN (WINAPI *pRtlCreateUnicodeStringFromAsciiz)(PUNICODE_STRING,LPCSTR);
44 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)( LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR* );
45 static VOID (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
46 static VOID (WINAPI *pRtlFreeUnicodeString)( PUNICODE_STRING );
47 static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
48 LPCSTR src, DWORD srclen );
49 static NTSTATUS (WINAPI *pRtlWow64EnableFsRedirection)( BOOLEAN enable );
50 static NTSTATUS (WINAPI *pRtlWow64EnableFsRedirectionEx)( ULONG disable, ULONG *old_value );
51
52 /* The attribute sets to test */
53 static struct testfile_s {
54 BOOL attr_done; /* set if attributes were tested for this file already */
55 const DWORD attr; /* desired attribute */
56 const char *name; /* filename to use */
57 const char *target; /* what to point to (only for reparse pts) */
58 const char *description; /* for error messages */
59 int nfound; /* How many were found (expect 1) */
60 WCHAR nameW[20]; /* unicode version of name (filled in later) */
61 } testfiles[] = {
62 { 0, FILE_ATTRIBUTE_NORMAL, "n.tmp", NULL, "normal" },
63 { 0, FILE_ATTRIBUTE_HIDDEN, "h.tmp", NULL, "hidden" },
64 { 0, FILE_ATTRIBUTE_SYSTEM, "s.tmp", NULL, "system" },
65 { 0, FILE_ATTRIBUTE_DIRECTORY, "d.tmp", NULL, "directory" },
66 { 0, FILE_ATTRIBUTE_DIRECTORY, ".", NULL, ". directory" },
67 { 0, FILE_ATTRIBUTE_DIRECTORY, "..", NULL, ".. directory" },
68 { 0, 0, NULL }
69 };
70 static const int max_test_dir_size = 20; /* size of above plus some for .. etc */
71
72 /* Create a test directory full of attribute test files, clear counts */
73 static void set_up_attribute_test(const char *testdirA)
74 {
75 int i;
76 BOOL ret;
77
78 ret = CreateDirectoryA(testdirA, NULL);
79 ok(ret, "couldn't create dir '%s', error %d\n", testdirA, GetLastError());
80
81 for (i=0; testfiles[i].name; i++) {
82 char buf[MAX_PATH];
83 pRtlMultiByteToUnicodeN(testfiles[i].nameW, sizeof(testfiles[i].nameW), NULL, testfiles[i].name, strlen(testfiles[i].name)+1);
84
85 if (strcmp(testfiles[i].name, ".") == 0 || strcmp(testfiles[i].name, "..") == 0)
86 continue;
87 sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
88 if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
89 ret = CreateDirectoryA(buf, NULL);
90 ok(ret, "couldn't create dir '%s', error %d\n", buf, GetLastError());
91 } else {
92 HANDLE h = CreateFileA(buf,
93 GENERIC_READ|GENERIC_WRITE,
94 0, NULL, CREATE_ALWAYS,
95 testfiles[i].attr, 0);
96 ok( h != INVALID_HANDLE_VALUE, "failed to create temp file '%s'\n", buf );
97 CloseHandle(h);
98 }
99 }
100 }
101
102 static void reset_found_files(void)
103 {
104 int i;
105
106 for (i = 0; testfiles[i].name; i++)
107 testfiles[i].nfound = 0;
108 }
109
110 /* Remove the given test directory and the attribute test files, if any */
111 static void tear_down_attribute_test(const char *testdirA)
112 {
113 int i;
114
115 for (i=0; testfiles[i].name; i++) {
116 int ret;
117 char buf[MAX_PATH];
118 if (strcmp(testfiles[i].name, ".") == 0 || strcmp(testfiles[i].name, "..") == 0)
119 continue;
120 sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
121 if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
122 ret = RemoveDirectoryA(buf);
123 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
124 "Failed to rmdir %s, error %d\n", buf, GetLastError());
125 } else {
126 ret = DeleteFileA(buf);
127 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
128 "Failed to rm %s, error %d\n", buf, GetLastError());
129 }
130 }
131 RemoveDirectoryA(testdirA);
132 }
133
134 /* Match one found file against testfiles[], increment count if found */
135 static void tally_test_file(FILE_BOTH_DIRECTORY_INFORMATION *dir_info)
136 {
137 int i;
138 DWORD attribmask =
139 (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT);
140 DWORD attrib = dir_info->FileAttributes & attribmask;
141 WCHAR *nameW = dir_info->FileName;
142 int namelen = dir_info->FileNameLength / sizeof(WCHAR);
143
144 for (i=0; testfiles[i].name; i++) {
145 int len = strlen(testfiles[i].name);
146 if (namelen != len || memcmp(nameW, testfiles[i].nameW, len*sizeof(WCHAR)))
147 continue;
148 if (!testfiles[i].attr_done) {
149 ok (attrib == (testfiles[i].attr & attribmask), "file %s: expected %s (%x), got %x (is your linux new enough?)\n", testfiles[i].name, testfiles[i].description, testfiles[i].attr, attrib);
150 testfiles[i].attr_done = TRUE;
151 }
152 testfiles[i].nfound++;
153 break;
154 }
155 ok(testfiles[i].name != NULL, "unexpected file found\n");
156 }
157
158 static void test_flags_NtQueryDirectoryFile(OBJECT_ATTRIBUTES *attr, const char *testdirA,
159 UNICODE_STRING *mask,
160 BOOLEAN single_entry, BOOLEAN restart_flag)
161 {
162 HANDLE dirh;
163 IO_STATUS_BLOCK io;
164 UINT data_pos, data_size;
165 UINT data_len; /* length of dir data */
166 BYTE data[8192]; /* directory data */
167 FILE_BOTH_DIRECTORY_INFORMATION *dir_info;
168 DWORD status;
169 int numfiles;
170 int i;
171
172 reset_found_files();
173
174 data_size = mask ? offsetof( FILE_BOTH_DIRECTORY_INFORMATION, FileName[256] ) : sizeof(data);
175
176 /* Read the directory and note which files are found */
177 status = pNtOpenFile( &dirh, SYNCHRONIZE | FILE_LIST_DIRECTORY, attr, &io, FILE_OPEN,
178 FILE_SYNCHRONOUS_IO_NONALERT|FILE_OPEN_FOR_BACKUP_INTENT|FILE_DIRECTORY_FILE);
179 ok (status == STATUS_SUCCESS, "failed to open dir '%s', ret 0x%x, error %d\n", testdirA, status, GetLastError());
180 if (status != STATUS_SUCCESS) {
181 skip("can't test if we can't open the directory\n");
182 return;
183 }
184
185 pNtQueryDirectoryFile( dirh, NULL, NULL, NULL, &io, data, data_size,
186 FileBothDirectoryInformation, single_entry, mask, restart_flag );
187 ok (U(io).Status == STATUS_SUCCESS, "failed to query directory; status %x\n", U(io).Status);
188 data_len = io.Information;
189 ok (data_len >= sizeof(FILE_BOTH_DIRECTORY_INFORMATION), "not enough data in directory\n");
190
191 data_pos = 0;
192 numfiles = 0;
193 while ((data_pos < data_len) && (numfiles < max_test_dir_size)) {
194 dir_info = (FILE_BOTH_DIRECTORY_INFORMATION *)(data + data_pos);
195
196 tally_test_file(dir_info);
197
198 if (dir_info->NextEntryOffset == 0) {
199 pNtQueryDirectoryFile( dirh, 0, NULL, NULL, &io, data, data_size,
200 FileBothDirectoryInformation, single_entry, mask, FALSE );
201 if (U(io).Status == STATUS_NO_MORE_FILES)
202 break;
203 ok (U(io).Status == STATUS_SUCCESS, "failed to query directory; status %x\n", U(io).Status);
204 data_len = io.Information;
205 if (data_len < sizeof(FILE_BOTH_DIRECTORY_INFORMATION))
206 break;
207 data_pos = 0;
208 } else {
209 data_pos += dir_info->NextEntryOffset;
210 }
211 numfiles++;
212 }
213 ok(numfiles < max_test_dir_size, "too many loops\n");
214
215 if (mask)
216 for (i=0; testfiles[i].name; i++)
217 ok(testfiles[i].nfound == (testfiles[i].nameW == mask->Buffer),
218 "Wrong number %d of %s files found (single_entry=%d,mask=%s)\n",
219 testfiles[i].nfound, testfiles[i].description, single_entry,
220 wine_dbgstr_wn(mask->Buffer, mask->Length/sizeof(WCHAR) ));
221 else
222 for (i=0; testfiles[i].name; i++)
223 ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found (single_entry=%d,restart=%d)\n",
224 testfiles[i].nfound, testfiles[i].description, single_entry, restart_flag);
225 pNtClose(dirh);
226 }
227
228 static void test_NtQueryDirectoryFile(void)
229 {
230 OBJECT_ATTRIBUTES attr;
231 UNICODE_STRING ntdirname;
232 char testdirA[MAX_PATH];
233 WCHAR testdirW[MAX_PATH];
234 int i;
235
236 /* Clean up from prior aborted run, if any, then set up test files */
237 ok(GetTempPathA(MAX_PATH, testdirA), "couldn't get temp dir\n");
238 strcat(testdirA, "NtQueryDirectoryFile.tmp");
239 tear_down_attribute_test(testdirA);
240 set_up_attribute_test(testdirA);
241
242 pRtlMultiByteToUnicodeN(testdirW, sizeof(testdirW), NULL, testdirA, strlen(testdirA)+1);
243 if (!pRtlDosPathNameToNtPathName_U(testdirW, &ntdirname, NULL, NULL))
244 {
245 ok(0, "RtlDosPathNametoNtPathName_U failed\n");
246 goto done;
247 }
248 InitializeObjectAttributes(&attr, &ntdirname, OBJ_CASE_INSENSITIVE, 0, NULL);
249
250 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, FALSE, TRUE);
251 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, FALSE, FALSE);
252 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, TRUE, TRUE);
253 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, TRUE, FALSE);
254
255 for (i = 0; testfiles[i].name; i++)
256 {
257 UNICODE_STRING mask;
258
259 if (testfiles[i].nameW[0] == '.') continue; /* . and .. as masks are broken on Windows */
260 mask.Buffer = testfiles[i].nameW;
261 mask.Length = mask.MaximumLength = lstrlenW(testfiles[i].nameW) * sizeof(WCHAR);
262 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, FALSE, TRUE);
263 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, FALSE, FALSE);
264 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, TRUE, TRUE);
265 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, TRUE, FALSE);
266 }
267
268 done:
269 tear_down_attribute_test(testdirA);
270 pRtlFreeUnicodeString(&ntdirname);
271 }
272
273 static void test_redirection(void)
274 {
275 ULONG old, cur;
276 NTSTATUS status;
277
278 if (!pRtlWow64EnableFsRedirection || !pRtlWow64EnableFsRedirectionEx)
279 {
280 skip( "Wow64 redirection not supported\n" );
281 return;
282 }
283 status = pRtlWow64EnableFsRedirectionEx( FALSE, &old );
284 if (status == STATUS_NOT_IMPLEMENTED)
285 {
286 skip( "Wow64 redirection not supported\n" );
287 return;
288 }
289 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
290
291 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
292 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
293 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
294
295 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
296 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
297 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
298 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
299 ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
300
301 status = pRtlWow64EnableFsRedirection( TRUE );
302 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
303 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
304 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
305 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
306
307 status = pRtlWow64EnableFsRedirectionEx( TRUE, NULL );
308 ok( status == STATUS_ACCESS_VIOLATION, "RtlWow64EnableFsRedirectionEx failed with status %x\n", status );
309 status = pRtlWow64EnableFsRedirectionEx( TRUE, (void*)1 );
310 ok( status == STATUS_ACCESS_VIOLATION, "RtlWow64EnableFsRedirectionEx failed with status %x\n", status );
311
312 status = pRtlWow64EnableFsRedirection( FALSE );
313 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
314 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
315 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
316 ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
317
318 pRtlWow64EnableFsRedirectionEx( old, &cur );
319 }
320
321 START_TEST(directory)
322 {
323 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
324 if (!hntdll)
325 {
326 skip("not running on NT, skipping test\n");
327 return;
328 }
329
330 pNtClose = (void *)GetProcAddress(hntdll, "NtClose");
331 pNtOpenFile = (void *)GetProcAddress(hntdll, "NtOpenFile");
332 pNtQueryDirectoryFile = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
333 pRtlCreateUnicodeStringFromAsciiz = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeStringFromAsciiz");
334 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
335 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
336 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
337 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll,"RtlMultiByteToUnicodeN");
338 pRtlWow64EnableFsRedirection = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirection");
339 pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirectionEx");
340
341 test_NtQueryDirectoryFile();
342 test_redirection();
343 }