winmm: Fix a failing mixer test on 98 and ME.
[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 /* 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 };
34
35 /* copied from libs/wine/string.c */
36 WCHAR *strstrW(const WCHAR *str, const WCHAR *sub)
37 {
38     while (*str)
39     {
40         const WCHAR *p1 = str, *p2 = sub;
41         while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
42         if (!*p2) return (WCHAR *)str;
43         str++;
44     }
45     return NULL;
46 }
47
48 static void test_getstring_bad(void)
49 {
50     HRESULT hr;
51     DWORD len;
52
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);
64
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);
79 }
80
81 static void test_getstring_basic(void)
82 {
83     HRESULT hr;
84     WCHAR * friendlyName;
85     WCHAR * executableName;
86     DWORD len, len2, slen;
87
88     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
89     expect_hr(S_FALSE, hr);
90     if (hr != S_FALSE)
91     {
92         skip("failed to get initial len\n");
93         return;
94     }
95
96     executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
97                                len * sizeof(WCHAR));
98     if (!executableName)
99     {
100         skip("failed to allocate memory\n");
101         return;
102     }
103
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);
111
112     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
113                            &len);
114     expect_hr(S_FALSE, hr);
115     if (hr != S_FALSE)
116     {
117         HeapFree(GetProcessHeap(), 0, executableName);
118         skip("failed to get initial len\n");
119         return;
120     }
121
122     friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
123                                len * sizeof(WCHAR));
124     if (!friendlyName)
125     {
126         HeapFree(GetProcessHeap(), 0, executableName);
127         skip("failed to allocate memory\n");
128         return;
129     }
130
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);
138
139     HeapFree(GetProcessHeap(), 0, executableName);
140     HeapFree(GetProcessHeap(), 0, friendlyName);
141 }
142
143 static void test_getstring_no_extra(void)
144 {
145     LONG ret;
146     HKEY hkey;
147     HRESULT hr;
148     static const WCHAR dotWinetest[] = {
149         '.','w','i','n','e','t','e','s','t',0
150     };
151     static const WCHAR winetestfile[] = {
152         'w','i','n','e','t','e','s','t', 'f','i','l','e',0
153     };
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
159     };
160     static const WCHAR action[] = {
161         'n','o','t','e','p','a','d','.','e','x','e',0
162     };
163     WCHAR buf[MAX_PATH];
164     DWORD len = MAX_PATH;
165
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)
173     {
174         RegDeleteTreeW(HKEY_CLASSES_ROOT, dotWinetest);
175         skip("failed to set dotWinetest key\n");
176     }
177
178     ret = RegCreateKeyW(HKEY_CLASSES_ROOT, winetestfileAction, &hkey);
179     if (ret != ERROR_SUCCESS)
180     {
181         RegDeleteTreeW(HKEY_CLASSES_ROOT, dotWinetest);
182         skip("failed to create winetestfileAction key\n");
183     }
184     ret = RegSetValueW(hkey, NULL, REG_SZ, action, lstrlenW(action));
185     RegCloseKey(hkey);
186     if (ret != ERROR_SUCCESS)
187     {
188         RegDeleteTreeW(HKEY_CLASSES_ROOT, dotWinetest);
189         RegDeleteTreeW(HKEY_CLASSES_ROOT, winetestfile);
190         skip("failed to set winetestfileAction key\n");
191     }
192
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);
199 }
200
201 START_TEST(assoc)
202 {
203     test_getstring_bad();
204     test_getstring_basic();
205     test_getstring_no_extra();
206 }