1 /* Unit test suite for various shell Association objects
2  *
3  * Copyright 2012 Detlef Riekenberg
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 "precomp.h"
21 
22 #include <shobjidl.h>
23 
24 static void test_IQueryAssociations_QueryInterface(void)
25 {
26     IQueryAssociations *qa;
27     IQueryAssociations *qa2;
28     IUnknown *unk;
29     HRESULT hr;
30 
31     hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&qa);
32     ok(hr == S_OK, "got 0x%08x\n", hr);
33 
34     hr = IQueryAssociations_QueryInterface(qa, &IID_IQueryAssociations, (void**)&qa2);
35     ok(hr == S_OK, "QueryInterface (IQueryAssociations) returned 0x%x\n", hr);
36     if (SUCCEEDED(hr)) {
37         IQueryAssociations_Release(qa2);
38     }
39 
40     hr = IQueryAssociations_QueryInterface(qa, &IID_IUnknown, (void**)&unk);
41     ok(hr == S_OK, "QueryInterface (IUnknown) returned 0x%x\n", hr);
42     if (SUCCEEDED(hr)) {
43         IUnknown_Release(unk);
44     }
45 
46     hr = IQueryAssociations_QueryInterface(qa, &IID_IUnknown, NULL);
47     ok(hr == E_POINTER, "got 0x%x (expected E_POINTER)\n", hr);
48 
49     IQueryAssociations_Release(qa);
50 }
51 
52 
53 static void test_IApplicationAssociationRegistration_QueryInterface(IApplicationAssociationRegistration *appreg)
54 {
55     IApplicationAssociationRegistration *appreg2;
56     IUnknown *unk;
57     HRESULT hr;
58 
59     hr = IApplicationAssociationRegistration_QueryInterface(appreg, &IID_IApplicationAssociationRegistration,
60        (void**)&appreg2);
61     ok(hr == S_OK, "QueryInterface (IApplicationAssociationRegistration) returned 0x%x\n", hr);
62     if (SUCCEEDED(hr)) {
63         IApplicationAssociationRegistration_Release(appreg2);
64     }
65 
66     hr = IApplicationAssociationRegistration_QueryInterface(appreg, &IID_IUnknown, (void**)&unk);
67     ok(hr == S_OK, "QueryInterface (IUnknown) returned 0x%x\n", hr);
68     if (SUCCEEDED(hr)) {
69         IUnknown_Release(unk);
70     }
71 
72     hr = IApplicationAssociationRegistration_QueryInterface(appreg, &IID_IUnknown, NULL);
73     ok(hr == E_POINTER, "got 0x%x (expected E_POINTER)\n", hr);
74 }
75 
76 struct assoc_getstring_test
77 {
78     const WCHAR *key;
79     ASSOCF       flags;
80     ASSOCSTR     str;
81     DWORD        len;
82     HRESULT      hr;
83     HRESULT      brokenhr;
84 };
85 
86 static const WCHAR httpW[] = {'h','t','t','p',0};
87 static const WCHAR badW[] = {'b','a','d','b','a','d',0};
88 
89 static struct assoc_getstring_test getstring_tests[] =
90 {
91     { httpW, 0, ASSOCSTR_EXECUTABLE, 2, 0x8007007a /* E_NOT_SUFFICIENT_BUFFER */, S_OK },
92     { httpW, ASSOCF_NOTRUNCATE, ASSOCSTR_EXECUTABLE, 2, E_POINTER },
93     { NULL }
94 };
95 
96 static void getstring_test(LPCWSTR assocName, HKEY progIdKey, ASSOCSTR str, LPCWSTR expected_string, int line)
97 {
98     IQueryAssociations *assoc;
99     HRESULT hr;
100     WCHAR *buffer = NULL;
101     DWORD len;
102 
103     hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&assoc);
104     ok_(__FILE__, line)(hr == S_OK, "failed to create IQueryAssociations, 0x%x\n", hr);
105     hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, assocName, progIdKey, NULL);
106     ok_(__FILE__, line)(hr == S_OK, "IQueryAssociations::Init failed, 0x%x\n", hr);
107 
108     hr = IQueryAssociations_GetString(assoc, ASSOCF_NONE, str, NULL, NULL, &len);
109     if (expected_string) {
110         ok_(__FILE__, line)(hr == S_FALSE, "GetString returned 0x%x, expected S_FALSE\n", hr);
111         if (hr != S_FALSE) {
112             /* don't try to allocate memory using uninitialized len */
113             IQueryAssociations_Release(assoc);
114             return;
115         }
116 
117         buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
118         ok_(__FILE__, line)(buffer != NULL, "out of memory\n");
119         hr = IQueryAssociations_GetString(assoc, 0, str, NULL, buffer, &len);
120         ok_(__FILE__, line)(hr == S_OK, "GetString returned 0x%x, expected S_OK\n", hr);
121 
122         ok_(__FILE__, line)(lstrcmpW(buffer, expected_string) == 0, "GetString returned %s, expected %s\n",
123                 wine_dbgstr_w(buffer), wine_dbgstr_w(expected_string));
124     } else {
125         ok_(__FILE__, line)(FAILED(hr), "GetString returned 0x%x, expected failure\n", hr);
126     }
127 
128     IQueryAssociations_Release(assoc);
129     HeapFree(GetProcessHeap(), 0, buffer);
130 }
131 
132 static void test_IQueryAssociations_GetString(void)
133 {
134     static WCHAR test_extensionW[] = {'.','t','e','s','t',0};
135     static WCHAR test_progidW[] = {'t','e','s','t','f','i','l','e',0};
136     static WCHAR DefaultIconW[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
137     /* folder.ico, why not */
138     static WCHAR test_iconW[] = {'s','h','e','l','l','3','2','.','d','l','l',',','1',0};
139     HKEY test_extension_key;
140     HKEY test_progid_key;
141     HKEY test_defaulticon_key;
142     LRESULT r;
143 
144     struct assoc_getstring_test *ptr = getstring_tests;
145     IQueryAssociations *assoc;
146     HRESULT hr;
147     DWORD len;
148     int i = 0;
149 
150     r = RegCreateKeyExW(HKEY_CLASSES_ROOT, test_extensionW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &test_extension_key, NULL);
151     if (r == ERROR_ACCESS_DENIED)
152     {
153         win_skip("Not enough permissions to create a test key.\n");
154         return;
155     }
156 
157     ok(r == ERROR_SUCCESS, "RegCreateKeyExW(HKCR, \".test\") failed: 0x%lx\n", r);
158     r = RegSetValueExW(test_extension_key, NULL, 0, REG_SZ, (PBYTE)test_progidW, sizeof(test_progidW));
159     ok(r == ERROR_SUCCESS, "RegSetValueExW(HKCR\\.test, NULL, \"testfile\") failed: 0x%lx\n", r);
160 
161     /* adding progid key with no information should fail to return information */
162     r = RegCreateKeyExW(HKEY_CLASSES_ROOT, test_progidW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &test_progid_key, NULL);
163     ok(r == ERROR_SUCCESS, "RegCreateKeyExW(HKCR, \"testfile\") failed: 0x%lx\n", r);
164     getstring_test(test_extensionW, NULL, ASSOCSTR_DEFAULTICON, NULL, __LINE__);
165     getstring_test(test_progidW, NULL, ASSOCSTR_DEFAULTICON, NULL, __LINE__);
166     getstring_test(NULL, test_progid_key, ASSOCSTR_DEFAULTICON, NULL, __LINE__);
167 
168     /* adding information to the progid should return that information */
169     r = RegCreateKeyExW(test_progid_key, DefaultIconW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &test_defaulticon_key, NULL);
170     ok(r == ERROR_SUCCESS, "RegCreateKeyExW(HKCR\\testfile\\DefaultIcon) failed: 0x%lx\n", r);
171     r = RegSetValueExW(test_defaulticon_key, NULL, 0, REG_SZ, (PBYTE)test_iconW, sizeof(test_iconW));
172     ok(r == ERROR_SUCCESS, "RegSetValueExW(HKCR\\testfile\\DefaultIcon, NULL, \"folder.ico\") failed: 0x%lx\n", r);
173     getstring_test(test_extensionW, NULL, ASSOCSTR_DEFAULTICON, test_iconW, __LINE__);
174     getstring_test(test_progidW, NULL, ASSOCSTR_DEFAULTICON, test_iconW, __LINE__);
175     getstring_test(NULL, test_progid_key, ASSOCSTR_DEFAULTICON, test_iconW, __LINE__);
176 
177     RegDeleteKeyW(test_progid_key, DefaultIconW);
178     RegDeleteKeyW(HKEY_CLASSES_ROOT, test_progidW);
179     RegDeleteKeyW(HKEY_CLASSES_ROOT, test_extensionW);
180 
181     hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&assoc);
182     ok(hr == S_OK, "failed to create object, 0x%x\n", hr);
183 
184     hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, httpW, NULL, NULL);
185     ok(hr == S_OK, "Init failed, 0x%x\n", hr);
186 
187     len = 0;
188     hr = IQueryAssociations_GetString(assoc, ASSOCF_NONE, ASSOCSTR_EXECUTABLE, NULL, NULL, &len);
189     ok(hr == S_FALSE, "got 0x%08x\n", hr);
190     ok(len > 0, "got wrong needed length, %d\n", len);
191 
192     while (ptr->key)
193     {
194         WCHAR buffW[MAX_PATH];
195         DWORD len;
196 
197         hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, ptr->key, NULL, NULL);
198         ok(hr == S_OK, "%d: Init failed, 0x%x\n", i, hr);
199 
200         len = ptr->len;
201         buffW[0] = ptr->flags & ASSOCF_NOTRUNCATE ? 0x1 : 0;
202         hr = IQueryAssociations_GetString(assoc, ptr->flags, ptr->str, NULL, buffW, &len);
203         if (hr != ptr->hr)
204             ok(broken(hr == ptr->brokenhr), "%d: GetString failed, 0x%08x\n", i, hr);
205         else
206         {
207             ok(hr == ptr->hr, "%d: GetString failed, 0x%08x\n", i, hr);
208             ok(len > ptr->len, "%d: got needed length %d\n", i, len);
209         }
210 
211         /* even with ASSOCF_NOTRUNCATE it's null terminated */
212         if ((ptr->flags & ASSOCF_NOTRUNCATE) && (ptr->len > 0))
213             ok(buffW[0] == 0 || broken(buffW[0] == 0x1) /* pre win7 */, "%d: got %x\n", i, buffW[0]);
214 
215         if (!(ptr->flags & ASSOCF_NOTRUNCATE) && ptr->len && FAILED(ptr->hr))
216             ok(buffW[0] != 0, "%d: got %x\n", i, buffW[0]);
217 
218         i++;
219         ptr++;
220     }
221 
222     IQueryAssociations_Release(assoc);
223 }
224 
225 static void test_IQueryAssociations_Init(void)
226 {
227     IQueryAssociations *assoc;
228     HRESULT hr;
229     DWORD len;
230 
231     hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&assoc);
232     ok(hr == S_OK, "failed to create object, 0x%x\n", hr);
233 
234     hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, NULL, NULL, NULL);
235     ok(hr == E_INVALIDARG, "Init failed, 0x%08x\n", hr);
236 
237     hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, httpW, NULL, NULL);
238     ok(hr == S_OK, "Init failed, 0x%08x\n", hr);
239 
240     hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, badW, NULL, NULL);
241     ok(hr == S_OK || broken(hr == S_FALSE) /* pre-vista */, "Init failed, 0x%08x\n", hr);
242 
243     len = 0;
244     hr = IQueryAssociations_GetString(assoc, ASSOCF_NONE, ASSOCSTR_EXECUTABLE, NULL, NULL, &len);
245     ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || broken(hr == E_FAIL) /* pre-vista */, "got 0x%08x\n", hr);
246 
247     IQueryAssociations_Release(assoc);
248 }
249 
250 static void test_IApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration *appreg)
251 {
252     static const WCHAR emptyW[] = {0};
253     static const WCHAR txtW[] = {'.','t','x','t',0};
254     static const WCHAR spacetxtW[] = {' ','.','t','x','t',0};
255     HRESULT hr;
256     LPWSTR assocprog = NULL;
257 
258     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_URLPROTOCOL, AL_EFFECTIVE, &assocprog);
259     ok(hr == E_INVALIDARG, "got 0x%x\n", hr);
260 
261     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_FILEEXTENSION, AL_EFFECTIVE, &assocprog);
262     ok(hr == E_INVALIDARG, "got 0x%x\n", hr);
263 
264     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, spacetxtW, AT_FILEEXTENSION, AL_EFFECTIVE, &assocprog);
265     ok(hr == E_INVALIDARG || hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /*Win8*/, "got 0x%x\n", hr);
266 
267     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, httpW, AT_URLPROTOCOL, AL_EFFECTIVE, NULL);
268     ok(hr == E_INVALIDARG, "got 0x%x\n", hr);
269 
270     /* AT_FILEEXTENSION must start with a period */
271     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, txtW, AT_FILEEXTENSION, AL_EFFECTIVE, &assocprog);
272     ok(hr == S_OK, "got 0x%x\n", hr);
273     trace("%s\n", wine_dbgstr_w(assocprog));
274     CoTaskMemFree(assocprog);
275 
276     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_STARTMENUCLIENT, AL_EFFECTIVE, &assocprog);
277     ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), "got 0x%x\n", hr);
278 
279     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_MIMETYPE, AL_EFFECTIVE, &assocprog);
280     ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), "got 0x%x\n", hr);
281 
282     hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, httpW, AT_URLPROTOCOL, AL_EFFECTIVE, &assocprog);
283     todo_wine ok(hr == S_OK, "got 0x%x\n", hr);
284     trace("%s\n", wine_dbgstr_w(assocprog));
285 
286     CoTaskMemFree(assocprog);
287 }
288 
289 START_TEST(assoc)
290 {
291     IQueryAssociations *qa;
292     IApplicationAssociationRegistration *appreg = NULL;
293     HRESULT hr;
294 
295     CoInitialize(NULL);
296 
297     /* this works since XP */
298     hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&qa);
299     if (hr == S_OK)
300     {
301         test_IQueryAssociations_QueryInterface();
302         test_IQueryAssociations_GetString();
303         test_IQueryAssociations_Init();
304 
305         IQueryAssociations_Release(qa);
306     }
307     else
308         win_skip("IQueryAssociations not supported, 0x%x\n", hr);
309 
310     /* this works since Vista */
311     hr = CoCreateInstance(&CLSID_ApplicationAssociationRegistration, NULL, CLSCTX_INPROC_SERVER,
312                           &IID_IApplicationAssociationRegistration, (LPVOID*)&appreg);
313     if (hr == S_OK)
314     {
315         test_IApplicationAssociationRegistration_QueryInterface(appreg);
316         test_IApplicationAssociationRegistration_QueryCurrentDefault(appreg);
317 
318         IApplicationAssociationRegistration_Release(appreg);
319     }
320     else
321         win_skip("IApplicationAssociationRegistration not supported: 0x%x\n", hr);
322 
323     CoUninitialize();
324 }
325