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