imagehlp/tests: Link directly to imagehlp and psapi.

Signed-off-by: Zebediah Figura <z.figura12@gmail.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
(cherry picked from commit 670913af21)
Signed-off-by: Michael Stefaniuc <mstefani@winehq.org>
This commit is contained in:
Zebediah Figura 2021-02-13 11:30:08 -06:00 committed by Michael Stefaniuc
parent f7c5cb04ba
commit a0452ee0bc
3 changed files with 35 additions and 121 deletions

View File

@ -1,4 +1,5 @@
TESTDLL = imagehlp.dll
IMPORTS = imagehlp psapi
C_SRCS = \
image.c \

View File

@ -28,16 +28,6 @@
#include "wine/test.h"
static HMODULE hImageHlp;
static BOOL (WINAPI *pImageGetDigestStream)(HANDLE, DWORD, DIGEST_FUNCTION, DIGEST_HANDLE);
static BOOL (WINAPI *pBindImageEx)(DWORD Flags, const char *ImageName, const char *DllPath,
const char *SymbolPath, PIMAGEHLP_STATUS_ROUTINE StatusRoutine);
static DWORD (WINAPI *pGetImageUnusedHeaderBytes)(PLOADED_IMAGE, LPDWORD);
static PLOADED_IMAGE (WINAPI *pImageLoad)(PCSTR, PCSTR);
static BOOL (WINAPI *pImageUnload)(PLOADED_IMAGE);
/* minimal PE file image */
#define VA_START 0x400000
#define FILE_PE_START 0x50
@ -322,13 +312,8 @@ static void test_get_digest_stream(void)
DWORD count;
struct update_accum accum = { 0, NULL };
if (!pImageGetDigestStream)
{
win_skip("ImageGetDigestStream function is not available\n");
return;
}
SetLastError(0xdeadbeef);
ret = pImageGetDigestStream(NULL, 0, NULL, NULL);
ret = ImageGetDigestStream(NULL, 0, NULL, NULL);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
file = create_temp_file(temp_file);
@ -338,16 +323,16 @@ static void test_get_digest_stream(void)
return;
}
SetLastError(0xdeadbeef);
ret = pImageGetDigestStream(file, 0, NULL, NULL);
ret = ImageGetDigestStream(file, 0, NULL, NULL);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
SetLastError(0xdeadbeef);
ret = pImageGetDigestStream(NULL, 0, accumulating_stream_output, &accum);
ret = ImageGetDigestStream(NULL, 0, accumulating_stream_output, &accum);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
/* Even with "valid" parameters, it fails with an empty file */
SetLastError(0xdeadbeef);
ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
ret = ImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
/* Finally, with a valid executable in the file, it succeeds. Note that
@ -362,11 +347,11 @@ static void test_get_digest_stream(void)
bin.nt_headers.OptionalHeader.SizeOfInitializedData = 0;
bin.nt_headers.OptionalHeader.SizeOfImage = 0;
ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
ret = ImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
check_updates("flags = 0", &a1, &accum);
free_updates(&accum);
ret = pImageGetDigestStream(file, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO,
ret = ImageGetDigestStream(file, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO,
accumulating_stream_output, &accum);
ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2, &accum);
@ -382,15 +367,9 @@ static void test_bind_image_ex(void)
char temp_file[MAX_PATH];
DWORD count;
if (!pBindImageEx)
{
win_skip("BindImageEx function is not available\n");
return;
}
/* call with a non-existent file */
SetLastError(0xdeadbeef);
ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, "nonexistent.dll", 0, 0,
ret = BindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, "nonexistent.dll", 0, 0,
testing_status_routine);
todo_wine ok(!ret && ((GetLastError() == ERROR_FILE_NOT_FOUND) ||
(GetLastError() == ERROR_INVALID_PARAMETER)),
@ -408,12 +387,12 @@ static void test_bind_image_ex(void)
CloseHandle(file);
/* call with a proper PE file, but with StatusRoutine set to NULL */
ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
ret = BindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
NULL);
ok(ret, "BindImageEx failed: %d\n", GetLastError());
/* call with a proper PE file and StatusRoutine */
ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
ret = BindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
testing_status_routine);
ok(ret, "BindImageEx failed: %d\n", GetLastError());
@ -436,12 +415,6 @@ static void test_image_load(void)
DWORD ret, count;
HANDLE file;
if (!pImageLoad || !pImageUnload || !pGetImageUnusedHeaderBytes)
{
win_skip("ImageLoad functions are not available\n");
return;
}
file = create_temp_file(temp_file);
if (file == INVALID_HANDLE_VALUE)
{
@ -452,7 +425,7 @@ static void test_image_load(void)
WriteFile(file, &bin, sizeof(bin), &count, NULL);
CloseHandle(file);
img = pImageLoad(temp_file, NULL);
img = ImageLoad(temp_file, NULL);
ok(img != NULL, "ImageLoad unexpectedly failed\n");
if (img)
@ -499,13 +472,13 @@ static void test_image_load(void)
"unexpected SizeOfImage, got 0x%x instead of 0x600\n", img->SizeOfImage);
count = 0xdeadbeef;
ret = pGetImageUnusedHeaderBytes(img, &count);
ret = GetImageUnusedHeaderBytes(img, &count);
todo_wine
ok(ret == 448, "GetImageUnusedHeaderBytes returned %u instead of 448\n", ret);
todo_wine
ok(count == 64, "unexpected size for unused header bytes, got %u instead of 64\n", count);
pImageUnload(img);
ImageUnload(img);
}
DeleteFileA(temp_file);
@ -513,23 +486,7 @@ static void test_image_load(void)
START_TEST(image)
{
hImageHlp = LoadLibraryA("imagehlp.dll");
if (!hImageHlp)
{
win_skip("ImageHlp unavailable\n");
return;
}
pImageGetDigestStream = (void *) GetProcAddress(hImageHlp, "ImageGetDigestStream");
pBindImageEx = (void *) GetProcAddress(hImageHlp, "BindImageEx");
pGetImageUnusedHeaderBytes = (void *) GetProcAddress(hImageHlp, "GetImageUnusedHeaderBytes");
pImageLoad = (void *) GetProcAddress(hImageHlp, "ImageLoad");
pImageUnload = (void *) GetProcAddress(hImageHlp, "ImageUnload");
test_get_digest_stream();
test_bind_image_ex();
test_image_load();
FreeLibrary(hImageHlp);
}

View File

@ -26,20 +26,12 @@
#include "winerror.h"
#include "winnt.h"
#include "imagehlp.h"
#define PSAPI_VERSION 1
#include "psapi.h"
static HMODULE hImageHlp, hPsapi, hNtdll;
static HMODULE hNtdll;
static char test_dll_path[MAX_PATH];
static BOOL (WINAPI *pImageAddCertificate)(HANDLE, LPWIN_CERTIFICATE, PDWORD);
static BOOL (WINAPI *pImageEnumerateCertificates)(HANDLE, WORD, PDWORD, PDWORD, DWORD);
static BOOL (WINAPI *pImageGetCertificateData)(HANDLE, DWORD, LPWIN_CERTIFICATE, PDWORD);
static BOOL (WINAPI *pImageGetCertificateHeader)(HANDLE, DWORD, LPWIN_CERTIFICATE);
static BOOL (WINAPI *pImageRemoveCertificate)(HANDLE, DWORD);
static PIMAGE_NT_HEADERS (WINAPI *pCheckSumMappedFile)(PVOID, DWORD, PDWORD, PDWORD);
static BOOL (WINAPI *pGetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
static PIMAGE_NT_HEADERS (WINAPI *pRtlImageNtHeader)(PVOID);
static const char test_cert_data[] =
@ -193,7 +185,7 @@ static DWORD test_add_certificate(const char *cert_data, int len)
cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
CopyMemory(cert->bCertificate, cert_data, len);
ret = pImageAddCertificate(hFile, cert, &index);
ret = ImageAddCertificate(hFile, cert, &index);
ok(ret, "Unable to add certificate to image, error %x\n", GetLastError());
trace("added cert index %d\n", index);
@ -218,7 +210,7 @@ static void test_get_certificate(const char *cert_data, int index)
return;
}
ret = pImageGetCertificateData(hFile, index, NULL, &cert_len);
ret = ImageGetCertificateData(hFile, index, NULL, &cert_len);
err = GetLastError();
ok ((ret == FALSE) && (err == ERROR_INSUFFICIENT_BUFFER), "ImageGetCertificateData gave unexpected result; ret=%d / err=%x\n", ret, err);
@ -232,7 +224,7 @@ static void test_get_certificate(const char *cert_data, int index)
return;
}
ret = pImageGetCertificateData(hFile, index, cert, &cert_len);
ret = ImageGetCertificateData(hFile, index, cert, &cert_len);
ok(ret, "Unable to retrieve certificate; err=%x\n", GetLastError());
ok(memcmp(cert->bCertificate, cert_data, cert_len - sizeof(WIN_CERTIFICATE)) == 0, "Certificate retrieved did not match original\n");
@ -254,13 +246,13 @@ static void test_remove_certificate(int index)
return;
}
ret = pImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &orig_count, NULL, 0);
ret = ImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &orig_count, NULL, 0);
ok (ret, "Unable to enumerate certificates in file; err=%x\n", GetLastError());
ret = pImageRemoveCertificate(hFile, index);
ret = ImageRemoveCertificate(hFile, index);
ok (ret, "Unable to remove certificate from file; err=%x\n", GetLastError());
/* Test to see if the certificate has actually been removed */
pImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &count, NULL, 0);
ImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &count, NULL, 0);
ok (count == orig_count - 1, "Certificate count mismatch; orig=%d new=%d\n", orig_count, count);
CloseHandle(hFile);
@ -275,20 +267,14 @@ static void test_pe_checksum(void)
char buffer[20];
BOOL ret_bool;
if (!pCheckSumMappedFile)
{
win_skip("CheckSumMappedFile not supported, skipping tests\n");
return;
}
SetLastError(0xdeadbeef);
ret = pCheckSumMappedFile(NULL, 0, &checksum_orig, &checksum_new);
ret = CheckSumMappedFile(NULL, 0, &checksum_orig, &checksum_new);
ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
ok(((GetLastError() == ERROR_INVALID_PARAMETER)||(GetLastError() == 0xdeadbeef)),
"Expected 0xdeadbeef (XP) or ERROR_INVALID_PARAMETER (Vista+), got %x\n", GetLastError());
SetLastError(0xdeadbeef);
ret = pCheckSumMappedFile((void *)0xdeadbeef, 0, &checksum_orig, &checksum_new);
ret = CheckSumMappedFile((void *)0xdeadbeef, 0, &checksum_orig, &checksum_new);
ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
ok(((GetLastError() == ERROR_INVALID_PARAMETER)||(GetLastError() == 0xdeadbeef)),
"Expected 0xdeadbeef (XP) or ERROR_INVALID_PARAMETER (Vista+), got %x\n", GetLastError());
@ -296,53 +282,47 @@ static void test_pe_checksum(void)
/* basic checksum tests */
memset(buffer, 0x11, sizeof(buffer));
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
ret = CheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
ok(ret == NULL, "Expected NULL, got %p\n", ret);
todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
todo_wine ok(checksum_new == 0xaabe, "Expected 0xaabe, got %x\n", checksum_new);
memset(buffer, 0x22, sizeof(buffer));
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
ret = CheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
ok(ret == NULL, "Expected NULL, got %p\n", ret);
todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
todo_wine ok(checksum_new == 0x5569, "Expected 0x5569, got %x\n", checksum_new);
memset(buffer, 0x22, sizeof(buffer));
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(buffer, 10, &checksum_orig, &checksum_new);
ret = CheckSumMappedFile(buffer, 10, &checksum_orig, &checksum_new);
ok(ret == NULL, "Expected NULL, got %p\n", ret);
todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
todo_wine ok(checksum_new == 0xaab4, "Expected 0xaab4, got %x\n", checksum_new);
memset(buffer, 0x22, sizeof(buffer));
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(buffer, 11, &checksum_orig, &checksum_new);
ret = CheckSumMappedFile(buffer, 11, &checksum_orig, &checksum_new);
ok(ret == NULL, "Expected NULL, got %p\n", ret);
todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
todo_wine ok(checksum_new == 0xaad7, "Expected 0xaad7, got %x\n", checksum_new);
/* test checksum of PE module */
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(test_pe_executable, sizeof(test_pe_executable),
ret = CheckSumMappedFile(test_pe_executable, sizeof(test_pe_executable),
&checksum_orig, &checksum_new);
ok((char *)ret == test_pe_executable + 0x80, "Expected %p, got %p\n", test_pe_executable + 0x80, ret);
ok(checksum_orig == 0xabcdef11, "Expected 0xabcdef11, got %x\n", checksum_orig);
ok(checksum_new == 0xaa4, "Expected 0xaa4, got %x\n", checksum_new);
if (!pGetModuleInformation)
{
win_skip("GetModuleInformation not supported, skipping tests\n");
return;
}
ret_bool = pGetModuleInformation(GetCurrentProcess(), GetModuleHandleA(NULL),
ret_bool = GetModuleInformation(GetCurrentProcess(), GetModuleHandleA(NULL),
&modinfo, sizeof(modinfo));
ok(ret_bool, "GetModuleInformation failed, error: %x\n", GetLastError());
SetLastError(0xdeadbeef);
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, modinfo.SizeOfImage, &checksum_orig, &checksum_new);
ret = CheckSumMappedFile(modinfo.lpBaseOfDll, modinfo.SizeOfImage, &checksum_orig, &checksum_new);
ok(ret != NULL, "Expected CheckSumMappedFile to succeed\n");
ok(GetLastError() == 0xdeadbeef, "Expected err=0xdeadbeef, got %x\n", GetLastError());
ok(checksum_orig != 0xdeadbeef, "Expected orig checksum != 0xdeadbeef\n");
@ -350,7 +330,7 @@ static void test_pe_checksum(void)
SetLastError(0xdeadbeef);
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile((char *)modinfo.lpBaseOfDll + 100, modinfo.SizeOfImage - 100,
ret = CheckSumMappedFile((char *)modinfo.lpBaseOfDll + 100, modinfo.SizeOfImage - 100,
&checksum_orig, &checksum_new);
ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
ok(GetLastError() == 0xdeadbeef, "Expected err=0xdeadbeef, got %x\n", GetLastError());
@ -361,27 +341,27 @@ static void test_pe_checksum(void)
checksum_correct = nt_header->OptionalHeader.CheckSum;
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, (char *)nt_header - (char *)modinfo.lpBaseOfDll,
ret = CheckSumMappedFile(modinfo.lpBaseOfDll, (char *)nt_header - (char *)modinfo.lpBaseOfDll,
&checksum_orig, &checksum_new);
ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
ok((checksum_orig == 0) || (checksum_orig == checksum_correct), "Expected %x, got %x\n", checksum_correct, checksum_orig);
ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %x\n", checksum_new);
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, sizeof(IMAGE_DOS_HEADER),
ret = CheckSumMappedFile(modinfo.lpBaseOfDll, sizeof(IMAGE_DOS_HEADER),
&checksum_orig, &checksum_new);
ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
ok((checksum_orig == 0) || (checksum_orig == checksum_correct), "Expected %x, got %x\n", checksum_correct, checksum_orig);
ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %x\n", checksum_new);
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, 0, &checksum_orig, &checksum_new);
ret = CheckSumMappedFile(modinfo.lpBaseOfDll, 0, &checksum_orig, &checksum_new);
ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
ok((checksum_orig == 0xdeadbeef) || (checksum_orig == checksum_correct), "Expected %x, got %x\n", checksum_correct, checksum_orig);
ok((checksum_new == 0xdeadbeef) || (checksum_new != 0 && checksum_new != 0xdeadbeef), "Got unexpected value %x\n", checksum_new);
checksum_orig = checksum_new = 0xdeadbeef;
ret = pCheckSumMappedFile((char *)modinfo.lpBaseOfDll + 1, 0,
ret = CheckSumMappedFile((char *)modinfo.lpBaseOfDll + 1, 0,
&checksum_orig, &checksum_new);
ok(ret == NULL, "Expected NULL, got %p\n", ret);
ok((checksum_orig == 0) || (checksum_orig == 0xdeadbeef), "Expected 0, got %x\n", checksum_orig);
@ -392,33 +372,11 @@ START_TEST(integrity)
{
DWORD file_size, file_size_orig, first, second;
hImageHlp = LoadLibraryA("imagehlp.dll");
if (!hImageHlp)
{
win_skip("ImageHlp unavailable\n");
return;
}
if (!copy_dll_file())
{
FreeLibrary(hImageHlp);
return;
}
file_size_orig = get_file_size();
pImageAddCertificate = (void *) GetProcAddress(hImageHlp, "ImageAddCertificate");
pImageEnumerateCertificates = (void *) GetProcAddress(hImageHlp, "ImageEnumerateCertificates");
pImageGetCertificateData = (void *) GetProcAddress(hImageHlp, "ImageGetCertificateData");
pImageGetCertificateHeader = (void *) GetProcAddress(hImageHlp, "ImageGetCertificateHeader");
pImageRemoveCertificate = (void *) GetProcAddress(hImageHlp, "ImageRemoveCertificate");
pCheckSumMappedFile = (void *) GetProcAddress(hImageHlp, "CheckSumMappedFile");
hPsapi = LoadLibraryA("psapi.dll");
if (hPsapi)
pGetModuleInformation = (void *) GetProcAddress(hPsapi, "GetModuleInformation");
hNtdll = LoadLibraryA("ntdll.dll");
if (hNtdll)
pRtlImageNtHeader = (void *) GetProcAddress(hNtdll, "RtlImageNtHeader");
@ -450,8 +408,6 @@ START_TEST(integrity)
test_pe_checksum();
if (hPsapi) FreeLibrary(hPsapi);
if (hNtdll) FreeLibrary(hNtdll);
FreeLibrary(hImageHlp);
DeleteFileA(test_dll_path);
}