include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / shell32 / tests / assoc.c
blob0e28d9f0355b17608bb8b2d739eac178ec531ba3
1 /* Unit test suite for various shell Association objects
3 * Copyright 2012 Detlef Riekenberg
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.
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.
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
20 #define COBJMACROS
22 #include <stdarg.h>
24 #include "shlwapi.h"
25 #include "shlguid.h"
26 #include "shobjidl.h"
28 #include "wine/test.h"
31 static void test_IQueryAssociations_QueryInterface(void)
33 IQueryAssociations *qa;
34 IQueryAssociations *qa2;
35 IUnknown *unk;
36 HRESULT hr;
38 hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&qa);
39 ok(hr == S_OK, "got 0x%08lx\n", hr);
41 hr = IQueryAssociations_QueryInterface(qa, &IID_IQueryAssociations, (void**)&qa2);
42 ok(hr == S_OK, "QueryInterface (IQueryAssociations) returned 0x%lx\n", hr);
43 if (SUCCEEDED(hr)) {
44 IQueryAssociations_Release(qa2);
47 hr = IQueryAssociations_QueryInterface(qa, &IID_IUnknown, (void**)&unk);
48 ok(hr == S_OK, "QueryInterface (IUnknown) returned 0x%lx\n", hr);
49 if (SUCCEEDED(hr)) {
50 IUnknown_Release(unk);
53 hr = IQueryAssociations_QueryInterface(qa, &IID_IUnknown, NULL);
54 ok(hr == E_POINTER, "got 0x%lx (expected E_POINTER)\n", hr);
56 IQueryAssociations_Release(qa);
60 static void test_IApplicationAssociationRegistration_QueryInterface(IApplicationAssociationRegistration *appreg)
62 IApplicationAssociationRegistration *appreg2;
63 IUnknown *unk;
64 HRESULT hr;
66 hr = IApplicationAssociationRegistration_QueryInterface(appreg, &IID_IApplicationAssociationRegistration,
67 (void**)&appreg2);
68 ok(hr == S_OK, "QueryInterface (IApplicationAssociationRegistration) returned 0x%lx\n", hr);
69 if (SUCCEEDED(hr)) {
70 IApplicationAssociationRegistration_Release(appreg2);
73 hr = IApplicationAssociationRegistration_QueryInterface(appreg, &IID_IUnknown, (void**)&unk);
74 ok(hr == S_OK, "QueryInterface (IUnknown) returned 0x%lx\n", hr);
75 if (SUCCEEDED(hr)) {
76 IUnknown_Release(unk);
79 hr = IApplicationAssociationRegistration_QueryInterface(appreg, &IID_IUnknown, NULL);
80 ok(hr == E_POINTER, "got 0x%lx (expected E_POINTER)\n", hr);
83 struct assoc_getstring_test
85 const WCHAR *key;
86 ASSOCF flags;
87 ASSOCSTR str;
88 DWORD len;
89 HRESULT hr;
90 HRESULT brokenhr;
93 static const WCHAR httpW[] = {'h','t','t','p',0};
94 static const WCHAR badW[] = {'b','a','d','b','a','d',0};
96 static struct assoc_getstring_test getstring_tests[] =
98 { httpW, 0, ASSOCSTR_EXECUTABLE, 2, 0x8007007a /* E_NOT_SUFFICIENT_BUFFER */, S_OK },
99 { httpW, ASSOCF_NOTRUNCATE, ASSOCSTR_EXECUTABLE, 2, E_POINTER },
100 { NULL }
103 static void getstring_test(LPCWSTR assocName, HKEY progIdKey, ASSOCSTR str, LPCWSTR expected_string, int line)
105 IQueryAssociations *assoc;
106 WCHAR *buffer;
107 HRESULT hr;
108 DWORD len;
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%lx\n", hr);
112 hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, assocName, progIdKey, NULL);
113 ok_(__FILE__, line)(hr == S_OK, "IQueryAssociations::Init failed, 0x%lx\n", hr);
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%lx, 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;
124 buffer = malloc(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%lx, expected S_OK\n", hr);
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 free(buffer);
132 } else {
133 ok_(__FILE__, line)(FAILED(hr), "GetString returned 0x%lx, expected failure\n", hr);
136 IQueryAssociations_Release(assoc);
139 static void test_IQueryAssociations_GetString(void)
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;
151 struct assoc_getstring_test *ptr = getstring_tests;
152 IQueryAssociations *assoc;
153 HRESULT hr;
154 DWORD len;
155 int i = 0;
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)
160 win_skip("Not enough permissions to create a test key.\n");
161 return;
164 ok(r == ERROR_SUCCESS, "RegCreateKeyExW(HKCR, \".test\") failed: 0x%Ix\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%Ix\n", r);
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%Ix\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__);
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%Ix\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%Ix\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__);
184 RegDeleteKeyW(test_progid_key, DefaultIconW);
185 RegDeleteKeyW(HKEY_CLASSES_ROOT, test_progidW);
186 RegDeleteKeyW(HKEY_CLASSES_ROOT, test_extensionW);
188 hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&assoc);
189 ok(hr == S_OK, "failed to create object, 0x%lx\n", hr);
191 hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, httpW, NULL, NULL);
192 ok(hr == S_OK, "Init failed, 0x%lx\n", hr);
194 len = 0;
195 hr = IQueryAssociations_GetString(assoc, ASSOCF_NONE, ASSOCSTR_EXECUTABLE, NULL, NULL, &len);
196 ok(hr == S_FALSE, "got 0x%08lx\n", hr);
197 ok(len > 0, "got wrong needed length, %ld\n", len);
199 while (ptr->key)
201 WCHAR buffW[MAX_PATH];
202 DWORD len;
204 hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, ptr->key, NULL, NULL);
205 ok(hr == S_OK, "%d: Init failed, 0x%lx\n", i, hr);
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%08lx\n", i, hr);
212 else
214 ok(hr == ptr->hr, "%d: GetString failed, 0x%08lx\n", i, hr);
215 ok(len > ptr->len, "%d: got needed length %ld\n", i, len);
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]);
222 if (!(ptr->flags & ASSOCF_NOTRUNCATE) && ptr->len && FAILED(ptr->hr))
223 ok(buffW[0] != 0, "%d: got %x\n", i, buffW[0]);
225 i++;
226 ptr++;
229 IQueryAssociations_Release(assoc);
232 static void test_IQueryAssociations_Init(void)
234 IQueryAssociations *assoc;
235 HRESULT hr;
236 DWORD len;
238 hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&assoc);
239 ok(hr == S_OK, "failed to create object, 0x%lx\n", hr);
241 hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, NULL, NULL, NULL);
242 ok(hr == E_INVALIDARG, "Init failed, 0x%08lx\n", hr);
244 hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, httpW, NULL, NULL);
245 ok(hr == S_OK, "Init failed, 0x%08lx\n", hr);
247 hr = IQueryAssociations_Init(assoc, ASSOCF_NONE, badW, NULL, NULL);
248 ok(hr == S_OK || broken(hr == S_FALSE) /* pre-vista */, "Init failed, 0x%08lx\n", hr);
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%08lx\n", hr);
254 IQueryAssociations_Release(assoc);
257 static void test_IApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration *appreg)
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;
265 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_URLPROTOCOL, AL_EFFECTIVE, &assocprog);
266 ok(hr == E_INVALIDARG, "got 0x%lx\n", hr);
268 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_FILEEXTENSION, AL_EFFECTIVE, &assocprog);
269 ok(hr == E_INVALIDARG, "got 0x%lx\n", hr);
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%lx\n", hr);
274 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, httpW, AT_URLPROTOCOL, AL_EFFECTIVE, NULL);
275 ok(hr == E_INVALIDARG, "got 0x%lx\n", hr);
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%lx\n", hr);
280 trace("%s\n", wine_dbgstr_w(assocprog));
281 CoTaskMemFree(assocprog);
283 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_STARTMENUCLIENT, AL_EFFECTIVE, &assocprog);
284 ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), "got 0x%lx\n", hr);
286 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, emptyW, AT_MIMETYPE, AL_EFFECTIVE, &assocprog);
287 ok(hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), "got 0x%lx\n", hr);
289 hr = IApplicationAssociationRegistration_QueryCurrentDefault(appreg, httpW, AT_URLPROTOCOL, AL_EFFECTIVE, &assocprog);
290 ok(hr == S_OK, "got 0x%lx\n", hr);
291 trace("%s\n", wine_dbgstr_w(assocprog));
293 CoTaskMemFree(assocprog);
296 START_TEST(assoc)
298 IQueryAssociations *qa;
299 IApplicationAssociationRegistration *appreg = NULL;
300 HRESULT hr;
302 CoInitialize(NULL);
304 /* this works since XP */
305 hr = CoCreateInstance(&CLSID_QueryAssociations, NULL, CLSCTX_INPROC_SERVER, &IID_IQueryAssociations, (void*)&qa);
306 if (hr == S_OK)
308 test_IQueryAssociations_QueryInterface();
309 test_IQueryAssociations_GetString();
310 test_IQueryAssociations_Init();
312 IQueryAssociations_Release(qa);
314 else
315 win_skip("IQueryAssociations not supported, 0x%lx\n", hr);
317 /* this works since Vista */
318 hr = CoCreateInstance(&CLSID_ApplicationAssociationRegistration, NULL, CLSCTX_INPROC_SERVER,
319 &IID_IApplicationAssociationRegistration, (LPVOID*)&appreg);
320 if (hr == S_OK)
322 test_IApplicationAssociationRegistration_QueryInterface(appreg);
323 test_IApplicationAssociationRegistration_QueryCurrentDefault(appreg);
325 IApplicationAssociationRegistration_Release(appreg);
327 else
328 win_skip("IApplicationAssociationRegistration not supported: 0x%lx\n", hr);
330 CoUninitialize();