Release 20050930.
[wine/gsoc-2012-control.git] / dlls / urlmon / tests / protocol.c
blobeea12843a7e68173db20d6ff1109f1fce14114f3
1 /*
2 * Copyright 2005 Jacek Caban
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.
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.
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
19 #define COBJMACROS
21 #include <wine/test.h>
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "ole2.h"
27 #include "urlmon.h"
29 #include "initguid.h"
31 #define DEFINE_EXPECT(func) \
32 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
34 #define SET_EXPECT(func) \
35 expect_ ## func = TRUE
37 #define CHECK_EXPECT(func) \
38 ok(expect_ ##func, "unexpected call\n"); \
39 expect_ ## func = FALSE; \
40 called_ ## func = TRUE
42 #define CHECK_EXPECT2(func) \
43 ok(expect_ ##func, "unexpected call\n"); \
44 called_ ## func = TRUE
46 #define CHECK_CALLED(func) \
47 ok(called_ ## func, "expected " #func "\n"); \
48 expect_ ## func = called_ ## func = FALSE
50 DEFINE_EXPECT(GetBindInfo);
51 DEFINE_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
52 DEFINE_EXPECT(ReportProgress_DIRECTBIND);
53 DEFINE_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
54 DEFINE_EXPECT(ReportData);
55 DEFINE_EXPECT(ReportResult);
57 static HRESULT expect_hrResult;
58 static LPCWSTR file_name;
60 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
62 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
63 *ppv = iface;
64 return S_OK;
66 return E_NOINTERFACE;
69 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
71 return 2;
74 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
76 return 1;
79 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
81 ok(0, "unexpected call\n");
82 return E_NOTIMPL;
85 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
86 LPCWSTR szStatusText)
88 static const WCHAR text_html[] = {'t','e','x','t','/','h','t','m','l',0};
90 switch(ulStatusCode) {
91 case BINDSTATUS_MIMETYPEAVAILABLE:
92 CHECK_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
93 if(szStatusText)
94 ok(!lstrcmpW(szStatusText, text_html), "szStatusText != text/html\n");
95 case BINDSTATUS_DIRECTBIND:
96 CHECK_EXPECT2(ReportProgress_DIRECTBIND);
97 if(szStatusText)
98 ok(!lstrcmpW(szStatusText, text_html), "szStatusText != text/html\n");
99 break;
100 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
101 CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
102 if(szStatusText)
103 ok(!lstrcmpW(szStatusText, file_name), "szStatusText != file_name\n");
104 break;
107 return S_OK;
110 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
111 ULONG ulProgress, ULONG ulProgressMax)
113 CHECK_EXPECT(ReportData);
115 ok(ulProgress == ulProgressMax, "ulProgress != ulProgressMax\n");
116 ok(ulProgressMax == 13, "ulProgressMax=%ld, expected 13\n", ulProgressMax);
117 ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION),
118 "grcf = %08lx\n", grfBSCF);
120 return S_OK;
123 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
124 DWORD dwError, LPCWSTR szResult)
126 CHECK_EXPECT(ReportResult);
128 ok(hrResult == expect_hrResult, "hrResult = %08lx, expected: %08lx\n",
129 hrResult, expect_hrResult);
130 if(SUCCEEDED(hrResult))
131 ok(dwError == ERROR_SUCCESS, "dwError = %ld, expected ERROR_SUCCESS\n", dwError);
132 else
133 ok(dwError != ERROR_SUCCESS, "dwError == ERROR_SUCCESS\n");
134 ok(!szResult, "szResult != NULL\n");
136 return S_OK;
139 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
140 ProtocolSink_QueryInterface,
141 ProtocolSink_AddRef,
142 ProtocolSink_Release,
143 ProtocolSink_Switch,
144 ProtocolSink_ReportProgress,
145 ProtocolSink_ReportData,
146 ProtocolSink_ReportResult
149 static IInternetProtocolSink protocol_sink = { &protocol_sink_vtbl };
151 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
153 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
154 *ppv = iface;
155 return S_OK;
157 return E_NOINTERFACE;
160 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
162 return 2;
165 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
167 return 1;
170 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
172 CHECK_EXPECT(GetBindInfo);
174 ok(grfBINDF != NULL, "grfBINDF == NULL\n");
175 if(grfBINDF)
176 ok(!*grfBINDF, "*grfBINDF != 0\n");
177 ok(pbindinfo != NULL, "pbindinfo == NULL\n");
178 ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %ld\n", pbindinfo->cbSize);
180 return S_OK;
183 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType,
184 LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
186 ok(0, "unexpected call\n");
187 return E_NOTIMPL;
190 static IInternetBindInfoVtbl bind_info_vtbl = {
191 BindInfo_QueryInterface,
192 BindInfo_AddRef,
193 BindInfo_Release,
194 BindInfo_GetBindInfo,
195 BindInfo_GetBindString
198 static IInternetBindInfo bind_info = { &bind_info_vtbl };
200 static void file_protocol_start(IInternetProtocol *protocol, LPCWSTR url, BOOL is_first)
202 HRESULT hres;
204 SET_EXPECT(GetBindInfo);
205 SET_EXPECT(ReportProgress_DIRECTBIND);
206 if(is_first) {
207 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
208 SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
209 SET_EXPECT(ReportResult);
211 SET_EXPECT(ReportData);
212 expect_hrResult = S_OK;
214 hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
215 ok(hres == S_OK, "Start failed: %08lx\n", hres);
217 CHECK_CALLED(GetBindInfo);
218 CHECK_CALLED(ReportProgress_DIRECTBIND);
219 if(is_first) {
220 CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
221 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
222 CHECK_CALLED(ReportResult);
224 CHECK_CALLED(ReportData);
227 static void test_file_protocol_url(LPCWSTR url)
229 IInternetProtocolInfo *protocol_info;
230 IUnknown *unk;
231 IClassFactory *factory;
232 HRESULT hres;
234 hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
235 &IID_IUnknown, (void**)&unk);
236 ok(hres == S_OK, "CoGetClassObject failed: %08lx\n", hres);
237 if(!SUCCEEDED(hres))
238 return;
240 hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
241 ok(hres == E_NOINTERFACE,
242 "Could not get IInternetProtocolInfo interface: %08lx, expected E_NOINTERFACE\n", hres);
244 hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
245 ok(hres == S_OK, "Could not get IClassFactory interface\n");
246 if(SUCCEEDED(hres)) {
247 IInternetProtocol *protocol;
248 BYTE buf[512];
249 ULONG cb;
250 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
251 ok(hres == S_OK, "Could not get IInternetProtocol: %08lx\n", hres);
253 if(SUCCEEDED(hres)) {
254 file_protocol_start(protocol, url, TRUE);
255 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
256 ok(hres == S_OK, "Read failed: %08lx\n", hres);
257 ok(cb == 2, "cb=%lu expected 2\n", cb);
258 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
259 ok(hres == S_FALSE, "Read failed: %08lx\n", hres);
260 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
261 ok(hres == S_FALSE, "Read failed: %08lx expected S_FALSE\n", hres);
262 ok(cb == 0, "cb=%lu expected 0\n", cb);
263 hres = IInternetProtocol_UnlockRequest(protocol);
264 ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
266 file_protocol_start(protocol, url, FALSE);
267 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
268 ok(hres == S_FALSE, "Read failed: %08lx\n", hres);
269 hres = IInternetProtocol_LockRequest(protocol, 0);
270 ok(hres == S_OK, "LockRequest failed: %08lx\n", hres);
271 hres = IInternetProtocol_UnlockRequest(protocol);
272 ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
274 IInternetProtocol_Release(protocol);
277 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
278 ok(hres == S_OK, "Could not get IInternetProtocol: %08lx\n", hres);
280 if(SUCCEEDED(hres)) {
281 file_protocol_start(protocol, url, TRUE);
282 hres = IInternetProtocol_LockRequest(protocol, 0);
283 ok(hres == S_OK, "LockRequest failed: %08lx\n", hres);
284 hres = IInternetProtocol_Terminate(protocol, 0);
285 ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
286 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
287 ok(hres == S_OK, "Read failed: %08lx\n\n", hres);
288 hres = IInternetProtocol_UnlockRequest(protocol);
289 ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
290 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
291 ok(hres == S_OK, "Read failed: %08lx\n", hres);
292 hres = IInternetProtocol_Terminate(protocol, 0);
293 ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
295 IInternetProtocol_Release(protocol);
298 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
299 ok(hres == S_OK, "Could not get IInternetProtocol: %08lx\n", hres);
301 if(SUCCEEDED(hres)) {
302 file_protocol_start(protocol, url, TRUE);
303 hres = IInternetProtocol_Terminate(protocol, 0);
304 ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
305 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
306 ok(hres == S_OK, "Read failed: %08lx\n", hres);
307 ok(cb == 2, "cb=%lu expected 2\n", cb);
309 IInternetProtocol_Release(protocol);
312 IClassFactory_Release(factory);
315 IUnknown_Release(unk);
318 static void test_file_protocol(void) {
319 IInternetProtocol *protocol;
320 WCHAR buf[MAX_PATH];
321 DWORD size;
322 ULONG len;
323 HANDLE file;
324 HRESULT hres;
326 static const WCHAR index_url[] =
327 {'f','i','l','e',':','i','n','d','e','x','.','h','t','m','l',0};
328 static const WCHAR index_url2[] =
329 {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
330 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
331 static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
332 static const char html_doc[] = "<HTML></HTML>";
334 file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
335 FILE_ATTRIBUTE_NORMAL, NULL);
336 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
337 if(file == INVALID_HANDLE_VALUE)
338 return;
339 WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
340 CloseHandle(file);
342 file_name = wszIndexHtml;
343 test_file_protocol_url(index_url);
345 memcpy(buf, wszFile, sizeof(wszFile));
346 len = sizeof(wszFile)/sizeof(WCHAR)-1;
347 len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
348 buf[len++] = '\\';
349 memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
351 file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
352 test_file_protocol_url(buf);
354 DeleteFileW(wszIndexHtml);
356 hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
357 &IID_IInternetProtocol, (void**)&protocol);
358 ok(hres == S_OK, "CoCreateInstance failed: %08lx\n", hres);
359 if(FAILED(hres))
360 return;
362 SET_EXPECT(GetBindInfo);
363 expect_hrResult = MK_E_SYNTAX;
364 hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
365 ok(hres == MK_E_SYNTAX, "Start failed: %08lx, expected MK_E_SYNTAX\n", hres);
366 CHECK_CALLED(GetBindInfo);
368 SET_EXPECT(GetBindInfo);
369 SET_EXPECT(ReportProgress_DIRECTBIND);
370 SET_EXPECT(ReportResult);
371 expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
372 hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
373 ok(hres == INET_E_RESOURCE_NOT_FOUND,
374 "Start failed: %08lx expected INET_E_RESOURCE_NOT_FOUND\n", hres);
375 CHECK_CALLED(GetBindInfo);
376 CHECK_CALLED(ReportProgress_DIRECTBIND);
377 CHECK_CALLED(ReportResult);
379 IInternetProtocol_Release(protocol);
381 hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
382 &IID_IInternetProtocol, (void**)&protocol);
383 ok(hres == S_OK, "CoCreateInstance failed: %08lx\n", hres);
384 if(FAILED(hres))
385 return;
387 SET_EXPECT(GetBindInfo);
388 SET_EXPECT(ReportProgress_DIRECTBIND);
389 SET_EXPECT(ReportResult);
390 expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
391 hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
392 ok(hres == INET_E_RESOURCE_NOT_FOUND,
393 "Start failed: %08lx, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
394 CHECK_CALLED(GetBindInfo);
395 CHECK_CALLED(ReportProgress_DIRECTBIND);
396 CHECK_CALLED(ReportResult);
398 IInternetProtocol_Release(protocol);
401 START_TEST(protocol)
403 OleInitialize(NULL);
405 test_file_protocol();
407 OleUninitialize();