1 /* Unit test suite for SHLWAPI IQueryAssociations functions
3 * Copyright 2008 Google (Lei Zhang)
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
22 #include "wine/test.h"
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)
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
;
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 };
40 static void test_getstring_bad(void)
42 static const WCHAR openwith
[] = {'O','p','e','n','W','i','t','h','.','e','x','e',0};
47 if (!pAssocQueryStringW
)
49 win_skip("AssocQueryStringW() is missing\n");
54 hr
= pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE
, NULL
, open
, NULL
, &len
);
55 expect_hr(E_INVALIDARG
, hr
);
56 ok(len
== 0xdeadbeef, "got %u\n", len
);
59 hr
= pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE
, badBad
, open
, NULL
, &len
);
61 hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
62 "Unexpected result : %08x\n", hr
);
63 ok(len
== 0xdeadbeef, "got %u\n", len
);
65 len
= sizeof(buf
)/sizeof(buf
[0]);
66 hr
= pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE
, dotBad
, open
, buf
, &len
);
68 hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
69 hr
== S_OK
/* Win8 */,
70 "Unexpected result : %08x\n", hr
);
73 ok(len
< sizeof(buf
)/sizeof(buf
[0]), "got %u\n", len
);
74 ok(!lstrcmpiW(buf
+ len
- sizeof(openwith
)/sizeof(openwith
[0]), openwith
), "wrong data\n");
78 hr
= pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE
, dotHtml
, invalid
, NULL
, &len
);
79 ok(hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
) ||
80 hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
81 "Unexpected result : %08x\n", hr
);
82 ok(len
== 0xdeadbeef, "got %u\n", len
);
84 hr
= pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE
, dotHtml
, open
, NULL
, NULL
);
85 ok(hr
== E_UNEXPECTED
||
86 hr
== E_INVALIDARG
, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
87 "Unexpected result : %08x\n", hr
);
90 hr
= pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME
, NULL
, open
, NULL
, &len
);
91 expect_hr(E_INVALIDARG
, hr
);
92 ok(len
== 0xdeadbeef, "got %u\n", len
);
95 hr
= pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME
, badBad
, open
, NULL
, &len
);
97 hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
98 "Unexpected result : %08x\n", hr
);
99 ok(len
== 0xdeadbeef, "got %u\n", len
);
102 hr
= pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME
, dotBad
, open
, NULL
, &len
);
104 hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
105 hr
== HRESULT_FROM_WIN32(ERROR_NOT_FOUND
) /* Win8 */ ||
106 hr
== S_FALSE
, /* Win10 */
107 "Unexpected result : %08x\n", hr
);
108 ok((hr
== S_FALSE
&& len
< sizeof(buf
)/sizeof(buf
[0])) || len
== 0xdeadbeef,
109 "got hr=%08x and len=%u\n", hr
, len
);
112 hr
= pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME
, dotHtml
, invalid
, NULL
, &len
);
113 ok(hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
) ||
114 hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
) || /* W2K/Vista/W2K8 */
115 hr
== E_FAIL
, /* Win9x/WinMe/NT4 */
116 "Unexpected result : %08x\n", hr
);
117 ok(len
== 0xdeadbeef, "got %u\n", len
);
119 hr
= pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME
, dotHtml
, open
, NULL
, NULL
);
120 ok(hr
== E_UNEXPECTED
||
121 hr
== E_INVALIDARG
, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
122 "Unexpected result : %08x\n", hr
);
125 static void test_getstring_basic(void)
128 WCHAR
* friendlyName
;
129 WCHAR
* executableName
;
130 DWORD len
, len2
, slen
;
132 if (!pAssocQueryStringW
)
134 win_skip("AssocQueryStringW() is missing\n");
138 hr
= pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE
, dotHtml
, open
, NULL
, &len
);
139 expect_hr(S_FALSE
, hr
);
142 skip("failed to get initial len\n");
146 executableName
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
147 len
* sizeof(WCHAR
));
150 skip("failed to allocate memory\n");
155 hr
= pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE
, dotHtml
, open
,
156 executableName
, &len2
);
158 slen
= lstrlenW(executableName
) + 1;
162 hr
= pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME
, dotHtml
, open
, NULL
,
165 hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
) /* Win9x/NT4 */ ||
166 hr
== HRESULT_FROM_WIN32(ERROR_NOT_FOUND
), /* Win8 */
167 "Unexpected result : %08x\n", hr
);
170 HeapFree(GetProcessHeap(), 0, executableName
);
171 skip("failed to get initial len\n");
175 friendlyName
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
176 len
* sizeof(WCHAR
));
179 HeapFree(GetProcessHeap(), 0, executableName
);
180 skip("failed to allocate memory\n");
185 hr
= pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME
, dotHtml
, open
,
186 friendlyName
, &len2
);
188 slen
= lstrlenW(friendlyName
) + 1;
192 HeapFree(GetProcessHeap(), 0, executableName
);
193 HeapFree(GetProcessHeap(), 0, friendlyName
);
196 static void test_getstring_no_extra(void)
201 static const CHAR dotWinetest
[] = {
202 '.','w','i','n','e','t','e','s','t',0
204 static const CHAR winetestfile
[] = {
205 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
207 static const CHAR winetestfileAction
[] = {
208 'w','i','n','e','t','e','s','t','f','i','l','e',
209 '\\','s','h','e','l','l',
211 '\\','c','o','m','m','a','n','d',0
213 static const CHAR action
[] = {
214 'n','o','t','e','p','a','d','.','e','x','e',0
217 DWORD len
= MAX_PATH
;
219 if (!pAssocQueryStringA
)
221 win_skip("AssocQueryStringA() is missing\n");
226 ret
= RegCreateKeyA(HKEY_CLASSES_ROOT
, dotWinetest
, &hkey
);
227 if (ret
!= ERROR_SUCCESS
) {
228 skip("failed to create dotWinetest key\n");
232 ret
= RegSetValueA(hkey
, NULL
, REG_SZ
, winetestfile
, lstrlenA(winetestfile
));
234 if (ret
!= ERROR_SUCCESS
)
236 skip("failed to set dotWinetest key\n");
240 ret
= RegCreateKeyA(HKEY_CLASSES_ROOT
, winetestfileAction
, &hkey
);
241 if (ret
!= ERROR_SUCCESS
)
243 skip("failed to create winetestfileAction key\n");
247 ret
= RegSetValueA(hkey
, NULL
, REG_SZ
, action
, lstrlenA(action
));
249 if (ret
!= ERROR_SUCCESS
)
251 skip("failed to set winetestfileAction key\n");
255 hr
= pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE
, dotWinetest
, NULL
, buf
, &len
);
257 hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
), /* XP and W2K3 */
258 "Unexpected result : %08x\n", hr
);
259 hr
= pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE
, dotWinetest
, "foo", buf
, &len
);
261 ok(strstr(buf
, action
) != NULL
,
262 "got '%s' (Expected result to include 'notepad.exe')\n", buf
);
265 SHDeleteKeyA(HKEY_CLASSES_ROOT
, dotWinetest
);
266 SHDeleteKeyA(HKEY_CLASSES_ROOT
, winetestfile
);
270 static void test_assoc_create(void)
273 IQueryAssociations
*pqa
;
277 win_skip("AssocCreate() is missing\n");
281 hr
= pAssocCreate(IID_NULL
, &IID_NULL
, NULL
);
282 ok(hr
== E_INVALIDARG
, "Unexpected result : %08x\n", hr
);
284 hr
= pAssocCreate(CLSID_QueryAssociations
, &IID_NULL
, (LPVOID
*)&pqa
);
285 ok(hr
== CLASS_E_CLASSNOTAVAILABLE
|| hr
== E_NOTIMPL
|| hr
== E_NOINTERFACE
286 , "Unexpected result : %08x\n", hr
);
288 hr
= pAssocCreate(IID_NULL
, &IID_IQueryAssociations
, (LPVOID
*)&pqa
);
289 ok(hr
== CLASS_E_CLASSNOTAVAILABLE
|| hr
== E_NOTIMPL
|| hr
== E_INVALIDARG
290 , "Unexpected result : %08x\n", hr
);
292 hr
= pAssocCreate(CLSID_QueryAssociations
, &IID_IQueryAssociations
, (LPVOID
*)&pqa
);
293 ok(hr
== S_OK
|| hr
== E_NOTIMPL
/* win98 */
294 , "Unexpected result : %08x\n", hr
);
297 IQueryAssociations_Release(pqa
);
300 hr
= pAssocCreate(CLSID_QueryAssociations
, &IID_IUnknown
, (LPVOID
*)&pqa
);
301 ok(hr
== S_OK
|| hr
== E_NOTIMPL
/* win98 */
302 , "Unexpected result : %08x\n", hr
);
305 IQueryAssociations_Release(pqa
);
312 hshlwapi
= GetModuleHandleA("shlwapi.dll");
313 pAssocQueryStringA
= (void*)GetProcAddress(hshlwapi
, "AssocQueryStringA");
314 pAssocQueryStringW
= (void*)GetProcAddress(hshlwapi
, "AssocQueryStringW");
315 pAssocCreate
= (void*)GetProcAddress(hshlwapi
, "AssocCreate");
317 test_getstring_bad();
318 test_getstring_basic();
319 test_getstring_no_extra();