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