push ada2d4f150af430b0cf01adcbe8b6d3e34deeeb1
[wine/hacks.git] / dlls / shlwapi / tests / assoc.c
blob3799ecc957ab398f61d01d9f2fa05a44354328c5
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"
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)
40 HRESULT hr;
41 DWORD len;
43 if (!pAssocQueryStringW)
45 win_skip("AssocQueryStringW() is missing\n");
46 return;
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);
52 ok(hr == E_FAIL ||
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);
56 ok(hr == E_FAIL ||
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,
60 &len);
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,
72 &len);
73 ok(hr == E_FAIL ||
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,
77 &len);
78 ok(hr == E_FAIL ||
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,
82 &len);
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,
88 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)
96 HRESULT hr;
97 WCHAR * friendlyName;
98 WCHAR * executableName;
99 DWORD len, len2, slen;
101 if (!pAssocQueryStringW)
103 win_skip("AssocQueryStringW() is missing\n");
104 return;
107 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
108 expect_hr(S_FALSE, hr);
109 if (hr != S_FALSE)
111 skip("failed to get initial len\n");
112 return;
115 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
116 len * sizeof(WCHAR));
117 if (!executableName)
119 skip("failed to allocate memory\n");
120 return;
123 len2 = len;
124 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
125 executableName, &len2);
126 expect_hr(S_OK, hr);
127 slen = lstrlenW(executableName) + 1;
128 expect(len, len2);
129 expect(len, slen);
131 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
132 &len);
133 ok(hr == S_FALSE ||
134 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* Win9x/NT4 */
135 "Unexpected result : %08x\n", hr);
136 if (hr != S_FALSE)
138 HeapFree(GetProcessHeap(), 0, executableName);
139 skip("failed to get initial len\n");
140 return;
143 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
144 len * sizeof(WCHAR));
145 if (!friendlyName)
147 HeapFree(GetProcessHeap(), 0, executableName);
148 skip("failed to allocate memory\n");
149 return;
152 len2 = len;
153 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
154 friendlyName, &len2);
155 expect_hr(S_OK, hr);
156 slen = lstrlenW(friendlyName) + 1;
157 expect(len, len2);
158 expect(len, slen);
160 HeapFree(GetProcessHeap(), 0, executableName);
161 HeapFree(GetProcessHeap(), 0, friendlyName);
164 static void test_getstring_no_extra(void)
166 LONG ret;
167 HKEY hkey;
168 HRESULT hr;
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',
178 '\\','f','o','o',
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
184 CHAR buf[MAX_PATH];
185 DWORD len = MAX_PATH;
187 if (!pAssocQueryStringA)
189 win_skip("AssocQueryStringA() is missing\n");
190 return;
193 buf[0] = '\0';
194 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
195 if (ret != ERROR_SUCCESS) {
196 skip("failed to create dotWinetest key\n");
197 return;
200 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
201 RegCloseKey(hkey);
202 if (ret != ERROR_SUCCESS)
204 skip("failed to set dotWinetest key\n");
205 goto cleanup;
208 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
209 if (ret != ERROR_SUCCESS)
211 skip("failed to create winetestfileAction key\n");
212 goto cleanup;
215 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
216 RegCloseKey(hkey);
217 if (ret != ERROR_SUCCESS)
219 skip("failed to set winetestfileAction key\n");
220 goto cleanup;
223 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
224 ok(hr == S_OK ||
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);
228 expect_hr(S_OK, hr);
229 ok(strstr(buf, action) != NULL,
230 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
232 cleanup:
233 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
234 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
238 START_TEST(assoc)
240 HMODULE hshlwapi;
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();