shlwapi/tests: Fix some failures on XP and W2K3.
[wine.git] / dlls / shlwapi / tests / assoc.c
blob3e4d8d4f40fcd0389d3ca7849ef2dcc6bb2e4381
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 expect_hr(S_FALSE, hr);
134 if (hr != S_FALSE)
136 HeapFree(GetProcessHeap(), 0, executableName);
137 skip("failed to get initial len\n");
138 return;
141 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
142 len * sizeof(WCHAR));
143 if (!friendlyName)
145 HeapFree(GetProcessHeap(), 0, executableName);
146 skip("failed to allocate memory\n");
147 return;
150 len2 = len;
151 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
152 friendlyName, &len2);
153 expect_hr(S_OK, hr);
154 slen = lstrlenW(friendlyName) + 1;
155 expect(len, len2);
156 expect(len, slen);
158 HeapFree(GetProcessHeap(), 0, executableName);
159 HeapFree(GetProcessHeap(), 0, friendlyName);
162 static void test_getstring_no_extra(void)
164 LONG ret;
165 HKEY hkey;
166 HRESULT hr;
167 static const CHAR dotWinetest[] = {
168 '.','w','i','n','e','t','e','s','t',0
170 static const CHAR winetestfile[] = {
171 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
173 static const CHAR winetestfileAction[] = {
174 'w','i','n','e','t','e','s','t','f','i','l','e',
175 '\\','s','h','e','l','l',
176 '\\','f','o','o',
177 '\\','c','o','m','m','a','n','d',0
179 static const CHAR action[] = {
180 'n','o','t','e','p','a','d','.','e','x','e',0
182 CHAR buf[MAX_PATH];
183 DWORD len = MAX_PATH;
185 if (!pAssocQueryStringA)
187 win_skip("AssocQueryStringA() is missing\n");
188 return;
191 buf[0] = '\0';
192 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
193 if (ret != ERROR_SUCCESS) {
194 skip("failed to create dotWinetest key\n");
195 return;
198 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
199 RegCloseKey(hkey);
200 if (ret != ERROR_SUCCESS)
202 skip("failed to set dotWinetest key\n");
203 goto cleanup;
206 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
207 if (ret != ERROR_SUCCESS)
209 skip("failed to create winetestfileAction key\n");
210 goto cleanup;
213 ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
214 RegCloseKey(hkey);
215 if (ret != ERROR_SUCCESS)
217 skip("failed to set winetestfileAction key\n");
218 goto cleanup;
221 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
222 ok(hr == S_OK ||
223 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
224 "Unexpected result : %08x\n", hr);
225 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
226 expect_hr(S_OK, hr);
227 ok(strstr(buf, action) != NULL,
228 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
230 cleanup:
231 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
232 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
236 START_TEST(assoc)
238 HMODULE hshlwapi;
239 hshlwapi = GetModuleHandleA("shlwapi.dll");
240 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
241 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
243 test_getstring_bad();
244 test_getstring_basic();
245 test_getstring_no_extra();