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