Release 1.5.29.
[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        hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
83        "Unexpected result : %08x\n", hr);
84     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
85                            &len);
86     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
87        hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
88        hr == E_FAIL, /* Win9x/WinMe/NT4 */
89        "Unexpected result : %08x\n", hr);
90     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
91                            NULL);
92     ok(hr == E_UNEXPECTED ||
93        hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
94        "Unexpected result : %08x\n", hr);
95 }
96
97 static void test_getstring_basic(void)
98 {
99     HRESULT hr;
100     WCHAR * friendlyName;
101     WCHAR * executableName;
102     DWORD len, len2, slen;
103
104     if (!pAssocQueryStringW)
105     {
106         win_skip("AssocQueryStringW() is missing\n");
107         return;
108     }
109
110     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
111     expect_hr(S_FALSE, hr);
112     if (hr != S_FALSE)
113     {
114         skip("failed to get initial len\n");
115         return;
116     }
117
118     executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
119                                len * sizeof(WCHAR));
120     if (!executableName)
121     {
122         skip("failed to allocate memory\n");
123         return;
124     }
125
126     len2 = len;
127     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
128                            executableName, &len2);
129     expect_hr(S_OK, hr);
130     slen = lstrlenW(executableName) + 1;
131     expect(len, len2);
132     expect(len, slen);
133
134     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
135                            &len);
136     ok(hr == S_FALSE ||
137        hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* Win9x/NT4 */ ||
138        hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
139        "Unexpected result : %08x\n", hr);
140     if (hr != S_FALSE)
141     {
142         HeapFree(GetProcessHeap(), 0, executableName);
143         skip("failed to get initial len\n");
144         return;
145     }
146
147     friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
148                                len * sizeof(WCHAR));
149     if (!friendlyName)
150     {
151         HeapFree(GetProcessHeap(), 0, executableName);
152         skip("failed to allocate memory\n");
153         return;
154     }
155
156     len2 = len;
157     hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
158                            friendlyName, &len2);
159     expect_hr(S_OK, hr);
160     slen = lstrlenW(friendlyName) + 1;
161     expect(len, len2);
162     expect(len, slen);
163
164     HeapFree(GetProcessHeap(), 0, executableName);
165     HeapFree(GetProcessHeap(), 0, friendlyName);
166 }
167
168 static void test_getstring_no_extra(void)
169 {
170     LONG ret;
171     HKEY hkey;
172     HRESULT hr;
173     static const CHAR dotWinetest[] = {
174         '.','w','i','n','e','t','e','s','t',0
175     };
176     static const CHAR winetestfile[] = {
177         'w','i','n','e','t','e','s','t', 'f','i','l','e',0
178     };
179     static const CHAR winetestfileAction[] = {
180         'w','i','n','e','t','e','s','t','f','i','l','e',
181         '\\','s','h','e','l','l',
182         '\\','f','o','o',
183         '\\','c','o','m','m','a','n','d',0
184     };
185     static const CHAR action[] = {
186         'n','o','t','e','p','a','d','.','e','x','e',0
187     };
188     CHAR buf[MAX_PATH];
189     DWORD len = MAX_PATH;
190
191     if (!pAssocQueryStringA)
192     {
193         win_skip("AssocQueryStringA() is missing\n");
194         return;
195     }
196
197     buf[0] = '\0';
198     ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
199     if (ret != ERROR_SUCCESS) {
200         skip("failed to create dotWinetest key\n");
201         return;
202     }
203
204     ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
205     RegCloseKey(hkey);
206     if (ret != ERROR_SUCCESS)
207     {
208         skip("failed to set dotWinetest key\n");
209         goto cleanup;
210     }
211
212     ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
213     if (ret != ERROR_SUCCESS)
214     {
215         skip("failed to create winetestfileAction key\n");
216         goto cleanup;
217     }
218
219     ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
220     RegCloseKey(hkey);
221     if (ret != ERROR_SUCCESS)
222     {
223         skip("failed to set winetestfileAction key\n");
224         goto cleanup;
225     }
226
227     hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
228     ok(hr == S_OK ||
229        hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
230        "Unexpected result : %08x\n", hr);
231     hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
232     expect_hr(S_OK, hr);
233     ok(strstr(buf, action) != NULL,
234         "got '%s' (Expected result to include 'notepad.exe')\n", buf);
235
236 cleanup:
237     SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
238     SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
239
240 }
241
242 static void test_assoc_create(void)
243 {
244     HRESULT hr;
245     IQueryAssociations *pqa;
246
247     if (!pAssocCreate)
248     {
249         win_skip("AssocCreate() is missing\n");
250         return;
251     }
252
253     hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
254     ok(hr == E_INVALIDARG, "Unexpected result : %08x\n", hr);
255
256     hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
257     ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_NOINTERFACE
258         , "Unexpected result : %08x\n", hr);
259
260     hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
261     ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_INVALIDARG
262         , "Unexpected result : %08x\n", hr);
263
264     hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
265     ok(hr == S_OK  || hr == E_NOTIMPL /* win98 */
266         , "Unexpected result : %08x\n", hr);
267     if(hr == S_OK)
268     {
269         IQueryAssociations_Release(pqa);
270     }
271
272     hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
273     ok(hr == S_OK  || hr == E_NOTIMPL /* win98 */
274         , "Unexpected result : %08x\n", hr);
275     if(hr == S_OK)
276     {
277         IQueryAssociations_Release(pqa);
278     }
279 }
280
281 START_TEST(assoc)
282 {
283     HMODULE hshlwapi;
284     hshlwapi = GetModuleHandleA("shlwapi.dll");
285     pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
286     pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
287     pAssocCreate       = (void*)GetProcAddress(hshlwapi, "AssocCreate");
288
289     test_getstring_bad();
290     test_getstring_basic();
291     test_getstring_no_extra();
292     test_assoc_create();
293 }