wintrust/tests: Fix test on win9x.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <windows.h>
25 #include <advpub.h>
26 #include <assert.h>
27 #include "wine/test.h"
28
29 /* defines for the TranslateInfString/Ex tests */
30 #define TEST_STRING1 "\\Application Name"
31 #define TEST_STRING2 "%49001%\\Application Name"
32
33 /* defines for the SetPerUserSecValues tests */
34 #define GUID_KEY    "SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\guid"
35 #define REG_VAL_EXISTS(key, value)   !RegQueryValueEx(key, value, NULL, NULL, NULL, NULL)
36 #define OPEN_GUID_KEY() !RegOpenKey(HKEY_LOCAL_MACHINE, GUID_KEY, &guid)
37
38 static HMODULE hAdvPack;
39 static HRESULT (WINAPI *pCloseINFEngine)(HINF);
40 static HRESULT (WINAPI *pDelNode)(LPCSTR,DWORD);
41 static HRESULT (WINAPI *pGetVersionFromFile)(LPCSTR,LPDWORD,LPDWORD,BOOL);
42 static HRESULT (WINAPI *pOpenINFEngine)(PCSTR,PCSTR,DWORD,HINF*,PVOID);
43 static HRESULT (WINAPI *pSetPerUserSecValues)(PPERUSERSECTION pPerUser);
44 static HRESULT (WINAPI *pTranslateInfString)(LPCSTR,LPCSTR,LPCSTR,LPCSTR,LPSTR,DWORD,LPDWORD,LPVOID);
45 static HRESULT (WINAPI *pTranslateInfStringEx)(HINF,PCSTR,PCSTR,PCSTR,PSTR,DWORD,PDWORD,PVOID);
46
47 static CHAR inf_file[MAX_PATH];
48 static CHAR PROG_FILES_ROOT[MAX_PATH];
49 static CHAR PROG_FILES[MAX_PATH];
50 static CHAR APP_PATH[MAX_PATH];
51 static DWORD APP_PATH_LEN;
52
53 static void get_progfiles_dir(void)
54 {
55     HKEY hkey;
56     DWORD size = MAX_PATH;
57
58     RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &hkey);
59     RegQueryValueExA(hkey, "ProgramFilesDir", NULL, NULL, (LPBYTE)PROG_FILES_ROOT, &size);
60     RegCloseKey(hkey);
61
62     lstrcpyA(PROG_FILES, PROG_FILES_ROOT + 3); /* skip C:\ */
63     lstrcpyA(APP_PATH, PROG_FILES_ROOT);
64     lstrcatA(APP_PATH, TEST_STRING1);
65     APP_PATH_LEN = lstrlenA(APP_PATH) + 1;
66 }
67
68 static BOOL init_function_pointers(void)
69 {
70     hAdvPack = LoadLibraryA("advpack.dll");
71
72     if (!hAdvPack)
73         return FALSE;
74
75     pCloseINFEngine = (void*)GetProcAddress(hAdvPack, "CloseINFEngine");
76     pDelNode = (void *)GetProcAddress(hAdvPack, "DelNode");
77     pGetVersionFromFile = (void *)GetProcAddress(hAdvPack, "GetVersionFromFile");
78     pOpenINFEngine = (void*)GetProcAddress(hAdvPack, "OpenINFEngine");
79     pSetPerUserSecValues = (void*)GetProcAddress(hAdvPack, "SetPerUserSecValues");
80     pTranslateInfString = (void *)GetProcAddress(hAdvPack, "TranslateInfString");
81     pTranslateInfStringEx = (void*)GetProcAddress(hAdvPack, "TranslateInfStringEx");
82
83     if (!pCloseINFEngine || !pDelNode || !pGetVersionFromFile ||
84         !pOpenINFEngine || !pSetPerUserSecValues || !pTranslateInfString)
85         return FALSE;
86
87     return TRUE;
88 }
89
90 static void version_test(void)
91 {
92     HRESULT hr;
93     DWORD major, minor;
94
95     major = minor = 0;
96     hr = pGetVersionFromFile("kernel32.dll", &major, &minor, FALSE);
97     ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
98         "0x%08x\n", hr);
99     trace("kernel32.dll Language ID: 0x%08x, Codepage ID: 0x%08x\n",
100            major, minor);
101
102     major = minor = 0;
103     hr = pGetVersionFromFile("kernel32.dll", &major, &minor, TRUE);
104     ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
105         "0x%08x\n", hr);
106     trace("kernel32.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
107           HIWORD(minor), LOWORD(minor));
108
109     major = minor = 0;
110     hr = pGetVersionFromFile("advpack.dll", &major, &minor, FALSE);
111     ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
112         "0x%08x\n", hr);
113     trace("advpack.dll Language ID: 0x%08x, Codepage ID: 0x%08x\n",
114            major, minor);
115
116     major = minor = 0;
117     hr = pGetVersionFromFile("advpack.dll", &major, &minor, TRUE);
118     ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
119         "0x%08x\n", hr);
120     trace("advpack.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
121           HIWORD(minor), LOWORD(minor));
122 }
123
124 static void delnode_test(void)
125 {
126     HRESULT hr;
127     HANDLE hn;
128     CHAR currDir[MAX_PATH];
129     int currDirLen;
130
131     /* Native DelNode apparently does not support relative paths, so we use
132        absolute paths for testing */
133     currDirLen = GetCurrentDirectoryA(sizeof(currDir) / sizeof(CHAR), currDir);
134     assert(currDirLen > 0 && currDirLen < sizeof(currDir) / sizeof(CHAR));
135
136     if(currDir[currDirLen - 1] == '\\')
137         currDir[--currDirLen] = 0;
138
139     /* Simple tests; these should fail. */
140     hr = pDelNode(NULL, 0);
141     ok (hr == E_FAIL, "DelNode called with NULL pathname should return E_FAIL\n");
142     hr = pDelNode("", 0);
143     ok (hr == E_FAIL, "DelNode called with empty pathname should return E_FAIL\n");
144
145     /* Test deletion of a file. */
146     hn = CreateFile("DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
147         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
148     assert(hn != INVALID_HANDLE_VALUE);
149     CloseHandle(hn);
150     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestFile1"), 0);
151     ok (hr == S_OK, "DelNode failed deleting a single file\n");
152     currDir[currDirLen] = '\0';
153
154     /* Test deletion of an empty directory. */
155     CreateDirectoryA("DelNodeTestDir", NULL);
156     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
157     ok (hr == S_OK, "DelNode failed deleting an empty directory\n");
158     currDir[currDirLen] = '\0';
159
160     /* Test deletion of a directory containing one file. */
161     CreateDirectoryA("DelNodeTestDir", NULL);
162     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
163         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
164     assert(hn != INVALID_HANDLE_VALUE);
165     CloseHandle(hn);
166     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
167     ok (hr == S_OK, "DelNode failed deleting a directory containing one file\n");
168     currDir[currDirLen] = '\0';
169
170     /* Test deletion of a directory containing multiple files. */
171     CreateDirectoryA("DelNodeTestDir", NULL);
172     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
173         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
174     assert(hn != INVALID_HANDLE_VALUE);
175     CloseHandle(hn);
176     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile2", GENERIC_WRITE, 0, NULL,
177         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
178     assert(hn != INVALID_HANDLE_VALUE);
179     CloseHandle(hn);
180     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile3", GENERIC_WRITE, 0, NULL,
181         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
182     assert(hn != INVALID_HANDLE_VALUE);
183     CloseHandle(hn);
184     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
185     ok (hr == S_OK, "DelNode failed deleting a directory containing multiple files\n");
186     currDir[currDirLen] = '\0';
187 }
188
189 static void append_str(char **str, const char *data, ...)
190 {
191     va_list valist;
192
193     va_start(valist, data);
194     vsprintf(*str, data, valist);
195     *str += strlen(*str);
196     va_end(valist);
197 }
198
199 static void create_inf_file(void)
200 {
201     char data[1024];
202     char *ptr = data;
203     DWORD dwNumberOfBytesWritten;
204     HANDLE hf = CreateFile(inf_file, GENERIC_WRITE, 0, NULL,
205                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
206
207     append_str(&ptr, "[Version]\n");
208     append_str(&ptr, "Signature=\"$Chicago$\"\n");
209     append_str(&ptr, "[CustInstDestSection]\n");
210     append_str(&ptr, "49001=ProgramFilesDir\n");
211     append_str(&ptr, "49010=DestA,1\n");
212     append_str(&ptr, "49020=DestB\n");
213     append_str(&ptr, "49030=DestC\n");
214     append_str(&ptr, "[ProgramFilesDir]\n");
215     append_str(&ptr, "HKLM,\"Software\\Microsoft\\Windows\\CurrentVersion\",");
216     append_str(&ptr, "\"ProgramFilesDir\",,\"%%24%%\\%%LProgramF%%\"\n");
217     append_str(&ptr, "[section]\n");
218     append_str(&ptr, "NotACustomDestination=Version\n");
219     append_str(&ptr, "CustomDestination=CustInstDestSection\n");
220     append_str(&ptr, "[Options.NTx86]\n");
221     append_str(&ptr, "49001=ProgramFilesDir\n");
222     append_str(&ptr, "InstallDir=%%49001%%\\%%DefaultAppPath%%\n");
223     append_str(&ptr, "Result1=%%49010%%\n");
224     append_str(&ptr, "Result2=%%49020%%\n");
225     append_str(&ptr, "Result3=%%49030%%\n");
226     append_str(&ptr, "CustomHDestination=CustInstDestSection\n");
227     append_str(&ptr, "[Strings]\n");
228     append_str(&ptr, "DefaultAppPath=\"Application Name\"\n");
229     append_str(&ptr, "LProgramF=\"%s\"\n", PROG_FILES);
230     append_str(&ptr, "[DestA]\n");
231     append_str(&ptr, "HKLM,\"Software\\Garbage\",\"ProgramFilesDir\",,'%%24%%\\%%LProgramF%%'\n");
232     append_str(&ptr, "[DestB]\n");
233     append_str(&ptr, "'HKLM','Software\\Microsoft\\Windows\\CurrentVersion',");
234     append_str(&ptr, "'ProgramFilesDir',,\"%%24%%\"\n");
235     append_str(&ptr, "[DestC]\n");
236     append_str(&ptr, "HKLM,\"Software\\Garbage\",\"ProgramFilesDir\",,'%%24%%'\n");
237
238     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
239     CloseHandle(hf);
240 }
241
242 static void translateinfstring_test(void)
243 {
244     HRESULT hr;
245     char buffer[MAX_PATH];
246     DWORD dwSize;
247
248     create_inf_file();
249
250     /* pass in a couple invalid parameters */
251     hr = pTranslateInfString(NULL, NULL, NULL, NULL, buffer, MAX_PATH, &dwSize, NULL);
252     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got 0x%08x\n", (UINT)hr);
253
254     /* try to open an inf file that doesn't exist */
255     hr = pTranslateInfString("c:\\a.inf", "Options.NTx86", "Options.NTx86",
256                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
257     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || hr == E_INVALIDARG || 
258        hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND), 
259        "Expected E_INVALIDARG, 0x80070002 or 0x8007007e, got 0x%08x\n", (UINT)hr);
260
261     if(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND))
262     {
263         trace("WinNT 3.51 detected. Skipping tests for TranslateInfString()\n");
264         return;
265     }
266
267     /* try a nonexistent section */
268     buffer[0] = 0;
269     hr = pTranslateInfString(inf_file, "idontexist", "Options.NTx86",
270                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
271     ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
272     ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
273     ok(dwSize == 25, "Expected size 25, got %d\n", dwSize);
274
275     buffer[0] = 0;
276     /* try other nonexistent section */
277     hr = pTranslateInfString(inf_file, "Options.NTx86", "idontexist",
278                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
279     ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG, 
280        "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
281
282     buffer[0] = 0;
283     /* try nonexistent key */
284     hr = pTranslateInfString(inf_file, "Options.NTx86", "Options.NTx86",
285                              "notvalid", buffer, MAX_PATH, &dwSize, NULL);
286     ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG, 
287        "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
288
289     buffer[0] = 0;
290     /* test the behavior of pszInstallSection */
291     hr = pTranslateInfString(inf_file, "section", "Options.NTx86",
292                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
293     ok(hr == ERROR_SUCCESS || hr == E_FAIL, 
294        "Expected ERROR_SUCCESS or E_FAIL, got 0x%08x\n", (UINT)hr);
295
296     if(hr == ERROR_SUCCESS)
297     {
298         ok(!strcmp(buffer, APP_PATH), "Expected '%s', got '%s'\n", APP_PATH, buffer);
299         ok(dwSize == APP_PATH_LEN, "Expected size %d, got %d\n", APP_PATH_LEN, dwSize);
300     }
301
302     buffer[0] = 0;
303     /* try without a pszInstallSection */
304     hr = pTranslateInfString(inf_file, NULL, "Options.NTx86",
305                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
306     ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
307     todo_wine
308     {
309         ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
310         ok(dwSize == 25, "Expected size 25, got %d\n", dwSize);
311     }
312
313     DeleteFile("c:\\a.inf");
314     DeleteFile(inf_file);
315 }
316
317 static void translateinfstringex_test(void)
318 {
319     HINF hinf;
320     HRESULT hr;
321     char buffer[MAX_PATH];
322     DWORD size = MAX_PATH;
323
324     create_inf_file();
325     
326     /* need to see if there are any flags */
327
328     /* try a NULL filename */
329     hr = pOpenINFEngine(NULL, "Options.NTx86", 0, &hinf, NULL);
330     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
331
332     /* try an empty filename */
333     hr = pOpenINFEngine("", "Options.NTx86", 0, &hinf, NULL);
334     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* NT+ */ ||
335        hr == HRESULT_FROM_WIN32(E_UNEXPECTED) /* 9x */,
336         "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND or E_UNEXPECTED), got %08x\n", hr);
337
338     /* try a NULL hinf */
339     hr = pOpenINFEngine(inf_file, "Options.NTx86", 0, NULL, NULL);
340     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
341
342     /* open the INF without the Install section specified */
343     hr = pOpenINFEngine(inf_file, NULL, 0, &hinf, NULL);
344     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
345
346     /* try a NULL hinf */
347     hr = pTranslateInfStringEx(NULL, inf_file, "Options.NTx86", "InstallDir",
348                               buffer, size, &size, NULL);
349     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
350
351     /* try a NULL filename */
352     hr = pTranslateInfStringEx(hinf, NULL, "Options.NTx86", "InstallDir",
353                               buffer, size, &size, NULL);
354     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
355
356     /* try an empty filename */
357     memset(buffer, 'a', 25);
358     buffer[24] = '\0';
359     size = MAX_PATH;
360     hr = pTranslateInfStringEx(hinf, "", "Options.NTx86", "InstallDir",
361                               buffer, size, &size, NULL);
362     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
363     todo_wine
364     {
365         ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
366         ok(size == 25, "Expected size 25, got %d\n", size);
367     }
368
369     /* try a NULL translate section */
370     hr = pTranslateInfStringEx(hinf, inf_file, NULL, "InstallDir",
371                               buffer, size, &size, NULL);
372     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
373
374     /* try an empty translate section */
375     hr = pTranslateInfStringEx(hinf, inf_file, "", "InstallDir",
376                               buffer, size, &size, NULL);
377     ok(hr == SPAPI_E_LINE_NOT_FOUND, "Expected SPAPI_E_LINE_NOT_FOUND, got %08x\n", hr);
378
379     /* try a NULL translate key */
380     hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", NULL,
381                               buffer, size, &size, NULL);
382     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
383
384     /* try an empty translate key */
385     hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", "",
386                               buffer, size, &size, NULL);
387     ok(hr == SPAPI_E_LINE_NOT_FOUND, "Expected SPAPI_E_LINE_NOT_FOUND, got %08x\n", hr);
388
389     /* successfully translate the string */
390     memset(buffer, 'a', 25);
391     buffer[24] = '\0';
392     size = MAX_PATH;
393     hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", "InstallDir",
394                               buffer, size, &size, NULL);
395     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
396     todo_wine
397     {
398         ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
399         ok(size == 25, "Expected size 25, got %d\n", size);
400     }
401
402     /* try a NULL hinf */
403     hr = pCloseINFEngine(NULL);
404     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
405
406     /* successfully close the hinf */
407     hr = pCloseINFEngine(hinf);
408     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
409
410     /* open the inf with the install section */
411     hr = pOpenINFEngine(inf_file, "section", 0, &hinf, NULL);
412     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
413
414     /* translate the string with the install section specified */
415     memset(buffer, 'a', APP_PATH_LEN);
416     buffer[APP_PATH_LEN - 1] = '\0';
417     size = MAX_PATH;
418     hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", "InstallDir",
419                               buffer, size, &size, NULL);
420     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
421     ok(!strcmp(buffer, APP_PATH), "Expected %s, got %s\n", APP_PATH, buffer);
422     ok(size == APP_PATH_LEN, "Expected size %d, got %d\n", APP_PATH_LEN, size);
423
424     /* Single quote test (Note size includes null on return from call) */
425     memset(buffer, 'a', APP_PATH_LEN);
426     buffer[APP_PATH_LEN - 1] = '\0';
427     size = MAX_PATH;
428     hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", "Result1",
429                               buffer, size, &size, NULL);
430     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
431     ok(!lstrcmpi(buffer, PROG_FILES_ROOT),
432            "Expected %s, got %s\n", PROG_FILES_ROOT, buffer);
433     ok(size == lstrlenA(PROG_FILES_ROOT)+1, "Expected size %d, got %d\n",
434            lstrlenA(PROG_FILES_ROOT)+1, size);
435
436     memset(buffer, 'a', APP_PATH_LEN);
437     buffer[APP_PATH_LEN - 1] = '\0';
438     size = MAX_PATH;
439     hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", "Result2",
440                               buffer, size, &size, NULL);
441     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
442     ok(!lstrcmpi(buffer, PROG_FILES_ROOT),
443            "Expected %s, got %s\n", PROG_FILES_ROOT, buffer);
444     ok(size == lstrlenA(PROG_FILES_ROOT)+1, "Expected size %d, got %d\n",
445            lstrlenA(PROG_FILES_ROOT)+1, size);
446
447     {
448         char drive[MAX_PATH];
449         lstrcpy(drive, PROG_FILES_ROOT);
450         drive[3] = 0x00; /* Just keep the system drive plus '\' */
451
452         memset(buffer, 'a', APP_PATH_LEN);
453         buffer[APP_PATH_LEN - 1] = '\0';
454         size = MAX_PATH;
455         hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", "Result3",
456                                   buffer, size, &size, NULL);
457         ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
458         ok(!lstrcmpi(buffer, drive),
459                "Expected %s, got %s\n", drive, buffer);
460         ok(size == lstrlenA(drive)+1, "Expected size %d, got %d\n",
461                lstrlenA(drive)+1, size);
462     }
463
464     /* close the INF again */
465     hr = pCloseINFEngine(hinf);
466     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
467
468     DeleteFileA(inf_file);
469
470     /* Create another .inf file which is just here to trigger a wine bug */
471     {
472         char data[1024];
473         char *ptr = data;
474         DWORD dwNumberOfBytesWritten;
475         HANDLE hf = CreateFile(inf_file, GENERIC_WRITE, 0, NULL,
476                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
477
478         append_str(&ptr, "[Version]\n");
479         append_str(&ptr, "Signature=\"$Chicago$\"\n");
480         append_str(&ptr, "[section]\n");
481         append_str(&ptr, "NotACustomDestination=Version\n");
482         append_str(&ptr, "CustomDestination=CustInstDestSection\n");
483         append_str(&ptr, "[CustInstDestSection]\n");
484         append_str(&ptr, "49010=DestA,1\n");
485         append_str(&ptr, "49020=DestB\n");
486         append_str(&ptr, "49030=DestC\n");
487         append_str(&ptr, "49040=DestD\n");
488         append_str(&ptr, "[Options.NTx86]\n");
489         append_str(&ptr, "Result2=%%49030%%\n");
490         append_str(&ptr, "[DestA]\n");
491         append_str(&ptr, "HKLM,\"Software\\Garbage\",\"ProgramFilesDir\",,'%%24%%'\n");
492         /* The point of this test is to have HKCU just before the quoted HKLM */
493         append_str(&ptr, "[DestB]\n");
494         append_str(&ptr, "HKCU,\"Software\\Garbage\",\"ProgramFilesDir\",,'%%24%%'\n");
495         append_str(&ptr, "[DestC]\n");
496         append_str(&ptr, "'HKLM','Software\\Microsoft\\Windows\\CurrentVersion',");
497         append_str(&ptr, "'ProgramFilesDir',,\"%%24%%\"\n");
498         append_str(&ptr, "[DestD]\n");
499         append_str(&ptr, "HKLM,\"Software\\Garbage\",\"ProgramFilesDir\",,'%%24%%'\n");
500
501         WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
502         CloseHandle(hf);
503     }
504
505     /* open the inf with the install section */
506     hr = pOpenINFEngine(inf_file, "section", 0, &hinf, NULL);
507     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
508
509     /* Single quote test (Note size includes null on return from call) */
510     memset(buffer, 'a', APP_PATH_LEN);
511     buffer[APP_PATH_LEN - 1] = '\0';
512     size = MAX_PATH;
513     hr = pTranslateInfStringEx(hinf, inf_file, "Options.NTx86", "Result2",
514                               buffer, size, &size, NULL);
515     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
516     ok(!lstrcmpi(buffer, PROG_FILES_ROOT),
517            "Expected %s, got %s\n", PROG_FILES_ROOT, buffer);
518     ok(size == lstrlenA(PROG_FILES_ROOT)+1, "Expected size %d, got %d\n",
519            lstrlenA(PROG_FILES_ROOT)+1, size);
520
521     /* close the INF again */
522     hr = pCloseINFEngine(hinf);
523     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
524
525     DeleteFileA(inf_file);
526 }
527
528 static BOOL check_reg_str(HKEY hkey, LPCSTR name, LPCSTR value)
529 {
530     DWORD size = MAX_PATH;
531     char check[MAX_PATH];
532
533     if (RegQueryValueEx(hkey, name, NULL, NULL, (LPBYTE)check, &size))
534         return FALSE;
535
536     return !lstrcmp(check, value);
537 }
538
539 static BOOL check_reg_dword(HKEY hkey, LPCSTR name, DWORD value)
540 {
541     DWORD size = sizeof(DWORD);
542     DWORD check;
543
544     if (RegQueryValueEx(hkey, name, NULL, NULL, (LPBYTE)&check, &size))
545         return FALSE;
546
547     return (check == value);
548 }
549
550 static void setperusersecvalues_test(void)
551 {
552     PERUSERSECTION peruser;
553     HRESULT hr;
554     HKEY guid;
555
556     lstrcpy(peruser.szDispName, "displayname");
557     lstrcpy(peruser.szLocale, "locale");
558     lstrcpy(peruser.szStub, "stub");
559     lstrcpy(peruser.szVersion, "1,1,1,1");
560     lstrcpy(peruser.szCompID, "compid");
561     peruser.dwIsInstalled = 1;
562     peruser.bRollback = FALSE;
563
564     /* try a NULL pPerUser */
565     if (0)
566     {
567         /* This crashes on systems with IE7 */
568         hr = pSetPerUserSecValues(NULL);
569         todo_wine
570         ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
571         ok(!OPEN_GUID_KEY(), "Expected guid key to not exist\n");
572     }
573
574     /* at the very least, szGUID must be valid */
575     peruser.szGUID[0] = '\0';
576     hr = pSetPerUserSecValues(&peruser);
577     ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
578     ok(!OPEN_GUID_KEY(), "Expected guid key to not exist\n");
579
580     /* set initial values */
581     lstrcpy(peruser.szGUID, "guid");
582     hr = pSetPerUserSecValues(&peruser);
583     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
584     ok(OPEN_GUID_KEY(), "Expected guid key to exist\n");
585     ok(check_reg_str(guid, NULL, "displayname"), "Expected displayname\n");
586     ok(check_reg_str(guid, "ComponentID", "compid"), "Expected compid\n");
587     ok(check_reg_str(guid, "Locale", "locale"), "Expected locale\n");
588     ok(check_reg_str(guid, "StubPath", "stub"), "Expected stub\n");
589     ok(check_reg_str(guid, "Version", "1,1,1,1"), "Expected 1,1,1,1\n");
590     ok(check_reg_dword(guid, "IsInstalled", 1), "Expected 1\n");
591     ok(!REG_VAL_EXISTS(guid, "OldDisplayName"), "Expected OldDisplayName to not exist\n");
592     ok(!REG_VAL_EXISTS(guid, "OldLocale"), "Expected OldLocale to not exist\n");
593     ok(!REG_VAL_EXISTS(guid, "OldStubPath"), "Expected OldStubPath to not exist\n");
594     ok(!REG_VAL_EXISTS(guid, "OldVersion"), "Expected OldVersion to not exist\n");
595     ok(!REG_VAL_EXISTS(guid, "RealStubPath"), "Expected RealStubPath to not exist\n");
596
597     /* raise the version, but bRollback is FALSE, so vals not saved */
598     lstrcpy(peruser.szVersion, "2,1,1,1");
599     hr = pSetPerUserSecValues(&peruser);
600     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
601     ok(check_reg_str(guid, NULL, "displayname"), "Expected displayname\n");
602     ok(check_reg_str(guid, "ComponentID", "compid"), "Expected compid\n");
603     ok(check_reg_str(guid, "Locale", "locale"), "Expected locale\n");
604     ok(check_reg_str(guid, "StubPath", "stub"), "Expected stub\n");
605     ok(check_reg_str(guid, "Version", "2,1,1,1"), "Expected 2,1,1,1\n");
606     ok(check_reg_dword(guid, "IsInstalled", 1), "Expected 1\n");
607     ok(!REG_VAL_EXISTS(guid, "OldDisplayName"), "Expected OldDisplayName to not exist\n");
608     ok(!REG_VAL_EXISTS(guid, "OldLocale"), "Expected OldLocale to not exist\n");
609     ok(!REG_VAL_EXISTS(guid, "OldStubPath"), "Expected OldStubPath to not exist\n");
610     ok(!REG_VAL_EXISTS(guid, "OldVersion"), "Expected OldVersion to not exist\n");
611     ok(!REG_VAL_EXISTS(guid, "RealStubPath"), "Expected RealStubPath to not exist\n");
612
613     /* raise the version again, bRollback is TRUE so vals are saved */
614     peruser.bRollback = TRUE;
615     lstrcpy(peruser.szVersion, "3,1,1,1");
616     hr = pSetPerUserSecValues(&peruser);
617     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
618     ok(check_reg_str(guid, NULL, "displayname"), "Expected displayname\n");
619     ok(check_reg_str(guid, "ComponentID", "compid"), "Expected compid\n");
620     ok(check_reg_str(guid, "Locale", "locale"), "Expected locale\n");
621     ok(check_reg_dword(guid, "IsInstalled", 1), "Expected 1\n");
622     ok(check_reg_str(guid, "Version", "3,1,1,1"), "Expected 3,1,1,1\n");
623     todo_wine
624     {
625         ok(check_reg_str(guid, "OldDisplayName", "displayname"), "Expected displayname\n");
626         ok(check_reg_str(guid, "OldLocale", "locale"), "Expected locale\n");
627         ok(check_reg_str(guid, "RealStubPath", "stub"), "Expected stub\n");
628         ok(check_reg_str(guid, "OldStubPath", "stub"), "Expected stub\n");
629         ok(check_reg_str(guid, "OldVersion", "2,1,1,1"), "Expected 2,1,1,1\n");
630         ok(check_reg_str(guid, "StubPath",
631            "rundll32.exe advpack.dll,UserInstStubWrapper guid"),
632            "Expected real stub\n");
633     }
634
635     RegDeleteKey(HKEY_LOCAL_MACHINE, GUID_KEY);
636 }
637
638 START_TEST(advpack)
639 {
640     if (!init_function_pointers())
641         return;
642
643     /* Make sure we create the temporary file in a directory
644      * were we have enough rights
645      */
646     GetTempPath(MAX_PATH, inf_file);
647     lstrcat(inf_file,"test.inf");
648
649     get_progfiles_dir();
650
651     version_test();
652     delnode_test();
653     setperusersecvalues_test();
654     translateinfstring_test();
655     translateinfstringex_test();
656
657     FreeLibrary(hAdvPack);
658 }