2 * Copyright 2006 Jacek Caban for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <wine/test.h>
32 #define DEFINE_EXPECT(func) \
33 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
35 #define SET_EXPECT(func) \
36 expect_ ## func = TRUE
38 #define CHECK_EXPECT(func) \
40 ok(expect_ ##func, "unexpected call " #func "\n"); \
41 expect_ ## func = FALSE; \
42 called_ ## func = TRUE; \
45 #define CHECK_EXPECT2(func) \
47 ok(expect_ ##func, "unexpected call " #func "\n"); \
48 called_ ## func = TRUE; \
51 #define CHECK_CALLED(func) \
53 ok(called_ ## func, "expected " #func "\n"); \
54 expect_ ## func = called_ ## func = FALSE; \
57 DEFINE_GUID(CLSID_ITSProtocol,0x9d148291,0xb9c8,0x11d0,0xa4,0xcc,0x00,0x00,0xf8,0x01,0x49,0xf6);
59 DEFINE_EXPECT(GetBindInfo);
60 DEFINE_EXPECT(ReportProgress_BEGINDOWNLOADDATA);
61 DEFINE_EXPECT(ReportProgress_SENDINGREQUEST);
62 DEFINE_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
63 DEFINE_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
64 DEFINE_EXPECT(ReportProgress_DIRECTBIND);
65 DEFINE_EXPECT(ReportData);
66 DEFINE_EXPECT(ReportResult);
68 static HRESULT expect_hrResult;
69 static IInternetProtocol *read_protocol = NULL;
76 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
78 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
85 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
90 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
95 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
97 ok(0, "unexpected call\n");
101 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
102 LPCWSTR szStatusText)
104 static const WCHAR blank_html[] = {'b','l','a','n','k','.','h','t','m','l',0};
105 static const WCHAR text_html[] = {'t','e','x','t','/','h','t','m','l',0};
106 static const WCHAR cache_file[] =
107 {'t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
109 switch(ulStatusCode) {
110 case BINDSTATUS_BEGINDOWNLOADDATA:
111 CHECK_EXPECT(ReportProgress_BEGINDOWNLOADDATA);
112 ok(!szStatusText, "szStatusText != NULL\n");
114 case BINDSTATUS_SENDINGREQUEST:
115 CHECK_EXPECT(ReportProgress_SENDINGREQUEST);
116 if(test_protocol == ITS_PROTOCOL)
117 ok(!lstrcmpW(szStatusText, blank_html), "unexpected szStatusText\n");
119 ok(szStatusText == NULL, "szStatusText != NULL\n");
121 case BINDSTATUS_MIMETYPEAVAILABLE:
122 CHECK_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
123 ok(!lstrcmpW(szStatusText, text_html), "unexpected szStatusText\n");
125 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
126 CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
127 ok(!lstrcmpW(szStatusText, cache_file), "unexpected szStatusText\n");
129 case BINDSTATUS_DIRECTBIND:
130 CHECK_EXPECT(ReportProgress_DIRECTBIND);
131 ok(!szStatusText, "szStatusText != NULL\n");
134 ok(0, "unexpected ulStatusCode %d\n", ulStatusCode);
141 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF, ULONG ulProgress,
144 CHECK_EXPECT(ReportData);
146 ok(ulProgress == ulProgressMax, "ulProgress != ulProgressMax\n");
147 if(test_protocol == ITS_PROTOCOL)
148 ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE), "grcf = %08x\n", grfBSCF);
150 ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION), "grcf = %08x\n", grfBSCF);
154 DWORD cb = 0xdeadbeef;
157 hres = IInternetProtocol_Read(read_protocol, buf, sizeof(buf), &cb);
158 ok(hres == S_OK, "Read failed: %08x\n", hres);
159 ok(cb == 13, "cb=%u expected 13\n", cb);
160 ok(!memcmp(buf, "<html></html>", 13), "unexpected data\n");
166 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
167 DWORD dwError, LPCWSTR szResult)
169 CHECK_EXPECT(ReportResult);
171 ok(hrResult == expect_hrResult, "expected: %08x got: %08x\n", expect_hrResult, hrResult);
172 ok(dwError == 0, "dwError = %d\n", dwError);
173 ok(!szResult, "szResult != NULL\n");
178 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
179 ProtocolSink_QueryInterface,
181 ProtocolSink_Release,
183 ProtocolSink_ReportProgress,
184 ProtocolSink_ReportData,
185 ProtocolSink_ReportResult
188 static IInternetProtocolSink protocol_sink = {
192 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
194 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
198 return E_NOINTERFACE;
201 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
206 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
211 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
213 CHECK_EXPECT(GetBindInfo);
215 ok(grfBINDF != NULL, "grfBINDF == NULL\n");
217 ok(!*grfBINDF, "*grfBINDF != 0\n");
218 ok(pbindinfo != NULL, "pbindinfo == NULL\n");
219 ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
224 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType, LPOLESTR *ppwzStr,
225 ULONG cEl, ULONG *pcElFetched)
227 ok(0, "unexpected call\n");
231 static IInternetBindInfoVtbl bind_info_vtbl = {
232 BindInfo_QueryInterface,
235 BindInfo_GetBindInfo,
236 BindInfo_GetBindString
239 static IInternetBindInfo bind_info = {
243 static void test_protocol_fail(IInternetProtocol *protocol, LPCWSTR url, HRESULT expected_hres)
247 SET_EXPECT(GetBindInfo);
248 SET_EXPECT(ReportResult);
250 expect_hrResult = expected_hres;
251 hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
252 ok(hres == expected_hres, "expected: %08x got: %08x\n", expected_hres, hres);
254 CHECK_CALLED(GetBindInfo);
255 CHECK_CALLED(ReportResult);
258 static void protocol_start(IInternetProtocol *protocol, LPCWSTR url)
262 SET_EXPECT(GetBindInfo);
263 if(test_protocol == MK_PROTOCOL)
264 SET_EXPECT(ReportProgress_DIRECTBIND);
265 SET_EXPECT(ReportProgress_SENDINGREQUEST);
266 SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
267 if(test_protocol == MK_PROTOCOL)
268 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
269 SET_EXPECT(ReportData);
270 if(test_protocol == ITS_PROTOCOL)
271 SET_EXPECT(ReportProgress_BEGINDOWNLOADDATA);
272 SET_EXPECT(ReportResult);
273 expect_hrResult = S_OK;
275 hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
276 ok(hres == S_OK, "Start failed: %08x\n", hres);
278 CHECK_CALLED(GetBindInfo);
279 if(test_protocol == MK_PROTOCOL)
280 CHECK_CALLED(ReportProgress_DIRECTBIND);
281 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
282 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
283 if(test_protocol == MK_PROTOCOL)
284 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
285 CHECK_CALLED(ReportData);
286 if(test_protocol == ITS_PROTOCOL)
287 CHECK_CALLED(ReportProgress_BEGINDOWNLOADDATA);
288 CHECK_CALLED(ReportResult);
291 static void test_protocol_url(IClassFactory *factory, LPCWSTR url)
293 IInternetProtocol *protocol;
298 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
299 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
303 protocol_start(protocol, url);
304 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
305 ok(hres == S_OK, "Read failed: %08x\n", hres);
306 ok(cb == 13, "cb=%u expected 13\n", cb);
307 ok(!memcmp(buf, "<html></html>", 13), "unexpected data\n");
308 ref = IInternetProtocol_Release(protocol);
309 ok(!ref, "protocol ref=%d\n", ref);
311 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
312 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
317 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
318 ok(hres == (test_protocol == ITS_PROTOCOL ? INET_E_DATA_NOT_AVAILABLE : E_FAIL),
319 "Read returned %08x\n", hres);
320 ok(cb == 0xdeadbeef, "cb=%u expected 0xdeadbeef\n", cb);
322 protocol_start(protocol, url);
323 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
324 ok(hres == S_OK, "Read failed: %08x\n", hres);
325 ok(cb == 2, "cb=%u expected 2\n", cb);
326 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
327 ok(hres == S_OK, "Read failed: %08x\n", hres);
328 ok(cb == 11, "cb=%u, expected 11\n", cb);
329 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
330 ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
331 ok(cb == 0, "cb=%u expected 0\n", cb);
332 hres = IInternetProtocol_UnlockRequest(protocol);
333 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
334 ref = IInternetProtocol_Release(protocol);
335 ok(!ref, "protocol ref=%d\n", ref);
337 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
338 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
342 protocol_start(protocol, url);
343 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
344 ok(hres == S_OK, "Read failed: %08x\n", hres);
345 hres = IInternetProtocol_LockRequest(protocol, 0);
346 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
347 hres = IInternetProtocol_UnlockRequest(protocol);
348 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
349 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
350 ok(hres == S_OK, "Read failed: %08x\n", hres);
351 ok(cb == 11, "cb=%u, expected 11\n", cb);
352 ref = IInternetProtocol_Release(protocol);
353 ok(!ref, "protocol ref=%d\n", ref);
355 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
356 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
360 protocol_start(protocol, url);
361 hres = IInternetProtocol_LockRequest(protocol, 0);
362 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
363 hres = IInternetProtocol_Terminate(protocol, 0);
364 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
365 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
366 ok(hres == S_OK, "Read failed: %08x\n", hres);
367 ok(cb == 2, "cb=%u, expected 2\n", cb);
368 hres = IInternetProtocol_UnlockRequest(protocol);
369 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
370 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
371 ok(hres == S_OK, "Read failed: %08x\n", hres);
372 ok(cb == 2, "cb=%u, expected 2\n", cb);
373 hres = IInternetProtocol_Terminate(protocol, 0);
374 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
375 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
376 ok(hres == S_OK, "Read failed: %08x\n", hres);
377 ok(cb == 2, "cb=%u expected 2\n", cb);
378 ref = IInternetProtocol_Release(protocol);
379 ok(!ref, "protocol ref=%d\n", ref);
381 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&read_protocol);
382 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
386 protocol_start(read_protocol, url);
387 ref = IInternetProtocol_Release(read_protocol);
388 ok(!ref, "protocol ref=%d\n", ref);
389 read_protocol = NULL;
392 static void test_its_protocol(void)
394 IClassFactory *factory;
399 static const WCHAR blank_url1[] = {'i','t','s',':',
400 't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
401 static const WCHAR blank_url2[] = {'m','S','-','i','T','s',':',
402 't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
403 static const WCHAR blank_url3[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
404 't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
405 static const WCHAR blank_url4[] = {'i','t','s',':',
406 't','e','s','t','.','c','h','m',':',':','b','l','a','n','k','.','h','t','m','l',0};
407 static const WCHAR wrong_url1[] =
408 {'i','t','s',':','t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','.','h','t','m','l',0};
409 static const WCHAR wrong_url2[] =
410 {'i','t','s',':','t','e','s','.','c','h','m',':',':','b','/','l','a','n','k','.','h','t','m','l',0};
411 static const WCHAR wrong_url3[] =
412 {'i','t','s',':','t','e','s','t','.','c','h','m','/','b','l','a','n','k','.','h','t','m','l',0};
413 static const WCHAR wrong_url4[] = {'m','k',':','@','M','S','I','T','S','t','o','r',':',
414 't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
415 static const WCHAR wrong_url5[] = {'f','i','l','e',':',
416 't','e','s','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
418 test_protocol = ITS_PROTOCOL;
420 hres = CoGetClassObject(&CLSID_ITSProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
421 ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
425 hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
426 ok(hres == S_OK, "Could not get IClassFactory interface\n");
427 if(SUCCEEDED(hres)) {
428 IInternetProtocol *protocol;
430 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
431 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
432 if(SUCCEEDED(hres)) {
433 test_protocol_fail(protocol, wrong_url1, STG_E_FILENOTFOUND);
434 test_protocol_fail(protocol, wrong_url2, STG_E_FILENOTFOUND);
435 test_protocol_fail(protocol, wrong_url3, STG_E_FILENOTFOUND);
437 hres = IInternetProtocol_Start(protocol, wrong_url4, &protocol_sink, &bind_info, 0, 0);
438 ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER,
439 "Start failed: %08x, expected INET_E_USE_DEFAULT_PROTOCOLHANDLER\n", hres);
441 hres = IInternetProtocol_Start(protocol, wrong_url5, &protocol_sink, &bind_info, 0, 0);
442 ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER,
443 "Start failed: %08x, expected INET_E_USE_DEFAULT_PROTOCOLHANDLER\n", hres);
445 ref = IInternetProtocol_Release(protocol);
446 ok(!ref, "protocol ref=%d\n", ref);
448 test_protocol_url(factory, blank_url1);
449 test_protocol_url(factory, blank_url2);
450 test_protocol_url(factory, blank_url3);
451 test_protocol_url(factory, blank_url4);
454 IClassFactory_Release(factory);
457 IUnknown_Release(unk);
460 static void test_mk_protocol(void)
465 static const WCHAR blank_url[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
466 't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
468 test_protocol = MK_PROTOCOL;
470 hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory,
472 ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
476 test_protocol_url(cf, blank_url);
478 IClassFactory_Release(cf);
481 static BOOL create_chm(void)
487 file = CreateFileA("test.chm", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
488 FILE_ATTRIBUTE_NORMAL, NULL);
489 ok(file != INVALID_HANDLE_VALUE, "Could not create test.chm file\n");
490 if(file == INVALID_HANDLE_VALUE)
493 src = FindResourceA(NULL, MAKEINTRESOURCEA(60), MAKEINTRESOURCEA(60));
495 WriteFile(file, LoadResource(NULL, src), SizeofResource(NULL, src), &size, NULL);
501 static void delete_chm(void)
505 ret = DeleteFileA("test.chm");
506 ok(ret, "DeleteFileA failed: %d\n", GetLastError());