[REACTOS]
[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 HRESULT hr;
48 DWORD len;
49
50 if (!pAssocQueryStringW)
51 {
52 win_skip("AssocQueryStringW() is missing\n");
53 return;
54 }
55
56 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
57 expect_hr(E_INVALIDARG, hr);
58 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
59 ok(hr == E_FAIL ||
60 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
61 "Unexpected result : %08x\n", hr);
62 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotBad, open, NULL, &len);
63 ok(hr == E_FAIL ||
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, invalid, NULL,
67 &len);
68 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
69 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
70 "Unexpected result : %08x\n", hr);
71 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, NULL);
72 ok(hr == E_UNEXPECTED ||
73 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
74 "Unexpected result : %08x\n", hr);
75
76 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
77 expect_hr(E_INVALIDARG, hr);
78 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, 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, dotBad, open, NULL,
84 &len);
85 ok(hr == E_FAIL ||
86 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
87 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
88 "Unexpected result : %08x\n", hr);
89 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
90 &len);
91 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
92 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
93 hr == E_FAIL, /* Win9x/WinMe/NT4 */
94 "Unexpected result : %08x\n", hr);
95 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
96 NULL);
97 ok(hr == E_UNEXPECTED ||
98 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
99 "Unexpected result : %08x\n", hr);
100 }
101
102 static void test_getstring_basic(void)
103 {
104 HRESULT hr;
105 WCHAR * friendlyName;
106 WCHAR * executableName;
107 DWORD len, len2, slen;
108
109 if (!pAssocQueryStringW)
110 {
111 win_skip("AssocQueryStringW() is missing\n");
112 return;
113 }
114
115 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
116 expect_hr(S_FALSE, hr);
117 if (hr != S_FALSE)
118 {
119 skip("failed to get initial len\n");
120 return;
121 }
122
123 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
124 len * sizeof(WCHAR));
125 if (!executableName)
126 {
127 skip("failed to allocate memory\n");
128 return;
129 }
130
131 len2 = len;
132 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
133 executableName, &len2);
134 expect_hr(S_OK, hr);
135 slen = lstrlenW(executableName) + 1;
136 expect(len, len2);
137 expect(len, slen);
138
139 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
140 &len);
141 ok(hr == S_FALSE ||
142 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* Win9x/NT4 */ ||
143 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), /* Win8 */
144 "Unexpected result : %08x\n", hr);
145 if (hr != S_FALSE)
146 {
147 HeapFree(GetProcessHeap(), 0, executableName);
148 skip("failed to get initial len\n");
149 return;
150 }
151
152 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
153 len * sizeof(WCHAR));
154 if (!friendlyName)
155 {
156 HeapFree(GetProcessHeap(), 0, executableName);
157 skip("failed to allocate memory\n");
158 return;
159 }
160
161 len2 = len;
162 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
163 friendlyName, &len2);
164 expect_hr(S_OK, hr);
165 slen = lstrlenW(friendlyName) + 1;
166 expect(len, len2);
167 expect(len, slen);
168
169 HeapFree(GetProcessHeap(), 0, executableName);
170 HeapFree(GetProcessHeap(), 0, friendlyName);
171 }
172
173 static void test_getstring_no_extra(void)
174 {
175 LONG ret;
176 HKEY hkey;
177 HRESULT hr;
178 static const CHAR dotWinetest[] = {
179 '.','w','i','n','e','t','e','s','t',0
180 };
181 static const CHAR winetestfile[] = {
182 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
183 };
184 static const CHAR winetestfileAction[] = {
185 'w','i','n','e','t','e','s','t','f','i','l','e',
186 '\\','s','h','e','l','l',
187 '\\','f','o','o',
188 '\\','c','o','m','m','a','n','d',0
189 };
190 static const CHAR action[] = {
191 'n','o','t','e','p','a','d','.','e','x','e',0
192 };
193 CHAR buf[MAX_PATH];
194 DWORD len = MAX_PATH;
195
196 if (!pAssocQueryStringA)
197 {
198 win_skip("AssocQueryStringA() is missing\n");
199 return;
200 }
201
202 buf[0] = '\0';
203 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
204 if (ret != ERROR_SUCCESS) {
205 skip("failed to create dotWinetest key\n");
206 return;
207 }
208
209 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
210 RegCloseKey(hkey);
211 if (ret != ERROR_SUCCESS)
212 {
213 skip("failed to set dotWinetest key\n");
214 goto cleanup;
215 }
216
217 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
218 if (ret != ERROR_SUCCESS)
219 {
220 skip("failed to create winetestfileAction key\n");
221 goto cleanup;
222 }
223
224 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
225 RegCloseKey(hkey);
226 if (ret != ERROR_SUCCESS)
227 {
228 skip("failed to set winetestfileAction key\n");
229 goto cleanup;
230 }
231
232 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
233 ok(hr == S_OK ||
234 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
235 "Unexpected result : %08x\n", hr);
236 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
237 expect_hr(S_OK, hr);
238 ok(strstr(buf, action) != NULL,
239 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
240
241 cleanup:
242 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
243 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
244
245 }
246
247 static void test_assoc_create(void)
248 {
249 HRESULT hr;
250 IQueryAssociations *pqa;
251
252 if (!pAssocCreate)
253 {
254 win_skip("AssocCreate() is missing\n");
255 return;
256 }
257
258 hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
259 ok(hr == E_INVALIDARG, "Unexpected result : %08x\n", hr);
260
261 hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
262 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_NOINTERFACE
263 , "Unexpected result : %08x\n", hr);
264
265 hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
266 ok(hr == CLASS_E_CLASSNOTAVAILABLE || hr == E_NOTIMPL || hr == E_INVALIDARG
267 , "Unexpected result : %08x\n", hr);
268
269 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
270 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
271 , "Unexpected result : %08x\n", hr);
272 if(hr == S_OK)
273 {
274 IQueryAssociations_Release(pqa);
275 }
276
277 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
278 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
279 , "Unexpected result : %08x\n", hr);
280 if(hr == S_OK)
281 {
282 IQueryAssociations_Release(pqa);
283 }
284 }
285
286 START_TEST(assoc)
287 {
288 HMODULE hshlwapi;
289 hshlwapi = GetModuleHandleA("shlwapi.dll");
290 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
291 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
292 pAssocCreate = (void*)GetProcAddress(hshlwapi, "AssocCreate");
293
294 test_getstring_bad();
295 test_getstring_basic();
296 test_getstring_no_extra();
297 test_assoc_create();
298 }