[KERNEL32_WINETEST]
[reactos.git] / rostests / winetests / kernel32 / resource.c
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 static int build_exe( void )
30 {
31 IMAGE_DOS_HEADER *dos;
32 IMAGE_NT_HEADERS *nt;
33 IMAGE_SECTION_HEADER *sec;
34 IMAGE_OPTIONAL_HEADER *opt;
35 HANDLE file;
36 DWORD written;
37 BYTE page[0x1000];
38 const int page_size = 0x1000;
39
40 memset( page, 0, sizeof page );
41
42 dos = (void*) page;
43 dos->e_magic = IMAGE_DOS_SIGNATURE;
44 dos->e_lfanew = sizeof *dos;
45
46 nt = (void*) &dos[1];
47
48 nt->Signature = IMAGE_NT_SIGNATURE;
49 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
50 nt->FileHeader.NumberOfSections = 2;
51 nt->FileHeader.SizeOfOptionalHeader = sizeof nt->OptionalHeader;
52 nt->FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
53
54 opt = &nt->OptionalHeader;
55
56 opt->Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
57 opt->MajorLinkerVersion = 1;
58 opt->BaseOfCode = 0x10;
59 opt->ImageBase = 0x10000000;
60 opt->MajorOperatingSystemVersion = 4;
61 opt->MajorImageVersion = 1;
62 opt->MajorSubsystemVersion = 4;
63 opt->SizeOfHeaders = sizeof *dos + sizeof *nt + sizeof *sec * 2;
64 opt->SizeOfImage = page_size*3;
65 opt->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
66
67 /* if SectionAlignment and File alignment are not specified */
68 /* UpdateResource fails trying to create a huge temporary file */
69 opt->SectionAlignment = page_size;
70 opt->FileAlignment = page_size;
71
72 sec = (void*) &nt[1];
73
74 memcpy( sec[0].Name, ".rodata", 8 );
75 sec[0].Misc.VirtualSize = page_size;
76 sec[0].PointerToRawData = page_size;
77 sec[0].SizeOfRawData = page_size;
78 sec[0].VirtualAddress = page_size;
79 sec[0].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
80
81 memcpy( sec[1].Name, ".rsrc", 6 );
82 sec[1].Misc.VirtualSize = page_size;
83 sec[1].SizeOfRawData = page_size;
84 sec[1].PointerToRawData = page_size*2;
85 sec[1].VirtualAddress = page_size*2;
86 sec[1].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
87
88 file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
89 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
90
91 /* write out the header */
92 WriteFile( file, page, sizeof page, &written, NULL );
93
94 /* write out an empty page for rodata */
95 memset( page, 0, sizeof page );
96 WriteFile( file, page, sizeof page, &written, NULL );
97
98 /* write out an empty page for the resources */
99 memset( page, 0, sizeof page );
100 WriteFile( file, page, sizeof page, &written, NULL );
101
102 CloseHandle( file );
103
104 return 0;
105 }
106
107 static void update_missing_exe( void )
108 {
109 HANDLE res;
110
111 SetLastError(0xdeadbeef);
112 res = BeginUpdateResource( filename, TRUE );
113 GLE = GetLastError();
114 ok( res == NULL, "BeginUpdateResource should fail\n");
115 }
116
117 static void update_empty_exe( void )
118 {
119 HANDLE file, res, test;
120 BOOL r;
121
122 file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
123 ok (file != INVALID_HANDLE_VALUE, "failed to create file\n");
124
125 CloseHandle( file );
126
127 res = BeginUpdateResource( filename, TRUE );
128 if ( res != NULL || GetLastError() != ERROR_FILE_INVALID )
129 {
130 ok( res != NULL, "BeginUpdateResource failed\n");
131
132 /* check if it's possible to open the file now */
133 test = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
134 ok (test != INVALID_HANDLE_VALUE, "failed to create file\n");
135
136 CloseHandle( test );
137
138 r = EndUpdateResource( res, FALSE );
139 ok( r == FALSE, "EndUpdateResource failed\n");
140 }
141 else
142 skip( "Can't update resource in empty file\n" );
143
144 res = BeginUpdateResource( filename, FALSE );
145 ok( res == NULL, "BeginUpdateResource failed\n");
146 }
147
148 static void update_resources_none( void )
149 {
150 HMODULE res;
151 BOOL r;
152
153 res = BeginUpdateResource( filename, FALSE );
154 ok( res != NULL, "BeginUpdateResource failed\n");
155
156 r = EndUpdateResource( res, FALSE );
157 ok( r, "EndUpdateResource failed\n");
158 }
159
160 static void update_resources_delete( void )
161 {
162 HMODULE res;
163 BOOL r;
164
165 res = BeginUpdateResource( filename, TRUE );
166 ok( res != NULL, "BeginUpdateResource failed\n");
167
168 r = EndUpdateResource( res, FALSE );
169 ok( r, "EndUpdateResource failed\n");
170 }
171
172 static void update_resources_version(void)
173 {
174 HANDLE res = NULL;
175 BOOL r;
176 char foo[] = "red and white";
177
178 res = BeginUpdateResource( filename, TRUE );
179 ok( res != NULL, "BeginUpdateResource failed\n");
180
181 if (0) /* this causes subsequent tests to fail on Vista */
182 {
183 r = UpdateResource( res,
184 MAKEINTRESOURCE(0x1230),
185 MAKEINTRESOURCE(0x4567),
186 0xabcd,
187 NULL, 0 );
188 ok( r == FALSE, "UpdateResource failed\n");
189 }
190
191 r = UpdateResource( res,
192 MAKEINTRESOURCE(0x1230),
193 MAKEINTRESOURCE(0x4567),
194 0xabcd,
195 foo, sizeof foo );
196 ok( r == TRUE, "UpdateResource failed: %d\n", GetLastError());
197
198 r = EndUpdateResource( res, FALSE );
199 ok( r, "EndUpdateResource failed: %d\n", GetLastError());
200 }
201
202
203 typedef void (*res_check_func)( IMAGE_RESOURCE_DIRECTORY* );
204
205 static void check_empty( IMAGE_RESOURCE_DIRECTORY *dir )
206 {
207 char *pad;
208
209 ok( dir->NumberOfNamedEntries == 0, "NumberOfNamedEntries should be 0 instead of %d\n", dir->NumberOfNamedEntries);
210 ok( dir->NumberOfIdEntries == 0, "NumberOfIdEntries should be 0 instead of %d\n", dir->NumberOfIdEntries);
211
212 pad = (char*) &dir[1];
213
214 ok( !memcmp( pad, "PADDINGXXPADDING", 16), "padding wrong\n");
215 }
216
217 static void check_not_empty( IMAGE_RESOURCE_DIRECTORY *dir )
218 {
219 ok( dir->NumberOfNamedEntries == 0, "NumberOfNamedEntries should be 0 instead of %d\n", dir->NumberOfNamedEntries);
220 ok( dir->NumberOfIdEntries == 1, "NumberOfIdEntries should be 1 instead of %d\n", dir->NumberOfIdEntries);
221 }
222
223 static void check_exe( res_check_func fn )
224 {
225 IMAGE_DOS_HEADER *dos;
226 IMAGE_NT_HEADERS *nt;
227 IMAGE_SECTION_HEADER *sec;
228 IMAGE_RESOURCE_DIRECTORY *dir;
229 HANDLE file, mapping;
230 DWORD length;
231
232 file = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
233 ok (file != INVALID_HANDLE_VALUE, "failed to create file (%d)\n", GetLastError());
234
235 length = GetFileSize( file, NULL );
236 ok( length == 0x3000, "file size wrong\n");
237
238 mapping = CreateFileMapping( file, NULL, PAGE_READONLY, 0, 0, NULL );
239 ok (mapping != NULL, "failed to create file\n");
240
241 dos = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
242 ok( dos != NULL, "failed to map file\n");
243
244 if (!dos)
245 goto end;
246
247 nt = (void*) ((BYTE*) dos + dos->e_lfanew);
248 ok( nt->FileHeader.NumberOfSections == 2, "number of sections wrong\n" );
249
250 if (nt->FileHeader.NumberOfSections < 2)
251 goto end;
252
253 sec = (void*) &nt[1];
254
255 ok( !memcmp(sec[1].Name, ".rsrc", 6), "resource section name wrong\n");
256
257 dir = (void*) ((BYTE*) dos + sec[1].VirtualAddress);
258
259 ok( dir->Characteristics == 0, "Characteristics wrong\n");
260 ok( dir->TimeDateStamp == 0 || abs( dir->TimeDateStamp - GetTickCount() ) < 1000 /* nt4 */,
261 "TimeDateStamp wrong %u\n", dir->TimeDateStamp);
262 ok( dir->MajorVersion == 4, "MajorVersion wrong\n");
263 ok( dir->MinorVersion == 0, "MinorVersion wrong\n");
264
265 fn( dir );
266
267 end:
268 UnmapViewOfFile( dos );
269
270 CloseHandle( mapping );
271
272 CloseHandle( file );
273 }
274
275 static void test_find_resource(void)
276 {
277 HRSRC rsrc;
278
279 rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(1), (LPCWSTR)RT_MENU );
280 ok( rsrc != 0, "resource not found\n" );
281 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
282 MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ));
283 ok( rsrc != 0, "resource not found\n" );
284 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
285 MAKELANGID( LANG_GERMAN, SUBLANG_DEFAULT ));
286 ok( rsrc != 0, "resource not found\n" );
287
288 SetLastError( 0xdeadbeef );
289 rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(1), (LPCWSTR)RT_DIALOG );
290 ok( !rsrc, "resource found\n" );
291 ok( GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND, "wrong error %u\n", GetLastError() );
292
293 SetLastError( 0xdeadbeef );
294 rsrc = FindResourceW( GetModuleHandle(0), (LPCWSTR)MAKEINTRESOURCE(2), (LPCWSTR)RT_MENU );
295 ok( !rsrc, "resource found\n" );
296 ok( GetLastError() == ERROR_RESOURCE_NAME_NOT_FOUND, "wrong error %u\n", GetLastError() );
297
298 SetLastError( 0xdeadbeef );
299 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
300 MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
301 ok( !rsrc, "resource found\n" );
302 ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
303
304 SetLastError( 0xdeadbeef );
305 rsrc = FindResourceExW( GetModuleHandle(0), (LPCWSTR)RT_MENU, (LPCWSTR)MAKEINTRESOURCE(1),
306 MAKELANGID( LANG_FRENCH, SUBLANG_DEFAULT ) );
307 ok( !rsrc, "resource found\n" );
308 ok( GetLastError() == ERROR_RESOURCE_LANG_NOT_FOUND, "wrong error %u\n", GetLastError() );
309 }
310
311 START_TEST(resource)
312 {
313 DeleteFile( filename );
314 update_missing_exe();
315
316 if (GLE == ERROR_CALL_NOT_IMPLEMENTED)
317 {
318 win_skip("Resource calls are not implemented\n");
319 return;
320 }
321
322 update_empty_exe();
323 build_exe();
324 update_resources_none();
325 check_exe( check_empty );
326 update_resources_delete();
327 check_exe( check_empty );
328 update_resources_version();
329 check_exe( check_not_empty );
330 DeleteFile( filename );
331 test_find_resource();
332 }