Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / shlwapi / tests / assoc.c
bloba6f5e2d5e61d31e2ea79e291910a4e1b61821afc
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 "Unexpected result : %08x\n", hr);
83 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
84 &len);
85 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
86 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
87 hr == E_FAIL, /* Win9x/WinMe/NT4 */
88 "Unexpected result : %08x\n", hr);
89 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
90 NULL);
91 ok(hr == E_UNEXPECTED ||
92 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
93 "Unexpected result : %08x\n", hr);
96 static void test_getstring_basic(void)
98 HRESULT hr;
99 WCHAR * friendlyName;
100 WCHAR * executableName;
101 DWORD len, len2, slen;
103 if (!pAssocQueryStringW)
105 win_skip("AssocQueryStringW() is missing\n");
106 return;
109 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
110 expect_hr(S_FALSE, hr);
111 if (hr != S_FALSE)
113 skip("failed to get initial len\n");
114 return;
117 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
118 len * sizeof(WCHAR));
119 if (!executableName)
121 skip("failed to allocate memory\n");
122 return;
125 len2 = len;
126 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
127 executableName, &len2);
128 expect_hr(S_OK, hr);
129 slen = lstrlenW(executableName) + 1;
130 expect(len, len2);
131 expect(len, slen);
133 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
134 &len);
135 ok(hr == S_FALSE ||
136 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* Win9x/NT4 */
137 "Unexpected result : %08x\n", hr);
138 if (hr != S_FALSE)
140 HeapFree(GetProcessHeap(), 0, executableName);
141 skip("failed to get initial len\n");
142 return;
145 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
146 len * sizeof(WCHAR));
147 if (!friendlyName)
149 HeapFree(GetProcessHeap(), 0, executableName);
150 skip("failed to allocate memory\n");
151 return;
154 len2 = len;
155 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
156 friendlyName, &len2);
157 expect_hr(S_OK, hr);
158 slen = lstrlenW(friendlyName) + 1;
159 expect(len, len2);
160 expect(len, slen);
162 HeapFree(GetProcessHeap(), 0, executableName);
163 HeapFree(GetProcessHeap(), 0, friendlyName);
166 static void test_getstring_no_extra(void)
168 LONG ret;
169 HKEY hkey;
170 HRESULT hr;
171 static const CHAR dotWinetest[] = {
172 '.','w','i','n','e','t','e','s','t',0
174 static const CHAR winetestfile[] = {
175 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
177 static const CHAR winetestfileAction[] = {
178 'w','i','n','e','t','e','s','t','f','i','l','e',
179 '\\','s','h','e','l','l',
180 '\\','f','o','o',
181 '\\','c','o','m','m','a','n','d',0
183 static const CHAR action[] = {
184 'n','o','t','e','p','a','d','.','e','x','e',0
186 CHAR buf[MAX_PATH];
187 DWORD len = MAX_PATH;
189 if (!pAssocQueryStringA)
191 win_skip("AssocQueryStringA() is missing\n");
192 return;
195 buf[0] = '\0';
196 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
197 if (ret != ERROR_SUCCESS) {
198 skip("failed to create dotWinetest key\n");
199 return;
202 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
203 RegCloseKey(hkey);
204 if (ret != ERROR_SUCCESS)
206 skip("failed to set dotWinetest key\n");
207 goto cleanup;
210 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
211 if (ret != ERROR_SUCCESS)
213 skip("failed to create winetestfileAction key\n");
214 goto cleanup;
217 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
218 RegCloseKey(hkey);
219 if (ret != ERROR_SUCCESS)
221 skip("failed to set winetestfileAction key\n");
222 goto cleanup;
225 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
226 ok(hr == S_OK ||
227 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
228 "Unexpected result : %08x\n", hr);
229 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
230 expect_hr(S_OK, hr);
231 ok(strstr(buf, action) != NULL,
232 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
234 cleanup:
235 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
236 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
240 static void test_assoc_create(void)
242 HRESULT hr;
243 IQueryAssociations *pqa;
245 if (!pAssocCreate)
247 win_skip("AssocCreate() is missing\n");
248 return;
251 hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
252 ok(hr == E_INVALIDARG, "Unexpected result : %08x\n", hr);
254 hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
255 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_NOINTERFACE
256 , "Unexpected result : %08x\n", hr);
258 hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
259 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_INVALIDARG
260 , "Unexpected result : %08x\n", hr);
262 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
263 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
264 , "Unexpected result : %08x\n", hr);
265 if(hr == S_OK)
267 IQueryAssociations_Release(pqa);
270 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
271 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
272 , "Unexpected result : %08x\n", hr);
273 if(hr == S_OK)
275 IQueryAssociations_Release(pqa);
279 START_TEST(assoc)
281 HMODULE hshlwapi;
282 hshlwapi = GetModuleHandleA("shlwapi.dll");
283 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
284 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
285 pAssocCreate = (void*)GetProcAddress(hshlwapi, "AssocCreate");
287 test_getstring_bad();
288 test_getstring_basic();
289 test_getstring_no_extra();
290 test_assoc_create();