shlwapi/tests: Fix test failures on Windows 8.
[wine/multimedia.git] / dlls / shlwapi / tests / assoc.c
blobcbc40ceaa43360a6da795249683d452528043e81
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
20 #include <stdarg.h>
22 #include "wine/test.h"
23 #include "shlwapi.h"
24 #include "shlguid.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};
43 WCHAR buf[MAX_PATH];
44 HRESULT hr;
45 DWORD len;
47 if (!pAssocQueryStringW)
49 win_skip("AssocQueryStringW() is missing\n");
50 return;
53 len = 0xdeadbeef;
54 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
55 expect_hr(E_INVALIDARG, hr);
56 ok(len == 0xdeadbeef, "got %u\n", len);
58 len = 0xdeadbeef;
59 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
60 ok(hr == E_FAIL ||
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);
67 ok(hr == E_FAIL ||
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);
71 if (hr == S_OK)
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");
77 len = 0xdeadbeef;
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);
89 len = 0xdeadbeef;
90 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
91 expect_hr(E_INVALIDARG, hr);
92 ok(len == 0xdeadbeef, "got %u\n", len);
94 len = 0xdeadbeef;
95 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, open, NULL, &len);
96 ok(hr == E_FAIL ||
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);
101 len = 0xdeadbeef;
102 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotBad, open, NULL, &len);
103 ok(hr == E_FAIL ||
104 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
105 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
106 "Unexpected result : %08x\n", hr);
107 ok(len == 0xdeadbeef, "got %u\n", len);
109 len = 0xdeadbeef;
110 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL, &len);
111 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
112 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
113 hr == E_FAIL, /* Win9x/WinMe/NT4 */
114 "Unexpected result : %08x\n", hr);
115 ok(len == 0xdeadbeef, "got %u\n", len);
117 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL, NULL);
118 ok(hr == E_UNEXPECTED ||
119 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
120 "Unexpected result : %08x\n", hr);
123 static void test_getstring_basic(void)
125 HRESULT hr;
126 WCHAR * friendlyName;
127 WCHAR * executableName;
128 DWORD len, len2, slen;
130 if (!pAssocQueryStringW)
132 win_skip("AssocQueryStringW() is missing\n");
133 return;
136 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
137 expect_hr(S_FALSE, hr);
138 if (hr != S_FALSE)
140 skip("failed to get initial len\n");
141 return;
144 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
145 len * sizeof(WCHAR));
146 if (!executableName)
148 skip("failed to allocate memory\n");
149 return;
152 len2 = len;
153 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
154 executableName, &len2);
155 expect_hr(S_OK, hr);
156 slen = lstrlenW(executableName) + 1;
157 expect(len, len2);
158 expect(len, slen);
160 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
161 &len);
162 ok(hr == S_FALSE ||
163 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* Win9x/NT4 */ ||
164 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
165 "Unexpected result : %08x\n", hr);
166 if (hr != S_FALSE)
168 HeapFree(GetProcessHeap(), 0, executableName);
169 skip("failed to get initial len\n");
170 return;
173 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
174 len * sizeof(WCHAR));
175 if (!friendlyName)
177 HeapFree(GetProcessHeap(), 0, executableName);
178 skip("failed to allocate memory\n");
179 return;
182 len2 = len;
183 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
184 friendlyName, &len2);
185 expect_hr(S_OK, hr);
186 slen = lstrlenW(friendlyName) + 1;
187 expect(len, len2);
188 expect(len, slen);
190 HeapFree(GetProcessHeap(), 0, executableName);
191 HeapFree(GetProcessHeap(), 0, friendlyName);
194 static void test_getstring_no_extra(void)
196 LONG ret;
197 HKEY hkey;
198 HRESULT hr;
199 static const CHAR dotWinetest[] = {
200 '.','w','i','n','e','t','e','s','t',0
202 static const CHAR winetestfile[] = {
203 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
205 static const CHAR winetestfileAction[] = {
206 'w','i','n','e','t','e','s','t','f','i','l','e',
207 '\\','s','h','e','l','l',
208 '\\','f','o','o',
209 '\\','c','o','m','m','a','n','d',0
211 static const CHAR action[] = {
212 'n','o','t','e','p','a','d','.','e','x','e',0
214 CHAR buf[MAX_PATH];
215 DWORD len = MAX_PATH;
217 if (!pAssocQueryStringA)
219 win_skip("AssocQueryStringA() is missing\n");
220 return;
223 buf[0] = '\0';
224 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
225 if (ret != ERROR_SUCCESS) {
226 skip("failed to create dotWinetest key\n");
227 return;
230 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
231 RegCloseKey(hkey);
232 if (ret != ERROR_SUCCESS)
234 skip("failed to set dotWinetest key\n");
235 goto cleanup;
238 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
239 if (ret != ERROR_SUCCESS)
241 skip("failed to create winetestfileAction key\n");
242 goto cleanup;
245 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
246 RegCloseKey(hkey);
247 if (ret != ERROR_SUCCESS)
249 skip("failed to set winetestfileAction key\n");
250 goto cleanup;
253 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
254 ok(hr == S_OK ||
255 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
256 "Unexpected result : %08x\n", hr);
257 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
258 expect_hr(S_OK, hr);
259 ok(strstr(buf, action) != NULL,
260 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
262 cleanup:
263 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
264 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
268 static void test_assoc_create(void)
270 HRESULT hr;
271 IQueryAssociations *pqa;
273 if (!pAssocCreate)
275 win_skip("AssocCreate() is missing\n");
276 return;
279 hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
280 ok(hr == E_INVALIDARG, "Unexpected result : %08x\n", hr);
282 hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
283 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_NOINTERFACE
284 , "Unexpected result : %08x\n", hr);
286 hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
287 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_INVALIDARG
288 , "Unexpected result : %08x\n", hr);
290 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
291 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
292 , "Unexpected result : %08x\n", hr);
293 if(hr == S_OK)
295 IQueryAssociations_Release(pqa);
298 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
299 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
300 , "Unexpected result : %08x\n", hr);
301 if(hr == S_OK)
303 IQueryAssociations_Release(pqa);
307 START_TEST(assoc)
309 HMODULE hshlwapi;
310 hshlwapi = GetModuleHandleA("shlwapi.dll");
311 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
312 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
313 pAssocCreate = (void*)GetProcAddress(hshlwapi, "AssocCreate");
315 test_getstring_bad();
316 test_getstring_basic();
317 test_getstring_no_extra();
318 test_assoc_create();