- Merge the remaining portion of the wlan-bringup branch
[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 "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 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 }
70 };
71 static const int max_test_dir_size = 20; /* size of above plus some for .. etc */
72
73 /* Create a test directory full of attribute test files, clear counts */
74 static void set_up_attribute_test(const char *testdirA)
75 {
76 int i;
77 BOOL ret;
78
79 ret = CreateDirectoryA(testdirA, NULL);
80 ok(ret, "couldn't create dir '%s', error %d\n", testdirA, GetLastError());
81
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);
85
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);
99 }
100 }
101 }
102
103 static void reset_found_files(void)
104 {
105 int i;
106
107 for (i = 0; testfiles[i].name; i++)
108 testfiles[i].nfound = 0;
109 }
110
111 /* Remove the given test directory and the attribute test files, if any */
112 static void tear_down_attribute_test(const char *testdirA)
113 {
114 int i;
115
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 = RemoveDirectory(buf);
124 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
125 "Failed to rmdir %s, error %d\n", buf, GetLastError());
126 } else {
127 ret = DeleteFile(buf);
128 ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
129 "Failed to rm %s, error %d\n", buf, GetLastError());
130 }
131 }
132 RemoveDirectoryA(testdirA);
133 }
134
135 /* Match one found file against testfiles[], increment count if found */
136 static void tally_test_file(FILE_BOTH_DIRECTORY_INFORMATION *dir_info)
137 {
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);
144
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);
155 }
156 testfiles[i].attr_done = TRUE;
157 }
158 testfiles[i].nfound++;
159 break;
160 }
161 ok(testfiles[i].name != NULL, "unexpected file found\n");
162 }
163
164 static void test_flags_NtQueryDirectoryFile(OBJECT_ATTRIBUTES *attr, const char *testdirA,
165 BOOLEAN single_entry, BOOLEAN restart_flag)
166 {
167 HANDLE dirh;
168 IO_STATUS_BLOCK io;
169 UINT data_pos;
170 UINT data_len; /* length of dir data */
171 BYTE data[8192]; /* directory data */
172 FILE_BOTH_DIRECTORY_INFORMATION *dir_info;
173 DWORD status;
174 int numfiles;
175 int i;
176
177 reset_found_files();
178
179 /* Read the directory and note which files are found */
180 status = pNtOpenFile( &dirh, SYNCHRONIZE | FILE_LIST_DIRECTORY, attr, &io, FILE_OPEN,
181 FILE_SYNCHRONOUS_IO_NONALERT|FILE_OPEN_FOR_BACKUP_INTENT|FILE_DIRECTORY_FILE);
182 ok (status == STATUS_SUCCESS, "failed to open dir '%s', ret 0x%x, error %d\n", testdirA, status, GetLastError());
183 if (status != STATUS_SUCCESS) {
184 skip("can't test if we can't open the directory\n");
185 return;
186 }
187
188 pNtQueryDirectoryFile( dirh, NULL, NULL, NULL, &io, data, sizeof(data),
189 FileBothDirectoryInformation, single_entry, NULL, restart_flag );
190 ok (U(io).Status == STATUS_SUCCESS, "filed to query directory; status %x\n", U(io).Status);
191 data_len = io.Information;
192 ok (data_len >= sizeof(FILE_BOTH_DIRECTORY_INFORMATION), "not enough data in directory\n");
193
194 data_pos = 0;
195 numfiles = 0;
196 while ((data_pos < data_len) && (numfiles < max_test_dir_size)) {
197 dir_info = (FILE_BOTH_DIRECTORY_INFORMATION *)(data + data_pos);
198
199 tally_test_file(dir_info);
200
201 if (dir_info->NextEntryOffset == 0) {
202 pNtQueryDirectoryFile( dirh, 0, NULL, NULL, &io, data, sizeof(data),
203 FileBothDirectoryInformation, single_entry, NULL, FALSE );
204 if (U(io).Status == STATUS_NO_MORE_FILES)
205 break;
206 ok (U(io).Status == STATUS_SUCCESS, "filed to query directory; status %x\n", U(io).Status);
207 data_len = io.Information;
208 if (data_len < sizeof(FILE_BOTH_DIRECTORY_INFORMATION))
209 break;
210 data_pos = 0;
211 } else {
212 data_pos += dir_info->NextEntryOffset;
213 }
214 numfiles++;
215 }
216 ok(numfiles < max_test_dir_size, "too many loops\n");
217
218 for (i=0; testfiles[i].name; i++)
219 ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found (ReturnSingleEntry=%d,RestartScan=%d)\n",
220 testfiles[i].nfound, testfiles[i].description, single_entry, restart_flag);
221
222 pNtClose(dirh);
223 }
224
225 static void test_NtQueryDirectoryFile(void)
226 {
227 OBJECT_ATTRIBUTES attr;
228 UNICODE_STRING ntdirname;
229 char testdirA[MAX_PATH];
230 WCHAR testdirW[MAX_PATH];
231
232 /* Clean up from prior aborted run, if any, then set up test files */
233 ok(GetTempPathA(MAX_PATH, testdirA), "couldn't get temp dir\n");
234 strcat(testdirA, "NtQueryDirectoryFile.tmp");
235 tear_down_attribute_test(testdirA);
236 set_up_attribute_test(testdirA);
237
238 pRtlMultiByteToUnicodeN(testdirW, sizeof(testdirW), NULL, testdirA, strlen(testdirA)+1);
239 if (!pRtlDosPathNameToNtPathName_U(testdirW, &ntdirname, NULL, NULL))
240 {
241 ok(0, "RtlDosPathNametoNtPathName_U failed\n");
242 goto done;
243 }
244 InitializeObjectAttributes(&attr, &ntdirname, OBJ_CASE_INSENSITIVE, 0, NULL);
245
246 test_flags_NtQueryDirectoryFile(&attr, testdirA, FALSE, TRUE);
247 test_flags_NtQueryDirectoryFile(&attr, testdirA, FALSE, FALSE);
248 test_flags_NtQueryDirectoryFile(&attr, testdirA, TRUE, TRUE);
249 test_flags_NtQueryDirectoryFile(&attr, testdirA, TRUE, FALSE);
250
251 done:
252 tear_down_attribute_test(testdirA);
253 pRtlFreeUnicodeString(&ntdirname);
254 }
255
256 static void test_redirection(void)
257 {
258 ULONG old, cur;
259 NTSTATUS status;
260
261 if (!pRtlWow64EnableFsRedirection || !pRtlWow64EnableFsRedirectionEx)
262 {
263 skip( "Wow64 redirection not supported\n" );
264 return;
265 }
266 status = pRtlWow64EnableFsRedirectionEx( FALSE, &old );
267 if (status == STATUS_NOT_IMPLEMENTED)
268 {
269 skip( "Wow64 redirection not supported\n" );
270 return;
271 }
272 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
273
274 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
275 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
276 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
277
278 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
279 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
280 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
281 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
282 ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
283
284 status = pRtlWow64EnableFsRedirection( TRUE );
285 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
286 status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
287 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
288 ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
289
290 status = pRtlWow64EnableFsRedirection( FALSE );
291 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
292 status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
293 ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
294 ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
295
296 pRtlWow64EnableFsRedirectionEx( old, &cur );
297 }
298
299 START_TEST(directory)
300 {
301 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
302 if (!hntdll)
303 {
304 skip("not running on NT, skipping test\n");
305 return;
306 }
307
308 pNtClose = (void *)GetProcAddress(hntdll, "NtClose");
309 pNtOpenFile = (void *)GetProcAddress(hntdll, "NtOpenFile");
310 pNtQueryDirectoryFile = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
311 pRtlCreateUnicodeStringFromAsciiz = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeStringFromAsciiz");
312 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
313 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
314 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
315 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll,"RtlMultiByteToUnicodeN");
316 pRtlWow64EnableFsRedirection = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirection");
317 pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirectionEx");
318
319 test_NtQueryDirectoryFile();
320 test_redirection();
321 }