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
22 #include "wine/test.h"
25 #define expect(expected, got) ok ( expected == got, "Expected %d, got %d\n", expected, got)
26 #define expect_hr(expected, got) ok ( expected == got, "Expected %08x, got %08x\n", expected, got)
28 static HRESULT (WINAPI *pAssocQueryStringA)(ASSOCF,ASSOCSTR,LPCSTR,LPCSTR,LPSTR,LPDWORD) = NULL;
29 static HRESULT (WINAPI *pAssocQueryStringW)(ASSOCF,ASSOCSTR,LPCWSTR,LPCWSTR,LPWSTR,LPDWORD) = NULL;
31 /* Every version of Windows with IE should have this association? */
32 static const WCHAR dotHtml[] = { '.','h','t','m','l',0 };
33 static const WCHAR badBad[] = { 'b','a','d','b','a','d',0 };
34 static const WCHAR dotBad[] = { '.','b','a','d',0 };
35 static const WCHAR open[] = { 'o','p','e','n',0 };
36 static const WCHAR invalid[] = { 'i','n','v','a','l','i','d',0 };
38 static void test_getstring_bad(void)
43 if (!pAssocQueryStringW)
45 win_skip("AssocQueryStringW() is missing\n");
49 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
50 expect_hr(E_INVALIDARG, hr);
51 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
53 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
54 "Unexpected result : %08x\n", hr);
55 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotBad, open, NULL, &len);
57 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
58 "Unexpected result : %08x\n", hr);
59 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, invalid, NULL,
61 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
62 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
63 "Unexpected result : %08x\n", hr);
64 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, NULL);
65 ok(hr == E_UNEXPECTED ||
66 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
67 "Unexpected result : %08x\n", hr);
69 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
70 expect_hr(E_INVALIDARG, hr);
71 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, open, NULL,
74 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
75 "Unexpected result : %08x\n", hr);
76 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotBad, open, NULL,
79 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
80 "Unexpected result : %08x\n", hr);
81 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
83 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
84 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
85 hr == E_FAIL, /* Win9x/WinMe/NT4 */
86 "Unexpected result : %08x\n", hr);
87 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
89 ok(hr == E_UNEXPECTED ||
90 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
91 "Unexpected result : %08x\n", hr);
94 static void test_getstring_basic(void)
98 WCHAR * executableName;
99 DWORD len, len2, slen;
101 if (!pAssocQueryStringW)
103 win_skip("AssocQueryStringW() is missing\n");
107 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
108 expect_hr(S_FALSE, hr);
111 skip("failed to get initial len\n");
115 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
116 len * sizeof(WCHAR));
119 skip("failed to allocate memory\n");
124 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
125 executableName, &len2);
127 slen = lstrlenW(executableName) + 1;
131 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
134 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* Win9x/NT4 */
135 "Unexpected result : %08x\n", hr);
138 HeapFree(GetProcessHeap(), 0, executableName);
139 skip("failed to get initial len\n");
143 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
144 len * sizeof(WCHAR));
147 HeapFree(GetProcessHeap(), 0, executableName);
148 skip("failed to allocate memory\n");
153 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
154 friendlyName, &len2);
156 slen = lstrlenW(friendlyName) + 1;
160 HeapFree(GetProcessHeap(), 0, executableName);
161 HeapFree(GetProcessHeap(), 0, friendlyName);
164 static void test_getstring_no_extra(void)
169 static const CHAR dotWinetest[] = {
170 '.','w','i','n','e','t','e','s','t',0
172 static const CHAR winetestfile[] = {
173 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
175 static const CHAR winetestfileAction[] = {
176 'w','i','n','e','t','e','s','t','f','i','l','e',
177 '\\','s','h','e','l','l',
179 '\\','c','o','m','m','a','n','d',0
181 static const CHAR action[] = {
182 'n','o','t','e','p','a','d','.','e','x','e',0
185 DWORD len = MAX_PATH;
187 if (!pAssocQueryStringA)
189 win_skip("AssocQueryStringA() is missing\n");
194 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
195 if (ret != ERROR_SUCCESS) {
196 skip("failed to create dotWinetest key\n");
200 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
202 if (ret != ERROR_SUCCESS)
204 skip("failed to set dotWinetest key\n");
208 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
209 if (ret != ERROR_SUCCESS)
211 skip("failed to create winetestfileAction key\n");
215 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
217 if (ret != ERROR_SUCCESS)
219 skip("failed to set winetestfileAction key\n");
223 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
225 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
226 "Unexpected result : %08x\n", hr);
227 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
229 ok(strstr(buf, action) != NULL,
230 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
233 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
234 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
241 hshlwapi = GetModuleHandleA("shlwapi.dll");
242 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
243 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
245 test_getstring_bad();
246 test_getstring_basic();
247 test_getstring_no_extra();