user32: Rename GetKeyboardLayout param; it's a thread ID, not a layout.
[wine] / dlls / ntdll / tests / 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     int todo;                 /* set if it doesn't work on wine yet */
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     { 1, FILE_ATTRIBUTE_HIDDEN,    "h.tmp", NULL, "hidden" },
64     { 1, FILE_ATTRIBUTE_SYSTEM,    "s.tmp", NULL, "system" },
65     { 0, FILE_ATTRIBUTE_DIRECTORY, "d.tmp", NULL, "directory" },
66     { 0, 0, NULL }
67 };
68 static const int max_test_dir_size = 20;  /* size of above plus some for .. etc */
69
70 /* Create a test directory full of attribute test files, clear counts */
71 static void set_up_attribute_test(const char *testdirA)
72 {
73     int i;
74     BOOL ret;
75
76     ret = CreateDirectoryA(testdirA, NULL);
77     ok(ret, "couldn't create dir '%s', error %d\n", testdirA, GetLastError());
78
79     for (i=0; testfiles[i].name; i++) {
80         char buf[MAX_PATH];
81         pRtlMultiByteToUnicodeN(testfiles[i].nameW, sizeof(testfiles[i].nameW), NULL, testfiles[i].name, strlen(testfiles[i].name)+1);
82
83         sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
84         testfiles[i].nfound = 0;
85         if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
86             ret = CreateDirectoryA(buf, NULL);
87             ok(ret, "couldn't create dir '%s', error %d\n", buf, GetLastError());
88         } else {
89             HANDLE h = CreateFileA(buf,
90                                    GENERIC_READ|GENERIC_WRITE,
91                                    0, NULL, CREATE_ALWAYS,
92                                    testfiles[i].attr, 0);
93             ok( h != INVALID_HANDLE_VALUE, "failed to create temp file '%s'\n", buf );
94             CloseHandle(h);
95         }
96     }
97 }
98
99 /* Remove the given test directory and the attribute test files, if any */
100 static void tear_down_attribute_test(const char *testdirA)
101 {
102     int i;
103
104     for (i=0; testfiles[i].name; i++) {
105         int ret;
106         char buf[MAX_PATH];
107         sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
108         if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
109             ret = RemoveDirectory(buf);
110             ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
111                "Failed to rmdir %s, error %d\n", buf, GetLastError());
112         } else {
113             ret = DeleteFile(buf);
114             ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
115                "Failed to rm %s, error %d\n", buf, GetLastError());
116         }
117     }
118     RemoveDirectoryA(testdirA);
119 }
120
121 /* Match one found file against testfiles[], increment count if found */
122 static void tally_test_file(FILE_BOTH_DIRECTORY_INFORMATION *dir_info)
123 {
124     int i;
125     DWORD attribmask =
126       (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT);
127     DWORD attrib = dir_info->FileAttributes & attribmask;
128     WCHAR *nameW = dir_info->FileName;
129     int namelen = dir_info->FileNameLength / sizeof(WCHAR);
130
131     if (nameW[0] == '.')
132         return;
133
134     for (i=0; testfiles[i].name; i++) {
135         int len = strlen(testfiles[i].name);
136         if (namelen != len || memcmp(nameW, testfiles[i].nameW, len*sizeof(WCHAR)))
137             continue;
138         if (testfiles[i].todo) {
139             todo_wine
140             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);
141         } else {
142             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);
143         }
144         testfiles[i].nfound++;
145         break;
146     }
147     ok(testfiles[i].name != NULL, "unexpected file found\n");
148 }
149
150 static void test_NtQueryDirectoryFile(void)
151 {
152     OBJECT_ATTRIBUTES attr;
153     UNICODE_STRING ntdirname;
154     char testdirA[MAX_PATH];
155     WCHAR testdirW[MAX_PATH];
156     HANDLE dirh;
157     IO_STATUS_BLOCK io;
158     UINT data_pos;
159     UINT data_len;    /* length of dir data */
160     BYTE data[8192];  /* directory data */
161     FILE_BOTH_DIRECTORY_INFORMATION *dir_info;
162     DWORD status;
163     int numfiles;
164     int i;
165
166     /* Clean up from prior aborted run, if any, then set up test files */
167     ok(GetTempPathA(MAX_PATH, testdirA), "couldn't get temp dir\n");
168     strcat(testdirA, "NtQueryDirectoryFile.tmp");
169     tear_down_attribute_test(testdirA);
170     set_up_attribute_test(testdirA);
171
172     /* Read the directory and note which files are found */
173     pRtlMultiByteToUnicodeN(testdirW, sizeof(testdirW), NULL, testdirA, strlen(testdirA)+1);
174     if (!pRtlDosPathNameToNtPathName_U(testdirW, &ntdirname, NULL, NULL))
175     {
176         ok(0,"RtlDosPathNametoNtPathName_U failed\n");
177         goto done;
178     }
179     InitializeObjectAttributes(&attr, &ntdirname, OBJ_CASE_INSENSITIVE, 0, NULL);
180     status = pNtOpenFile( &dirh, SYNCHRONIZE | FILE_LIST_DIRECTORY, &attr, &io,
181                          FILE_OPEN,
182                          FILE_SYNCHRONOUS_IO_NONALERT|FILE_OPEN_FOR_BACKUP_INTENT|FILE_DIRECTORY_FILE);
183     ok (status == STATUS_SUCCESS, "failed to open dir '%s', ret 0x%x, error %d\n", testdirA, status, GetLastError());
184     if (status != STATUS_SUCCESS) {
185        skip("can't test if we can't open the directory\n");
186        goto done;
187     }
188
189     pNtQueryDirectoryFile( dirh, NULL, NULL, NULL, &io, data, sizeof(data),
190                        FileBothDirectoryInformation, FALSE, NULL, TRUE );
191     ok (U(io).Status == STATUS_SUCCESS, "filed to query directory; status %x\n", U(io).Status);
192     data_len = io.Information;
193     ok (data_len >= sizeof(FILE_BOTH_DIRECTORY_INFORMATION), "not enough data in directory\n");
194
195     data_pos = 0;
196     numfiles = 0;
197     while ((data_pos < data_len) && (numfiles < max_test_dir_size)) {
198         dir_info = (FILE_BOTH_DIRECTORY_INFORMATION *)(data + data_pos);
199
200         tally_test_file(dir_info);
201
202         if (dir_info->NextEntryOffset == 0) {
203             pNtQueryDirectoryFile( dirh, 0, NULL, NULL, &io, data, sizeof(data),
204                                FileBothDirectoryInformation, FALSE, NULL, FALSE );
205             if (U(io).Status == STATUS_NO_MORE_FILES)
206                 break;
207             ok (U(io).Status == STATUS_SUCCESS, "filed to query directory; status %x\n", U(io).Status);
208             data_len = io.Information;
209             if (data_len < sizeof(FILE_BOTH_DIRECTORY_INFORMATION))
210                 break;
211             data_pos = 0;
212         } else {
213             data_pos += dir_info->NextEntryOffset;
214         }
215         numfiles++;
216     }
217     ok(numfiles < max_test_dir_size, "too many loops\n");
218
219     for (i=0; testfiles[i].name; i++)
220         ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found\n",
221           testfiles[i].nfound, testfiles[i].description);
222
223     pNtClose(dirh);
224 done:
225     tear_down_attribute_test(testdirA);
226     pRtlFreeUnicodeString(&ntdirname);
227 }
228
229 static void test_redirection(void)
230 {
231     ULONG old, cur;
232     NTSTATUS status;
233
234     if (!pRtlWow64EnableFsRedirection || !pRtlWow64EnableFsRedirectionEx)
235     {
236         skip( "Wow64 redirection not supported\n" );
237         return;
238     }
239     status = pRtlWow64EnableFsRedirectionEx( FALSE, &old );
240     if (status == STATUS_NOT_IMPLEMENTED)
241     {
242         skip( "Wow64 redirection not supported\n" );
243         return;
244     }
245     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
246
247     status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
248     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
249     ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
250
251     status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
252     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
253     status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
254     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
255     ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
256
257     status = pRtlWow64EnableFsRedirection( TRUE );
258     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
259     status = pRtlWow64EnableFsRedirectionEx( TRUE, &cur );
260     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
261     ok( !cur, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
262
263     status = pRtlWow64EnableFsRedirection( FALSE );
264     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
265     status = pRtlWow64EnableFsRedirectionEx( FALSE, &cur );
266     ok( !status, "RtlWow64EnableFsRedirectionEx failed status %x\n", status );
267     ok( cur == 1, "RtlWow64EnableFsRedirectionEx got %u\n", cur );
268
269     pRtlWow64EnableFsRedirectionEx( old, &cur );
270 }
271
272 START_TEST(directory)
273 {
274     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
275     if (!hntdll)
276     {
277         skip("not running on NT, skipping test\n");
278         return;
279     }
280
281     pNtClose                = (void *)GetProcAddress(hntdll, "NtClose");
282     pNtOpenFile             = (void *)GetProcAddress(hntdll, "NtOpenFile");
283     pNtQueryDirectoryFile   = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
284     pRtlCreateUnicodeStringFromAsciiz = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeStringFromAsciiz");
285     pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
286     pRtlInitUnicodeString   = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
287     pRtlFreeUnicodeString   = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
288     pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll,"RtlMultiByteToUnicodeN");
289     pRtlWow64EnableFsRedirection = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirection");
290     pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll,"RtlWow64EnableFsRedirectionEx");
291
292     test_NtQueryDirectoryFile();
293     test_redirection();
294 }