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
22 #define NONAMELESSUNION
30 #include "wine/test.h"
32 static char *load_resource(const char *name
)
34 static char path
[MAX_PATH
];
40 GetTempPathA(ARRAY_SIZE(path
), path
);
43 file
= CreateFileA(path
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
44 ok(file
!= INVALID_HANDLE_VALUE
, "Failed to create file %s, error %lu.\n",
45 debugstr_a(path
), GetLastError());
47 res
= FindResourceA(NULL
, name
, "TESTDLL");
48 ok(!!res
, "Failed to load resource, error %lu.\n", GetLastError());
49 ptr
= LockResource(LoadResource(GetModuleHandleA(NULL
), res
));
50 WriteFile(file
, ptr
, SizeofResource( GetModuleHandleA(NULL
), res
), &written
, NULL
);
51 ok(written
== SizeofResource(GetModuleHandleA(NULL
), res
), "Failed to write resource.\n");
57 /* minimal PE file image */
58 #define VA_START 0x400000
59 #define FILE_PE_START 0x50
60 #define NUM_SECTIONS 3
61 #define FILE_TEXT 0x200
62 #define RVA_TEXT 0x1000
63 #define RVA_BSS 0x2000
64 #define FILE_IDATA 0x400
65 #define RVA_IDATA 0x3000
66 #define FILE_TOTAL 0x600
67 #define RVA_TOTAL 0x4000
70 IMAGE_IMPORT_DESCRIPTOR descriptors
[2];
71 IMAGE_THUNK_DATA32 original_thunks
[2];
72 IMAGE_THUNK_DATA32 thunks
[2];
73 struct __IMPORT_BY_NAME
{
79 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks))
81 static struct _PeImage
{
82 IMAGE_DOS_HEADER dos_header
;
83 char __alignment1
[FILE_PE_START
- sizeof(IMAGE_DOS_HEADER
)];
84 IMAGE_NT_HEADERS32 nt_headers
;
85 IMAGE_SECTION_HEADER sections
[NUM_SECTIONS
];
86 char __alignment2
[FILE_TEXT
- FILE_PE_START
- sizeof(IMAGE_NT_HEADERS32
) -
87 NUM_SECTIONS
* sizeof(IMAGE_SECTION_HEADER
)];
88 unsigned char text_section
[FILE_IDATA
-FILE_TEXT
];
89 struct Imports idata_section
;
90 char __alignment3
[FILE_TOTAL
-FILE_IDATA
-sizeof(struct Imports
)];
93 {IMAGE_DOS_SIGNATURE
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, 0, {0}, FILE_PE_START
},
94 /* alignment before PE header */
98 /* basic headers - 3 sections, no symbols, EXE file */
99 {IMAGE_FILE_MACHINE_I386
, NUM_SECTIONS
, 0, 0, 0, sizeof(IMAGE_OPTIONAL_HEADER32
),
100 IMAGE_FILE_32BIT_MACHINE
| IMAGE_FILE_EXECUTABLE_IMAGE
},
101 /* optional header */
102 {IMAGE_NT_OPTIONAL_HDR32_MAGIC
, 4, 0, FILE_IDATA
-FILE_TEXT
,
103 FILE_TOTAL
-FILE_IDATA
+ FILE_IDATA
-FILE_TEXT
, 0x400,
104 RVA_TEXT
, RVA_TEXT
, RVA_BSS
, VA_START
, 0x1000, 0x200, 4, 0, 1, 0, 4, 0, 0,
105 RVA_TOTAL
, FILE_TEXT
, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
, 0,
106 0x200000, 0x1000, 0x100000, 0x1000, 0, 0x10,
108 {RVA_IDATA
, sizeof(struct Imports
)}
114 {".text", {0x100}, RVA_TEXT
, FILE_IDATA
-FILE_TEXT
, FILE_TEXT
,
115 0, 0, 0, 0, IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
},
116 {".bss", {0x400}, RVA_BSS
, 0, 0, 0, 0, 0, 0,
117 IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
},
118 {".idata", {sizeof(struct Imports
)}, RVA_IDATA
, FILE_TOTAL
-FILE_IDATA
, FILE_IDATA
, 0,
119 0, 0, 0, IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
}
121 /* alignment before first section */
125 0x31, 0xC0, /* xor eax, eax */
126 0xFF, 0x25, EXIT_PROCESS
&0xFF, (EXIT_PROCESS
>>8)&0xFF, (EXIT_PROCESS
>>16)&0xFF,
127 (EXIT_PROCESS
>>24)&0xFF, /* jmp ExitProcess */
133 {{RVA_IDATA
+ FIELD_OFFSET(struct Imports
, original_thunks
)}, 0, 0,
134 RVA_IDATA
+ FIELD_OFFSET(struct Imports
, dllname
),
135 RVA_IDATA
+ FIELD_OFFSET(struct Imports
, thunks
)
139 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
140 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
144 /* final alignment */
164 struct blob
*updates
;
167 struct expected_update_accum
170 const struct expected_blob
*updates
;
174 static BOOL WINAPI
accumulating_stream_output(DIGEST_HANDLE handle
, BYTE
*pb
,
177 struct update_accum
*accum
= (struct update_accum
*)handle
;
181 accum
->updates
= HeapReAlloc(GetProcessHeap(), 0, accum
->updates
,
182 (accum
->cUpdates
+ 1) * sizeof(struct blob
));
184 accum
->updates
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob
));
187 struct blob
*blob
= &accum
->updates
[accum
->cUpdates
];
189 blob
->pb
= HeapAlloc(GetProcessHeap(), 0, cb
);
192 memcpy(blob
->pb
, pb
, cb
);
201 static void check_updates(LPCSTR header
, const struct expected_update_accum
*expected
,
202 const struct update_accum
*got
)
206 todo_wine_if (expected
->todo
)
207 ok(expected
->cUpdates
== got
->cUpdates
, "%s: expected %ld updates, got %ld\n",
208 header
, expected
->cUpdates
, got
->cUpdates
);
209 for (i
= 0; i
< min(expected
->cUpdates
, got
->cUpdates
); i
++)
211 ok(expected
->updates
[i
].cb
== got
->updates
[i
].cb
, "%s, update %ld: expected %ld bytes, got %ld\n",
212 header
, i
, expected
->updates
[i
].cb
, got
->updates
[i
].cb
);
213 if (expected
->updates
[i
].cb
&& expected
->updates
[i
].cb
== got
->updates
[i
].cb
)
214 ok(!memcmp(expected
->updates
[i
].pb
, got
->updates
[i
].pb
, got
->updates
[i
].cb
),
215 "%s, update %ld: unexpected value\n", header
, i
);
219 /* Frees the updates stored in accum */
220 static void free_updates(struct update_accum
*accum
)
224 for (i
= 0; i
< accum
->cUpdates
; i
++)
225 HeapFree(GetProcessHeap(), 0, accum
->updates
[i
].pb
);
226 HeapFree(GetProcessHeap(), 0, accum
->updates
);
227 accum
->updates
= NULL
;
231 static const struct expected_blob b1
[] = {
232 {FILE_PE_START
, &bin
},
233 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
234 {sizeof(bin
.nt_headers
), &bin
.nt_headers
},
235 {sizeof(bin
.sections
), &bin
.sections
},
236 {FILE_IDATA
-FILE_TEXT
, &bin
.text_section
},
237 {sizeof(bin
.idata_section
.descriptors
[0].u
.OriginalFirstThunk
),
238 &bin
.idata_section
.descriptors
[0].u
.OriginalFirstThunk
},
239 {FIELD_OFFSET(struct Imports
, thunks
)-
240 (FIELD_OFFSET(struct Imports
, descriptors
)+FIELD_OFFSET(IMAGE_IMPORT_DESCRIPTOR
, Name
)),
241 &bin
.idata_section
.descriptors
[0].Name
},
242 {FILE_TOTAL
-FILE_IDATA
-FIELD_OFFSET(struct Imports
, ibn
),
243 &bin
.idata_section
.ibn
}
245 static const struct expected_update_accum a1
= { ARRAY_SIZE(b1
), b1
, TRUE
};
247 static const struct expected_blob b2
[] = {
248 {FILE_PE_START
, &bin
},
249 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
250 {sizeof(bin
.nt_headers
), &bin
.nt_headers
},
251 {sizeof(bin
.sections
), &bin
.sections
},
252 {FILE_IDATA
-FILE_TEXT
, &bin
.text_section
},
253 {FILE_TOTAL
-FILE_IDATA
, &bin
.idata_section
}
255 static const struct expected_update_accum a2
= { ARRAY_SIZE(b2
), b2
, FALSE
};
257 /* Creates a test file and returns a handle to it. The file's path is returned
258 * in temp_file, which must be at least MAX_PATH characters in length.
260 static HANDLE
create_temp_file(char *temp_file
)
262 HANDLE file
= INVALID_HANDLE_VALUE
;
263 char temp_path
[MAX_PATH
];
265 if (GetTempPathA(sizeof(temp_path
), temp_path
))
267 if (GetTempFileNameA(temp_path
, "img", 0, temp_file
))
268 file
= CreateFileA(temp_file
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
269 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
274 static void update_checksum(void)
280 bin
.nt_headers
.OptionalHeader
.CheckSum
= 0;
282 for(ptr
= (WORD
const *)&bin
, size
= (sizeof(bin
)+1)/sizeof(WORD
); size
> 0; ptr
++, size
--)
285 if (HIWORD(sum
) != 0)
287 sum
= LOWORD(sum
) + HIWORD(sum
);
290 sum
= (WORD
)(LOWORD(sum
) + HIWORD(sum
));
293 bin
.nt_headers
.OptionalHeader
.CheckSum
= sum
;
296 static void test_get_digest_stream(void)
300 char temp_file
[MAX_PATH
];
302 struct update_accum accum
= { 0, NULL
};
304 SetLastError(0xdeadbeef);
305 ret
= ImageGetDigestStream(NULL
, 0, NULL
, NULL
);
306 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
307 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
308 file
= create_temp_file(temp_file
);
309 if (file
== INVALID_HANDLE_VALUE
)
311 skip("couldn't create temp file\n");
314 SetLastError(0xdeadbeef);
315 ret
= ImageGetDigestStream(file
, 0, NULL
, NULL
);
316 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
317 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
318 SetLastError(0xdeadbeef);
319 ret
= ImageGetDigestStream(NULL
, 0, accumulating_stream_output
, &accum
);
320 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
321 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
322 /* Even with "valid" parameters, it fails with an empty file */
323 SetLastError(0xdeadbeef);
324 ret
= ImageGetDigestStream(file
, 0, accumulating_stream_output
, &accum
);
325 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
326 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
327 /* Finally, with a valid executable in the file, it succeeds. Note that
328 * the file pointer need not be positioned at the beginning.
331 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
332 FlushFileBuffers(file
);
334 /* zero out some fields ImageGetDigestStream would zero out */
335 bin
.nt_headers
.OptionalHeader
.CheckSum
= 0;
336 bin
.nt_headers
.OptionalHeader
.SizeOfInitializedData
= 0;
337 bin
.nt_headers
.OptionalHeader
.SizeOfImage
= 0;
339 ret
= ImageGetDigestStream(file
, 0, accumulating_stream_output
, &accum
);
340 ok(ret
, "ImageGetDigestStream failed: %ld\n", GetLastError());
341 check_updates("flags = 0", &a1
, &accum
);
342 free_updates(&accum
);
343 ret
= ImageGetDigestStream(file
, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO
,
344 accumulating_stream_output
, &accum
);
345 ok(ret
, "ImageGetDigestStream failed: %ld\n", GetLastError());
346 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2
, &accum
);
347 free_updates(&accum
);
349 DeleteFileA(temp_file
);
352 static unsigned int got_SysAllocString
, got_GetOpenFileNameA
, got_SHRegGetIntW
;
354 static BOOL WINAPI
bind_image_cb(IMAGEHLP_STATUS_REASON reason
, const char *file
,
355 const char *module
, ULONG_PTR va
, ULONG_PTR param
)
357 static char last_module
[MAX_PATH
];
359 if (winetest_debug
> 1)
360 trace("reason %u, file %s, module %s, va %#Ix, param %#Ix\n",
361 reason
, debugstr_a(file
), debugstr_a(module
), va
, param
);
363 if (reason
== BindImportModule
)
365 ok(!strchr(module
, '\\'), "got module name %s\n", debugstr_a(module
));
366 strcpy(last_module
, module
);
367 ok(!va
, "got VA %#Ix\n", va
);
368 ok(!param
, "got param %#Ix\n", param
);
370 else if (reason
== BindImportProcedure
)
372 char full_path
[MAX_PATH
];
375 todo_wine
ok(!!va
, "expected nonzero VA\n");
376 ret
= SearchPathA(NULL
, last_module
, ".dll", sizeof(full_path
), full_path
, NULL
);
377 ok(ret
, "got error %lu\n", GetLastError());
378 ok(!strcmp(module
, full_path
), "expected %s, got %s\n", debugstr_a(full_path
), debugstr_a(module
));
380 if (!strcmp((const char *)param
, "SysAllocString"))
382 ok(!strcmp(last_module
, "oleaut32.dll"), "got wrong module %s\n", debugstr_a(module
));
383 ++got_SysAllocString
;
385 else if (!strcmp((const char *)param
, "GetOpenFileNameA"))
387 ok(!strcmp(last_module
, "comdlg32.dll"), "got wrong module %s\n", debugstr_a(module
));
388 ++got_GetOpenFileNameA
;
390 else if (!strcmp((const char *)param
, "Ordinal117"))
392 ok(!strcmp(last_module
, "shlwapi.dll"), "got wrong module %s\n", debugstr_a(module
));
398 ok(0, "got unexpected reason %#x\n", reason
);
403 static void test_bind_image_ex(void)
405 const char *filename
= load_resource("testdll.dll");
408 SetLastError(0xdeadbeef);
409 ret
= BindImageEx(BIND_ALL_IMAGES
| BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
,
410 "nonexistent.dll", 0, 0, bind_image_cb
);
411 ok(!ret
, "expected failure\n");
412 ok(GetLastError() == ERROR_FILE_NOT_FOUND
|| GetLastError() == ERROR_INVALID_PARAMETER
,
413 "got error %lu\n", GetLastError());
415 ret
= BindImageEx(BIND_ALL_IMAGES
| BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
,
416 filename
, NULL
, NULL
, bind_image_cb
);
417 ok(ret
, "got error %lu\n", GetLastError());
418 ok(got_SysAllocString
== 1, "got %u imports of SysAllocString\n", got_SysAllocString
);
419 ok(got_GetOpenFileNameA
== 1, "got %u imports of GetOpenFileNameA\n", got_GetOpenFileNameA
);
420 todo_wine
ok(got_SHRegGetIntW
== 1, "got %u imports of SHRegGetIntW\n", got_SHRegGetIntW
);
422 ret
= DeleteFileA(filename
);
423 ok(ret
, "got error %lu\n", GetLastError());
426 static void test_image_load(void)
428 char temp_file
[MAX_PATH
];
433 file
= create_temp_file(temp_file
);
434 if (file
== INVALID_HANDLE_VALUE
)
436 skip("couldn't create temp file\n");
440 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
443 img
= ImageLoad(temp_file
, NULL
);
444 ok(img
!= NULL
, "ImageLoad unexpectedly failed\n");
448 ok(!strcmp(img
->ModuleName
, temp_file
),
449 "unexpected ModuleName, got %s instead of %s\n", img
->ModuleName
, temp_file
);
450 ok(img
->MappedAddress
!= NULL
, "MappedAddress != NULL\n");
451 if (img
->MappedAddress
)
453 ok(!memcmp(img
->MappedAddress
, &bin
.dos_header
, sizeof(bin
.dos_header
)),
454 "MappedAddress doesn't point to IMAGE_DOS_HEADER\n");
456 ok(img
->FileHeader
!= NULL
, "FileHeader != NULL\n");
459 ok(!memcmp(img
->FileHeader
, &bin
.nt_headers
, sizeof(bin
.nt_headers
)),
460 "FileHeader doesn't point to IMAGE_NT_HEADERS32\n");
462 ok(img
->NumberOfSections
== 3,
463 "unexpected NumberOfSections, got %ld instead of 3\n", img
->NumberOfSections
);
464 if (img
->NumberOfSections
>= 3)
466 ok(!strcmp((const char *)img
->Sections
[0].Name
, ".text"),
467 "unexpected name for section 0, expected .text, got %s\n",
468 (const char *)img
->Sections
[0].Name
);
469 ok(!strcmp((const char *)img
->Sections
[1].Name
, ".bss"),
470 "unexpected name for section 1, expected .bss, got %s\n",
471 (const char *)img
->Sections
[1].Name
);
472 ok(!strcmp((const char *)img
->Sections
[2].Name
, ".idata"),
473 "unexpected name for section 2, expected .idata, got %s\n",
474 (const char *)img
->Sections
[2].Name
);
476 ok(img
->Characteristics
== 0x102,
477 "unexpected Characteristics, got 0x%lx instead of 0x102\n", img
->Characteristics
);
478 ok(img
->fSystemImage
== 0,
479 "unexpected fSystemImage, got %d instead of 0\n", img
->fSystemImage
);
480 ok(img
->fDOSImage
== 0,
481 "unexpected fDOSImage, got %d instead of 0\n", img
->fDOSImage
);
482 todo_wine
ok(img
->fReadOnly
== 1 || broken(!img
->fReadOnly
) /* <= WinXP */,
483 "unexpected fReadOnly, got %d instead of 1\n", img
->fReadOnly
);
484 todo_wine
ok(img
->Version
== 1 || broken(!img
->Version
) /* <= WinXP */,
485 "unexpected Version, got %d instead of 1\n", img
->Version
);
486 ok(img
->SizeOfImage
== 0x600,
487 "unexpected SizeOfImage, got 0x%lx instead of 0x600\n", img
->SizeOfImage
);
490 ret
= GetImageUnusedHeaderBytes(img
, &count
);
492 ok(ret
== 448, "GetImageUnusedHeaderBytes returned %lu instead of 448\n", ret
);
494 ok(count
== 64, "unexpected size for unused header bytes, got %lu instead of 64\n", count
);
499 DeleteFileA(temp_file
);
504 test_get_digest_stream();
505 test_bind_image_ex();