msacm: Czech language resources encoding fix.
[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 *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);
34
35 static void version_test(void)
36 {
37     HRESULT hr;
38     DWORD major, minor;
39
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);
44     trace("kernel32.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
45            major, minor);
46
47     major = minor = 0;
48     hr = pGetVersionFromFile("kernel32.dll", &major, &minor, TRUE);
49     ok (hr == S_OK, "GetVersionFromFileEx(kernel32.dll) failed, returned "
50         "0x%08lx\n", hr);
51     trace("kernel32.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
52           HIWORD(minor), LOWORD(minor));
53
54     major = minor = 0;
55     hr = pGetVersionFromFile("advpack.dll", &major, &minor, FALSE);
56     ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
57         "0x%08lx\n", hr);
58     trace("advpack.dll Language ID: 0x%08lx, Codepage ID: 0x%08lx\n",
59            major, minor);
60
61     major = minor = 0;
62     hr = pGetVersionFromFile("advpack.dll", &major, &minor, TRUE);
63     ok (hr == S_OK, "GetVersionFromFileEx(advpack.dll) failed, returned "
64         "0x%08lx\n", hr);
65     trace("advpack.dll version: %d.%d.%d.%d\n", HIWORD(major), LOWORD(major),
66           HIWORD(minor), LOWORD(minor));
67 }
68
69 static void delnode_test(void)
70 {
71     HRESULT hr;
72     HANDLE hn;
73     CHAR currDir[MAX_PATH];
74     int currDirLen;
75
76     /* Native DelNode apparently does not support relative paths, so we use
77        absolute paths for testing */
78     currDirLen = GetCurrentDirectoryA(sizeof(currDir) / sizeof(CHAR), currDir);
79     assert(currDirLen > 0 && currDirLen < sizeof(currDir) / sizeof(CHAR));
80
81     if(currDir[currDirLen - 1] == '\\')
82         currDir[--currDirLen] = 0;
83
84     /* Simple tests; these should fail. */
85     hr = pDelNode(NULL, 0);
86     ok (hr == E_FAIL, "DelNode called with NULL pathname should return E_FAIL\n");
87     hr = pDelNode("", 0);
88     ok (hr == E_FAIL, "DelNode called with empty pathname should return E_FAIL\n");
89
90     /* Test deletion of a file. */
91     hn = CreateFile("DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
92         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
93     assert(hn != INVALID_HANDLE_VALUE);
94     CloseHandle(hn);
95     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestFile1"), 0);
96     ok (hr == S_OK, "DelNode failed deleting a single file\n");
97     currDir[currDirLen] = '\0';
98
99     /* Test deletion of an empty directory. */
100     CreateDirectoryA("DelNodeTestDir", NULL);
101     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
102     ok (hr == S_OK, "DelNode failed deleting an empty directory\n");
103     currDir[currDirLen] = '\0';
104
105     /* Test deletion of a directory containing one file. */
106     CreateDirectoryA("DelNodeTestDir", NULL);
107     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
108         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
109     assert(hn != INVALID_HANDLE_VALUE);
110     CloseHandle(hn);
111     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
112     ok (hr == S_OK, "DelNode failed deleting a directory containing one file\n");
113     currDir[currDirLen] = '\0';
114
115     /* Test deletion of a directory containing multiple files. */
116     CreateDirectoryA("DelNodeTestDir", NULL);
117     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile1", GENERIC_WRITE, 0, NULL,
118         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
119     assert(hn != INVALID_HANDLE_VALUE);
120     CloseHandle(hn);
121     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile2", GENERIC_WRITE, 0, NULL,
122         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
123     assert(hn != INVALID_HANDLE_VALUE);
124     CloseHandle(hn);
125     hn = CreateFile("DelNodeTestDir\\DelNodeTestFile3", GENERIC_WRITE, 0, NULL,
126         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
127     assert(hn != INVALID_HANDLE_VALUE);
128     CloseHandle(hn);
129     hr = pDelNode(lstrcat(currDir, "\\DelNodeTestDir"), 0);
130     ok (hr == S_OK, "DelNode failed deleting a directory containing multiple files\n");
131     currDir[currDirLen] = '\0';
132 }
133
134 static void append_str(char **str, const char *data)
135 {
136     sprintf(*str, data);
137     *str += strlen(*str);
138 }
139
140 static void create_inf_file()
141 {
142     char data[1024];
143     char *ptr = data;
144     DWORD dwNumberOfBytesWritten;
145     HANDLE hf = CreateFile("c:\\test.inf", GENERIC_WRITE, 0, NULL,
146                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
147
148     append_str(&ptr, "[Version]\n");
149     append_str(&ptr, "Signature=\"$Chicago$\"\n");
150     append_str(&ptr, "[CustInstDestSection]\n");
151     append_str(&ptr, "49001=ProgramFilesDir\n");
152     append_str(&ptr, "[ProgramFilesDir]\n");
153     append_str(&ptr, "HKLM,\"Software\\Microsoft\\Windows\\CurrentVersion\",");
154     append_str(&ptr, "\"ProgramFilesDir\",,\"%%24%%\\%%LProgramF%%\"\n");
155     append_str(&ptr, "[section]\n");
156     append_str(&ptr, "NotACustomDestination=Version\n");
157     append_str(&ptr, "CustomDestination=CustInstDestSection\n");
158     append_str(&ptr, "[Options.NTx86]\n");
159     append_str(&ptr, "49001=ProgramFilesDir\n");
160     append_str(&ptr, "InstallDir=%%49001%%\\%%DefaultAppPath%%\n");
161     append_str(&ptr, "CustomHDestination=CustInstDestSection\n");
162     append_str(&ptr, "[Strings]\n");
163     append_str(&ptr, "DefaultAppPath=\"Application Name\"\n");
164     append_str(&ptr, "LProgramF=\"Program Files\"\n");
165
166     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
167     CloseHandle(hf);
168 }
169
170 static void translateinfstring_test()
171 {
172     HRESULT hr;
173     char buffer[MAX_PATH];
174     DWORD dwSize;
175
176     create_inf_file();
177
178     /* pass in a couple invalid parameters */
179     hr = pTranslateInfString(NULL, NULL, NULL, NULL, buffer, MAX_PATH, &dwSize, NULL);
180     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got 0x%08x\n", (UINT)hr);
181
182     /* try to open an inf file that doesn't exist */
183     hr = pTranslateInfString("c:\\a.inf", "Options.NTx86", "Options.NTx86",
184                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
185     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || hr == E_INVALIDARG || 
186        hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND), 
187        "Expected E_INVALIDARG, 0x80070002 or 0x8007007e, got 0x%08x\n", (UINT)hr);
188
189     if(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND))
190     {
191         trace("WinNT 3.51 detected. Skipping tests for TranslateInfString()\n");
192         return;
193     }
194
195     /* try a nonexistent section */
196     buffer[0] = 0;
197     hr = pTranslateInfString("c:\\test.inf", "idontexist", "Options.NTx86",
198                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
199     ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
200     ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
201     ok(dwSize == 25, "Expected size 25, got %ld\n", dwSize);
202
203     buffer[0] = 0;
204     /* try other nonexistent section */
205     hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "idontexist",
206                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
207     ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG, 
208        "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
209
210     buffer[0] = 0;
211     /* try nonexistent key */
212     hr = pTranslateInfString("c:\\test.inf", "Options.NTx86", "Options.NTx86",
213                              "notvalid", buffer, MAX_PATH, &dwSize, NULL);
214     ok(hr == SPAPI_E_LINE_NOT_FOUND || hr == E_INVALIDARG, 
215        "Expected SPAPI_E_LINE_NOT_FOUND or E_INVALIDARG, got 0x%08x\n", (UINT)hr);
216
217     buffer[0] = 0;
218     /* test the behavior of pszInstallSection */
219     hr = pTranslateInfString("c:\\test.inf", "section", "Options.NTx86",
220                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
221     ok(hr == ERROR_SUCCESS || hr == E_FAIL, 
222        "Expected ERROR_SUCCESS or E_FAIL, got 0x%08x\n", (UINT)hr);
223
224     if(hr == ERROR_SUCCESS)
225     todo_wine
226     {
227         HKEY key;
228         DWORD len = MAX_PATH;
229         char cmpbuffer[MAX_PATH];
230         LONG res = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &key);
231         if(res == ERROR_SUCCESS) {
232             res = RegQueryValueExA(key, "ProgramFilesDir", NULL, NULL, (LPBYTE)cmpbuffer, &len);
233             if(res == ERROR_SUCCESS) {
234                 strcat(cmpbuffer, TEST_STRING1);
235                 ok(!strcmp(buffer, cmpbuffer), "Expected '%s', got '%s'\n", cmpbuffer, buffer);
236                 ok(dwSize == (strlen(cmpbuffer)+1), "Expected size %d, got %ld\n",
237                    strlen(cmpbuffer)+1, dwSize);
238             }
239             RegCloseKey(key);
240         }
241     }
242
243     buffer[0] = 0;
244     /* try without a pszInstallSection */
245     hr = pTranslateInfString("c:\\test.inf", NULL, "Options.NTx86",
246                              "InstallDir", buffer, MAX_PATH, &dwSize, NULL);
247     ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", (UINT)hr);
248     ok(!strcmp(buffer, TEST_STRING2), "Expected %s, got %s\n", TEST_STRING2, buffer);
249     ok(dwSize == 25, "Expected size 25, got %ld\n", dwSize);
250
251     DeleteFile("c:\\a.inf");
252     DeleteFile("c:\\test.inf");
253 }
254
255 START_TEST(advpack)
256 {
257     HMODULE hdll;
258
259     hdll = LoadLibraryA("advpack.dll");
260     if (!hdll)
261         return;
262
263     pGetVersionFromFile = (void*)GetProcAddress(hdll, "GetVersionFromFile");
264     pDelNode = (void*)GetProcAddress(hdll, "DelNode");
265     pTranslateInfString = (void*)GetProcAddress(hdll, "TranslateInfString");
266     if (!pGetVersionFromFile || !pDelNode || !pTranslateInfString)
267         return;
268
269     version_test();
270     delnode_test();
271     translateinfstring_test();
272
273     FreeLibrary(hdll);
274 }