Fix gcc 4.0 -Wpointer-sign warnings.
[wine] / dlls / mshtml / tests / protocol.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define COBJMACROS
20
21 #include <wine/test.h>
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "ole2.h"
27 #include "urlmon.h"
28
29 #include "initguid.h"
30
31 DEFINE_GUID(CLSID_ResProtocol, 0x3050F3BC, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
32
33 static BOOL expect_GetBindInfo = FALSE, called_GetBindInfo = FALSE;
34 static BOOL expect_ReportProgress = FALSE, called_ReportProgress = FALSE;
35 static BOOL expect_ReportData = FALSE, called_ReportData = FALSE;
36 static BOOL expect_ReportResult = FALSE, called_ReportResult = FALSE;
37
38 static HRESULT expect_hrResult;
39 static BOOL expect_hr_win32err = FALSE;
40
41 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
42 {
43     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
44         *ppv = iface;
45         return S_OK;
46     }
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
51 {
52     return 2;
53 }
54
55 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
56 {
57     return 1;
58 }
59
60 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
61 {
62     ok(0, "unexpected call\n");
63     return E_NOTIMPL;
64 }
65
66 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
67         LPCWSTR szStatusText)
68 {
69     static const WCHAR text_html[] = {'t','e','x','t','/','h','t','m','l',0};
70
71     ok(expect_ReportProgress, "unexpected call\n");
72     ok(ulStatusCode == BINDSTATUS_MIMETYPEAVAILABLE,
73             "ulStatusCode=%ld expected BINDSTATUS_MIMETYPEAVAILABLE\n", ulStatusCode);
74     ok(!lstrcmpW(szStatusText, text_html), "szStatusText != text/html\n");
75     expect_ReportProgress = FALSE;
76     called_ReportProgress = TRUE;
77     return S_OK;
78 }
79
80 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF, ULONG ulProgress,
81         ULONG ulProgressMax)
82 {
83     ok(expect_ReportData, "unexpected call\n");
84     ok(ulProgress == ulProgressMax, "ulProgress != ulProgressMax\n");
85     ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE),
86             "grcf = %08lx\n", grfBSCF);
87     expect_ReportData = FALSE;
88     called_ReportData = TRUE;
89     return S_OK;
90 }
91
92 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult, DWORD dwError,
93         LPCWSTR szResult)
94 {
95     ok(expect_ReportResult, "unexpected call\n");
96     if(expect_hr_win32err)
97         ok((hrResult&0xffff0000) == ((FACILITY_WIN32 << 16)|0x80000000),
98                 "expected win32 err got: %08lx\n", hrResult);
99     else
100         ok(hrResult == expect_hrResult, "expected: %08lx got: %08lx\n", expect_hrResult, hrResult);
101     ok(dwError == 0, "dwError = %ld\n", dwError);
102     ok(!szResult, "szResult != NULL\n");
103     called_ReportResult = TRUE;
104     expect_ReportResult = FALSE;
105     return S_OK;
106 }
107
108 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
109     ProtocolSink_QueryInterface,
110     ProtocolSink_AddRef,
111     ProtocolSink_Release,
112     ProtocolSink_Switch,
113     ProtocolSink_ReportProgress,
114     ProtocolSink_ReportData,
115     ProtocolSink_ReportResult
116 };
117
118 static IInternetProtocolSink protocol_sink = {
119     &protocol_sink_vtbl
120 };
121
122 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
123 {
124     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
125         *ppv = iface;
126         return S_OK;
127     }
128     return E_NOINTERFACE;
129 }
130
131 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
132 {
133     return 2;
134 }
135
136 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
137 {
138     return 1;
139 }
140
141 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
142 {
143     ok(expect_GetBindInfo, "unexpected call\n");
144     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
145     if(grfBINDF)
146         ok(!*grfBINDF, "*grfBINDF != 0\n");
147     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
148     ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %ld\n", pbindinfo->cbSize);
149     called_GetBindInfo = TRUE;
150     expect_GetBindInfo = FALSE;
151     return S_OK;
152 }
153
154 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType, LPOLESTR *ppwzStr,
155         ULONG cEl, ULONG *pcElFetched)
156 {
157     ok(0, "unexpected call\n");
158     return E_NOTIMPL;
159 }
160
161 static IInternetBindInfoVtbl bind_info_vtbl = {
162     BindInfo_QueryInterface,
163     BindInfo_AddRef,
164     BindInfo_Release,
165     BindInfo_GetBindInfo,
166     BindInfo_GetBindString
167 };
168
169 static IInternetBindInfo bind_info = {
170     &bind_info_vtbl
171 };
172
173 static void test_protocol_fail(IInternetProtocol *protocol, LPCWSTR url, HRESULT expected_hres,
174         BOOL expect_win32err)
175 {
176     HRESULT hres;
177
178     expect_ReportResult = TRUE;
179     expect_GetBindInfo = TRUE;
180     expect_hrResult = expected_hres;
181     expect_hr_win32err = expect_win32err;
182     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
183     if(expect_win32err)
184         ok((hres&0xffff0000) == ((FACILITY_WIN32 << 16)|0x80000000), "expected win32 err got: %08lx\n", hres);
185     else
186         ok(hres == expected_hres, "expected: %08lx got: %08lx\n", expected_hres, hres);
187     ok(called_ReportResult, "expected ReportResult\n");
188     ok(called_GetBindInfo, "expected GetBindInfo\n");
189     expect_ReportResult =  called_ReportResult = FALSE;
190     expect_GetBindInfo = called_GetBindInfo = FALSE;
191 }
192
193 static void protocol_start(IInternetProtocol *protocol, LPCWSTR url)
194 {
195     HRESULT hres;
196
197     expect_GetBindInfo = TRUE;
198     expect_ReportResult = TRUE;
199     expect_ReportProgress = TRUE;
200     expect_ReportData = TRUE;
201     expect_hrResult = S_OK;
202     expect_hr_win32err = FALSE;
203     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
204     ok(hres == S_OK, "Start failed: %08lx\n", hres);
205     ok(called_GetBindInfo, "expected GetBindInfo\n");
206     todo_wine {
207         ok(called_ReportProgress, "expected ReportProgress\n");
208     }
209     ok(called_ReportData, "expected ReportData\n");
210     ok(called_ReportResult, "expected ReportResult\n");
211     called_GetBindInfo =  expect_GetBindInfo = FALSE;
212     called_ReportProgress = expect_ReportProgress = FALSE;
213     called_ReportData = expect_ReportData = FALSE;
214     called_ReportResult = expect_ReportResult = FALSE;
215 }
216
217 static void test_res_protocol(void)
218 {
219     IInternetProtocolInfo *protocol_info;
220     IUnknown *unk;
221     IClassFactory *factory;
222     HRESULT hres;
223
224     static const WCHAR blank_url[] =
225         {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l','/','b','l','a','n','k','.','h','t','m',0};
226     static const WCHAR wrong_url1[] =
227         {'m','s','h','t','m','l','.','d','l','l','/','b','l','a','n','k','.','m','t','h',0};
228     static const WCHAR wrong_url2[] =
229         {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l',0};
230     static const WCHAR wrong_url3[] =
231         {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l','/','x','x','.','h','t','m',0};
232     static const WCHAR wrong_url4[] =
233         {'r','e','s',':','/','/','x','x','.','d','l','l','/','b','l','a','n','k','.','h','t','m',0};
234
235
236     hres = CoGetClassObject(&CLSID_ResProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
237     ok(hres == S_OK, "CoGetClassObject failed: %08lx\n", hres);
238
239     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
240     ok(hres == S_OK, "Could not get IInternetProtocolInfo interface: %08lx\n", hres);
241     if(SUCCEEDED(hres)) {
242         /* TODO: test IInternetProtocol interface */
243         IInternetProtocol_Release(protocol_info);
244     }
245
246     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
247     ok(hres == S_OK, "Could not get IClassFactory interface\n");
248     if(SUCCEEDED(hres)) {
249         IInternetProtocol *protocol;
250         BYTE buf[512];
251         ULONG cb;
252         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
253         ok(hres == S_OK, "Could not get IInternetProtocol: %08lx\n", hres);
254
255         if(SUCCEEDED(hres)) {
256             test_protocol_fail(protocol, wrong_url1, MK_E_SYNTAX, FALSE);
257             test_protocol_fail(protocol, wrong_url2, MK_E_SYNTAX, FALSE);
258             test_protocol_fail(protocol, wrong_url3, E_FAIL, TRUE);
259             test_protocol_fail(protocol, wrong_url4, E_FAIL, TRUE);
260
261             cb = 0xdeadbeef;
262             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
263             ok(hres == E_FAIL, "Read returned %08lx expected E_FAIL\n", hres);
264             ok(cb == 0xdeadbeef, "cb=%lu expected 0xdeadbeef\n", cb);
265     
266             protocol_start(protocol, blank_url);
267             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
268             ok(hres == S_OK, "Read failed: %08lx\n", hres);
269             ok(cb == 2, "cb=%lu expected 2\n", cb);
270             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
271             ok(hres == S_OK, "Read failed: %08lx\n", hres);
272             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
273             ok(hres == S_FALSE, "Read failed: %08lx expected S_FALSE\n", hres);
274             ok(cb == 0, "cb=%lu expected 0\n", cb);
275             hres = IInternetProtocol_UnlockRequest(protocol);
276             ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
277
278             protocol_start(protocol, blank_url);
279             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
280             ok(hres == S_OK, "Read failed: %08lx\n", hres);
281             hres = IInternetProtocol_LockRequest(protocol, 0);
282             ok(hres == S_OK, "LockRequest failed: %08lx\n", hres);
283             hres = IInternetProtocol_UnlockRequest(protocol);
284             ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
285             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
286             ok(hres == S_OK, "Read failed: %08lx\n", hres);
287
288             protocol_start(protocol, blank_url);
289             hres = IInternetProtocol_LockRequest(protocol, 0);
290             ok(hres == S_OK, "LockRequest failed: %08lx\n", hres);
291             hres = IInternetProtocol_Terminate(protocol, 0);
292             ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
293             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
294             ok(hres == S_OK, "Read failed: %08lx\n\n", hres);
295             hres = IInternetProtocol_UnlockRequest(protocol);
296             ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
297             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
298             ok(hres == S_OK, "Read failed: %08lx\n", hres);
299             hres = IInternetProtocol_Terminate(protocol, 0);
300             ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
301             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
302             ok(hres == S_OK, "Read failed: %08lx\n", hres);
303             ok(cb == 2, "cb=%lu expected 2\n", cb);
304
305             protocol_start(protocol, blank_url);
306             hres = IInternetProtocol_LockRequest(protocol, 0);
307             ok(hres == S_OK, "LockRequest failed: %08lx\n", hres);
308             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
309             ok(hres == S_OK, "Read failed: %08lx\n", hres);
310             protocol_start(protocol, blank_url);
311             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
312             ok(hres == S_OK, "Read failed: %08lx\n", hres);
313
314             IInternetProtocol_Release(protocol);
315         }
316
317         IClassFactory_Release(factory);
318     }
319
320     IUnknown_Release(unk);
321 }
322
323 START_TEST(protocol)
324 {
325     OleInitialize(NULL);
326
327     test_res_protocol();
328
329     OleUninitialize();
330 }