1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* Unit test suite for environment functions.
*
* Copyright 2006 Mike McCormack
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <windows.h>
#include <stdio.h>
#include "wine/test.h"
static const char filename[] = "test_.exe";
static DWORD GLE;
static int build_exe( void )
{
IMAGE_DOS_HEADER *dos;
IMAGE_NT_HEADERS *nt;
IMAGE_SECTION_HEADER *sec;
IMAGE_OPTIONAL_HEADER *opt;
HANDLE file;
DWORD written;
BYTE page[0x1000];
const int page_size = 0x1000;
memset( page, 0, sizeof page );
dos = (void*) page;
dos->e_magic = IMAGE_DOS_SIGNATURE;
dos->e_lfanew = sizeof *dos;
nt = (void*) &dos[1];
nt->Signature = IMAGE_NT_SIGNATURE;
nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
nt->FileHeader.NumberOfSections = 2;
nt->FileHeader.SizeOfOptionalHeader = sizeof nt->OptionalHeader;
nt->FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
opt = &nt->OptionalHeader;
opt->Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
opt->MajorLinkerVersion = 1;
opt->BaseOfCode = 0x10;
opt->ImageBase = 0x10000000;
opt->MajorOperatingSystemVersion = 4;
opt->MajorImageVersion = 1;
opt->MajorSubsystemVersion = 4;
opt->SizeOfHeaders = sizeof *dos + sizeof *nt + sizeof *sec * 2;
opt->SizeOfImage = page_size*3;
opt->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
/* if SectionAlignment and File alignment are not specified */
/* UpdateResource fails trying to create a huge temporary file */
opt->SectionAlignment = page_size;
opt->FileAlignment = page_size;
sec = (void*) &nt[1];
memcpy( sec[0].Name, ".rodata", 8 );
sec[0].Misc.VirtualSize = page_size;
sec[0].PointerToRawData = page_size;
sec[0].SizeOfRawData = page_size;
sec[0].VirtualAddress = page_size;
sec[0].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
memcpy( sec[1].Name, ".rsrc", 6 );
sec[1].Misc.VirtualSize = page_size;
sec[1].SizeOfRawData = page_size;
sec[1].PointerToRawData = page_size*2;
sec[1].VirtualAddress = page_size*2;
sec[1].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
/* write out the header */
WriteFile( file, page, sizeof page, &written, NULL );
/* write out an empty page for rodata */
memset( page, 0, sizeof page );
WriteFile( file, page, sizeof page, &written, NULL );
/* write out an empty page for the resources */
memset( page, 0, sizeof page );
WriteFile( file, page, sizeof page, &written, NULL );
CloseHandle( file );
return 0;
}
static void update_missing_exe( void )
{
HANDLE res;
SetLastError(0xdeadbeef);
res = BeginUpdateResource( filename, TRUE );
GLE = GetLastError();
ok( res == NULL, "BeginUpdateResource should fail\n");
}
static void update_empty_exe( void )
{
HANDLE file, res, test;
BOOL r;
file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
CloseHandle( file );
res = BeginUpdateResource( filename, TRUE );
ok( res != NULL, "BeginUpdateResource failed\n");
/* check if it's possible to open the file now */
test = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
ok (test != INVALID_HANDLE_VALUE, "failed to create file\n");
CloseHandle( test );
r = EndUpdateResource( res, FALSE );
ok( r == FALSE, "EndUpdateResource failed\n");
res = BeginUpdateResource( filename, FALSE );
ok( res == NULL, "BeginUpdateResource failed\n");
}
static void update_resources_none( void )
{
HMODULE res;
BOOL r;
res = BeginUpdateResource( filename, FALSE );
ok( res != NULL, "BeginUpdateResource failed\n");
r = EndUpdateResource( res, FALSE );
ok( r, "EndUpdateResource failed\n");
}
static void update_resources_delete( void )
{
HMODULE res;
BOOL r;
res = BeginUpdateResource( filename, TRUE );
ok( res != NULL, "BeginUpdateResource failed\n");
r = EndUpdateResource( res, FALSE );
ok( r, "EndUpdateResource failed\n");
}
static void update_resources_version(void)
{
HANDLE res = NULL;
BOOL r;
char foo[] = "red and white";
res = BeginUpdateResource( filename, TRUE );
ok( res != NULL, "BeginUpdateResource failed\n");
r = UpdateResource( res,
MAKEINTRESOURCE(0x1230),
MAKEINTRESOURCE(0x4567),
0xabcd,
NULL, 0 );
ok( r == FALSE, "UpdateResource failed\n");
r = UpdateResource( res,
MAKEINTRESOURCE(0x1230),
MAKEINTRESOURCE(0x4567),
0xabcd,
foo, sizeof foo );
ok( r == TRUE, "UpdateResource failed\n");
r = EndUpdateResource( res, FALSE );
ok( r, "EndUpdateResource failed\n");
}
typedef void (*res_check_func)( IMAGE_RESOURCE_DIRECTORY* );
static void check_empty( IMAGE_RESOURCE_DIRECTORY *dir )
{
char *pad;
ok( dir->NumberOfNamedEntries == 0, "NumberOfNamedEntries should be 0 instead of %d\n", dir->NumberOfNamedEntries);
ok( dir->NumberOfIdEntries == 0, "NumberOfIdEntries should be 0 instead of %d\n", dir->NumberOfIdEntries);
pad = (char*) &dir[1];
ok( !memcmp( pad, "PADDINGXXPADDING", 16), "padding wrong\n");
}
static void check_not_empty( IMAGE_RESOURCE_DIRECTORY *dir )
{
ok( dir->NumberOfNamedEntries == 0, "NumberOfNamedEntries should be 0 instead of %d\n", dir->NumberOfNamedEntries);
ok( dir->NumberOfIdEntries == 1, "NumberOfIdEntries should be 1 instead of %d\n", dir->NumberOfIdEntries);
}
static void check_exe( res_check_func fn )
{
IMAGE_DOS_HEADER *dos;
IMAGE_NT_HEADERS *nt;
IMAGE_SECTION_HEADER *sec;
IMAGE_RESOURCE_DIRECTORY *dir;
HANDLE file, mapping;
DWORD length;
file = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
ok (file != INVALID_HANDLE_VALUE, "failed to create file (%d)\n", GetLastError());
length = GetFileSize( file, NULL );
ok( length == 0x3000, "file size wrong\n");
mapping = CreateFileMapping( file, NULL, PAGE_READONLY, 0, 0, NULL );
ok (mapping != NULL, "failed to create file\n");
dos = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
ok( dos != NULL, "failed to map file\n");
if (!dos)
goto end;
nt = (void*) ((BYTE*) dos + dos->e_lfanew);
ok( nt->FileHeader.NumberOfSections == 2, "number of sections wrong\n" );
if (nt->FileHeader.NumberOfSections < 2)
goto end;
sec = (void*) &nt[1];
ok( !memcmp(sec[1].Name, ".rsrc", 6), "resource section name wrong\n");
dir = (void*) ((BYTE*) dos + sec[1].VirtualAddress);
ok( dir->Characteristics == 0, "Characteristics wrong\n");
ok( dir->TimeDateStamp == 0, "TimeDateStamp wrong\n");
ok( dir->MajorVersion == 4, "MajorVersion wrong\n");
ok( dir->MinorVersion == 0, "MinorVersion wrong\n");
fn( dir );
end:
UnmapViewOfFile( dos );
CloseHandle( mapping );
CloseHandle( file );
}
static void test_find_resource(void)
{
HRSRC rsrc;
rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(1), (LPCWSTR)RT_MENU );
ok( rsrc != 0, "resource not found\n" );
rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ));
ok( rsrc != 0, "resource not found\n" );
rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
MAKELANGID( LANG_GERMAN, SUBLANG_DEFAULT ));
ok( rsrc != 0, "resource not found\n" );
SetLastError( 0xdeadbeef );
rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(1), (LPCWSTR)RT_DIALOG );
ok( !rsrc, "resource found\n" );
ok( GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND, "wrong error %u\n", GetLastError() );
SetLastError( 0xdeadbeef );
rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(2), (LPCWSTR)RT_MENU );
ok( !rsrc, "resource found\n" );
ok( GetLastError() == ERROR_RESOURCE_NAME_NOT_FOUND, "wrong error %u\n", GetLastError() );
SetLastError( 0xdeadbeef );
rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
ok( !rsrc, "resource found\n" );
ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
SetLastError( 0xdeadbeef );
rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
MAKELANGID( LANG_FRENCH, SUBLANG_DEFAULT ) );
ok( !rsrc, "resource found\n" );
ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
}
START_TEST(resource)
{
DeleteFile( filename );
update_missing_exe();
if (GLE == ERROR_CALL_NOT_IMPLEMENTED)
{
skip("Resource calls are not implemented\n");
return;
}
update_empty_exe();
build_exe();
update_resources_none();
check_exe( check_empty );
update_resources_delete();
check_exe( check_empty );
update_resources_version();
check_exe( check_not_empty );
DeleteFile( filename );
test_find_resource();
}
|