c4250389f9b71df57361e24b511ff0f196e6cf2b
[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;
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, 0, assocName, progIdKey, NULL);
113 ok_(__FILE__, line)(hr == S_OK, "IQueryAssociations::Init failed, 0x%x\n", hr);
114
115 hr = IQueryAssociations_GetString(assoc, 0, str, NULL, NULL, &len);
116 if (hr != S_FALSE) {
117 if (expected_string) {
118 ok_(__FILE__, line)(SUCCEEDED(hr), "GetString returned 0x%x, expected success\n", hr);
119 } else {
120 ok_(__FILE__, line)(FAILED(hr), "GetString returned 0x%x, expected failure\n", hr);
121 }
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
128 if (expected_string) {
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 }
132 }
133
134 static void test_IQueryAssociations_GetString(void)
135 {
136 static WCHAR test_extensionW[] = {'.','t','e','s','t',0};
137 static WCHAR test_progidW[] = {'t','e','s','t','f','i','l','e',0};
138 static WCHAR DefaultIconW[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
139 /* folder.ico, why not */
140 static WCHAR test_iconW[] = {'s','h','e','l','l','3','2','.','d','l','l',',','1',0};
141 HKEY test_extension_key;
142 HKEY test_progid_key;
143 HKEY test_defaulticon_key;
144 LRESULT r;
145
146 struct assoc_getstring_test *ptr = getstring_tests;
147 IQueryAssociations *assoc;
148 HRESULT hr;
149 DWORD len;
150 int i = 0;
151
152 r = RegCreateKeyExW(HKEY_CLASSES_ROOT, test_extensionW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &test_extension_key, NULL);
153 if (r == ERROR_ACCESS_DENIED)
154 {
155 win_skip("Not enough permissions to create a test key.\n");
156 return;
157 }
158
159 ok(r == ERROR_SUCCESS, "RegCreateKeyExW(HKCR, \".test\") failed: 0x%lx\n", r);
160 r = RegSetValueExW(test_extension_key, NULL, 0, REG_SZ, (PBYTE)test_progidW, sizeof(test_progidW));
161 ok(r == ERROR_SUCCESS, "RegSetValueExW(HKCR\\.test, NULL, \"testfile\") failed: 0x%lx\n", r);
162
163 /* adding progid key with no information should fail to return information */
164 r = RegCreateKeyExW(HKEY_CLASSES_ROOT, test_progidW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &test_progid_key, NULL);
165 ok(r == ERROR_SUCCESS, "RegCreateKeyExW(HKCR, \"testfile\") failed: 0x%lx\n", r);
166 getstring_test(test_extensionW, NULL, ASSOCSTR_DEFAULTICON, NULL, __LINE__);
167 getstring_test(test_progidW, NULL, ASSOCSTR_DEFAULTICON, NULL, __LINE__);
168 getstring_test(NULL, test_progid_key, ASSOCSTR_DEFAULTICON, NULL, __LINE__);
169
170 /* adding information to the progid should return that information */
171 r = RegCreateKeyExW(test_progid_key, DefaultIconW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &test_defaulticon_key, NULL);
172 ok(r == ERROR_SUCCESS, "RegCreateKeyExW(HKCR\\testfile\\DefaultIcon) failed: 0x%lx\n", r);
173 r = RegSetValueExW(test_defaulticon_key, NULL, 0, REG_SZ, (PBYTE)test_iconW, sizeof(test_iconW));
174 ok(r == ERROR_SUCCESS, "RegSetValueExW(HKCR\\testfile\\DefaultIcon, NULL, \"folder.ico\") failed: 0x%lx\n", r);
175 getstring_test(test_extensionW, NULL, ASSOCSTR_DEFAULTICON, test_iconW, __LINE__);
176 getstring_test(test_progidW, NULL, ASSOCSTR_DEFAULTICON, test_iconW, __LINE__);
177 getstring_test(NULL, test_progid_key, ASSOCSTR_DEFAULTICON, test_iconW, __LINE__);
178
179 RegDeleteKeyW(HKEY_CLASSES_ROOT, test_extensionW);
180 RegDeleteKeyW(HKEY_CLASSES_ROOT, test_progidW);
181
182 hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&assoc);
183 ok(hr == S_OK, "failed to create object, 0x%x\n", hr);
184
185 hr = IQueryAssociations_Init(assoc, 0, httpW, NULL, NULL);
186 ok(hr == S_OK, "Init failed, 0x%x\n", hr);
187
188 len = 0;
189 hr = IQueryAssociations_GetString(assoc, 0, ASSOCSTR_EXECUTABLE, NULL, NULL, &len);
190 ok(hr == S_FALSE, "got 0x%08x\n", hr);
191 ok(len > 0, "got wrong needed length, %d\n", len);
192
193 while (ptr->key)
194 {
195 WCHAR buffW[MAX_PATH];
196 DWORD len;
197
198 hr = IQueryAssociations_Init(assoc, 0, ptr->key, NULL, NULL);
199 ok(hr == S_OK, "%d: Init failed, 0x%x\n", i, hr);
200
201 len = ptr->len;
202 buffW[0] = ptr->flags & ASSOCF_NOTRUNCATE ? 0x1 : 0;
203 hr = IQueryAssociations_GetString(assoc, ptr->flags, ptr->str, NULL, buffW, &len);
204 if (hr != ptr->hr)
205 ok(broken(hr == ptr->brokenhr), "%d: GetString failed, 0x%08x\n", i, hr);
206 else
207 {
208 ok(hr == ptr->hr, "%d: GetString failed, 0x%08x\n", i, hr);
209 ok(len > ptr->len, "%d: got needed length %d\n", i, len);
210 }
211
212 /* even with ASSOCF_NOTRUNCATE it's null terminated */
213 if ((ptr->flags & ASSOCF_NOTRUNCATE) && (ptr->len > 0))
214 ok(buffW[0] == 0 || broken(buffW[0] == 0x1) /* pre win7 */, "%d: got %x\n", i, buffW[0]);
215
216 if (!(ptr->flags & ASSOCF_NOTRUNCATE) && ptr->len && FAILED(ptr->hr))
217 ok(buffW[0] != 0, "%d: got %x\n", i, buffW[0]);
218
219 i++;
220 ptr++;
221 }
222
223 IQueryAssociations_Release(assoc);
224 }
225
226 static void test_IQueryAssociations_Init(void)
227 {
228 IQueryAssociations *assoc;
229 HRESULT hr;
230 DWORD len;
231
232 hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&assoc);
233 ok(hr == S_OK, "failed to create object, 0x%x\n", hr);
234
235 hr = IQueryAssociations_Init(assoc, 0, NULL, NULL, NULL);
236 ok(hr == E_INVALIDARG, "Init failed, 0x%08x\n", hr);
237
238 hr = IQueryAssociations_Init(assoc, 0, httpW, NULL, NULL);
239 ok(hr == S_OK, "Init failed, 0x%08x\n", hr);
240
241 hr = IQueryAssociations_Init(assoc, 0, badW, NULL, NULL);
242 ok(hr == S_OK || broken(hr == S_FALSE) /* pre-vista */, "Init failed, 0x%08x\n", hr);
243
244 len = 0;
245 hr = IQueryAssociations_GetString(assoc, 0, ASSOCSTR_EXECUTABLE, NULL, NULL, &len);
246 ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || broken(hr == E_FAIL) /* pre-vista */, "got 0x%08x\n", hr);
247
248 IQueryAssociations_Release(assoc);
249 }
250
251 static void test_IApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration *appreg)
252 {
253 static const WCHAR emptyW[] = {0};
254 static const WCHAR txtW[] = {'.','t','x','t',0};
255 static const WCHAR spacetxtW[] = {' ','.','t','x','t',0};
256 HRESULT hr;
257 LPWSTR assocprog = NULL;
258
259 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_URLPROTOCOL, AL_EFFECTIVE, &assocprog);
260 ok(hr == E_INVALIDARG, "got 0x%x\n", hr);
261
262 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_FILEEXTENSION, AL_EFFECTIVE, &assocprog);
263 ok(hr == E_INVALIDARG, "got 0x%x\n", hr);
264
265 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, spacetxtW, AT_FILEEXTENSION, AL_EFFECTIVE, &assocprog);
266 ok(hr == E_INVALIDARG || hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /*Win8*/, "got 0x%x\n", hr);
267
268 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, httpW, AT_URLPROTOCOL, AL_EFFECTIVE, NULL);
269 ok(hr == E_INVALIDARG, "got 0x%x\n", hr);
270
271 /* AT_FILEEXTENSION must start with a period */
272 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, txtW, AT_FILEEXTENSION, AL_EFFECTIVE, &assocprog);
273 ok(hr == S_OK, "got 0x%x\n", hr);
274 trace("%s\n", wine_dbgstr_w(assocprog));
275 CoTaskMemFree(assocprog);
276
277 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_STARTMENUCLIENT, AL_EFFECTIVE, &assocprog);
278 ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), "got 0x%x\n", hr);
279
280 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_MIMETYPE, AL_EFFECTIVE, &assocprog);
281 ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), "got 0x%x\n", hr);
282
283 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, httpW, AT_URLPROTOCOL, AL_EFFECTIVE, &assocprog);
284 todo_wine ok(hr == S_OK, "got 0x%x\n", hr);
285 trace("%s\n", wine_dbgstr_w(assocprog));
286
287 CoTaskMemFree(assocprog);
288 }
289
290 START_TEST(assoc)
291 {
292 IQueryAssociations *qa;
293 IApplicationAssociationRegistration *appreg = NULL;
294 HRESULT hr;
295
296 CoInitialize(NULL);
297
298 /* this works since XP */
299 hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&qa);
300 if (hr == S_OK)
301 {
302 test_IQueryAssociations_QueryInterface();
303 test_IQueryAssociations_GetString();
304 test_IQueryAssociations_Init();
305
306 IQueryAssociations_Release(qa);
307 }
308 else
309 win_skip("IQueryAssociations not supported, 0x%x\n", hr);
310
311 /* this works since Vista */
312 hr = CoCreateInstance(&CLSID_ApplicationAssociationRegistration, NULL, CLSCTX_INPROC_SERVER,
313 &IID_IApplicationAssociationRegistration, (LPVOID*)&appreg);
314 if (hr == S_OK)
315 {
316 test_IApplicationAssociationRegistration_QueryInterface(appreg);
317 test_IApplicationAssociationRegistration_QueryCurrentDefault(appreg);
318
319 IApplicationAssociationRegistration_Release(appreg);
320 }
321 else
322 win_skip("IApplicationAssociationRegistration not supported: 0x%x\n", hr);
323
324 CoUninitialize();
325 }