shlwapi/tests: Don't test uninitialized parts of the variant.
[wine] / dlls / shlwapi / tests / assoc.c
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
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)
27
28 static HRESULT (WINAPI *pAssocQueryStringA)(ASSOCF,ASSOCSTR,LPCSTR,LPCSTR,LPSTR,LPDWORD) = NULL;
29 static HRESULT (WINAPI *pAssocQueryStringW)(ASSOCF,ASSOCSTR,LPCWSTR,LPCWSTR,LPWSTR,LPDWORD) = NULL;
30
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 };
37
38 static void test_getstring_bad(void)
39 {
40     HRESULT hr;
41     DWORD len;
42
43     if (!pAssocQueryStringW)
44     {
45         win_skip("AssocQueryStringW() is missing\n");
46         return;
47     }
48
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);
68
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);
92 }
93
94 static void test_getstring_basic(void)
95 {
96     HRESULT hr;
97     WCHAR * friendlyName;
98     WCHAR * executableName;
99     DWORD len, len2, slen;
100
101     if (!pAssocQueryStringW)
102     {
103         win_skip("AssocQueryStringW() is missing\n");
104         return;
105     }
106
107     hr = pAssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
108     expect_hr(S_FALSE, hr);
109     if (hr != S_FALSE)
110     {
111         skip("failed to get initial len\n");
112         return;
113     }
114
115     executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
116                                len * sizeof(WCHAR));
117     if (!executableName)
118     {
119         skip("failed to allocate memory\n");
120         return;
121     }
122
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);
130
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)
137     {
138         HeapFree(GetProcessHeap(), 0, executableName);
139         skip("failed to get initial len\n");
140         return;
141     }
142
143     friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
144                                len * sizeof(WCHAR));
145     if (!friendlyName)
146     {
147         HeapFree(GetProcessHeap(), 0, executableName);
148         skip("failed to allocate memory\n");
149         return;
150     }
151
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);
159
160     HeapFree(GetProcessHeap(), 0, executableName);
161     HeapFree(GetProcessHeap(), 0, friendlyName);
162 }
163
164 static void test_getstring_no_extra(void)
165 {
166     LONG ret;
167     HKEY hkey;
168     HRESULT hr;
169     static const CHAR dotWinetest[] = {
170         '.','w','i','n','e','t','e','s','t',0
171     };
172     static const CHAR winetestfile[] = {
173         'w','i','n','e','t','e','s','t', 'f','i','l','e',0
174     };
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
180     };
181     static const CHAR action[] = {
182         'n','o','t','e','p','a','d','.','e','x','e',0
183     };
184     CHAR buf[MAX_PATH];
185     DWORD len = MAX_PATH;
186
187     if (!pAssocQueryStringA)
188     {
189         win_skip("AssocQueryStringA() is missing\n");
190         return;
191     }
192
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;
198     }
199
200     ret = RegSetValueA(hkey, NULL, REG_SZ, winetestfile, lstrlenA(winetestfile));
201     RegCloseKey(hkey);
202     if (ret != ERROR_SUCCESS)
203     {
204         skip("failed to set dotWinetest key\n");
205         goto cleanup;
206     }
207
208     ret = RegCreateKeyA(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
209     if (ret != ERROR_SUCCESS)
210     {
211         skip("failed to create winetestfileAction key\n");
212         goto cleanup;
213     }
214
215     ret = RegSetValueA(hkey, NULL, REG_SZ, action, lstrlenA(action));
216     RegCloseKey(hkey);
217     if (ret != ERROR_SUCCESS)
218     {
219         skip("failed to set winetestfileAction key\n");
220         goto cleanup;
221     }
222
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);
231
232 cleanup:
233     SHDeleteKeyA(HKEY_CLASSES_ROOT, dotWinetest);
234     SHDeleteKeyA(HKEY_CLASSES_ROOT, winetestfile);
235
236 }
237
238 START_TEST(assoc)
239 {
240     HMODULE hshlwapi;
241     hshlwapi = GetModuleHandleA("shlwapi.dll");
242     pAssocQueryStringA = (void*)GetProcAddress(hshlwapi, "AssocQueryStringA");
243     pAssocQueryStringW = (void*)GetProcAddress(hshlwapi, "AssocQueryStringW");
244
245     test_getstring_bad();
246     test_getstring_basic();
247     test_getstring_no_extra();
248 }