push 6434d21a91ee1ef2b42b81e2beea154c98f99170
[wine/hacks.git] / dlls / ntdll / tests / file.c
blobae0bcefb9c516439d6c24ae6a1945656698e0785
1 /* Unit test suite for Ntdll file functions
3 * Copyright 2007 Jeff Latimer
4 * Copyright 2007 Andrey Turkin
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTES
21 * We use function pointers here as there is no import library for NTDLL on
22 * windows.
25 #include <stdio.h>
26 #include <stdarg.h>
28 #include "ntstatus.h"
29 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro
30 * definition errors when we get to winnt.h
32 #define WIN32_NO_STATUS
34 #include "wine/test.h"
35 #include "winternl.h"
37 #ifndef IO_COMPLETION_ALL_ACCESS
38 #define IO_COMPLETION_ALL_ACCESS 0x001F0003
39 #endif
41 static VOID (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
42 static NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
43 ULONG, ULONG, ULONG, PLARGE_INTEGER );
44 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
45 PIO_APC_ROUTINE apc, void* apc_user,
46 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
47 PLARGE_INTEGER offset, PULONG key);
48 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
49 PIO_APC_ROUTINE apc, void* apc_user,
50 PIO_STATUS_BLOCK io_status,
51 const void* buffer, ULONG length,
52 PLARGE_INTEGER offset, PULONG key);
53 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
55 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
56 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
57 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
58 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
59 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, ULONG);
60 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
62 static inline BOOL is_signaled( HANDLE obj )
64 return WaitForSingleObject( obj, 0 ) == 0;
67 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
68 #define TEST_BUF_LEN 3
70 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
72 *read = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
73 1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
74 ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
76 *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
77 ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
79 return TRUE;
82 static HANDLE create_temp_file( ULONG flags )
84 char buffer[MAX_PATH];
85 HANDLE handle;
87 GetTempFileNameA( ".", "foo", 0, buffer );
88 handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
89 flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
90 ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
91 return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
94 #define CVALUE_FIRST 0xfffabbcc
95 #define CKEY_FIRST 0x1030341
96 #define CKEY_SECOND 0x132E46
98 ULONG_PTR completionKey;
99 IO_STATUS_BLOCK ioSb;
100 ULONG_PTR completionValue;
102 static long get_pending_msgs(HANDLE h)
104 NTSTATUS res;
105 ULONG a, req;
107 res = pNtQueryIoCompletion( h, IoCompletionBasicInformation, (PVOID)&a, sizeof(a), &req );
108 ok( res == STATUS_SUCCESS, "NtQueryIoCompletion failed: %x\n", res );
109 if (res != STATUS_SUCCESS) return -1;
110 ok( req == sizeof(a), "Unexpected response size: %x\n", req );
111 return a;
114 static BOOL get_msg(HANDLE h)
116 LARGE_INTEGER timeout = {{-10000000*3}};
117 DWORD res = pNtRemoveIoCompletion( h, &completionKey, &completionValue, &ioSb, &timeout);
118 ok( res == STATUS_SUCCESS, "NtRemoveIoCompletion failed: %x\n", res );
119 if (res != STATUS_SUCCESS)
121 completionKey = completionValue = 0;
122 memset(&ioSb, 0, sizeof(ioSb));
123 return FALSE;
125 return TRUE;
129 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
131 int *count = arg;
133 trace( "apc called block %p iosb.status %x iosb.info %lu\n",
134 iosb, U(*iosb).Status, iosb->Information );
135 (*count)++;
136 ok( !reserved, "reserved is not 0: %x\n", reserved );
139 static void read_file_test(void)
141 const char text[] = "foobar";
142 HANDLE handle, read, write;
143 NTSTATUS status;
144 IO_STATUS_BLOCK iosb;
145 DWORD written;
146 int apc_count = 0;
147 char buffer[128];
148 LARGE_INTEGER offset;
149 HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
151 buffer[0] = 1;
153 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
155 /* try read with no data */
156 U(iosb).Status = 0xdeadbabe;
157 iosb.Information = 0xdeadbeef;
158 ok( is_signaled( read ), "read handle is not signaled\n" );
159 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
160 ok( status == STATUS_PENDING, "wrong status %x\n", status );
161 ok( !is_signaled( read ), "read handle is signaled\n" );
162 ok( !is_signaled( event ), "event is signaled\n" );
163 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
164 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
165 ok( !apc_count, "apc was called\n" );
166 WriteFile( write, buffer, 1, &written, NULL );
167 /* iosb updated here by async i/o */
168 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
169 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
170 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
171 ok( !is_signaled( read ), "read handle is signaled\n" );
172 ok( is_signaled( event ), "event is not signaled\n" );
173 ok( !apc_count, "apc was called\n" );
174 apc_count = 0;
175 SleepEx( 1, FALSE ); /* non-alertable sleep */
176 ok( !apc_count, "apc was called\n" );
177 SleepEx( 1, TRUE ); /* alertable sleep */
178 ok( apc_count == 1, "apc not called\n" );
180 /* with no event, the pipe handle itself gets signaled */
181 apc_count = 0;
182 U(iosb).Status = 0xdeadbabe;
183 iosb.Information = 0xdeadbeef;
184 ok( !is_signaled( read ), "read handle is not signaled\n" );
185 status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
186 ok( status == STATUS_PENDING, "wrong status %x\n", status );
187 ok( !is_signaled( read ), "read handle is signaled\n" );
188 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
189 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
190 ok( !apc_count, "apc was called\n" );
191 WriteFile( write, buffer, 1, &written, NULL );
192 /* iosb updated here by async i/o */
193 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
194 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
195 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
196 ok( is_signaled( read ), "read handle is signaled\n" );
197 ok( !apc_count, "apc was called\n" );
198 apc_count = 0;
199 SleepEx( 1, FALSE ); /* non-alertable sleep */
200 ok( !apc_count, "apc was called\n" );
201 SleepEx( 1, TRUE ); /* alertable sleep */
202 ok( apc_count == 1, "apc not called\n" );
204 /* now read with data ready */
205 apc_count = 0;
206 U(iosb).Status = 0xdeadbabe;
207 iosb.Information = 0xdeadbeef;
208 ResetEvent( event );
209 WriteFile( write, buffer, 1, &written, NULL );
210 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
211 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
212 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
213 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
214 ok( is_signaled( event ), "event is not signaled\n" );
215 ok( !apc_count, "apc was called\n" );
216 SleepEx( 1, FALSE ); /* non-alertable sleep */
217 ok( !apc_count, "apc was called\n" );
218 SleepEx( 1, TRUE ); /* alertable sleep */
219 ok( apc_count == 1, "apc not called\n" );
221 /* try read with no data */
222 apc_count = 0;
223 U(iosb).Status = 0xdeadbabe;
224 iosb.Information = 0xdeadbeef;
225 ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
226 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
227 ok( status == STATUS_PENDING, "wrong status %x\n", status );
228 ok( !is_signaled( event ), "event is signaled\n" );
229 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
230 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
231 ok( !apc_count, "apc was called\n" );
232 WriteFile( write, buffer, 1, &written, NULL );
233 /* partial read is good enough */
234 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
235 ok( is_signaled( event ), "event is signaled\n" );
236 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
237 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
238 ok( !apc_count, "apc was called\n" );
239 SleepEx( 1, TRUE ); /* alertable sleep */
240 ok( apc_count == 1, "apc was not called\n" );
242 /* read from disconnected pipe */
243 apc_count = 0;
244 U(iosb).Status = 0xdeadbabe;
245 iosb.Information = 0xdeadbeef;
246 CloseHandle( write );
247 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
248 ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
249 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
250 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
251 ok( !is_signaled( event ), "event is signaled\n" );
252 ok( !apc_count, "apc was called\n" );
253 SleepEx( 1, TRUE ); /* alertable sleep */
254 ok( !apc_count, "apc was called\n" );
255 CloseHandle( read );
257 /* read from closed handle */
258 apc_count = 0;
259 U(iosb).Status = 0xdeadbabe;
260 iosb.Information = 0xdeadbeef;
261 SetEvent( event );
262 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
263 ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
264 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
265 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
266 ok( is_signaled( event ), "event is signaled\n" ); /* not reset on invalid handle */
267 ok( !apc_count, "apc was called\n" );
268 SleepEx( 1, TRUE ); /* alertable sleep */
269 ok( !apc_count, "apc was called\n" );
271 /* disconnect while async read is in progress */
272 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
273 apc_count = 0;
274 U(iosb).Status = 0xdeadbabe;
275 iosb.Information = 0xdeadbeef;
276 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
277 ok( status == STATUS_PENDING, "wrong status %x\n", status );
278 ok( !is_signaled( event ), "event is signaled\n" );
279 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
280 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
281 ok( !apc_count, "apc was called\n" );
282 CloseHandle( write );
283 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
284 ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
285 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
286 ok( is_signaled( event ), "event is signaled\n" );
287 ok( !apc_count, "apc was called\n" );
288 SleepEx( 1, TRUE ); /* alertable sleep */
289 ok( apc_count == 1, "apc was not called\n" );
290 CloseHandle( read );
292 /* now try a real file */
293 if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
294 apc_count = 0;
295 U(iosb).Status = 0xdeadbabe;
296 iosb.Information = 0xdeadbeef;
297 offset.QuadPart = 0;
298 ResetEvent( event );
299 pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
300 ok( status == STATUS_PENDING, "wrong status %x\n", status );
301 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
302 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
303 ok( is_signaled( event ), "event is signaled\n" );
304 ok( !apc_count, "apc was called\n" );
305 SleepEx( 1, TRUE ); /* alertable sleep */
306 ok( apc_count == 1, "apc was not called\n" );
308 apc_count = 0;
309 U(iosb).Status = 0xdeadbabe;
310 iosb.Information = 0xdeadbeef;
311 offset.QuadPart = 0;
312 ResetEvent( event );
313 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
314 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
315 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
316 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
317 ok( is_signaled( event ), "event is signaled\n" );
318 ok( !apc_count, "apc was called\n" );
319 SleepEx( 1, TRUE ); /* alertable sleep */
320 ok( apc_count == 1, "apc was not called\n" );
322 /* read beyond eof */
323 apc_count = 0;
324 U(iosb).Status = 0xdeadbabe;
325 iosb.Information = 0xdeadbeef;
326 offset.QuadPart = strlen(text) + 2;
327 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
328 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
329 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
330 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
331 ok( !is_signaled( event ), "event is signaled\n" );
332 ok( !apc_count, "apc was called\n" );
333 SleepEx( 1, TRUE ); /* alertable sleep */
334 ok( !apc_count, "apc was called\n" );
335 CloseHandle( handle );
337 /* now a non-overlapped file */
338 if (!(handle = create_temp_file(0))) return;
339 apc_count = 0;
340 U(iosb).Status = 0xdeadbabe;
341 iosb.Information = 0xdeadbeef;
342 offset.QuadPart = 0;
343 pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
344 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
345 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
346 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
347 ok( is_signaled( event ), "event is signaled\n" );
348 ok( !apc_count, "apc was called\n" );
349 SleepEx( 1, TRUE ); /* alertable sleep */
350 ok( apc_count == 1, "apc was not called\n" );
352 apc_count = 0;
353 U(iosb).Status = 0xdeadbabe;
354 iosb.Information = 0xdeadbeef;
355 offset.QuadPart = 0;
356 ResetEvent( event );
357 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
358 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
359 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
360 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
361 ok( is_signaled( event ), "event is signaled\n" );
362 ok( !apc_count, "apc was called\n" );
363 SleepEx( 1, TRUE ); /* alertable sleep */
364 todo_wine ok( !apc_count, "apc was called\n" );
366 /* read beyond eof */
367 apc_count = 0;
368 U(iosb).Status = 0xdeadbabe;
369 iosb.Information = 0xdeadbeef;
370 offset.QuadPart = strlen(text) + 2;
371 ResetEvent( event );
372 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
373 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
374 todo_wine ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
375 todo_wine ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
376 todo_wine ok( is_signaled( event ), "event is not signaled\n" );
377 ok( !apc_count, "apc was called\n" );
378 SleepEx( 1, TRUE ); /* alertable sleep */
379 ok( !apc_count, "apc was called\n" );
381 CloseHandle( handle );
383 CloseHandle( event );
386 static void nt_mailslot_test(void)
388 HANDLE hslot;
389 ACCESS_MASK DesiredAccess;
390 OBJECT_ATTRIBUTES attr;
392 ULONG CreateOptions;
393 ULONG MailslotQuota;
394 ULONG MaxMessageSize;
395 LARGE_INTEGER TimeOut;
396 IO_STATUS_BLOCK IoStatusBlock;
397 NTSTATUS rc;
398 UNICODE_STRING str;
399 WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
400 'R',':','\\','F','R','E','D','\0' };
402 TimeOut.QuadPart = -1;
404 pRtlInitUnicodeString(&str, buffer1);
405 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
406 CreateOptions = MailslotQuota = MaxMessageSize = 0;
407 DesiredAccess = GENERIC_READ;
410 * Check for NULL pointer handling
412 rc = pNtCreateMailslotFile(NULL, DesiredAccess,
413 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
414 &TimeOut);
415 ok( rc == STATUS_ACCESS_VIOLATION ||
416 rc == STATUS_INVALID_PARAMETER, /* win2k3 */
417 "rc = %x not STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER\n", rc);
420 * Test to see if the Timeout can be NULL
422 hslot = (HANDLE)0xdeadbeef;
423 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
424 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
425 NULL);
426 ok( rc == STATUS_SUCCESS ||
427 rc == STATUS_INVALID_PARAMETER, /* win2k3 */
428 "rc = %x not STATUS_SUCCESS or STATUS_INVALID_PARAMETER\n", rc);
429 ok( hslot != 0, "Handle is invalid\n");
431 if ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
434 * Test that the length field is checked properly
436 attr.Length = 0;
437 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
438 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
439 &TimeOut);
440 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
442 if (rc == STATUS_SUCCESS) pNtClose(hslot);
444 attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
445 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
446 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
447 &TimeOut);
448 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
450 if (rc == STATUS_SUCCESS) pNtClose(hslot);
453 * Test handling of a NULL unicode string in ObjectName
455 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
456 attr.ObjectName = NULL;
457 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
458 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
459 &TimeOut);
460 ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD ||
461 rc == STATUS_INVALID_PARAMETER,
462 "rc = %x not STATUS_OBJECT_PATH_SYNTAX_BAD or STATUS_INVALID_PARAMETER\n", rc);
464 if (rc == STATUS_SUCCESS) pNtClose(hslot);
467 * Test a valid call
469 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
470 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
471 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
472 &TimeOut);
473 ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x\n", rc);
474 ok( hslot != 0, "Handle is invalid\n");
476 rc = pNtClose(hslot);
477 ok( rc == STATUS_SUCCESS, "NtClose failed\n");
480 static void test_iocp_setcompletion(HANDLE h)
482 NTSTATUS res;
483 long count;
485 res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, 3 );
486 ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
488 count = get_pending_msgs(h);
489 ok( count == 1, "Unexpected msg count: %ld\n", count );
491 if (get_msg(h))
493 ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
494 ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
495 ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
496 ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
499 count = get_pending_msgs(h);
500 ok( !count, "Unexpected msg count: %ld\n", count );
503 static void test_iocp_fileio(HANDLE h)
505 static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
507 IO_STATUS_BLOCK iosb;
508 FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
509 HANDLE hPipeSrv, hPipeClt;
510 NTSTATUS res;
512 hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
513 ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
514 if (hPipeSrv != INVALID_HANDLE_VALUE )
516 hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
517 ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
518 if (hPipeClt != INVALID_HANDLE_VALUE)
520 res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
521 ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
522 CloseHandle(hPipeClt);
524 CloseHandle( hPipeSrv );
527 hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
528 ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
529 if (hPipeSrv == INVALID_HANDLE_VALUE )
530 return;
532 hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
533 ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
534 if (hPipeClt != INVALID_HANDLE_VALUE)
536 OVERLAPPED o = {0,};
537 BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
538 DWORD read;
539 long count;
541 NTSTATUS res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
542 ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
543 ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
545 memset( send_buf, 0, TEST_BUF_LEN );
546 memset( recv_buf, 0xde, TEST_BUF_LEN );
547 count = get_pending_msgs(h);
548 ok( !count, "Unexpected msg count: %ld\n", count );
549 ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
550 count = get_pending_msgs(h);
551 ok( !count, "Unexpected msg count: %ld\n", count );
552 WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
554 if (get_msg(h))
556 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
557 ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
558 ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
559 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
560 ok( !memcmp( send_buf, recv_buf, TEST_BUF_LEN ), "Receive buffer (%x %x %x) did not match send buffer (%x %x %x)\n", recv_buf[0], recv_buf[1], recv_buf[2], send_buf[0], send_buf[1], send_buf[2] );
562 count = get_pending_msgs(h);
563 ok( !count, "Unexpected msg count: %ld\n", count );
565 memset( send_buf, 0, TEST_BUF_LEN );
566 memset( recv_buf, 0xde, TEST_BUF_LEN );
567 WriteFile( hPipeClt, send_buf, 2, &read, NULL );
568 count = get_pending_msgs(h);
569 ok( !count, "Unexpected msg count: %ld\n", count );
570 ReadFile( hPipeSrv, recv_buf, 2, &read, &o);
571 count = get_pending_msgs(h);
572 ok( count == 1, "Unexpected msg count: %ld\n", count );
573 if (get_msg(h))
575 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
576 ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
577 ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
578 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
579 ok( !memcmp( send_buf, recv_buf, 2 ), "Receive buffer (%x %x) did not match send buffer (%x %x)\n", recv_buf[0], recv_buf[1], send_buf[0], send_buf[1] );
582 ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
583 CloseHandle( hPipeSrv );
584 count = get_pending_msgs(h);
585 ok( count == 1, "Unexpected msg count: %ld\n", count );
586 if (get_msg(h))
588 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
589 ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
590 /* wine sends wrong status here */
591 todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
592 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
596 CloseHandle( hPipeClt );
599 static void test_iocompletion(void)
601 HANDLE h = INVALID_HANDLE_VALUE;
602 NTSTATUS res;
604 res = pNtCreateIoCompletion( &h, IO_COMPLETION_ALL_ACCESS, NULL, 0);
606 ok( res == 0, "NtCreateIoCompletion anonymous failed: %x\n", res );
607 ok( h && h != INVALID_HANDLE_VALUE, "Invalid handle returned\n" );
609 if ( h && h != INVALID_HANDLE_VALUE)
611 test_iocp_setcompletion(h);
612 test_iocp_fileio(h);
613 pNtClose(h);
617 START_TEST(file)
619 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
620 if (!hntdll)
622 skip("not running on NT, skipping test\n");
623 return;
626 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
627 pNtCreateMailslotFile = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
628 pNtReadFile = (void *)GetProcAddress(hntdll, "NtReadFile");
629 pNtWriteFile = (void *)GetProcAddress(hntdll, "NtWriteFile");
630 pNtClose = (void *)GetProcAddress(hntdll, "NtClose");
631 pNtCreateIoCompletion = (void *)GetProcAddress(hntdll, "NtCreateIoCompletion");
632 pNtOpenIoCompletion = (void *)GetProcAddress(hntdll, "NtOpenIoCompletion");
633 pNtQueryIoCompletion = (void *)GetProcAddress(hntdll, "NtQueryIoCompletion");
634 pNtRemoveIoCompletion = (void *)GetProcAddress(hntdll, "NtRemoveIoCompletion");
635 pNtSetIoCompletion = (void *)GetProcAddress(hntdll, "NtSetIoCompletion");
636 pNtSetInformationFile = (void *)GetProcAddress(hntdll, "NtSetInformationFile");
638 read_file_test();
639 nt_mailslot_test();
640 test_iocompletion();