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