d3dx9: Implement ID3DXBaseEffect::GetAnnotation().
[wine/multimedia.git] / dlls / kernel32 / tests / resource.c
bloba85e6d2daa110c6521f5c53c84c30adc96f599bf
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-0x4000) */
43 static const IMAGE_SECTION_HEADER sh_rodata_2 =
45 ".rodata", {3*page_size}, page_size, 3*page_size, page_size, 0, 0, 0, 0,
46 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
49 /* rodata @ [0x1000-0x2000) */
50 static const IMAGE_SECTION_HEADER sh_rodata_3 =
52 ".rodata", {page_size}, page_size, page_size, page_size, 0, 0, 0, 0,
53 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
56 /* rsrc @ [0x3000-0x4000) */
57 static const IMAGE_SECTION_HEADER sh_rsrc_1 =
59 ".rsrc\0\0", {page_size}, rva_rsrc_start, page_size, rva_rsrc_start, 0, 0, 0, 0,
60 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
63 /* rsrc @ [0x4000-0x5000) */
64 static const IMAGE_SECTION_HEADER sh_rsrc_2 =
66 ".rsrc\0\0", {page_size}, 4*page_size, page_size, 4*page_size, 0, 0, 0, 0,
67 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
70 /* rsrc @ [0x2000-0x4000) */
71 static const IMAGE_SECTION_HEADER sh_rsrc_3 =
73 ".rsrc\0\0", {2*page_size}, rva_rsrc_start-page_size, 2*page_size, rva_rsrc_start-page_size, 0, 0, 0, 0,
74 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
77 /* rsrc @ [0x2000-0x3000) */
78 static const IMAGE_SECTION_HEADER sh_rsrc_4 =
80 ".rsrc\0\0", {page_size}, rva_rsrc_start-page_size, page_size, rva_rsrc_start-page_size, 0, 0, 0, 0,
81 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
84 /* rsrc @ [0x3000-0x6000) */
85 static const IMAGE_SECTION_HEADER sh_rsrc_5 =
87 ".rsrc\0\0", {3*page_size}, rva_rsrc_start, 3*page_size, rva_rsrc_start, 0, 0, 0, 0,
88 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
91 /* rsrc @ [0x4000-0x7000) */
92 static const IMAGE_SECTION_HEADER sh_rsrc_6 =
94 ".rsrc\0\0", {3*page_size}, 4*page_size, 3*page_size, 4*page_size, 0, 0, 0, 0,
95 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
98 /* rsrc @ [0x2000-0x5000) */
99 static const IMAGE_SECTION_HEADER sh_rsrc_7 =
101 ".rsrc\0\0", {3*page_size}, 2*page_size, 3*page_size, 2*page_size, 0, 0, 0, 0,
102 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
105 /* rsrc @ [0x3000-0x4000), small SizeOfRawData */
106 static const IMAGE_SECTION_HEADER sh_rsrc_8 =
108 ".rsrc\0\0", {page_size}, rva_rsrc_start, 8, rva_rsrc_start, 0, 0, 0, 0,
109 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
112 /* reloc @ [0x4000-0x5000) */
113 static const IMAGE_SECTION_HEADER sh_junk =
115 ".reloc\0", {page_size}, 4*page_size, page_size, 4*page_size, 0, 0, 0, 0,
116 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
119 /* reloc @ [0x6000-0x7000) */
120 static const IMAGE_SECTION_HEADER sh_junk_2 =
122 ".reloc\0", {page_size}, 6*page_size, page_size, 6*page_size, 0, 0, 0, 0,
123 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
126 typedef struct _sec_build
128 const IMAGE_SECTION_HEADER *sect_in[max_sections];
129 } sec_build;
131 typedef struct _sec_verify
133 const IMAGE_SECTION_HEADER *sect_out[max_sections];
134 DWORD length;
135 int rsrc_section;
136 DWORD NumberOfNamedEntries, NumberOfIdEntries;
137 } sec_verify;
139 static const struct _sec_variants
141 sec_build build;
142 sec_verify chk_none, chk_delete, chk_version, chk_bigdata;
143 } sec_variants[] =
145 /* .rsrc is the last section, data directory entry points to whole section */
147 {{&sh_rodata_1, &sh_rsrc_1, NULL}},
148 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
149 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
150 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 1},
151 {{&sh_rodata_1, &sh_rsrc_5, NULL}, 6*page_size, 1, 0, 1}
153 /* single .rodata section with compatible characteristics, data directory entry points to section end */
154 /* Vista+ - existing section isn't used, new section is created at the end of file */
155 /* NT4/2000/2003 - image is broken */
156 #if 0
158 {{&sh_rodata_2, NULL, NULL}},
159 {{&sh_rodata_2, &sh_rsrc_2, NULL}, 5*page_size, 1, 0, 0},
160 {{&sh_rodata_2, &sh_rsrc_2, NULL}, 5*page_size, 1, 0, 0},
161 {{&sh_rodata_2, &sh_rsrc_2, NULL}, 5*page_size, 1, 0, 1},
162 {{&sh_rodata_2, &sh_rsrc_6, NULL}, 7*page_size, 1, 0, 1}
164 #endif
165 /* .rsrc is the last section, data directory entry points to section end */
166 /* Vista+ - resources are moved to section start (trashing data that could be there), and section is trimmed */
167 /* NT4/2000/2003 - resources are moved to section start (trashing data that could be there); section isn't trimmed */
169 {{&sh_rodata_3, &sh_rsrc_3, NULL}},
170 {{&sh_rodata_3, &sh_rsrc_4, NULL}, 3*page_size, 1, 0, 0},
171 {{&sh_rodata_3, &sh_rsrc_4, NULL}, 3*page_size, 1, 0, 0},
172 {{&sh_rodata_3, &sh_rsrc_4, NULL}, 3*page_size, 1, 0, 1},
173 {{&sh_rodata_3, &sh_rsrc_7, NULL}, 5*page_size, 1, 0, 1}
175 /* .rsrc is not the last section */
176 /* section is reused; sections after .rsrc are shifted to give space to rsrc (in-image offset and RVA!) */
178 {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}},
179 {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}, 5*page_size, 1, 0, 0},
180 {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}, 5*page_size, 1, 0, 0},
181 {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}, 5*page_size, 1, 0, 1},
182 {{&sh_rodata_1, &sh_rsrc_5, &sh_junk_2}, 7*page_size, 1, 0, 1}
184 /* .rsrc is the last section, data directory entry points to whole section, file size is not aligned on FileAlign */
186 {{&sh_rodata_1, &sh_rsrc_8, NULL}},
187 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
188 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 0},
189 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 1},
190 {{&sh_rodata_1, &sh_rsrc_5, NULL}, 6*page_size, 1, 0, 1}
194 static int build_exe( const sec_build* sec_descr )
196 IMAGE_DOS_HEADER *dos;
197 IMAGE_NT_HEADERS *nt;
198 IMAGE_SECTION_HEADER *sec;
199 IMAGE_OPTIONAL_HEADER *opt;
200 HANDLE file;
201 DWORD written, i, file_size;
202 BYTE page[page_size];
204 memset( page, 0, sizeof page );
206 dos = (void*) page;
207 dos->e_magic = IMAGE_DOS_SIGNATURE;
208 dos->e_lfanew = sizeof *dos;
210 nt = (void*) &dos[1];
212 nt->Signature = IMAGE_NT_SIGNATURE;
213 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
214 nt->FileHeader.NumberOfSections = 0;
215 nt->FileHeader.SizeOfOptionalHeader = sizeof nt->OptionalHeader;
216 nt->FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
218 opt = &nt->OptionalHeader;
220 opt->Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
221 opt->MajorLinkerVersion = 1;
222 opt->BaseOfCode = 0x10;
223 opt->ImageBase = 0x10000000;
224 opt->MajorOperatingSystemVersion = 4;
225 opt->MajorImageVersion = 1;
226 opt->MajorSubsystemVersion = 4;
227 opt->SizeOfHeaders = sizeof *dos + sizeof *nt + sizeof *sec * 2;
228 opt->SizeOfImage = page_size;
229 opt->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
231 /* if SectionAlignment and File alignment are not specified */
232 /* UpdateResource fails trying to create a huge temporary file */
233 opt->SectionAlignment = page_size;
234 opt->FileAlignment = page_size;
236 opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress = rva_rsrc_start;
237 opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].Size = page_size;
239 sec = (void*) &nt[1];
241 file_size = 0;
242 for ( i = 0; i < max_sections; i++ )
243 if ( sec_descr->sect_in[i] )
245 DWORD virt_end_of_section = sec_descr->sect_in[i]->Misc.VirtualSize +
246 sec_descr->sect_in[i]->VirtualAddress;
247 DWORD phys_end_of_section = sec_descr->sect_in[i]->SizeOfRawData +
248 sec_descr->sect_in[i]->PointerToRawData;
249 memcpy( sec + nt->FileHeader.NumberOfSections, sec_descr->sect_in[i],
250 sizeof(sec[0]) );
251 nt->FileHeader.NumberOfSections++;
252 if ( opt->SizeOfImage < virt_end_of_section )
253 opt->SizeOfImage = virt_end_of_section;
254 if ( file_size < phys_end_of_section )
255 file_size = phys_end_of_section;
258 file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
259 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
261 /* write out the header */
262 WriteFile( file, page, sizeof page, &written, NULL );
264 /* write out zeroed pages for sections */
265 memset( page, 0, sizeof page );
266 for ( i = page_size; i < file_size; i += page_size )
268 DWORD size = min(page_size, file_size - i);
269 WriteFile( file, page, size, &written, NULL );
272 CloseHandle( file );
274 return 0;
277 static void update_missing_exe( void )
279 HANDLE res;
281 SetLastError(0xdeadbeef);
282 res = BeginUpdateResource( filename, TRUE );
283 GLE = GetLastError();
284 ok( res == NULL, "BeginUpdateResource should fail\n");
287 static void update_empty_exe( void )
289 HANDLE file, res, test;
290 BOOL r;
292 file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
293 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
295 CloseHandle( file );
297 res = BeginUpdateResource( filename, TRUE );
298 if ( res != NULL || GetLastError() != ERROR_FILE_INVALID )
300 ok( res != NULL, "BeginUpdateResource failed\n");
302 /* check if it's possible to open the file now */
303 test = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
304 ok (test != INVALID_HANDLE_VALUE, "failed to create file\n");
306 CloseHandle( test );
308 r = EndUpdateResource( res, FALSE );
309 ok( r == FALSE, "EndUpdateResource failed\n");
311 else
312 skip( "Can't update resource in empty file\n" );
314 res = BeginUpdateResource( filename, FALSE );
315 ok( res == NULL, "BeginUpdateResource failed\n");
318 static void update_resources_none( void )
320 HMODULE res;
321 BOOL r;
323 res = BeginUpdateResource( filename, FALSE );
324 ok( res != NULL, "BeginUpdateResource failed\n");
326 r = EndUpdateResource( res, FALSE );
327 ok( r, "EndUpdateResource failed\n");
330 static void update_resources_delete( void )
332 HMODULE res;
333 BOOL r;
335 res = BeginUpdateResource( filename, TRUE );
336 ok( res != NULL, "BeginUpdateResource failed\n");
338 r = EndUpdateResource( res, FALSE );
339 ok( r, "EndUpdateResource failed\n");
342 static void update_resources_version( void )
344 HANDLE res = NULL;
345 BOOL r;
346 char foo[] = "red and white";
348 res = BeginUpdateResource( filename, TRUE );
349 ok( res != NULL, "BeginUpdateResource failed\n");
351 if (0) /* this causes subsequent tests to fail on Vista */
353 r = UpdateResource( res,
354 MAKEINTRESOURCE(0x1230),
355 MAKEINTRESOURCE(0x4567),
356 0xabcd,
357 NULL, 0 );
358 ok( r == FALSE, "UpdateResource failed\n");
361 r = UpdateResource( res,
362 MAKEINTRESOURCE(0x1230),
363 MAKEINTRESOURCE(0x4567),
364 0xabcd,
365 foo, sizeof foo );
366 ok( r == TRUE, "UpdateResource failed: %d\n", GetLastError());
368 r = EndUpdateResource( res, FALSE );
369 ok( r, "EndUpdateResource failed: %d\n", GetLastError());
372 static void update_resources_bigdata( void )
374 HANDLE res = NULL;
375 BOOL r;
376 char foo[2*page_size] = "foobar";
378 res = BeginUpdateResource( filename, TRUE );
379 ok( res != NULL, "BeginUpdateResource succeeded\n");
381 r = UpdateResource( res,
382 MAKEINTRESOURCE(0x3012),
383 MAKEINTRESOURCE(0x5647),
384 0xcdba,
385 foo, sizeof foo );
386 ok( r == TRUE, "UpdateResource failed: %d\n", GetLastError());
388 r = EndUpdateResource( res, FALSE );
389 ok( r, "EndUpdateResource failed\n");
392 static void check_exe( const sec_verify *verify )
394 int i;
395 IMAGE_DOS_HEADER *dos;
396 IMAGE_NT_HEADERS *nt;
397 IMAGE_SECTION_HEADER *sec;
398 IMAGE_RESOURCE_DIRECTORY *dir;
399 HANDLE file, mapping;
400 DWORD length, sec_count = 0;
402 file = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
403 ok (file != INVALID_HANDLE_VALUE, "failed to create file (%d)\n", GetLastError());
405 length = GetFileSize( file, NULL );
406 ok( length >= verify->length, "file size wrong\n");
408 mapping = CreateFileMapping( file, NULL, PAGE_READONLY, 0, 0, NULL );
409 ok (mapping != NULL, "failed to create file\n");
411 dos = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
412 ok( dos != NULL, "failed to map file\n");
414 if (!dos)
415 goto end;
417 nt = (void*) ((BYTE*) dos + dos->e_lfanew);
418 sec = (void*) &nt[1];
420 for(i = 0; i < max_sections; i++)
421 if (verify->sect_out[i])
423 ok( !memcmp(&verify->sect_out[i]->Name, &sec[sec_count].Name, 8), "section %d name wrong\n", sec_count);
424 ok( verify->sect_out[i]->VirtualAddress == sec[sec_count].VirtualAddress, "section %d vaddr wrong\n", sec_count);
425 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);
426 ok( verify->sect_out[i]->PointerToRawData == sec[sec_count].PointerToRawData, "section %d PointerToRawData wrong\n", sec_count);
427 ok( verify->sect_out[i]->Characteristics == sec[sec_count].Characteristics , "section %d characteristics wrong\n", sec_count);
428 sec_count++;
431 ok( nt->FileHeader.NumberOfSections == sec_count, "number of sections wrong\n" );
433 if (verify->rsrc_section >= 0 && verify->rsrc_section < nt->FileHeader.NumberOfSections)
435 dir = (void*) ((BYTE*) dos + sec[verify->rsrc_section].VirtualAddress);
437 ok( dir->Characteristics == 0, "Characteristics wrong\n");
438 ok( dir->TimeDateStamp == 0 || abs( dir->TimeDateStamp - GetTickCount() ) < 1000 /* nt4 */,
439 "TimeDateStamp wrong %u\n", dir->TimeDateStamp);
440 ok( dir->MajorVersion == 4, "MajorVersion wrong\n");
441 ok( dir->MinorVersion == 0, "MinorVersion wrong\n");
443 ok( dir->NumberOfNamedEntries == verify->NumberOfNamedEntries, "NumberOfNamedEntries should be %d instead of %d\n",
444 verify->NumberOfNamedEntries, dir->NumberOfNamedEntries);
445 ok( dir->NumberOfIdEntries == verify->NumberOfIdEntries, "NumberOfIdEntries should be %d instead of %d\n",
446 verify->NumberOfIdEntries, dir->NumberOfIdEntries);
449 end:
450 UnmapViewOfFile( dos );
452 CloseHandle( mapping );
454 CloseHandle( file );
457 static void test_find_resource(void)
459 HRSRC rsrc;
461 rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(1), (LPCWSTR)RT_MENU );
462 ok( rsrc != 0, "resource not found\n" );
463 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
464 MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ));
465 ok( rsrc != 0, "resource not found\n" );
466 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
467 MAKELANGID( LANG_GERMAN, SUBLANG_DEFAULT ));
468 ok( rsrc != 0, "resource not found\n" );
470 SetLastError( 0xdeadbeef );
471 rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(1), (LPCWSTR)RT_DIALOG );
472 ok( !rsrc, "resource found\n" );
473 ok( GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND, "wrong error %u\n", GetLastError() );
475 SetLastError( 0xdeadbeef );
476 rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(2), (LPCWSTR)RT_MENU );
477 ok( !rsrc, "resource found\n" );
478 ok( GetLastError() == ERROR_RESOURCE_NAME_NOT_FOUND, "wrong error %u\n", GetLastError() );
480 SetLastError( 0xdeadbeef );
481 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
482 MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
483 ok( !rsrc, "resource found\n" );
484 ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
486 SetLastError( 0xdeadbeef );
487 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
488 MAKELANGID( LANG_FRENCH, SUBLANG_DEFAULT ) );
489 ok( !rsrc, "resource found\n" );
490 ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
493 START_TEST(resource)
495 DWORD i;
497 DeleteFile( filename );
498 update_missing_exe();
500 if (GLE == ERROR_CALL_NOT_IMPLEMENTED)
502 win_skip("Resource calls are not implemented\n");
503 return;
506 update_empty_exe();
508 for(i=0; i < sizeof( sec_variants ) / sizeof( sec_variants[0] ); i++)
510 const struct _sec_variants *sec = &sec_variants[i];
511 build_exe( &sec->build );
512 update_resources_none();
513 check_exe( &sec->chk_none );
514 update_resources_delete();
515 check_exe( &sec->chk_delete );
516 update_resources_version();
517 check_exe( &sec->chk_version );
518 update_resources_bigdata();
519 check_exe( &sec->chk_bigdata );
520 DeleteFile( filename );
522 test_find_resource();