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 *pDelNode)(LPCSTR,DWORD);
32 static HRESULT (WINAPI *pGetVersionFromFile)(LPSTR,LPDWORD,LPDWORD,BOOL);
33 static HRESULT (WINAPI *pTranslateInfString)(LPSTR,LPSTR,LPSTR,LPSTR,LPSTR,DWORD,LPDWORD,LPVOID);
35 static BOOL init_function_pointers(void)
37 HMODULE hAdvPack = LoadLibraryA("advpack.dll");
42 pDelNode = (void *)GetProcAddress(hAdvPack, "DelNode");
43 pGetVersionFromFile = (void *)GetProcAddress(hAdvPack, "GetVersionFromFile");
44 pTranslateInfString = (void *)GetProcAddress(hAdvPack, "TranslateInfString");
46 if (!pDelNode || !pGetVersionFromFile || !pTranslateInfString)
52 static void version_test(void)
58 hr = pGetVersionFromFile("kernel32.dll", &major, &minor, FALSE);
59 ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
61 trace("kernel32.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
65 hr = pGetVersionFromFile("kernel32.dll", &major, &minor, TRUE);
66 ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
68 trace("kernel32.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
69 HIWORD(minor), LOWORD(minor));
72 hr = pGetVersionFromFile("advpack.dll", &major, &minor, FALSE);
73 ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
75 trace("advpack.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
79 hr = pGetVersionFromFile("advpack.dll", &major, &minor, TRUE);
80 ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
82 trace("advpack.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
83 HIWORD(minor), LOWORD(minor));
86 static void delnode_test(void)
90 CHAR currDir[MAX_PATH];
93 /* Native DelNode apparently does not support relative paths, so we use
94 absolute paths for testing */
95 currDirLen = GetCurrentDirectoryA(sizeof(currDir) / sizeof(CHAR), currDir);
96 assert(currDirLen > 0 && currDirLen < sizeof(currDir) / sizeof(CHAR));
98 if(currDir[currDirLen - 1] == '\\')
99 currDir[--currDirLen] = 0;
101 /* Simple tests; these should fail. */
102 hr = pDelNode(NULL, 0);
103 ok (hr == E_FAIL, "DelNode called with NULL pathname should return E_FAIL\n");
104 hr = pDelNode("", 0);
105 ok (hr == E_FAIL, "DelNode called with empty pathname should return E_FAIL\n");
107 /* Test deletion of a file. */
108 hn = CreateFile("DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
109 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
110 assert(hn != INVALID_HANDLE_VALUE);
112 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestFile1"), 0);
113 ok (hr == S_OK, "DelNode failed deleting a single file\n");
114 currDir[currDirLen] = '\0';
116 /* Test deletion of an empty directory. */
117 CreateDirectoryA("DelNodeTestDir", NULL);
118 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
119 ok (hr == S_OK, "DelNode failed deleting an empty directory\n");
120 currDir[currDirLen] = '\0';
122 /* Test deletion of a directory containing one file. */
123 CreateDirectoryA("DelNodeTestDir", NULL);
124 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
125 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
126 assert(hn != INVALID_HANDLE_VALUE);
128 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
129 ok (hr == S_OK, "DelNode failed deleting a directory containing one file\n");
130 currDir[currDirLen] = '\0';
132 /* Test deletion of a directory containing multiple files. */
133 CreateDirectoryA("DelNodeTestDir", NULL);
134 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
135 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
136 assert(hn != INVALID_HANDLE_VALUE);
138 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile2", GENERIC_WRITE, 0, NULL,
139 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
140 assert(hn != INVALID_HANDLE_VALUE);
142 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile3", GENERIC_WRITE, 0, NULL,
143 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
144 assert(hn != INVALID_HANDLE_VALUE);
146 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
147 ok (hr == S_OK, "DelNode failed deleting a directory containing multiple files\n");
148 currDir[currDirLen] = '\0';
151 static void append_str(char **str, const char *data)
154 *str += strlen(*str);
157 static void create_inf_file()
161 DWORD dwNumberOfBytesWritten;
162 HANDLE hf = CreateFile("c:\\test.inf", GENERIC_WRITE, 0, NULL,
163 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
165 append_str(&ptr, "[Version]\n");
166 append_str(&ptr, "Signature=\"$Chicago$\"\n");
167 append_str(&ptr, "[CustInstDestSection]\n");
168 append_str(&ptr, "49001=ProgramFilesDir\n");
169 append_str(&ptr, "[ProgramFilesDir]\n");
170 append_str(&ptr, "HKLM,\"Software\\Microsoft\\Windows\\CurrentVersion\",");
171 append_str(&ptr, "\"ProgramFilesDir\",,\"%%24%%\\%%LProgramF%%\"\n");
172 append_str(&ptr, "[section]\n");
173 append_str(&ptr, "NotACustomDestination=Version\n");
174 append_str(&ptr, "CustomDestination=CustInstDestSection\n");
175 append_str(&ptr, "[Options.NTx86]\n");
176 append_str(&ptr, "49001=ProgramFilesDir\n");
177 append_str(&ptr, "InstallDir=%%49001%%\\%%DefaultAppPath%%\n");
178 append_str(&ptr, "CustomHDestination=CustInstDestSection\n");
179 append_str(&ptr, "[Strings]\n");
180 append_str(&ptr, "DefaultAppPath=\"Application Name\"\n");
181 append_str(&ptr, "LProgramF=\"Program Files\"\n");
183 WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
187 static void translateinfstring_test()
190 char buffer[MAX_PATH];
195 /* pass in a couple invalid parameters */
196 hr = pTranslateInfString(NULL, NULL, NULL, NULL, buffer, MAX_PATH, &dwSize, NULL);
197 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got 0x%08x\n", (UINT)hr);
199 /* try to open an inf file that doesn't exist */
200 hr = pTranslateInfString("c:\\a.inf", "Options.NTx86", "Options.NTx86",
201 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
202 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || hr == E_INVALIDARG ||
203 hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND),
204 "Expected E_INVALIDARG, 0x80070002 or 0x8007007e, got 0x%08x\n", (UINT)hr);
206 if(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND))
208 trace("WinNT 3.51 detected. Skipping tests for TranslateInfString()\n");
212 /* try a nonexistent section */
214 hr = pTranslateInfString("c:\\test.inf", "idontexist", "Options.NTx86",
215 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
216 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
217 ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
218 ok(dwSize == 25, "Expected size 25, got %ld\n", dwSize);
221 /* try other nonexistent section */
222 hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "idontexist",
223 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
224 ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG,
225 "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
228 /* try nonexistent key */
229 hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "Options.NTx86",
230 "notvalid", buffer, MAX_PATH, &dwSize, NULL);
231 ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG,
232 "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
235 /* test the behavior of pszInstallSection */
236 hr = pTranslateInfString("c:\\test.inf", "section", "Options.NTx86",
237 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
238 ok(hr == ERROR_SUCCESS || hr == E_FAIL,
239 "Expected ERROR_SUCCESS or E_FAIL, got 0x%08x\n", (UINT)hr);
241 if(hr == ERROR_SUCCESS)
244 DWORD len = MAX_PATH;
245 char cmpbuffer[MAX_PATH];
246 LONG res = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &key);
247 if(res == ERROR_SUCCESS) {
248 res = RegQueryValueExA(key, "ProgramFilesDir", NULL, NULL, (LPBYTE)cmpbuffer, &len);
249 if(res == ERROR_SUCCESS) {
250 strcat(cmpbuffer, TEST_STRING1);
251 ok(!strcmp(buffer, cmpbuffer), "Expected '%s', got '%s'\n", cmpbuffer, buffer);
252 ok(dwSize == (strlen(cmpbuffer)+1), "Expected size %d, got %ld\n",
253 strlen(cmpbuffer)+1, dwSize);
260 /* try without a pszInstallSection */
261 hr = pTranslateInfString("c:\\test.inf", NULL, "Options.NTx86",
262 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
263 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
266 ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
267 ok(dwSize == 25, "Expected size 25, got %ld\n", dwSize);
270 DeleteFile("c:\\a.inf");
271 DeleteFile("c:\\test.inf");
276 if (!init_function_pointers())
281 translateinfstring_test();