wininet: Set last error for invalid URL argument.
[wine] / dlls / imagehlp / tests / integrity.c
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
30 static HMODULE hImageHlp;
31 static char test_dll_path[MAX_PATH];
32
33 static BOOL (WINAPI *pImageAddCertificate)(HANDLE, LPWIN_CERTIFICATE, PDWORD);
34 static BOOL (WINAPI *pImageEnumerateCertificates)(HANDLE, WORD, PDWORD, PDWORD, DWORD);
35 static BOOL (WINAPI *pImageGetCertificateData)(HANDLE, DWORD, LPWIN_CERTIFICATE, PDWORD);
36 static BOOL (WINAPI *pImageGetCertificateHeader)(HANDLE, DWORD, LPWIN_CERTIFICATE);
37 static BOOL (WINAPI *pImageRemoveCertificate)(HANDLE, DWORD);
38
39 static char test_cert_data[] =
40 {0x30,0x82,0x02,0xE1,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x07,0x02
41 ,0xA0,0x82,0x02,0xD2,0x30,0x82,0x02,0xCE,0x02,0x01,0x01,0x31,0x00,0x30,0x0B
42 ,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x07,0x01,0xA0,0x82,0x02,0xB4
43 ,0x30,0x82,0x02,0xB0,0x30,0x82,0x02,0x19,0xA0,0x03,0x02,0x01,0x02,0x02,0x09
44 ,0x00,0xE2,0x59,0x17,0xA5,0x87,0x0F,0x88,0x89,0x30,0x0D,0x06,0x09,0x2A,0x86
45 ,0x48,0x86,0xF7,0x0D,0x01,0x01,0x05,0x05,0x00,0x30,0x45,0x31,0x0B,0x30,0x09
46 ,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x41,0x55,0x31,0x13,0x30,0x11,0x06,0x03
47 ,0x55,0x04,0x08,0x13,0x0A,0x53,0x6F,0x6D,0x65,0x2D,0x53,0x74,0x61,0x74,0x65
48 ,0x31,0x21,0x30,0x1F,0x06,0x03,0x55,0x04,0x0A,0x13,0x18,0x49,0x6E,0x74,0x65
49 ,0x72,0x6E,0x65,0x74,0x20,0x57,0x69,0x64,0x67,0x69,0x74,0x73,0x20,0x50,0x74
50 ,0x79,0x20,0x4C,0x74,0x64,0x30,0x1E,0x17,0x0D,0x30,0x39,0x31,0x31,0x32,0x30
51 ,0x31,0x37,0x33,0x38,0x31,0x32,0x5A,0x17,0x0D,0x31,0x30,0x31,0x31,0x32,0x30
52 ,0x31,0x37,0x33,0x38,0x31,0x32,0x5A,0x30,0x45,0x31,0x0B,0x30,0x09,0x06,0x03
53 ,0x55,0x04,0x06,0x13,0x02,0x41,0x55,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04
54 ,0x08,0x13,0x0A,0x53,0x6F,0x6D,0x65,0x2D,0x53,0x74,0x61,0x74,0x65,0x31,0x21
55 ,0x30,0x1F,0x06,0x03,0x55,0x04,0x0A,0x13,0x18,0x49,0x6E,0x74,0x65,0x72,0x6E
56 ,0x65,0x74,0x20,0x57,0x69,0x64,0x67,0x69,0x74,0x73,0x20,0x50,0x74,0x79,0x20
57 ,0x4C,0x74,0x64,0x30,0x81,0x9F,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7
58 ,0x0D,0x01,0x01,0x01,0x05,0x00,0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xF8,0xA1,0x49,0xA4,0x47,0x30
73 ,0x45,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x41,0x55,0x31
74 ,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x08,0x13,0x0A,0x53,0x6F,0x6D,0x65,0x2D
75 ,0x53,0x74,0x61,0x74,0x65,0x31,0x21,0x30,0x1F,0x06,0x03,0x55,0x04,0x0A,0x13
76 ,0x18,0x49,0x6E,0x74,0x65,0x72,0x6E,0x65,0x74,0x20,0x57,0x69,0x64,0x67,0x69
77 ,0x74,0x73,0x20,0x50,0x74,0x79,0x20,0x4C,0x74,0x64,0x82,0x09,0x00,0xE2,0x59
78 ,0x17,0xA5,0x87,0x0F,0x88,0x89,0x30,0x0C,0x06,0x03,0x55,0x1D,0x13,0x04,0x05
79 ,0x30,0x03,0x01,0x01,0xFF,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D
80 ,0x01,0x01,0x05,0x05,0x00,0x03,0x81,0x81,0x00,0x52,0x09,0xA5,0x81,0x63,0xEF
81 ,0xF7,0x76,0x65,0x2B,0xA5,0x48,0xC1,0xC5,0xE0,0x73,0x60,0x9B,0x66,0x2E,0x21
82 ,0xCF,0xF2,0xBD,0xFF,0x81,0xC4,0x99,0x39,0xD0,0x5D,0x1B,0x12,0xFD,0xAE,0x30
83 ,0x5D,0x9C,0x1A,0xD4,0x76,0x8A,0x25,0x10,0x0A,0x7E,0x5D,0x78,0xB5,0x94,0xD8
84 ,0x97,0xBD,0x9A,0x5A,0xD6,0x23,0xCA,0x5C,0x46,0x8C,0xC7,0x30,0x45,0xB4,0x77
85 ,0x44,0x6F,0x16,0xDD,0xC6,0x58,0xFE,0x16,0x15,0xAD,0xB8,0x58,0x49,0x9A,0xFE
86 ,0x6B,0x87,0x78,0xEE,0x13,0xFF,0x29,0x26,0x8E,0x13,0x83,0x0D,0x18,0xCA,0x9F
87 ,0xA9,0x3E,0x6E,0x3C,0xA6,0x50,0x4A,0x04,0x71,0x9F,0x2E,0xCF,0x25,0xA6,0x03
88 ,0x46,0xCA,0xEB,0xEA,0x67,0x89,0x49,0x7C,0x43,0xA2,0x52,0xD9,0x41,0xCC,0x65
89 ,0xED,0x2D,0xA1,0x00,0x31,0x00};
90
91 static char test_cert_data_2[] = {0xDE,0xAD,0xBE,0xEF,0x01,0x02,0x03};
92
93 static BOOL copy_dll_file(void)
94 {
95     char sys_dir[MAX_PATH+15];
96     char temp_path[MAX_PATH];
97
98     if (GetSystemDirectoryA(sys_dir, MAX_PATH) == 0)
99     {
100         skip("Failed to get system directory. Skipping certificate/PE image tests.\n");
101         return FALSE;
102     }
103
104     if (sys_dir[lstrlenA(sys_dir) - 1] != '\\')
105         lstrcatA(sys_dir, "\\");
106
107     lstrcatA(sys_dir, "imagehlp.dll");
108
109     /* Copy DLL to a temp file */
110     GetTempPath(MAX_PATH, temp_path);
111     GetTempFileName(temp_path, "img", 0, test_dll_path);
112
113     if (CopyFile(sys_dir, test_dll_path, FALSE) == 0)
114     {
115         skip("Unable to create copy of imagehlp.dll for tests.\n");
116         return FALSE;
117     }
118
119     return TRUE;
120 }
121
122 static DWORD get_file_size(void)
123 {
124     HANDLE file;
125     DWORD filesize = 0;
126
127     file = CreateFileA(test_dll_path, GENERIC_READ, FILE_SHARE_READ, NULL,
128                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
129     if (file == INVALID_HANDLE_VALUE)
130         return 0;
131
132     filesize = GetFileSize(file, NULL);
133     CloseHandle(file);
134
135     return filesize;
136 }
137
138 static void test_add_certificate(char *cert_data, int len)
139 {
140     HANDLE hFile;
141     LPWIN_CERTIFICATE cert;
142     DWORD cert_len;
143     DWORD index;
144     BOOL ret;
145
146     hFile = CreateFileA(test_dll_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
147
148     if (hFile == INVALID_HANDLE_VALUE)
149     {
150         skip("Unable to open %s, skipping test\n", test_dll_path);
151         return;
152     }
153
154     cert_len = sizeof(WIN_CERTIFICATE) + len;
155     cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cert_len);
156
157     if (!cert)
158     {
159         skip("Unable to allocate memory, skipping test\n");
160         CloseHandle(hFile);
161         return;
162     }
163
164     cert->dwLength = cert_len;
165     cert->wRevision = WIN_CERT_REVISION_1_0;
166     cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
167     CopyMemory(cert->bCertificate, cert_data, len);
168
169     ret = pImageAddCertificate(hFile, cert, &index);
170     ok(ret, "Unable to add certificate to image, error %x\n", GetLastError());
171
172     HeapFree(GetProcessHeap(), 0, cert);
173     CloseHandle(hFile);
174 }
175
176 static void test_get_certificate(char *cert_data, int index)
177 {
178     HANDLE hFile;
179     LPWIN_CERTIFICATE cert;
180     DWORD cert_len = 0;
181     DWORD err, ret;
182
183     hFile = CreateFileA(test_dll_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
184
185     if (hFile == INVALID_HANDLE_VALUE)
186     {
187         skip("Unable to open %s, skipping test\n", test_dll_path);
188         return;
189     }
190
191     ret = pImageGetCertificateData(hFile, index, NULL, &cert_len);
192     err = GetLastError();
193
194     ok ((ret == FALSE) && (err == ERROR_INSUFFICIENT_BUFFER), "ImageGetCertificateData gave unexpected result; ret=%d / err=%x\n", ret, err);
195
196     cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cert_len);
197
198     if (!cert)
199     {
200         skip("Unable to allocate memory, skipping test\n");
201         CloseHandle(hFile);
202         return;
203     }
204
205     ret = pImageGetCertificateData(hFile, index, cert, &cert_len);
206     ok(ret, "Unable to retrieve certificate; err=%x\n", GetLastError());
207     ok(memcmp(cert->bCertificate, cert_data, cert_len - sizeof(WIN_CERTIFICATE)) == 0, "Certificate retrieved did not match original\n");
208
209     HeapFree(GetProcessHeap(), 0, cert);
210     CloseHandle(hFile);
211 }
212
213 static void test_remove_certificate(int index)
214 {
215     DWORD orig_count = 0, count = 0;
216     HANDLE hFile;
217     BOOL ret;
218
219     hFile = CreateFileA(test_dll_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
220
221     if (hFile == INVALID_HANDLE_VALUE)
222     {
223         skip("Unable to open %s, skipping test\n", test_dll_path);
224         return;
225     }
226
227     ret = pImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &orig_count, NULL, 0);
228     ok (ret, "Unable to enumerate certificates in file; err=%x\n", GetLastError());
229     ret = pImageRemoveCertificate(hFile, index);
230     ok (ret, "Unable to remove certificate from file; err=%x\n", GetLastError());
231
232     /* Test to see if the certificate has actually been removed */
233     pImageEnumerateCertificates(hFile, CERT_SECTION_TYPE_ANY, &count, NULL, 0);
234     ok (count == orig_count - 1, "Certificate count mismatch; orig=%d new=%d\n", orig_count, count);
235
236     CloseHandle(hFile);
237 }
238
239 START_TEST(integrity)
240 {
241     DWORD file_size, file_size_orig;
242
243     hImageHlp = LoadLibraryA("imagehlp.dll");
244
245     if (!hImageHlp)
246     {
247         win_skip("ImageHlp unavailable\n");
248         return;
249     }
250
251     if (!copy_dll_file())
252     {
253         FreeLibrary(hImageHlp);
254         return;
255     }
256
257     file_size_orig = get_file_size();
258     /*
259      * Align file_size_orig to an 8-byte boundary. This avoids tests failures where
260      * the original dll is not correctly aligned (but when written to it will be).
261      */
262     if (file_size_orig % 8 != 0)
263     {
264         skip("We need to align to an 8-byte boundary\n");
265         file_size_orig = (file_size_orig + 7) & ~7;
266     }
267
268     pImageAddCertificate = (void *) GetProcAddress(hImageHlp, "ImageAddCertificate");
269     pImageEnumerateCertificates = (void *) GetProcAddress(hImageHlp, "ImageEnumerateCertificates");
270     pImageGetCertificateData = (void *) GetProcAddress(hImageHlp, "ImageGetCertificateData");
271     pImageGetCertificateHeader = (void *) GetProcAddress(hImageHlp, "ImageGetCertificateHeader");
272     pImageRemoveCertificate = (void *) GetProcAddress(hImageHlp, "ImageRemoveCertificate");
273
274     test_add_certificate(test_cert_data, sizeof(test_cert_data));
275     test_get_certificate(test_cert_data, 0);
276     test_remove_certificate(0);
277
278     file_size = get_file_size();
279     ok(file_size == file_size_orig, "File size different after add and remove (old: %d; new: %d)\n", file_size_orig, file_size);
280
281     /* Try adding multiple certificates */
282     test_add_certificate(test_cert_data, sizeof(test_cert_data));
283     test_add_certificate(test_cert_data_2, sizeof(test_cert_data_2));
284
285     test_get_certificate(test_cert_data, 0);
286     test_get_certificate(test_cert_data_2, 1);
287
288     /* Remove the first one and verify the second certificate is intact */
289     test_remove_certificate(0);
290     test_get_certificate(test_cert_data_2, 0);
291
292     test_remove_certificate(0);
293
294     file_size = get_file_size();
295     ok(file_size == file_size_orig, "File size different after add and remove (old: %d; new: %d)\n", file_size_orig, file_size);
296
297     FreeLibrary(hImageHlp);
298     DeleteFile(test_dll_path);
299 }