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