shlwapi/tests: Enable compilation with long types.
[wine.git] / dlls / shlwapi / tests / assoc.c
blob84eda1317bb16a6cb5196717b917c55d6d1703d7
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 %ld, got %ld\n", expected, got)
27 #define expect_hr(expected, got) ok ( expected == got, "Expected %08lx, got %08lx\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 %lu\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 : %08lx\n", hr);
63 ok(len == 0xdeadbeef, "got %lu\n", len);
65 len = ARRAY_SIZE(buf);
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 : %08lx\n", hr);
71 if (hr == S_OK)
73 ok(len < ARRAY_SIZE(buf), "got %lu\n", len);
74 ok(!lstrcmpiW(buf + len - ARRAY_SIZE(openwith), 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 : %08lx\n", hr);
82 ok(len == 0xdeadbeef, "got %lu\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 : %08lx\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 %lu\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 : %08lx\n", hr);
99 ok(len == 0xdeadbeef, "got %lu\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 hr == S_FALSE, /* Win10 */
107 "Unexpected result : %08lx\n", hr);
108 ok((hr == S_FALSE && len < ARRAY_SIZE(buf)) || len == 0xdeadbeef,
109 "got hr=%08lx and len=%lu\n", hr, len);
111 len = 0xdeadbeef;
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 : %08lx\n", hr);
117 ok(len == 0xdeadbeef, "got %lu\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 : %08lx\n", hr);
125 static void test_getstring_basic(void)
127 HRESULT hr;
128 WCHAR * friendlyName;
129 WCHAR * executableName;
130 DWORD len, len2, slen;
132 if (!pAssocQueryStringW)
134 win_skip("AssocQueryStringW() is missing\n");
135 return;
138 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
139 expect_hr(S_FALSE, hr);
140 if (hr != S_FALSE)
142 skip("failed to get initial len\n");
143 return;
146 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
147 len * sizeof(WCHAR));
148 if (!executableName)
150 skip("failed to allocate memory\n");
151 return;
154 len2 = len;
155 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
156 executableName, &len2);
157 expect_hr(S_OK, hr);
158 slen = lstrlenW(executableName) + 1;
159 expect(len, len2);
160 expect(len, slen);
162 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
163 &len);
164 ok(hr == S_FALSE ||
165 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* Win9x/NT4 */ ||
166 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
167 "Unexpected result : %08lx\n", hr);
168 if (hr != S_FALSE)
170 HeapFree(GetProcessHeap(), 0, executableName);
171 skip("failed to get initial len\n");
172 return;
175 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
176 len * sizeof(WCHAR));
177 if (!friendlyName)
179 HeapFree(GetProcessHeap(), 0, executableName);
180 skip("failed to allocate memory\n");
181 return;
184 len2 = len;
185 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
186 friendlyName, &len2);
187 expect_hr(S_OK, hr);
188 slen = lstrlenW(friendlyName) + 1;
189 expect(len, len2);
190 expect(len, slen);
192 HeapFree(GetProcessHeap(), 0, executableName);
193 HeapFree(GetProcessHeap(), 0, friendlyName);
196 static void test_getstring_no_extra(void)
198 LONG ret;
199 HKEY hkey;
200 HRESULT hr;
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',
210 '\\','f','o','o',
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
216 CHAR buf[MAX_PATH];
217 DWORD len = MAX_PATH;
219 if (!pAssocQueryStringA)
221 win_skip("AssocQueryStringA() is missing\n");
222 return;
225 buf[0] = '\0';
226 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
227 if (ret != ERROR_SUCCESS) {
228 skip("failed to create dotWinetest key\n");
229 return;
232 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
233 RegCloseKey(hkey);
234 if (ret != ERROR_SUCCESS)
236 skip("failed to set dotWinetest key\n");
237 goto cleanup;
240 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
241 if (ret != ERROR_SUCCESS)
243 skip("failed to create winetestfileAction key\n");
244 goto cleanup;
247 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
248 RegCloseKey(hkey);
249 if (ret != ERROR_SUCCESS)
251 skip("failed to set winetestfileAction key\n");
252 goto cleanup;
255 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
256 ok(hr == S_OK ||
257 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
258 "Unexpected result : %08lx\n", hr);
259 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
260 expect_hr(S_OK, hr);
261 ok(strstr(buf, action) != NULL,
262 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
264 cleanup:
265 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
266 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
270 static void test_assoc_create(void)
272 HRESULT hr;
273 IQueryAssociations *pqa;
275 if (!pAssocCreate)
277 win_skip("AssocCreate() is missing\n");
278 return;
281 hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
282 ok(hr == E_INVALIDARG, "Unexpected result : %08lx\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 : %08lx\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 : %08lx\n", hr);
292 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
293 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
294 , "Unexpected result : %08lx\n", hr);
295 if(hr == S_OK)
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 : %08lx\n", hr);
303 if(hr == S_OK)
305 IQueryAssociations_Release(pqa);
309 START_TEST(assoc)
311 HMODULE hshlwapi;
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();
320 test_assoc_create();