kernel32/tests: Use WineTest contexts to simplify test_Loader().

Signed-off-by: Francois Gouget <fgouget@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Francois Gouget 2021-07-18 07:35:11 +02:00 committed by Alexandre Julliard
parent 3f779e47ee
commit 566a98800d
1 changed files with 50 additions and 48 deletions

View File

@ -896,6 +896,7 @@ static void test_Loader(void)
for (i = 0; i < ARRAY_SIZE(td); i++)
{
winetest_push_context("%d", i);
nt_header = nt_header_template;
nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
@ -915,56 +916,55 @@ static void test_Loader(void)
MEMORY_BASIC_INFORMATION info;
void *ptr;
ok( td[i].errors[0] == ERROR_SUCCESS, "%d: should have failed\n", i );
ok( td[i].errors[0] == ERROR_SUCCESS, "should have failed\n" );
SetLastError(0xdeadbeef);
size = VirtualQuery(hlib, &info, sizeof(info));
ok(size == sizeof(info),
"%d: VirtualQuery error %d\n", i, GetLastError());
ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %lx != expected %x\n",
i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
ok(info.BaseAddress == hlib, "%p != %p\n", info.BaseAddress, hlib);
ok(info.AllocationBase == hlib, "%p != %p\n", info.AllocationBase, hlib);
ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "got %lx != expected %x\n",
info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
if (nt_header.OptionalHeader.SectionAlignment < page_size)
ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%x != PAGE_EXECUTE_WRITECOPY\n", info.Protect);
else
ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
ok(info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect);
ok(info.Type == SEC_IMAGE, "%x != SEC_IMAGE\n", info.Type);
SetLastError(0xdeadbeef);
ptr = VirtualAlloc(hlib, page_size, MEM_COMMIT, info.Protect);
ok(!ptr, "%d: VirtualAlloc should fail\n", i);
ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
ok(!ptr, "VirtualAlloc should fail\n");
ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
SetLastError(0xdeadbeef);
size = VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info));
ok(size == sizeof(info),
"%d: VirtualQuery error %d\n", i, GetLastError());
ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
if (nt_header.OptionalHeader.SectionAlignment == page_size ||
nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
{
ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %p != expected %p\n",
i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "got %p != expected %p\n",
info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
ok(info.AllocationBase == 0, "%p != 0\n", info.AllocationBase);
ok(info.AllocationProtect == 0, "%x != 0\n", info.AllocationProtect);
/*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
ok(info.State == MEM_FREE, "%x != MEM_FREE\n", info.State);
ok(info.Type == 0, "%x != 0\n", info.Type);
ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
}
else
{
ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
ok(info.RegionSize == ALIGN_SIZE(file_size, page_size), "%d: got %lx != expected %x\n",
i, info.RegionSize, ALIGN_SIZE(file_size, page_size));
ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%x != PAGE_EXECUTE_WRITECOPY\n", info.Protect);
ok(info.BaseAddress == hlib, "got %p != expected %p\n", info.BaseAddress, hlib);
ok(info.AllocationBase == hlib, "%p != %p\n", info.AllocationBase, hlib);
ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
ok(info.RegionSize == ALIGN_SIZE(file_size, page_size), "got %lx != expected %x\n",
info.RegionSize, ALIGN_SIZE(file_size, page_size));
ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
ok(info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect);
ok(info.Type == SEC_IMAGE, "%x != SEC_IMAGE\n", info.Type);
}
/* header: check the zeroing of alignment */
@ -974,7 +974,7 @@ static void test_Loader(void)
start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
size = ALIGN_SIZE((ULONG_PTR)start, page_size) - (ULONG_PTR)start;
ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
ok(!memcmp(start, filler, size), "header alignment is not cleared\n");
}
if (nt_header.FileHeader.NumberOfSections)
@ -982,25 +982,25 @@ static void test_Loader(void)
SetLastError(0xdeadbeef);
size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
ok(size == sizeof(info),
"%d: VirtualQuery error %d\n", i, GetLastError());
"VirtualQuery error %d\n", GetLastError());
if (nt_header.OptionalHeader.SectionAlignment < page_size)
{
ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %lx != expected %x\n",
i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
ok(info.BaseAddress == hlib, "got %p != expected %p\n", info.BaseAddress, hlib);
ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "got %lx != expected %x\n",
info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%x != PAGE_EXECUTE_WRITECOPY\n", info.Protect);
}
else
{
ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, page_size), "%d: got %lx != expected %x\n",
i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, page_size));
ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)hlib + section.VirtualAddress);
ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, page_size), "got %lx != expected %x\n",
info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, page_size));
ok(info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect);
}
ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
ok(info.AllocationBase == hlib, "%p != %p\n", info.AllocationBase, hlib);
ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
ok(info.Type == SEC_IMAGE, "%x != SEC_IMAGE\n", info.Type);
if (nt_header.OptionalHeader.SectionAlignment >= page_size)
ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
@ -1014,14 +1014,14 @@ static void test_Loader(void)
start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
size = ALIGN_SIZE((ULONG_PTR)start, page_size) - (ULONG_PTR)start;
ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
ok(memcmp(start, filler, size), "alignment should not be cleared\n");
}
SetLastError(0xdeadbeef);
ptr = VirtualAlloc((char *)hlib + section.VirtualAddress, page_size, MEM_COMMIT, info.Protect);
ok(!ptr, "%d: VirtualAlloc should fail\n", i);
ok(!ptr, "VirtualAlloc should fail\n");
ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_ADDRESS,
"%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
"expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
}
SetLastError(0xdeadbeef);
@ -1120,6 +1120,7 @@ static void test_Loader(void)
if (!file_size)
{
ok(0, "could not create %s\n", dll_name);
winetest_pop_context();
break;
}
@ -1137,12 +1138,13 @@ static void test_Loader(void)
{
error_match = td[i].errors[error_index] == GetLastError();
}
ok(error_match, "%d: unexpected error %d\n", i, GetLastError());
ok(error_match, "unexpected error %d\n", GetLastError());
}
SetLastError(0xdeadbeef);
ret = DeleteFileA(dll_name);
ok(ret, "DeleteFile error %d\n", GetLastError());
winetest_pop_context();
}
nt_header = nt_header_template;