imagehlp/tests: Add test for BindImageEx with NULL as StatusRoutine.
[wine.git] / dlls / imagehlp / tests / image.c
blob48443f5e91159706bcb586d57e8a1c513e494c9b
1 /*
2 * Copyright 2008 Juan Lang
3 * Copyright 2010 Andrey Turkin
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdio.h>
20 #include <stdarg.h>
22 #define NONAMELESSUNION
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winver.h>
26 #include <winnt.h>
27 #include <imagehlp.h>
29 #include "wine/test.h"
31 static HMODULE hImageHlp;
33 static BOOL (WINAPI *pImageGetDigestStream)(HANDLE, DWORD, DIGEST_FUNCTION, DIGEST_HANDLE);
34 static BOOL (WINAPI *pBindImageEx)(DWORD Flags, const char *ImageName, const char *DllPath,
35 const char *SymbolPath, PIMAGEHLP_STATUS_ROUTINE StatusRoutine);
37 /* minimal PE file image */
38 #define VA_START 0x400000
39 #define FILE_PE_START 0x50
40 #define NUM_SECTIONS 3
41 #define FILE_TEXT 0x200
42 #define RVA_TEXT 0x1000
43 #define RVA_BSS 0x2000
44 #define FILE_IDATA 0x400
45 #define RVA_IDATA 0x3000
46 #define FILE_TOTAL 0x600
47 #define RVA_TOTAL 0x4000
48 #include <pshpack1.h>
49 struct Imports {
50 IMAGE_IMPORT_DESCRIPTOR descriptors[2];
51 IMAGE_THUNK_DATA32 original_thunks[2];
52 IMAGE_THUNK_DATA32 thunks[2];
53 struct __IMPORT_BY_NAME {
54 WORD hint;
55 char funcname[0x20];
56 } ibn;
57 char dllname[0x10];
59 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks[0]))
61 static struct _PeImage {
62 IMAGE_DOS_HEADER dos_header;
63 char __alignment1[FILE_PE_START - sizeof(IMAGE_DOS_HEADER)];
64 IMAGE_NT_HEADERS32 nt_headers;
65 IMAGE_SECTION_HEADER sections[NUM_SECTIONS];
66 char __alignment2[FILE_TEXT - FILE_PE_START - sizeof(IMAGE_NT_HEADERS32) -
67 NUM_SECTIONS * sizeof(IMAGE_SECTION_HEADER)];
68 unsigned char text_section[FILE_IDATA-FILE_TEXT];
69 struct Imports idata_section;
70 char __alignment3[FILE_TOTAL-FILE_IDATA-sizeof(struct Imports)];
71 } bin = {
72 /* dos header */
73 {IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {}, 0, 0, {}, FILE_PE_START},
74 /* alignment before PE header */
75 {},
76 /* nt headers */
77 {IMAGE_NT_SIGNATURE,
78 /* basic headers - 3 sections, no symbols, EXE file */
79 {IMAGE_FILE_MACHINE_I386, NUM_SECTIONS, 0, 0, 0, sizeof(IMAGE_OPTIONAL_HEADER32),
80 IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_EXECUTABLE_IMAGE},
81 /* optional header */
82 {IMAGE_NT_OPTIONAL_HDR32_MAGIC, 4, 0, FILE_IDATA-FILE_TEXT,
83 FILE_TOTAL-FILE_IDATA + FILE_IDATA-FILE_TEXT, 0x400,
84 RVA_TEXT, RVA_TEXT, RVA_BSS, VA_START, 0x1000, 0x200, 4, 0, 1, 0, 4, 0, 0,
85 RVA_TOTAL, FILE_TEXT, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI, 0,
86 0x200000, 0x1000, 0x100000, 0x1000, 0, 0x10,
87 {{0, 0},
88 {RVA_IDATA, sizeof(struct Imports)}
92 /* sections */
94 {".text", {0x100}, RVA_TEXT, FILE_IDATA-FILE_TEXT, FILE_TEXT,
95 0, 0, 0, 0, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ},
96 {".bss", {0x400}, RVA_BSS, 0, 0, 0, 0, 0, 0,
97 IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE},
98 {".idata", {sizeof(struct Imports)}, RVA_IDATA, FILE_TOTAL-FILE_IDATA, FILE_IDATA, 0,
99 0, 0, 0, IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE}
101 /* alignment before first section */
103 /* .text section */
105 0x31, 0xC0, /* xor eax, eax */
106 0xFF, 0x25, EXIT_PROCESS&0xFF, (EXIT_PROCESS>>8)&0xFF, (EXIT_PROCESS>>16)&0xFF,
107 (EXIT_PROCESS>>24)&0xFF, /* jmp ExitProcess */
110 /* .idata section */
113 {{RVA_IDATA + FIELD_OFFSET(struct Imports, original_thunks)}, 0, 0,
114 RVA_IDATA + FIELD_OFFSET(struct Imports, dllname),
115 RVA_IDATA + FIELD_OFFSET(struct Imports, thunks)
117 {{0}, 0, 0, 0, 0}
119 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
120 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
121 {0,"ExitProcess"},
122 "KERNEL32.DLL"
124 /* final alignment */
127 #include <poppack.h>
129 struct blob
131 DWORD cb;
132 BYTE *pb;
135 struct expected_blob
137 DWORD cb;
138 const void *pb;
141 struct update_accum
143 DWORD cUpdates;
144 struct blob *updates;
147 struct expected_update_accum
149 DWORD cUpdates;
150 const struct expected_blob *updates;
151 BOOL todo;
154 static int status_routine_called[BindSymbolsNotUpdated+1];
157 static BOOL WINAPI accumulating_stream_output(DIGEST_HANDLE handle, BYTE *pb,
158 DWORD cb)
160 struct update_accum *accum = (struct update_accum *)handle;
161 BOOL ret = FALSE;
163 if (accum->cUpdates)
164 accum->updates = HeapReAlloc(GetProcessHeap(), 0, accum->updates,
165 (accum->cUpdates + 1) * sizeof(struct blob));
166 else
167 accum->updates = HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob));
168 if (accum->updates)
170 struct blob *blob = &accum->updates[accum->cUpdates];
172 blob->pb = HeapAlloc(GetProcessHeap(), 0, cb);
173 if (blob->pb)
175 memcpy(blob->pb, pb, cb);
176 blob->cb = cb;
177 ret = TRUE;
179 accum->cUpdates++;
181 return ret;
184 static void check_updates(LPCSTR header, const struct expected_update_accum *expected,
185 const struct update_accum *got)
187 DWORD i;
189 if (expected->todo)
190 todo_wine ok(expected->cUpdates == got->cUpdates, "%s: expected %d updates, got %d\n",
191 header, expected->cUpdates, got->cUpdates);
192 else
193 ok(expected->cUpdates == got->cUpdates, "%s: expected %d updates, got %d\n",
194 header, expected->cUpdates, got->cUpdates);
195 for (i = 0; i < min(expected->cUpdates, got->cUpdates); i++)
197 ok(expected->updates[i].cb == got->updates[i].cb, "%s, update %d: expected %d bytes, got %d\n",
198 header, i, expected->updates[i].cb, got->updates[i].cb);
199 if (expected->updates[i].cb && expected->updates[i].cb == got->updates[i].cb)
200 ok(!memcmp(expected->updates[i].pb, got->updates[i].pb, got->updates[i].cb),
201 "%s, update %d: unexpected value\n", header, i);
205 /* Frees the updates stored in accum */
206 static void free_updates(struct update_accum *accum)
208 DWORD i;
210 for (i = 0; i < accum->cUpdates; i++)
211 HeapFree(GetProcessHeap(), 0, accum->updates[i].pb);
212 HeapFree(GetProcessHeap(), 0, accum->updates);
213 accum->updates = NULL;
214 accum->cUpdates = 0;
217 static const struct expected_blob b1[] = {
218 {FILE_PE_START, &bin},
219 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
220 {sizeof(bin.nt_headers), &bin.nt_headers},
221 {sizeof(bin.sections), &bin.sections},
222 {FILE_IDATA-FILE_TEXT, &bin.text_section},
223 {sizeof(bin.idata_section.descriptors[0].u.OriginalFirstThunk),
224 &bin.idata_section.descriptors[0].u.OriginalFirstThunk},
225 {FIELD_OFFSET(struct Imports, thunks)-FIELD_OFFSET(struct Imports, descriptors[0].Name),
226 &bin.idata_section.descriptors[0].Name},
227 {FILE_TOTAL-FILE_IDATA-FIELD_OFFSET(struct Imports, ibn),
228 &bin.idata_section.ibn}
230 static const struct expected_update_accum a1 = { sizeof(b1) / sizeof(b1[0]), b1, TRUE };
232 static const struct expected_blob b2[] = {
233 {FILE_PE_START, &bin},
234 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
235 {sizeof(bin.nt_headers), &bin.nt_headers},
236 {sizeof(bin.sections), &bin.sections},
237 {FILE_IDATA-FILE_TEXT, &bin.text_section},
238 {FILE_TOTAL-FILE_IDATA, &bin.idata_section}
240 static const struct expected_update_accum a2 = { sizeof(b2) / sizeof(b2[0]), b2, FALSE };
242 /* Creates a test file and returns a handle to it. The file's path is returned
243 * in temp_file, which must be at least MAX_PATH characters in length.
245 static HANDLE create_temp_file(char *temp_file)
247 HANDLE file = INVALID_HANDLE_VALUE;
248 char temp_path[MAX_PATH];
250 if (GetTempPathA(sizeof(temp_path), temp_path))
252 if (GetTempFileNameA(temp_path, "img", 0, temp_file))
253 file = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
254 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
256 return file;
259 static void update_checksum(void)
261 WORD const * ptr;
262 DWORD size;
263 DWORD sum = 0;
265 bin.nt_headers.OptionalHeader.CheckSum = 0;
267 for(ptr = (WORD const *)&bin, size = (sizeof(bin)+1)/sizeof(WORD); size > 0; ptr++, size--)
269 sum += *ptr;
270 if (HIWORD(sum) != 0)
272 sum = LOWORD(sum) + HIWORD(sum);
275 sum = (WORD)(LOWORD(sum) + HIWORD(sum));
276 sum += sizeof(bin);
278 bin.nt_headers.OptionalHeader.CheckSum = sum;
281 static BOOL CALLBACK testing_status_routine(IMAGEHLP_STATUS_REASON reason, const char *ImageName,
282 const char *DllName, ULONG_PTR Va, ULONG_PTR Parameter)
284 char kernel32_path[MAX_PATH];
286 if (0 <= (int)reason && reason <= BindSymbolsNotUpdated)
287 status_routine_called[reason]++;
288 else
289 ok(0, "expected reason between 0 and %d, got %d\n", BindSymbolsNotUpdated+1, reason);
291 switch(reason)
293 case BindImportModule:
294 ok(!strcmp(DllName, "KERNEL32.DLL"), "expected DllName to be KERNEL32.DLL, got %s\n",
295 DllName);
296 break;
298 case BindImportProcedure:
299 case BindForwarderNOT:
300 GetSystemDirectoryA(kernel32_path, MAX_PATH);
301 strcat(kernel32_path, "\\KERNEL32.DLL");
302 ok(!lstrcmpiA(DllName, kernel32_path), "expected DllName to be %s, got %s\n",
303 kernel32_path, DllName);
304 ok(!strcmp((char *)Parameter, "ExitProcess"),
305 "expected Parameter to be ExitProcess, got %s\n", (char *)Parameter);
306 break;
308 default:
309 ok(0, "got unexpected reason %d\n", reason);
310 break;
312 return TRUE;
315 static void test_get_digest_stream(void)
317 BOOL ret;
318 HANDLE file;
319 char temp_file[MAX_PATH];
320 DWORD count;
321 struct update_accum accum = { 0, NULL };
323 if (!pImageGetDigestStream)
325 win_skip("ImageGetDigestStream function is not available\n");
326 return;
328 SetLastError(0xdeadbeef);
329 ret = pImageGetDigestStream(NULL, 0, NULL, NULL);
330 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
331 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
332 file = create_temp_file(temp_file);
333 if (file == INVALID_HANDLE_VALUE)
335 skip("couldn't create temp file\n");
336 return;
338 SetLastError(0xdeadbeef);
339 ret = pImageGetDigestStream(file, 0, NULL, NULL);
340 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
341 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
342 SetLastError(0xdeadbeef);
343 ret = pImageGetDigestStream(NULL, 0, accumulating_stream_output, &accum);
344 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
345 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
346 /* Even with "valid" parameters, it fails with an empty file */
347 SetLastError(0xdeadbeef);
348 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
349 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
350 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
351 /* Finally, with a valid executable in the file, it succeeds. Note that
352 * the file pointer need not be positioned at the beginning.
354 update_checksum();
355 WriteFile(file, &bin, sizeof(bin), &count, NULL);
356 FlushFileBuffers(file);
358 /* zero out some fields ImageGetDigestStream would zero out */
359 bin.nt_headers.OptionalHeader.CheckSum = 0;
360 bin.nt_headers.OptionalHeader.SizeOfInitializedData = 0;
361 bin.nt_headers.OptionalHeader.SizeOfImage = 0;
363 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
364 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
365 check_updates("flags = 0", &a1, &accum);
366 free_updates(&accum);
367 ret = pImageGetDigestStream(file, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO,
368 accumulating_stream_output, &accum);
369 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
370 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2, &accum);
371 free_updates(&accum);
372 CloseHandle(file);
373 DeleteFileA(temp_file);
376 static void test_bind_image_ex(void)
378 BOOL ret;
379 HANDLE file;
380 char temp_file[MAX_PATH];
381 DWORD count;
383 if (!pBindImageEx)
385 win_skip("BindImageEx function is not available\n");
386 return;
389 /* call with a non-existent file */
390 SetLastError(0xdeadbeef);
391 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, "nonexistent.dll", 0, 0,
392 testing_status_routine);
393 todo_wine ok(!ret && ((GetLastError() == ERROR_FILE_NOT_FOUND) ||
394 (GetLastError() == ERROR_INVALID_PARAMETER)),
395 "expected ERROR_FILE_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n",
396 GetLastError());
398 file = create_temp_file(temp_file);
399 if (file == INVALID_HANDLE_VALUE)
401 skip("couldn't create temp file\n");
402 return;
405 WriteFile(file, &bin, sizeof(bin), &count, NULL);
406 CloseHandle(file);
408 /* call with a proper PE file, but with StatusRoutine set to NULL */
409 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
410 NULL);
411 ok(ret, "BindImageEx failed: %d\n", GetLastError());
413 /* call with a proper PE file and StatusRoutine */
414 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
415 testing_status_routine);
416 ok(ret, "BindImageEx failed: %d\n", GetLastError());
418 todo_wine ok(status_routine_called[BindImportModule] == 1,
419 "StatusRoutine was called %d times\n", status_routine_called[BindImportModule]);
421 todo_wine ok((status_routine_called[BindImportProcedure] == 1)
422 #if defined(_WIN64)
423 || broken(status_routine_called[BindImportProcedure] == 0) /* < Win8 */
424 #endif
425 , "StatusRoutine was called %d times\n", status_routine_called[BindImportProcedure]);
427 DeleteFileA(temp_file);
430 START_TEST(image)
432 hImageHlp = LoadLibraryA("imagehlp.dll");
434 if (!hImageHlp)
436 win_skip("ImageHlp unavailable\n");
437 return;
440 pImageGetDigestStream = (void *) GetProcAddress(hImageHlp, "ImageGetDigestStream");
441 pBindImageEx = (void *) GetProcAddress(hImageHlp, "BindImageEx");
443 test_get_digest_stream();
444 test_bind_image_ex();
446 FreeLibrary(hImageHlp);