Release 1.6-rc2.
[wine/testsucceed.git] / dlls / shlwapi / tests / assoc.c
blob389a9f5ec1161c02f42361155f4d731e12136b39
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 HRESULT hr;
43 DWORD len;
45 if (!pAssocQueryStringW)
47 win_skip("AssocQueryStringW() is missing\n");
48 return;
51 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
52 expect_hr(E_INVALIDARG, hr);
53 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
54 ok(hr == E_FAIL ||
55 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
56 "Unexpected result : %08x\n", hr);
57 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotBad, open, NULL, &len);
58 ok(hr == E_FAIL ||
59 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
60 "Unexpected result : %08x\n", hr);
61 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, invalid, NULL,
62 &len);
63 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
64 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
65 "Unexpected result : %08x\n", hr);
66 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, NULL);
67 ok(hr == E_UNEXPECTED ||
68 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
69 "Unexpected result : %08x\n", hr);
71 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
72 expect_hr(E_INVALIDARG, hr);
73 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, open, NULL,
74 &len);
75 ok(hr == E_FAIL ||
76 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
77 "Unexpected result : %08x\n", hr);
78 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotBad, open, NULL,
79 &len);
80 ok(hr == E_FAIL ||
81 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
82 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
83 "Unexpected result : %08x\n", hr);
84 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
85 &len);
86 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
87 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
88 hr == E_FAIL, /* Win9x/WinMe/NT4 */
89 "Unexpected result : %08x\n", hr);
90 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
91 NULL);
92 ok(hr == E_UNEXPECTED ||
93 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
94 "Unexpected result : %08x\n", hr);
97 static void test_getstring_basic(void)
99 HRESULT hr;
100 WCHAR * friendlyName;
101 WCHAR * executableName;
102 DWORD len, len2, slen;
104 if (!pAssocQueryStringW)
106 win_skip("AssocQueryStringW() is missing\n");
107 return;
110 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
111 expect_hr(S_FALSE, hr);
112 if (hr != S_FALSE)
114 skip("failed to get initial len\n");
115 return;
118 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
119 len * sizeof(WCHAR));
120 if (!executableName)
122 skip("failed to allocate memory\n");
123 return;
126 len2 = len;
127 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
128 executableName, &len2);
129 expect_hr(S_OK, hr);
130 slen = lstrlenW(executableName) + 1;
131 expect(len, len2);
132 expect(len, slen);
134 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
135 &len);
136 ok(hr == S_FALSE ||
137 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* Win9x/NT4 */ ||
138 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
139 "Unexpected result : %08x\n", hr);
140 if (hr != S_FALSE)
142 HeapFree(GetProcessHeap(), 0, executableName);
143 skip("failed to get initial len\n");
144 return;
147 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
148 len * sizeof(WCHAR));
149 if (!friendlyName)
151 HeapFree(GetProcessHeap(), 0, executableName);
152 skip("failed to allocate memory\n");
153 return;
156 len2 = len;
157 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
158 friendlyName, &len2);
159 expect_hr(S_OK, hr);
160 slen = lstrlenW(friendlyName) + 1;
161 expect(len, len2);
162 expect(len, slen);
164 HeapFree(GetProcessHeap(), 0, executableName);
165 HeapFree(GetProcessHeap(), 0, friendlyName);
168 static void test_getstring_no_extra(void)
170 LONG ret;
171 HKEY hkey;
172 HRESULT hr;
173 static const CHAR dotWinetest[] = {
174 '.','w','i','n','e','t','e','s','t',0
176 static const CHAR winetestfile[] = {
177 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
179 static const CHAR winetestfileAction[] = {
180 'w','i','n','e','t','e','s','t','f','i','l','e',
181 '\\','s','h','e','l','l',
182 '\\','f','o','o',
183 '\\','c','o','m','m','a','n','d',0
185 static const CHAR action[] = {
186 'n','o','t','e','p','a','d','.','e','x','e',0
188 CHAR buf[MAX_PATH];
189 DWORD len = MAX_PATH;
191 if (!pAssocQueryStringA)
193 win_skip("AssocQueryStringA() is missing\n");
194 return;
197 buf[0] = '\0';
198 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
199 if (ret != ERROR_SUCCESS) {
200 skip("failed to create dotWinetest key\n");
201 return;
204 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
205 RegCloseKey(hkey);
206 if (ret != ERROR_SUCCESS)
208 skip("failed to set dotWinetest key\n");
209 goto cleanup;
212 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
213 if (ret != ERROR_SUCCESS)
215 skip("failed to create winetestfileAction key\n");
216 goto cleanup;
219 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
220 RegCloseKey(hkey);
221 if (ret != ERROR_SUCCESS)
223 skip("failed to set winetestfileAction key\n");
224 goto cleanup;
227 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
228 ok(hr == S_OK ||
229 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
230 "Unexpected result : %08x\n", hr);
231 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
232 expect_hr(S_OK, hr);
233 ok(strstr(buf, action) != NULL,
234 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
236 cleanup:
237 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
238 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
242 static void test_assoc_create(void)
244 HRESULT hr;
245 IQueryAssociations *pqa;
247 if (!pAssocCreate)
249 win_skip("AssocCreate() is missing\n");
250 return;
253 hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
254 ok(hr == E_INVALIDARG, "Unexpected result : %08x\n", hr);
256 hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
257 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_NOINTERFACE
258 , "Unexpected result : %08x\n", hr);
260 hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
261 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_INVALIDARG
262 , "Unexpected result : %08x\n", hr);
264 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
265 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
266 , "Unexpected result : %08x\n", hr);
267 if(hr == S_OK)
269 IQueryAssociations_Release(pqa);
272 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
273 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
274 , "Unexpected result : %08x\n", hr);
275 if(hr == S_OK)
277 IQueryAssociations_Release(pqa);
281 START_TEST(assoc)
283 HMODULE hshlwapi;
284 hshlwapi = GetModuleHandleA("shlwapi.dll");
285 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
286 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
287 pAssocCreate = (void*)GetProcAddress(hshlwapi, "AssocCreate");
289 test_getstring_bad();
290 test_getstring_basic();
291 test_getstring_no_extra();
292 test_assoc_create();