2 * Unit tests for advpack.dll
4 * Copyright (C) 2005 Robert Reif
5 * Copyright (C) 2005 Sami Aario
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.
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.
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
26 #include "wine/test.h"
28 #define TEST_STRING1 "\\Application Name"
29 #define TEST_STRING2 "%49001%\\Application Name"
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);
38 static CHAR PROG_FILES[MAX_PATH];
39 static DWORD PROG_FILES_LEN;
41 static void get_progfiles_dir(void)
44 DWORD size = MAX_PATH;
46 RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &hkey);
47 RegQueryValueExA(hkey, "ProgramFilesDir", NULL, NULL, (LPBYTE)PROG_FILES, &size);
50 lstrcatA(PROG_FILES, TEST_STRING1);
51 PROG_FILES_LEN = lstrlenA(PROG_FILES) + 1;
54 static BOOL init_function_pointers(void)
56 HMODULE hAdvPack = LoadLibraryA("advpack.dll");
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");
68 if (!pCloseINFEngine || !pDelNode || !pGetVersionFromFile ||
69 !pOpenINFEngine || !pTranslateInfString)
75 static void version_test(void)
81 hr = pGetVersionFromFile("kernel32.dll", &major, &minor, FALSE);
82 ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
84 trace("kernel32.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
88 hr = pGetVersionFromFile("kernel32.dll", &major, &minor, TRUE);
89 ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
91 trace("kernel32.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
92 HIWORD(minor), LOWORD(minor));
95 hr = pGetVersionFromFile("advpack.dll", &major, &minor, FALSE);
96 ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
98 trace("advpack.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
102 hr = pGetVersionFromFile("advpack.dll", &major, &minor, TRUE);
103 ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
105 trace("advpack.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
106 HIWORD(minor), LOWORD(minor));
109 static void delnode_test(void)
113 CHAR currDir[MAX_PATH];
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));
121 if(currDir[currDirLen - 1] == '\\')
122 currDir[--currDirLen] = 0;
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");
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);
135 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestFile1"), 0);
136 ok (hr == S_OK, "DelNode failed deleting a single file\n");
137 currDir[currDirLen] = '\0';
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';
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);
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';
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);
161 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile2", GENERIC_WRITE, 0, NULL,
162 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
163 assert(hn != INVALID_HANDLE_VALUE);
165 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile3", GENERIC_WRITE, 0, NULL,
166 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
167 assert(hn != INVALID_HANDLE_VALUE);
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';
174 static void append_str(char **str, const char *data)
177 *str += strlen(*str);
180 static void create_inf_file()
184 DWORD dwNumberOfBytesWritten;
185 HANDLE hf = CreateFile("c:\\test.inf", GENERIC_WRITE, 0, NULL,
186 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
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");
206 WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
210 static void translateinfstring_test()
213 char buffer[MAX_PATH];
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);
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);
229 if(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND))
231 trace("WinNT 3.51 detected. Skipping tests for TranslateInfString()\n");
235 /* try a nonexistent section */
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);
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);
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);
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);
264 if(hr == ERROR_SUCCESS)
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);
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);
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);
281 DeleteFile("c:\\a.inf");
282 DeleteFile("c:\\test.inf");
285 static void translateinfstringex_test(void)
289 char buffer[MAX_PATH];
290 DWORD size = MAX_PATH;
294 /* need to see if there are any flags */
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);
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);
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);
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);
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);
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);
323 /* try an empty filename */
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);
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);
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);
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);
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);
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);
354 /* successfully translate the string */
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);
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);
365 /* try a NULL hinf */
366 hr = pCloseINFEngine(NULL);
367 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
369 /* successfully close the hinf */
370 hr = pCloseINFEngine(hinf);
371 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
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);
377 /* translate the string with the install section specified */
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);
385 /* close the INF again */
386 hr = pCloseINFEngine(hinf);
387 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
389 DeleteFileA("c:\\test.inf");
394 if (!init_function_pointers())
401 translateinfstring_test();
402 translateinfstringex_test();