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