comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / advpack / tests / advpack.c
1 /*
2  * Unit tests for advpack.dll
3  *
4  * Copyright (C) 2005 Robert Reif
5  * Copyright (C) 2005 Sami Aario
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdio.h>
23 #include <windows.h>
24 #include <advpub.h>
25 #include <assert.h>
26 #include "wine/test.h"
27
28 #define TEST_STRING1 "\\Application Name"
29 #define TEST_STRING2 "%49001%\\Application Name"
30
31 static HRESULT (WINAPI *pCloseINFEngine)(HINF);
32 static HRESULT (WINAPI *pDelNode)(LPCSTR,DWORD);
33 static HRESULT (WINAPI *pGetVersionFromFile)(LPSTR,LPDWORD,LPDWORD,BOOL);
34 static HRESULT (WINAPI *pOpenINFEngine)(PCSTR,PCSTR,DWORD,HINF*,PVOID);
35 static HRESULT (WINAPI *pTranslateInfString)(LPSTR,LPSTR,LPSTR,LPSTR,LPSTR,DWORD,LPDWORD,LPVOID);
36 static HRESULT (WINAPI *pTranslateInfStringEx)(HINF,PCSTR,PCSTR,PCSTR,PSTR,DWORD,PDWORD,PVOID);
37
38 static CHAR PROG_FILES[MAX_PATH];
39 static DWORD PROG_FILES_LEN;
40
41 static void get_progfiles_dir(void)
42 {
43     HKEY hkey;
44     DWORD size = MAX_PATH;
45
46     RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &hkey);
47     RegQueryValueExA(hkey, "ProgramFilesDir", NULL, NULL, (LPBYTE)PROG_FILES, &size);
48     RegCloseKey(hkey);
49
50     lstrcatA(PROG_FILES, TEST_STRING1);
51     PROG_FILES_LEN = lstrlenA(PROG_FILES) + 1;
52 }
53
54 static BOOL init_function_pointers(void)
55 {
56     HMODULE hAdvPack = LoadLibraryA("advpack.dll");
57
58     if (!hAdvPack)
59         return FALSE;
60
61     pCloseINFEngine = (void*)GetProcAddress(hAdvPack, "CloseINFEngine");
62     pDelNode = (void *)GetProcAddress(hAdvPack, "DelNode");
63     pGetVersionFromFile = (void *)GetProcAddress(hAdvPack, "GetVersionFromFile");
64     pOpenINFEngine = (void*)GetProcAddress(hAdvPack, "OpenINFEngine");
65     pTranslateInfString = (void *)GetProcAddress(hAdvPack, "TranslateInfString");
66     pTranslateInfStringEx = (void*)GetProcAddress(hAdvPack, "TranslateInfStringEx");
67
68     if (!pCloseINFEngine || !pDelNode || !pGetVersionFromFile ||
69         !pOpenINFEngine || !pTranslateInfString)
70         return FALSE;
71
72     return TRUE;
73 }
74
75 static void version_test(void)
76 {
77     HRESULT hr;
78     DWORD major, minor;
79
80     major = minor = 0;
81     hr = pGetVersionFromFile("kernel32.dll", &major, &minor, FALSE);
82     ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
83         "0x%08lx\n", hr);
84     trace("kernel32.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
85            major, minor);
86
87     major = minor = 0;
88     hr = pGetVersionFromFile("kernel32.dll", &major, &minor, TRUE);
89     ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
90         "0x%08lx\n", hr);
91     trace("kernel32.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
92           HIWORD(minor), LOWORD(minor));
93
94     major = minor = 0;
95     hr = pGetVersionFromFile("advpack.dll", &major, &minor, FALSE);
96     ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
97         "0x%08lx\n", hr);
98     trace("advpack.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
99            major, minor);
100
101     major = minor = 0;
102     hr = pGetVersionFromFile("advpack.dll", &major, &minor, TRUE);
103     ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
104         "0x%08lx\n", hr);
105     trace("advpack.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
106           HIWORD(minor), LOWORD(minor));
107 }
108
109 static void delnode_test(void)
110 {
111     HRESULT hr;
112     HANDLE hn;
113     CHAR currDir[MAX_PATH];
114     int currDirLen;
115
116     /* Native DelNode apparently does not support relative paths, so we use
117        absolute paths for testing */
118     currDirLen = GetCurrentDirectoryA(sizeof(currDir) / sizeof(CHAR), currDir);
119     assert(currDirLen > 0 && currDirLen < sizeof(currDir) / sizeof(CHAR));
120
121     if(currDir[currDirLen - 1] == '\\')
122         currDir[--currDirLen] = 0;
123
124     /* Simple tests; these should fail. */
125     hr = pDelNode(NULL, 0);
126     ok (hr == E_FAIL, "DelNode called with NULL pathname should return E_FAIL\n");
127     hr = pDelNode("", 0);
128     ok (hr == E_FAIL, "DelNode called with empty pathname should return E_FAIL\n");
129
130     /* Test deletion of a file. */
131     hn = CreateFile("DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
132         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
133     assert(hn != INVALID_HANDLE_VALUE);
134     CloseHandle(hn);
135     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestFile1"), 0);
136     ok (hr == S_OK, "DelNode failed deleting a single file\n");
137     currDir[currDirLen] = '\0';
138
139     /* Test deletion of an empty directory. */
140     CreateDirectoryA("DelNodeTestDir", NULL);
141     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
142     ok (hr == S_OK, "DelNode failed deleting an empty directory\n");
143     currDir[currDirLen] = '\0';
144
145     /* Test deletion of a directory containing one file. */
146     CreateDirectoryA("DelNodeTestDir", NULL);
147     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
148         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
149     assert(hn != INVALID_HANDLE_VALUE);
150     CloseHandle(hn);
151     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
152     ok (hr == S_OK, "DelNode failed deleting a directory containing one file\n");
153     currDir[currDirLen] = '\0';
154
155     /* Test deletion of a directory containing multiple files. */
156     CreateDirectoryA("DelNodeTestDir", NULL);
157     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
158         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
159     assert(hn != INVALID_HANDLE_VALUE);
160     CloseHandle(hn);
161     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile2", GENERIC_WRITE, 0, NULL,
162         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
163     assert(hn != INVALID_HANDLE_VALUE);
164     CloseHandle(hn);
165     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile3", GENERIC_WRITE, 0, NULL,
166         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
167     assert(hn != INVALID_HANDLE_VALUE);
168     CloseHandle(hn);
169     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
170     ok (hr == S_OK, "DelNode failed deleting a directory containing multiple files\n");
171     currDir[currDirLen] = '\0';
172 }
173
174 static void append_str(char **str, const char *data)
175 {
176     sprintf(*str, data);
177     *str += strlen(*str);
178 }
179
180 static void create_inf_file()
181 {
182     char data[1024];
183     char *ptr = data;
184     DWORD dwNumberOfBytesWritten;
185     HANDLE hf = CreateFile("c:\\test.inf", GENERIC_WRITE, 0, NULL,
186                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
187
188     append_str(&ptr, "[Version]\n");
189     append_str(&ptr, "Signature=\"$Chicago$\"\n");
190     append_str(&ptr, "[CustInstDestSection]\n");
191     append_str(&ptr, "49001=ProgramFilesDir\n");
192     append_str(&ptr, "[ProgramFilesDir]\n");
193     append_str(&ptr, "HKLM,\"Software\\Microsoft\\Windows\\CurrentVersion\",");
194     append_str(&ptr, "\"ProgramFilesDir\",,\"%%24%%\\%%LProgramF%%\"\n");
195     append_str(&ptr, "[section]\n");
196     append_str(&ptr, "NotACustomDestination=Version\n");
197     append_str(&ptr, "CustomDestination=CustInstDestSection\n");
198     append_str(&ptr, "[Options.NTx86]\n");
199     append_str(&ptr, "49001=ProgramFilesDir\n");
200     append_str(&ptr, "InstallDir=%%49001%%\\%%DefaultAppPath%%\n");
201     append_str(&ptr, "CustomHDestination=CustInstDestSection\n");
202     append_str(&ptr, "[Strings]\n");
203     append_str(&ptr, "DefaultAppPath=\"Application Name\"\n");
204     append_str(&ptr, "LProgramF=\"Program Files\"\n");
205
206     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
207     CloseHandle(hf);
208 }
209
210 static void translateinfstring_test()
211 {
212     HRESULT hr;
213     char buffer[MAX_PATH];
214     DWORD dwSize;
215
216     create_inf_file();
217
218     /* pass in a couple invalid parameters */
219     hr = pTranslateInfString(NULL, NULL, NULL, NULL, buffer, MAX_PATH, &dwSize, NULL);
220     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got 0x%08x\n", (UINT)hr);
221
222     /* try to open an inf file that doesn't exist */
223     hr = pTranslateInfString("c:\\a.inf", "Options.NTx86", "Options.NTx86",
224                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
225     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || hr == E_INVALIDARG || 
226        hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND), 
227        "Expected E_INVALIDARG, 0x80070002 or 0x8007007e, got 0x%08x\n", (UINT)hr);
228
229     if(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND))
230     {
231         trace("WinNT 3.51 detected. Skipping tests for TranslateInfString()\n");
232         return;
233     }
234
235     /* try a nonexistent section */
236     buffer[0] = 0;
237     hr = pTranslateInfString("c:\\test.inf", "idontexist", "Options.NTx86",
238                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
239     ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
240     ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
241     ok(dwSize == 25, "Expected size 25, got %ld\n", dwSize);
242
243     buffer[0] = 0;
244     /* try other nonexistent section */
245     hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "idontexist",
246                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
247     ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG, 
248        "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
249
250     buffer[0] = 0;
251     /* try nonexistent key */
252     hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "Options.NTx86",
253                              "notvalid", buffer, MAX_PATH, &dwSize, NULL);
254     ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG, 
255        "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
256
257     buffer[0] = 0;
258     /* test the behavior of pszInstallSection */
259     hr = pTranslateInfString("c:\\test.inf", "section", "Options.NTx86",
260                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
261     ok(hr == ERROR_SUCCESS || hr == E_FAIL, 
262        "Expected ERROR_SUCCESS or E_FAIL, got 0x%08x\n", (UINT)hr);
263
264     if(hr == ERROR_SUCCESS)
265     {
266         ok(!strcmp(buffer, PROG_FILES), "Expected '%s', got '%s'\n", PROG_FILES, buffer);
267         ok(dwSize == PROG_FILES_LEN, "Expected size %ld, got %ld\n", PROG_FILES_LEN, dwSize);
268     }
269
270     buffer[0] = 0;
271     /* try without a pszInstallSection */
272     hr = pTranslateInfString("c:\\test.inf", NULL, "Options.NTx86",
273                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
274     ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
275     todo_wine
276     {
277         ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
278         ok(dwSize == 25, "Expected size 25, got %ld\n", dwSize);
279     }
280
281     DeleteFile("c:\\a.inf");
282     DeleteFile("c:\\test.inf");
283 }
284
285 static void translateinfstringex_test(void)
286 {
287     HINF hinf;
288     HRESULT hr;
289     char buffer[MAX_PATH];
290     DWORD size = MAX_PATH;
291
292     create_inf_file();
293     
294     /* need to see if there are any flags */
295
296     /* try a NULL filename */
297     hr = pOpenINFEngine(NULL, "Options.NTx86", 0, &hinf, NULL);
298     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
299
300     /* try an empty filename */
301     hr = pOpenINFEngine("", "Options.NTx86", 0, &hinf, NULL);
302     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
303         "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %ld\n", hr);
304
305     /* try a NULL hinf */
306     hr = pOpenINFEngine("c:\\test.inf", "Options.NTx86", 0, NULL, NULL);
307     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
308
309     /* open the INF without the Install section specified */
310     hr = pOpenINFEngine("c:\\test.inf", NULL, 0, &hinf, NULL);
311     ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
312
313     /* try a NULL hinf */
314     hr = pTranslateInfStringEx(NULL, "c:\\test.inf", "Options.NTx86", "InstallDir",
315                               buffer, size, &size, NULL);
316     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
317
318     /* try a NULL filename */
319     hr = pTranslateInfStringEx(hinf, NULL, "Options.NTx86", "InstallDir",
320                               buffer, size, &size, NULL);
321     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
322
323     /* try an empty filename */
324     size = MAX_PATH;
325     hr = pTranslateInfStringEx(hinf, "", "Options.NTx86", "InstallDir",
326                               buffer, size, &size, NULL);
327     ok(hr == S_OK, "Expected S_OK, got %08x\n", (UINT)hr);
328     todo_wine
329     {
330         ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
331         ok(size == 25, "Expected size 25, got %ld\n", size);
332     }
333
334     /* try a NULL translate section */
335     hr = pTranslateInfStringEx(hinf, "c:\\test.inf", NULL, "InstallDir",
336                               buffer, size, &size, NULL);
337     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
338
339     /* try an empty translate section */
340     hr = pTranslateInfStringEx(hinf, "c:\\test.inf", "", "InstallDir",
341                               buffer, size, &size, NULL);
342     ok(hr == SPAPI_E_LINE_NOT_FOUND, "Expected SPAPI_E_LINE_NOT_FOUND, got %ld\n", hr);
343
344     /* try a NULL translate key */
345     hr = pTranslateInfStringEx(hinf, "c:\\test.inf", "Options.NTx86", NULL,
346                               buffer, size, &size, NULL);
347     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
348
349     /* try an empty translate key */
350     hr = pTranslateInfStringEx(hinf, "c:\\test.inf", "Options.NTx86", "",
351                               buffer, size, &size, NULL);
352     ok(hr == SPAPI_E_LINE_NOT_FOUND, "Expected SPAPI_E_LINE_NOT_FOUND, got %ld\n", hr);
353
354     /* successfully translate the string */
355     size = MAX_PATH;
356     hr = pTranslateInfStringEx(hinf, "c:\\test.inf", "Options.NTx86", "InstallDir",
357                               buffer, size, &size, NULL);
358     ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
359     todo_wine
360     {
361         ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
362         ok(size == 25, "Expected size 25, got %ld\n", size);
363     }
364
365     /* try a NULL hinf */
366     hr = pCloseINFEngine(NULL);
367     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
368
369     /* successfully close the hinf */
370     hr = pCloseINFEngine(hinf);
371     ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
372
373     /* open the inf with the install section */
374     hr = pOpenINFEngine("c:\\test.inf", "section", 0, &hinf, NULL);
375     ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
376
377     /* translate the string with the install section specified */
378     size = MAX_PATH;
379     hr = pTranslateInfStringEx(hinf, "c:\\test.inf", "Options.NTx86", "InstallDir",
380                               buffer, size, &size, NULL);
381     ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
382     ok(!strcmp(buffer, PROG_FILES), "Expected %s, got %s\n", PROG_FILES, buffer);
383     ok(size == PROG_FILES_LEN, "Expected size %ld, got %ld\n", PROG_FILES_LEN, size);
384
385     /* close the INF again */
386     hr = pCloseINFEngine(hinf);
387     ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
388
389     DeleteFileA("c:\\test.inf");
390 }
391
392 START_TEST(advpack)
393 {
394     if (!init_function_pointers())
395         return;
396
397     get_progfiles_dir();
398
399     version_test();
400     delnode_test();
401     translateinfstring_test();
402     translateinfstringex_test();
403 }