janitorial: Remove remaining NULL checks before free() (found by Smatch).
[wine] / dlls / version / tests / info.c
1 /*
2  * Copyright (C) 2004 Stefan Leichter
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20 #include <stdio.h>
21
22 #include "wine/test.h"
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "winver.h"
27
28 #define MY_LAST_ERROR ((DWORD)-1)
29 #define EXPECT_BAD_PATH__NOT_FOUND \
30     ok( (ERROR_PATH_NOT_FOUND == GetLastError()) || \
31         (ERROR_RESOURCE_DATA_NOT_FOUND == GetLastError()) || \
32         (ERROR_FILE_NOT_FOUND == GetLastError()) || \
33         (ERROR_BAD_PATHNAME == GetLastError()), \
34         "Last error wrong! ERROR_RESOURCE_DATA_NOT_FOUND/ERROR_BAD_PATHNAME (98)/" \
35         "ERROR_PATH_NOT_FOUND (NT4)/ERROR_FILE_NOT_FOUND (2k3)" \
36         "expected, got 0x%08x\n", GetLastError());
37 #define EXPECT_INVALID__NOT_FOUND \
38     ok( (ERROR_PATH_NOT_FOUND == GetLastError()) || \
39         (ERROR_RESOURCE_DATA_NOT_FOUND == GetLastError()) || \
40         (ERROR_FILE_NOT_FOUND == GetLastError()) || \
41         (ERROR_INVALID_PARAMETER == GetLastError()), \
42         "Last error wrong! ERROR_RESOURCE_DATA_NOT_FOUND/ERROR_INVALID_PARAMETER (98)/" \
43         "ERROR_PATH_NOT_FOUND (NT4)/ERROR_FILE_NOT_FOUND (2k3)" \
44         "expected, got 0x%08x\n", GetLastError());
45
46 static void test_info_size(void)
47 {   DWORD hdl, retval;
48     char mypath[MAX_PATH] = "";
49
50     SetLastError(MY_LAST_ERROR);
51     retval = GetFileVersionInfoSizeA( NULL, NULL);
52     ok( !retval,
53         "GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
54         retval);
55     EXPECT_INVALID__NOT_FOUND;
56
57     hdl = 0x55555555;
58     SetLastError(MY_LAST_ERROR);
59     retval = GetFileVersionInfoSizeA( NULL, &hdl);
60     ok( !retval,
61         "GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
62         retval);
63     EXPECT_INVALID__NOT_FOUND;
64     ok( hdl == 0L,
65         "Handle wrong! 0L expected, got 0x%08x\n", hdl);
66
67     SetLastError(MY_LAST_ERROR);
68     retval = GetFileVersionInfoSizeA( "", NULL);
69     ok( !retval,
70         "GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
71         retval);
72     EXPECT_BAD_PATH__NOT_FOUND;
73
74     hdl = 0x55555555;
75     SetLastError(MY_LAST_ERROR);
76     retval = GetFileVersionInfoSizeA( "", &hdl);
77     ok( !retval,
78         "GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
79         retval);
80     EXPECT_BAD_PATH__NOT_FOUND;
81     ok( hdl == 0L,
82         "Handle wrong! 0L expected, got 0x%08x\n", hdl);
83
84     SetLastError(MY_LAST_ERROR);
85     retval = GetFileVersionInfoSizeA( "kernel32.dll", NULL);
86     ok( retval,
87         "GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
88         retval);
89     ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
90         "Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got 0x%08x\n",
91         MY_LAST_ERROR, GetLastError());
92
93     hdl = 0x55555555;
94     SetLastError(MY_LAST_ERROR);
95     retval = GetFileVersionInfoSizeA( "kernel32.dll", &hdl);
96     ok( retval,
97         "GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
98         retval);
99     ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
100         "Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got 0x%08x\n",
101         MY_LAST_ERROR, GetLastError());
102     ok( hdl == 0L,
103         "Handle wrong! 0L expected, got 0x%08x\n", hdl);
104
105     SetLastError(MY_LAST_ERROR);
106     retval = GetFileVersionInfoSizeA( "notexist.dll", NULL);
107     ok( !retval,
108         "GetFileVersionInfoSizeA result wrong! 0L expected, got 0x%08x\n",
109         retval);
110     ok( (ERROR_FILE_NOT_FOUND == GetLastError()) ||
111         (ERROR_RESOURCE_DATA_NOT_FOUND == GetLastError()) ||
112         (MY_LAST_ERROR == GetLastError()),
113         "Last error wrong! ERROR_FILE_NOT_FOUND/ERROR_RESOURCE_DATA_NOT_FOUND "
114         "(XP)/0x%08x (NT4) expected, got 0x%08x\n", MY_LAST_ERROR, GetLastError());
115
116     /* test a currently loaded executable */
117     if(GetModuleFileNameA(NULL, mypath, MAX_PATH)) {
118         hdl = 0x55555555;
119         SetLastError(MY_LAST_ERROR);
120         retval = GetFileVersionInfoSizeA( mypath, &hdl);
121         ok( retval,
122             "GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
123             retval);
124         ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
125             "Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got 0x%08x\n",
126             MY_LAST_ERROR, GetLastError());
127         ok( hdl == 0L,
128             "Handle wrong! 0L expected, got 0x%08x\n", hdl);
129     }
130     else
131         trace("skipping GetModuleFileNameA(NULL,..) failed\n");
132
133     /* test a not loaded executable */
134     if(GetSystemDirectoryA(mypath, MAX_PATH)) {
135         lstrcatA(mypath, "\\regsvr32.exe");
136
137         if(INVALID_FILE_ATTRIBUTES == GetFileAttributesA(mypath))
138             trace("GetFileAttributesA(%s) failed\n", mypath);
139         else {
140             hdl = 0x55555555;
141             SetLastError(MY_LAST_ERROR);
142             retval = GetFileVersionInfoSizeA( mypath, &hdl);
143             ok( retval,
144                 "GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
145                 retval);
146             ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
147                 "Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got 0x%08x\n",
148                 MY_LAST_ERROR, GetLastError());
149             ok( hdl == 0L,
150                 "Handle wrong! 0L expected, got 0x%08x\n", hdl);
151         }
152     }
153     else
154         trace("skipping GetModuleFileNameA(NULL,..) failed\n");
155 }
156
157 static void VersionDwordLong2String(DWORDLONG Version, LPSTR lpszVerString)
158 {
159     WORD a, b, c, d;
160
161     a = (WORD)(Version >> 48);
162     b = (WORD)((Version >> 32) & 0xffff);
163     c = (WORD)((Version >> 16) & 0xffff);
164     d = (WORD)(Version & 0xffff);
165
166     sprintf(lpszVerString, "%d.%d.%d.%d", a, b, c, d);
167
168     return;
169 }
170
171 static void test_info(void)
172 {
173     DWORD hdl, retval;
174     PVOID pVersionInfo = NULL;
175     BOOL boolret;
176     VS_FIXEDFILEINFO *pFixedVersionInfo;
177     UINT uiLength;
178     char VersionString[MAX_PATH];
179     static CHAR backslash[] = "\\";
180     DWORDLONG dwlVersion;
181
182     hdl = 0x55555555;
183     SetLastError(MY_LAST_ERROR);
184     retval = GetFileVersionInfoSizeA( "kernel32.dll", &hdl);
185     ok( retval,
186         "GetFileVersionInfoSizeA result wrong! <> 0L expected, got 0x%08x\n",
187         retval);
188     ok((NO_ERROR == GetLastError()) || (MY_LAST_ERROR == GetLastError()),
189         "Last error wrong! NO_ERROR/0x%08x (NT4)  expected, got 0x%08x\n",
190         MY_LAST_ERROR, GetLastError());
191     ok( hdl == 0L,
192         "Handle wrong! 0L expected, got 0x%08x\n", hdl);
193
194     if ( retval == 0 || hdl != 0)
195         return;
196
197     pVersionInfo = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, retval );
198     ok(pVersionInfo != 0, "HeapAlloc failed\n" );
199     if (pVersionInfo == 0)
200         return;
201
202 #if 0
203     /* this test crashes on WinNT4
204      */
205     boolret = GetFileVersionInfoA( "kernel32.dll", 0, retval, 0);
206     ok (!boolret, "GetFileVersionInfoA should have failed: GetLastError = 0x%08lx\n", GetLastError());
207     ok ((GetLastError() == ERROR_INVALID_DATA) || (GetLastError() == ERROR_BAD_PATHNAME) ||
208         (GetLastError() == NO_ERROR),
209         "Last error wrong! ERROR_INVALID_DATA/ERROR_BAD_PATHNAME (ME)/"
210         "NO_ERROR (95) expected, got 0x%08lx\n",
211         GetLastError());
212 #endif
213
214     boolret = GetFileVersionInfoA( "kernel32.dll", 0, retval, pVersionInfo );
215     ok (boolret, "GetFileVersionInfoA failed: GetLastError = 0x%08x\n", GetLastError());
216     if (!boolret)
217         return;
218
219     boolret = VerQueryValueA( pVersionInfo, backslash, (LPVOID *)&pFixedVersionInfo, &uiLength );
220     ok (boolret, "VerQueryValueA failed: GetLastError = 0x%08x\n", GetLastError());
221     if (!boolret)
222         return;
223
224     dwlVersion = (((DWORDLONG)pFixedVersionInfo->dwFileVersionMS) << 32) +
225         pFixedVersionInfo->dwFileVersionLS;
226
227     VersionDwordLong2String(dwlVersion, VersionString);
228
229     trace("kernel32.dll version: %s\n", VersionString);
230
231 #if 0
232     /* this test crashes on WinNT4
233      */
234     boolret = VerQueryValueA( pVersionInfo, "\\", (LPVOID *)&pFixedVersionInfo, 0);
235     ok (boolret, "VerQueryValue failed: GetLastError = 0x%08lx\n", GetLastError());
236 #endif
237 }
238
239 static void test_32bit_win(void)
240 {
241     DWORD hdlA, retvalA;
242     DWORD hdlW, retvalW = 0;
243     BOOL retA,retW;
244     PVOID pVersionInfoA = NULL;
245     PVOID pVersionInfoW = NULL;
246     char *pBufA;
247     WCHAR *pBufW;
248     UINT uiLengthA, uiLengthW;
249     char mypathA[MAX_PATH];
250     WCHAR mypathW[MAX_PATH];
251     char rootA[] = "\\";
252     WCHAR rootW[] = { '\\', 0 };
253     char varfileinfoA[] = "\\VarFileInfo\\Translation";
254     WCHAR varfileinfoW[]    = { '\\','V','a','r','F','i','l','e','I','n','f','o',
255                                 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
256     char WineVarFileInfoA[] = { 0x09, 0x04, 0xE4, 0x04 };
257     char FileDescriptionA[] = "\\StringFileInfo\\040904E4\\FileDescription";
258     WCHAR FileDescriptionW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
259                                 '\\','0','4','0','9','0','4','E','4',
260                                 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n', 0 };
261     char WineFileDescriptionA[] = "Wine version test";
262     WCHAR WineFileDescriptionW[] = { 'W','i','n','e',' ','v','e','r','s','i','o','n',' ','t','e','s','t', 0 };
263     BOOL is_unicode_enabled = TRUE;
264
265     /* A copy from dlls/version/info.c */
266     typedef struct
267     {
268         WORD  wLength;
269         WORD  wValueLength;
270         WORD  wType;
271         WCHAR szKey[1];
272 #if 0   /* variable length structure */
273         /* DWORD aligned */
274         BYTE  Value[];
275         /* DWORD aligned */
276         VS_VERSION_INFO_STRUCT32 Children[];
277 #endif
278     } VS_VERSION_INFO_STRUCT32;
279
280     /* If we call GetFileVersionInfoA on a system that supports Unicode, NT/W2K/XP/W2K3 (by default) and Wine,
281      * the versioninfo will contain Unicode strings.
282      * Part of the test is to call both the A and W versions, which should have the same Version Information
283      * for some requests, on systems that support both calls.
284      */
285
286     /* First get the versioninfo via the W versions */
287     GetModuleFileNameW(NULL, mypathW, MAX_PATH);
288     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
289     {
290         trace("GetModuleFileNameW not existing on this platform, skipping comparison between A- and W-calls\n");
291         is_unicode_enabled = FALSE;
292     }
293
294     if (is_unicode_enabled)
295     { 
296         retvalW = GetFileVersionInfoSizeW( mypathW, &hdlW);
297         pVersionInfoW = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, retvalW );
298         retW = GetFileVersionInfoW( mypathW, 0, retvalW, pVersionInfoW );
299     }
300
301     GetModuleFileNameA(NULL, mypathA, MAX_PATH);
302     retvalA = GetFileVersionInfoSizeA( mypathA, &hdlA);
303     pVersionInfoA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, retvalA );
304     retA = GetFileVersionInfoA( mypathA, 0, retvalA, pVersionInfoA );
305
306     if (is_unicode_enabled)
307     { 
308         ok( retvalA == retvalW, "The size of the struct should be the same for both A/W calls, it is (%d) vs. (%d)\n",
309                                 retvalA, retvalW);
310         ok( !memcmp(pVersionInfoA, pVersionInfoW, retvalA), "Both structs should be the same, they aren't\n");
311     }
312
313     /* The structs on Windows are bigger then just the struct for the basic information. The total struct
314      * contains also an empty part, which is used for converted strings. The converted strings are a result
315      * of calling VerQueryValueA on a 32bit resource and calling VerQueryValueW on a 16bit resource.
316      * The first WORD of the structure (wLength) shows the size of the base struct. The total struct size depends
317      * on the Windows version:
318      *
319      * 16bits resource (numbers are from a sample app):
320      *
321      * Windows Version    Retrieved with A/W    wLength        StructSize
322      * ====================================================================================
323      * Win98              A                     0x01B4 (436)   436
324      * NT4                A/W                   0x01B4 (436)   2048 ???
325      * W2K/XP/W2K3        A/W                   0x01B4 (436)   1536 which is (436 - sizeof(VS_FIXEDFILEINFO)) * 4
326      *
327      * 32bits resource (numbers are from this test executable version_crosstest.exe):
328      * Windows Version    Retrieved with A/W    wLength        StructSize
329      * =============================================================
330      * Win98              A                     0x01E0 (480)   848 (structure data doesn't seem correct)
331      * NT4                A/W                   0x0350 (848)   1272 (848 * 1.5)
332      * W2K/XP/W2K3        A/W                   0x0350 (848)   1700 which is (848 * 2) + 4 
333      *
334      * Wine will follow the implementation (eventually) of W2K/XP/W2K3
335      */
336
337     /* Now some tests for the above (only if we are unicode enabled) */
338
339     if (is_unicode_enabled)
340     { 
341         VS_VERSION_INFO_STRUCT32 *vvis = (VS_VERSION_INFO_STRUCT32 *)pVersionInfoW;
342         ok ( retvalW == ((vvis->wLength * 2) + 4) || retvalW == (vvis->wLength * 1.5),
343              "Structure is not of the correct size\n");
344     }
345
346     /* Although the 32bit resource structures contain Unicode strings, VerQueryValueA will always return normal strings,
347      * VerQueryValueW will always return Unicode ones. (That means everything returned for StringFileInfo requests).
348      */
349
350     /* Get the VS_FIXEDFILEINFO information, this must be the same for both A- and W-Calls */ 
351
352     retA = VerQueryValueA( pVersionInfoA, rootA, (LPVOID *)&pBufA, &uiLengthA );
353     ok (retA, "VerQueryValueA failed: GetLastError = 0x%08x\n", GetLastError());
354     ok ( uiLengthA == sizeof(VS_FIXEDFILEINFO), "Size (%d) doesn't match the size of the VS_FIXEDFILEINFO struct\n", uiLengthA);
355
356     if (is_unicode_enabled)
357     { 
358         retW = VerQueryValueW( pVersionInfoW, rootW, (LPVOID *)&pBufW, &uiLengthW );
359         ok (retW, "VerQueryValueW failed: GetLastError = 0x%08x\n", GetLastError());
360         ok ( uiLengthA == sizeof(VS_FIXEDFILEINFO), "Size (%d) doesn't match the size of the VS_FIXEDFILEINFO struct\n", uiLengthA);
361
362         ok( uiLengthA == uiLengthW, "The size of VS_FIXEDFILEINFO should be the same for both A/W calls, it is (%d) vs. (%d)\n",
363                                     uiLengthA, uiLengthW);
364         ok( !memcmp(pBufA, pBufW, uiLengthA), "Both values should be the same, they aren't\n");
365     }
366
367     /* Get some VarFileInfo information, this must be the same for both A- and W-Calls */
368
369     retA = VerQueryValueA( pVersionInfoA, varfileinfoA, (LPVOID *)&pBufA, &uiLengthA );
370     ok (retA, "VerQueryValueA failed: GetLastError = 0x%08x\n", GetLastError());
371     ok( !memcmp(pBufA, WineVarFileInfoA, uiLengthA), "The VarFileInfo should have matched 0904e404 (non case sensitive)\n");
372
373     if (is_unicode_enabled)
374     { 
375         retW = VerQueryValueW( pVersionInfoW, varfileinfoW, (LPVOID *)&pBufW, &uiLengthW );
376         ok (retW, "VerQueryValueW failed: GetLastError = 0x%08x\n", GetLastError());
377         ok( uiLengthA == uiLengthW, "The size of the VarFileInfo information should be the same for both A/W calls, it is (%d) vs. (%d)\n",
378                                     uiLengthA, uiLengthW);
379         ok( !memcmp(pBufA, pBufW, uiLengthA), "Both values should be the same, they aren't\n");
380     }
381
382     /* Get some StringFileInfo information, this will be ANSI for A-Calls and Unicode for W-Calls */
383
384     retA = VerQueryValueA( pVersionInfoA, FileDescriptionA, (LPVOID *)&pBufA, &uiLengthA );
385     ok (retA, "VerQueryValueA failed: GetLastError = 0x%08x\n", GetLastError());
386     ok( !lstrcmpA(WineFileDescriptionA, pBufA), "FileDescription should have been 'Wine version test'\n");
387
388     /* Test a second time */
389     retA = VerQueryValueA( pVersionInfoA, FileDescriptionA, (LPVOID *)&pBufA, &uiLengthA );
390     ok (retA, "VerQueryValueA failed: GetLastError = 0x%08x\n", GetLastError());
391     ok( !lstrcmpA(WineFileDescriptionA, pBufA), "FileDescription should have been 'Wine version test'\n");
392
393     if (is_unicode_enabled)
394     { 
395         retW = VerQueryValueW( pVersionInfoW, FileDescriptionW, (LPVOID *)&pBufW, &uiLengthW );
396         ok (retW, "VerQueryValueW failed: GetLastError = 0x%08x\n", GetLastError());
397         ok( !lstrcmpW(WineFileDescriptionW, pBufW), "FileDescription should have been 'Wine version test' (unicode)\n");
398     }
399
400     HeapFree( GetProcessHeap(), 0, pVersionInfoA);
401     if (is_unicode_enabled)
402         HeapFree( GetProcessHeap(), 0, pVersionInfoW);
403 }
404
405 START_TEST(info)
406 {
407     test_info_size();
408     test_info();
409    
410     /* Test several AW-calls on a 32 bit windows executable */
411     trace("Testing 32 bit windows application\n");
412     test_32bit_win();
413 }