kernel32/tests: A couple spelling fixes in a comment.
[wine.git] / dlls / kernel32 / tests / resource.c
blob74066cfba3cbb593a93dd5451ac7f6b4dd2f68d0
1 /*
2 * Unit test suite for resource functions.
4 * Copyright 2006 Mike McCormack
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.
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.
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
21 #include <windows.h>
22 #include <stdio.h>
24 #include "wine/test.h"
26 static const char filename[] = "test_.exe";
27 static DWORD GLE;
29 enum constants {
30 page_size = 0x1000,
31 rva_rsrc_start = page_size * 3,
32 max_sections = 3
35 /* rodata @ [0x1000-0x3000) */
36 static const IMAGE_SECTION_HEADER sh_rodata_1 =
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
42 /* rodata @ [0x1000-0x2000) */
43 static const IMAGE_SECTION_HEADER sh_rodata_2 =
45 ".rodata", {page_size}, page_size, page_size, page_size, 0, 0, 0, 0,
46 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
49 /* rsrc @ [0x3000-0x4000) */
50 static const IMAGE_SECTION_HEADER sh_rsrc_1 =
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
56 /* rsrc @ [0x2000-0x4000) */
57 static const IMAGE_SECTION_HEADER sh_rsrc_2 =
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
63 /* rsrc @ [0x2000-0x3000) */
64 static const IMAGE_SECTION_HEADER sh_rsrc_3 =
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
70 /* rsrc @ [0x3000-0x6000) */
71 static const IMAGE_SECTION_HEADER sh_rsrc_4 =
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
77 /* rsrc @ [0x2000-0x5000) */
78 static const IMAGE_SECTION_HEADER sh_rsrc_5 =
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
84 /* rsrc @ [0x3000-0x4000), small SizeOfRawData */
85 static const IMAGE_SECTION_HEADER sh_rsrc_6 =
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
91 /* reloc @ [0x4000-0x5000) */
92 static const IMAGE_SECTION_HEADER sh_junk =
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
98 /* reloc @ [0x6000-0x7000) */
99 static const IMAGE_SECTION_HEADER sh_junk_2 =
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
105 typedef struct _sec_build
107 const IMAGE_SECTION_HEADER *sect_in[max_sections];
108 } sec_build;
110 typedef struct _sec_verify
112 const IMAGE_SECTION_HEADER *sect_out[max_sections];
113 DWORD length;
114 int rsrc_section;
115 DWORD NumberOfNamedEntries, NumberOfIdEntries;
116 } sec_verify;
118 static const struct _sec_variants
120 sec_build build;
121 sec_verify chk_none, chk_delete, chk_version, chk_bigdata;
122 } sec_variants[] =
124 /* .rsrc is the last section, data directory entry points to whole section */
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}
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 */
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}
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!) */
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}
151 /* .rsrc is the last section, data directory entry points to whole section, file size is not aligned on FileAlign */
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}
161 static int build_exe( const sec_build* sec_descr )
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];
171 memset( page, 0, sizeof page );
173 dos = (void*) page;
174 dos->e_magic = IMAGE_DOS_SIGNATURE;
175 dos->e_lfanew = sizeof *dos;
177 nt = (void*) &dos[1];
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;
185 opt = &nt->OptionalHeader;
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;
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;
203 opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress = rva_rsrc_start;
204 opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].Size = page_size;
206 sec = (void*) &nt[1];
208 file_size = 0;
209 for ( i = 0; i < max_sections; i++ )
210 if ( sec_descr->sect_in[i] )
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;
225 file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
226 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
228 /* write out the header */
229 WriteFile( file, page, sizeof page, &written, NULL );
231 /* write out zeroed pages for sections */
232 memset( page, 0, sizeof page );
233 for ( i = page_size; i < file_size; i += page_size )
235 DWORD size = min(page_size, file_size - i);
236 WriteFile( file, page, size, &written, NULL );
239 CloseHandle( file );
241 return 0;
244 static void update_missing_exe( void )
246 HANDLE res;
248 SetLastError(0xdeadbeef);
249 res = BeginUpdateResourceA( filename, TRUE );
250 GLE = GetLastError();
251 ok( res == NULL, "BeginUpdateResource should fail\n");
254 static void update_empty_exe( void )
256 HANDLE file, res, test;
257 BOOL r;
259 file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
260 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
262 CloseHandle( file );
264 res = BeginUpdateResourceA( filename, TRUE );
265 if ( res != NULL || GetLastError() != ERROR_FILE_INVALID )
267 ok( res != NULL, "BeginUpdateResource failed\n");
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");
273 CloseHandle( test );
275 r = EndUpdateResourceA( res, FALSE );
276 ok( r == FALSE, "EndUpdateResource failed\n");
278 else
279 skip( "Can't update resource in empty file\n" );
281 res = BeginUpdateResourceA( filename, FALSE );
282 ok( res == NULL, "BeginUpdateResource failed\n");
285 static void update_resources_none( void )
287 HMODULE res;
288 BOOL r;
290 res = BeginUpdateResourceA( filename, FALSE );
291 ok( res != NULL, "BeginUpdateResource failed\n");
293 r = EndUpdateResourceA( res, FALSE );
294 ok( r, "EndUpdateResource failed\n");
297 static void update_resources_delete( void )
299 HMODULE res;
300 BOOL r;
302 res = BeginUpdateResourceA( filename, TRUE );
303 ok( res != NULL, "BeginUpdateResource failed\n");
305 r = EndUpdateResourceA( res, FALSE );
306 ok( r, "EndUpdateResource failed\n");
309 static void update_resources_version( void )
311 HANDLE res = NULL;
312 BOOL r;
313 char foo[] = "red and white";
315 res = BeginUpdateResourceA( filename, TRUE );
316 ok( res != NULL, "BeginUpdateResource failed\n");
318 if (0) /* this causes subsequent tests to fail on Vista */
320 r = UpdateResourceA( res,
321 MAKEINTRESOURCEA(0x1230),
322 MAKEINTRESOURCEA(0x4567),
323 0xabcd,
324 NULL, 0 );
325 ok( r == FALSE, "UpdateResource failed\n");
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());
335 r = EndUpdateResourceA( res, FALSE );
336 ok( r, "EndUpdateResource failed: %d\n", GetLastError());
339 static void update_resources_bigdata( void )
341 HANDLE res = NULL;
342 BOOL r;
343 char foo[2*page_size] = "foobar";
345 res = BeginUpdateResourceA( filename, TRUE );
346 ok( res != NULL, "BeginUpdateResource succeeded\n");
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());
355 r = EndUpdateResourceA( res, FALSE );
356 ok( r, "EndUpdateResource failed\n");
359 static void check_exe( const sec_verify *verify )
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;
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());
373 length = GetFileSize( file, NULL );
374 ok( length >= verify->length, "file size wrong\n");
376 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 0, NULL );
377 ok (mapping != NULL, "failed to create file\n");
379 dos = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
380 ok( dos != NULL, "failed to map file\n");
382 if (!dos)
383 goto end;
385 nt = (void*) ((BYTE*) dos + dos->e_lfanew);
386 opt = &nt->OptionalHeader;
387 sec = (void*) &nt[1];
389 for(i = 0; i < max_sections; i++)
390 if (verify->sect_out[i])
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++;
400 ok( nt->FileHeader.NumberOfSections == sec_count, "number of sections wrong\n" );
402 if (verify->rsrc_section >= 0 && verify->rsrc_section < nt->FileHeader.NumberOfSections)
404 dir = (void*) ((BYTE*) dos + sec[verify->rsrc_section].VirtualAddress);
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");
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);
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);
422 end:
423 UnmapViewOfFile( dos );
425 CloseHandle( mapping );
427 CloseHandle( file );
430 static void test_find_resource(void)
432 HRSRC rsrc;
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" );
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() );
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() );
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() );
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() );
466 START_TEST(resource)
468 DWORD i;
470 DeleteFileA( filename );
471 update_missing_exe();
473 if (GLE == ERROR_CALL_NOT_IMPLEMENTED)
475 win_skip("Resource calls are not implemented\n");
476 return;
479 update_empty_exe();
481 for(i=0; i < sizeof( sec_variants ) / sizeof( sec_variants[0] ); i++)
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 );
495 test_find_resource();