ntdll: Add some test cases for asynchronous I/O.
[wine/dibdrv.git] / dlls / ntdll / tests / file.c
blobaf143f2b8403145ed0d859affca9637dec87b4da
1 /* Unit test suite for Ntdll file functions
3 * Copyright 2007 Jeff Latimer
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * NOTES
20 * We use function pointers here as there is no import library for NTDLL on
21 * windows.
24 #include <stdio.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro
29 * definition errors when we get to winnt.h
31 #define WIN32_NO_STATUS
33 #include "wine/test.h"
34 #include "winternl.h"
36 static VOID (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
37 static VOID (WINAPI *pRtlFreeUnicodeString)(PUNICODE_STRING);
38 static NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
39 ULONG, ULONG, ULONG, PLARGE_INTEGER );
40 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
41 PIO_APC_ROUTINE apc, void* apc_user,
42 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
43 PLARGE_INTEGER offset, PULONG key);
44 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
45 PIO_APC_ROUTINE apc, void* apc_user,
46 PIO_STATUS_BLOCK io_status,
47 const void* buffer, ULONG length,
48 PLARGE_INTEGER offset, PULONG key);
49 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
51 static inline BOOL is_signaled( HANDLE obj )
53 return WaitForSingleObject( obj, 0 ) == 0;
56 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
58 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
60 *read = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
61 1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
62 ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
64 *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
65 ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
67 return TRUE;
70 static HANDLE create_temp_file( ULONG flags )
72 char buffer[MAX_PATH];
73 HANDLE handle;
75 GetTempFileNameA( ".", "foo", 0, buffer );
76 handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
77 flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
78 ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
79 return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
82 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
84 int *count = arg;
86 trace( "apc called block %p iosb.status %x iosb.info %lu\n",
87 iosb, U(*iosb).Status, iosb->Information );
88 (*count)++;
89 ok( !reserved, "reserved is not 0: %x\n", reserved );
92 static void read_file_test(void)
94 const char text[] = "foobar";
95 HANDLE handle, read, write;
96 NTSTATUS status;
97 IO_STATUS_BLOCK iosb;
98 DWORD written;
99 int apc_count = 0;
100 char buffer[128];
101 LARGE_INTEGER offset;
102 HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
104 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
106 /* try read with no data */
107 U(iosb).Status = 0xdeadbabe;
108 iosb.Information = 0xdeadbeef;
109 ok( is_signaled( read ), "read handle is not signaled\n" );
110 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
111 ok( status == STATUS_PENDING, "wrong status %x\n", status );
112 ok( !is_signaled( read ), "read handle is signaled\n" );
113 ok( !is_signaled( event ), "event is signaled\n" );
114 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
115 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
116 ok( !apc_count, "apc was called\n" );
117 WriteFile( write, buffer, 1, &written, NULL );
118 /* iosb updated here by async i/o */
119 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
120 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
121 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
122 ok( !is_signaled( read ), "read handle is signaled\n" );
123 ok( is_signaled( event ), "event is not signaled\n" );
124 ok( !apc_count, "apc was called\n" );
125 apc_count = 0;
126 SleepEx( 1, FALSE ); /* non-alertable sleep */
127 ok( !apc_count, "apc was called\n" );
128 SleepEx( 1, TRUE ); /* alertable sleep */
129 ok( apc_count == 1, "apc not called\n" );
131 /* with no event, the pipe handle itself gets signaled */
132 apc_count = 0;
133 U(iosb).Status = 0xdeadbabe;
134 iosb.Information = 0xdeadbeef;
135 ok( !is_signaled( read ), "read handle is not signaled\n" );
136 status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
137 ok( status == STATUS_PENDING, "wrong status %x\n", status );
138 ok( !is_signaled( read ), "read handle is signaled\n" );
139 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
140 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
141 ok( !apc_count, "apc was called\n" );
142 WriteFile( write, buffer, 1, &written, NULL );
143 /* iosb updated here by async i/o */
144 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
145 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
146 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
147 ok( is_signaled( read ), "read handle is signaled\n" );
148 ok( !apc_count, "apc was called\n" );
149 apc_count = 0;
150 SleepEx( 1, FALSE ); /* non-alertable sleep */
151 ok( !apc_count, "apc was called\n" );
152 SleepEx( 1, TRUE ); /* alertable sleep */
153 ok( apc_count == 1, "apc not called\n" );
155 /* now read with data ready */
156 apc_count = 0;
157 U(iosb).Status = 0xdeadbabe;
158 iosb.Information = 0xdeadbeef;
159 ResetEvent( event );
160 WriteFile( write, buffer, 1, &written, NULL );
161 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
162 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
163 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
164 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
165 ok( is_signaled( event ), "event is not signaled\n" );
166 ok( !apc_count, "apc was called\n" );
167 SleepEx( 1, FALSE ); /* non-alertable sleep */
168 ok( !apc_count, "apc was called\n" );
169 SleepEx( 1, TRUE ); /* alertable sleep */
170 ok( apc_count == 1, "apc not called\n" );
172 /* try read with no data */
173 apc_count = 0;
174 U(iosb).Status = 0xdeadbabe;
175 iosb.Information = 0xdeadbeef;
176 ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
177 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
178 ok( status == STATUS_PENDING, "wrong status %x\n", status );
179 ok( !is_signaled( event ), "event is signaled\n" );
180 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
181 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
182 ok( !apc_count, "apc was called\n" );
183 WriteFile( write, buffer, 1, &written, NULL );
184 /* partial read is good enough */
185 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
186 ok( is_signaled( event ), "event is signaled\n" );
187 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
188 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
189 ok( !apc_count, "apc was called\n" );
190 SleepEx( 1, TRUE ); /* alertable sleep */
191 ok( apc_count == 1, "apc was not called\n" );
193 /* read from disconnected pipe */
194 apc_count = 0;
195 U(iosb).Status = 0xdeadbabe;
196 iosb.Information = 0xdeadbeef;
197 CloseHandle( write );
198 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
199 ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
200 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
201 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
202 ok( !is_signaled( event ), "event is signaled\n" );
203 ok( !apc_count, "apc was called\n" );
204 SleepEx( 1, TRUE ); /* alertable sleep */
205 ok( !apc_count, "apc was called\n" );
206 CloseHandle( read );
208 /* read from closed handle */
209 apc_count = 0;
210 U(iosb).Status = 0xdeadbabe;
211 iosb.Information = 0xdeadbeef;
212 SetEvent( event );
213 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
214 ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
215 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
216 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
217 ok( is_signaled( event ), "event is signaled\n" ); /* not reset on invalid handle */
218 ok( !apc_count, "apc was called\n" );
219 SleepEx( 1, TRUE ); /* alertable sleep */
220 ok( !apc_count, "apc was called\n" );
222 /* disconnect while async read is in progress */
223 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
224 apc_count = 0;
225 U(iosb).Status = 0xdeadbabe;
226 iosb.Information = 0xdeadbeef;
227 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
228 ok( status == STATUS_PENDING, "wrong status %x\n", status );
229 ok( !is_signaled( event ), "event is signaled\n" );
230 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
231 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
232 ok( !apc_count, "apc was called\n" );
233 CloseHandle( write );
234 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
235 ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
236 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
237 ok( is_signaled( event ), "event is signaled\n" );
238 ok( !apc_count, "apc was called\n" );
239 SleepEx( 1, TRUE ); /* alertable sleep */
240 ok( apc_count == 1, "apc was not called\n" );
241 CloseHandle( read );
243 /* now try a real file */
244 if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
245 apc_count = 0;
246 U(iosb).Status = 0xdeadbabe;
247 iosb.Information = 0xdeadbeef;
248 offset.QuadPart = 0;
249 ResetEvent( event );
250 pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
251 ok( status == STATUS_PENDING, "wrong status %x\n", status );
252 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
253 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
254 ok( is_signaled( event ), "event is signaled\n" );
255 ok( !apc_count, "apc was called\n" );
256 SleepEx( 1, TRUE ); /* alertable sleep */
257 ok( apc_count == 1, "apc was not called\n" );
259 apc_count = 0;
260 U(iosb).Status = 0xdeadbabe;
261 iosb.Information = 0xdeadbeef;
262 offset.QuadPart = 0;
263 ResetEvent( event );
264 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
265 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
266 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
267 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
268 ok( is_signaled( event ), "event is signaled\n" );
269 ok( !apc_count, "apc was called\n" );
270 SleepEx( 1, TRUE ); /* alertable sleep */
271 ok( apc_count == 1, "apc was not called\n" );
273 /* read beyond eof */
274 apc_count = 0;
275 U(iosb).Status = 0xdeadbabe;
276 iosb.Information = 0xdeadbeef;
277 offset.QuadPart = strlen(text) + 2;
278 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
279 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
280 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
281 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
282 ok( !is_signaled( event ), "event is signaled\n" );
283 ok( !apc_count, "apc was called\n" );
284 SleepEx( 1, TRUE ); /* alertable sleep */
285 ok( !apc_count, "apc was called\n" );
286 CloseHandle( handle );
288 /* now a non-overlapped file */
289 if (!(handle = create_temp_file(0))) return;
290 apc_count = 0;
291 U(iosb).Status = 0xdeadbabe;
292 iosb.Information = 0xdeadbeef;
293 offset.QuadPart = 0;
294 pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
295 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
296 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
297 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
298 ok( is_signaled( event ), "event is signaled\n" );
299 ok( !apc_count, "apc was called\n" );
300 SleepEx( 1, TRUE ); /* alertable sleep */
301 ok( apc_count == 1, "apc was not called\n" );
303 apc_count = 0;
304 U(iosb).Status = 0xdeadbabe;
305 iosb.Information = 0xdeadbeef;
306 offset.QuadPart = 0;
307 ResetEvent( event );
308 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
309 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
310 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
311 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
312 ok( is_signaled( event ), "event is signaled\n" );
313 ok( !apc_count, "apc was called\n" );
314 SleepEx( 1, TRUE ); /* alertable sleep */
315 todo_wine ok( !apc_count, "apc was called\n" );
317 /* read beyond eof */
318 apc_count = 0;
319 U(iosb).Status = 0xdeadbabe;
320 iosb.Information = 0xdeadbeef;
321 offset.QuadPart = strlen(text) + 2;
322 ResetEvent( event );
323 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
324 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
325 todo_wine ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
326 todo_wine ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
327 todo_wine ok( is_signaled( event ), "event is not signaled\n" );
328 ok( !apc_count, "apc was called\n" );
329 SleepEx( 1, TRUE ); /* alertable sleep */
330 ok( !apc_count, "apc was called\n" );
332 CloseHandle( handle );
334 CloseHandle( event );
337 static void nt_mailslot_test(void)
339 HANDLE hslot;
340 ACCESS_MASK DesiredAccess;
341 OBJECT_ATTRIBUTES attr;
343 ULONG CreateOptions;
344 ULONG MailslotQuota;
345 ULONG MaxMessageSize;
346 LARGE_INTEGER TimeOut;
347 IO_STATUS_BLOCK IoStatusBlock;
348 NTSTATUS rc;
349 UNICODE_STRING str;
350 WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
351 'R',':','\\','F','R','E','D','\0' };
353 TimeOut.QuadPart = -1;
355 pRtlInitUnicodeString(&str, buffer1);
356 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
357 DesiredAccess = CreateOptions = MailslotQuota = MaxMessageSize = 0;
360 * Check for NULL pointer handling
362 rc = pNtCreateMailslotFile(NULL, DesiredAccess,
363 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
364 &TimeOut);
365 ok( rc == STATUS_ACCESS_VIOLATION, "rc = %x not c0000005 STATUS_ACCESS_VIOLATION\n", rc);
368 * Test to see if the Timeout can be NULL
370 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
371 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
372 NULL);
373 ok( rc == STATUS_SUCCESS, "rc = %x not STATUS_SUCCESS\n", rc);
374 ok( hslot != 0, "Handle is invalid\n");
376 if ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
379 * Test that the length field is checked properly
381 attr.Length = 0;
382 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
383 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
384 &TimeOut);
385 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
387 if (rc == STATUS_SUCCESS) pNtClose(hslot);
389 attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
390 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
391 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
392 &TimeOut);
393 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
395 if (rc == STATUS_SUCCESS) pNtClose(hslot);
398 * Test handling of a NULL unicode string in ObjectName
400 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
401 attr.ObjectName = NULL;
402 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
403 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
404 &TimeOut);
405 ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD, "rc = %x not c000003b STATUS_OBJECT_PATH_SYNTAX_BAD\n", rc);
407 if (rc == STATUS_SUCCESS) pNtClose(hslot);
410 * Test a valid call
412 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
413 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
414 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
415 &TimeOut);
416 ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x %u\n", rc, GetLastError());
417 ok( hslot != 0, "Handle is invalid\n");
419 rc = pNtClose(hslot);
420 ok( rc == STATUS_SUCCESS, "NtClose failed\n");
422 pRtlFreeUnicodeString(&str);
425 START_TEST(file)
427 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
428 if (!hntdll)
430 skip("not running on NT, skipping test\n");
431 return;
434 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
435 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
436 pNtCreateMailslotFile = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
437 pNtReadFile = (void *)GetProcAddress(hntdll, "NtReadFile");
438 pNtWriteFile = (void *)GetProcAddress(hntdll, "NtWriteFile");
439 pNtClose = (void *)GetProcAddress(hntdll, "NtClose");
441 read_file_test();
442 nt_mailslot_test();