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