mmdevapi/tests: Add tests for volume control interfaces.
[wine] / dlls / urlmon / tests / protocol.c
1 /*
2  * Copyright 2005-2011 Jacek Caban for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "ole2.h"
29 #include "urlmon.h"
30 #include "wininet.h"
31
32 static HRESULT (WINAPI *pCoInternetGetSession)(DWORD, IInternetSession **, DWORD);
33 static HRESULT (WINAPI *pReleaseBindInfo)(BINDINFO*);
34 static HRESULT (WINAPI *pCreateUri)(LPCWSTR, DWORD, DWORD_PTR, IUri**);
35
36 #define DEFINE_EXPECT(func) \
37     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
38
39 #define SET_EXPECT(func) \
40     expect_ ## func = TRUE
41
42 #define CHECK_EXPECT2(func) \
43     do { \
44         ok(expect_ ##func, "unexpected call " #func  "\n"); \
45         called_ ## func = TRUE; \
46     }while(0)
47
48 #define CHECK_EXPECT(func) \
49     do { \
50         CHECK_EXPECT2(func);     \
51         expect_ ## func = FALSE; \
52     }while(0)
53
54 #define CHECK_CALLED(func) \
55     do { \
56         ok(called_ ## func, "expected " #func "\n"); \
57         expect_ ## func = called_ ## func = FALSE; \
58     }while(0)
59
60 #define CHECK_NOT_CALLED(func) \
61     do { \
62         ok(!called_ ## func, "unexpected " #func "\n"); \
63         expect_ ## func = called_ ## func = FALSE; \
64     }while(0)
65
66 #define CLEAR_CALLED(func) \
67     expect_ ## func = called_ ## func = FALSE
68
69 DEFINE_EXPECT(GetBindInfo);
70 DEFINE_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
71 DEFINE_EXPECT(ReportProgress_DIRECTBIND);
72 DEFINE_EXPECT(ReportProgress_RAWMIMETYPE);
73 DEFINE_EXPECT(ReportProgress_FINDINGRESOURCE);
74 DEFINE_EXPECT(ReportProgress_CONNECTING);
75 DEFINE_EXPECT(ReportProgress_SENDINGREQUEST);
76 DEFINE_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
77 DEFINE_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
78 DEFINE_EXPECT(ReportProgress_PROTOCOLCLASSID);
79 DEFINE_EXPECT(ReportProgress_COOKIE_SENT);
80 DEFINE_EXPECT(ReportProgress_REDIRECTING);
81 DEFINE_EXPECT(ReportProgress_ENCODING);
82 DEFINE_EXPECT(ReportProgress_ACCEPTRANGES);
83 DEFINE_EXPECT(ReportProgress_PROXYDETECTING);
84 DEFINE_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
85 DEFINE_EXPECT(ReportProgress_DECODING);
86 DEFINE_EXPECT(ReportData);
87 DEFINE_EXPECT(ReportData2);
88 DEFINE_EXPECT(ReportResult);
89 DEFINE_EXPECT(GetBindString_ACCEPT_MIMES);
90 DEFINE_EXPECT(GetBindString_USER_AGENT);
91 DEFINE_EXPECT(GetBindString_POST_COOKIE);
92 DEFINE_EXPECT(GetBindString_URL);
93 DEFINE_EXPECT(QueryService_HttpNegotiate);
94 DEFINE_EXPECT(QueryService_InternetProtocol);
95 DEFINE_EXPECT(QueryService_HttpSecurity);
96 DEFINE_EXPECT(QueryInterface_IWinInetInfo);
97 DEFINE_EXPECT(QueryInterface_IWinInetHttpInfo);
98 DEFINE_EXPECT(BeginningTransaction);
99 DEFINE_EXPECT(GetRootSecurityId);
100 DEFINE_EXPECT(OnResponse);
101 DEFINE_EXPECT(Switch);
102 DEFINE_EXPECT(Continue);
103 DEFINE_EXPECT(CreateInstance);
104 DEFINE_EXPECT(Start);
105 DEFINE_EXPECT(StartEx);
106 DEFINE_EXPECT(Terminate);
107 DEFINE_EXPECT(Read);
108 DEFINE_EXPECT(Read2);
109 DEFINE_EXPECT(SetPriority);
110 DEFINE_EXPECT(LockRequest);
111 DEFINE_EXPECT(UnlockRequest);
112 DEFINE_EXPECT(Abort);
113 DEFINE_EXPECT(MimeFilter_CreateInstance);
114 DEFINE_EXPECT(MimeFilter_Start);
115 DEFINE_EXPECT(MimeFilter_ReportProgress);
116 DEFINE_EXPECT(MimeFilter_ReportData);
117 DEFINE_EXPECT(MimeFilter_ReportResult);
118 DEFINE_EXPECT(MimeFilter_Terminate);
119 DEFINE_EXPECT(MimeFilter_LockRequest);
120 DEFINE_EXPECT(MimeFilter_UnlockRequest);
121 DEFINE_EXPECT(MimeFilter_Read);
122 DEFINE_EXPECT(MimeFilter_Switch);
123 DEFINE_EXPECT(MimeFilter_Continue);
124 DEFINE_EXPECT(Stream_Seek);
125 DEFINE_EXPECT(Stream_Read);
126
127 static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
128 static const WCHAR index_url[] =
129     {'f','i','l','e',':','i','n','d','e','x','.','h','t','m','l',0};
130
131 static const WCHAR acc_mimeW[] = {'*','/','*',0};
132 static const WCHAR user_agentW[] = {'W','i','n','e',0};
133 static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
134 static const WCHAR hostW[] = {'w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
135 static const WCHAR winehq_ipW[] = {'2','0','9','.','4','6','.','2','5','.','1','3','4',0};
136 static const WCHAR emptyW[] = {0};
137 static const WCHAR gzipW[] = {'g','z','i','p',0};
138
139 static HRESULT expect_hrResult;
140 static LPCWSTR file_name, http_url, expect_wsz;
141 static IInternetProtocol *async_protocol = NULL;
142 static BOOL first_data_notif, http_is_first, test_redirect;
143 static int prot_state, read_report_data, post_stream_read;
144 static DWORD bindf, ex_priority , pi;
145 static IInternetProtocol *binding_protocol, *filtered_protocol;
146 static IInternetBindInfo *prot_bind_info;
147 static IInternetProtocolSink *binding_sink, *filtered_sink;
148 static void *expect_pv;
149 static HANDLE event_complete, event_complete2, event_continue, event_continue_done;
150 static BOOL binding_test;
151 static PROTOCOLDATA protocoldata, *pdata, continue_protdata;
152 static DWORD prot_read, pi, filter_state, http_post_test, thread_id;
153 static BOOL security_problem, test_async_req, impl_protex;
154 static BOOL async_read_pending, mimefilter_test, direct_read, wait_for_switch, emulate_prot, short_read, test_abort;
155
156 enum {
157     STATE_CONNECTING,
158     STATE_SENDINGREQUEST,
159     STATE_STARTDOWNLOADING,
160     STATE_DOWNLOADING
161 } state;
162
163 static enum {
164     FILE_TEST,
165     HTTP_TEST,
166     HTTPS_TEST,
167     FTP_TEST,
168     MK_TEST,
169     ITS_TEST,
170     BIND_TEST
171 } tested_protocol;
172
173 static const WCHAR protocol_names[][10] = {
174     {'f','i','l','e',0},
175     {'h','t','t','p',0},
176     {'h','t','t','p','s',0},
177     {'f','t','p',0},
178     {'m','k',0},
179     {'i','t','s',0},
180     {'t','e','s','t',0}
181 };
182
183 static const WCHAR binding_urls[][130] = {
184     {'f','i','l','e',':','t','e','s','t','.','h','t','m','l',0},
185     {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
186      'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0},
187     {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
188      '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0},
189     {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
190      '/','p','u','b','/','o','t','h','e','r',
191      '/','w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0},
192     {'m','k',':','t','e','s','t',0},
193     {'i','t','s',':','t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0},
194     {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0}
195 };
196
197 static const CHAR post_data[] = "mode=Test";
198
199 static const char *debugstr_guid(REFIID riid)
200 {
201     static char buf[50];
202
203     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
204             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
205             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
206             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
207
208     return buf;
209 }
210
211 static int strcmp_wa(LPCWSTR strw, const char *stra)
212 {
213     CHAR buf[512];
214     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
215     return lstrcmpA(stra, buf);
216 }
217
218 static HRESULT WINAPI HttpSecurity_QueryInterface(IHttpSecurity *iface, REFIID riid, void **ppv)
219 {
220     if(IsEqualGUID(&IID_IUnknown, riid)
221             || IsEqualGUID(&IID_IHttpSecurity, riid)) {
222         *ppv = iface;
223         return S_OK;
224     }
225
226     ok(0, "unexpected call\n");
227     return E_NOINTERFACE;
228 }
229
230 static ULONG WINAPI HttpSecurity_AddRef(IHttpSecurity *iface)
231 {
232     return 2;
233 }
234
235 static ULONG WINAPI HttpSecurity_Release(IHttpSecurity *iface)
236 {
237     return 1;
238 }
239
240 static  HRESULT WINAPI HttpSecurity_GetWindow(IHttpSecurity* iface, REFGUID rguidReason, HWND *phwnd)
241 {
242     trace("HttpSecurity_GetWindow\n");
243
244     return S_FALSE;
245 }
246
247 static HRESULT WINAPI HttpSecurity_OnSecurityProblem(IHttpSecurity *iface, DWORD dwProblem)
248 {
249     trace("Security problem: %u\n", dwProblem);
250     ok(dwProblem == ERROR_INTERNET_SEC_CERT_REV_FAILED, "Expected ERROR_INTERNET_SEC_CERT_REV_FAILED got %u\n", dwProblem);
251
252     /* Only retry once */
253     if (security_problem)
254         return E_ABORT;
255
256     security_problem = TRUE;
257     SET_EXPECT(BeginningTransaction);
258
259     return RPC_E_RETRY;
260 }
261
262 static IHttpSecurityVtbl HttpSecurityVtbl = {
263     HttpSecurity_QueryInterface,
264     HttpSecurity_AddRef,
265     HttpSecurity_Release,
266     HttpSecurity_GetWindow,
267     HttpSecurity_OnSecurityProblem
268 };
269
270 static IHttpSecurity http_security = { &HttpSecurityVtbl };
271
272 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface, REFIID riid, void **ppv)
273 {
274     if(IsEqualGUID(&IID_IUnknown, riid)
275             || IsEqualGUID(&IID_IHttpNegotiate, riid)
276             || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
277         *ppv = iface;
278         return S_OK;
279     }
280
281     ok(0, "unexpected call\n");
282     return E_NOINTERFACE;
283 }
284
285 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
286 {
287     return 2;
288 }
289
290 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
291 {
292     return 1;
293 }
294
295 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface, LPCWSTR szURL,
296         LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
297 {
298     LPWSTR addl_headers;
299
300     static const WCHAR wszHeaders[] =
301         {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ','a','p','p','l','i','c','a','t',
302          'i','o','n','/','x','-','w','w','w','-','f','o','r','m','-','u','r','l','e','n','c','o',
303          'd','e','d','\r','\n',0};
304
305     CHECK_EXPECT(BeginningTransaction);
306
307     if(binding_test)
308         ok(!lstrcmpW(szURL, binding_urls[tested_protocol]), "szURL != http_url\n");
309     else
310         ok(!lstrcmpW(szURL, http_url), "szURL != http_url\n");
311     ok(!dwReserved, "dwReserved=%d, expected 0\n", dwReserved);
312     ok(pszAdditionalHeaders != NULL, "pszAdditionalHeaders == NULL\n");
313     if(pszAdditionalHeaders)
314     {
315         ok(*pszAdditionalHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
316         if (http_post_test)
317         {
318             addl_headers = CoTaskMemAlloc(sizeof(wszHeaders));
319             memcpy(addl_headers, wszHeaders, sizeof(wszHeaders));
320             *pszAdditionalHeaders = addl_headers;
321         }
322     }
323
324     return S_OK;
325 }
326
327 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
328         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
329 {
330     CHECK_EXPECT(OnResponse);
331
332     ok(dwResponseCode == 200, "dwResponseCode=%d, expected 200\n", dwResponseCode);
333     ok(szResponseHeaders != NULL, "szResponseHeaders == NULL\n");
334     ok(szRequestHeaders == NULL, "szRequestHeaders != NULL\n");
335     ok(pszAdditionalRequestHeaders == NULL, "pszAdditionalHeaders != NULL\n");
336
337     return S_OK;
338 }
339
340 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
341         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
342 {
343     static const BYTE sec_id[] = {'h','t','t','p',':','t','e','s','t',1,0,0,0};
344     
345     CHECK_EXPECT(GetRootSecurityId);
346
347     ok(!dwReserved, "dwReserved=%ld, expected 0\n", dwReserved);
348     ok(pbSecurityId != NULL, "pbSecurityId == NULL\n");
349     ok(pcbSecurityId != NULL, "pcbSecurityId == NULL\n");
350
351     if(pcbSecurityId) {
352         ok(*pcbSecurityId == 512, "*pcbSecurityId=%d, expected 512\n", *pcbSecurityId);
353         *pcbSecurityId = sizeof(sec_id);
354     }
355
356     if(pbSecurityId)
357         memcpy(pbSecurityId, sec_id, sizeof(sec_id));
358
359     return E_FAIL;
360 }
361
362 static IHttpNegotiate2Vtbl HttpNegotiateVtbl = {
363     HttpNegotiate_QueryInterface,
364     HttpNegotiate_AddRef,
365     HttpNegotiate_Release,
366     HttpNegotiate_BeginningTransaction,
367     HttpNegotiate_OnResponse,
368     HttpNegotiate_GetRootSecurityId
369 };
370
371 static IHttpNegotiate2 http_negotiate = { &HttpNegotiateVtbl };
372
373 static HRESULT QueryInterface(REFIID,void**);
374
375 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
376 {
377     return QueryInterface(riid, ppv);
378 }
379
380 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
381 {
382     return 2;
383 }
384
385 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
386 {
387     return 1;
388 }
389
390 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
391         REFIID riid, void **ppv)
392 {
393     if(IsEqualGUID(&IID_IHttpNegotiate, guidService) || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
394         CHECK_EXPECT2(QueryService_HttpNegotiate);
395         return IHttpNegotiate2_QueryInterface(&http_negotiate, riid, ppv);
396     }
397
398     if(IsEqualGUID(&IID_IInternetProtocol, guidService)) {
399         ok(IsEqualGUID(&IID_IInternetProtocol, riid), "unexpected riid\n");
400         CHECK_EXPECT(QueryService_InternetProtocol);
401         return E_NOINTERFACE;
402     }
403
404     if(IsEqualGUID(&IID_IHttpSecurity, guidService)) {
405         ok(IsEqualGUID(&IID_IHttpSecurity, riid), "unexpected riid\n");
406         CHECK_EXPECT(QueryService_HttpSecurity);
407         return IHttpSecurity_QueryInterface(&http_security, riid, ppv);
408     }
409
410     ok(0, "unexpected service %s\n", debugstr_guid(guidService));
411     return E_FAIL;
412 }
413
414 static const IServiceProviderVtbl ServiceProviderVtbl = {
415     ServiceProvider_QueryInterface,
416     ServiceProvider_AddRef,
417     ServiceProvider_Release,
418     ServiceProvider_QueryService
419 };
420
421 static IServiceProvider service_provider = { &ServiceProviderVtbl };
422
423 static HRESULT WINAPI Stream_QueryInterface(IStream *iface, REFIID riid, void **ppv)
424 {
425     ok(0, "unexpected call\n");
426     return E_NOINTERFACE;
427 }
428
429 static ULONG WINAPI Stream_AddRef(IStream *iface)
430 {
431     return 2;
432 }
433
434 static ULONG WINAPI Stream_Release(IStream *iface)
435 {
436     return 1;
437 }
438
439 static HRESULT WINAPI Stream_Read(IStream *iface, void *pv,
440         ULONG cb, ULONG *pcbRead)
441 {
442     CHECK_EXPECT2(Stream_Read);
443
444     ok(GetCurrentThreadId() != thread_id, "wrong thread %d\n", GetCurrentThreadId());
445
446     ok(pv != NULL, "pv == NULL\n");
447     ok(cb == 0x20000 || broken(cb == 0x2000), "cb = %d\n", cb);
448     ok(pcbRead != NULL, "pcbRead == NULL\n");
449
450     if(post_stream_read) {
451         *pcbRead = 0;
452         return S_FALSE;
453     }
454
455     memcpy(pv, post_data, sizeof(post_data)-1);
456     post_stream_read += *pcbRead = sizeof(post_data)-1;
457     return S_OK;
458 }
459
460 static HRESULT WINAPI Stream_Write(IStream *iface, const void *pv,
461         ULONG cb, ULONG *pcbWritten)
462 {
463     ok(0, "unexpected call\n");
464     return E_NOTIMPL;
465 }
466
467 static HRESULT WINAPI Stream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
468         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
469 {
470     CHECK_EXPECT(Stream_Seek);
471
472     ok(!dlibMove.QuadPart, "dlibMove != 0\n");
473     ok(dwOrigin == STREAM_SEEK_SET, "dwOrigin = %d\n", dwOrigin);
474     ok(!plibNewPosition, "plibNewPosition == NULL\n");
475
476     return S_OK;
477 }
478
479 static HRESULT WINAPI Stream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
480 {
481     ok(0, "unexpected call\n");
482     return E_NOTIMPL;
483 }
484
485 static HRESULT WINAPI Stream_CopyTo(IStream *iface, IStream *pstm,
486         ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
487 {
488     ok(0, "unexpected call\n");
489     return E_NOTIMPL;
490 }
491
492 static HRESULT WINAPI Stream_Commit(IStream *iface, DWORD grfCommitFlags)
493 {
494     ok(0, "unexpected call\n");
495     return E_NOTIMPL;
496 }
497
498 static HRESULT WINAPI Stream_Revert(IStream *iface)
499 {
500     ok(0, "unexpected call\n");
501     return E_NOTIMPL;
502 }
503
504 static HRESULT WINAPI Stream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
505         ULARGE_INTEGER cb, DWORD dwLockType)
506 {
507     ok(0, "unexpected call\n");
508     return E_NOTIMPL;
509 }
510
511 static HRESULT WINAPI Stream_UnlockRegion(IStream *iface,
512         ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
513 {
514     ok(0, "unexpected call\n");
515     return E_NOTIMPL;
516 }
517
518 static HRESULT WINAPI Stream_Stat(IStream *iface, STATSTG *pstatstg,
519         DWORD dwStatFlag)
520 {
521     ok(0, "unexpected call\n");
522     return E_NOTIMPL;
523 }
524
525 static HRESULT WINAPI Stream_Clone(IStream *iface, IStream **ppstm)
526 {
527     ok(0, "unexpected call\n");
528     return E_NOTIMPL;
529 }
530
531 static const IStreamVtbl StreamVtbl = {
532     Stream_QueryInterface,
533     Stream_AddRef,
534     Stream_Release,
535     Stream_Read,
536     Stream_Write,
537     Stream_Seek,
538     Stream_SetSize,
539     Stream_CopyTo,
540     Stream_Commit,
541     Stream_Revert,
542     Stream_LockRegion,
543     Stream_UnlockRegion,
544     Stream_Stat,
545     Stream_Clone
546 };
547
548 static IStream Stream = { &StreamVtbl };
549
550 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
551 {
552     return QueryInterface(riid, ppv);
553 }
554
555 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
556 {
557     return 2;
558 }
559
560 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
561 {
562     return 1;
563 }
564
565 static void call_continue(PROTOCOLDATA *protocol_data)
566 {
567     HRESULT hres;
568
569     if(state == STATE_CONNECTING) {
570         if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST)
571             CLEAR_CALLED(ReportProgress_COOKIE_SENT);
572         if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST || tested_protocol == FTP_TEST) {
573             if (http_is_first) {
574                 CLEAR_CALLED(ReportProgress_FINDINGRESOURCE);
575                 CLEAR_CALLED(ReportProgress_CONNECTING);
576                 CLEAR_CALLED(ReportProgress_PROXYDETECTING);
577             }else todo_wine {
578                 CHECK_NOT_CALLED(ReportProgress_FINDINGRESOURCE);
579                 /* IE7 does call this */
580                 CLEAR_CALLED(ReportProgress_CONNECTING);
581             }
582         }
583         if(tested_protocol == FTP_TEST)
584             todo_wine CHECK_CALLED(ReportProgress_SENDINGREQUEST);
585         else if (tested_protocol != HTTPS_TEST)
586             CHECK_CALLED(ReportProgress_SENDINGREQUEST);
587         if(test_redirect)
588             CHECK_CALLED(ReportProgress_REDIRECTING);
589         state = test_async_req ? STATE_SENDINGREQUEST : STATE_STARTDOWNLOADING;
590     }
591
592     switch(state) {
593     case STATE_SENDINGREQUEST:
594         SET_EXPECT(Stream_Read);
595         SET_EXPECT(ReportProgress_SENDINGREQUEST);
596         break;
597     case STATE_STARTDOWNLOADING:
598         if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
599             SET_EXPECT(OnResponse);
600             if(tested_protocol == HTTPS_TEST || test_redirect || test_abort)
601                 SET_EXPECT(ReportProgress_ACCEPTRANGES);
602             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
603             if(bindf & BINDF_NEEDFILE)
604                 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
605         }
606     default:
607         break;
608     }
609
610     if(state != STATE_SENDINGREQUEST)
611         SET_EXPECT(ReportData);
612     hres = IInternetProtocol_Continue(async_protocol, protocol_data);
613     ok(hres == S_OK, "Continue failed: %08x\n", hres);
614     if(tested_protocol == FTP_TEST || security_problem)
615         CLEAR_CALLED(ReportData);
616     else if(state != STATE_SENDINGREQUEST)
617         CHECK_CALLED(ReportData);
618
619     switch(state) {
620     case STATE_SENDINGREQUEST:
621         CHECK_CALLED(Stream_Read);
622         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
623         state = STATE_STARTDOWNLOADING;
624         break;
625     case STATE_STARTDOWNLOADING:
626         if (! security_problem)
627         {
628             state = STATE_DOWNLOADING;
629             if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
630                 CHECK_CALLED(OnResponse);
631                 if(tested_protocol == HTTPS_TEST)
632                     CHECK_CALLED(ReportProgress_ACCEPTRANGES);
633                 else if(test_redirect)
634                     CLEAR_CALLED(ReportProgress_ACCEPTRANGES);
635                 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
636                 if(bindf & BINDF_NEEDFILE)
637                     CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
638             }
639         }
640         else
641         {
642             security_problem = FALSE;
643             SET_EXPECT(ReportProgress_CONNECTING);
644         }
645     default:
646         break;
647     }
648 }
649
650 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
651 {
652     if(tested_protocol == FTP_TEST)
653         CHECK_EXPECT2(Switch);
654     else
655         CHECK_EXPECT(Switch);
656
657     ok(pProtocolData != NULL, "pProtocolData == NULL\n");
658     if(binding_test) {
659         ok(pProtocolData != &protocoldata, "pProtocolData == &protocoldata\n");
660         ok(pProtocolData->grfFlags == protocoldata.grfFlags, "grfFlags wrong %x/%x\n",
661            pProtocolData->grfFlags, protocoldata.grfFlags );
662         ok(pProtocolData->dwState == protocoldata.dwState, "dwState wrong %x/%x\n",
663            pProtocolData->dwState, protocoldata.dwState );
664         ok(pProtocolData->pData == protocoldata.pData, "pData wrong %p/%p\n",
665            pProtocolData->pData, protocoldata.pData );
666         ok(pProtocolData->cbData == protocoldata.cbData, "cbData wrong %x/%x\n",
667            pProtocolData->cbData, protocoldata.cbData );
668     }
669
670     pdata = pProtocolData;
671
672     if(binding_test) {
673         SetEvent(event_complete);
674         WaitForSingleObject(event_complete2, INFINITE);
675         return S_OK;
676     }if(direct_read) {
677         continue_protdata = *pProtocolData;
678         SetEvent(event_continue);
679         WaitForSingleObject(event_continue_done, INFINITE);
680     }else {
681         call_continue(pProtocolData);
682         SetEvent(event_complete);
683     }
684
685     return S_OK;
686 }
687
688 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
689         LPCWSTR szStatusText)
690 {
691     static const WCHAR null_guid[] = {'{','0','0','0','0','0','0','0','0','-','0','0','0','0','-',
692         '0','0','0','0','-','0','0','0','0','-','0','0','0','0','0','0','0','0','0','0','0','0','}',0};
693     static const WCHAR text_plain[] = {'t','e','x','t','/','p','l','a','i','n',0};
694
695     switch(ulStatusCode) {
696     case BINDSTATUS_MIMETYPEAVAILABLE:
697         CHECK_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
698         if(tested_protocol != FILE_TEST && tested_protocol != ITS_TEST && !mimefilter_test && (pi & PI_MIMEVERIFICATION)) {
699             if(!short_read || !direct_read)
700                 CHECK_CALLED(Read); /* set in Continue */
701             else if(short_read)
702                 CHECK_CALLED(Read2); /* set in Read */
703         }
704         ok(szStatusText != NULL, "szStatusText == NULL\n");
705         if(szStatusText) {
706             if(tested_protocol == BIND_TEST)
707                 ok(szStatusText == expect_wsz, "unexpected szStatusText\n");
708             else if (http_post_test)
709                 ok(lstrlenW(text_plain) <= lstrlenW(szStatusText) &&
710                    !memcmp(szStatusText, text_plain, lstrlenW(text_plain)*sizeof(WCHAR)),
711                    "szStatusText != text/plain\n");
712             else if(!mimefilter_test)
713                 ok(lstrlenW(text_htmlW) <= lstrlenW(szStatusText) &&
714                    !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
715                    "szStatusText != text/html\n");
716         }
717         break;
718     case BINDSTATUS_DIRECTBIND:
719         CHECK_EXPECT2(ReportProgress_DIRECTBIND);
720         ok(szStatusText == NULL, "szStatusText != NULL\n");
721         break;
722     case BINDSTATUS_RAWMIMETYPE:
723         CHECK_EXPECT2(ReportProgress_RAWMIMETYPE);
724         ok(szStatusText != NULL, "szStatusText == NULL\n");
725         if(szStatusText)
726             ok(lstrlenW(szStatusText) < lstrlenW(text_htmlW) ||
727                !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
728                "szStatusText != text/html\n");
729         break;
730     case BINDSTATUS_CACHEFILENAMEAVAILABLE:
731         CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
732         ok(szStatusText != NULL, "szStatusText == NULL\n");
733         if(szStatusText) {
734             if(binding_test)
735                 ok(!lstrcmpW(szStatusText, expect_wsz), "unexpected szStatusText\n");
736             else if(tested_protocol == FILE_TEST)
737                 ok(!lstrcmpW(szStatusText, file_name), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
738             else
739                 ok(szStatusText != NULL, "szStatusText == NULL\n");
740         }
741         break;
742     case BINDSTATUS_FINDINGRESOURCE:
743         CHECK_EXPECT2(ReportProgress_FINDINGRESOURCE);
744         ok(szStatusText != NULL, "szStatusText == NULL\n");
745         break;
746     case BINDSTATUS_CONNECTING:
747         CHECK_EXPECT2(ReportProgress_CONNECTING);
748         ok(szStatusText != NULL, "szStatusText == NULL\n");
749         break;
750     case BINDSTATUS_SENDINGREQUEST:
751         CHECK_EXPECT2(ReportProgress_SENDINGREQUEST);
752         if(tested_protocol == FILE_TEST || tested_protocol == ITS_TEST) {
753             ok(szStatusText != NULL, "szStatusText == NULL\n");
754             if(szStatusText)
755                 ok(!*szStatusText, "wrong szStatusText\n");
756         }
757         break;
758     case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
759         CHECK_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
760         ok(szStatusText != NULL, "szStatusText == NULL\n");
761         if(szStatusText)
762             ok(!lstrcmpW(szStatusText, text_htmlW), "szStatusText != text/html\n");
763         break;
764     case BINDSTATUS_PROTOCOLCLASSID:
765         CHECK_EXPECT(ReportProgress_PROTOCOLCLASSID);
766         ok(szStatusText != NULL, "szStatusText == NULL\n");
767         ok(!lstrcmpW(szStatusText, null_guid), "unexpected classid %s\n", wine_dbgstr_w(szStatusText));
768         break;
769     case BINDSTATUS_COOKIE_SENT:
770         CHECK_EXPECT(ReportProgress_COOKIE_SENT);
771         ok(szStatusText == NULL, "szStatusText != NULL\n");
772         break;
773     case BINDSTATUS_REDIRECTING:
774         CHECK_EXPECT(ReportProgress_REDIRECTING);
775         if(test_redirect)
776             ok(!strcmp_wa(szStatusText, "http://test.winehq.org/tests/hello.html"), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
777         else
778             ok(szStatusText == NULL, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
779         break;
780     case BINDSTATUS_ENCODING:
781         CHECK_EXPECT(ReportProgress_ENCODING);
782         ok(!strcmp_wa(szStatusText, "gzip"), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
783         break;
784     case BINDSTATUS_ACCEPTRANGES:
785         CHECK_EXPECT(ReportProgress_ACCEPTRANGES);
786         ok(!szStatusText, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
787         break;
788     case BINDSTATUS_PROXYDETECTING:
789         CHECK_EXPECT(ReportProgress_PROXYDETECTING);
790         SET_EXPECT(ReportProgress_CONNECTING);
791         ok(!szStatusText, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
792         break;
793     case BINDSTATUS_LOADINGMIMEHANDLER:
794         CHECK_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
795         ok(!szStatusText, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
796         break;
797     case BINDSTATUS_DECODING:
798         CHECK_EXPECT(ReportProgress_DECODING);
799         ok(!lstrcmpW(szStatusText, gzipW), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
800         break;
801     default:
802         ok(0, "Unexpected status %d\n", ulStatusCode);
803     };
804
805     return S_OK;
806 }
807
808 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
809         ULONG ulProgress, ULONG ulProgressMax)
810 {
811     HRESULT hres;
812
813     static int rec_depth;
814     rec_depth++;
815
816     if(!mimefilter_test && (tested_protocol == FILE_TEST || tested_protocol == ITS_TEST)) {
817         CHECK_EXPECT2(ReportData);
818
819         ok(ulProgress == ulProgressMax, "ulProgress (%d) != ulProgressMax (%d)\n",
820            ulProgress, ulProgressMax);
821         ok(ulProgressMax == 13, "ulProgressMax=%d, expected 13\n", ulProgressMax);
822         /* BSCF_SKIPDRAINDATAFORFILEURLS added in IE8 */
823         if(tested_protocol == FILE_TEST)
824             ok((grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION)) ||
825                (grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_SKIPDRAINDATAFORFILEURLS)),
826                "grcfBSCF = %08x\n", grfBSCF);
827         else
828             ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE), "grcfBSCF = %08x\n", grfBSCF);
829     }else if(direct_read) {
830         BYTE buf[14096];
831         ULONG read;
832
833         if(!read_report_data && rec_depth == 1) {
834             BOOL reported_all_data = called_ReportData2;
835
836             CHECK_EXPECT2(ReportData);
837
838             if(short_read) {
839                 ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE)
840                    || grfBSCF == BSCF_FIRSTDATANOTIFICATION, /* < IE8 */
841                    "grcfBSCF = %08x\n", grfBSCF);
842                 CHECK_CALLED(Read); /* Set in Continue */
843                 first_data_notif = FALSE;
844             }else if(first_data_notif) {
845                 ok(grfBSCF == BSCF_FIRSTDATANOTIFICATION, "grcfBSCF = %08x\n", grfBSCF);
846                 first_data_notif = FALSE;
847             }else if(reported_all_data) {
848                 ok(grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION),
849                    "grcfBSCF = %08x\n", grfBSCF);
850             }else if(!direct_read) {
851                 ok(grfBSCF == BSCF_INTERMEDIATEDATANOTIFICATION, "grcfBSCF = %08x\n", grfBSCF);
852             }
853
854             do {
855                 read = 0;
856                 if(emulate_prot)
857                     SET_EXPECT(Read);
858                 else
859                     SET_EXPECT(ReportData2);
860                 SET_EXPECT(ReportResult);
861                 if(!emulate_prot)
862                     SET_EXPECT(Switch);
863                 hres = IInternetProtocol_Read(binding_test ? binding_protocol : async_protocol, expect_pv = buf, sizeof(buf), &read);
864                 ok(hres == E_PENDING || hres == S_FALSE || hres == S_OK, "Read failed: %08x\n", hres);
865                 if(hres == S_OK)
866                     ok(read, "read == 0\n");
867                 if(reported_all_data)
868                     ok(hres == S_FALSE, "Read failed: %08x, expected S_FALSE\n", hres);
869                 if(!emulate_prot && hres != E_PENDING)
870                     CHECK_NOT_CALLED(Switch); /* otherwise checked in wait_for_switch loop */
871                 if(emulate_prot)
872                     CHECK_CALLED(Read);
873                 if(!reported_all_data && called_ReportData2) {
874                     if(!emulate_prot)
875                         CHECK_CALLED(ReportData2);
876                     CHECK_CALLED(ReportResult);
877                     ok(hres == S_OK, "Read failed: %08x\n", hres);
878                     reported_all_data = TRUE;
879                 }else {
880                     if(!emulate_prot)
881                         CHECK_NOT_CALLED(ReportData2);
882                     CHECK_NOT_CALLED(ReportResult);
883                 }
884             }while(hres == S_OK);
885             if(hres == S_FALSE)
886                 wait_for_switch = FALSE;
887         }else {
888             CHECK_EXPECT(ReportData2);
889
890             ok(grfBSCF & BSCF_LASTDATANOTIFICATION, "grfBSCF = %08x\n", grfBSCF);
891
892             read = 0xdeadbeef;
893             if(emulate_prot)
894                 SET_EXPECT(Read2);
895             hres = IInternetProtocol_Read(binding_test ? binding_protocol : async_protocol, expect_pv = buf, sizeof(buf), &read);
896             if(emulate_prot)
897                 CHECK_CALLED(Read2);
898             ok(hres == S_FALSE, "Read returned: %08x, expected E_FALSE\n", hres);
899             ok(!read, "read = %d\n", read);
900         }
901     }else if(!binding_test && (tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST
902             || tested_protocol == FTP_TEST)) {
903         if(!(grfBSCF & BSCF_LASTDATANOTIFICATION) || (grfBSCF & BSCF_DATAFULLYAVAILABLE))
904             CHECK_EXPECT(ReportData);
905         else if (http_post_test)
906             ok(ulProgress == 13, "Read %u bytes instead of 13\n", ulProgress);
907
908         ok(ulProgress, "ulProgress == 0\n");
909
910         if(first_data_notif) {
911             ok(grfBSCF == BSCF_FIRSTDATANOTIFICATION
912                || grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE),
913                "grcfBSCF = %08x\n", grfBSCF);
914             first_data_notif = FALSE;
915         } else {
916             ok(grfBSCF == BSCF_INTERMEDIATEDATANOTIFICATION
917                || grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION)
918                || broken(grfBSCF == (BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION)),
919                "grcfBSCF = %08x\n", grfBSCF);
920         }
921
922         if(!(bindf & BINDF_FROMURLMON) &&
923            !(grfBSCF & BSCF_LASTDATANOTIFICATION)) {
924             if(state == STATE_CONNECTING) {
925                 state = STATE_DOWNLOADING;
926                 if(http_is_first) {
927                     CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
928                     CHECK_CALLED(ReportProgress_CONNECTING);
929                 } else todo_wine {
930                     CHECK_NOT_CALLED(ReportProgress_FINDINGRESOURCE);
931                     CHECK_NOT_CALLED(ReportProgress_CONNECTING);
932                 }
933                 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
934                 CHECK_CALLED(OnResponse);
935                 CHECK_CALLED(ReportProgress_RAWMIMETYPE);
936             }
937             SetEvent(event_complete);
938         }
939     }else if(!read_report_data) {
940         BYTE buf[1000];
941         ULONG read;
942         HRESULT hres;
943
944         CHECK_EXPECT(ReportData);
945
946         if(tested_protocol != BIND_TEST) {
947             do {
948                 if(mimefilter_test)
949                     SET_EXPECT(MimeFilter_Read);
950                 else if(rec_depth > 1)
951                     SET_EXPECT(Read2);
952                 else
953                     SET_EXPECT(Read);
954                 hres = IInternetProtocol_Read(binding_protocol, expect_pv=buf, sizeof(buf), &read);
955                 if(mimefilter_test)
956                     CHECK_CALLED(MimeFilter_Read);
957                 else if(rec_depth > 1)
958                     CHECK_CALLED(Read2);
959                 else
960                     CHECK_CALLED(Read);
961             }while(hres == S_OK);
962         }
963     }
964
965     rec_depth--;
966     return S_OK;
967 }
968
969 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
970         DWORD dwError, LPCWSTR szResult)
971 {
972     CHECK_EXPECT(ReportResult);
973
974     if(tested_protocol == FTP_TEST)
975         ok(hrResult == E_PENDING || hrResult == S_OK, "hrResult = %08x, expected E_PENDING or S_OK\n", hrResult);
976     else
977         ok(hrResult == expect_hrResult, "hrResult = %08x, expected: %08x\n",
978            hrResult, expect_hrResult);
979     if(SUCCEEDED(hrResult) || tested_protocol == FTP_TEST || test_abort)
980         ok(dwError == ERROR_SUCCESS, "dwError = %d, expected ERROR_SUCCESS\n", dwError);
981     else
982         ok(dwError != ERROR_SUCCESS ||
983            broken(tested_protocol == MK_TEST), /* WinME and NT4 */
984            "dwError == ERROR_SUCCESS\n");
985     ok(!szResult, "szResult != NULL\n");
986
987     if(direct_read)
988         SET_EXPECT(ReportData); /* checked after main loop */
989
990     return S_OK;
991 }
992
993 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
994     ProtocolSink_QueryInterface,
995     ProtocolSink_AddRef,
996     ProtocolSink_Release,
997     ProtocolSink_Switch,
998     ProtocolSink_ReportProgress,
999     ProtocolSink_ReportData,
1000     ProtocolSink_ReportResult
1001 };
1002
1003 static IInternetProtocolSink protocol_sink = { &protocol_sink_vtbl };
1004
1005 static HRESULT WINAPI MimeProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
1006 {
1007     ok(0, "unexpected call\n");
1008     return E_NOTIMPL;
1009 }
1010
1011 static ULONG WINAPI MimeProtocolSink_AddRef(IInternetProtocolSink *iface)
1012 {
1013     return 2;
1014 }
1015
1016 static ULONG WINAPI MimeProtocolSink_Release(IInternetProtocolSink *iface)
1017 {
1018     return 1;
1019 }
1020
1021 static HRESULT WINAPI MimeProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
1022 {
1023     HRESULT hres;
1024
1025     CHECK_EXPECT(MimeFilter_Switch);
1026
1027     SET_EXPECT(Switch);
1028     hres = IInternetProtocolSink_Switch(filtered_sink, pProtocolData);
1029     ok(hres == S_OK, "Switch failed: %08x\n", hres);
1030     CHECK_CALLED(Switch);
1031
1032     return S_OK;
1033 }
1034
1035 static HRESULT WINAPI MimeProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
1036         LPCWSTR szStatusText)
1037 {
1038     CHECK_EXPECT(MimeFilter_ReportProgress);
1039     return S_OK;
1040 }
1041
1042 static HRESULT WINAPI MimeProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
1043         ULONG ulProgress, ULONG ulProgressMax)
1044 {
1045     DWORD read = 0;
1046     BYTE buf[8192];
1047     HRESULT hres;
1048
1049     CHECK_EXPECT(MimeFilter_ReportData);
1050
1051     if(!filter_state) {
1052         SET_EXPECT(Read);
1053         hres = IInternetProtocol_Read(filtered_protocol, buf, sizeof(buf), &read);
1054         if(tested_protocol == HTTP_TEST)
1055             ok(hres == S_OK || hres == E_PENDING || hres == S_FALSE, "Read failed: %08x\n", hres);
1056         else
1057             ok(hres == S_OK, "Read failed: %08x\n", hres);
1058         CHECK_CALLED(Read);
1059
1060         SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1061         hres = IInternetProtocolSink_ReportProgress(filtered_sink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE, text_htmlW);
1062         ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
1063         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1064
1065         /* FIXME: test BINDSTATUS_CACHEFILENAMEAVAILABLE */
1066     }
1067
1068     if(!read_report_data)
1069         SET_EXPECT(ReportData);
1070     hres = IInternetProtocolSink_ReportData(filtered_sink, grfBSCF, ulProgress, ulProgressMax);
1071     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1072     if(!read_report_data)
1073         CHECK_CALLED(ReportData);
1074
1075     if(!filter_state)
1076         filter_state = 1;
1077
1078     return S_OK;
1079 }
1080
1081 static HRESULT WINAPI MimeProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
1082         DWORD dwError, LPCWSTR szResult)
1083 {
1084     HRESULT hres;
1085
1086     CHECK_EXPECT(MimeFilter_ReportResult);
1087
1088     ok(hrResult == S_OK, "hrResult = %08x\n", hrResult);
1089     ok(dwError == ERROR_SUCCESS, "dwError = %u\n", dwError);
1090     ok(!szResult, "szResult = %s\n", wine_dbgstr_w(szResult));
1091
1092     SET_EXPECT(ReportResult);
1093     hres = IInternetProtocolSink_ReportResult(filtered_sink, hrResult, dwError, szResult);
1094     ok(SUCCEEDED(hres), "ReportResult failed: %08x\n", hres);
1095     CHECK_CALLED(ReportResult);
1096
1097     return S_OK;
1098 }
1099
1100 static IInternetProtocolSinkVtbl mime_protocol_sink_vtbl = {
1101     MimeProtocolSink_QueryInterface,
1102     MimeProtocolSink_AddRef,
1103     MimeProtocolSink_Release,
1104     MimeProtocolSink_Switch,
1105     MimeProtocolSink_ReportProgress,
1106     MimeProtocolSink_ReportData,
1107     MimeProtocolSink_ReportResult
1108 };
1109
1110 static IInternetProtocolSink mime_protocol_sink = { &mime_protocol_sink_vtbl };
1111
1112 static HRESULT QueryInterface(REFIID riid, void **ppv)
1113 {
1114     static const IID IID_undocumented = {0x58DFC7D0,0x5381,0x43E5,{0x9D,0x72,0x4C,0xDD,0xE4,0xCB,0x0F,0x1A}};
1115
1116     *ppv = NULL;
1117
1118     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid))
1119         *ppv = &protocol_sink;
1120     if(IsEqualGUID(&IID_IServiceProvider, riid))
1121         *ppv = &service_provider;
1122     if(IsEqualGUID(&IID_IUriContainer, riid))
1123         return E_NOINTERFACE; /* TODO */
1124
1125     /* NOTE: IE8 queries for undocumented {58DFC7D0-5381-43E5-9D72-4CDDE4CB0F1A} interface. */
1126     if(IsEqualGUID(&IID_undocumented, riid))
1127         return E_NOINTERFACE;
1128
1129     if(*ppv)
1130         return S_OK;
1131
1132     ok(0, "unexpected call %s\n", debugstr_guid(riid));
1133     return E_NOINTERFACE;
1134 }
1135
1136 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
1137 {
1138     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
1139         *ppv = iface;
1140         return S_OK;
1141     }
1142     return E_NOINTERFACE;
1143 }
1144
1145 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
1146 {
1147     return 2;
1148 }
1149
1150 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
1151 {
1152     return 1;
1153 }
1154
1155 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
1156 {
1157     DWORD cbSize;
1158
1159     CHECK_EXPECT(GetBindInfo);
1160
1161     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
1162     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
1163     ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
1164
1165     *grfBINDF = bindf;
1166     if(binding_test)
1167         *grfBINDF |= BINDF_FROMURLMON;
1168     cbSize = pbindinfo->cbSize;
1169     memset(pbindinfo, 0, cbSize);
1170     pbindinfo->cbSize = cbSize;
1171
1172     if(http_post_test)
1173     {
1174         pbindinfo->cbstgmedData = sizeof(post_data)-1;
1175         pbindinfo->dwBindVerb = BINDVERB_POST;
1176         pbindinfo->stgmedData.tymed = http_post_test;
1177
1178         if(http_post_test == TYMED_HGLOBAL) {
1179             HGLOBAL data;
1180
1181             /* Must be GMEM_FIXED, GMEM_MOVABLE does not work properly */
1182             data = GlobalAlloc(GPTR, sizeof(post_data));
1183             memcpy(data, post_data, sizeof(post_data));
1184             U(pbindinfo->stgmedData).hGlobal = data;
1185         }else {
1186             IStream *post_stream;
1187             HGLOBAL data;
1188             HRESULT hres;
1189
1190             if(0) {
1191             /* Must be GMEM_FIXED, GMEM_MOVABLE does not work properly */
1192             data = GlobalAlloc(GPTR, sizeof(post_data));
1193             memcpy(data, post_data, sizeof(post_data));
1194             U(pbindinfo->stgmedData).hGlobal = data;
1195
1196             hres = CreateStreamOnHGlobal(data, FALSE, &post_stream);
1197             ok(hres == S_OK, "CreateStreamOnHGlobal failed: %08x\n", hres);
1198
1199             U(pbindinfo->stgmedData).pstm =post_stream;/* &Stream; */
1200             }
1201             U(pbindinfo->stgmedData).pstm = &Stream;
1202         }
1203     }
1204
1205     return S_OK;
1206 }
1207
1208 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType,
1209         LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1210 {
1211     ok(ppwzStr != NULL, "ppwzStr == NULL\n");
1212     ok(pcElFetched != NULL, "pcElFetched == NULL\n");
1213
1214     switch(ulStringType) {
1215     case BINDSTRING_ACCEPT_MIMES:
1216         CHECK_EXPECT(GetBindString_ACCEPT_MIMES);
1217         ok(cEl == 256, "cEl=%d, expected 256\n", cEl);
1218         if(pcElFetched) {
1219             ok(*pcElFetched == 256, "*pcElFetched=%d, expected 256\n", *pcElFetched);
1220             *pcElFetched = 1;
1221         }
1222         if(ppwzStr) {
1223             *ppwzStr = CoTaskMemAlloc(sizeof(acc_mimeW));
1224             memcpy(*ppwzStr, acc_mimeW, sizeof(acc_mimeW));
1225         }
1226         return S_OK;
1227     case BINDSTRING_USER_AGENT:
1228         CHECK_EXPECT(GetBindString_USER_AGENT);
1229         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
1230         if(pcElFetched) {
1231             ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
1232             *pcElFetched = 1;
1233         }
1234         if(ppwzStr) {
1235             *ppwzStr = CoTaskMemAlloc(sizeof(user_agentW));
1236             memcpy(*ppwzStr, user_agentW, sizeof(user_agentW));
1237         }
1238         return S_OK;
1239     case BINDSTRING_POST_COOKIE:
1240         CHECK_EXPECT(GetBindString_POST_COOKIE);
1241         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
1242         if(pcElFetched)
1243             ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
1244         return S_OK;
1245     case BINDSTRING_URL: {
1246         DWORD size;
1247
1248         CHECK_EXPECT(GetBindString_URL);
1249         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
1250         ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
1251         *pcElFetched = 1;
1252
1253         size = (lstrlenW(binding_urls[tested_protocol])+1)*sizeof(WCHAR);
1254         *ppwzStr = CoTaskMemAlloc(size);
1255         memcpy(*ppwzStr, binding_urls[tested_protocol], size);
1256         return S_OK;
1257     }
1258     default:
1259         ok(0, "unexpected call\n");
1260     }
1261
1262     return E_NOTIMPL;
1263 }
1264
1265 static IInternetBindInfoVtbl bind_info_vtbl = {
1266     BindInfo_QueryInterface,
1267     BindInfo_AddRef,
1268     BindInfo_Release,
1269     BindInfo_GetBindInfo,
1270     BindInfo_GetBindString
1271 };
1272
1273 static IInternetBindInfo bind_info = { &bind_info_vtbl };
1274
1275 static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
1276                                                   REFIID riid, void **ppv)
1277 {
1278     ok(0, "unexpected call\n");
1279     return E_NOINTERFACE;
1280 }
1281
1282 static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
1283 {
1284     return 2;
1285 }
1286
1287 static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
1288 {
1289     return 1;
1290 }
1291
1292 static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
1293 {
1294     CHECK_EXPECT(SetPriority);
1295     ok(nPriority == ex_priority, "nPriority=%d\n", nPriority);
1296     return S_OK;
1297 }
1298
1299 static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
1300 {
1301     ok(0, "unexpected call\n");
1302     return E_NOTIMPL;
1303 }
1304
1305
1306 static const IInternetPriorityVtbl InternetPriorityVtbl = {
1307     InternetPriority_QueryInterface,
1308     InternetPriority_AddRef,
1309     InternetPriority_Release,
1310     InternetPriority_SetPriority,
1311     InternetPriority_GetPriority
1312 };
1313
1314 static IInternetPriority InternetPriority = { &InternetPriorityVtbl };
1315
1316 static ULONG WINAPI Protocol_AddRef(IInternetProtocolEx *iface)
1317 {
1318     return 2;
1319 }
1320
1321 static ULONG WINAPI Protocol_Release(IInternetProtocolEx *iface)
1322 {
1323     return 1;
1324 }
1325
1326 static HRESULT WINAPI Protocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
1327         DWORD dwOptions)
1328 {
1329     HRESULT hres;
1330
1331     CHECK_EXPECT(Abort);
1332
1333     SET_EXPECT(ReportResult);
1334     hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
1335     ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1336     CHECK_CALLED(ReportResult);
1337
1338     return S_OK;
1339 }
1340
1341 static HRESULT WINAPI Protocol_Suspend(IInternetProtocolEx *iface)
1342 {
1343     ok(0, "unexpected call\n");
1344     return E_NOTIMPL;
1345 }
1346
1347 static HRESULT WINAPI Protocol_Resume(IInternetProtocolEx *iface)
1348 {
1349     ok(0, "unexpected call\n");
1350     return E_NOTIMPL;
1351 }
1352
1353 static HRESULT WINAPI Protocol_Seek(IInternetProtocolEx *iface,
1354         LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
1355 {
1356     ok(0, "unexpected call\n");
1357     return E_NOTIMPL;
1358 }
1359
1360 static HRESULT WINAPI ProtocolEmul_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
1361 {
1362     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
1363         *ppv = iface;
1364         return S_OK;
1365     }
1366
1367     if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
1368         if(impl_protex) {
1369             *ppv = iface;
1370             return S_OK;
1371         }
1372         *ppv = NULL;
1373         return E_NOINTERFACE;
1374     }
1375
1376     if(IsEqualGUID(&IID_IInternetPriority, riid)) {
1377         *ppv = &InternetPriority;
1378         return S_OK;
1379     }
1380
1381     if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
1382         CHECK_EXPECT(QueryInterface_IWinInetInfo);
1383         *ppv = NULL;
1384         return E_NOINTERFACE;
1385     }
1386
1387     if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
1388         CHECK_EXPECT(QueryInterface_IWinInetHttpInfo);
1389         *ppv = NULL;
1390         return E_NOINTERFACE;
1391     }
1392
1393     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1394     *ppv = NULL;
1395     return E_NOINTERFACE;
1396 }
1397
1398 static DWORD WINAPI thread_proc(PVOID arg)
1399 {
1400     HRESULT hres;
1401
1402     memset(&protocoldata, -1, sizeof(protocoldata));
1403
1404     prot_state = 0;
1405
1406     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
1407     hres = IInternetProtocolSink_ReportProgress(binding_sink,
1408             BINDSTATUS_FINDINGRESOURCE, hostW);
1409     CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
1410     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
1411
1412     SET_EXPECT(ReportProgress_CONNECTING);
1413     hres = IInternetProtocolSink_ReportProgress(binding_sink,
1414             BINDSTATUS_CONNECTING, winehq_ipW);
1415     CHECK_CALLED(ReportProgress_CONNECTING);
1416     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
1417
1418     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1419     hres = IInternetProtocolSink_ReportProgress(binding_sink,
1420             BINDSTATUS_SENDINGREQUEST, NULL);
1421     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1422     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
1423
1424     prot_state = 1;
1425     SET_EXPECT(Switch);
1426     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1427     CHECK_CALLED(Switch);
1428     ok(hres == S_OK, "Switch failed: %08x\n", hres);
1429
1430     if(!short_read) {
1431         prot_state = 2;
1432         if(mimefilter_test)
1433             SET_EXPECT(MimeFilter_Switch);
1434         else
1435             SET_EXPECT(Switch);
1436         hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1437         ok(hres == S_OK, "Switch failed: %08x\n", hres);
1438         if(mimefilter_test)
1439             CHECK_CALLED(MimeFilter_Switch);
1440         else
1441             CHECK_CALLED(Switch);
1442
1443         if(test_abort) {
1444             SetEvent(event_complete);
1445             return 0;
1446         }
1447
1448         prot_state = 2;
1449         if(mimefilter_test)
1450             SET_EXPECT(MimeFilter_Switch);
1451         else
1452             SET_EXPECT(Switch);
1453         hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1454         ok(hres == S_OK, "Switch failed: %08x\n", hres);
1455         if(mimefilter_test)
1456             CHECK_CALLED(MimeFilter_Switch);
1457         else
1458             CHECK_CALLED(Switch);
1459
1460         prot_state = 3;
1461         if(mimefilter_test)
1462             SET_EXPECT(MimeFilter_Switch);
1463         else
1464             SET_EXPECT(Switch);
1465         hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1466         ok(hres == S_OK, "Switch failed: %08x\n", hres);
1467         if(mimefilter_test)
1468             CHECK_CALLED(MimeFilter_Switch);
1469         else
1470             CHECK_CALLED(Switch);
1471     }
1472
1473     SetEvent(event_complete);
1474
1475     return 0;
1476 }
1477
1478 static void protocol_start(IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo, DWORD pi)
1479 {
1480     BINDINFO bindinfo, exp_bindinfo;
1481     DWORD cbindf = 0;
1482     HRESULT hres;
1483
1484     ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
1485     ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
1486     ok(pOIProtSink != &protocol_sink, "unexpected pOIProtSink\n");
1487     ok(pOIBindInfo != &bind_info, "unexpected pOIBindInfo\n");
1488     ok(!pi, "pi = %x\n", pi);
1489
1490     if(binding_test)
1491         ok(pOIProtSink == binding_sink, "pOIProtSink != binding_sink\n");
1492
1493     memset(&bindinfo, 0, sizeof(bindinfo));
1494     bindinfo.cbSize = sizeof(bindinfo);
1495     memcpy(&exp_bindinfo, &bindinfo, sizeof(bindinfo));
1496     SET_EXPECT(GetBindInfo);
1497     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
1498     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
1499     CHECK_CALLED(GetBindInfo);
1500     ok(cbindf == (bindf|BINDF_FROMURLMON), "bindf = %x, expected %x\n",
1501        cbindf, (bindf|BINDF_FROMURLMON));
1502     ok(!memcmp(&exp_bindinfo, &bindinfo, sizeof(bindinfo)), "unexpected bindinfo\n");
1503     pReleaseBindInfo(&bindinfo);
1504
1505     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1506     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, emptyW);
1507     ok(hres == S_OK, "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
1508     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1509
1510     if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
1511         IServiceProvider *service_provider;
1512         IHttpNegotiate *http_negotiate;
1513         IHttpNegotiate2 *http_negotiate2;
1514         LPWSTR ua = (LPWSTR)0xdeadbeef, accept_mimes[256];
1515         LPWSTR additional_headers = NULL;
1516         BYTE sec_id[100];
1517         DWORD fetched = 0, size = 100;
1518         DWORD tid;
1519
1520         SET_EXPECT(GetBindString_USER_AGENT);
1521         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT,
1522                                                &ua, 1, &fetched);
1523         CHECK_CALLED(GetBindString_USER_AGENT);
1524         ok(hres == S_OK, "GetBindString(BINDSTRING_USER_AGETNT) failed: %08x\n", hres);
1525         ok(fetched == 1, "fetched = %d, expected 254\n", fetched);
1526         ok(ua != NULL, "ua =  %p\n", ua);
1527         ok(!lstrcmpW(ua, user_agentW), "unexpected user agent %s\n", wine_dbgstr_w(ua));
1528         CoTaskMemFree(ua);
1529
1530         fetched = 256;
1531         SET_EXPECT(GetBindString_ACCEPT_MIMES);
1532         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
1533                                                accept_mimes, 256, &fetched);
1534         CHECK_CALLED(GetBindString_ACCEPT_MIMES);
1535
1536         ok(hres == S_OK,
1537            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
1538         ok(fetched == 1, "fetched = %d, expected 1\n", fetched);
1539         ok(!lstrcmpW(acc_mimeW, accept_mimes[0]), "unexpected mimes %s\n", wine_dbgstr_w(accept_mimes[0]));
1540         CoTaskMemFree(accept_mimes[0]);
1541
1542         hres = IInternetBindInfo_QueryInterface(pOIBindInfo, &IID_IServiceProvider,
1543                                                 (void**)&service_provider);
1544         ok(hres == S_OK, "QueryInterface failed: %08x\n", hres);
1545
1546         SET_EXPECT(QueryService_HttpNegotiate);
1547         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
1548                 &IID_IHttpNegotiate, (void**)&http_negotiate);
1549         CHECK_CALLED(QueryService_HttpNegotiate);
1550         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
1551
1552         SET_EXPECT(BeginningTransaction);
1553         hres = IHttpNegotiate_BeginningTransaction(http_negotiate, binding_urls[tested_protocol],
1554                                                    NULL, 0, &additional_headers);
1555         CHECK_CALLED(BeginningTransaction);
1556         IHttpNegotiate_Release(http_negotiate);
1557         ok(hres == S_OK, "BeginningTransction failed: %08x\n", hres);
1558         ok(additional_headers == NULL, "additional_headers=%p\n", additional_headers);
1559
1560         SET_EXPECT(QueryService_HttpNegotiate);
1561         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
1562                 &IID_IHttpNegotiate2, (void**)&http_negotiate2);
1563         CHECK_CALLED(QueryService_HttpNegotiate);
1564         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
1565
1566         size = 512;
1567         SET_EXPECT(GetRootSecurityId);
1568         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, sec_id, &size, 0);
1569         CHECK_CALLED(GetRootSecurityId);
1570         IHttpNegotiate2_Release(http_negotiate2);
1571         ok(hres == E_FAIL, "GetRootSecurityId failed: %08x, expected E_FAIL\n", hres);
1572         ok(size == 13, "size=%d\n", size);
1573
1574         IServiceProvider_Release(service_provider);
1575
1576         CreateThread(NULL, 0, thread_proc, NULL, 0, &tid);
1577         return;
1578     }
1579
1580     SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1581     hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
1582             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
1583     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
1584     CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
1585
1586     if(mimefilter_test) {
1587         SET_EXPECT(MimeFilter_CreateInstance);
1588         SET_EXPECT(MimeFilter_Start);
1589         SET_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
1590     }
1591     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1592     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
1593             mimefilter_test ? gzipW : (expect_wsz = text_htmlW));
1594     ok(hres == S_OK,
1595        "ReportProgress(BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE) failed: %08x\n", hres);
1596     if(mimefilter_test) {
1597         CHECK_CALLED(MimeFilter_CreateInstance);
1598         CHECK_CALLED(MimeFilter_Start);
1599         CHECK_CALLED(ReportProgress_LOADINGMIMEHANDLER);
1600         todo_wine CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1601     }else {
1602         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1603     }
1604
1605     if(mimefilter_test)
1606         SET_EXPECT(MimeFilter_ReportData);
1607     else
1608         SET_EXPECT(ReportData);
1609     hres = IInternetProtocolSink_ReportData(pOIProtSink,
1610             BSCF_FIRSTDATANOTIFICATION | (tested_protocol == ITS_TEST ? BSCF_DATAFULLYAVAILABLE : BSCF_LASTDATANOTIFICATION),
1611             13, 13);
1612     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1613     if(mimefilter_test)
1614         CHECK_CALLED(MimeFilter_ReportData);
1615     else
1616         CHECK_CALLED(ReportData);
1617
1618     if(tested_protocol == ITS_TEST) {
1619         SET_EXPECT(ReportData);
1620         hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
1621         ok(hres == S_OK, "ReportProgress(BINDSTATUS_BEGINDOWNLOADDATA) failed: %08x\n", hres);
1622         CHECK_CALLED(ReportData);
1623     }
1624
1625     if(tested_protocol == BIND_TEST) {
1626         hres = IInternetProtocol_Terminate(binding_protocol, 0);
1627         ok(hres == E_FAIL, "Termiante failed: %08x\n", hres);
1628     }
1629
1630     if(mimefilter_test)
1631         SET_EXPECT(MimeFilter_ReportResult);
1632     else
1633         SET_EXPECT(ReportResult);
1634     hres = IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
1635     ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1636     if(mimefilter_test)
1637         CHECK_CALLED(MimeFilter_ReportResult);
1638     else
1639         CHECK_CALLED(ReportResult);
1640 }
1641
1642 static HRESULT WINAPI ProtocolEmul_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
1643         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
1644         DWORD grfPI, HANDLE_PTR dwReserved)
1645 {
1646     CHECK_EXPECT(Start);
1647
1648     ok(!dwReserved, "dwReserved = %lx\n", dwReserved);
1649     protocol_start(pOIProtSink, pOIBindInfo, grfPI);
1650     return S_OK;
1651 }
1652
1653 static HRESULT WINAPI ProtocolEmul_Continue(IInternetProtocolEx *iface,
1654         PROTOCOLDATA *pProtocolData)
1655 {
1656     DWORD bscf = 0, pr;
1657     HRESULT hres;
1658
1659     CHECK_EXPECT(Continue);
1660
1661     ok(pProtocolData != NULL, "pProtocolData == NULL\n");
1662     if(!pProtocolData || tested_protocol == BIND_TEST)
1663         return S_OK;
1664     if(binding_test) {
1665         ok(pProtocolData != &protocoldata, "pProtocolData == &protocoldata\n");
1666         ok(pProtocolData->grfFlags == protocoldata.grfFlags, "grfFlags wrong %x/%x\n",
1667            pProtocolData->grfFlags, protocoldata.grfFlags );
1668         ok(pProtocolData->dwState == protocoldata.dwState, "dwState wrong %x/%x\n",
1669            pProtocolData->dwState, protocoldata.dwState );
1670         ok(pProtocolData->pData == protocoldata.pData, "pData wrong %p/%p\n",
1671            pProtocolData->pData, protocoldata.pData );
1672         ok(pProtocolData->cbData == protocoldata.cbData, "cbData wrong %x/%x\n",
1673            pProtocolData->cbData, protocoldata.cbData );
1674     }
1675
1676     switch(prot_state) {
1677     case 1: {
1678         IServiceProvider *service_provider;
1679         IHttpNegotiate *http_negotiate;
1680         static WCHAR header[] = {'?',0};
1681
1682         hres = IInternetProtocolSink_QueryInterface(binding_sink, &IID_IServiceProvider,
1683                                                     (void**)&service_provider);
1684         ok(hres == S_OK, "Could not get IServiceProvicder\n");
1685
1686         SET_EXPECT(QueryService_HttpNegotiate);
1687         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
1688                                              &IID_IHttpNegotiate, (void**)&http_negotiate);
1689         IServiceProvider_Release(service_provider);
1690         CHECK_CALLED(QueryService_HttpNegotiate);
1691         ok(hres == S_OK, "Could not get IHttpNegotiate\n");
1692
1693         SET_EXPECT(OnResponse);
1694         hres = IHttpNegotiate_OnResponse(http_negotiate, 200, header, NULL, NULL);
1695         IHttpNegotiate_Release(http_negotiate);
1696         CHECK_CALLED(OnResponse);
1697         IHttpNegotiate_Release(http_negotiate);
1698         ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
1699
1700         if(mimefilter_test) {
1701             SET_EXPECT(MimeFilter_CreateInstance);
1702             SET_EXPECT(MimeFilter_Start);
1703             SET_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
1704         }else if(!(pi & PI_MIMEVERIFICATION)) {
1705             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1706         }
1707         hres = IInternetProtocolSink_ReportProgress(binding_sink,
1708                 BINDSTATUS_MIMETYPEAVAILABLE, mimefilter_test ? gzipW : text_htmlW);
1709         if(mimefilter_test) {
1710             CHECK_CALLED(MimeFilter_CreateInstance);
1711             CHECK_CALLED(MimeFilter_Start);
1712             CHECK_CALLED(ReportProgress_LOADINGMIMEHANDLER);
1713         }else if(!(pi & PI_MIMEVERIFICATION)) {
1714             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1715         }
1716         ok(hres == S_OK,
1717            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
1718
1719         bscf |= BSCF_FIRSTDATANOTIFICATION;
1720         break;
1721     }
1722     case 2:
1723     case 3:
1724         bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
1725         break;
1726     }
1727
1728     pr = prot_read;
1729     if(mimefilter_test) {
1730         SET_EXPECT(MimeFilter_ReportData);
1731     }else if(pi & PI_MIMEVERIFICATION) {
1732         if(pr < 200)
1733             SET_EXPECT(Read); /* checked in ReportData for short_read */
1734         if(pr == 200) {
1735             if(!mimefilter_test)
1736                 SET_EXPECT(Read); /* checked in BINDSTATUS_MIMETYPEAVAILABLE or ReportData */
1737             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1738         }
1739         if(pr >= 200)
1740             SET_EXPECT(ReportData);
1741     }else {
1742         SET_EXPECT(ReportData);
1743     }
1744
1745     hres = IInternetProtocolSink_ReportData(binding_sink, bscf, pr, 400);
1746     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1747
1748     if(mimefilter_test) {
1749         SET_EXPECT(MimeFilter_ReportData);
1750     }else if(pi & PI_MIMEVERIFICATION) {
1751         if(!short_read && pr < 200)
1752             CHECK_CALLED(Read);
1753         if(pr == 200) {
1754             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1755         }
1756     }else {
1757         CHECK_CALLED(ReportData);
1758     }
1759
1760     if(prot_state == 3)
1761         prot_state = 4;
1762
1763     return S_OK;
1764 }
1765
1766 static HRESULT WINAPI ProtocolEmul_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
1767 {
1768     CHECK_EXPECT(Terminate);
1769     ok(!dwOptions, "dwOptions=%d\n", dwOptions);
1770     return S_OK;
1771 }
1772
1773 static HRESULT WINAPI ProtocolEmul_Read(IInternetProtocolEx *iface, void *pv,
1774         ULONG cb, ULONG *pcbRead)
1775 {
1776     if(read_report_data)
1777         CHECK_EXPECT2(Read2);
1778
1779     if(mimefilter_test || short_read) {
1780         if(!read_report_data)
1781             CHECK_EXPECT2(Read);
1782     }else if((pi & PI_MIMEVERIFICATION)) {
1783         if(!read_report_data)
1784             CHECK_EXPECT2(Read);
1785
1786         if(prot_read < 300) {
1787             ok(pv != expect_pv, "pv == expect_pv\n");
1788             if(prot_read < 300)
1789                 ok(cb == 2048-prot_read, "cb=%d\n", cb);
1790             else
1791                 ok(cb == 700, "cb=%d\n", cb);
1792         }else {
1793             ok(expect_pv <= pv && (BYTE*)pv < (BYTE*)expect_pv + cb, "pv != expect_pv\n");
1794         }
1795     }else {
1796         if(!read_report_data)
1797             CHECK_EXPECT(Read);
1798
1799         ok(pv == expect_pv, "pv != expect_pv\n");
1800         ok(cb == 1000, "cb=%d\n", cb);
1801         ok(!*pcbRead, "*pcbRead = %d\n", *pcbRead);
1802     }
1803     ok(pcbRead != NULL, "pcbRead == NULL\n");
1804
1805     if(prot_state == 3 || (short_read && prot_state != 4)) {
1806         HRESULT hres;
1807
1808         prot_state = 4;
1809         if(short_read) {
1810             SET_EXPECT(Read2); /* checked in BINDSTATUS_MIMETYPEAVAILABLE */
1811             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1812         }
1813         if(mimefilter_test)
1814             SET_EXPECT(MimeFilter_ReportData);
1815         else if(direct_read)
1816             SET_EXPECT(ReportData2);
1817         read_report_data++;
1818         hres = IInternetProtocolSink_ReportData(binding_sink,
1819                 BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION, 0, 0);
1820         read_report_data--;
1821         ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1822         if(short_read)
1823             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1824         if(mimefilter_test)
1825             CHECK_CALLED(MimeFilter_ReportData);
1826         else if(direct_read)
1827             CHECK_CALLED(ReportData2);
1828
1829         if(mimefilter_test)
1830             SET_EXPECT(MimeFilter_ReportResult);
1831         else
1832             SET_EXPECT(ReportResult);
1833         hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
1834         ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1835         if(mimefilter_test)
1836             CHECK_CALLED(MimeFilter_ReportResult);
1837         else
1838             CHECK_CALLED(ReportResult);
1839
1840         memset(pv, 'x', 100);
1841         prot_read += *pcbRead = 100;
1842         return S_OK;
1843     }if(prot_state == 4) {
1844         *pcbRead = 0;
1845         return S_FALSE;
1846     }
1847
1848     if((async_read_pending = !async_read_pending)) {
1849         *pcbRead = 0;
1850         return tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST ? E_PENDING : S_FALSE;
1851     }
1852
1853     memset(pv, 'x', 100);
1854     prot_read += *pcbRead = 100;
1855     return S_OK;
1856 }
1857
1858 static HRESULT WINAPI ProtocolEmul_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
1859 {
1860     CHECK_EXPECT(LockRequest);
1861     ok(dwOptions == 0, "dwOptions=%x\n", dwOptions);
1862     return S_OK;
1863 }
1864
1865 static HRESULT WINAPI ProtocolEmul_UnlockRequest(IInternetProtocolEx *iface)
1866 {
1867     CHECK_EXPECT(UnlockRequest);
1868     return S_OK;
1869 }
1870
1871 static HRESULT WINAPI ProtocolEmul_StartEx(IInternetProtocolEx *iface, IUri *pUri,
1872         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
1873         DWORD grfPI, HANDLE *dwReserved)
1874 {
1875     CHECK_EXPECT(StartEx);
1876     ok(!dwReserved, "dwReserved = %p\n", dwReserved);
1877     protocol_start(pOIProtSink, pOIBindInfo, grfPI);
1878     return S_OK;
1879 }
1880
1881 static const IInternetProtocolExVtbl ProtocolVtbl = {
1882     ProtocolEmul_QueryInterface,
1883     Protocol_AddRef,
1884     Protocol_Release,
1885     ProtocolEmul_Start,
1886     ProtocolEmul_Continue,
1887     Protocol_Abort,
1888     ProtocolEmul_Terminate,
1889     Protocol_Suspend,
1890     Protocol_Resume,
1891     ProtocolEmul_Read,
1892     Protocol_Seek,
1893     ProtocolEmul_LockRequest,
1894     ProtocolEmul_UnlockRequest,
1895     ProtocolEmul_StartEx
1896 };
1897
1898 static IInternetProtocolEx Protocol = { &ProtocolVtbl };
1899
1900 static HRESULT WINAPI MimeProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
1901 {
1902     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
1903         *ppv = iface;
1904         return S_OK;
1905     }
1906
1907     if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
1908         *ppv = &mime_protocol_sink;
1909         return S_OK;
1910     }
1911
1912     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1913     *ppv = NULL;
1914     return E_NOINTERFACE;
1915 }
1916
1917 static HRESULT WINAPI MimeProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
1918         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
1919         DWORD grfPI, HANDLE_PTR dwReserved)
1920 {
1921     PROTOCOLFILTERDATA *data;
1922     LPOLESTR url_str = NULL;
1923     DWORD fetched = 0;
1924     BINDINFO bindinfo;
1925     DWORD cbindf = 0;
1926     HRESULT hres;
1927
1928     CHECK_EXPECT(MimeFilter_Start);
1929
1930     ok(!lstrcmpW(szUrl, gzipW), "wrong url %s\n", wine_dbgstr_w(szUrl));
1931     ok(grfPI == (PI_FILTER_MODE|PI_FORCE_ASYNC), "grfPI=%x, expected PI_FILTER_MODE|PI_FORCE_ASYNC\n", grfPI);
1932     ok(dwReserved, "dwReserved == 0\n");
1933     ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
1934     ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
1935
1936     if(binding_test) {
1937         ok(pOIProtSink != binding_sink, "pOIProtSink == protocol_sink\n");
1938         ok(pOIBindInfo == prot_bind_info, "pOIBindInfo != bind_info\n");
1939     }else {
1940         ok(pOIProtSink == &protocol_sink, "pOIProtSink != protocol_sink\n");
1941         ok(pOIBindInfo == &bind_info, "pOIBindInfo != bind_info\n");
1942     }
1943
1944     data = (void*)dwReserved;
1945     ok(data->cbSize == sizeof(*data), "data->cbSize = %d\n", data->cbSize);
1946     ok(!data->pProtocolSink, "data->pProtocolSink != NULL\n");
1947     ok(data->pProtocol != NULL, "data->pProtocol == NULL\n");
1948     ok(!data->pUnk, "data->pUnk != NULL\n");
1949     ok(!data->dwFilterFlags, "data->dwProtocolFlags = %x\n", data->dwFilterFlags);
1950     if(binding_test) {
1951         IInternetProtocolSink *prot_sink;
1952
1953         IInternetProtocol_QueryInterface(data->pProtocol, &IID_IInternetProtocolSink, (void**)&prot_sink);
1954         ok(prot_sink == pOIProtSink, "QI(data->pProtocol, IID_IInternetProtocolSink) != pOIProtSink\n");
1955         IInternetProtocolSink_Release(prot_sink);
1956
1957         ok(data->pProtocol != binding_protocol, "data->pProtocol == binding_protocol\n");
1958
1959         filtered_protocol = data->pProtocol;
1960         IInternetProtocol_AddRef(filtered_protocol);
1961     }else {
1962         IInternetProtocol *prot;
1963
1964         IInternetProtocol_QueryInterface(data->pProtocol, &IID_IInternetProtocol, (void**)&prot);
1965         ok(prot == async_protocol, "QI(data->pProtocol, IID_IInternetProtocol) != async_protocol\n");
1966         IInternetProtocol_Release(prot);
1967
1968         ok(data->pProtocol != async_protocol, "data->pProtocol == async_protocol\n");
1969     }
1970
1971     filtered_sink = pOIProtSink;
1972
1973     SET_EXPECT(ReportProgress_DECODING);
1974     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DECODING, gzipW);
1975     ok(hres == S_OK, "ReportProgress(BINDSTATUS_DECODING) failed: %08x\n", hres);
1976     CHECK_CALLED(ReportProgress_DECODING);
1977
1978     SET_EXPECT(GetBindInfo);
1979     memset(&bindinfo, 0, sizeof(bindinfo));
1980     bindinfo.cbSize = sizeof(bindinfo);
1981     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
1982     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
1983     ok(cbindf == (bindf|BINDF_FROMURLMON), "cbindf = %x, expected %x\n", cbindf, bindf);
1984     CHECK_CALLED(GetBindInfo);
1985
1986     SET_EXPECT(GetBindString_URL);
1987     hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_URL, &url_str, 1, &fetched);
1988     ok(hres == S_OK, "GetBindString(BINDSTRING_URL) failed: %08x\n", hres);
1989     ok(fetched == 1, "fetched = %d\n", fetched);
1990     ok(!lstrcmpW(url_str, binding_urls[tested_protocol]), "wrong url_str %s\n", wine_dbgstr_w(url_str));
1991     CoTaskMemFree(url_str);
1992     CHECK_CALLED(GetBindString_URL);
1993
1994     return S_OK;
1995 }
1996
1997 static HRESULT WINAPI Protocol_Continue(IInternetProtocolEx *iface,
1998         PROTOCOLDATA *pProtocolData)
1999 {
2000     CHECK_EXPECT(MimeFilter_Continue);
2001     return E_NOTIMPL;
2002 }
2003
2004 static HRESULT WINAPI MimeProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
2005 {
2006     HRESULT hres;
2007
2008     CHECK_EXPECT(MimeFilter_Terminate);
2009
2010     ok(!dwOptions, "dwOptions = %x\n", dwOptions);
2011
2012     SET_EXPECT(Terminate);
2013     hres = IInternetProtocol_Terminate(filtered_protocol, dwOptions);
2014     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2015     CHECK_CALLED(Terminate);
2016
2017     return S_OK;
2018 }
2019
2020 static HRESULT WINAPI MimeProtocol_Read(IInternetProtocolEx *iface, void *pv,
2021         ULONG cb, ULONG *pcbRead)
2022 {
2023     BYTE buf[2096];
2024     DWORD read = 0;
2025     HRESULT hres;
2026
2027     CHECK_EXPECT(MimeFilter_Read);
2028
2029     ok(pv != NULL, "pv == NULL\n");
2030     ok(cb != 0, "cb == 0\n");
2031     ok(pcbRead != NULL, "pcbRead == NULL\n");
2032
2033     if(read_report_data)
2034         SET_EXPECT(Read2);
2035     else
2036         SET_EXPECT(Read);
2037     hres = IInternetProtocol_Read(filtered_protocol, buf, sizeof(buf), &read);
2038     ok(hres == S_OK || hres == S_FALSE || hres == E_PENDING, "Read failed: %08x\n", hres);
2039     if(read_report_data)
2040         CHECK_CALLED(Read2);
2041     else
2042         CHECK_CALLED(Read);
2043
2044     if(pcbRead) {
2045         ok(*pcbRead == 0, "*pcbRead=%d, expected 0\n", *pcbRead);
2046         *pcbRead = read;
2047     }
2048
2049     memset(pv, 'x', read);
2050     return hres;
2051 }
2052
2053 static HRESULT WINAPI MimeProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
2054 {
2055     HRESULT hres;
2056
2057     CHECK_EXPECT(MimeFilter_LockRequest);
2058
2059     ok(!dwOptions, "dwOptions = %x\n", dwOptions);
2060
2061     SET_EXPECT(LockRequest);
2062     hres = IInternetProtocol_LockRequest(filtered_protocol, dwOptions);
2063     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2064     CHECK_CALLED(LockRequest);
2065
2066     return S_OK;
2067 }
2068
2069 static HRESULT WINAPI MimeProtocol_UnlockRequest(IInternetProtocolEx *iface)
2070 {
2071     HRESULT hres;
2072
2073     CHECK_EXPECT(MimeFilter_UnlockRequest);
2074
2075     SET_EXPECT(UnlockRequest);
2076     hres = IInternetProtocol_UnlockRequest(filtered_protocol);
2077     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2078     CHECK_CALLED(UnlockRequest);
2079
2080     return S_OK;
2081 }
2082
2083 static const IInternetProtocolExVtbl MimeProtocolVtbl = {
2084     MimeProtocol_QueryInterface,
2085     Protocol_AddRef,
2086     Protocol_Release,
2087     MimeProtocol_Start,
2088     Protocol_Continue,
2089     Protocol_Abort,
2090     MimeProtocol_Terminate,
2091     Protocol_Suspend,
2092     Protocol_Resume,
2093     MimeProtocol_Read,
2094     Protocol_Seek,
2095     MimeProtocol_LockRequest,
2096     MimeProtocol_UnlockRequest
2097 };
2098
2099 static IInternetProtocolEx MimeProtocol = { &MimeProtocolVtbl };
2100
2101 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
2102 {
2103     ok(0, "unexpected call\n");
2104     return E_NOINTERFACE;
2105 }
2106
2107 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
2108 {
2109     return 2;
2110 }
2111
2112 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
2113 {
2114     return 1;
2115 }
2116
2117 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
2118                                         REFIID riid, void **ppv)
2119 {
2120     CHECK_EXPECT(CreateInstance);
2121
2122     ok(pOuter == (IUnknown*)prot_bind_info, "pOuter != protocol_unk\n");
2123     ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", debugstr_guid(riid));
2124     ok(ppv != NULL, "ppv == NULL\n");
2125
2126     *ppv = &Protocol;
2127     return S_OK;
2128 }
2129
2130 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
2131 {
2132     ok(0, "unexpected call\n");
2133     return S_OK;
2134 }
2135
2136 static const IClassFactoryVtbl ClassFactoryVtbl = {
2137     ClassFactory_QueryInterface,
2138     ClassFactory_AddRef,
2139     ClassFactory_Release,
2140     ClassFactory_CreateInstance,
2141     ClassFactory_LockServer
2142 };
2143
2144 static IClassFactory ClassFactory = { &ClassFactoryVtbl };
2145
2146 static HRESULT WINAPI MimeFilter_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
2147 {
2148     CHECK_EXPECT(MimeFilter_CreateInstance);
2149
2150     ok(!outer, "outer = %p\n", outer);
2151     ok(IsEqualGUID(&IID_IInternetProtocol, riid), "unexpected riid %s\n", debugstr_guid(riid));
2152
2153     *ppv = &MimeProtocol;
2154     return S_OK;
2155 }
2156
2157 static const IClassFactoryVtbl MimeFilterCFVtbl = {
2158     ClassFactory_QueryInterface,
2159     ClassFactory_AddRef,
2160     ClassFactory_Release,
2161     MimeFilter_CreateInstance,
2162     ClassFactory_LockServer
2163 };
2164
2165 static IClassFactory mimefilter_cf = { &MimeFilterCFVtbl };
2166
2167 #define TEST_BINDING     0x0001
2168 #define TEST_FILTER      0x0002
2169 #define TEST_FIRST_HTTP  0x0004
2170 #define TEST_DIRECT_READ 0x0008
2171 #define TEST_POST        0x0010
2172 #define TEST_EMULATEPROT 0x0020
2173 #define TEST_SHORT_READ  0x0040
2174 #define TEST_REDIRECT    0x0080
2175 #define TEST_ABORT       0x0100
2176 #define TEST_ASYNCREQ    0x0200
2177 #define TEST_USEIURI     0x0400
2178 #define TEST_IMPLPROTEX  0x0800
2179
2180 static void register_filter(BOOL do_register)
2181 {
2182     IInternetSession *session;
2183     HRESULT hres;
2184
2185     static const WCHAR gzipW[] = {'g','z','i','p',0};
2186
2187     hres = pCoInternetGetSession(0, &session, 0);
2188     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2189
2190     if(do_register) {
2191         hres = IInternetSession_RegisterMimeFilter(session, &mimefilter_cf, &IID_IInternetProtocol, gzipW);
2192         ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
2193     }else {
2194         hres = IInternetSession_UnregisterMimeFilter(session, &mimefilter_cf, gzipW);
2195         ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
2196     }
2197
2198     IInternetSession_Release(session);
2199 }
2200
2201 static void init_test(int prot, DWORD flags)
2202 {
2203     tested_protocol = prot;
2204     binding_test = (flags & TEST_BINDING) != 0;
2205     first_data_notif = TRUE;
2206     prot_read = 0;
2207     prot_state = 0;
2208     async_read_pending = TRUE;
2209     mimefilter_test = (flags & TEST_FILTER) != 0;
2210     filter_state = 0;
2211     post_stream_read = 0;
2212     ResetEvent(event_complete);
2213     ResetEvent(event_complete2);
2214     ResetEvent(event_continue);
2215     ResetEvent(event_continue_done);
2216     async_protocol = binding_protocol = filtered_protocol = NULL;
2217     filtered_sink = NULL;
2218     http_is_first = (flags & TEST_FIRST_HTTP) != 0;
2219     first_data_notif = TRUE;
2220     state = STATE_CONNECTING;
2221     test_async_req = (flags & TEST_ASYNCREQ) != 0;
2222     direct_read = (flags & TEST_DIRECT_READ) != 0;
2223     emulate_prot = (flags & TEST_EMULATEPROT) != 0;
2224     wait_for_switch = TRUE;
2225     short_read = (flags & TEST_SHORT_READ) != 0;
2226     http_post_test = TYMED_NULL;
2227     test_redirect = (flags & TEST_REDIRECT) != 0;
2228     test_abort = (flags & TEST_ABORT) != 0;
2229     impl_protex = (flags & TEST_IMPLPROTEX) != 0;
2230
2231     register_filter(mimefilter_test);
2232 }
2233
2234 static void test_priority(IInternetProtocol *protocol)
2235 {
2236     IInternetPriority *priority;
2237     LONG pr;
2238     HRESULT hres;
2239
2240     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority,
2241                                             (void**)&priority);
2242     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
2243     if(FAILED(hres))
2244         return;
2245
2246     hres = IInternetPriority_GetPriority(priority, &pr);
2247     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2248     ok(pr == 0, "pr=%d, expected 0\n", pr);
2249
2250     hres = IInternetPriority_SetPriority(priority, 1);
2251     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2252
2253     hres = IInternetPriority_GetPriority(priority, &pr);
2254     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2255     ok(pr == 1, "pr=%d, expected 1\n", pr);
2256
2257     IInternetPriority_Release(priority);
2258 }
2259
2260 static void test_early_abort(const CLSID *clsid)
2261 {
2262     IInternetProtocol *protocol;
2263     HRESULT hres;
2264
2265     hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2266             &IID_IInternetProtocol, (void**)&protocol);
2267     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2268
2269     hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
2270     ok(hres == S_OK, "Abort failed: %08x\n", hres);
2271
2272     hres = IInternetProtocol_Abort(protocol, E_FAIL, 0);
2273     ok(hres == S_OK, "Abort failed: %08x\n", hres);
2274
2275     IInternetProtocol_Release(protocol);
2276 }
2277
2278 static BOOL file_protocol_start(IInternetProtocol *protocol, LPCWSTR url,
2279         IInternetProtocolEx *protocolex, IUri *uri, BOOL is_first)
2280 {
2281     HRESULT hres;
2282
2283     SET_EXPECT(GetBindInfo);
2284     if(!(bindf & BINDF_FROMURLMON))
2285        SET_EXPECT(ReportProgress_DIRECTBIND);
2286     if(is_first) {
2287         SET_EXPECT(ReportProgress_SENDINGREQUEST);
2288         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2289         if(bindf & BINDF_FROMURLMON)
2290             SET_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
2291         else
2292             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
2293     }
2294     SET_EXPECT(ReportData);
2295     if(is_first)
2296         SET_EXPECT(ReportResult);
2297
2298     expect_hrResult = S_OK;
2299
2300     if(protocolex) {
2301         hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, 0, 0);
2302         ok(hres == S_OK, "StartEx failed: %08x\n", hres);
2303     }else {
2304         hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
2305         if(hres == INET_E_RESOURCE_NOT_FOUND) {
2306             win_skip("Start failed\n");
2307             return FALSE;
2308         }
2309         ok(hres == S_OK, "Start failed: %08x\n", hres);
2310     }
2311
2312     CHECK_CALLED(GetBindInfo);
2313     if(!(bindf & BINDF_FROMURLMON))
2314        CHECK_CALLED(ReportProgress_DIRECTBIND);
2315     if(is_first) {
2316         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2317         CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
2318         if(bindf & BINDF_FROMURLMON)
2319             CHECK_CALLED(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
2320         else
2321             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
2322     }
2323     CHECK_CALLED(ReportData);
2324     if(is_first)
2325         CHECK_CALLED(ReportResult);
2326
2327     return TRUE;
2328 }
2329
2330 static void test_file_protocol_url(LPCWSTR url)
2331 {
2332     IInternetProtocolInfo *protocol_info;
2333     IUnknown *unk;
2334     IClassFactory *factory;
2335     IInternetProtocol *protocol;
2336     BYTE buf[512];
2337     ULONG cb;
2338     HRESULT hres;
2339
2340     hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
2341             &IID_IUnknown, (void**)&unk);
2342     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2343     if(FAILED(hres))
2344         return;
2345
2346     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2347     ok(hres == E_NOINTERFACE,
2348             "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2349
2350     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2351     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2352     IUnknown_Release(unk);
2353     if(FAILED(hres))
2354         return;
2355
2356     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
2357     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2358
2359     if(SUCCEEDED(hres)) {
2360         if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
2361             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2362             ok(hres == S_OK, "Read failed: %08x\n", hres);
2363             ok(cb == 2, "cb=%u expected 2\n", cb);
2364             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
2365             ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2366             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
2367             ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
2368             ok(cb == 0, "cb=%u expected 0\n", cb);
2369             hres = IInternetProtocol_UnlockRequest(protocol);
2370             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2371         }
2372
2373         if(file_protocol_start(protocol, url, NULL, NULL, FALSE)) {
2374             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2375             ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2376             hres = IInternetProtocol_LockRequest(protocol, 0);
2377             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2378             hres = IInternetProtocol_UnlockRequest(protocol);
2379             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2380         }
2381
2382         IInternetProtocol_Release(protocol);
2383     }
2384
2385     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
2386     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2387     if(SUCCEEDED(hres)) {
2388         if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
2389             hres = IInternetProtocol_LockRequest(protocol, 0);
2390             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2391             hres = IInternetProtocol_Terminate(protocol, 0);
2392             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2393             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2394             ok(hres == S_OK, "Read failed: %08x\n\n", hres);
2395             hres = IInternetProtocol_UnlockRequest(protocol);
2396             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2397             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2398             ok(hres == S_OK, "Read failed: %08x\n", hres);
2399             hres = IInternetProtocol_Terminate(protocol, 0);
2400             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2401         }
2402
2403         IInternetProtocol_Release(protocol);
2404     }
2405
2406     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
2407     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2408     if(SUCCEEDED(hres)) {
2409         if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
2410             hres = IInternetProtocol_Terminate(protocol, 0);
2411             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2412             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2413             ok(hres == S_OK, "Read failed: %08x\n", hres);
2414             ok(cb == 2, "cb=%u expected 2\n", cb);
2415         }
2416
2417         IInternetProtocol_Release(protocol);
2418     }
2419
2420     if(pCreateUri) {
2421         IInternetProtocolEx *protocolex;
2422         IUri *uri;
2423
2424         hres = pCreateUri(url, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
2425         ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
2426
2427         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocolEx, (void**)&protocolex);
2428         ok(hres == S_OK, "Could not get IInternetProtocolEx: %08x\n", hres);
2429
2430         if(file_protocol_start(NULL, NULL, protocolex, uri, TRUE)) {
2431             hres = IInternetProtocolEx_Read(protocolex, buf, 2, &cb);
2432             ok(hres == S_OK, "Read failed: %08x\n", hres);
2433             hres = IInternetProtocolEx_LockRequest(protocolex, 0);
2434             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2435             hres = IInternetProtocolEx_UnlockRequest(protocolex);
2436             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2437         }
2438
2439         IUri_Release(uri);
2440         IInternetProtocolEx_Release(protocolex);
2441     }else {
2442         win_skip("Skipping file protocol StartEx tests\n");
2443     }
2444
2445     IClassFactory_Release(factory);
2446 }
2447
2448 static void test_file_protocol_fail(void)
2449 {
2450     IInternetProtocol *protocol;
2451     HRESULT hres;
2452
2453     static const WCHAR index_url2[] =
2454         {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
2455
2456     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2457             &IID_IInternetProtocol, (void**)&protocol);
2458     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2459     if(FAILED(hres))
2460         return;
2461
2462     SET_EXPECT(GetBindInfo);
2463     expect_hrResult = MK_E_SYNTAX;
2464     hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
2465     ok(hres == MK_E_SYNTAX ||
2466        hres == E_INVALIDARG,
2467        "Start failed: %08x, expected MK_E_SYNTAX or E_INVALIDARG\n", hres);
2468     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2469
2470     SET_EXPECT(GetBindInfo);
2471     if(!(bindf & BINDF_FROMURLMON))
2472         SET_EXPECT(ReportProgress_DIRECTBIND);
2473     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2474     SET_EXPECT(ReportResult);
2475     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2476     hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
2477     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2478             "Start failed: %08x expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2479     CHECK_CALLED(GetBindInfo);
2480     if(!(bindf & BINDF_FROMURLMON))
2481         CHECK_CALLED(ReportProgress_DIRECTBIND);
2482     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2483     CHECK_CALLED(ReportResult);
2484
2485     IInternetProtocol_Release(protocol);
2486
2487     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2488             &IID_IInternetProtocol, (void**)&protocol);
2489     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2490     if(FAILED(hres))
2491         return;
2492
2493     SET_EXPECT(GetBindInfo);
2494     if(!(bindf & BINDF_FROMURLMON))
2495         SET_EXPECT(ReportProgress_DIRECTBIND);
2496     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2497     SET_EXPECT(ReportResult);
2498     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2499
2500     hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
2501     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2502             "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2503     CHECK_CALLED(GetBindInfo);
2504     if(!(bindf & BINDF_FROMURLMON))
2505         CHECK_CALLED(ReportProgress_DIRECTBIND);
2506     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2507     CHECK_CALLED(ReportResult);
2508
2509     SET_EXPECT(GetBindInfo);
2510     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
2511     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2512     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2513
2514     SET_EXPECT(GetBindInfo);
2515     hres = IInternetProtocol_Start(protocol, emptyW, &protocol_sink, &bind_info, 0, 0);
2516     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2517     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2518
2519     IInternetProtocol_Release(protocol);
2520 }
2521
2522 static void test_file_protocol(void) {
2523     WCHAR buf[INTERNET_MAX_URL_LENGTH], file_name_buf[MAX_PATH];
2524     DWORD size;
2525     ULONG len;
2526     HANDLE file;
2527
2528     static const WCHAR wszFile[] = {'f','i','l','e',':',0};
2529     static const WCHAR wszFile2[] = {'f','i','l','e',':','/','/',0};
2530     static const WCHAR wszFile3[] = {'f','i','l','e',':','/','/','/',0};
2531     static const WCHAR wszFile4[] = {'f','i','l','e',':','\\','\\',0};
2532     static const char html_doc[] = "<HTML></HTML>";
2533
2534     trace("Testing file protocol...\n");
2535     init_test(FILE_TEST, 0);
2536
2537     SetLastError(0xdeadbeef);
2538     file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2539             FILE_ATTRIBUTE_NORMAL, NULL);
2540     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
2541     if(file == INVALID_HANDLE_VALUE)
2542         return;
2543     WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
2544     CloseHandle(file);
2545
2546     file_name = wszIndexHtml;
2547     bindf = 0;
2548     test_file_protocol_url(index_url);
2549     bindf = BINDF_FROMURLMON;
2550     test_file_protocol_url(index_url);
2551     bindf = BINDF_FROMURLMON | BINDF_NEEDFILE;
2552     test_file_protocol_url(index_url);
2553
2554     memcpy(buf, wszFile, sizeof(wszFile));
2555     len = sizeof(wszFile)/sizeof(WCHAR)-1;
2556     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2557     buf[len++] = '\\';
2558     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2559
2560     file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
2561     bindf = 0;
2562     test_file_protocol_url(buf);
2563     bindf = BINDF_FROMURLMON;
2564     test_file_protocol_url(buf);
2565
2566     memcpy(buf, wszFile2, sizeof(wszFile2));
2567     len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
2568     file_name_buf[len++] = '\\';
2569     memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2570     lstrcpyW(buf+sizeof(wszFile2)/sizeof(WCHAR)-1, file_name_buf);
2571     file_name = file_name_buf;
2572     bindf = 0;
2573     test_file_protocol_url(buf);
2574     bindf = BINDF_FROMURLMON;
2575     test_file_protocol_url(buf);
2576
2577     buf[sizeof(wszFile2)/sizeof(WCHAR)] = '|';
2578     test_file_protocol_url(buf);
2579
2580     memcpy(buf, wszFile3, sizeof(wszFile3));
2581     len = sizeof(wszFile3)/sizeof(WCHAR)-1;
2582     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2583     buf[len++] = '\\';
2584     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2585
2586     file_name = buf + sizeof(wszFile3)/sizeof(WCHAR)-1;
2587     bindf = 0;
2588     test_file_protocol_url(buf);
2589     bindf = BINDF_FROMURLMON;
2590     test_file_protocol_url(buf);
2591
2592     memcpy(buf, wszFile4, sizeof(wszFile4));
2593     len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
2594     file_name_buf[len++] = '\\';
2595     memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2596     lstrcpyW(buf+sizeof(wszFile4)/sizeof(WCHAR)-1, file_name_buf);
2597     file_name = file_name_buf;
2598     bindf = 0;
2599     test_file_protocol_url(buf);
2600     bindf = BINDF_FROMURLMON;
2601     test_file_protocol_url(buf);
2602
2603     buf[sizeof(wszFile4)/sizeof(WCHAR)] = '|';
2604     test_file_protocol_url(buf);
2605
2606     DeleteFileW(wszIndexHtml);
2607
2608     bindf = 0;
2609     test_file_protocol_fail();
2610     bindf = BINDF_FROMURLMON;
2611     test_file_protocol_fail();
2612 }
2613
2614 static BOOL http_protocol_start(LPCWSTR url, BOOL use_iuri)
2615 {
2616     static BOOL got_user_agent = FALSE;
2617     IUri *uri = NULL;
2618     HRESULT hres;
2619
2620     if(use_iuri && pCreateUri) {
2621         hres = pCreateUri(url, 0, 0, &uri);
2622         ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
2623     }
2624
2625     SET_EXPECT(GetBindInfo);
2626     if (!(bindf & BINDF_FROMURLMON))
2627         SET_EXPECT(ReportProgress_DIRECTBIND);
2628     if(!got_user_agent)
2629         SET_EXPECT(GetBindString_USER_AGENT);
2630     SET_EXPECT(GetBindString_ACCEPT_MIMES);
2631     SET_EXPECT(QueryService_HttpNegotiate);
2632     SET_EXPECT(BeginningTransaction);
2633     SET_EXPECT(GetRootSecurityId);
2634     if(http_post_test) {
2635         SET_EXPECT(GetBindString_POST_COOKIE);
2636         if(http_post_test == TYMED_ISTREAM)
2637             SET_EXPECT(Stream_Seek);
2638     }
2639
2640     if(uri) {
2641         IInternetProtocolEx *protocolex;
2642
2643         hres = IInternetProtocol_QueryInterface(async_protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
2644         ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);
2645
2646         hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, 0, 0);
2647         ok(hres == S_OK, "Start failed: %08x\n", hres);
2648
2649         IInternetProtocolEx_Release(protocolex);
2650         IUri_Release(uri);
2651     }else {
2652         hres = IInternetProtocol_Start(async_protocol, url, &protocol_sink, &bind_info, 0, 0);
2653         ok(hres == S_OK, "Start failed: %08x\n", hres);
2654     }
2655     if(FAILED(hres))
2656         return FALSE;
2657
2658     CHECK_CALLED(GetBindInfo);
2659     if (!(bindf & BINDF_FROMURLMON))
2660         CHECK_CALLED(ReportProgress_DIRECTBIND);
2661     if (!got_user_agent)
2662     {
2663         CHECK_CALLED(GetBindString_USER_AGENT);
2664         got_user_agent = TRUE;
2665     }
2666     CHECK_CALLED(GetBindString_ACCEPT_MIMES);
2667     CHECK_CALLED(QueryService_HttpNegotiate);
2668     CHECK_CALLED(BeginningTransaction);
2669     /* GetRootSecurityId called on WinXP but not on Win98 */
2670     CLEAR_CALLED(GetRootSecurityId);
2671     if(http_post_test) {
2672         CHECK_CALLED(GetBindString_POST_COOKIE);
2673         if(http_post_test == TYMED_ISTREAM)
2674             CHECK_CALLED(Stream_Seek);
2675     }
2676
2677     return TRUE;
2678 }
2679
2680 static void test_protocol_terminate(IInternetProtocol *protocol)
2681 {
2682     BYTE buf[3600];
2683     DWORD cb;
2684     HRESULT hres;
2685
2686     hres = IInternetProtocol_LockRequest(protocol, 0);
2687     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2688
2689     hres = IInternetProtocol_Read(protocol, buf, 1, &cb);
2690     ok(hres == test_abort ? S_OK : S_FALSE, "Read failed: %08x\n", hres);
2691
2692     hres = IInternetProtocol_Terminate(protocol, 0);
2693     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2694
2695     /* This wait is to give the internet handles being freed in Terminate
2696      * enough time to actually terminate in all cases. Internet handles
2697      * terminate asynchronously and native reuses the main InternetOpen
2698      * handle. The only case in which this seems to be necessary is on
2699      * wine with native wininet and urlmon, resulting in the next time
2700      * test_http_protocol_url being called the first data notification actually
2701      * being an extra last data notification from the previous connection
2702      * about once out of every ten times. */
2703     Sleep(100);
2704
2705     hres = IInternetProtocol_UnlockRequest(protocol);
2706     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2707 }
2708
2709 static void test_http_info(IInternetProtocol *protocol)
2710 {
2711     IWinInetHttpInfo *info;
2712     HRESULT hres;
2713
2714     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&info);
2715     ok(hres == S_OK, "Could not get IWinInterHttpInfo iface: %08x\n", hres);
2716
2717     /* TODO */
2718
2719     IWinInetHttpInfo_Release(info);
2720 }
2721
2722 /* is_first refers to whether this is the first call to this function
2723  * _for this url_ */
2724 static void test_http_protocol_url(LPCWSTR url, int prot, DWORD flags, DWORD tymed)
2725 {
2726     IInternetProtocolInfo *protocol_info;
2727     IClassFactory *factory;
2728     IUnknown *unk;
2729     HRESULT hres;
2730
2731     init_test(prot, flags);
2732     http_url = url;
2733     http_post_test = tymed;
2734
2735     hres = CoGetClassObject(prot == HTTPS_TEST ? &CLSID_HttpSProtocol : &CLSID_HttpProtocol,
2736             CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2737     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2738     if(FAILED(hres))
2739         return;
2740
2741     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2742     ok(hres == E_NOINTERFACE,
2743         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
2744         hres);
2745
2746     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2747     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2748     IUnknown_Release(unk);
2749     if(FAILED(hres))
2750         return;
2751
2752     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2753                                         (void**)&async_protocol);
2754     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2755     if(SUCCEEDED(hres)) {
2756         BYTE buf[3600];
2757         DWORD cb;
2758         ULONG ref;
2759
2760         test_priority(async_protocol);
2761         test_http_info(async_protocol);
2762
2763         SET_EXPECT(ReportProgress_COOKIE_SENT);
2764         SET_EXPECT(ReportProgress_FINDINGRESOURCE);
2765         SET_EXPECT(ReportProgress_CONNECTING);
2766         SET_EXPECT(ReportProgress_SENDINGREQUEST);
2767         if(test_redirect)
2768             SET_EXPECT(ReportProgress_REDIRECTING);
2769         SET_EXPECT(ReportProgress_PROXYDETECTING);
2770         if(prot == HTTP_TEST)
2771             SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2772         else
2773             SET_EXPECT(QueryService_HttpSecurity);
2774         if(!(bindf & BINDF_FROMURLMON)) {
2775             SET_EXPECT(OnResponse);
2776             SET_EXPECT(ReportProgress_RAWMIMETYPE);
2777             SET_EXPECT(ReportData);
2778         } else {
2779             SET_EXPECT(Switch);
2780         }
2781
2782         if(!http_protocol_start(url, (flags & TEST_USEIURI) != 0))
2783             return;
2784
2785         if(!direct_read && !test_abort)
2786             SET_EXPECT(ReportResult);
2787         expect_hrResult = test_abort ? E_ABORT : S_OK;
2788
2789         if(direct_read) {
2790             SET_EXPECT(Switch);
2791             while(wait_for_switch) {
2792                 WaitForSingleObject(event_continue, INFINITE);
2793                 CHECK_CALLED(Switch); /* Set in ReportData */
2794                 call_continue(&continue_protdata);
2795                 SetEvent(event_continue_done);
2796             }
2797         }else {
2798             hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2799             ok((hres == E_PENDING && cb==0) ||
2800                (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2801
2802             WaitForSingleObject(event_complete, INFINITE);
2803             if(bindf & BINDF_FROMURLMON)
2804                 CHECK_CALLED(Switch);
2805             else
2806                 CHECK_CALLED(ReportData);
2807             if(prot == HTTPS_TEST)
2808                 CLEAR_CALLED(QueryService_HttpSecurity);
2809
2810             while(1) {
2811                 if(bindf & BINDF_FROMURLMON)
2812                     SET_EXPECT(Switch);
2813                 else
2814                     SET_EXPECT(ReportData);
2815                 hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
2816                 if(hres == E_PENDING) {
2817                     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2818                     ok((hres == E_PENDING && cb==0) ||
2819                        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2820                     WaitForSingleObject(event_complete, INFINITE);
2821                     if(bindf & BINDF_FROMURLMON)
2822                         CHECK_CALLED(Switch);
2823                     else
2824                         CHECK_CALLED(ReportData);
2825
2826                     if(test_abort) {
2827                         HRESULT hres;
2828
2829                         SET_EXPECT(ReportResult);
2830                         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2831                         ok(hres == S_OK, "Abort failed: %08x\n", hres);
2832                         CHECK_CALLED(ReportResult);
2833
2834                         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2835                         ok(hres == INET_E_RESULT_DISPATCHED, "Abort failed: %08x\n", hres);
2836                         break;
2837                     }
2838                 }else {
2839                     if(bindf & BINDF_FROMURLMON)
2840                         CHECK_NOT_CALLED(Switch);
2841                     else
2842                         CHECK_NOT_CALLED(ReportData);
2843                     if(cb == 0) break;
2844                 }
2845             }
2846             if(!test_abort) {
2847                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2848                 CHECK_CALLED(ReportResult);
2849             }
2850         }
2851         if(prot == HTTPS_TEST)
2852             CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
2853
2854         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2855         ok(hres == INET_E_RESULT_DISPATCHED, "Abort failed: %08x\n", hres);
2856
2857         test_protocol_terminate(async_protocol);
2858
2859         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2860         ok(hres == S_OK, "Abort failed: %08x\n", hres);
2861
2862         ref = IInternetProtocol_Release(async_protocol);
2863         ok(!ref, "ref=%x\n", ref);
2864     }
2865
2866     IClassFactory_Release(factory);
2867 }
2868
2869 static void test_http_protocol(void)
2870 {
2871     static const WCHAR posttest_url[] =
2872         {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
2873          't','e','s','t','s','/','p','o','s','t','.','p','h','p',0};
2874     static const WCHAR redirect_url[] =
2875         {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
2876          't','e','s','t','s','/','r','e','d','i','r','e','c','t',0};
2877     static const WCHAR winetest_url[] =
2878         {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
2879          't','e','s','t','s','/','d','a','t','a','.','p','h','p',0};
2880
2881     trace("Testing http protocol (not from urlmon)...\n");
2882     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
2883     test_http_protocol_url(winetest_url, HTTP_TEST, TEST_FIRST_HTTP, TYMED_NULL);
2884
2885     trace("Testing http protocol (from urlmon)...\n");
2886     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2887     test_http_protocol_url(winetest_url, HTTP_TEST, 0, TYMED_NULL);
2888
2889     trace("Testing http protocol (to file)...\n");
2890     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
2891     test_http_protocol_url(winetest_url, HTTP_TEST, 0, TYMED_NULL);
2892
2893     trace("Testing http protocol (post data)...\n");
2894     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2895     test_http_protocol_url(posttest_url, HTTP_TEST, TEST_FIRST_HTTP|TEST_POST, TYMED_HGLOBAL);
2896
2897     trace("Testing http protocol (post data stream)...\n");
2898     test_http_protocol_url(posttest_url, HTTP_TEST, TEST_FIRST_HTTP|TEST_POST|TEST_ASYNCREQ, TYMED_ISTREAM);
2899
2900     trace("Testing http protocol (direct read)...\n");
2901     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2902     test_http_protocol_url(winetest_url, HTTP_TEST, TEST_DIRECT_READ|TEST_USEIURI, TYMED_NULL);
2903
2904     trace("Testing http protocol (redirected)...\n");
2905     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2906     test_http_protocol_url(redirect_url, HTTP_TEST, TEST_REDIRECT, TYMED_NULL);
2907
2908     trace("Testing http protocol abort...\n");
2909     test_http_protocol_url(winetest_url, HTTP_TEST, TEST_ABORT, TYMED_NULL);
2910
2911     test_early_abort(&CLSID_HttpProtocol);
2912     test_early_abort(&CLSID_HttpSProtocol);
2913 }
2914
2915 static void test_https_protocol(void)
2916 {
2917     static const WCHAR codeweavers_url[] =
2918         {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
2919          '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0};
2920
2921     trace("Testing https protocol (from urlmon)...\n");
2922     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2923     test_http_protocol_url(codeweavers_url, HTTPS_TEST, TEST_FIRST_HTTP, TYMED_NULL);
2924 }
2925
2926
2927 static void test_ftp_protocol(void)
2928 {
2929     IInternetProtocolInfo *protocol_info;
2930     IClassFactory *factory;
2931     IUnknown *unk;
2932     BYTE buf[4096];
2933     ULONG ref;
2934     DWORD cb;
2935     HRESULT hres;
2936
2937     static const WCHAR ftp_urlW[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
2938     '/','p','u','b','/','o','t','h','e','r','/',
2939     'w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0};
2940
2941     trace("Testing ftp protocol...\n");
2942
2943     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2944     state = STATE_STARTDOWNLOADING;
2945     tested_protocol = FTP_TEST;
2946     first_data_notif = TRUE;
2947     expect_hrResult = E_PENDING;
2948
2949     hres = CoGetClassObject(&CLSID_FtpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2950     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2951     if(FAILED(hres))
2952         return;
2953
2954     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2955     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2956
2957     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2958     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2959     IUnknown_Release(unk);
2960     if(FAILED(hres))
2961         return;
2962
2963     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2964                                         (void**)&async_protocol);
2965     IClassFactory_Release(factory);
2966     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2967
2968     test_priority(async_protocol);
2969     test_http_info(async_protocol);
2970
2971     SET_EXPECT(GetBindInfo);
2972     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
2973     SET_EXPECT(ReportProgress_CONNECTING);
2974     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2975     SET_EXPECT(Switch);
2976
2977     hres = IInternetProtocol_Start(async_protocol, ftp_urlW, &protocol_sink, &bind_info, 0, 0);
2978     ok(hres == S_OK, "Start failed: %08x\n", hres);
2979     CHECK_CALLED(GetBindInfo);
2980
2981     SET_EXPECT(ReportResult);
2982
2983     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2984     ok((hres == E_PENDING && cb==0) ||
2985        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2986
2987     WaitForSingleObject(event_complete, INFINITE);
2988
2989     while(1) {
2990         hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
2991         if(hres == E_PENDING)
2992             WaitForSingleObject(event_complete, INFINITE);
2993         else
2994             if(cb == 0) break;
2995     }
2996
2997     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2998     CHECK_CALLED(ReportResult);
2999     CHECK_CALLED(Switch);
3000
3001     test_protocol_terminate(async_protocol);
3002
3003     if(pCreateUri) {
3004         IInternetProtocolEx *protocolex;
3005
3006         hres = IInternetProtocol_QueryInterface(async_protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
3007         ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);
3008         IInternetProtocolEx_Release(protocolex);
3009     }
3010
3011     ref = IInternetProtocol_Release(async_protocol);
3012     ok(!ref, "ref=%d\n", ref);
3013
3014     test_early_abort(&CLSID_FtpProtocol);
3015 }
3016
3017 static void test_gopher_protocol(void)
3018 {
3019     IInternetProtocolInfo *protocol_info;
3020     IClassFactory *factory;
3021     IUnknown *unk;
3022     HRESULT hres;
3023
3024     trace("Testing gopher protocol...\n");
3025
3026     hres = CoGetClassObject(&CLSID_GopherProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
3027     ok(hres == S_OK ||
3028        hres == REGDB_E_CLASSNOTREG, /* Gopher protocol has been removed as of Vista */
3029        "CoGetClassObject failed: %08x\n", hres);
3030     if(FAILED(hres))
3031         return;
3032
3033     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
3034     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
3035
3036     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
3037     ok(hres == S_OK, "Could not get IClassFactory interface\n");
3038     IUnknown_Release(unk);
3039     if(FAILED(hres))
3040         return;
3041
3042     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
3043                                         (void**)&async_protocol);
3044     IClassFactory_Release(factory);
3045     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
3046
3047     test_priority(async_protocol);
3048
3049     IInternetProtocol_Release(async_protocol);
3050
3051     test_early_abort(&CLSID_GopherProtocol);
3052 }
3053
3054 static void test_mk_protocol(void)
3055 {
3056     IInternetProtocolInfo *protocol_info;
3057     IInternetProtocol *protocol;
3058     IClassFactory *factory;
3059     IUnknown *unk;
3060     HRESULT hres;
3061
3062     static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
3063                                        ':',':','/','t','e','s','t','.','h','t','m','l',0};
3064     static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};
3065
3066     trace("Testing mk protocol...\n");
3067     init_test(MK_TEST, 0);
3068
3069     hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
3070             &IID_IUnknown, (void**)&unk);
3071     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
3072
3073     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
3074     ok(hres == E_NOINTERFACE,
3075         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
3076         hres);
3077
3078     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
3079     ok(hres == S_OK, "Could not get IClassFactory interface\n");
3080     IUnknown_Release(unk);
3081     if(FAILED(hres))
3082         return;
3083
3084     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
3085                                         (void**)&protocol);
3086     IClassFactory_Release(factory);
3087     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
3088
3089     SET_EXPECT(GetBindInfo);
3090     hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
3091     ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
3092        "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
3093     CLEAR_CALLED(GetBindInfo);
3094
3095     SET_EXPECT(GetBindInfo);
3096     SET_EXPECT(ReportProgress_DIRECTBIND);
3097     SET_EXPECT(ReportProgress_SENDINGREQUEST);
3098     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
3099     SET_EXPECT(ReportResult);
3100     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
3101
3102     hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
3103     ok(hres == INET_E_RESOURCE_NOT_FOUND ||
3104        hres == INET_E_INVALID_URL, /* win2k3 */
3105        "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
3106
3107     if (hres == INET_E_RESOURCE_NOT_FOUND) {
3108         CHECK_CALLED(GetBindInfo);
3109         CLEAR_CALLED(ReportProgress_DIRECTBIND);
3110         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
3111         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
3112         CHECK_CALLED(ReportResult);
3113     }else {
3114         CLEAR_CALLED(GetBindInfo);
3115         CLEAR_CALLED(ReportProgress_DIRECTBIND);
3116         CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
3117         CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
3118         CLEAR_CALLED(ReportResult);
3119     }
3120
3121     IInternetProtocol_Release(protocol);
3122 }
3123
3124 static void test_CreateBinding(void)
3125 {
3126     IInternetProtocol *protocol;
3127     IInternetPriority *priority;
3128     IInternetSession *session;
3129     IWinInetHttpInfo *http_info;
3130     IWinInetInfo *inet_info;
3131     LONG p;
3132     BYTE buf[1000];
3133     DWORD read;
3134     HRESULT hres;
3135
3136     static const WCHAR test_url[] =
3137         {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
3138     static const WCHAR wsz_test[] = {'t','e','s','t',0};
3139
3140     trace("Testing CreateBinding...\n");
3141     init_test(BIND_TEST, TEST_BINDING);
3142
3143     hres = pCoInternetGetSession(0, &session, 0);
3144     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
3145
3146     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
3147     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
3148
3149     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
3150     binding_protocol = protocol;
3151     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3152     ok(protocol != NULL, "protocol == NULL\n");
3153
3154     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
3155     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
3156
3157     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
3158     ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);
3159
3160     hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
3161     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
3162     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
3163     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
3164     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
3165     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
3166
3167     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
3168     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
3169
3170     p = 0xdeadbeef;
3171     hres = IInternetPriority_GetPriority(priority, &p);
3172     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
3173     ok(!p, "p=%d\n", p);
3174
3175     ex_priority = 100;
3176     hres = IInternetPriority_SetPriority(priority, 100);
3177     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
3178
3179     p = 0xdeadbeef;
3180     hres = IInternetPriority_GetPriority(priority, &p);
3181     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
3182     ok(p == 100, "p=%d\n", p);
3183
3184     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
3185     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3186
3187     SET_EXPECT(QueryService_InternetProtocol);
3188     SET_EXPECT(CreateInstance);
3189     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
3190     SET_EXPECT(SetPriority);
3191     SET_EXPECT(Start);
3192
3193     expect_hrResult = S_OK;
3194     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
3195     ok(hres == S_OK, "Start failed: %08x\n", hres);
3196
3197     CHECK_CALLED(QueryService_InternetProtocol);
3198     CHECK_CALLED(CreateInstance);
3199     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
3200     CHECK_CALLED(SetPriority);
3201     CHECK_CALLED(Start);
3202
3203     SET_EXPECT(QueryInterface_IWinInetInfo);
3204     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
3205     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3206     CHECK_CALLED(QueryInterface_IWinInetInfo);
3207
3208     SET_EXPECT(QueryInterface_IWinInetInfo);
3209     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
3210     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3211     CHECK_CALLED(QueryInterface_IWinInetInfo);
3212
3213     SET_EXPECT(QueryInterface_IWinInetHttpInfo);
3214     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&http_info);
3215     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3216     CHECK_CALLED(QueryInterface_IWinInetHttpInfo);
3217
3218     SET_EXPECT(Read);
3219     read = 0xdeadbeef;
3220     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
3221     ok(hres == S_OK, "Read failed: %08x\n", hres);
3222     ok(read == 100, "read = %d\n", read);
3223     CHECK_CALLED(Read);
3224
3225     SET_EXPECT(Read);
3226     read = 0xdeadbeef;
3227     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
3228     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
3229     ok(!read, "read = %d\n", read);
3230     CHECK_CALLED(Read);
3231
3232     p = 0xdeadbeef;
3233     hres = IInternetPriority_GetPriority(priority, &p);
3234     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
3235     ok(p == 100, "p=%d\n", p);
3236
3237     hres = IInternetPriority_SetPriority(priority, 101);
3238     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
3239
3240     SET_EXPECT(Terminate);
3241     hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
3242     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
3243     CHECK_CALLED(Terminate);
3244
3245     SET_EXPECT(Continue);
3246     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
3247     ok(hres == S_OK, "Switch failed: %08x\n", hres);
3248     CHECK_CALLED(Continue);
3249
3250     hres = IInternetProtocolSink_ReportProgress(binding_sink,
3251             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
3252     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
3253
3254     hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
3255     ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);
3256
3257     hres = IInternetProtocolSink_ReportData(binding_sink, 0, 0, 0);
3258     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
3259
3260     IInternetProtocolSink_Release(binding_sink);
3261     IInternetPriority_Release(priority);
3262     IInternetBindInfo_Release(prot_bind_info);
3263     IInternetProtocol_Release(protocol);
3264
3265     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
3266     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3267     ok(protocol != NULL, "protocol == NULL\n");
3268
3269     hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
3270     ok(hres == S_OK, "Abort failed: %08x\n", hres);
3271
3272     hres = IInternetProtocol_Abort(protocol, E_FAIL, 0);
3273     ok(hres == S_OK, "Abort failed: %08x\n", hres);
3274
3275     IInternetProtocol_Release(protocol);
3276
3277     hres = IInternetSession_UnregisterNameSpace(session, &ClassFactory, wsz_test);
3278     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
3279
3280     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
3281     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3282     ok(protocol != NULL, "protocol == NULL\n");
3283
3284     SET_EXPECT(QueryService_InternetProtocol);
3285     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
3286     ok(hres == MK_E_SYNTAX, "Start failed: %08x, expected MK_E_SYNTAX\n", hres);
3287     CHECK_CALLED(QueryService_InternetProtocol);
3288
3289     IInternetProtocol_Release(protocol);
3290
3291     IInternetSession_Release(session);
3292 }
3293
3294 static void test_binding(int prot, DWORD grf_pi, DWORD test_flags)
3295 {
3296     IInternetProtocolEx *protocolex = NULL;
3297     IInternetProtocol *protocol;
3298     IInternetSession *session;
3299     IUri *uri = NULL;
3300     ULONG ref;
3301     HRESULT hres;
3302
3303     pi = grf_pi;
3304
3305     init_test(prot, test_flags|TEST_BINDING);
3306
3307     hres = pCoInternetGetSession(0, &session, 0);
3308     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
3309
3310     if(test_flags & TEST_EMULATEPROT) {
3311         hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
3312         ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
3313     }
3314
3315     hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
3316     binding_protocol = protocol;
3317     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3318     ok(protocol != NULL, "protocol == NULL\n");
3319
3320     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
3321     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
3322
3323     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
3324     ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
3325
3326     if(test_flags & TEST_USEIURI) {
3327         hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
3328         ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);
3329
3330         hres = pCreateUri(binding_urls[prot], Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
3331         ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
3332     }
3333
3334     ex_priority = 0;
3335     SET_EXPECT(QueryService_InternetProtocol);
3336     SET_EXPECT(CreateInstance);
3337     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
3338     SET_EXPECT(SetPriority);
3339     if(impl_protex)
3340         SET_EXPECT(StartEx);
3341     else
3342         SET_EXPECT(Start);
3343
3344     expect_hrResult = S_OK;
3345
3346     if(protocolex) {
3347         hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, pi, 0);
3348         ok(hres == S_OK, "StartEx failed: %08x\n", hres);
3349     }else {
3350         hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, pi, 0);
3351         ok(hres == S_OK, "Start failed: %08x\n", hres);
3352     }
3353
3354     CHECK_CALLED(QueryService_InternetProtocol);
3355     CHECK_CALLED(CreateInstance);
3356     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
3357     CHECK_CALLED(SetPriority);
3358     if(impl_protex)
3359         CHECK_CALLED(StartEx);
3360     else
3361         CHECK_CALLED(Start);
3362
3363     if(protocolex)
3364         IInternetProtocolEx_Release(protocolex);
3365     if(uri)
3366         IUri_Release(uri);
3367
3368     if(prot == HTTP_TEST || prot == HTTPS_TEST) {
3369         while(prot_state < 4) {
3370             WaitForSingleObject(event_complete, INFINITE);
3371             if(mimefilter_test && filtered_protocol) {
3372                 SET_EXPECT(Continue);
3373                 IInternetProtocol_Continue(filtered_protocol, pdata);
3374                 CHECK_CALLED(Continue);
3375             }else {
3376                 SET_EXPECT(Continue);
3377                 IInternetProtocol_Continue(protocol, pdata);
3378                 CHECK_CALLED(Continue);
3379             }
3380             if(test_abort && prot_state == 2) {
3381                 SET_EXPECT(Abort);
3382                 hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
3383                 ok(hres == S_OK, "Abort failed: %08x\n", hres);
3384                 CHECK_CALLED(Abort);
3385
3386                 hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
3387                 ok(hres == S_OK, "Abort failed: %08x\n", hres);
3388                 SetEvent(event_complete2);
3389                 break;
3390             }
3391             SetEvent(event_complete2);
3392         }
3393         if(direct_read)
3394             CHECK_CALLED(ReportData); /* Set in ReportResult */
3395         WaitForSingleObject(event_complete, INFINITE);
3396     }else {
3397         if(mimefilter_test)
3398             SET_EXPECT(MimeFilter_LockRequest);
3399         else
3400             SET_EXPECT(LockRequest);
3401         hres = IInternetProtocol_LockRequest(protocol, 0);
3402         ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
3403         if(mimefilter_test)
3404             CHECK_CALLED(MimeFilter_LockRequest);
3405         else
3406             CHECK_CALLED(LockRequest);
3407
3408         if(mimefilter_test)
3409             SET_EXPECT(MimeFilter_UnlockRequest);
3410         else
3411             SET_EXPECT(UnlockRequest);
3412         hres = IInternetProtocol_UnlockRequest(protocol);
3413         ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
3414         if(mimefilter_test)
3415             CHECK_CALLED(MimeFilter_UnlockRequest);
3416         else
3417             CHECK_CALLED(UnlockRequest);
3418     }
3419
3420     if(mimefilter_test)
3421         SET_EXPECT(MimeFilter_Terminate);
3422     else
3423         SET_EXPECT(Terminate);
3424     hres = IInternetProtocol_Terminate(protocol, 0);
3425     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
3426     if(mimefilter_test)
3427         CLEAR_CALLED(MimeFilter_Terminate);
3428     else
3429         CHECK_CALLED(Terminate);
3430
3431     if(filtered_protocol)
3432         IInternetProtocol_Release(filtered_protocol);
3433     IInternetBindInfo_Release(prot_bind_info);
3434     IInternetProtocolSink_Release(binding_sink);
3435     ref = IInternetProtocol_Release(protocol);
3436     ok(!ref, "ref=%u, expected 0\n", ref);
3437
3438     if(test_flags & TEST_EMULATEPROT) {
3439         hres = IInternetSession_UnregisterNameSpace(session, &ClassFactory, protocol_names[prot]);
3440         ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
3441     }
3442
3443     IInternetSession_Release(session);
3444 }
3445
3446 START_TEST(protocol)
3447 {
3448     HMODULE hurlmon;
3449
3450     hurlmon = GetModuleHandle("urlmon.dll");
3451     pCoInternetGetSession = (void*) GetProcAddress(hurlmon, "CoInternetGetSession");
3452     pReleaseBindInfo = (void*) GetProcAddress(hurlmon, "ReleaseBindInfo");
3453     pCreateUri = (void*) GetProcAddress(hurlmon, "CreateUri");
3454
3455     if(!GetProcAddress(hurlmon, "CompareSecurityIds")) {
3456         win_skip("Various needed functions not present, too old IE\n");
3457         return;
3458     }
3459
3460     if(!pCreateUri)
3461         win_skip("CreateUri not supported\n");
3462
3463     OleInitialize(NULL);
3464
3465     event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
3466     event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
3467     event_continue = CreateEvent(NULL, FALSE, FALSE, NULL);
3468     event_continue_done = CreateEvent(NULL, FALSE, FALSE, NULL);
3469     thread_id = GetCurrentThreadId();
3470
3471     test_file_protocol();
3472     test_http_protocol();
3473     test_https_protocol();
3474     test_ftp_protocol();
3475     test_gopher_protocol();
3476     test_mk_protocol();
3477     test_CreateBinding();
3478
3479     bindf &= ~BINDF_FROMURLMON;
3480     trace("Testing file binding (mime verification, emulate prot)...\n");
3481     test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3482     trace("Testing http binding (mime verification, emulate prot)...\n");
3483     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3484     trace("Testing its binding (mime verification, emulate prot)...\n");
3485     test_binding(ITS_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3486     trace("Testing http binding (mime verification, emulate prot, short read, direct read)...\n");
3487     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_SHORT_READ|TEST_DIRECT_READ);
3488     trace("Testing file binding (mime verification, emulate prot, mime filter)...\n");
3489     test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_FILTER);
3490     trace("Testing http binding (mime verification, emulate prot, mime filter)...\n");
3491     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_FILTER);
3492     trace("Testing http binding (mime verification, emulate prot, direct read)...\n");
3493     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_DIRECT_READ);
3494     trace("Testing http binding (mime verification, emulate prot, abort)...\n");
3495     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_ABORT);
3496     if(pCreateUri) {
3497         trace("Testing file binding (use IUri, mime verification, emulate prot)...\n");
3498         test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_USEIURI);
3499         trace("Testing file binding (use IUri, impl StartEx, mime verification, emulate prot)...\n");
3500         test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_USEIURI|TEST_IMPLPROTEX);
3501         trace("Testing file binding (impl StartEx, mime verification, emulate prot)...\n");
3502         test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_IMPLPROTEX);
3503     }
3504
3505     CloseHandle(event_complete);
3506     CloseHandle(event_complete2);
3507     CloseHandle(event_continue);
3508     CloseHandle(event_continue_done);
3509
3510     OleUninitialize();
3511 }