Reactos
at master 875 lines 34 kB view raw
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 26static const char filename[] = "test_.exe"; 27static const WCHAR filenameW[] = {'t','e','s','t','_','.','e','x','e',0}; 28static DWORD GLE; 29 30enum constants { 31 page_size = 0x1000, 32 rva_rsrc_start = page_size * 3, 33 max_sections = 3 34}; 35 36/* rodata @ [0x1000-0x3000) */ 37static const IMAGE_SECTION_HEADER sh_rodata_1 = 38{ 39 ".rodata", {2*page_size}, page_size, 2*page_size, page_size, 0, 0, 0, 0, 40 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 41}; 42 43/* rodata @ [0x1000-0x2000) */ 44static const IMAGE_SECTION_HEADER sh_rodata_2 = 45{ 46 ".rodata", {page_size}, page_size, page_size, page_size, 0, 0, 0, 0, 47 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 48}; 49 50/* rsrc @ [0x3000-0x4000) */ 51static const IMAGE_SECTION_HEADER sh_rsrc_1 = 52{ 53 ".rsrc\0\0", {page_size}, rva_rsrc_start, page_size, rva_rsrc_start, 0, 0, 0, 0, 54 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 55}; 56 57/* rsrc @ [0x2000-0x4000) */ 58static const IMAGE_SECTION_HEADER sh_rsrc_2 = 59{ 60 ".rsrc\0\0", {2*page_size}, rva_rsrc_start-page_size, 2*page_size, rva_rsrc_start-page_size, 0, 0, 0, 0, 61 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 62}; 63 64/* rsrc @ [0x2000-0x3000) */ 65static const IMAGE_SECTION_HEADER sh_rsrc_3 = 66{ 67 ".rsrc\0\0", {page_size}, rva_rsrc_start-page_size, page_size, rva_rsrc_start-page_size, 0, 0, 0, 0, 68 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 69}; 70 71/* rsrc @ [0x3000-0x6000) */ 72static const IMAGE_SECTION_HEADER sh_rsrc_4 = 73{ 74 ".rsrc\0\0", {3*page_size}, rva_rsrc_start, 3*page_size, rva_rsrc_start, 0, 0, 0, 0, 75 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 76}; 77 78/* rsrc @ [0x2000-0x5000) */ 79static const IMAGE_SECTION_HEADER sh_rsrc_5 = 80{ 81 ".rsrc\0\0", {3*page_size}, 2*page_size, 3*page_size, 2*page_size, 0, 0, 0, 0, 82 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 83}; 84 85/* rsrc @ [0x3000-0x4000), small SizeOfRawData */ 86static const IMAGE_SECTION_HEADER sh_rsrc_6 = 87{ 88 ".rsrc\0\0", {page_size}, rva_rsrc_start, 8, rva_rsrc_start, 0, 0, 0, 0, 89 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 90}; 91 92/* reloc @ [0x4000-0x5000) */ 93static const IMAGE_SECTION_HEADER sh_junk = 94{ 95 ".reloc\0", {page_size}, 4*page_size, page_size, 4*page_size, 0, 0, 0, 0, 96 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 97}; 98 99/* reloc @ [0x6000-0x7000) */ 100static const IMAGE_SECTION_HEADER sh_junk_2 = 101{ 102 ".reloc\0", {page_size}, 6*page_size, page_size, 6*page_size, 0, 0, 0, 0, 103 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ 104}; 105 106typedef struct _sec_build 107{ 108 const IMAGE_SECTION_HEADER *sect_in[max_sections]; 109} sec_build; 110 111typedef struct _sec_verify 112{ 113 const IMAGE_SECTION_HEADER *sect_out[max_sections]; 114 DWORD length; 115 int rsrc_section; 116 DWORD NumberOfNamedEntries, NumberOfIdEntries; 117} sec_verify; 118 119static const struct _sec_variants 120{ 121 sec_build build; 122 sec_verify chk_none, chk_delete, chk_version, chk_bigdata; 123} sec_variants[] = 124{ 125 /* .rsrc is the last section, data directory entry points to whole section */ 126 { 127 {{&sh_rodata_1, &sh_rsrc_1, NULL}}, 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, 0}, 130 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 1}, 131 {{&sh_rodata_1, &sh_rsrc_4, NULL}, 6*page_size, 1, 0, 1} 132 }, 133 /* .rsrc is the last section, data directory entry points to section end */ 134 /* Vista+ - resources are moved to section start (trashing data that could be there), and section is trimmed */ 135 /* NT4/2000/2003 - resources are moved to section start (trashing data that could be there); section isn't trimmed */ 136 { 137 {{&sh_rodata_2, &sh_rsrc_2, NULL}}, 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, 0}, 140 {{&sh_rodata_2, &sh_rsrc_3, NULL}, 3*page_size, 1, 0, 1}, 141 {{&sh_rodata_2, &sh_rsrc_5, NULL}, 5*page_size, 1, 0, 1} 142 }, 143 /* .rsrc is not the last section */ 144 /* section is reused; sections after .rsrc are shifted to give space to rsrc (in-image offset and RVA!) */ 145 { 146 {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}}, 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, 0}, 149 {{&sh_rodata_1, &sh_rsrc_1, &sh_junk}, 5*page_size, 1, 0, 1}, 150 {{&sh_rodata_1, &sh_rsrc_4, &sh_junk_2}, 7*page_size, 1, 0, 1} 151 }, 152 /* .rsrc is the last section, data directory entry points to whole section, file size is not aligned on FileAlign */ 153 { 154 {{&sh_rodata_1, &sh_rsrc_6, NULL}}, 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, 0}, 157 {{&sh_rodata_1, &sh_rsrc_1, NULL}, 4*page_size, 1, 0, 1}, 158 {{&sh_rodata_1, &sh_rsrc_4, NULL}, 6*page_size, 1, 0, 1} 159 } 160}; 161 162static int build_exe( const sec_build* sec_descr ) 163{ 164 IMAGE_DOS_HEADER *dos; 165 IMAGE_NT_HEADERS *nt; 166 IMAGE_SECTION_HEADER *sec; 167 IMAGE_OPTIONAL_HEADER *opt; 168 HANDLE file; 169 DWORD written, i, file_size; 170 BYTE page[page_size]; 171 172 memset( page, 0, sizeof page ); 173 174 dos = (void*) page; 175 dos->e_magic = IMAGE_DOS_SIGNATURE; 176 dos->e_lfanew = sizeof *dos; 177 178 nt = (void*) &dos[1]; 179 180 nt->Signature = IMAGE_NT_SIGNATURE; 181 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386; 182 nt->FileHeader.NumberOfSections = 0; 183 nt->FileHeader.SizeOfOptionalHeader = sizeof nt->OptionalHeader; 184 nt->FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL; 185 186 opt = &nt->OptionalHeader; 187 188 opt->Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC; 189 opt->MajorLinkerVersion = 1; 190 opt->BaseOfCode = 0x10; 191 opt->ImageBase = 0x10000000; 192 opt->MajorOperatingSystemVersion = 4; 193 opt->MajorImageVersion = 1; 194 opt->MajorSubsystemVersion = 4; 195 opt->SizeOfHeaders = sizeof *dos + sizeof *nt + sizeof *sec * 2; 196 opt->SizeOfImage = page_size; 197 opt->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI; 198 199 /* if SectionAlignment and File alignment are not specified */ 200 /* UpdateResource fails trying to create a huge temporary file */ 201 opt->SectionAlignment = page_size; 202 opt->FileAlignment = page_size; 203 204 opt->NumberOfRvaAndSizes = IMAGE_FILE_RESOURCE_DIRECTORY + 1; 205 opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress = rva_rsrc_start; 206 opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].Size = page_size; 207 208 sec = (void*) &nt[1]; 209 210 file_size = 0; 211 for ( i = 0; i < max_sections; i++ ) 212 if ( sec_descr->sect_in[i] ) 213 { 214 DWORD virt_end_of_section = sec_descr->sect_in[i]->Misc.VirtualSize + 215 sec_descr->sect_in[i]->VirtualAddress; 216 DWORD phys_end_of_section = sec_descr->sect_in[i]->SizeOfRawData + 217 sec_descr->sect_in[i]->PointerToRawData; 218 memcpy( sec + nt->FileHeader.NumberOfSections, sec_descr->sect_in[i], 219 sizeof(sec[0]) ); 220 nt->FileHeader.NumberOfSections++; 221 if ( opt->SizeOfImage < virt_end_of_section ) 222 opt->SizeOfImage = virt_end_of_section; 223 if ( file_size < phys_end_of_section ) 224 file_size = phys_end_of_section; 225 } 226 227 file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0); 228 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n"); 229 230 /* write out the header */ 231 WriteFile( file, page, sizeof page, &written, NULL ); 232 233 /* write out zeroed pages for sections */ 234 memset( page, 0, sizeof page ); 235 for ( i = page_size; i < file_size; i += page_size ) 236 { 237 DWORD size = min(page_size, file_size - i); 238 WriteFile( file, page, size, &written, NULL ); 239 } 240 241 CloseHandle( file ); 242 243 return 0; 244} 245 246static void update_missing_exe( void ) 247{ 248 HANDLE res; 249 250 SetLastError(0xdeadbeef); 251 res = BeginUpdateResourceA( filename, TRUE ); 252 GLE = GetLastError(); 253 ok( res == NULL, "BeginUpdateResource should fail\n"); 254} 255 256static void update_empty_exe( void ) 257{ 258 HANDLE file, res, test; 259 BOOL r; 260 261 file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0); 262 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n"); 263 264 CloseHandle( file ); 265 266 res = BeginUpdateResourceA( filename, TRUE ); 267 if ( res != NULL || GetLastError() != ERROR_FILE_INVALID ) 268 { 269 ok( res != NULL, "BeginUpdateResource failed\n"); 270 271 /* check if it's possible to open the file now */ 272 test = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0); 273 ok (test != INVALID_HANDLE_VALUE, "failed to create file\n"); 274 275 CloseHandle( test ); 276 277 r = EndUpdateResourceA( res, FALSE ); 278 ok( r == FALSE, "EndUpdateResource failed\n"); 279 } 280 else 281 skip( "Can't update resource in empty file\n" ); 282 283 res = BeginUpdateResourceA( filename, FALSE ); 284 ok( res == NULL, "BeginUpdateResource failed\n"); 285} 286 287static void update_resources_none( void ) 288{ 289 HMODULE res; 290 BOOL r; 291 292 res = BeginUpdateResourceA( filename, FALSE ); 293 ok( res != NULL, "BeginUpdateResource failed\n"); 294 295 r = EndUpdateResourceA( res, FALSE ); 296 ok( r, "EndUpdateResource failed\n"); 297} 298 299static void update_resources_delete( void ) 300{ 301 HMODULE res; 302 BOOL r; 303 304 res = BeginUpdateResourceA( filename, TRUE ); 305 ok( res != NULL, "BeginUpdateResource failed\n"); 306 307 r = EndUpdateResourceA( res, FALSE ); 308 ok( r, "EndUpdateResource failed\n"); 309} 310 311static void update_resources_version( void ) 312{ 313 HANDLE res = NULL; 314 BOOL r; 315 char foo[] = "red and white"; 316 317 res = BeginUpdateResourceA( filename, TRUE ); 318 ok( res != NULL, "BeginUpdateResource failed\n"); 319 320 if (0) /* this causes subsequent tests to fail on Vista */ 321 { 322 r = UpdateResourceA( res, 323 MAKEINTRESOURCEA(0x1230), 324 MAKEINTRESOURCEA(0x4567), 325 0xabcd, 326 NULL, 0 ); 327 ok( r == FALSE, "UpdateResource failed\n"); 328 } 329 330 r = UpdateResourceA( res, 331 MAKEINTRESOURCEA(0x1230), 332 MAKEINTRESOURCEA(0x4567), 333 0xabcd, 334 foo, sizeof foo ); 335 ok( r == TRUE, "UpdateResource failed: %ld\n", GetLastError()); 336 337 r = EndUpdateResourceA( res, FALSE ); 338 ok( r, "EndUpdateResource failed: %ld\n", GetLastError()); 339} 340 341static void update_resources_bigdata( void ) 342{ 343 HANDLE res = NULL; 344 BOOL r; 345 char foo[2*page_size] = "foobar"; 346 347 res = BeginUpdateResourceA( filename, TRUE ); 348 ok( res != NULL, "BeginUpdateResource succeeded\n"); 349 350 r = UpdateResourceA( res, 351 MAKEINTRESOURCEA(0x3012), 352 MAKEINTRESOURCEA(0x5647), 353 0xcdba, 354 foo, sizeof foo ); 355 ok( r == TRUE, "UpdateResource failed: %ld\n", GetLastError()); 356 357 r = EndUpdateResourceA( res, FALSE ); 358 ok( r, "EndUpdateResource failed\n"); 359} 360 361static void update_resources_name( void ) 362{ 363 char foo[] = "resource data", res_name[] = "name", res_type[] = "type"; 364 HANDLE res = NULL; 365 HMODULE module; 366 HRSRC rsrc; 367 BOOL ret; 368 369 res = BeginUpdateResourceA( filename, TRUE ); 370 ok( res != NULL, "BeginUpdateResource failed: %lu\n", GetLastError() ); 371 if ( !res ) return; 372 373 ret = UpdateResourceA( res, res_type, res_name, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), foo, sizeof(foo) ); 374 ok( ret == TRUE, "UpdateResource failed: %lu\n", GetLastError() ); 375 376 ret = EndUpdateResourceA( res, FALSE ); 377 ok( ret, "EndUpdateResource failed: %lu\n", GetLastError() ); 378 if ( !ret ) return; 379 380 module = LoadLibraryExA( filename, NULL, LOAD_LIBRARY_AS_DATAFILE ); 381 ok( module != NULL, "LoadLibraryEx failed: %lu\n", GetLastError() ); 382 if ( !module ) return; 383 384 rsrc = FindResourceA( module, res_name, res_type ); 385 ok( rsrc != NULL || 386 broken( GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND ) /* win2008 */, 387 "FindResource failed: %lu\n", GetLastError() ); 388 389 ret = FreeLibrary(module); 390 ok( ret, "FreeLibrary failed: %lu\n", GetLastError() ); 391} 392 393static void check_exe( const sec_verify *verify ) 394{ 395 int i; 396 IMAGE_DOS_HEADER *dos; 397 IMAGE_NT_HEADERS *nt; 398 IMAGE_OPTIONAL_HEADER *opt; 399 IMAGE_SECTION_HEADER *sec; 400 IMAGE_RESOURCE_DIRECTORY *dir; 401 HANDLE file, mapping; 402 DWORD length, sec_count = 0; 403 404 file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0); 405 ok (file != INVALID_HANDLE_VALUE, "failed to create file (%ld)\n", GetLastError()); 406 407 length = GetFileSize( file, NULL ); 408 ok( length >= verify->length, "file size wrong\n"); 409 410 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 0, NULL ); 411 ok (mapping != NULL, "failed to create file\n"); 412 413 dos = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length ); 414 ok( dos != NULL, "failed to map file\n"); 415 416 if (!dos) 417 goto end; 418 419 nt = (void*) ((BYTE*) dos + dos->e_lfanew); 420 opt = &nt->OptionalHeader; 421 sec = (void*) &nt[1]; 422 423 for(i = 0; i < max_sections; i++) 424 if (verify->sect_out[i]) 425 { 426 ok( !memcmp(&verify->sect_out[i]->Name, &sec[sec_count].Name, 8), "section %ld name wrong\n", sec_count); 427 ok( verify->sect_out[i]->VirtualAddress == sec[sec_count].VirtualAddress, "section %ld vaddr wrong\n", sec_count); 428 ok( verify->sect_out[i]->SizeOfRawData <= sec[sec_count].SizeOfRawData, "section %ld SizeOfRawData wrong (%ld vs %ld)\n", sec_count, verify->sect_out[i]->SizeOfRawData ,sec[sec_count].SizeOfRawData); 429 ok( verify->sect_out[i]->PointerToRawData == sec[sec_count].PointerToRawData, "section %ld PointerToRawData wrong\n", sec_count); 430 ok( verify->sect_out[i]->Characteristics == sec[sec_count].Characteristics , "section %ld characteristics wrong\n", sec_count); 431 sec_count++; 432 } 433 434 ok( nt->FileHeader.NumberOfSections == sec_count, "number of sections wrong\n" ); 435 436 if (verify->rsrc_section >= 0 && verify->rsrc_section < nt->FileHeader.NumberOfSections) 437 { 438 dir = (void*) ((BYTE*) dos + sec[verify->rsrc_section].VirtualAddress); 439 440 ok( dir->Characteristics == 0, "Characteristics wrong\n"); 441 ok( dir->TimeDateStamp == 0, "TimeDateStamp wrong %lu\n", dir->TimeDateStamp); 442 ok( dir->MajorVersion == 4, "MajorVersion wrong\n"); 443 ok( dir->MinorVersion == 0, "MinorVersion wrong\n"); 444 445 ok( dir->NumberOfNamedEntries == verify->NumberOfNamedEntries, "NumberOfNamedEntries should be %ld instead of %d\n", 446 verify->NumberOfNamedEntries, dir->NumberOfNamedEntries); 447 ok( dir->NumberOfIdEntries == verify->NumberOfIdEntries, "NumberOfIdEntries should be %ld instead of %d\n", 448 verify->NumberOfIdEntries, dir->NumberOfIdEntries); 449 450 ok(opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress == sec[verify->rsrc_section].VirtualAddress, 451 "VirtualAddress in optional header should be %ld instead of %ld\n", 452 sec[verify->rsrc_section].VirtualAddress, opt->DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY].VirtualAddress); 453 } 454 455end: 456 UnmapViewOfFile( dos ); 457 458 CloseHandle( mapping ); 459 460 CloseHandle( file ); 461} 462 463static void test_find_resource(void) 464{ 465 HRSRC rsrc; 466 467 rsrc = FindResourceW( GetModuleHandleW(NULL), MAKEINTRESOURCEW(1), (LPCWSTR)RT_MENU ); 468 ok( rsrc != 0, "resource not found\n" ); 469 rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1), 470 MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL )); 471 ok( rsrc != 0, "resource not found\n" ); 472 rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1), 473 MAKELANGID( LANG_GERMAN, SUBLANG_DEFAULT )); 474 ok( rsrc != 0, "resource not found\n" ); 475 476 SetLastError( 0xdeadbeef ); 477 rsrc = FindResourceW( GetModuleHandleW(NULL), MAKEINTRESOURCEW(1), (LPCWSTR)RT_DIALOG ); 478 ok( !rsrc, "resource found\n" ); 479 ok( GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND, "wrong error %lu\n", GetLastError() ); 480 481 SetLastError( 0xdeadbeef ); 482 rsrc = FindResourceW( GetModuleHandleW(NULL), MAKEINTRESOURCEW(2), (LPCWSTR)RT_MENU ); 483 ok( !rsrc, "resource found\n" ); 484 ok( GetLastError() == ERROR_RESOURCE_NAME_NOT_FOUND, "wrong error %lu\n", GetLastError() ); 485 486 SetLastError( 0xdeadbeef ); 487 rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1), 488 MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) ); 489 ok( !rsrc, "resource found\n" ); 490 ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %lu\n", GetLastError() ); 491 492 SetLastError( 0xdeadbeef ); 493 rsrc = FindResourceExW( GetModuleHandleW(NULL), (LPCWSTR)RT_MENU, MAKEINTRESOURCEW(1), 494 MAKELANGID( LANG_FRENCH, SUBLANG_DEFAULT ) ); 495 ok( !rsrc, "resource found\n" ); 496 ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %lu\n", GetLastError() ); 497} 498 499typedef struct 500{ 501 void *unknown[6]; 502 HGLOBAL pFileName; 503} QUEUEDUPDATES; 504 505static void test_internal_structure(void) 506{ 507 HANDLE res; 508 QUEUEDUPDATES *res_data; 509 WCHAR *res_filenameW; 510 511 res = BeginUpdateResourceW( filenameW, FALSE ); 512 ok( res != NULL, "BeginUpdateResourceW failed\n" ); 513 res_data = GlobalLock(res); 514 ok( res_data != NULL, "GlobalLock failed\n" ); 515 res_filenameW = GlobalLock( res_data->pFileName ); 516 ok( res_filenameW != NULL, "GlobalLock for res_filenameW failed\n" ); 517 ok( !lstrcmpW( res_filenameW, filenameW ), "Filename fields do not match\n" ); 518 ok( GlobalUnlock( res_filenameW ), "GlobalUnlock res_filenamed failed\n" ); 519 ok( GlobalUnlock( res_data ), "GlobalUnlock res_data failed\n" ); 520 ok( EndUpdateResourceW( res, TRUE ), "EndUpdateResourceW failed\n"); 521} 522 523static const struct 524{ 525 IMAGE_DOS_HEADER dos; 526 IMAGE_NT_HEADERS nt; 527 IMAGE_SECTION_HEADER section; 528} dll_image = 529{ 530 { IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 }, 0, 0, { 0 }, 531 sizeof(IMAGE_DOS_HEADER) }, 532 { 533 IMAGE_NT_SIGNATURE, /* Signature */ 534 { 535#if defined __i386__ 536 IMAGE_FILE_MACHINE_I386, /* Machine */ 537#elif defined __x86_64__ 538 IMAGE_FILE_MACHINE_AMD64, /* Machine */ 539#elif defined __arm__ 540 IMAGE_FILE_MACHINE_ARMNT, /* Machine */ 541#elif defined __aarch64__ 542 IMAGE_FILE_MACHINE_ARM64, /* Machine */ 543#else 544# error You must specify the machine type 545#endif 546 1, /* NumberOfSections */ 547 0, /* TimeDateStamp */ 548 0, /* PointerToSymbolTable */ 549 0, /* NumberOfSymbols */ 550 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */ 551 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */ 552 }, 553 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */ 554 1, /* MajorLinkerVersion */ 555 0, /* MinorLinkerVersion */ 556 0, /* SizeOfCode */ 557 0, /* SizeOfInitializedData */ 558 0, /* SizeOfUninitializedData */ 559 0, /* AddressOfEntryPoint */ 560 0x1000, /* BaseOfCode */ 561#ifndef _WIN64 562 0, /* BaseOfData */ 563#endif 564 0x10000000, /* ImageBase */ 565 0x1000, /* SectionAlignment */ 566 0x1000, /* FileAlignment */ 567 4, /* MajorOperatingSystemVersion */ 568 0, /* MinorOperatingSystemVersion */ 569 1, /* MajorImageVersion */ 570 0, /* MinorImageVersion */ 571 4, /* MajorSubsystemVersion */ 572 0, /* MinorSubsystemVersion */ 573 0, /* Win32VersionValue */ 574 0x3000, /* SizeOfImage */ 575 sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS), 576 0, /* CheckSum */ 577 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */ 578 IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE | IMAGE_DLLCHARACTERISTICS_NX_COMPAT, /* DllCharacteristics */ 579 0, /* SizeOfStackReserve */ 580 0, /* SizeOfStackCommit */ 581 0, /* SizeOfHeapReserve */ 582 0, /* SizeOfHeapCommit */ 583 0, /* LoaderFlags */ 584 IMAGE_FILE_RESOURCE_DIRECTORY + 1, /* NumberOfRvaAndSizes */ 585 { { 0 }, { 0 }, { 0x1000, 0x1000 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */ 586 } 587 }, 588 { ".rsrc\0\0", { 0 }, 0x1000, 0x1000, 0, 0, 0, 0, 0, 589 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ } 590}; 591 592static void create_test_dll( const WCHAR *name ) 593{ 594 DWORD dummy; 595 HANDLE handle = CreateFileW( name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0 ); 596 597 ok( handle != INVALID_HANDLE_VALUE, "failed to create file err %lu\n", GetLastError() ); 598 WriteFile( handle, &dll_image, sizeof(dll_image), &dummy, NULL ); 599 SetFilePointer( handle, dll_image.nt.OptionalHeader.SizeOfImage, NULL, FILE_BEGIN ); 600 SetEndOfFile( handle ); 601 CloseHandle( handle ); 602} 603 604static struct mui_res 605{ 606 DWORD signature; 607 DWORD size; 608 DWORD version; 609 DWORD path_type; 610 DWORD file_type; 611 DWORD system_attributes; 612 DWORD fallback_location; 613 BYTE service_checksum[16]; 614 BYTE checksum[16]; 615 DWORD unk1[2]; 616 DWORD mui_path_off; 617 DWORD mui_path_size; 618 DWORD unk2[2]; 619 DWORD ln_type_name_off; 620 DWORD ln_type_name_size; 621 DWORD ln_type_id_off; 622 DWORD ln_type_id_size; 623 DWORD mui_type_name_off; 624 DWORD mui_type_name_size; 625 DWORD mui_type_id_off; 626 DWORD mui_type_id_size; 627 DWORD lang_off; 628 DWORD lang_size; 629 DWORD fallback_lang_off; 630 DWORD fallback_lang_size; 631 WCHAR ln_type_names[8]; 632 DWORD ln_type_ids[1]; 633 WCHAR mui_type_names[8]; 634 DWORD mui_type_ids[1]; 635 WCHAR lang[8]; 636 WCHAR fallback_lang[8]; 637} ln_mui_res = { 638 0xfecdfecd, sizeof(ln_mui_res), 0x10000, 0, 639 MUI_FILETYPE_LANGUAGE_NEUTRAL_MAIN >> 1, 640 0, 0, {'s','c'}, {'c'}, {0}, 0, 0, {0}, 641 offsetof(struct mui_res, ln_type_names), sizeof(L"MUI\0"), 642 offsetof(struct mui_res, ln_type_ids), sizeof(ln_mui_res.ln_type_ids), 643 offsetof(struct mui_res, mui_type_names), sizeof(L"MUI\0"), 644 offsetof(struct mui_res, mui_type_ids), sizeof(ln_mui_res.mui_type_ids), 0, 0, 645 offsetof(struct mui_res, fallback_lang), sizeof(L"en-US"), 646 {'M','U','I',0,0}, {RT_CURSOR}, {'M','U','I',0,0}, {RT_STRING}, {0}, {'e','n','-','U','S',0}, 647}, en_mui_res = { 648 0xfecdfecd, sizeof(ln_mui_res), 0x10000, 0, 649 MUI_FILETYPE_LANGUAGE_NEUTRAL_MUI >> 1, 650 0, 0, {'s','c'}, {'c'}, {0}, 0, 0, {0}, 651 offsetof(struct mui_res, ln_type_names), sizeof(L"MUI\0"), 652 offsetof(struct mui_res, ln_type_ids), sizeof(ln_mui_res.ln_type_ids), 653 0, 0, 0, 0, offsetof(struct mui_res, lang), sizeof(L"en-US"), 0, 0, 654 {'M','U','I',0,0}, {RT_STRING}, {0}, {0}, {'e','n','-','U','S',0} 655}; 656 657static void test_mui(void) 658{ 659#if defined(__REACTOS__) && DLL_EXPORT_VERSION < 0x600 660 skip("Cannot build test_mui() unless DLL_EXPORT_VERSION >= 0x600.\n"); 661#else 662 static const WCHAR ln_dll[] = L"test_mui.dll"; 663 static const WCHAR en_dll[] = L"en-US\\test_mui.dll.mui"; 664 static const BYTE zeros[16] = { 0 }; 665 BYTE buf[1024]; 666 FILEMUIINFO *info = (FILEMUIINFO *)buf; 667 const WCHAR *str; 668 DWORD size, *id; 669 HANDLE res; 670 BOOL r; 671 672 size = 0; 673 r = GetFileMUIInfo( 0, ln_dll, NULL, &size); 674 ok( !r, "GetFileMUIInfo succeeded\n" ); 675 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %ld\n", GetLastError() ); 676 677 create_test_dll( ln_dll ); 678 CreateDirectoryW( L"en-US", NULL ); 679 create_test_dll( en_dll ); 680 681 size = 0; 682 r = GetFileMUIInfo( 0, ln_dll, NULL, &size ); 683 ok( r, "GetFileMUIInfo failed: %ld\n", GetLastError() ); 684 ok( size == sizeof(*info), "unexpected size: %ld\n", size ); 685 686 memset( buf, 0xfe, sizeof(buf) ); 687 size = sizeof(buf); 688 info->dwSize = sizeof(buf); 689 info->dwVersion = 0; 690 r = GetFileMUIInfo( 0, ln_dll, info, &size ); 691 ok( !r, "GetFileMUIInfo succeeded\n" ); 692 ok( GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError() = %ld\n", GetLastError() ); 693 ok( !size, "size = %ld\n", size ); 694 695 size = sizeof(buf); 696 info->dwVersion = MUI_FILEINFO_VERSION; 697 r = GetFileMUIInfo( 0, ln_dll, info, &size ); 698 ok( r, "GetFileMUIInfo failed: %ld\n", GetLastError() ); 699 ok( info->dwSize == sizeof(buf), "dwSize = %ld\n", info->dwSize ); 700 ok( info->dwVersion == MUI_FILEINFO_VERSION, "dwVersion = %ld\n", info->dwVersion ); 701 ok( info->dwFileType == MUI_FILETYPE_NOT_LANGUAGE_NEUTRAL, "dwFileType = %ld\n", info->dwFileType ); 702 ok( !memcmp(info->pChecksum, zeros, sizeof(info->pChecksum)), "pChecksum = %s\n", 703 wine_dbgstr_an((char *)info->pChecksum, sizeof(info->pChecksum)) ); 704 ok( !memcmp(info->pServiceChecksum, zeros, sizeof(info->pServiceChecksum)), "pServiceChecksum = %s\n", 705 wine_dbgstr_an((char *)info->pServiceChecksum, sizeof(info->pServiceChecksum)) ); 706 ok( !info->dwLanguageNameOffset, "dwLanguageNameOffset = %ld\n", info->dwLanguageNameOffset ); 707 ok( !info->dwTypeIDMainSize, "dwTypeIDMainSize = %ld\n", info->dwTypeIDMainSize ); 708 ok( !info->dwTypeIDMainOffset, "dwTypeIDMainOffset = %ld\n", info->dwTypeIDMainOffset ); 709 ok( !info->dwTypeNameMainOffset, "dwTypeNameMainOffset = %ld\n", info->dwTypeNameMainOffset ); 710 ok( !info->dwTypeIDMUISize, "dwTypeIDMUISize = %ld\n", info->dwTypeIDMUISize ); 711 ok( !info->dwTypeIDMUIOffset, "dwTypeIDMUIOffset = %ld\n", info->dwTypeIDMUIOffset ); 712 ok( !info->dwTypeNameMUIOffset, "dwTypeNameMUIOffset = %ld\n", info->dwTypeNameMUIOffset ); 713 ok( !memcmp(info->abBuffer, zeros, sizeof(info->abBuffer)), "abBuffer = %s\n", 714 wine_dbgstr_an((char *)info->abBuffer, sizeof(info->abBuffer)) ); 715 716 res = BeginUpdateResourceW( ln_dll, TRUE ); 717 ok( res != NULL, "BeginUpdateResourceW failed: %ld\n", GetLastError() ); 718 r = UpdateResourceW( res, L"MUI", MAKEINTRESOURCEW(1), 0, &ln_mui_res, 4 ); 719 ok( r, "UpdateResource failed: %ld\n", GetLastError() ); 720 r = EndUpdateResourceW( res, FALSE ); 721 ok( r, "EndUpdateResourceW failed: %ld\n", GetLastError() ); 722 723 size = 0; 724 r = GetFileMUIInfo( MUI_QUERY_TYPE | MUI_QUERY_CHECKSUM | MUI_QUERY_LANGUAGE_NAME 725 | MUI_QUERY_RESOURCE_TYPES, ln_dll, NULL, &size ); 726 ok( !r, "GetFileMUIInfo succeeded\n" ); 727#if defined(__REACTOS__) && defined(_WIN64) 728 ok( GetLastError() == ERROR_BAD_EXE_FORMAT || GetLastError() == ERROR_INSUFFICIENT_BUFFER /* Vista x64 */, "GetLastError() = %ld\n", GetLastError() ); 729#else 730 ok( GetLastError() == ERROR_BAD_EXE_FORMAT, "GetLastError() = %ld\n", GetLastError() ); 731#endif 732 733 res = BeginUpdateResourceW( ln_dll, TRUE ); 734 ok( res != NULL, "BeginUpdateResourceW failed: %ld\n", GetLastError() ); 735 r = UpdateResourceW( res, L"MUI", MAKEINTRESOURCEW(1), 0, &ln_mui_res, sizeof(ln_mui_res) ); 736 ok( r, "UpdateResource failed: %ld\n", GetLastError() ); 737 r = EndUpdateResourceW( res, FALSE ); 738 ok( r, "EndUpdateResourceW failed: %ld\n", GetLastError() ); 739 740 size = 0; 741 r = GetFileMUIInfo( MUI_QUERY_TYPE | MUI_QUERY_CHECKSUM | MUI_QUERY_LANGUAGE_NAME 742 | MUI_QUERY_RESOURCE_TYPES, ln_dll, NULL, &size ); 743 ok( !r, "GetFileMUIInfo succeeded\n" ); 744 ok( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() = %ld\n", GetLastError() ); 745 ok( size, "size was not set\n" ); 746 747 memset( buf, 0xfe, sizeof(buf) ); 748 size = sizeof(buf); 749 info->dwSize = sizeof(buf); 750 info->dwVersion = MUI_FILEINFO_VERSION; 751 r = GetFileMUIInfo( MUI_QUERY_TYPE | MUI_QUERY_CHECKSUM | MUI_QUERY_LANGUAGE_NAME 752 | MUI_QUERY_RESOURCE_TYPES, ln_dll, info, &size ); 753 ok( r, "GetFileMUIInfo failed: %ld\n", GetLastError() ); 754 ok( info->dwSize == sizeof(buf), "dwSize = %ld\n", info->dwSize ); 755 ok( info->dwVersion == MUI_FILEINFO_VERSION, "dwVersion = %ld\n", info->dwVersion ); 756 ok( info->dwFileType == MUI_FILETYPE_LANGUAGE_NEUTRAL_MAIN, "dwFileType = %ld\n", info->dwFileType ); 757 ok( info->pChecksum[0] == 'c', "pChecksum = %s\n", 758 wine_dbgstr_an((char *)info->pChecksum, sizeof(info->pChecksum)) ); 759 ok( info->pServiceChecksum[0] == 's', "pServiceChecksum = %s\n", 760 wine_dbgstr_an((char *)info->pServiceChecksum, sizeof(info->pServiceChecksum)) ); 761 ok( info->dwLanguageNameOffset == 72, "dwLanguageNameOffset = %ld\n", info->dwLanguageNameOffset ); 762#if defined(__REACTOS__) && DLL_EXPORT_VERSION >= 0x600 763 if (is_reactos()) { 764 ok(FALSE, "FIXME: These tests crash on ReactOS!\n"); 765 } else { 766#endif 767 str = (WCHAR *)(buf + info->dwLanguageNameOffset); 768 ok( !wcscmp(str, L"en-US"), "language name = %s\n", wine_dbgstr_w(str) ); 769 ok( info->dwTypeIDMainSize == 1, "dwTypeIDMainSize = %ld\n", info->dwTypeIDMainSize ); 770 ok( info->dwTypeIDMainOffset == 84, "dwTypeIDMainOffset = %ld\n", info->dwTypeIDMainOffset ); 771 id = (DWORD *)(buf + info->dwTypeIDMainOffset); 772 ok( id[0] == RT_CURSOR, "type ID main[0] = %ld\n", id[0] ); 773 ok( info->dwTypeNameMainOffset == 88, "dwTypeNameMainOffset = %ld\n", info->dwTypeNameMainOffset ); 774 str = (WCHAR *)(buf + info->dwTypeNameMainOffset); 775 ok( !wcscmp(str, L"MUI"), "type name main[0] = %s\n", wine_dbgstr_w(str) ); 776 str += wcslen(str) + 1; 777 ok( !str[0], "string list is not NULL terminated: %s\n", wine_dbgstr_w(str) ); 778 ok( info->dwTypeIDMUISize == 1, "dwTypeIDMUISize = %ld\n", info->dwTypeIDMUISize ); 779 ok( info->dwTypeIDMUIOffset == 98, "dwTypeIDMUIOffset = %ld\n", info->dwTypeIDMUIOffset ); 780 id = (DWORD *)(buf + info->dwTypeIDMUIOffset); 781 ok( id[0] == RT_STRING, "type ID MUI[0] = %ld\n", id[0] ); 782 ok( info->dwTypeNameMUIOffset == 102, "dwTypeNameMUIOffset = %ld\n", info->dwTypeNameMUIOffset ); 783 str = (WCHAR *)(buf + info->dwTypeNameMUIOffset); 784 ok( !wcscmp(str, L"MUI"), "type name MUI[0] = %s\n", wine_dbgstr_w(str) ); 785 str += wcslen(str) + 1; 786 ok( !str[0], "string list is not NULL terminated: %s\n", wine_dbgstr_w(str) ); 787#if defined(__REACTOS__) && DLL_EXPORT_VERSION >= 0x600 788 } 789#endif 790 791 res = BeginUpdateResourceW( en_dll, TRUE ); 792 ok( res != NULL, "BeginUpdateResourceW failed: %ld\n", GetLastError() ); 793 r = UpdateResourceW( res, L"MUI", MAKEINTRESOURCEW(1), 0, &en_mui_res, sizeof(en_mui_res) ); 794 ok( r, "UpdateResource failed: %ld\n", GetLastError() ); 795 r = EndUpdateResourceW( res, FALSE ); 796 ok( r, "EndUpdateResourceW failed: %ld\n", GetLastError() ); 797 798 memset( buf, 0xfe, sizeof(buf) ); 799 size = sizeof(buf); 800 info->dwSize = sizeof(buf); 801 info->dwVersion = MUI_FILEINFO_VERSION; 802 r = GetFileMUIInfo( MUI_QUERY_TYPE | MUI_QUERY_CHECKSUM | MUI_QUERY_LANGUAGE_NAME 803 | MUI_QUERY_RESOURCE_TYPES, en_dll, info, &size ); 804 ok( r, "GetFileMUIInfo failed: %ld\n", GetLastError() ); 805 ok( info->dwSize == sizeof(buf), "dwSize = %ld\n", info->dwSize ); 806 ok( info->dwVersion == MUI_FILEINFO_VERSION, "dwVersion = %ld\n", info->dwVersion ); 807 ok( info->dwFileType == MUI_FILETYPE_LANGUAGE_NEUTRAL_MUI, "dwFileType = %ld\n", info->dwFileType ); 808 ok( info->pChecksum[0] == 'c', "pChecksum = %s\n", 809 wine_dbgstr_an((char *)info->pChecksum, sizeof(info->pChecksum)) ); 810 ok( info->pServiceChecksum[0] == 's', "pServiceChecksum = %s\n", 811 wine_dbgstr_an((char *)info->pServiceChecksum, sizeof(info->pServiceChecksum)) ); 812 ok( info->dwLanguageNameOffset == 72, "dwLanguageNameOffset = %ld\n", info->dwLanguageNameOffset ); 813#if defined(__REACTOS__) && DLL_EXPORT_VERSION >= 0x600 814 if (is_reactos()) { 815 ok(FALSE, "FIXME: These tests crash on ReactOS!\n"); 816 } else { 817#endif 818 str = (WCHAR *)(buf + info->dwLanguageNameOffset); 819 ok( !wcscmp(str, L"en-US"), "language name = %s\n", wine_dbgstr_w(str) ); 820 ok( !info->dwTypeIDMainSize, "dwTypeIDMainSize = %ld\n", info->dwTypeIDMainSize ); 821 ok( !info->dwTypeIDMainOffset, "dwTypeIDMainOffset = %ld\n", info->dwTypeIDMainOffset ); 822 ok( !info->dwTypeNameMainOffset, "dwTypeNameMainOffset = %ld\n", info->dwTypeNameMainOffset ); 823 ok( info->dwTypeIDMUISize == 1, "dwTypeIDMUISize = %ld\n", info->dwTypeIDMUISize ); 824 ok( info->dwTypeIDMUIOffset == 84, "dwTypeIDMUIOffset = %ld\n", info->dwTypeIDMUIOffset ); 825 id = (DWORD *)(buf + info->dwTypeIDMUIOffset); 826 ok( id[0] == RT_STRING, "type ID MUI[0] = %ld\n", id[0] ); 827 ok( info->dwTypeNameMUIOffset == 88, "dwTypeNameMUIOffset = %ld\n", info->dwTypeNameMUIOffset ); 828 str = (WCHAR *)(buf + info->dwTypeNameMUIOffset); 829 ok( !wcscmp(str, L"MUI"), "type name MUI[0] = %s\n", wine_dbgstr_w(str) ); 830 str += wcslen(str) + 1; 831 ok( !str[0], "string list is not NULL terminated: %s\n", wine_dbgstr_w(str) ); 832#if defined(__REACTOS__) && DLL_EXPORT_VERSION >= 0x600 833 } 834#endif 835 836 DeleteFileW( ln_dll ); 837 DeleteFileW( en_dll ); 838 RemoveDirectoryW( L"en-US" ); 839#endif 840} 841 842START_TEST(resource) 843{ 844 DWORD i; 845 846 DeleteFileA( filename ); 847 update_missing_exe(); 848 849 if (GLE == ERROR_CALL_NOT_IMPLEMENTED) 850 { 851 win_skip("Resource calls are not implemented\n"); 852 return; 853 } 854 855 update_empty_exe(); 856 857 for(i=0; i < ARRAY_SIZE(sec_variants); i++) 858 { 859 const struct _sec_variants *sec = &sec_variants[i]; 860 build_exe( &sec->build ); 861 test_internal_structure(); 862 update_resources_none(); 863 check_exe( &sec->chk_none ); 864 update_resources_delete(); 865 check_exe( &sec->chk_delete ); 866 update_resources_version(); 867 check_exe( &sec->chk_version ); 868 update_resources_bigdata(); 869 check_exe( &sec->chk_bigdata ); 870 update_resources_name(); 871 DeleteFileA( filename ); 872 } 873 test_find_resource(); 874 test_mui(); 875}