[NTDLL_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[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_SHARE_READ,
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 set_up_case_test(const char *testdir)
274 {
275 BOOL ret;
276 char buf[MAX_PATH];
277 HANDLE h;
278
279 ret = CreateDirectoryA(testdir, NULL);
280 ok(ret, "couldn't create dir '%s', error %d\n", testdir, GetLastError());
281
282 sprintf(buf, "%s\\%s", testdir, "TesT");
283 h = CreateFileA(buf, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
284 FILE_ATTRIBUTE_NORMAL, 0);
285 ok(h != INVALID_HANDLE_VALUE, "failed to create temp file '%s'\n", buf);
286 CloseHandle(h);
287 }
288
289 static void tear_down_case_test(const char *testdir)
290 {
291 int ret;
292 char buf[MAX_PATH];
293
294 sprintf(buf, "%s\\%s", testdir, "TesT");
295 ret = DeleteFileA(buf);
296 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
297 "Failed to rm %s, error %d\n", buf, GetLastError());
298 RemoveDirectoryA(testdir);
299 }
300
301 static void test_NtQueryDirectoryFile_case(void)
302 {
303 static const char testfile[] = "TesT";
304 static const WCHAR testfile_w[] = {'T','e','s','T'};
305 static int testfile_len = sizeof(testfile) - 1;
306 static WCHAR testmask[] = {'t','e','s','t'};
307 OBJECT_ATTRIBUTES attr;
308 UNICODE_STRING ntdirname;
309 char testdir[MAX_PATH];
310 WCHAR testdir_w[MAX_PATH];
311 HANDLE dirh;
312 UNICODE_STRING mask;
313 IO_STATUS_BLOCK io;
314 UINT data_size, data_len;
315 BYTE data[8192];
316 FILE_BOTH_DIRECTORY_INFORMATION *dir_info = (FILE_BOTH_DIRECTORY_INFORMATION *)data;
317 DWORD status;
318 WCHAR *name;
319 ULONG name_len;
320
321 /* Clean up from prior aborted run, if any, then set up test files */
322 ok(GetTempPathA(MAX_PATH, testdir), "couldn't get temp dir\n");
323 strcat(testdir, "case.tmp");
324 tear_down_case_test(testdir);
325 set_up_case_test(testdir);
326
327 pRtlMultiByteToUnicodeN(testdir_w, sizeof(testdir_w), NULL, testdir, strlen(testdir) + 1);
328 if (!pRtlDosPathNameToNtPathName_U(testdir_w, &ntdirname, NULL, NULL))
329 {
330 ok(0, "RtlDosPathNametoNtPathName_U failed\n");
331 goto done;
332 }
333 InitializeObjectAttributes(&attr, &ntdirname, OBJ_CASE_INSENSITIVE, 0, NULL);
334
335 data_size = offsetof(FILE_BOTH_DIRECTORY_INFORMATION, FileName[256]);
336
337 status = pNtOpenFile(&dirh, SYNCHRONIZE | FILE_LIST_DIRECTORY, &attr, &io, FILE_SHARE_READ,
338 FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT | FILE_DIRECTORY_FILE);
339 ok (status == STATUS_SUCCESS, "failed to open dir '%s', ret 0x%x, error %d\n", testdir, status, GetLastError());
340 if (status != STATUS_SUCCESS)
341 {
342 skip("can't test if we can't open the directory\n");
343 return;
344 }
345
346 mask.Buffer = testmask;
347 mask.Length = mask.MaximumLength = sizeof(testmask);
348 pNtQueryDirectoryFile(dirh, NULL, NULL, NULL, &io, data, data_size,
349 FileBothDirectoryInformation, TRUE, &mask, FALSE);
350 ok(U(io).Status == STATUS_SUCCESS, "failed to query directory; status %x\n", U(io).Status);
351 data_len = io.Information;
352 ok(data_len >= sizeof(FILE_BOTH_DIRECTORY_INFORMATION), "not enough data in directory\n");
353
354 name = dir_info->FileName;
355 name_len = dir_info->FileNameLength / sizeof(WCHAR);
356
357 ok(name_len == testfile_len, "unexpected filename length %u\n", name_len);
358 ok(!memcmp(name, testfile_w, testfile_len * sizeof(WCHAR)), "unexpected filename %s\n",
359 wine_dbgstr_wn(name, name_len));
360
361 pNtClose(dirh);
362
363 done:
364 tear_down_case_test(testdir);
365 pRtlFreeUnicodeString(&ntdirname);
366 }
367
368 static void test_redirection(void)
369 {
370 ULONG old, cur;
371 NTSTATUS status;
372
373 if (!pRtlWow64EnableFsRedirection || !pRtlWow64EnableFsRedirectionEx)
374 {
375 skip( "Wow64 redirection not supported\n" );
376 return;
377 }
378 status = pRtlWow64EnableFsRedirectionEx( FALSE, &old );
379 if (status == STATUS_NOT_IMPLEMENTED)
380 {
381 skip( "Wow64 redirection not supported\n" );
382 return;
383 }
384 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
385
386 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
387 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
388 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
389
390 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
391 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
392 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
393 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
394 ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
395
396 status = pRtlWow64EnableFsRedirection( TRUE );
397 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
398 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
399 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
400 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
401
402 status = pRtlWow64EnableFsRedirectionEx( TRUE, NULL );
403 ok( status == STATUS_ACCESS_VIOLATION, "RtlWow64EnableFsRedirectionEx failed with status %x\n", status );
404 status = pRtlWow64EnableFsRedirectionEx( TRUE, (void*)1 );
405 ok( status == STATUS_ACCESS_VIOLATION, "RtlWow64EnableFsRedirectionEx failed with status %x\n", status );
406
407 status = pRtlWow64EnableFsRedirection( FALSE );
408 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
409 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
410 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
411 ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
412
413 pRtlWow64EnableFsRedirectionEx( old, &cur );
414 }
415
416 START_TEST(directory)
417 {
418 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
419 if (!hntdll)
420 {
421 skip("not running on NT, skipping test\n");
422 return;
423 }
424
425 pNtClose = (void *)GetProcAddress(hntdll, "NtClose");
426 pNtOpenFile = (void *)GetProcAddress(hntdll, "NtOpenFile");
427 pNtQueryDirectoryFile = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
428 pRtlCreateUnicodeStringFromAsciiz = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeStringFromAsciiz");
429 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
430 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
431 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
432 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll,"RtlMultiByteToUnicodeN");
433 pRtlWow64EnableFsRedirection = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirection");
434 pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirectionEx");
435
436 test_NtQueryDirectoryFile();
437 test_NtQueryDirectoryFile_case();
438 test_redirection();
439 }