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