[SHLWAPI_WINETEST] Sync with Wine 3.0. CORE-14225
[reactos.git] / modules / rostests / winetests / shlwapi / assoc.c
1 /* Unit test suite for SHLWAPI IQueryAssociations functions
2 *
3 * Copyright 2008 Google (Lei Zhang)
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 #define expect(expected, got) ok ( expected == got, "Expected %d, got %d\n", expected, got)
23 #define expect_hr(expected, got) ok ( expected == got, "Expected %08x, got %08x\n", expected, got)
24
25 static HRESULT (WINAPI *pAssocQueryStringA)(ASSOCF,ASSOCSTR,LPCSTR,LPCSTR,LPSTR,LPDWORD) = NULL;
26 static HRESULT (WINAPI *pAssocQueryStringW)(ASSOCF,ASSOCSTR,LPCWSTR,LPCWSTR,LPWSTR,LPDWORD) = NULL;
27 static HRESULT (WINAPI *pAssocCreate)(CLSID, REFIID, void **) = NULL;
28
29 /* Every version of Windows with IE should have this association? */
30 static const WCHAR dotHtml[] = { '.','h','t','m','l',0 };
31 static const WCHAR badBad[] = { 'b','a','d','b','a','d',0 };
32 static const WCHAR dotBad[] = { '.','b','a','d',0 };
33 static const WCHAR open[] = { 'o','p','e','n',0 };
34 static const WCHAR invalid[] = { 'i','n','v','a','l','i','d',0 };
35
36 static void test_getstring_bad(void)
37 {
38 static const WCHAR openwith[] = {'O','p','e','n','W','i','t','h','.','e','x','e',0};
39 WCHAR buf[MAX_PATH];
40 HRESULT hr;
41 DWORD len;
42
43 if (!pAssocQueryStringW)
44 {
45 win_skip("AssocQueryStringW() is missing\n");
46 return;
47 }
48
49 len = 0xdeadbeef;
50 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
51 expect_hr(E_INVALIDARG, hr);
52 ok(len == 0xdeadbeef, "got %u\n", len);
53
54 len = 0xdeadbeef;
55 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
56 ok(hr == E_FAIL ||
57 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
58 "Unexpected result : %08x\n", hr);
59 ok(len == 0xdeadbeef, "got %u\n", len);
60
61 len = sizeof(buf)/sizeof(buf[0]);
62 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotBad, open, buf, &len);
63 ok(hr == E_FAIL ||
64 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
65 hr == S_OK /* Win8 */,
66 "Unexpected result : %08x\n", hr);
67 if (hr == S_OK)
68 {
69 ok(len < sizeof(buf)/sizeof(buf[0]), "got %u\n", len);
70 ok(!lstrcmpiW(buf + len - sizeof(openwith)/sizeof(openwith[0]), openwith), "wrong data\n");
71 }
72
73 len = 0xdeadbeef;
74 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, invalid, NULL, &len);
75 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
76 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
77 "Unexpected result : %08x\n", hr);
78 ok(len == 0xdeadbeef, "got %u\n", len);
79
80 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, NULL);
81 ok(hr == E_UNEXPECTED ||
82 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
83 "Unexpected result : %08x\n", hr);
84
85 len = 0xdeadbeef;
86 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
87 expect_hr(E_INVALIDARG, hr);
88 ok(len == 0xdeadbeef, "got %u\n", len);
89
90 len = 0xdeadbeef;
91 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, open, NULL, &len);
92 ok(hr == E_FAIL ||
93 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
94 "Unexpected result : %08x\n", hr);
95 ok(len == 0xdeadbeef, "got %u\n", len);
96
97 len = 0xdeadbeef;
98 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotBad, open, NULL, &len);
99 ok(hr == E_FAIL ||
100 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
101 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND) /* Win8 */ ||
102 hr == S_FALSE, /* Win10 */
103 "Unexpected result : %08x\n", hr);
104 ok((hr == S_FALSE && len < sizeof(buf)/sizeof(buf[0])) || len == 0xdeadbeef,
105 "got hr=%08x and len=%u\n", hr, len);
106
107 len = 0xdeadbeef;
108 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL, &len);
109 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
110 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
111 hr == E_FAIL, /* Win9x/WinMe/NT4 */
112 "Unexpected result : %08x\n", hr);
113 ok(len == 0xdeadbeef, "got %u\n", len);
114
115 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL, NULL);
116 ok(hr == E_UNEXPECTED ||
117 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
118 "Unexpected result : %08x\n", hr);
119 }
120
121 static void test_getstring_basic(void)
122 {
123 HRESULT hr;
124 WCHAR * friendlyName;
125 WCHAR * executableName;
126 DWORD len, len2, slen;
127
128 if (!pAssocQueryStringW)
129 {
130 win_skip("AssocQueryStringW() is missing\n");
131 return;
132 }
133
134 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
135 expect_hr(S_FALSE, hr);
136 if (hr != S_FALSE)
137 {
138 skip("failed to get initial len\n");
139 return;
140 }
141
142 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
143 len * sizeof(WCHAR));
144 if (!executableName)
145 {
146 skip("failed to allocate memory\n");
147 return;
148 }
149
150 len2 = len;
151 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
152 executableName, &len2);
153 expect_hr(S_OK, hr);
154 slen = lstrlenW(executableName) + 1;
155 expect(len, len2);
156 expect(len, slen);
157
158 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
159 &len);
160 ok(hr == S_FALSE ||
161 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* Win9x/NT4 */ ||
162 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
163 "Unexpected result : %08x\n", hr);
164 if (hr != S_FALSE)
165 {
166 HeapFree(GetProcessHeap(), 0, executableName);
167 skip("failed to get initial len\n");
168 return;
169 }
170
171 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
172 len * sizeof(WCHAR));
173 if (!friendlyName)
174 {
175 HeapFree(GetProcessHeap(), 0, executableName);
176 skip("failed to allocate memory\n");
177 return;
178 }
179
180 len2 = len;
181 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
182 friendlyName, &len2);
183 expect_hr(S_OK, hr);
184 slen = lstrlenW(friendlyName) + 1;
185 expect(len, len2);
186 expect(len, slen);
187
188 HeapFree(GetProcessHeap(), 0, executableName);
189 HeapFree(GetProcessHeap(), 0, friendlyName);
190 }
191
192 static void test_getstring_no_extra(void)
193 {
194 LONG ret;
195 HKEY hkey;
196 HRESULT hr;
197 static const CHAR dotWinetest[] = {
198 '.','w','i','n','e','t','e','s','t',0
199 };
200 static const CHAR winetestfile[] = {
201 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
202 };
203 static const CHAR winetestfileAction[] = {
204 'w','i','n','e','t','e','s','t','f','i','l','e',
205 '\\','s','h','e','l','l',
206 '\\','f','o','o',
207 '\\','c','o','m','m','a','n','d',0
208 };
209 static const CHAR action[] = {
210 'n','o','t','e','p','a','d','.','e','x','e',0
211 };
212 CHAR buf[MAX_PATH];
213 DWORD len = MAX_PATH;
214
215 if (!pAssocQueryStringA)
216 {
217 win_skip("AssocQueryStringA() is missing\n");
218 return;
219 }
220
221 buf[0] = '\0';
222 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
223 if (ret != ERROR_SUCCESS) {
224 skip("failed to create dotWinetest key\n");
225 return;
226 }
227
228 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
229 RegCloseKey(hkey);
230 if (ret != ERROR_SUCCESS)
231 {
232 skip("failed to set dotWinetest key\n");
233 goto cleanup;
234 }
235
236 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
237 if (ret != ERROR_SUCCESS)
238 {
239 skip("failed to create winetestfileAction key\n");
240 goto cleanup;
241 }
242
243 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
244 RegCloseKey(hkey);
245 if (ret != ERROR_SUCCESS)
246 {
247 skip("failed to set winetestfileAction key\n");
248 goto cleanup;
249 }
250
251 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
252 ok(hr == S_OK ||
253 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
254 "Unexpected result : %08x\n", hr);
255 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
256 expect_hr(S_OK, hr);
257 ok(strstr(buf, action) != NULL,
258 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
259
260 cleanup:
261 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
262 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
263
264 }
265
266 static void test_assoc_create(void)
267 {
268 HRESULT hr;
269 IQueryAssociations *pqa;
270
271 if (!pAssocCreate)
272 {
273 win_skip("AssocCreate() is missing\n");
274 return;
275 }
276
277 hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
278 ok(hr == E_INVALIDARG, "Unexpected result : %08x\n", hr);
279
280 hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
281 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_NOINTERFACE
282 , "Unexpected result : %08x\n", hr);
283
284 hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
285 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_INVALIDARG
286 , "Unexpected result : %08x\n", hr);
287
288 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
289 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
290 , "Unexpected result : %08x\n", hr);
291 if(hr == S_OK)
292 {
293 IQueryAssociations_Release(pqa);
294 }
295
296 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
297 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
298 , "Unexpected result : %08x\n", hr);
299 if(hr == S_OK)
300 {
301 IQueryAssociations_Release(pqa);
302 }
303 }
304
305 START_TEST(assoc)
306 {
307 HMODULE hshlwapi;
308 hshlwapi = GetModuleHandleA("shlwapi.dll");
309 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
310 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
311 pAssocCreate = (void*)GetProcAddress(hshlwapi, "AssocCreate");
312
313 test_getstring_bad();
314 test_getstring_basic();
315 test_getstring_no_extra();
316 test_assoc_create();
317 }