msvcirt: Add implementation of streambuf::snextc.
[wine.git] / dlls / ntdll / tests / directory.c
blob730b0f1b29b5f1b308865cca88e67b3f2f1081dc
1 /* Unit test suite for Ntdll directory functions
3 * Copyright 2007 Jeff Latimer
4 * Copyright 2007 Andrey Turkin
5 * Copyright 2008 Jeff Zaroyko
6 * Copyright 2009 Dan Kegel
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.
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.
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
22 * NOTES
23 * We use function pointers here as there is no import library for NTDLL on
24 * windows.
27 #include <stdio.h>
28 #include <stdarg.h>
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
34 #define WIN32_NO_STATUS
36 #include "wine/test.h"
37 #include "winternl.h"
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 );
52 /* The attribute sets to test */
53 static struct testfile_s {
54 BOOL todo; /* set if it doesn't work on wine yet */
55 BOOL attr_done; /* set if attributes were tested for this file already */
56 const DWORD attr; /* desired attribute */
57 const char *name; /* filename to use */
58 const char *target; /* what to point to (only for reparse pts) */
59 const char *description; /* for error messages */
60 int nfound; /* How many were found (expect 1) */
61 WCHAR nameW[20]; /* unicode version of name (filled in later) */
62 } testfiles[] = {
63 { 0, 0, FILE_ATTRIBUTE_NORMAL, "n.tmp", NULL, "normal" },
64 { 1, 0, FILE_ATTRIBUTE_HIDDEN, "h.tmp", NULL, "hidden" },
65 { 1, 0, FILE_ATTRIBUTE_SYSTEM, "s.tmp", NULL, "system" },
66 { 0, 0, FILE_ATTRIBUTE_DIRECTORY, "d.tmp", NULL, "directory" },
67 { 0, 0, FILE_ATTRIBUTE_DIRECTORY, ".", NULL, ". directory" },
68 { 0, 0, FILE_ATTRIBUTE_DIRECTORY, "..", NULL, ".. directory" },
69 { 0, 0, 0, NULL }
71 static const int max_test_dir_size = 20; /* size of above plus some for .. etc */
73 /* Create a test directory full of attribute test files, clear counts */
74 static void set_up_attribute_test(const char *testdirA)
76 int i;
77 BOOL ret;
79 ret = CreateDirectoryA(testdirA, NULL);
80 ok(ret, "couldn't create dir '%s', error %d\n", testdirA, GetLastError());
82 for (i=0; testfiles[i].name; i++) {
83 char buf[MAX_PATH];
84 pRtlMultiByteToUnicodeN(testfiles[i].nameW, sizeof(testfiles[i].nameW), NULL, testfiles[i].name, strlen(testfiles[i].name)+1);
86 if (strcmp(testfiles[i].name, ".") == 0 || strcmp(testfiles[i].name, "..") == 0)
87 continue;
88 sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
89 if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
90 ret = CreateDirectoryA(buf, NULL);
91 ok(ret, "couldn't create dir '%s', error %d\n", buf, GetLastError());
92 } else {
93 HANDLE h = CreateFileA(buf,
94 GENERIC_READ|GENERIC_WRITE,
95 0, NULL, CREATE_ALWAYS,
96 testfiles[i].attr, 0);
97 ok( h != INVALID_HANDLE_VALUE, "failed to create temp file '%s'\n", buf );
98 CloseHandle(h);
103 static void reset_found_files(void)
105 int i;
107 for (i = 0; testfiles[i].name; i++)
108 testfiles[i].nfound = 0;
111 /* Remove the given test directory and the attribute test files, if any */
112 static void tear_down_attribute_test(const char *testdirA)
114 int i;
116 for (i=0; testfiles[i].name; i++) {
117 int ret;
118 char buf[MAX_PATH];
119 if (strcmp(testfiles[i].name, ".") == 0 || strcmp(testfiles[i].name, "..") == 0)
120 continue;
121 sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
122 if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
123 ret = RemoveDirectoryA(buf);
124 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
125 "Failed to rmdir %s, error %d\n", buf, GetLastError());
126 } else {
127 ret = DeleteFileA(buf);
128 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
129 "Failed to rm %s, error %d\n", buf, GetLastError());
132 RemoveDirectoryA(testdirA);
135 /* Match one found file against testfiles[], increment count if found */
136 static void tally_test_file(FILE_BOTH_DIRECTORY_INFORMATION *dir_info)
138 int i;
139 DWORD attribmask =
140 (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT);
141 DWORD attrib = dir_info->FileAttributes & attribmask;
142 WCHAR *nameW = dir_info->FileName;
143 int namelen = dir_info->FileNameLength / sizeof(WCHAR);
145 for (i=0; testfiles[i].name; i++) {
146 int len = strlen(testfiles[i].name);
147 if (namelen != len || memcmp(nameW, testfiles[i].nameW, len*sizeof(WCHAR)))
148 continue;
149 if (!testfiles[i].attr_done) {
150 if (testfiles[i].todo) {
151 todo_wine
152 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);
153 } else {
154 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);
156 testfiles[i].attr_done = TRUE;
158 testfiles[i].nfound++;
159 break;
161 ok(testfiles[i].name != NULL, "unexpected file found\n");
164 static void test_flags_NtQueryDirectoryFile(OBJECT_ATTRIBUTES *attr, const char *testdirA,
165 UNICODE_STRING *mask,
166 BOOLEAN single_entry, BOOLEAN restart_flag)
168 HANDLE dirh;
169 IO_STATUS_BLOCK io;
170 UINT data_pos, data_size;
171 UINT data_len; /* length of dir data */
172 BYTE data[8192]; /* directory data */
173 FILE_BOTH_DIRECTORY_INFORMATION *dir_info;
174 DWORD status;
175 int numfiles;
176 int i;
178 reset_found_files();
180 data_size = mask ? offsetof( FILE_BOTH_DIRECTORY_INFORMATION, FileName[256] ) : sizeof(data);
182 /* Read the directory and note which files are found */
183 status = pNtOpenFile( &dirh, SYNCHRONIZE | FILE_LIST_DIRECTORY, attr, &io, FILE_SHARE_READ,
184 FILE_SYNCHRONOUS_IO_NONALERT|FILE_OPEN_FOR_BACKUP_INTENT|FILE_DIRECTORY_FILE);
185 ok (status == STATUS_SUCCESS, "failed to open dir '%s', ret 0x%x, error %d\n", testdirA, status, GetLastError());
186 if (status != STATUS_SUCCESS) {
187 skip("can't test if we can't open the directory\n");
188 return;
191 pNtQueryDirectoryFile( dirh, NULL, NULL, NULL, &io, data, data_size,
192 FileBothDirectoryInformation, single_entry, mask, restart_flag );
193 ok (U(io).Status == STATUS_SUCCESS, "failed to query directory; status %x\n", U(io).Status);
194 data_len = io.Information;
195 ok (data_len >= sizeof(FILE_BOTH_DIRECTORY_INFORMATION), "not enough data in directory\n");
197 data_pos = 0;
198 numfiles = 0;
199 while ((data_pos < data_len) && (numfiles < max_test_dir_size)) {
200 dir_info = (FILE_BOTH_DIRECTORY_INFORMATION *)(data + data_pos);
202 tally_test_file(dir_info);
204 if (dir_info->NextEntryOffset == 0) {
205 pNtQueryDirectoryFile( dirh, 0, NULL, NULL, &io, data, data_size,
206 FileBothDirectoryInformation, single_entry, mask, FALSE );
207 if (U(io).Status == STATUS_NO_MORE_FILES)
208 break;
209 ok (U(io).Status == STATUS_SUCCESS, "failed to query directory; status %x\n", U(io).Status);
210 data_len = io.Information;
211 if (data_len < sizeof(FILE_BOTH_DIRECTORY_INFORMATION))
212 break;
213 data_pos = 0;
214 } else {
215 data_pos += dir_info->NextEntryOffset;
217 numfiles++;
219 ok(numfiles < max_test_dir_size, "too many loops\n");
221 if (mask)
222 for (i=0; testfiles[i].name; i++)
223 ok(testfiles[i].nfound == (testfiles[i].nameW == mask->Buffer),
224 "Wrong number %d of %s files found (single_entry=%d,mask=%s)\n",
225 testfiles[i].nfound, testfiles[i].description, single_entry,
226 wine_dbgstr_wn(mask->Buffer, mask->Length/sizeof(WCHAR) ));
227 else
228 for (i=0; testfiles[i].name; i++)
229 ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found (single_entry=%d,restart=%d)\n",
230 testfiles[i].nfound, testfiles[i].description, single_entry, restart_flag);
231 pNtClose(dirh);
234 static void test_NtQueryDirectoryFile(void)
236 OBJECT_ATTRIBUTES attr;
237 UNICODE_STRING ntdirname;
238 char testdirA[MAX_PATH];
239 WCHAR testdirW[MAX_PATH];
240 int i;
242 /* Clean up from prior aborted run, if any, then set up test files */
243 ok(GetTempPathA(MAX_PATH, testdirA), "couldn't get temp dir\n");
244 strcat(testdirA, "NtQueryDirectoryFile.tmp");
245 tear_down_attribute_test(testdirA);
246 set_up_attribute_test(testdirA);
248 pRtlMultiByteToUnicodeN(testdirW, sizeof(testdirW), NULL, testdirA, strlen(testdirA)+1);
249 if (!pRtlDosPathNameToNtPathName_U(testdirW, &ntdirname, NULL, NULL))
251 ok(0, "RtlDosPathNametoNtPathName_U failed\n");
252 goto done;
254 InitializeObjectAttributes(&attr, &ntdirname, OBJ_CASE_INSENSITIVE, 0, NULL);
256 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, FALSE, TRUE);
257 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, FALSE, FALSE);
258 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, TRUE, TRUE);
259 test_flags_NtQueryDirectoryFile(&attr, testdirA, NULL, TRUE, FALSE);
261 for (i = 0; testfiles[i].name; i++)
263 UNICODE_STRING mask;
265 if (testfiles[i].nameW[0] == '.') continue; /* . and .. as masks are broken on Windows */
266 mask.Buffer = testfiles[i].nameW;
267 mask.Length = mask.MaximumLength = lstrlenW(testfiles[i].nameW) * sizeof(WCHAR);
268 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, FALSE, TRUE);
269 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, FALSE, FALSE);
270 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, TRUE, TRUE);
271 test_flags_NtQueryDirectoryFile(&attr, testdirA, &mask, TRUE, FALSE);
274 done:
275 tear_down_attribute_test(testdirA);
276 pRtlFreeUnicodeString(&ntdirname);
279 static void set_up_case_test(const char *testdir)
281 BOOL ret;
282 char buf[MAX_PATH];
283 HANDLE h;
285 ret = CreateDirectoryA(testdir, NULL);
286 ok(ret, "couldn't create dir '%s', error %d\n", testdir, GetLastError());
288 sprintf(buf, "%s\\%s", testdir, "TesT");
289 h = CreateFileA(buf, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
290 FILE_ATTRIBUTE_NORMAL, 0);
291 ok(h != INVALID_HANDLE_VALUE, "failed to create temp file '%s'\n", buf);
292 CloseHandle(h);
295 static void tear_down_case_test(const char *testdir)
297 int ret;
298 char buf[MAX_PATH];
300 sprintf(buf, "%s\\%s", testdir, "TesT");
301 ret = DeleteFileA(buf);
302 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
303 "Failed to rm %s, error %d\n", buf, GetLastError());
304 RemoveDirectoryA(testdir);
307 static void test_NtQueryDirectoryFile_case(void)
309 static const char testfile[] = "TesT";
310 static const WCHAR testfile_w[] = {'T','e','s','T'};
311 static int testfile_len = sizeof(testfile) - 1;
312 static WCHAR testmask[] = {'t','e','s','t'};
313 OBJECT_ATTRIBUTES attr;
314 UNICODE_STRING ntdirname;
315 char testdir[MAX_PATH];
316 WCHAR testdir_w[MAX_PATH];
317 HANDLE dirh;
318 UNICODE_STRING mask;
319 IO_STATUS_BLOCK io;
320 UINT data_size, data_len;
321 BYTE data[8192];
322 FILE_BOTH_DIRECTORY_INFORMATION *dir_info = (FILE_BOTH_DIRECTORY_INFORMATION *)data;
323 DWORD status;
324 WCHAR *name;
325 ULONG name_len;
327 /* Clean up from prior aborted run, if any, then set up test files */
328 ok(GetTempPathA(MAX_PATH, testdir), "couldn't get temp dir\n");
329 strcat(testdir, "case.tmp");
330 tear_down_case_test(testdir);
331 set_up_case_test(testdir);
333 pRtlMultiByteToUnicodeN(testdir_w, sizeof(testdir_w), NULL, testdir, strlen(testdir) + 1);
334 if (!pRtlDosPathNameToNtPathName_U(testdir_w, &ntdirname, NULL, NULL))
336 ok(0, "RtlDosPathNametoNtPathName_U failed\n");
337 goto done;
339 InitializeObjectAttributes(&attr, &ntdirname, OBJ_CASE_INSENSITIVE, 0, NULL);
341 data_size = offsetof(FILE_BOTH_DIRECTORY_INFORMATION, FileName[256]);
343 status = pNtOpenFile(&dirh, SYNCHRONIZE | FILE_LIST_DIRECTORY, &attr, &io, FILE_SHARE_READ,
344 FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT | FILE_DIRECTORY_FILE);
345 ok (status == STATUS_SUCCESS, "failed to open dir '%s', ret 0x%x, error %d\n", testdir, status, GetLastError());
346 if (status != STATUS_SUCCESS)
348 skip("can't test if we can't open the directory\n");
349 return;
352 mask.Buffer = testmask;
353 mask.Length = mask.MaximumLength = sizeof(testmask);
354 pNtQueryDirectoryFile(dirh, NULL, NULL, NULL, &io, data, data_size,
355 FileBothDirectoryInformation, TRUE, &mask, FALSE);
356 ok(U(io).Status == STATUS_SUCCESS, "failed to query directory; status %x\n", U(io).Status);
357 data_len = io.Information;
358 ok(data_len >= sizeof(FILE_BOTH_DIRECTORY_INFORMATION), "not enough data in directory\n");
360 name = dir_info->FileName;
361 name_len = dir_info->FileNameLength / sizeof(WCHAR);
363 ok(name_len == testfile_len, "unexpected filename length %u\n", name_len);
364 ok(!memcmp(name, testfile_w, testfile_len * sizeof(WCHAR)), "unexpected filename %s\n",
365 wine_dbgstr_wn(name, name_len));
367 pNtClose(dirh);
369 done:
370 tear_down_case_test(testdir);
371 pRtlFreeUnicodeString(&ntdirname);
374 static void test_redirection(void)
376 ULONG old, cur;
377 NTSTATUS status;
379 if (!pRtlWow64EnableFsRedirection || !pRtlWow64EnableFsRedirectionEx)
381 skip( "Wow64 redirection not supported\n" );
382 return;
384 status = pRtlWow64EnableFsRedirectionEx( FALSE, &old );
385 if (status == STATUS_NOT_IMPLEMENTED)
387 skip( "Wow64 redirection not supported\n" );
388 return;
390 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
392 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
393 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
394 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
396 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
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 == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
402 status = pRtlWow64EnableFsRedirection( TRUE );
403 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
404 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
405 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
406 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
408 status = pRtlWow64EnableFsRedirectionEx( TRUE, NULL );
409 ok( status == STATUS_ACCESS_VIOLATION, "RtlWow64EnableFsRedirectionEx failed with status %x\n", status );
410 status = pRtlWow64EnableFsRedirectionEx( TRUE, (void*)1 );
411 ok( status == STATUS_ACCESS_VIOLATION, "RtlWow64EnableFsRedirectionEx failed with status %x\n", status );
413 status = pRtlWow64EnableFsRedirection( FALSE );
414 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
415 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
416 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
417 ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
419 pRtlWow64EnableFsRedirectionEx( old, &cur );
422 START_TEST(directory)
424 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
425 if (!hntdll)
427 skip("not running on NT, skipping test\n");
428 return;
431 pNtClose = (void *)GetProcAddress(hntdll, "NtClose");
432 pNtOpenFile = (void *)GetProcAddress(hntdll, "NtOpenFile");
433 pNtQueryDirectoryFile = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
434 pRtlCreateUnicodeStringFromAsciiz = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeStringFromAsciiz");
435 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
436 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
437 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
438 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll,"RtlMultiByteToUnicodeN");
439 pRtlWow64EnableFsRedirection = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirection");
440 pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirectionEx");
442 test_NtQueryDirectoryFile();
443 test_NtQueryDirectoryFile_case();
444 test_redirection();