WinME fixes:
[wine.git] / dlls / advpack / tests / advpack.c
blob5ec4f73b99a85c5de1dc91f91e83df3d5900e7a6
1 /*
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
22 #include <stdio.h>
23 #include <windows.h>
24 #include <advpub.h>
25 #include <assert.h>
26 #include "wine/test.h"
28 #define TEST_STRING1 "C:\\Program Files\\Application Name"
29 #define TEST_STRING2 "%49001%\\Application Name"
31 static HRESULT (WINAPI *pGetVersionFromFile)(LPSTR,LPDWORD,LPDWORD,BOOL);
32 static HRESULT (WINAPI *pDelNode)(LPCSTR,DWORD);
33 static HRESULT (WINAPI *pTranslateInfString)(LPSTR,LPSTR,LPSTR,LPSTR,LPSTR,DWORD,LPDWORD,LPVOID);
35 static void version_test(void)
37 HRESULT hr;
38 DWORD major, minor;
40 major = minor = 0;
41 hr = pGetVersionFromFile("kernel32.dll", &major, &minor, FALSE);
42 ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
43 "0x%08lx\n", hr);
45 trace("kernel32.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
46 major, minor);
48 major = minor = 0;
49 hr = pGetVersionFromFile("kernel32.dll", &major, &minor, TRUE);
50 ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
51 "0x%08lx\n", hr);
53 trace("kernel32.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
54 HIWORD(minor), LOWORD(minor));
57 static void delnode_test(void)
59 HRESULT hr;
60 HANDLE hn;
61 CHAR currDir[MAX_PATH];
62 int currDirLen;
64 /* Native DelNode apparently does not support relative paths, so we use
65 absolute paths for testing */
66 currDirLen = GetCurrentDirectoryA(sizeof(currDir) / sizeof(CHAR), currDir);
67 assert(currDirLen > 0 && currDirLen < sizeof(currDir) / sizeof(CHAR));
69 if(currDir[currDirLen - 1] == '\\')
70 currDir[--currDirLen] = 0;
72 /* Simple tests; these should fail. */
73 hr = pDelNode(NULL, 0);
74 ok (hr == E_FAIL, "DelNode called with NULL pathname should return E_FAIL\n");
75 hr = pDelNode("", 0);
76 ok (hr == E_FAIL, "DelNode called with empty pathname should return E_FAIL\n");
78 /* Test deletion of a file. */
79 hn = CreateFile("DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
80 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
81 assert(hn != INVALID_HANDLE_VALUE);
82 CloseHandle(hn);
83 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestFile1"), 0);
84 ok (hr == S_OK, "DelNode failed deleting a single file\n");
85 currDir[currDirLen] = '\0';
87 /* Test deletion of an empty directory. */
88 CreateDirectoryA("DelNodeTestDir", NULL);
89 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
90 ok (hr == S_OK, "DelNode failed deleting an empty directory\n");
91 currDir[currDirLen] = '\0';
93 /* Test deletion of a directory containing one file. */
94 CreateDirectoryA("DelNodeTestDir", NULL);
95 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
96 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
97 assert(hn != INVALID_HANDLE_VALUE);
98 CloseHandle(hn);
99 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
100 ok (hr == S_OK, "DelNode failed deleting a directory containing one file\n");
101 currDir[currDirLen] = '\0';
103 /* Test deletion of a directory containing multiple files. */
104 CreateDirectoryA("DelNodeTestDir", NULL);
105 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
106 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
107 assert(hn != INVALID_HANDLE_VALUE);
108 CloseHandle(hn);
109 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile2", GENERIC_WRITE, 0, NULL,
110 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
111 assert(hn != INVALID_HANDLE_VALUE);
112 CloseHandle(hn);
113 hn = CreateFile("DelNodeTestDir\\DelNodeTestFile3", GENERIC_WRITE, 0, NULL,
114 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
115 assert(hn != INVALID_HANDLE_VALUE);
116 CloseHandle(hn);
117 hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
118 ok (hr == S_OK, "DelNode failed deleting a directory containing multiple files\n");
119 currDir[currDirLen] = '\0';
122 static void append_str(char **str, const char *data)
124 sprintf(*str, data);
125 *str += strlen(*str);
128 static void create_inf_file()
130 char data[1024];
131 char *ptr = data;
132 DWORD dwNumberOfBytesWritten;
133 HANDLE hf = CreateFile("c:\\test.inf", GENERIC_WRITE, 0, NULL,
134 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
136 append_str(&ptr, "[Version]\n");
137 append_str(&ptr, "Signature=\"$Chicago$\"\n");
138 append_str(&ptr, "[CustInstDestSection]\n");
139 append_str(&ptr, "49001=ProgramFilesDir\n");
140 append_str(&ptr, "[ProgramFilesDir]\n");
141 append_str(&ptr, "HKLM,\"Software\\Microsoft\\Windows\\CurrentVersion\",");
142 append_str(&ptr, "\"ProgramFilesDir\",,\"%%24%%\\%%LProgramF%%\"\n");
143 append_str(&ptr, "[section]\n");
144 append_str(&ptr, "NotACustomDestination=Version\n");
145 append_str(&ptr, "CustomDestination=CustInstDestSection\n");
146 append_str(&ptr, "[Options.NTx86]\n");
147 append_str(&ptr, "49001=ProgramFilesDir\n");
148 append_str(&ptr, "InstallDir=%%49001%%\\%%DefaultAppPath%%\n");
149 append_str(&ptr, "CustomHDestination=CustInstDestSection\n");
150 append_str(&ptr, "[Strings]\n");
151 append_str(&ptr, "DefaultAppPath=\"Application Name\"\n");
152 append_str(&ptr, "LProgramF=\"Program Files\"\n");
154 WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
155 CloseHandle(hf);
158 static void translateinfstring_test()
160 HRESULT hr;
161 char buffer[MAX_PATH];
162 DWORD dwSize;
164 create_inf_file();
166 /* pass in a couple invalid parameters */
167 hr = pTranslateInfString(NULL, NULL, NULL, NULL, buffer, MAX_PATH, &dwSize, NULL);
168 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got 0x%08x\n", (UINT)hr);
170 /* try to open an inf file that doesn't exist */
171 hr = pTranslateInfString("c:\\a.inf", "Options.NTx86", "Options.NTx86",
172 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
173 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || hr == E_INVALIDARG,
174 "Expected 0x80070002 or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
176 /* try a nonexistent section */
177 buffer[0] = 0;
178 hr = pTranslateInfString("c:\\test.inf", "idontexist", "Options.NTx86",
179 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
180 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
181 ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
182 ok(dwSize == 25, "Expected size 25, got %ld\n", dwSize);
184 buffer[0] = 0;
185 /* try other nonexistent section */
186 hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "idontexist",
187 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
188 ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG,
189 "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
191 buffer[0] = 0;
192 /* try nonexistent key */
193 hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "Options.NTx86",
194 "notvalid", buffer, MAX_PATH, &dwSize, NULL);
195 ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG,
196 "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
198 buffer[0] = 0;
199 /* test the behavior of pszInstallSection */
200 hr = pTranslateInfString("c:\\test.inf", "section", "Options.NTx86",
201 "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
202 ok(hr == ERROR_SUCCESS || hr == E_FAIL,
203 "Expected ERROR_SUCCESS or E_FAIL, got 0x%08x\n", (UINT)hr);
205 if(hr == ERROR_SUCCESS)
206 todo_wine
208 ok(!strcmp(buffer, TEST_STRING1), "Expected %s, got %s\n", TEST_STRING1, buffer);
209 ok(dwSize == 34, "Expected size 34, got %ld\n", dwSize);
212 buffer[0] = 0;
213 /* try without a pszInstallSection */
214 hr = pTranslateInfString("c:\\test.inf", NULL, "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);
220 DeleteFile("c:\\a.inf");
221 DeleteFile("c:\\test.inf");
224 START_TEST(advpack)
226 HMODULE hdll;
228 hdll = LoadLibraryA("advpack.dll");
229 if (!hdll)
230 return;
232 pGetVersionFromFile = (void*)GetProcAddress(hdll, "GetVersionFromFile");
233 pDelNode = (void*)GetProcAddress(hdll, "DelNode");
234 pTranslateInfString = (void*)GetProcAddress(hdll, "TranslateInfString");
235 if (!pGetVersionFromFile || !pDelNode || !pTranslateInfString)
236 return;
238 version_test();
239 delnode_test();
240 translateinfstring_test();
242 FreeLibrary(hdll);