2 * Unit test suite for Virtual* family of APIs.
4 * Copyright 2004 Dmitry Timoshkov
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/test.h"
28 static void test_VirtualAlloc(void)
32 MEMORY_BASIC_INFORMATION info
;
34 SetLastError(0xdeadbeef);
35 addr1
= VirtualAlloc(0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
36 ok(addr1
== NULL
, "VirtualAlloc should fail on zero-sized allocation\n");
37 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
38 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
39 "got %ld, expected ERROR_INVALID_PARAMETER\n", GetLastError());
41 addr1
= VirtualAlloc(0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
42 ok(addr1
!= NULL
, "VirtualAlloc failed\n");
44 /* test a not committed memory */
45 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
46 "VirtualQuery failed\n");
47 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
48 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
49 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%lx != PAGE_NOACCESS\n", info
.AllocationProtect
);
50 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
51 ok(info
.State
== MEM_RESERVE
, "%lx != MEM_RESERVE\n", info
.State
);
52 /* NT reports Protect == 0 for a not committed memory block */
53 ok(info
.Protect
== 0 /* NT */ ||
54 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
55 "%lx != PAGE_NOACCESS\n", info
.Protect
);
56 ok(info
.Type
== MEM_PRIVATE
, "%lx != MEM_PRIVATE\n", info
.Type
);
58 SetLastError(0xdeadbeef);
59 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
60 "VirtualProtect should fail on a not committed memory\n");
61 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
62 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
63 "got %ld, expected ERROR_INVALID_ADDRESS\n", GetLastError());
65 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
66 ok(addr1
== addr2
, "VirtualAlloc failed\n");
68 /* test a committed memory */
69 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
70 "VirtualQuery failed\n");
71 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
72 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
73 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%lx != PAGE_NOACCESS\n", info
.AllocationProtect
);
74 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
75 ok(info
.State
== MEM_COMMIT
, "%lx != MEM_COMMIT\n", info
.State
);
76 /* this time NT reports PAGE_NOACCESS as well */
77 ok(info
.Protect
== PAGE_NOACCESS
, "%lx != PAGE_NOACCESS\n", info
.Protect
);
78 ok(info
.Type
== MEM_PRIVATE
, "%lx != MEM_PRIVATE\n", info
.Type
);
80 /* this should fail, since not the whole range is committed yet */
81 SetLastError(0xdeadbeef);
82 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
83 "VirtualProtect should fail on a not committed memory\n");
84 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
85 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
86 "got %ld, expected ERROR_INVALID_ADDRESS\n", GetLastError());
88 ok(VirtualProtect(addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtect failed\n");
89 ok(old_prot
== PAGE_NOACCESS
,
90 "wrong old protection: got %04lx instead of PAGE_NOACCESS\n", old_prot
);
92 ok(VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtect failed\n");
93 ok(old_prot
== PAGE_READONLY
,
94 "wrong old protection: got %04lx instead of PAGE_READONLY\n", old_prot
);
96 ok(!VirtualFree(addr1
, 0x10000, 0), "VirtualFree should fail with type 0\n");
97 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
98 "got %ld, expected ERROR_INVALID_PARAMETER\n", GetLastError());
100 ok(VirtualFree(addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFree failed\n");
102 /* if the type is MEM_RELEASE, size must be 0 */
103 ok(!VirtualFree(addr1
, 1, MEM_RELEASE
), "VirtualFree should fail\n");
104 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
105 "got %ld, expected ERROR_INVALID_PARAMETER\n", GetLastError());
107 ok(VirtualFree(addr1
, 0, MEM_RELEASE
), "VirtualFree failed\n");
110 static void test_MapViewOfFile(void)
112 static const char testfile
[] = "testfile.xxx";
113 HANDLE file
, mapping
;
116 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
117 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
118 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
119 SetEndOfFile( file
);
121 /* read/write mapping */
123 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
124 ok( mapping
!= 0, "CreateFileMapping failed\n" );
126 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
127 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAPE_READ failed\n" );
128 UnmapViewOfFile( ptr
);
130 /* this fails on win9x but succeeds on NT */
131 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
132 if (ptr
) UnmapViewOfFile( ptr
);
133 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %lx\n", GetLastError() );
135 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
136 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
137 UnmapViewOfFile( ptr
);
139 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
140 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE failed\n" );
141 UnmapViewOfFile( ptr
);
142 CloseHandle( mapping
);
144 /* read-only mapping */
146 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
147 ok( mapping
!= 0, "CreateFileMapping failed\n" );
149 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
150 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ failed\n" );
151 UnmapViewOfFile( ptr
);
153 /* this fails on win9x but succeeds on NT */
154 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
155 if (ptr
) UnmapViewOfFile( ptr
);
156 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %lx\n", GetLastError() );
158 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
159 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
160 UnmapViewOfFile( ptr
);
162 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
163 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
164 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
165 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %lx\n", GetLastError() );
166 CloseHandle( mapping
);
168 /* copy-on-write mapping */
170 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
171 ok( mapping
!= 0, "CreateFileMapping failed\n" );
173 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
174 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ failed\n" );
175 UnmapViewOfFile( ptr
);
177 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
178 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_COPY failed\n" );
179 UnmapViewOfFile( ptr
);
181 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
182 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
183 UnmapViewOfFile( ptr
);
185 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
186 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
187 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
188 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %lx\n", GetLastError() );
189 CloseHandle( mapping
);
191 /* no access mapping */
193 mapping
= CreateFileMappingA( file
, NULL
, PAGE_NOACCESS
, 0, 4096, NULL
);
194 /* fails on NT but succeeds on win9x */
195 if (!mapping
) ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %lx\n", GetLastError() );
198 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
199 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ failed\n" );
200 UnmapViewOfFile( ptr
);
202 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
203 ok( !ptr
, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
204 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %lx\n", GetLastError() );
206 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
207 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
208 UnmapViewOfFile( ptr
);
210 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
211 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
212 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %lx\n", GetLastError() );
214 CloseHandle( mapping
);
219 /* now try read-only file */
221 file
= CreateFileA( testfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
222 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
224 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
225 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
226 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
227 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %lx\n", GetLastError() );
229 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
230 ok( mapping
!= 0, "CreateFileMapping PAGE_WRITECOPY failed\n" );
231 CloseHandle( mapping
);
233 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
234 ok( mapping
!= 0, "CreateFileMapping PAGE_READONLY failed\n" );
235 CloseHandle( mapping
);
238 /* now try no access file */
240 file
= CreateFileA( testfile
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
241 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
243 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
244 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
245 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
246 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %lx\n", GetLastError() );
248 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
249 ok( !mapping
, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
250 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
251 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %lx\n", GetLastError() );
253 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
254 ok( !mapping
, "CreateFileMapping PAGE_READONLY succeeded\n" );
255 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
256 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %lx\n", GetLastError() );
261 DeleteFileA( testfile
);
267 test_MapViewOfFile();