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");