comctl32/listview: Free ID array when removing all items.
[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 NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
47                                                    LPCSTR src, DWORD srclen );
48
49 /* The attribute sets to test */
50 struct testfile_s {
51     int todo;                 /* set if it doesn't work on wine yet */
52     const DWORD attr;         /* desired attribute */
53     const char *name;         /* filename to use */
54     const char *target;       /* what to point to (only for reparse pts) */
55     const char *description;  /* for error messages */
56     int nfound;               /* How many were found (expect 1) */
57     WCHAR nameW[20];          /* unicode version of name (filled in later) */
58 } testfiles[] = {
59     { 0, FILE_ATTRIBUTE_NORMAL,    "n.tmp", NULL, "normal" },
60     { 1, FILE_ATTRIBUTE_HIDDEN,    "h.tmp", NULL, "hidden" },
61     { 1, FILE_ATTRIBUTE_SYSTEM,    "s.tmp", NULL, "system" },
62     { 0, FILE_ATTRIBUTE_DIRECTORY, "d.tmp", NULL, "directory" },
63     { 0, 0, NULL }
64 };
65 static const int max_test_dir_size = 20;  /* size of above plus some for .. etc */
66
67 /* Create a test directory full of attribute test files, clear counts */
68 static void set_up_attribute_test(const char *testdirA)
69 {
70     int i;
71
72     ok(CreateDirectoryA(testdirA, NULL),
73        "couldn't create dir '%s', error %d\n", testdirA, GetLastError());
74
75     for (i=0; testfiles[i].name; i++) {
76         char buf[MAX_PATH];
77         pRtlMultiByteToUnicodeN(testfiles[i].nameW, sizeof(testfiles[i].nameW), NULL, testfiles[i].name, strlen(testfiles[i].name)+1);
78
79         sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
80         testfiles[i].nfound = 0;
81         if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
82             ok(CreateDirectoryA(buf, NULL),
83                "couldn't create dir '%s', error %d\n", buf, GetLastError());
84         } else {
85             HANDLE h = CreateFileA(buf,
86                                    GENERIC_READ|GENERIC_WRITE,
87                                    0, NULL, CREATE_ALWAYS,
88                                    testfiles[i].attr, 0);
89             ok( h != INVALID_HANDLE_VALUE, "failed to create temp file '%s'\n", buf );
90             CloseHandle(h);
91         }
92     }
93 }
94
95 /* Remove the given test directory and the attribute test files, if any */
96 static void tear_down_attribute_test(const char *testdirA)
97 {
98     int i;
99
100     for (i=0; testfiles[i].name; i++) {
101         int ret;
102         char buf[MAX_PATH];
103         sprintf(buf, "%s\\%s", testdirA, testfiles[i].name);
104         if (testfiles[i].attr & FILE_ATTRIBUTE_DIRECTORY) {
105             ret = RemoveDirectory(buf);
106             ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
107                "Failed to rmdir %s, error %d\n", buf, GetLastError());
108         } else {
109             ret = DeleteFile(buf);
110             ok(ret || (GetLastError() == ERROR_PATH_NOT_FOUND),
111                "Failed to rm %s, error %d\n", buf, GetLastError());
112         }
113     }
114     RemoveDirectoryA(testdirA);
115 }
116
117 /* Match one found file against testfiles[], increment count if found */
118 static void tally_test_file(FILE_BOTH_DIRECTORY_INFORMATION *dir_info)
119 {
120     int i;
121     DWORD attribmask =
122       (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT);
123     DWORD attrib = dir_info->FileAttributes & attribmask;
124     WCHAR *nameW = dir_info->FileName;
125     int namelen = dir_info->FileNameLength / sizeof(WCHAR);
126
127     if (nameW[0] == '.')
128         return;
129
130     for (i=0; testfiles[i].name; i++) {
131         int len = strlen(testfiles[i].name);
132         if (namelen != len || memcmp(nameW, testfiles[i].nameW, len*sizeof(WCHAR)))
133             continue;
134         if (testfiles[i].todo) {
135             todo_wine
136             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);
137         } else {
138             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);
139         }
140         testfiles[i].nfound++;
141         break;
142     }
143     ok(testfiles[i].name != NULL, "unexpected file found\n");
144 }
145
146 static void test_NtQueryDirectoryFile(void)
147 {
148     OBJECT_ATTRIBUTES attr;
149     UNICODE_STRING ntdirname;
150     char testdirA[MAX_PATH];
151     WCHAR testdirW[MAX_PATH];
152     HANDLE dirh;
153     IO_STATUS_BLOCK io;
154     UINT data_pos;
155     UINT data_len;    /* length of dir data */
156     BYTE data[8192];  /* directory data */
157     FILE_BOTH_DIRECTORY_INFORMATION *dir_info;
158     DWORD status;
159     int numfiles;
160     int i;
161
162     /* Clean up from prior aborted run, if any, then set up test files */
163     ok(GetTempPathA(MAX_PATH, testdirA), "couldn't get temp dir\n");
164     strcat(testdirA, "NtQueryDirectoryFile.tmp");
165     tear_down_attribute_test(testdirA);
166     set_up_attribute_test(testdirA);
167
168     /* Read the directory and note which files are found */
169     pRtlMultiByteToUnicodeN(testdirW, sizeof(testdirW), NULL, testdirA, strlen(testdirA)+1);
170     if (!pRtlDosPathNameToNtPathName_U(testdirW, &ntdirname, NULL, NULL))
171     {
172         ok(0,"RtlDosPathNametoNtPathName_U failed\n");
173         goto done;
174     }
175     InitializeObjectAttributes(&attr, &ntdirname, 0, 0, NULL);
176     status = pNtOpenFile( &dirh, SYNCHRONIZE | FILE_LIST_DIRECTORY, &attr, &io,
177                          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        goto done;
183     }
184
185     pNtQueryDirectoryFile( dirh, NULL, NULL, NULL, &io, data, sizeof(data),
186                        FileBothDirectoryInformation, FALSE, NULL, TRUE );
187     ok (U(io).Status == STATUS_SUCCESS, "filed 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, sizeof(data),
200                                FileBothDirectoryInformation, FALSE, NULL, FALSE );
201             if (U(io).Status == STATUS_NO_MORE_FILES)
202                 break;
203             ok (U(io).Status == STATUS_SUCCESS, "filed 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     for (i=0; testfiles[i].name; i++)
216         ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found\n",
217           testfiles[i].nfound, testfiles[i].description);
218
219     pNtClose(dirh);
220 done:
221     tear_down_attribute_test(testdirA);
222 }
223
224 START_TEST(directory)
225 {
226     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
227     if (!hntdll)
228     {
229         skip("not running on NT, skipping test\n");
230         return;
231     }
232
233     pNtClose                = (void *)GetProcAddress(hntdll, "NtClose");
234     pNtOpenFile             = (void *)GetProcAddress(hntdll, "NtOpenFile");
235     pNtQueryDirectoryFile   = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
236     pRtlCreateUnicodeStringFromAsciiz = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeStringFromAsciiz");
237     pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
238     pRtlInitUnicodeString   = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
239     pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll,"RtlMultiByteToUnicodeN");
240
241     test_NtQueryDirectoryFile();
242 }