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