e008b0b461887b2e85ce7b1c40cd07b03a88a402
[reactos.git] / rostests / winetests / imagehlp / image.c
1 /*
2 * Copyright 2008 Juan Lang
3 * Copyright 2010 Andrey Turkin
4 *
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.
9 *
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.
14 *
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
18 */
19 #include <stdio.h>
20 #include <stdarg.h>
21
22 #define NONAMELESSUNION
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winver.h>
26 #include <winnt.h>
27 #include <imagehlp.h>
28
29 #include "wine/test.h"
30
31 static HMODULE hImageHlp;
32
33 static BOOL (WINAPI *pImageGetDigestStream)(HANDLE, DWORD, DIGEST_FUNCTION, DIGEST_HANDLE);
34
35 /* minimal PE file image */
36 #define VA_START 0x400000
37 #define FILE_PE_START 0x50
38 #define NUM_SECTIONS 3
39 #define FILE_TEXT 0x200
40 #define RVA_TEXT 0x1000
41 #define RVA_BSS 0x2000
42 #define FILE_IDATA 0x400
43 #define RVA_IDATA 0x3000
44 #define FILE_TOTAL 0x600
45 #define RVA_TOTAL 0x4000
46 #include <pshpack1.h>
47 struct Imports {
48 IMAGE_IMPORT_DESCRIPTOR descriptors[2];
49 IMAGE_THUNK_DATA32 original_thunks[2];
50 IMAGE_THUNK_DATA32 thunks[2];
51 struct __IMPORT_BY_NAME {
52 WORD hint;
53 char funcname[0x20];
54 } ibn;
55 char dllname[0x10];
56 };
57 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks[0]))
58
59 static struct _PeImage {
60 IMAGE_DOS_HEADER dos_header;
61 char __alignment1[FILE_PE_START - sizeof(IMAGE_DOS_HEADER)];
62 IMAGE_NT_HEADERS32 nt_headers;
63 IMAGE_SECTION_HEADER sections[NUM_SECTIONS];
64 char __alignment2[FILE_TEXT - FILE_PE_START - sizeof(IMAGE_NT_HEADERS32) -
65 NUM_SECTIONS * sizeof(IMAGE_SECTION_HEADER)];
66 unsigned char text_section[FILE_IDATA-FILE_TEXT];
67 struct Imports idata_section;
68 char __alignment3[FILE_TOTAL-FILE_IDATA-sizeof(struct Imports)];
69 } bin = {
70 /* dos header */
71 {IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {}, 0, 0, {}, FILE_PE_START},
72 /* alignment before PE header */
73 {},
74 /* nt headers */
75 {IMAGE_NT_SIGNATURE,
76 /* basic headers - 3 sections, no symbols, EXE file */
77 {IMAGE_FILE_MACHINE_I386, NUM_SECTIONS, 0, 0, 0, sizeof(IMAGE_OPTIONAL_HEADER32),
78 IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_EXECUTABLE_IMAGE},
79 /* optional header */
80 {IMAGE_NT_OPTIONAL_HDR32_MAGIC, 4, 0, FILE_IDATA-FILE_TEXT,
81 FILE_TOTAL-FILE_IDATA + FILE_IDATA-FILE_TEXT, 0x400,
82 RVA_TEXT, RVA_TEXT, RVA_BSS, VA_START, 0x1000, 0x200, 4, 0, 1, 0, 4, 0, 0,
83 RVA_TOTAL, FILE_TEXT, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI, 0,
84 0x200000, 0x1000, 0x100000, 0x1000, 0, 0x10,
85 {{0, 0},
86 {RVA_IDATA, sizeof(struct Imports)}
87 }
88 }
89 },
90 /* sections */
91 {
92 {".text", {0x100}, RVA_TEXT, FILE_IDATA-FILE_TEXT, FILE_TEXT,
93 0, 0, 0, 0, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ},
94 {".bss", {0x400}, RVA_BSS, 0, 0, 0, 0, 0, 0,
95 IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE},
96 {".idata", {sizeof(struct Imports)}, RVA_IDATA, FILE_TOTAL-FILE_IDATA, FILE_IDATA, 0,
97 0, 0, 0, IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE}
98 },
99 /* alignment before first section */
100 {},
101 /* .text section */
102 {
103 0x31, 0xC0, /* xor eax, eax */
104 0xFF, 0x25, EXIT_PROCESS&0xFF, (EXIT_PROCESS>>8)&0xFF, (EXIT_PROCESS>>16)&0xFF,
105 (EXIT_PROCESS>>24)&0xFF, /* jmp ExitProcess */
106 0
107 },
108 /* .idata section */
109 {
110 {
111 {{RVA_IDATA + FIELD_OFFSET(struct Imports, original_thunks)}, 0, 0,
112 RVA_IDATA + FIELD_OFFSET(struct Imports, dllname),
113 RVA_IDATA + FIELD_OFFSET(struct Imports, thunks)
114 },
115 {{0}, 0, 0, 0, 0}
116 },
117 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
118 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
119 {0,"ExitProcess"},
120 "KERNEL32.DLL"
121 },
122 /* final alignment */
123 {}
124 };
125 #include <poppack.h>
126
127 struct blob
128 {
129 DWORD cb;
130 BYTE *pb;
131 };
132
133 struct expected_blob
134 {
135 DWORD cb;
136 const void *pb;
137 };
138
139 struct update_accum
140 {
141 DWORD cUpdates;
142 struct blob *updates;
143 };
144
145 struct expected_update_accum
146 {
147 DWORD cUpdates;
148 const struct expected_blob *updates;
149 BOOL todo;
150 };
151
152 static BOOL WINAPI accumulating_stream_output(DIGEST_HANDLE handle, BYTE *pb,
153 DWORD cb)
154 {
155 struct update_accum *accum = (struct update_accum *)handle;
156 BOOL ret = FALSE;
157
158 if (accum->cUpdates)
159 accum->updates = HeapReAlloc(GetProcessHeap(), 0, accum->updates,
160 (accum->cUpdates + 1) * sizeof(struct blob));
161 else
162 accum->updates = HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob));
163 if (accum->updates)
164 {
165 struct blob *blob = &accum->updates[accum->cUpdates];
166
167 blob->pb = HeapAlloc(GetProcessHeap(), 0, cb);
168 if (blob->pb)
169 {
170 memcpy(blob->pb, pb, cb);
171 blob->cb = cb;
172 ret = TRUE;
173 }
174 accum->cUpdates++;
175 }
176 return ret;
177 }
178
179 static void check_updates(LPCSTR header, const struct expected_update_accum *expected,
180 const struct update_accum *got)
181 {
182 DWORD i;
183
184 if (expected->todo)
185 todo_wine ok(expected->cUpdates == got->cUpdates, "%s: expected %d updates, got %d\n",
186 header, expected->cUpdates, got->cUpdates);
187 else
188 ok(expected->cUpdates == got->cUpdates, "%s: expected %d updates, got %d\n",
189 header, expected->cUpdates, got->cUpdates);
190 for (i = 0; i < min(expected->cUpdates, got->cUpdates); i++)
191 {
192 ok(expected->updates[i].cb == got->updates[i].cb, "%s, update %d: expected %d bytes, got %d\n",
193 header, i, expected->updates[i].cb, got->updates[i].cb);
194 if (expected->updates[i].cb && expected->updates[i].cb == got->updates[i].cb)
195 ok(!memcmp(expected->updates[i].pb, got->updates[i].pb, got->updates[i].cb),
196 "%s, update %d: unexpected value\n", header, i);
197 }
198 }
199
200 /* Frees the updates stored in accum */
201 static void free_updates(struct update_accum *accum)
202 {
203 DWORD i;
204
205 for (i = 0; i < accum->cUpdates; i++)
206 HeapFree(GetProcessHeap(), 0, accum->updates[i].pb);
207 HeapFree(GetProcessHeap(), 0, accum->updates);
208 accum->updates = NULL;
209 accum->cUpdates = 0;
210 }
211
212 static const struct expected_blob b1[] = {
213 {FILE_PE_START, &bin},
214 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
215 {sizeof(bin.nt_headers), &bin.nt_headers},
216 {sizeof(bin.sections), &bin.sections},
217 {FILE_IDATA-FILE_TEXT, &bin.text_section},
218 {sizeof(bin.idata_section.descriptors[0].u.OriginalFirstThunk),
219 &bin.idata_section.descriptors[0].u.OriginalFirstThunk},
220 {FIELD_OFFSET(struct Imports, thunks)-FIELD_OFFSET(struct Imports, descriptors[0].Name),
221 &bin.idata_section.descriptors[0].Name},
222 {FILE_TOTAL-FILE_IDATA-FIELD_OFFSET(struct Imports, ibn),
223 &bin.idata_section.ibn}
224 };
225 static const struct expected_update_accum a1 = { sizeof(b1) / sizeof(b1[0]), b1, TRUE };
226
227 static const struct expected_blob b2[] = {
228 {FILE_PE_START, &bin},
229 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
230 {sizeof(bin.nt_headers), &bin.nt_headers},
231 {sizeof(bin.sections), &bin.sections},
232 {FILE_IDATA-FILE_TEXT, &bin.text_section},
233 {FILE_TOTAL-FILE_IDATA, &bin.idata_section}
234 };
235 static const struct expected_update_accum a2 = { sizeof(b2) / sizeof(b2[0]), b2, FALSE };
236
237 /* Creates a test file and returns a handle to it. The file's path is returned
238 * in temp_file, which must be at least MAX_PATH characters in length.
239 */
240 static HANDLE create_temp_file(char *temp_file)
241 {
242 HANDLE file = INVALID_HANDLE_VALUE;
243 char temp_path[MAX_PATH];
244
245 if (GetTempPathA(sizeof(temp_path), temp_path))
246 {
247 if (GetTempFileNameA(temp_path, "img", 0, temp_file))
248 file = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
249 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
250 }
251 return file;
252 }
253
254 static void update_checksum(void)
255 {
256 WORD const * ptr;
257 DWORD size;
258 DWORD sum = 0;
259
260 bin.nt_headers.OptionalHeader.CheckSum = 0;
261
262 for(ptr = (WORD const *)&bin, size = (sizeof(bin)+1)/sizeof(WORD); size > 0; ptr++, size--)
263 {
264 sum += *ptr;
265 if (HIWORD(sum) != 0)
266 {
267 sum = LOWORD(sum) + HIWORD(sum);
268 }
269 }
270 sum = (WORD)(LOWORD(sum) + HIWORD(sum));
271 sum += sizeof(bin);
272
273 bin.nt_headers.OptionalHeader.CheckSum = sum;
274 }
275
276 static void test_get_digest_stream(void)
277 {
278 BOOL ret;
279 HANDLE file;
280 char temp_file[MAX_PATH];
281 DWORD count;
282 struct update_accum accum = { 0, NULL };
283
284 SetLastError(0xdeadbeef);
285 ret = pImageGetDigestStream(NULL, 0, NULL, NULL);
286 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
287 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
288 file = create_temp_file(temp_file);
289 if (file == INVALID_HANDLE_VALUE)
290 {
291 skip("couldn't create temp file\n");
292 return;
293 }
294 SetLastError(0xdeadbeef);
295 ret = pImageGetDigestStream(file, 0, NULL, NULL);
296 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
297 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
298 SetLastError(0xdeadbeef);
299 ret = pImageGetDigestStream(NULL, 0, accumulating_stream_output, &accum);
300 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
301 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
302 /* Even with "valid" parameters, it fails with an empty file */
303 SetLastError(0xdeadbeef);
304 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
305 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
306 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
307 /* Finally, with a valid executable in the file, it succeeds. Note that
308 * the file pointer need not be positioned at the beginning.
309 */
310 update_checksum();
311 WriteFile(file, &bin, sizeof(bin), &count, NULL);
312 FlushFileBuffers(file);
313
314 /* zero out some fields ImageGetDigestStream would zero out */
315 bin.nt_headers.OptionalHeader.CheckSum = 0;
316 bin.nt_headers.OptionalHeader.SizeOfInitializedData = 0;
317 bin.nt_headers.OptionalHeader.SizeOfImage = 0;
318
319 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
320 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
321 check_updates("flags = 0", &a1, &accum);
322 free_updates(&accum);
323 ret = pImageGetDigestStream(file, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO,
324 accumulating_stream_output, &accum);
325 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
326 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2, &accum);
327 free_updates(&accum);
328 CloseHandle(file);
329 DeleteFileA(temp_file);
330 }
331
332 START_TEST(image)
333 {
334 hImageHlp = LoadLibraryA("imagehlp.dll");
335
336 if (!hImageHlp)
337 {
338 win_skip("ImageHlp unavailable\n");
339 return;
340 }
341
342 pImageGetDigestStream = (void *) GetProcAddress(hImageHlp, "ImageGetDigestStream");
343
344 if (!pImageGetDigestStream)
345 {
346 win_skip("ImageGetDigestStream function is not available\n");
347 } else
348 {
349 test_get_digest_stream();
350 }
351
352 FreeLibrary(hImageHlp);
353 }