1 /*
2  * Unit test suite for resource functions.
3  *
4  * Copyright 2006 Mike McCormack
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 #include <windows.h>
22 #include <stdio.h>
23 
24 #include "wine/test.h"
25 
26 static const char filename[] = "test_.exe";
27 static DWORD GLE;
28 
29 enum constants {
30     page_size = 0x1000,
31     rva_rsrc_start = page_size * 3,
32     max_sections = 3
33 };
34 
35 /* rodata @ [0x1000-0x3000) */
36 static const IMAGE_SECTION_HEADER sh_rodata_1 =
37 {
38     ".rodata", {2*page_size}, page_size, 2*page_size, page_size, 0, 0, 0, 0,
39     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
40 };
41 
42 /* rodata @ [0x1000-0x2000) */
43 static const IMAGE_SECTION_HEADER sh_rodata_2 =
44 {
45     ".rodata", {page_size}, page_size, page_size, page_size, 0, 0, 0, 0,
46     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
47 };
48 
49 /* rsrc @ [0x3000-0x4000) */
50 static const IMAGE_SECTION_HEADER sh_rsrc_1 =
51 {
52     ".rsrc\0\0", {page_size}, rva_rsrc_start, page_size, rva_rsrc_start, 0, 0, 0, 0,
53     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
54 };
55 
56 /* rsrc @ [0x2000-0x4000) */
57 static const IMAGE_SECTION_HEADER sh_rsrc_2 =
58 {
59     ".rsrc\0\0", {2*page_size}, rva_rsrc_start-page_size, 2*page_size, rva_rsrc_start-page_size, 0, 0, 0, 0,
60     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
61 };
62 
63 /* rsrc @ [0x2000-0x3000) */
64 static const IMAGE_SECTION_HEADER sh_rsrc_3 =
65 {
66     ".rsrc\0\0", {page_size}, rva_rsrc_start-page_size, page_size, rva_rsrc_start-page_size, 0, 0, 0, 0,
67     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
68 };
69 
70 /* rsrc @ [0x3000-0x6000) */
71 static const IMAGE_SECTION_HEADER sh_rsrc_4 =
72 {
73     ".rsrc\0\0", {3*page_size}, rva_rsrc_start, 3*page_size, rva_rsrc_start, 0, 0, 0, 0,
74     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
75 };
76 
77 /* rsrc @ [0x2000-0x5000) */
78 static const IMAGE_SECTION_HEADER sh_rsrc_5 =
79 {
80     ".rsrc\0\0", {3*page_size}, 2*page_size, 3*page_size, 2*page_size, 0, 0, 0, 0,
81     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
82 };
83 
84 /* rsrc @ [0x3000-0x4000), small SizeOfRawData */
85 static const IMAGE_SECTION_HEADER sh_rsrc_6 =
86 {
87     ".rsrc\0\0", {page_size}, rva_rsrc_start, 8, rva_rsrc_start, 0, 0, 0, 0,
88     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
89 };
90 
91 /* reloc @ [0x4000-0x5000) */
92 static const IMAGE_SECTION_HEADER sh_junk =
93 {
94     ".reloc\0", {page_size}, 4*page_size, page_size, 4*page_size, 0, 0, 0, 0,
95     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
96 };
97 
98 /* reloc @ [0x6000-0x7000) */
99 static const IMAGE_SECTION_HEADER sh_junk_2 =
100 {
101     ".reloc\0", {page_size}, 6*page_size, page_size, 6*page_size, 0, 0, 0, 0,
102     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
103 };
104 
105 typedef struct _sec_build
106 {
107     const IMAGE_SECTION_HEADER *sect_in[max_sections];
108 } sec_build;
109 
110 typedef struct _sec_verify
111 {
112     const IMAGE_SECTION_HEADER *sect_out[max_sections];
113     DWORD length;
114     int rsrc_section;
115     DWORD NumberOfNamedEntries, NumberOfIdEntries;
116 } sec_verify;
117 
118 static const struct _sec_variants
119 {
120     sec_build build;
121     sec_verify chk_none, chk_delete, chk_version, chk_bigdata;
122 } sec_variants[] =
123 {
124     /* .rsrc is the last section, data directory entry points to whole section */
125     {
126         {{&sh_rodata_1, &sh_rsrc_1, NULL}},
127         {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
128         {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
129         {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 1},
130         {{&sh_rodata_1, &sh_rsrc_4, NULL}, 6*page_size, 1, 0, 1}
131     },
132     /* .rsrc is the last section, data directory entry points to section end */
133     /* Vista+ - resources are moved to section start (trashing data that could be there), and section is trimmed */
134     /* NT4/2000/2003 - resources are moved to section start (trashing data that could be there); section isn't trimmed */
135     {
136         {{&sh_rodata_2, &sh_rsrc_2, NULL}},
137         {{&sh_rodata_2, &sh_rsrc_3, NULL}, 3*page_size, 1, 0, 0},
138         {{&sh_rodata_2, &sh_rsrc_3, NULL}, 3*page_size, 1, 0, 0},
139         {{&sh_rodata_2, &sh_rsrc_3, NULL}, 3*page_size, 1, 0, 1},
140         {{&sh_rodata_2, &sh_rsrc_5, NULL}, 5*page_size, 1, 0, 1}
141     },
142     /* .rsrc is not the last section */
143     /* section is reused; sections after .rsrc are shifted to give space to rsrc (in-image offset and RVA!) */
144     {
145         {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}},
146         {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}, 5*page_size, 1, 0, 0},
147         {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}, 5*page_size, 1, 0, 0},
148         {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}, 5*page_size, 1, 0, 1},
149         {{&sh_rodata_1, &sh_rsrc_4, &sh_junk_2}, 7*page_size, 1, 0, 1}
150     },
151     /* .rsrc is the last section, data directory entry points to whole section, file size is not aligned on FileAlign */
152     {
153         {{&sh_rodata_1, &sh_rsrc_6, NULL}},
154         {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
155         {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
156         {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 1},
157         {{&sh_rodata_1, &sh_rsrc_4, NULL}, 6*page_size, 1, 0, 1}
158     }
159 };
160 
161 static int build_exe( const sec_build* sec_descr )
162 {
163     IMAGE_DOS_HEADER *dos;
164     IMAGE_NT_HEADERS *nt;
165     IMAGE_SECTION_HEADER *sec;
166     IMAGE_OPTIONAL_HEADER *opt;
167     HANDLE file;
168     DWORD written, i, file_size;
169     BYTE page[page_size];
170 
171     memset( page, 0, sizeof page );
172 
173     dos = (void*) page;
174     dos->e_magic = IMAGE_DOS_SIGNATURE;
175     dos->e_lfanew = sizeof *dos;
176 
177     nt = (void*) &dos[1];
178 
179     nt->Signature = IMAGE_NT_SIGNATURE;
180     nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
181     nt->FileHeader.NumberOfSections = 0;
182     nt->FileHeader.SizeOfOptionalHeader = sizeof nt->OptionalHeader;
183     nt->FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
184 
185     opt = &nt->OptionalHeader;
186 
187     opt->Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
188     opt->MajorLinkerVersion = 1;
189     opt->BaseOfCode = 0x10;
190     opt->ImageBase = 0x10000000;
191     opt->MajorOperatingSystemVersion = 4;
192     opt->MajorImageVersion = 1;
193     opt->MajorSubsystemVersion = 4;
194     opt->SizeOfHeaders = sizeof *dos + sizeof *nt + sizeof *sec * 2;
195     opt->SizeOfImage = page_size;
196     opt->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
197 
198     /* if SectionAlignment and File alignment are not specified */
199     /* UpdateResource fails trying to create a huge temporary file */
200     opt->SectionAlignment = page_size;
201     opt->FileAlignment = page_size;
202 
203     opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress = rva_rsrc_start;
204     opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].Size = page_size;
205 
206     sec = (void*) &nt[1];
207 
208     file_size = 0;
209     for ( i = 0; i < max_sections; i++ )
210         if ( sec_descr->sect_in[i] )
211         {
212             DWORD virt_end_of_section = sec_descr->sect_in[i]->Misc.VirtualSize +
213                 sec_descr->sect_in[i]->VirtualAddress;
214             DWORD phys_end_of_section = sec_descr->sect_in[i]->SizeOfRawData +
215                 sec_descr->sect_in[i]->PointerToRawData;
216             memcpy( sec + nt->FileHeader.NumberOfSections, sec_descr->sect_in[i],
217                     sizeof(sec[0]) );
218             nt->FileHeader.NumberOfSections++;
219             if ( opt->SizeOfImage < virt_end_of_section )
220                 opt->SizeOfImage = virt_end_of_section;
221             if ( file_size < phys_end_of_section )
222                 file_size = phys_end_of_section;
223         }
224 
225     file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
226     ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
227 
228     /* write out the header */
229     WriteFile( file, page, sizeof page, &written, NULL );
230 
231     /* write out zeroed pages for sections */
232     memset( page, 0, sizeof page );
233     for ( i = page_size; i < file_size; i += page_size )
234     {
235 	DWORD size = min(page_size, file_size - i);
236         WriteFile( file, page, size, &written, NULL );
237     }
238 
239     CloseHandle( file );
240 
241     return 0;
242 }
243 
244 static void update_missing_exe( void )
245 {
246     HANDLE res;
247 
248     SetLastError(0xdeadbeef);
249     res = BeginUpdateResourceA( filename, TRUE );
250     GLE = GetLastError();
251     ok( res == NULL, "BeginUpdateResource should fail\n");
252 }
253 
254 static void update_empty_exe( void )
255 {
256     HANDLE file, res, test;
257     BOOL r;
258 
259     file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
260     ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
261 
262     CloseHandle( file );
263 
264     res = BeginUpdateResourceA( filename, TRUE );
265     if ( res != NULL || GetLastError() != ERROR_FILE_INVALID )
266     {
267         ok( res != NULL, "BeginUpdateResource failed\n");
268 
269         /* check if it's possible to open the file now */
270         test = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
271         ok (test != INVALID_HANDLE_VALUE, "failed to create file\n");
272 
273         CloseHandle( test );
274 
275         r = EndUpdateResourceA( res, FALSE );
276         ok( r == FALSE, "EndUpdateResource failed\n");
277     }
278     else
279         skip( "Can't update resource in empty file\n" );
280 
281     res = BeginUpdateResourceA( filename, FALSE );
282     ok( res == NULL, "BeginUpdateResource failed\n");
283 }
284 
285 static void update_resources_none( void )
286 {
287     HMODULE res;
288     BOOL r;
289 
290     res = BeginUpdateResourceA( filename, FALSE );
291     ok( res != NULL, "BeginUpdateResource failed\n");
292 
293     r = EndUpdateResourceA( res, FALSE );
294     ok( r, "EndUpdateResource failed\n");
295 }
296 
297 static void update_resources_delete( void )
298 {
299     HMODULE res;
300     BOOL r;
301 
302     res = BeginUpdateResourceA( filename, TRUE );
303     ok( res != NULL, "BeginUpdateResource failed\n");
304 
305     r = EndUpdateResourceA( res, FALSE );
306     ok( r, "EndUpdateResource failed\n");
307 }
308 
309 static void update_resources_version( void )
310 {
311     HANDLE res = NULL;
312     BOOL r;
313     char foo[] = "red and white";
314 
315     res = BeginUpdateResourceA( filename, TRUE );
316     ok( res != NULL, "BeginUpdateResource failed\n");
317 
318     if (0)  /* this causes subsequent tests to fail on Vista */
319     {
320         r = UpdateResourceA( res,
321                             MAKEINTRESOURCEA(0x1230),
322                             MAKEINTRESOURCEA(0x4567),
323                             0xabcd,
324                             NULL, 0 );
325         ok( r == FALSE, "UpdateResource failed\n");
326     }
327 
328     r = UpdateResourceA( res,
329                         MAKEINTRESOURCEA(0x1230),
330                         MAKEINTRESOURCEA(0x4567),
331                         0xabcd,
332                         foo, sizeof foo );
333     ok( r == TRUE, "UpdateResource failed: %d\n", GetLastError());
334 
335     r = EndUpdateResourceA( res, FALSE );
336     ok( r, "EndUpdateResource failed: %d\n", GetLastError());
337 }
338 
339 static void update_resources_bigdata( void )
340 {
341     HANDLE res = NULL;
342     BOOL r;
343     char foo[2*page_size] = "foobar";
344 
345     res = BeginUpdateResourceA( filename, TRUE );
346     ok( res != NULL, "BeginUpdateResource succeeded\n");
347 
348     r = UpdateResourceA( res,
349                         MAKEINTRESOURCEA(0x3012),
350                         MAKEINTRESOURCEA(0x5647),
351                         0xcdba,
352                         foo, sizeof foo );
353     ok( r == TRUE, "UpdateResource failed: %d\n", GetLastError());
354 
355     r = EndUpdateResourceA( res, FALSE );
356     ok( r, "EndUpdateResource failed\n");
357 }
358 
359 static void check_exe( const sec_verify *verify )
360 {
361     int i;
362     IMAGE_DOS_HEADER *dos;
363     IMAGE_NT_HEADERS *nt;
364     IMAGE_OPTIONAL_HEADER *opt;
365     IMAGE_SECTION_HEADER *sec;
366     IMAGE_RESOURCE_DIRECTORY *dir;
367     HANDLE file, mapping;
368     DWORD length, sec_count = 0;
369 
370     file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
371     ok (file != INVALID_HANDLE_VALUE, "failed to create file (%d)\n", GetLastError());
372 
373     length = GetFileSize( file, NULL );
374     ok( length >= verify->length, "file size wrong\n");
375 
376     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 0, NULL );
377     ok (mapping != NULL, "failed to create file\n");
378 
379     dos = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
380     ok( dos != NULL, "failed to map file\n");
381 
382     if (!dos)
383         goto end;
384 
385     nt = (void*) ((BYTE*) dos + dos->e_lfanew);
386     opt = &nt->OptionalHeader;
387     sec = (void*) &nt[1];
388 
389     for(i = 0; i < max_sections; i++)
390         if (verify->sect_out[i])
391         {
392             ok( !memcmp(&verify->sect_out[i]->Name, &sec[sec_count].Name, 8), "section %d name wrong\n", sec_count);
393             ok( verify->sect_out[i]->VirtualAddress == sec[sec_count].VirtualAddress, "section %d vaddr wrong\n", sec_count);
394             ok( verify->sect_out[i]->SizeOfRawData <= sec[sec_count].SizeOfRawData, "section %d SizeOfRawData wrong (%d vs %d)\n", sec_count, verify->sect_out[i]->SizeOfRawData ,sec[sec_count].SizeOfRawData);
395             ok( verify->sect_out[i]->PointerToRawData == sec[sec_count].PointerToRawData, "section %d PointerToRawData wrong\n", sec_count);
396             ok( verify->sect_out[i]->Characteristics == sec[sec_count].Characteristics , "section %d characteristics wrong\n", sec_count);
397             sec_count++;
398         }
399 
400     ok( nt->FileHeader.NumberOfSections == sec_count, "number of sections wrong\n" );
401 
402     if (verify->rsrc_section >= 0 && verify->rsrc_section < nt->FileHeader.NumberOfSections)
403     {
404         dir = (void*) ((BYTE*) dos + sec[verify->rsrc_section].VirtualAddress);
405 
406         ok( dir->Characteristics == 0, "Characteristics wrong\n");
407         ok( dir->TimeDateStamp == 0 || abs( dir->TimeDateStamp - GetTickCount() ) < 1000 /* nt4 */,
408             "TimeDateStamp wrong %u\n", dir->TimeDateStamp);
409         ok( dir->MajorVersion == 4, "MajorVersion wrong\n");
410         ok( dir->MinorVersion == 0, "MinorVersion wrong\n");
411 
412         ok( dir->NumberOfNamedEntries == verify->NumberOfNamedEntries, "NumberOfNamedEntries should be %d instead of %d\n",
413                 verify->NumberOfNamedEntries, dir->NumberOfNamedEntries);
414         ok( dir->NumberOfIdEntries == verify->NumberOfIdEntries, "NumberOfIdEntries should be %d instead of %d\n",
415                 verify->NumberOfIdEntries, dir->NumberOfIdEntries);
416 
417         ok(opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress == sec[verify->rsrc_section].VirtualAddress,
418                 "VirtualAddress in optional header should be %d instead of %d\n",
419                 sec[verify->rsrc_section].VirtualAddress, opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress);
420     }
421 
422 end:
423     UnmapViewOfFile( dos );
424 
425     CloseHandle( mapping );
426 
427     CloseHandle( file );
428 }
429 
430 static void test_find_resource(void)
431 {
432     HRSRC rsrc;
433 
434     rsrc = FindResourceW( GetModuleHandleW(NULL), MAKEINTRESOURCEW(1), (LPCWSTR)RT_MENU );
435     ok( rsrc != 0, "resource not found\n" );
436     rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1),
437                             MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ));
438     ok( rsrc != 0, "resource not found\n" );
439     rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1),
440                             MAKELANGID( LANG_GERMAN, SUBLANG_DEFAULT ));
441     ok( rsrc != 0, "resource not found\n" );
442 
443     SetLastError( 0xdeadbeef );
444     rsrc = FindResourceW( GetModuleHandleW(NULL), MAKEINTRESOURCEW(1), (LPCWSTR)RT_DIALOG );
445     ok( !rsrc, "resource found\n" );
446     ok( GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND, "wrong error %u\n", GetLastError() );
447 
448     SetLastError( 0xdeadbeef );
449     rsrc = FindResourceW( GetModuleHandleW(NULL), MAKEINTRESOURCEW(2), (LPCWSTR)RT_MENU );
450     ok( !rsrc, "resource found\n" );
451     ok( GetLastError() == ERROR_RESOURCE_NAME_NOT_FOUND, "wrong error %u\n", GetLastError() );
452 
453     SetLastError( 0xdeadbeef );
454     rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1),
455                             MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
456     ok( !rsrc, "resource found\n" );
457     ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
458 
459     SetLastError( 0xdeadbeef );
460     rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1),
461                             MAKELANGID( LANG_FRENCH, SUBLANG_DEFAULT ) );
462     ok( !rsrc, "resource found\n" );
463     ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
464 }
465 
466 START_TEST(resource)
467 {
468     DWORD i;
469 
470     DeleteFileA( filename );
471     update_missing_exe();
472 
473     if (GLE == ERROR_CALL_NOT_IMPLEMENTED)
474     {
475         win_skip("Resource calls are not implemented\n");
476         return;
477     }
478 
479     update_empty_exe();
480 
481     for(i=0; i < sizeof( sec_variants ) / sizeof( sec_variants[0] ); i++)
482     {
483         const struct _sec_variants *sec = &sec_variants[i];
484         build_exe( &sec->build );
485         update_resources_none();
486         check_exe( &sec->chk_none );
487         update_resources_delete();
488         check_exe( &sec->chk_delete );
489         update_resources_version();
490         check_exe( &sec->chk_version );
491         update_resources_bigdata();
492         check_exe( &sec->chk_bigdata );
493         DeleteFileA( filename );
494     }
495     test_find_resource();
496 }
497