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
29 #include "wine/test.h"
31 static HMODULE hImageHlp
;
33 static BOOL (WINAPI
*pImageGetDigestStream
)(HANDLE
, DWORD
, DIGEST_FUNCTION
, DIGEST_HANDLE
);
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
48 IMAGE_IMPORT_DESCRIPTOR descriptors
[2];
49 IMAGE_THUNK_DATA32 original_thunks
[2];
50 IMAGE_THUNK_DATA32 thunks
[2];
51 struct __IMPORT_BY_NAME
{
57 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks[0]))
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
)];
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 */
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
},
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,
86 {RVA_IDATA
, sizeof(struct Imports
)}
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
}
99 /* alignment before first section */
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 */
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
)
117 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
118 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
122 /* final alignment */
142 struct blob
*updates
;
145 struct expected_update_accum
148 const struct expected_blob
*updates
;
152 static BOOL WINAPI
accumulating_stream_output(DIGEST_HANDLE handle
, BYTE
*pb
,
155 struct update_accum
*accum
= (struct update_accum
*)handle
;
159 accum
->updates
= HeapReAlloc(GetProcessHeap(), 0, accum
->updates
,
160 (accum
->cUpdates
+ 1) * sizeof(struct blob
));
162 accum
->updates
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob
));
165 struct blob
*blob
= &accum
->updates
[accum
->cUpdates
];
167 blob
->pb
= HeapAlloc(GetProcessHeap(), 0, cb
);
170 memcpy(blob
->pb
, pb
, cb
);
179 static void check_updates(LPCSTR header
, const struct expected_update_accum
*expected
,
180 const struct update_accum
*got
)
185 todo_wine
ok(expected
->cUpdates
== got
->cUpdates
, "%s: expected %d updates, got %d\n",
186 header
, expected
->cUpdates
, got
->cUpdates
);
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
++)
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
);
200 /* Frees the updates stored in accum */
201 static void free_updates(struct update_accum
*accum
)
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
;
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
}
225 static const struct expected_update_accum a1
= { sizeof(b1
) / sizeof(b1
[0]), b1
, TRUE
};
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
}
235 static const struct expected_update_accum a2
= { sizeof(b2
) / sizeof(b2
[0]), b2
, FALSE
};
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.
240 static HANDLE
create_temp_file(char *temp_file
)
242 HANDLE file
= INVALID_HANDLE_VALUE
;
243 char temp_path
[MAX_PATH
];
245 if (GetTempPathA(sizeof(temp_path
), temp_path
))
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
);
254 static void update_checksum(void)
260 bin
.nt_headers
.OptionalHeader
.CheckSum
= 0;
262 for(ptr
= (WORD
const *)&bin
, size
= (sizeof(bin
)+1)/sizeof(WORD
); size
> 0; ptr
++, size
--)
265 if (HIWORD(sum
) != 0)
267 sum
= LOWORD(sum
) + HIWORD(sum
);
270 sum
= (WORD
)(LOWORD(sum
) + HIWORD(sum
));
273 bin
.nt_headers
.OptionalHeader
.CheckSum
= sum
;
276 static void test_get_digest_stream(void)
280 char temp_file
[MAX_PATH
];
282 struct update_accum accum
= { 0, NULL
};
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
)
291 skip("couldn't create temp file\n");
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.
311 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
312 FlushFileBuffers(file
);
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;
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
);
329 DeleteFileA(temp_file
);
334 hImageHlp
= LoadLibraryA("imagehlp.dll");
338 win_skip("ImageHlp unavailable\n");
342 pImageGetDigestStream
= (void *) GetProcAddress(hImageHlp
, "ImageGetDigestStream");
344 if (!pImageGetDigestStream
)
346 win_skip("ImageGetDigestStream function is not available\n");
349 test_get_digest_stream();
352 FreeLibrary(hImageHlp
);