ReactOS 0.4.16-dev-1948-gd260c1d
assoc.c
Go to the documentation of this file.
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#include <stdarg.h>
21
22#include "wine/test.h"
23#include "shlwapi.h"
24#include "shlguid.h"
25
26#define expect(expected, got) ok ( expected == got, "Expected %ld, got %ld\n", expected, got)
27#define expect_hr(expected, got) ok ( expected == got, "Expected %08lx, got %08lx\n", expected, got)
28
29static HRESULT (WINAPI *pAssocQueryStringA)(ASSOCF,ASSOCSTR,LPCSTR,LPCSTR,LPSTR,LPDWORD) = NULL;
30static HRESULT (WINAPI *pAssocQueryStringW)(ASSOCF,ASSOCSTR,LPCWSTR,LPCWSTR,LPWSTR,LPDWORD) = NULL;
31static HRESULT (WINAPI *pAssocCreate)(CLSID, REFIID, void **) = NULL;
32
33/* Should every version of Windows with IE have .html association? */
34
35static void test_getstring_bad(void)
36{
37 static const WCHAR openwith[] = {'O','p','e','n','W','i','t','h','.','e','x','e',0};
39 HRESULT hr;
40 DWORD len;
41
42 if (!pAssocQueryStringW)
43 {
44 win_skip("AssocQueryStringW() is missing\n");
45 return;
46 }
47
48 len = 0xdeadbeef;
49 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, L"open", NULL, &len);
51 ok(len == 0xdeadbeef, "got %lu\n", len);
52
53 len = 0xdeadbeef;
54 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, L"badbad", L"open", NULL, &len);
55 ok(hr == E_FAIL ||
56 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
57 "Unexpected result : %08lx\n", hr);
58 ok(len == 0xdeadbeef, "got %lu\n", len);
59
61 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, L".bad", L"open", buf, &len);
62 ok(hr == E_FAIL ||
63 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
64 hr == S_OK /* Win8 */,
65 "Unexpected result : %08lx\n", hr);
66 if (hr == S_OK)
67 {
68 ok(len < ARRAY_SIZE(buf), "got %lu\n", len);
69 ok(!lstrcmpiW(buf + len - ARRAY_SIZE(openwith), openwith), "wrong data\n");
70 }
71
72 len = 0xdeadbeef;
73 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, L".html", L"invalid", NULL, &len);
75 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
76 "Unexpected result : %08lx\n", hr);
77 ok(len == 0xdeadbeef, "got %lu\n", len);
78
79 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, L".html", L"open", NULL, NULL);
80 ok(hr == E_UNEXPECTED ||
81 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
82 "Unexpected result : %08lx\n", hr);
83
84 len = 0xdeadbeef;
85 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, L"open", NULL, &len);
87 ok(len == 0xdeadbeef, "got %lu\n", len);
88
89 len = 0xdeadbeef;
90 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, L"badbad", L"open", NULL, &len);
91 ok(hr == E_FAIL ||
92 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION), /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
93 "Unexpected result : %08lx\n", hr);
94 ok(len == 0xdeadbeef, "got %lu\n", len);
95
96 len = 0xdeadbeef;
97 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, L".bad", L"open", NULL, &len);
98 ok(hr == E_FAIL ||
99 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */ ||
100 hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND) /* Win8 */ ||
101 hr == S_FALSE, /* Win10 */
102 "Unexpected result : %08lx\n", hr);
103 ok((hr == S_FALSE && len < ARRAY_SIZE(buf)) || len == 0xdeadbeef,
104 "got hr=%08lx and len=%lu\n", hr, len);
105
106 len = 0xdeadbeef;
107 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, L".html", L"invalid", NULL, &len);
109 hr == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION) || /* W2K/Vista/W2K8 */
110 hr == E_FAIL, /* Win9x/WinMe/NT4 */
111 "Unexpected result : %08lx\n", hr);
112 ok(len == 0xdeadbeef, "got %lu\n", len);
113
114 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, L".html", L"open", NULL, NULL);
115 ok(hr == E_UNEXPECTED ||
116 hr == E_INVALIDARG, /* Win9x/WinMe/NT4/W2K/Vista/W2K8 */
117 "Unexpected result : %08lx\n", hr);
118}
119
120static void test_getstring_basic(void)
121{
122 HRESULT hr;
123 WCHAR * friendlyName;
124 WCHAR * executableName;
125 DWORD len, len2, slen;
126
127 if (!pAssocQueryStringW)
128 {
129 win_skip("AssocQueryStringW() is missing\n");
130 return;
131 }
132
133 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, L".html", L"open", NULL, &len);
135 if (hr != S_FALSE)
136 {
137 skip("failed to get initial len\n");
138 return;
139 }
140
141 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
142 len * sizeof(WCHAR));
143 if (!executableName)
144 {
145 skip("failed to allocate memory\n");
146 return;
147 }
148
149 len2 = len;
150 hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, L".html", L"open",
151 executableName, &len2);
152 expect_hr(S_OK, hr);
153 slen = lstrlenW(executableName) + 1;
154 expect(len, len2);
155 expect(len, slen);
156
157 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, L".html", L"open", NULL,
158 &len);
159 ok(hr == S_FALSE ||
160 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) /* Win9x/NT4 */ ||
162 "Unexpected result : %08lx\n", hr);
163 if (hr != S_FALSE)
164 {
165 HeapFree(GetProcessHeap(), 0, executableName);
166 skip("failed to get initial len\n");
167 return;
168 }
169
170 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
171 len * sizeof(WCHAR));
172 if (!friendlyName)
173 {
174 HeapFree(GetProcessHeap(), 0, executableName);
175 skip("failed to allocate memory\n");
176 return;
177 }
178
179 len2 = len;
180 hr = pAssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, L".html", L"open",
181 friendlyName, &len2);
182 expect_hr(S_OK, hr);
183 slen = lstrlenW(friendlyName) + 1;
184 expect(len, len2);
185 expect(len, slen);
186
187 HeapFree(GetProcessHeap(), 0, executableName);
188 HeapFree(GetProcessHeap(), 0, friendlyName);
189}
190
191static void test_getstring_no_extra(void)
192{
193 LONG ret;
194 HKEY hkey;
195 HRESULT hr;
196 static const CHAR dotWinetest[] = {
197 '.','w','i','n','e','t','e','s','t',0
198 };
199 static const CHAR winetestfile[] = {
200 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
201 };
202 static const CHAR winetestfileAction[] = {
203 'w','i','n','e','t','e','s','t','f','i','l','e',
204 '\\','s','h','e','l','l',
205 '\\','f','o','o',
206 '\\','c','o','m','m','a','n','d',0
207 };
208 static const CHAR action[] = {
209 'n','o','t','e','p','a','d','.','e','x','e',0
210 };
213
214 if (!pAssocQueryStringA)
215 {
216 win_skip("AssocQueryStringA() is missing\n");
217 return;
218 }
219
220 buf[0] = '\0';
221 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
222 if (ret != ERROR_SUCCESS) {
223 skip("failed to create dotWinetest key\n");
224 return;
225 }
226
227 ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
228 RegCloseKey(hkey);
229 if (ret != ERROR_SUCCESS)
230 {
231 skip("failed to set dotWinetest key\n");
232 goto cleanup;
233 }
234
235 ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
236 if (ret != ERROR_SUCCESS)
237 {
238 skip("failed to create winetestfileAction key\n");
239 goto cleanup;
240 }
241
243 RegCloseKey(hkey);
244 if (ret != ERROR_SUCCESS)
245 {
246 skip("failed to set winetestfileAction key\n");
247 goto cleanup;
248 }
249
250 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL, buf, &len);
251 ok(hr == S_OK ||
252 hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), /* XP and W2K3 */
253 "Unexpected result : %08lx\n", hr);
254 hr = pAssocQueryStringA(0, ASSOCSTR_EXECUTABLE, dotWinetest, "foo", buf, &len);
255 expect_hr(S_OK, hr);
256 ok(strstr(buf, action) != NULL,
257 "got '%s' (Expected result to include 'notepad.exe')\n", buf);
258
259cleanup:
260 SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
261 SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
262
263}
264
265static void test_assoc_create(void)
266{
267 HRESULT hr;
268 IQueryAssociations *pqa;
269
270 if (!pAssocCreate)
271 {
272 win_skip("AssocCreate() is missing\n");
273 return;
274 }
275
276 hr = pAssocCreate(IID_NULL, &IID_NULL, NULL);
277 ok(hr == E_INVALIDARG, "Unexpected result : %08lx\n", hr);
278
279 hr = pAssocCreate(CLSID_QueryAssociations, &IID_NULL, (LPVOID*)&pqa);
281 , "Unexpected result : %08lx\n", hr);
282
283 hr = pAssocCreate(IID_NULL, &IID_IQueryAssociations, (LPVOID*)&pqa);
285 , "Unexpected result : %08lx\n", hr);
286
287 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IQueryAssociations, (LPVOID*)&pqa);
288 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
289 , "Unexpected result : %08lx\n", hr);
290 if(hr == S_OK)
291 {
293 }
294
295 hr = pAssocCreate(CLSID_QueryAssociations, &IID_IUnknown, (LPVOID*)&pqa);
296 ok(hr == S_OK || hr == E_NOTIMPL /* win98 */
297 , "Unexpected result : %08lx\n", hr);
298 if(hr == S_OK)
299 {
301 }
302}
303
305{
307 hshlwapi = GetModuleHandleA("shlwapi.dll");
308 pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
309 pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
310 pAssocCreate = (void*)GetProcAddress(hshlwapi, "AssocCreate");
311
316}
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
#define ARRAY_SIZE(A)
Definition: main.h:20
const GUID IID_IUnknown
#define RegCloseKey(hKey)
Definition: registry.h:49
#define E_INVALIDARG
Definition: ddrawi.h:101
#define E_NOTIMPL
Definition: ddrawi.h:99
#define E_FAIL
Definition: ddrawi.h:102
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
LONG WINAPI RegCreateKeyA(HKEY hKey, LPCSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:1179
LONG WINAPI RegSetValueA(HKEY hKeyOriginal, LPCSTR lpSubKey, DWORD dwType, LPCSTR lpData, DWORD cbData)
Definition: reg.c:4954
#define GetProcessHeap()
Definition: compat.h:736
#define GetProcAddress(x, y)
Definition: compat.h:753
#define HeapAlloc
Definition: compat.h:733
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define lstrlenW
Definition: compat.h:750
static void cleanup(void)
Definition: main.c:1335
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
Definition: locale.c:4265
DWORD WINAPI SHDeleteKeyA(HKEY hKey, LPCSTR lpszSubKey)
Definition: reg.c:1533
return ret
Definition: mutex.c:146
action
Definition: namespace.c:707
#define L(x)
Definition: resources.c:13
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLsizei len
Definition: glext.h:6722
#define S_OK
Definition: intsafe.h:52
#define REG_SZ
Definition: layer.c:22
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
#define win_skip
Definition: minitest.h:67
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define expect_hr(expected, got)
Definition: assoc.c:27
static void **static void test_getstring_bad(void)
Definition: assoc.c:35
static LPSTR
Definition: assoc.c:29
static void test_getstring_basic(void)
Definition: assoc.c:120
static ASSOCSTR
Definition: assoc.c:29
static LPWSTR
Definition: assoc.c:30
static void test_assoc_create(void)
Definition: assoc.c:265
static void test_getstring_no_extra(void)
Definition: assoc.c:191
#define expect(expected, got)
Definition: assoc.c:26
static LPCWSTR
Definition: assoc.c:30
static LPDWORD
Definition: assoc.c:29
static LPCSTR
Definition: assoc.c:29
IID CLSID
Definition: mstsclib_i.c:62
long LONG
Definition: pedump.c:60
#define REFIID
Definition: guiddef.h:118
#define IID_NULL
Definition: guiddef.h:98
#define IQueryAssociations_Release(p)
Definition: shlwapi.h:987
@ ASSOCSTR_FRIENDLYAPPNAME
Definition: shlwapi.h:889
@ ASSOCSTR_EXECUTABLE
Definition: shlwapi.h:887
DWORD ASSOCF
Definition: shlwapi.h:967
HRESULT hr
Definition: shlfolder.c:183
static HMODULE hshlwapi
Definition: shreg.c:35
#define HRESULT
Definition: msvc.h:7
#define WINAPI
Definition: msvc.h:6
#define S_FALSE
Definition: winerror.h:3451
static HRESULT HRESULT_FROM_WIN32(unsigned int x)
Definition: winerror.h:210
#define E_NOINTERFACE
Definition: winerror.h:3479
#define ERROR_NO_ASSOCIATION
Definition: winerror.h:1001
#define E_UNEXPECTED
Definition: winerror.h:3528
#define CLASS_E_CLASSNOTAVAILABLE
Definition: winerror.h:3772
#define ERROR_NOT_FOUND
Definition: winerror.h:1014
#define HKEY_CLASSES_ROOT
Definition: winreg.h:10
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175