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