1 /*
2  * Test suite for imagehlp integrity functions
3  *
4  * Copyright 2009 Owen Rudge for CodeWeavers
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 #define COBJMACROS
22 #include "wine/test.h"
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winerror.h"
27 #include "winnt.h"
28 #include "imagehlp.h"
29 #include "psapi.h"
30 
31 static HMODULE hImageHlp, hPsapi, hNtdll;
32 static char test_dll_path[MAX_PATH];
33 
34 static BOOL (WINAPI *pImageAddCertificate)(HANDLE, LPWIN_CERTIFICATE, PDWORD);
35 static BOOL (WINAPI *pImageEnumerateCertificates)(HANDLE, WORD, PDWORD, PDWORD, DWORD);
36 static BOOL (WINAPI *pImageGetCertificateData)(HANDLE, DWORD, LPWIN_CERTIFICATE, PDWORD);
37 static BOOL (WINAPI *pImageGetCertificateHeader)(HANDLE, DWORD, LPWIN_CERTIFICATE);
38 static BOOL (WINAPI *pImageRemoveCertificate)(HANDLE, DWORD);
39 static PIMAGE_NT_HEADERS (WINAPI *pCheckSumMappedFile)(PVOID, DWORD, PDWORD, PDWORD);
40 
41 static BOOL (WINAPI *pGetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
42 
43 static PIMAGE_NT_HEADERS (WINAPI *pRtlImageNtHeader)(PVOID);
44 
45 static const char test_cert_data[] =
46 {0x30,0x82,0x02,0xE1,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x07,0x02
47 ,0xA0,0x82,0x02,0xD2,0x30,0x82,0x02,0xCE,0x02,0x01,0x01,0x31,0x00,0x30,0x0B
48 ,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x07,0x01,0xA0,0x82,0x02,0xB4
49 ,0x30,0x82,0x02,0xB0,0x30,0x82,0x02,0x19,0xA0,0x03,0x02,0x01,0x02,0x02,0x09
50 ,0x00,0xE2,0x59,0x17,0xA5,0x87,0x0F,0x88,0x89,0x30,0x0D,0x06,0x09,0x2A,0x86
51 ,0x48,0x86,0xF7,0x0D,0x01,0x01,0x05,0x05,0x00,0x30,0x45,0x31,0x0B,0x30,0x09
52 ,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x41,0x55,0x31,0x13,0x30,0x11,0x06,0x03
53 ,0x55,0x04,0x08,0x13,0x0A,0x53,0x6F,0x6D,0x65,0x2D,0x53,0x74,0x61,0x74,0x65
54 ,0x31,0x21,0x30,0x1F,0x06,0x03,0x55,0x04,0x0A,0x13,0x18,0x49,0x6E,0x74,0x65
55 ,0x72,0x6E,0x65,0x74,0x20,0x57,0x69,0x64,0x67,0x69,0x74,0x73,0x20,0x50,0x74
56 ,0x79,0x20,0x4C,0x74,0x64,0x30,0x1E,0x17,0x0D,0x30,0x39,0x31,0x31,0x32,0x30
57 ,0x31,0x37,0x33,0x38,0x31,0x32,0x5A,0x17,0x0D,0x31,0x30,0x31,0x31,0x32,0x30
58 ,0x31,0x37,0x33,0x38,0x31,0x32,0x5A,0x30,0x45,0x31,0x0B,0x30,0x09,0x06,0x03
59 ,0x55,0x04,0x06,0x13,0x02,0x41,0x55,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04
60 ,0x08,0x13,0x0A,0x53,0x6F,0x6D,0x65,0x2D,0x53,0x74,0x61,0x74,0x65,0x31,0x21
61 ,0x30,0x1F,0x06,0x03,0x55,0x04,0x0A,0x13,0x18,0x49,0x6E,0x74,0x65,0x72,0x6E
62 ,0x65,0x74,0x20,0x57,0x69,0x64,0x67,0x69,0x74,0x73,0x20,0x50,0x74,0x79,0x20
63 ,0x4C,0x74,0x64,0x30,0x81,0x9F,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7
64 ,0x0D,0x01,0x01,0x01,0x05,0x00,0x03,0x81,0x8D,0x00,0x30,0x81,0x89,0x02,0x81
65 ,0x81,0x00,0x9B,0xC1,0x5E,0x28,0x70,0x32,0x81,0xEF,0x41,0x5C,0xCA,0x29,0x4A
66 ,0xB0,0x12,0xF7,0xAE,0x1E,0x30,0x93,0x14,0x3E,0x54,0x7C,0xC3,0x60,0x8C,0xB2
67 ,0x2F,0xC4,0x1F,0x20,0xEE,0x76,0xAC,0x83,0xD9,0xD4,0xC0,0x3C,0x78,0x6B,0xAA
68 ,0xA2,0x35,0x08,0x72,0x4A,0x5F,0xAE,0xD6,0x7D,0x5A,0xD8,0x27,0xEC,0xE0,0x24
69 ,0xBE,0xBE,0x62,0x86,0xF9,0x83,0x66,0x20,0xBC,0xF6,0x4B,0xC8,0x2D,0x1B,0x4C
70 ,0x5C,0xFA,0x0C,0x42,0x9F,0x57,0x49,0xDC,0xB9,0xC7,0x88,0x53,0xFA,0x26,0x21
71 ,0xC3,0xAB,0x4D,0x93,0x83,0x48,0x88,0xF1,0x14,0xB8,0x64,0x03,0x46,0x58,0x35
72 ,0xAC,0xD2,0xD2,0x9C,0xD4,0x6F,0xA4,0xE4,0x88,0x83,0x1C,0xD8,0x98,0xEE,0x2C
73 ,0xA3,0xEC,0x0C,0x4B,0xFB,0x1D,0x6E,0xBE,0xD9,0x77,0x02,0x03,0x01,0x00,0x01
74 ,0xA3,0x81,0xA7,0x30,0x81,0xA4,0x30,0x1D,0x06,0x03,0x55,0x1D,0x0E,0x04,0x16
75 ,0x04,0x14,0x3F,0xB3,0xC8,0x15,0x12,0xC7,0xD8,0xC0,0x13,0x3D,0xBE,0xF1,0x2F
76 ,0x5A,0xB3,0x51,0x59,0x79,0x89,0xF8,0x30,0x75,0x06,0x03,0x55,0x1D,0x23,0x04
77 ,0x6E,0x30,0x6C,0x80,0x14,0x3F,0xB3,0xC8,0x15,0x12,0xC7,0xD8,0xC0,0x13,0x3D
78 ,0xBE,0xF1,0x2F,0x5A,0xB3,0x51,0x59,0x79,0x89,0xF8,0xA1,0x49,0xA4,0x47,0x30
79 ,0x45,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x41,0x55,0x31
80 ,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x08,0x13,0x0A,0x53,0x6F,0x6D,0x65,0x2D
81 ,0x53,0x74,0x61,0x74,0x65,0x31,0x21,0x30,0x1F,0x06,0x03,0x55,0x04,0x0A,0x13
82 ,0x18,0x49,0x6E,0x74,0x65,0x72,0x6E,0x65,0x74,0x20,0x57,0x69,0x64,0x67,0x69
83 ,0x74,0x73,0x20,0x50,0x74,0x79,0x20,0x4C,0x74,0x64,0x82,0x09,0x00,0xE2,0x59
84 ,0x17,0xA5,0x87,0x0F,0x88,0x89,0x30,0x0C,0x06,0x03,0x55,0x1D,0x13,0x04,0x05
85 ,0x30,0x03,0x01,0x01,0xFF,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D
86 ,0x01,0x01,0x05,0x05,0x00,0x03,0x81,0x81,0x00,0x52,0x09,0xA5,0x81,0x63,0xEF
87 ,0xF7,0x76,0x65,0x2B,0xA5,0x48,0xC1,0xC5,0xE0,0x73,0x60,0x9B,0x66,0x2E,0x21
88 ,0xCF,0xF2,0xBD,0xFF,0x81,0xC4,0x99,0x39,0xD0,0x5D,0x1B,0x12,0xFD,0xAE,0x30
89 ,0x5D,0x9C,0x1A,0xD4,0x76,0x8A,0x25,0x10,0x0A,0x7E,0x5D,0x78,0xB5,0x94,0xD8
90 ,0x97,0xBD,0x9A,0x5A,0xD6,0x23,0xCA,0x5C,0x46,0x8C,0xC7,0x30,0x45,0xB4,0x77
91 ,0x44,0x6F,0x16,0xDD,0xC6,0x58,0xFE,0x16,0x15,0xAD,0xB8,0x58,0x49,0x9A,0xFE
92 ,0x6B,0x87,0x78,0xEE,0x13,0xFF,0x29,0x26,0x8E,0x13,0x83,0x0D,0x18,0xCA,0x9F
93 ,0xA9,0x3E,0x6E,0x3C,0xA6,0x50,0x4A,0x04,0x71,0x9F,0x2E,0xCF,0x25,0xA6,0x03
94 ,0x46,0xCA,0xEB,0xEA,0x67,0x89,0x49,0x7C,0x43,0xA2,0x52,0xD9,0x41,0xCC,0x65
95 ,0xED,0x2D,0xA1,0x00,0x31,0x00};
96 
97 static const char test_cert_data_2[] = {0xDE,0xAD,0xBE,0xEF,0x01,0x02,0x03};
98 
99 static char test_pe_executable[] =
100 {
101     0x4d,0x5a,0x90,0x00,0x03,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xff,0xff,0x00,
102     0x00,0xb8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,
103     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
104     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
105     0x80,0x00,0x00,0x00,0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,
106     0x4c,0xcd,0x21,0x54,0x68,0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,
107     0x20,0x63,0x61,0x6e,0x6e,0x6f,0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,
108     0x69,0x6e,0x20,0x44,0x4f,0x53,0x20,0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,
109     0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x45,0x00,0x00,0x4c,0x01,0x0f,
110     0x00,0xfd,0x38,0xc9,0x55,0x00,0x24,0x01,0x00,0xea,0x04,0x00,0x00,0xe0,0x00,
111     0x07,0x01,0x0b,0x01,0x02,0x18,0x00,0x1a,0x00,0x00,0x00,0x2c,0x00,0x00,0x00,
112     0x06,0x00,0x00,0xe0,0x14,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x30,0x00,0x00,
113     0x00,0x00,0x40,0x00,0x00,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x04,0x00,0x00,
114     0x00,0x01,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,
115     0x01,0x00,0x00,0x04,0x00,0x00,/* checksum */ 0x11,0xEF,0xCD,0xAB,0x03,0x00,
116     0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,
117     0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00
118 };
119 
120 static BOOL copy_dll_file(void)
121 {
122     char sys_dir[MAX_PATH+15];
123     char temp_path[MAX_PATH];
124 
125     if (GetSystemDirectoryA(sys_dir, MAX_PATH) == 0)
126     {
127         skip("Failed to get system directory. Skipping certificate/PE image tests.\n");
128         return FALSE;
129     }
130 
131     if (sys_dir[lstrlenA(sys_dir) - 1] != '\\')
132         lstrcatA(sys_dir, "\\");
133 
134     lstrcatA(sys_dir, "imagehlp.dll");
135 
136     /* Copy DLL to a temp file */
137     GetTempPathA(MAX_PATH, temp_path);
138     GetTempFileNameA(temp_path, "img", 0, test_dll_path);
139 
140     if (CopyFileA(sys_dir, test_dll_path, FALSE) == 0)
141     {
142         skip("Unable to create copy of imagehlp.dll for tests.\n");
143         return FALSE;
144     }
145 
146     return TRUE;
147 }
148 
149 static DWORD get_file_size(void)
150 {
151     HANDLE file;
152     DWORD filesize = 0;
153 
154     file = CreateFileA(test_dll_path, GENERIC_READ, FILE_SHARE_READ, NULL,
155                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
156     if (file == INVALID_HANDLE_VALUE)
157         return 0;
158 
159     filesize = GetFileSize(file, NULL);
160     CloseHandle(file);
161 
162     return filesize;
163 }
164 
165 static DWORD test_add_certificate(const char *cert_data, int len)
166 {
167     HANDLE hFile;
168     LPWIN_CERTIFICATE cert;
169     DWORD cert_len;
170     DWORD index;
171     BOOL ret;
172 
173     hFile = CreateFileA(test_dll_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
174 
175     if (hFile == INVALID_HANDLE_VALUE)
176     {
177         skip("Unable to open %s, skipping test\n", test_dll_path);
178         return ~0;
179     }
180 
181     cert_len = sizeof(WIN_CERTIFICATE) + len;
182     cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cert_len);
183 
184     if (!cert)
185     {
186         skip("Unable to allocate memory, skipping test\n");
187         CloseHandle(hFile);
188         return ~0;
189     }
190 
191     cert->dwLength = cert_len;
192     cert->wRevision = WIN_CERT_REVISION_1_0;
193     cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
194     CopyMemory(cert->bCertificate, cert_data, len);
195 
196     ret = pImageAddCertificate(hFile, cert, &index);
197     ok(ret, "Unable to add certificate to image, error %x\n", GetLastError());
198     trace("added cert index %d\n", index);
199 
200     HeapFree(GetProcessHeap(), 0, cert);
201     CloseHandle(hFile);
202     return index;
203 }
204 
205 static void test_get_certificate(const char *cert_data, int index)
206 {
207     HANDLE hFile;
208     LPWIN_CERTIFICATE cert;
209     DWORD cert_len = 0;
210     DWORD err;
211     BOOL ret;
212 
213     hFile = CreateFileA(test_dll_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
214 
215     if (hFile == INVALID_HANDLE_VALUE)
216     {
217         skip("Unable to open %s, skipping test\n", test_dll_path);
218         return;
219     }
220 
221     ret = pImageGetCertificateData(hFile, index, NULL, &cert_len);
222     err = GetLastError();
223 
224     ok ((ret == FALSE) && (err == ERROR_INSUFFICIENT_BUFFER), "ImageGetCertificateData gave unexpected result; ret=%d / err=%x\n", ret, err);
225 
226     cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cert_len);
227 
228     if (!cert)
229     {
230         skip("Unable to allocate memory, skipping test\n");
231         CloseHandle(hFile);
232         return;
233     }
234 
235     ret = pImageGetCertificateData(hFile, index, cert, &cert_len);
236     ok(ret, "Unable to retrieve certificate; err=%x\n", GetLastError());
237     ok(memcmp(cert->bCertificate, cert_data, cert_len - sizeof(WIN_CERTIFICATE)) == 0, "Certificate retrieved did not match original\n");
238 
239     HeapFree(GetProcessHeap(), 0, cert);
240     CloseHandle(hFile);
241 }
242 
243 static void test_remove_certificate(int index)
244 {
245     DWORD orig_count = 0, count = 0;
246     HANDLE hFile;
247     BOOL ret;
248 
249     hFile = CreateFileA(test_dll_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
250 
251     if (hFile == INVALID_HANDLE_VALUE)
252     {
253         skip("Unable to open %s, skipping test\n", test_dll_path);
254         return;
255     }
256 
257     ret = pImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &orig_count, NULL, 0);
258     ok (ret, "Unable to enumerate certificates in file; err=%x\n", GetLastError());
259     ret = pImageRemoveCertificate(hFile, index);
260     ok (ret, "Unable to remove certificate from file; err=%x\n", GetLastError());
261 
262     /* Test to see if the certificate has actually been removed */
263     pImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &count, NULL, 0);
264     ok (count == orig_count - 1, "Certificate count mismatch; orig=%d new=%d\n", orig_count, count);
265 
266     CloseHandle(hFile);
267 }
268 
269 static void test_pe_checksum(void)
270 {
271     DWORD checksum_orig, checksum_new, checksum_correct;
272     PIMAGE_NT_HEADERS nt_header;
273     PIMAGE_NT_HEADERS ret;
274     MODULEINFO modinfo;
275     char buffer[20];
276     BOOL ret_bool;
277 
278     if (!pCheckSumMappedFile)
279     {
280         win_skip("CheckSumMappedFile not supported, skipping tests\n");
281         return;
282     }
283 
284     SetLastError(0xdeadbeef);
285     ret = pCheckSumMappedFile(NULL, 0, &checksum_orig, &checksum_new);
286     ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
287     ok(((GetLastError() == ERROR_INVALID_PARAMETER)||(GetLastError() == 0xdeadbeef)),
288        "Expected 0xdeadbeef (XP) or ERROR_INVALID_PARAMETER (Vista+), got %x\n", GetLastError());
289 
290     SetLastError(0xdeadbeef);
291     ret = pCheckSumMappedFile((void *)0xdeadbeef, 0, &checksum_orig, &checksum_new);
292     ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
293     ok(((GetLastError() == ERROR_INVALID_PARAMETER)||(GetLastError() == 0xdeadbeef)),
294        "Expected 0xdeadbeef (XP) or ERROR_INVALID_PARAMETER (Vista+), got %x\n", GetLastError());
295 
296     /* basic checksum tests */
297     memset(buffer, 0x11, sizeof(buffer));
298     checksum_orig = checksum_new = 0xdeadbeef;
299     ret = pCheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
300     ok(ret == NULL, "Expected NULL, got %p\n", ret);
301     todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
302     todo_wine ok(checksum_new == 0xaabe, "Expected 0xaabe, got %x\n", checksum_new);
303 
304     memset(buffer, 0x22, sizeof(buffer));
305     checksum_orig = checksum_new = 0xdeadbeef;
306     ret = pCheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
307     ok(ret == NULL, "Expected NULL, got %p\n", ret);
308     todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
309     todo_wine ok(checksum_new == 0x5569, "Expected 0x5569, got %x\n", checksum_new);
310 
311     memset(buffer, 0x22, sizeof(buffer));
312     checksum_orig = checksum_new = 0xdeadbeef;
313     ret = pCheckSumMappedFile(buffer, 10, &checksum_orig, &checksum_new);
314     ok(ret == NULL, "Expected NULL, got %p\n", ret);
315     todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
316     todo_wine ok(checksum_new == 0xaab4, "Expected 0xaab4, got %x\n", checksum_new);
317 
318     memset(buffer, 0x22, sizeof(buffer));
319     checksum_orig = checksum_new = 0xdeadbeef;
320     ret = pCheckSumMappedFile(buffer, 11, &checksum_orig, &checksum_new);
321     ok(ret == NULL, "Expected NULL, got %p\n", ret);
322     todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
323     todo_wine ok(checksum_new == 0xaad7, "Expected 0xaad7, got %x\n", checksum_new);
324 
325     /* test checksum of PE module */
326     checksum_orig = checksum_new = 0xdeadbeef;
327     ret = pCheckSumMappedFile(test_pe_executable, sizeof(test_pe_executable),
328                               &checksum_orig, &checksum_new);
329     ok((char *)ret == test_pe_executable + 0x80, "Expected %p, got %p\n", test_pe_executable + 0x80, ret);
330     ok(checksum_orig == 0xabcdef11, "Expected 0xabcdef11, got %x\n", checksum_orig);
331     ok(checksum_new == 0xaa4, "Expected 0xaa4, got %x\n", checksum_new);
332 
333     if (!pGetModuleInformation)
334     {
335         win_skip("GetModuleInformation not supported, skipping tests\n");
336         return;
337     }
338 
339     ret_bool = pGetModuleInformation(GetCurrentProcess(), GetModuleHandleA(NULL),
340                                      &modinfo, sizeof(modinfo));
341     ok(ret_bool, "GetModuleInformation failed, error: %x\n", GetLastError());
342 
343     SetLastError(0xdeadbeef);
344     checksum_orig = checksum_new = 0xdeadbeef;
345     ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, modinfo.SizeOfImage, &checksum_orig, &checksum_new);
346     ok(ret != NULL, "Expected CheckSumMappedFile to succeed\n");
347     ok(GetLastError() == 0xdeadbeef, "Expected err=0xdeadbeef, got %x\n", GetLastError());
348     ok(checksum_orig != 0xdeadbeef, "Expected orig checksum != 0xdeadbeef\n");
349     ok(checksum_new != 0xdeadbeef, "Expected new checksum != 0xdeadbeef\n");
350 
351     SetLastError(0xdeadbeef);
352     checksum_orig = checksum_new = 0xdeadbeef;
353     ret = pCheckSumMappedFile((char *)modinfo.lpBaseOfDll + 100, modinfo.SizeOfImage - 100,
354                               &checksum_orig, &checksum_new);
355     ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
356     ok(GetLastError() == 0xdeadbeef, "Expected err=0xdeadbeef, got %x\n", GetLastError());
357     todo_wine ok(checksum_orig == 0, "Expected 0, got %x\n", checksum_orig);
358     todo_wine ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %x\n", checksum_new);
359 
360     nt_header = pRtlImageNtHeader( modinfo.lpBaseOfDll );
361     checksum_correct = nt_header->OptionalHeader.CheckSum;
362 
363     checksum_orig = checksum_new = 0xdeadbeef;
364     ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, (char *)nt_header - (char *)modinfo.lpBaseOfDll,
365                               &checksum_orig, &checksum_new);
366     ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
367     ok((checksum_orig == 0) || (checksum_orig == checksum_correct), "Expected %x, got %x\n", checksum_correct, checksum_orig);
368     ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %x\n", checksum_new);
369 
370     checksum_orig = checksum_new = 0xdeadbeef;
371     ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, sizeof(IMAGE_DOS_HEADER),
372                               &checksum_orig, &checksum_new);
373     ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
374     ok((checksum_orig == 0) || (checksum_orig == checksum_correct), "Expected %x, got %x\n", checksum_correct, checksum_orig);
375     ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %x\n", checksum_new);
376 
377     checksum_orig = checksum_new = 0xdeadbeef;
378     ret = pCheckSumMappedFile(modinfo.lpBaseOfDll, 0, &checksum_orig, &checksum_new);
379     ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
380     ok((checksum_orig == 0xdeadbeef) || (checksum_orig == checksum_correct), "Expected %x, got %x\n", checksum_correct, checksum_orig);
381     ok((checksum_new == 0xdeadbeef) || (checksum_new != 0 && checksum_new != 0xdeadbeef), "Got unexpected value %x\n", checksum_new);
382 
383     checksum_orig = checksum_new = 0xdeadbeef;
384     ret = pCheckSumMappedFile((char *)modinfo.lpBaseOfDll + 1, 0,
385                               &checksum_orig, &checksum_new);
386     ok(ret == NULL, "Expected NULL, got %p\n", ret);
387     ok((checksum_orig == 0) || (checksum_orig == 0xdeadbeef), "Expected 0, got %x\n", checksum_orig);
388     ok((checksum_new == 0) || (checksum_new == 0xdeadbeef), "Expected 0, got %x\n", checksum_new);
389 }
390 
391 START_TEST(integrity)
392 {
393     DWORD file_size, file_size_orig, first, second;
394 
395     hImageHlp = LoadLibraryA("imagehlp.dll");
396 
397     if (!hImageHlp)
398     {
399         win_skip("ImageHlp unavailable\n");
400         return;
401     }
402 
403     if (!copy_dll_file())
404     {
405         FreeLibrary(hImageHlp);
406         return;
407     }
408 
409     file_size_orig = get_file_size();
410 
411     pImageAddCertificate = (void *) GetProcAddress(hImageHlp, "ImageAddCertificate");
412     pImageEnumerateCertificates = (void *) GetProcAddress(hImageHlp, "ImageEnumerateCertificates");
413     pImageGetCertificateData = (void *) GetProcAddress(hImageHlp, "ImageGetCertificateData");
414     pImageGetCertificateHeader = (void *) GetProcAddress(hImageHlp, "ImageGetCertificateHeader");
415     pImageRemoveCertificate = (void *) GetProcAddress(hImageHlp, "ImageRemoveCertificate");
416     pCheckSumMappedFile = (void *) GetProcAddress(hImageHlp, "CheckSumMappedFile");
417 
418     hPsapi = LoadLibraryA("psapi.dll");
419     if (hPsapi)
420         pGetModuleInformation = (void *) GetProcAddress(hPsapi, "GetModuleInformation");
421 
422     hNtdll = LoadLibraryA("ntdll.dll");
423     if (hNtdll)
424         pRtlImageNtHeader = (void *) GetProcAddress(hNtdll, "RtlImageNtHeader");
425 
426     first = test_add_certificate(test_cert_data, sizeof(test_cert_data));
427     test_get_certificate(test_cert_data, first);
428     test_remove_certificate(first);
429 
430     file_size = get_file_size();
431     ok(file_size == file_size_orig, "File size different after add and remove (old: %d; new: %d)\n", file_size_orig, file_size);
432 
433     /* Try adding multiple certificates */
434     first = test_add_certificate(test_cert_data, sizeof(test_cert_data));
435     second = test_add_certificate(test_cert_data_2, sizeof(test_cert_data_2));
436     ok(second == first + 1, "got %d %d\n", first, second);
437 
438     test_get_certificate(test_cert_data, first);
439     test_get_certificate(test_cert_data_2, second);
440 
441     /* Remove the first one and verify the second certificate is intact */
442     test_remove_certificate(first);
443     second--;
444     test_get_certificate(test_cert_data_2, second);
445 
446     test_remove_certificate(second);
447 
448     file_size = get_file_size();
449     ok(file_size == file_size_orig, "File size different after add and remove (old: %d; new: %d)\n", file_size_orig, file_size);
450 
451     test_pe_checksum();
452 
453     if (hPsapi) FreeLibrary(hPsapi);
454     if (hNtdll) FreeLibrary(hNtdll);
455     FreeLibrary(hImageHlp);
456     DeleteFileA(test_dll_path);
457 }
458