push 8742e928a3d078c1d2cecb5ceee0ffde3118cbb7
[wine/hacks.git] / dlls / shlwapi / tests / assoc.c
blob2e383bb959ebc3957853aa8988a41cf116c982ac
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 /* Every version of Windows with IE should have this association? */
29 static const WCHAR dotHtml[] = { '.','h','t','m','l',0 };
30 static const WCHAR badBad[] = { 'b','a','d','b','a','d',0 };
31 static const WCHAR dotBad[] = { '.','b','a','d',0 };
32 static const WCHAR open[] = { 'o','p','e','n',0 };
33 static const WCHAR invalid[] = { 'i','n','v','a','l','i','d',0 };
35 /* copied from libs/wine/string.c */
36 WCHAR *strstrW(const WCHAR *str, const WCHAR *sub)
38 while (*str)
40 const WCHAR *p1 = str, *p2 = sub;
41 while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
42 if (!*p2) return (WCHAR *)str;
43 str++;
45 return NULL;
48 static void test_getstring_bad(void)
50 HRESULT hr;
51 DWORD len;
53 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
54 expect_hr(E_INVALIDARG, hr);
55 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
56 expect_hr(E_FAIL, hr);
57 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotBad, open, NULL, &len);
58 expect_hr(E_FAIL, hr);
59 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, invalid, NULL,
60 &len);
61 expect_hr(0x80070002, hr); /* NOT FOUND */
62 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, NULL);
63 expect_hr(E_UNEXPECTED, hr);
65 hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
66 expect_hr(E_INVALIDARG, hr);
67 hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, open, NULL,
68 &len);
69 expect_hr(E_FAIL, hr);
70 hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotBad, open, NULL,
71 &len);
72 expect_hr(E_FAIL, hr);
73 hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
74 &len);
75 expect_hr(0x80070002, hr); /* NOT FOUND */
76 hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
77 NULL);
78 expect_hr(E_UNEXPECTED, hr);
81 static void test_getstring_basic(void)
83 HRESULT hr;
84 WCHAR * friendlyName;
85 WCHAR * executableName;
86 DWORD len, len2, slen;
88 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
89 expect_hr(S_FALSE, hr);
90 if (hr != S_FALSE)
92 skip("failed to get initial len\n");
93 return;
96 executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
97 len * sizeof(WCHAR));
98 if (!executableName)
100 skip("failed to allocate memory\n");
101 return;
104 len2 = len;
105 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
106 executableName, &len2);
107 expect_hr(S_OK, hr);
108 slen = lstrlenW(executableName) + 1;
109 expect(len, len2);
110 expect(len, slen);
112 hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
113 &len);
114 expect_hr(S_FALSE, hr);
115 if (hr != S_FALSE)
117 HeapFree(GetProcessHeap(), 0, executableName);
118 skip("failed to get initial len\n");
119 return;
122 friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
123 len * sizeof(WCHAR));
124 if (!friendlyName)
126 HeapFree(GetProcessHeap(), 0, executableName);
127 skip("failed to allocate memory\n");
128 return;
131 len2 = len;
132 hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
133 friendlyName, &len2);
134 expect_hr(S_OK, hr);
135 slen = lstrlenW(friendlyName) + 1;
136 expect(len, len2);
137 expect(len, slen);
139 HeapFree(GetProcessHeap(), 0, executableName);
140 HeapFree(GetProcessHeap(), 0, friendlyName);
143 static void test_getstring_no_extra(void)
145 LONG ret;
146 HKEY hkey;
147 HRESULT hr;
148 static const WCHAR dotWinetest[] = {
149 '.','w','i','n','e','t','e','s','t',0
151 static const WCHAR winetestfile[] = {
152 'w','i','n','e','t','e','s','t', 'f','i','l','e',0
154 static const WCHAR winetestfileAction[] = {
155 'w','i','n','e','t','e','s','t','f','i','l','e',
156 '\\','s','h','e','l','l',
157 '\\','f','o','o',
158 '\\','c','o','m','m','a','n','d',0
160 static const WCHAR action[] = {
161 'n','o','t','e','p','a','d','.','e','x','e',0
163 WCHAR buf[MAX_PATH];
164 DWORD len = MAX_PATH;
166 ret = RegCreateKeyW(HKEY_CLASSES_ROOT, dotWinetest, &hkey);
167 if (ret != ERROR_SUCCESS)
168 skip("failed to create dotWinetest key\n");
169 ret = RegSetValueW(hkey, NULL, REG_SZ, winetestfile,
170 lstrlenW(winetestfile));
171 RegCloseKey(hkey);
172 if (ret != ERROR_SUCCESS)
174 RegDeleteTreeW(HKEY_CLASSES_ROOT, dotWinetest);
175 skip("failed to set dotWinetest key\n");
178 ret = RegCreateKeyW(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
179 if (ret != ERROR_SUCCESS)
181 RegDeleteTreeW(HKEY_CLASSES_ROOT, dotWinetest);
182 skip("failed to create winetestfileAction key\n");
184 ret = RegSetValueW(hkey, NULL, REG_SZ, action, lstrlenW(action));
185 RegCloseKey(hkey);
186 if (ret != ERROR_SUCCESS)
188 RegDeleteTreeW(HKEY_CLASSES_ROOT, dotWinetest);
189 RegDeleteTreeW(HKEY_CLASSES_ROOT, winetestfile);
190 skip("failed to set winetestfileAction key\n");
193 hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotWinetest, NULL,
194 buf, &len);
195 expect_hr(S_OK, hr);
196 ok(strstrW(buf, action) != NULL, "exe path does not contain notepad\n");
197 RegDeleteTreeW(HKEY_CLASSES_ROOT, dotWinetest);
198 RegDeleteTreeW(HKEY_CLASSES_ROOT, winetestfile);
201 START_TEST(assoc)
203 test_getstring_bad();
204 test_getstring_basic();
205 test_getstring_no_extra();