wined3d: Unify pixel format correction.
[wine] / dlls / kernel32 / tests / virtual.c
1 /*
2  * Unit test suite for Virtual* family of APIs.
3  *
4  * Copyright 2004 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wine/test.h"
28
29 #define NUM_THREADS 4
30
31 static HINSTANCE hkernel32;
32 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
33 static BOOL   (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
34
35 /* ############################### */
36
37 static HANDLE create_target_process(const char *arg)
38 {
39     char **argv;
40     char cmdline[MAX_PATH];
41     PROCESS_INFORMATION pi;
42     STARTUPINFO si = { 0 };
43     si.cb = sizeof(si);
44
45     winetest_get_mainargs( &argv );
46     sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
47     ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
48                      &si, &pi) != 0, "error: %u\n", GetLastError());
49     ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
50     return pi.hProcess;
51 }
52
53 static void test_VirtualAllocEx(void)
54 {
55     const unsigned int alloc_size = 1<<15;
56     char *src, *dst;
57     unsigned long bytes_written = 0, bytes_read = 0, i;
58     void *addr1, *addr2;
59     BOOL b;
60     DWORD old_prot;
61     MEMORY_BASIC_INFORMATION info;
62     HANDLE hProcess;
63
64     /* not exported in all windows-versions  */
65     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
66         skip("VirtualAllocEx not found\n");
67         return;
68     }
69
70     hProcess = create_target_process("sleep");
71     ok(hProcess != NULL, "Can't start process\n");
72
73     SetLastError(0xdeadbeef);
74     addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
75                            PAGE_EXECUTE_READWRITE);
76     if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
77     {   /* Win9x */
78         skip("VirtualAllocEx not implemented\n");
79         TerminateProcess(hProcess, 0);
80         CloseHandle(hProcess);
81         return;
82     }
83
84     src = HeapAlloc( GetProcessHeap(), 0, alloc_size );
85     dst = HeapAlloc( GetProcessHeap(), 0, alloc_size );
86     for (i = 0; i < alloc_size; i++)
87         src[i] = i & 0xff;
88
89     ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
90     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
91     ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
92        bytes_written);
93     b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
94     ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
95     ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
96     b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
97     ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
98
99     HeapFree( GetProcessHeap(), 0, src );
100     HeapFree( GetProcessHeap(), 0, dst );
101
102     /*
103      * The following tests parallel those in test_VirtualAlloc()
104      */
105
106     SetLastError(0xdeadbeef);
107     addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
108     ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
109     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
110        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
111         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
112
113     addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
114     ok(addr1 != NULL, "VirtualAllocEx failed\n");
115
116     /* test a not committed memory */
117     memset(&info, 'q', sizeof(info));
118     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
119     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
120     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
121     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
122     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
123     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
124     /* NT reports Protect == 0 for a not committed memory block */
125     ok(info.Protect == 0 /* NT */ ||
126        info.Protect == PAGE_NOACCESS, /* Win9x */
127         "%x != PAGE_NOACCESS\n", info.Protect);
128     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
129
130     SetLastError(0xdeadbeef);
131     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
132        "VirtualProtectEx should fail on a not committed memory\n");
133     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
134        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
135         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
136
137     addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
138     ok(addr1 == addr2, "VirtualAllocEx failed\n");
139
140     /* test a committed memory */
141     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
142         "VirtualQueryEx failed\n");
143     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
144     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
145     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
146     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
147     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
148     /* this time NT reports PAGE_NOACCESS as well */
149     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
150     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
151
152     /* this should fail, since not the whole range is committed yet */
153     SetLastError(0xdeadbeef);
154     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
155         "VirtualProtectEx should fail on a not committed memory\n");
156     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
157        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
158         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
159
160     old_prot = 0;
161     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
162     ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
163
164     old_prot = 0;
165     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
166     ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
167
168     ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
169        "VirtualFreeEx should fail with type 0\n");
170     ok(GetLastError() == ERROR_INVALID_PARAMETER,
171         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
172
173     ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
174
175     /* if the type is MEM_RELEASE, size must be 0 */
176     ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
177        "VirtualFreeEx should fail\n");
178     ok(GetLastError() == ERROR_INVALID_PARAMETER,
179         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
180
181     ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
182
183     TerminateProcess(hProcess, 0);
184     CloseHandle(hProcess);
185 }
186
187 static void test_VirtualAlloc(void)
188 {
189     void *addr1, *addr2;
190     DWORD old_prot;
191     MEMORY_BASIC_INFORMATION info;
192
193     SetLastError(0xdeadbeef);
194     addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
195     ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
196     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
197        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
198         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
199
200     addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
201     ok(addr1 != NULL, "VirtualAlloc failed\n");
202
203     /* test a not committed memory */
204     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
205         "VirtualQuery failed\n");
206     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
207     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
208     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
209     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
210     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
211     /* NT reports Protect == 0 for a not committed memory block */
212     ok(info.Protect == 0 /* NT */ ||
213        info.Protect == PAGE_NOACCESS, /* Win9x */
214         "%x != PAGE_NOACCESS\n", info.Protect);
215     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
216
217     SetLastError(0xdeadbeef);
218     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
219        "VirtualProtect should fail on a not committed memory\n");
220     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
221        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
222         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
223
224     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
225     ok(addr1 == addr2, "VirtualAlloc failed\n");
226
227     /* test a committed memory */
228     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
229         "VirtualQuery failed\n");
230     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
231     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
232     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
233     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
234     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
235     /* this time NT reports PAGE_NOACCESS as well */
236     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
237     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
238
239     /* this should fail, since not the whole range is committed yet */
240     SetLastError(0xdeadbeef);
241     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
242         "VirtualProtect should fail on a not committed memory\n");
243     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
244        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
245         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
246
247     ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
248     ok(old_prot == PAGE_NOACCESS,
249         "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
250
251     ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
252     ok(old_prot == PAGE_READONLY,
253         "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
254
255     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
256     ok(GetLastError() == ERROR_INVALID_PARAMETER,
257         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
258
259     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
260
261     /* if the type is MEM_RELEASE, size must be 0 */
262     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
263     ok(GetLastError() == ERROR_INVALID_PARAMETER,
264         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
265
266     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
267 }
268
269 static void test_MapViewOfFile(void)
270 {
271     static const char testfile[] = "testfile.xxx";
272     HANDLE file, mapping;
273     void *ptr;
274
275     SetLastError(0xdeadbeef);
276     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
277     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
278     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
279     SetEndOfFile( file );
280
281     /* read/write mapping */
282
283     SetLastError(0xdeadbeef);
284     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
285     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
286
287     SetLastError(0xdeadbeef);
288     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
289     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
290     UnmapViewOfFile( ptr );
291
292     /* this fails on win9x but succeeds on NT */
293     SetLastError(0xdeadbeef);
294     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
295     if (ptr) UnmapViewOfFile( ptr );
296     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
297
298     SetLastError(0xdeadbeef);
299     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
300     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
301     UnmapViewOfFile( ptr );
302
303     SetLastError(0xdeadbeef);
304     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
305     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
306     UnmapViewOfFile( ptr );
307     CloseHandle( mapping );
308
309     /* read-only mapping */
310
311     SetLastError(0xdeadbeef);
312     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
313     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
314
315     SetLastError(0xdeadbeef);
316     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
317     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
318     UnmapViewOfFile( ptr );
319
320     /* this fails on win9x but succeeds on NT */
321     SetLastError(0xdeadbeef);
322     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
323     if (ptr) UnmapViewOfFile( ptr );
324     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
325
326     SetLastError(0xdeadbeef);
327     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
328     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
329     UnmapViewOfFile( ptr );
330
331     SetLastError(0xdeadbeef);
332     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
333     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
334     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
335         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
336     CloseHandle( mapping );
337
338     /* copy-on-write mapping */
339
340     SetLastError(0xdeadbeef);
341     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
342     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
343
344     SetLastError(0xdeadbeef);
345     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
346     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
347     UnmapViewOfFile( ptr );
348
349     SetLastError(0xdeadbeef);
350     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
351     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
352     UnmapViewOfFile( ptr );
353
354     SetLastError(0xdeadbeef);
355     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
356     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
357     UnmapViewOfFile( ptr );
358
359     SetLastError(0xdeadbeef);
360     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
361     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
362     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
363         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
364     CloseHandle( mapping );
365
366     /* no access mapping */
367
368     SetLastError(0xdeadbeef);
369     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
370     /* fails on NT but succeeds on win9x */
371     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
372     else
373     {
374         SetLastError(0xdeadbeef);
375         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
376         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
377         UnmapViewOfFile( ptr );
378
379         SetLastError(0xdeadbeef);
380         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
381         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
382         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
383
384         SetLastError(0xdeadbeef);
385         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
386         ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
387         UnmapViewOfFile( ptr );
388
389         SetLastError(0xdeadbeef);
390         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
391         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
392         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
393
394         CloseHandle( mapping );
395     }
396
397     CloseHandle( file );
398
399     /* now try read-only file */
400
401     SetLastError(0xdeadbeef);
402     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
403     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
404
405     SetLastError(0xdeadbeef);
406     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
407     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
408     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
409         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
410
411     SetLastError(0xdeadbeef);
412     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
413     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
414     CloseHandle( mapping );
415
416     SetLastError(0xdeadbeef);
417     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
418     ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
419     CloseHandle( mapping );
420     CloseHandle( file );
421
422     /* now try no access file */
423
424     SetLastError(0xdeadbeef);
425     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
426     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
427
428     SetLastError(0xdeadbeef);
429     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
430     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
431     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
432         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
433
434     SetLastError(0xdeadbeef);
435     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
436     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
437     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
438         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
439
440     SetLastError(0xdeadbeef);
441     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
442     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
443     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
444         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
445
446     CloseHandle( file );
447     DeleteFileA( testfile );
448
449     SetLastError(0xdeadbeef);
450     file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "Global\\Foo");
451     ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
452
453     SetLastError(0xdeadbeef);
454     mapping = OpenFileMapping( FILE_MAP_READ, FALSE, "Global\\Foo" );
455     ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
456     SetLastError(0xdeadbeef);
457     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
458 todo_wine ok( !ptr, "MapViewOfFile FILE_MAP_WRITE should fail\n" );
459 todo_wine ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
460     CloseHandle( mapping );
461
462     CloseHandle( file );
463 }
464
465 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
466                                             ULONG zero_bits, SIZE_T commit_size,
467                                             const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
468                                             ULONG inherit, ULONG alloc_type, ULONG protect );
469 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
470
471 static void test_NtMapViewOfSection(void)
472 {
473     HANDLE hProcess;
474
475     static const char testfile[] = "testfile.xxx";
476     static const char data[] = "test data for NtMapViewOfSection";
477     char buffer[sizeof(data)];
478     HANDLE file, mapping;
479     void *ptr;
480     BOOL ret;
481     DWORD status, written;
482     SIZE_T size, result;
483     LARGE_INTEGER offset;
484
485     pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
486     pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
487     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
488     {
489         skip( "NtMapViewOfSection not found\n" );
490         return;
491     }
492
493     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
494     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
495     WriteFile( file, data, sizeof(data), &written, NULL );
496     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
497     SetEndOfFile( file );
498
499     /* read/write mapping */
500
501     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
502     ok( mapping != 0, "CreateFileMapping failed\n" );
503
504     hProcess = create_target_process("sleep");
505     ok(hProcess != NULL, "Can't start process\n");
506
507     ptr = NULL;
508     size = 0;
509     offset.QuadPart = 0;
510     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
511     ok( !status, "NtMapViewOfSection failed status %x\n", status );
512
513     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
514     ok( ret, "ReadProcessMemory failed\n" );
515     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
516     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
517
518     status = pNtUnmapViewOfSection( hProcess, ptr );
519     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
520
521     CloseHandle( mapping );
522     CloseHandle( file );
523     DeleteFileA( testfile );
524 }
525
526 START_TEST(virtual)
527 {
528     int argc;
529     char **argv;
530     argc = winetest_get_mainargs( &argv );
531
532     if (argc >= 3)
533     {
534         if (!strcmp(argv[2], "sleep"))
535         {
536             Sleep(5000); /* spawned process runs for at most 5 seconds */
537             return;
538         }
539         while (1)
540         {
541             void *mem;
542             BOOL ret;
543             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
544                                PAGE_EXECUTE_READWRITE);
545             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
546             if (mem == NULL) break;
547             ret = VirtualFree(mem, 0, MEM_RELEASE);
548             ok(ret, "VirtualFree failed %u\n", GetLastError());
549             if (!ret) break;
550         }
551         return;
552     }
553
554     hkernel32 = GetModuleHandleA("kernel32.dll");
555     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
556     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
557
558     test_VirtualAllocEx();
559     test_VirtualAlloc();
560     test_MapViewOfFile();
561     test_NtMapViewOfSection();
562 }