version/tests: Check the correct variable.
[wine] / dlls / shlwapi / tests / assoc.c
1 /* Unit test suite for SHLWAPI IQueryAssociations functions
2  *
3  * Copyright 2008 Google (Lei Zhang)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdarg.h>
21
22 #include "wine/test.h"
23 #include "shlwapi.h"
24 #include "shlguid.h"
25
26 #define expect(expected, got) ok ( expected == got, "Expected %d, got %d\n", expected, got)
27 #define expect_hr(expected, got) ok ( expected == got, "Expected %08x, got %08x\n", expected, got)
28
29 static HRESULT (WINAPI *pAssocQueryStringA)(ASSOCF,ASSOCSTR,LPCSTR,LPCSTR,LPSTR,LPDWORD) = NULL;
30 static HRESULT (WINAPI *pAssocQueryStringW)(ASSOCF,ASSOCSTR,LPCWSTR,LPCWSTR,LPWSTR,LPDWORD) = NULL;
31 static HRESULT (WINAPI *pAssocCreate)(CLSID, REFIID, void **) = NULL;
32
33 /* Every version of Windows with IE should have this association? */
34 static const WCHAR dotHtml[] = { '.','h','t','m','l',0 };
35 static const WCHAR badBad[] = { 'b','a','d','b','a','d',0 };
36 static const WCHAR dotBad[] = { '.','b','a','d',0 };
37 static const WCHAR open[] = { 'o','p','e','n',0 };
38 static const WCHAR invalid[] = { 'i','n','v','a','l','i','d',0 };
39
40 static void test_getstring_bad(void)
41 {
42     HRESULT hr;
43     DWORD len;
44
45     if (!pAssocQueryStringW)
46     {
47         win_skip("AssocQueryStringW() is missing\n");
48         return;
49     }
50
51     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
52     expect_hr(E_INVALIDARG, hr);
53     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
54     ok(hr == E_FAIL ||
55        hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
56        "Unexpected result : %08x\n", hr);
57     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotBad, open, NULL, &len);
58     ok(hr == E_FAIL ||
59        hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
60        "Unexpected result : %08x\n", hr);
61     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, invalid, NULL,
62                            &len);
63     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
64        hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
65        "Unexpected result : %08x\n", hr);
66     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, NULL);
67     ok(hr == E_UNEXPECTED ||
68        hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
69        "Unexpected result : %08x\n", hr);
70
71     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
72     expect_hr(E_INVALIDARG, hr);
73     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, open, NULL,
74                            &len);
75     ok(hr == E_FAIL ||
76        hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
77        "Unexpected result : %08x\n", hr);
78     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotBad, open, NULL,
79                            &len);
80     ok(hr == E_FAIL ||
81        hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
82        "Unexpected result : %08x\n", hr);
83     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
84                            &len);
85     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
86        hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
87        hr == E_FAIL, /* Win9x/WinMe/NT4 */
88        "Unexpected result : %08x\n", hr);
89     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
90                            NULL);
91     ok(hr == E_UNEXPECTED ||
92        hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
93        "Unexpected result : %08x\n", hr);
94 }
95
96 static void test_getstring_basic(void)
97 {
98     HRESULT hr;
99     WCHAR * friendlyName;
100     WCHAR * executableName;
101     DWORD len, len2, slen;
102
103     if (!pAssocQueryStringW)
104     {
105         win_skip("AssocQueryStringW() is missing\n");
106         return;
107     }
108
109     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
110     expect_hr(S_FALSE, hr);
111     if (hr != S_FALSE)
112     {
113         skip("failed to get initial len\n");
114         return;
115     }
116
117     executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
118                                len * sizeof(WCHAR));
119     if (!executableName)
120     {
121         skip("failed to allocate memory\n");
122         return;
123     }
124
125     len2 = len;
126     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
127                            executableName, &len2);
128     expect_hr(S_OK, hr);
129     slen = lstrlenW(executableName) + 1;
130     expect(len, len2);
131     expect(len, slen);
132
133     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
134                            &len);
135     ok(hr == S_FALSE ||
136        hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* Win9x/NT4 */
137        "Unexpected result : %08x\n", hr);
138     if (hr != S_FALSE)
139     {
140         HeapFree(GetProcessHeap(), 0, executableName);
141         skip("failed to get initial len\n");
142         return;
143     }
144
145     friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
146                                len * sizeof(WCHAR));
147     if (!friendlyName)
148     {
149         HeapFree(GetProcessHeap(), 0, executableName);
150         skip("failed to allocate memory\n");
151         return;
152     }
153
154     len2 = len;
155     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
156                            friendlyName, &len2);
157     expect_hr(S_OK, hr);
158     slen = lstrlenW(friendlyName) + 1;
159     expect(len, len2);
160     expect(len, slen);
161
162     HeapFree(GetProcessHeap(), 0, executableName);
163     HeapFree(GetProcessHeap(), 0, friendlyName);
164 }
165
166 static void test_getstring_no_extra(void)
167 {
168     LONG ret;
169     HKEY hkey;
170     HRESULT hr;
171     static const CHAR dotWinetest[] = {
172         '.','w','i','n','e','t','e','s','t',0
173     };
174     static const CHAR winetestfile[] = {
175         'w','i','n','e','t','e','s','t', 'f','i','l','e',0
176     };
177     static const CHAR winetestfileAction[] = {
178         'w','i','n','e','t','e','s','t','f','i','l','e',
179         '\\','s','h','e','l','l',
180         '\\','f','o','o',
181         '\\','c','o','m','m','a','n','d',0
182     };
183     static const CHAR action[] = {
184         'n','o','t','e','p','a','d','.','e','x','e',0
185     };
186     CHAR buf[MAX_PATH];
187     DWORD len = MAX_PATH;
188
189     if (!pAssocQueryStringA)
190     {
191         win_skip("AssocQueryStringA() is missing\n");
192         return;
193     }
194
195     buf[0] = '\0';
196     ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
197     if (ret != ERROR_SUCCESS) {
198         skip("failed to create dotWinetest key\n");
199         return;
200     }
201
202     ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
203     RegCloseKey(hkey);
204     if (ret != ERROR_SUCCESS)
205     {
206         skip("failed to set dotWinetest key\n");
207         goto cleanup;
208     }
209
210     ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
211     if (ret != ERROR_SUCCESS)
212     {
213         skip("failed to create winetestfileAction key\n");
214         goto cleanup;
215     }
216
217     ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
218     RegCloseKey(hkey);
219     if (ret != ERROR_SUCCESS)
220     {
221         skip("failed to set winetestfileAction key\n");
222         goto cleanup;
223     }
224
225     hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
226     ok(hr == S_OK ||
227        hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
228        "Unexpected result : %08x\n", hr);
229     hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
230     expect_hr(S_OK, hr);
231     ok(strstr(buf, action) != NULL,
232         "got '%s' (Expected result to include 'notepad.exe')\n", buf);
233
234 cleanup:
235     SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
236     SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
237
238 }
239
240 static void test_assoc_create(void)
241 {
242     HRESULT hr;
243     IQueryAssociations *pqa;
244
245     if (!pAssocCreate)
246     {
247         win_skip("AssocCreate() is missing\n");
248         return;
249     }
250
251     hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
252     ok(hr == E_INVALIDARG, "Unexpected result : %08x\n", hr);
253
254     hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
255     ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_NOINTERFACE
256         , "Unexpected result : %08x\n", hr);
257
258     hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
259     ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_INVALIDARG
260         , "Unexpected result : %08x\n", hr);
261
262     hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
263     ok(hr == S_OK  || hr == E_NOTIMPL /* win98 */
264         , "Unexpected result : %08x\n", hr);
265     if(hr == S_OK)
266     {
267         IQueryAssociations_Release(pqa);
268     }
269
270     hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
271     ok(hr == S_OK  || hr == E_NOTIMPL /* win98 */
272         , "Unexpected result : %08x\n", hr);
273     if(hr == S_OK)
274     {
275         IQueryAssociations_Release(pqa);
276     }
277 }
278
279 START_TEST(assoc)
280 {
281     HMODULE hshlwapi;
282     hshlwapi = GetModuleHandleA("shlwapi.dll");
283     pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
284     pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
285     pAssocCreate       = (void*)GetProcAddress(hshlwapi, "AssocCreate");
286
287     test_getstring_bad();
288     test_getstring_basic();
289     test_getstring_no_extra();
290     test_assoc_create();
291 }