ws2_32: name is never NULL as array (Coverity).
[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 || test_abort)
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 || test_abort)
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 register_filter(BOOL do_register)
2214 {
2215     IInternetSession *session;
2216     HRESULT hres;
2217
2218     static const WCHAR gzipW[] = {'g','z','i','p',0};
2219
2220     hres = pCoInternetGetSession(0, &session, 0);
2221     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2222
2223     if(do_register) {
2224         hres = IInternetSession_RegisterMimeFilter(session, &mimefilter_cf, &IID_IInternetProtocol, gzipW);
2225         ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
2226     }else {
2227         hres = IInternetSession_UnregisterMimeFilter(session, &mimefilter_cf, gzipW);
2228         ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
2229     }
2230
2231     IInternetSession_Release(session);
2232 }
2233
2234 static void init_test(int prot, DWORD flags)
2235 {
2236     tested_protocol = prot;
2237     binding_test = (flags & TEST_BINDING) != 0;
2238     first_data_notif = TRUE;
2239     prot_read = 0;
2240     prot_state = 0;
2241     async_read_pending = TRUE;
2242     mimefilter_test = (flags & TEST_FILTER) != 0;
2243     filter_state = 0;
2244     post_stream_read = 0;
2245     ResetEvent(event_complete);
2246     ResetEvent(event_complete2);
2247     ResetEvent(event_continue);
2248     ResetEvent(event_continue_done);
2249     async_protocol = binding_protocol = filtered_protocol = NULL;
2250     filtered_sink = NULL;
2251     http_is_first = (flags & TEST_FIRST_HTTP) != 0;
2252     first_data_notif = TRUE;
2253     state = STATE_CONNECTING;
2254     test_async_req = (flags & TEST_ASYNCREQ) != 0;
2255     direct_read = (flags & TEST_DIRECT_READ) != 0;
2256     emulate_prot = (flags & TEST_EMULATEPROT) != 0;
2257     wait_for_switch = TRUE;
2258     short_read = (flags & TEST_SHORT_READ) != 0;
2259     http_post_test = TYMED_NULL;
2260     test_redirect = (flags & TEST_REDIRECT) != 0;
2261     test_abort = (flags & TEST_ABORT) != 0;
2262     impl_protex = (flags & TEST_IMPLPROTEX) != 0;
2263
2264     register_filter(mimefilter_test);
2265 }
2266
2267 static void test_priority(IInternetProtocol *protocol)
2268 {
2269     IInternetPriority *priority;
2270     LONG pr;
2271     HRESULT hres;
2272
2273     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority,
2274                                             (void**)&priority);
2275     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
2276     if(FAILED(hres))
2277         return;
2278
2279     hres = IInternetPriority_GetPriority(priority, &pr);
2280     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2281     ok(pr == 0, "pr=%d, expected 0\n", pr);
2282
2283     hres = IInternetPriority_SetPriority(priority, 1);
2284     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2285
2286     hres = IInternetPriority_GetPriority(priority, &pr);
2287     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2288     ok(pr == 1, "pr=%d, expected 1\n", pr);
2289
2290     IInternetPriority_Release(priority);
2291 }
2292
2293 static void test_early_abort(const CLSID *clsid)
2294 {
2295     IInternetProtocol *protocol;
2296     HRESULT hres;
2297
2298     hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2299             &IID_IInternetProtocol, (void**)&protocol);
2300     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2301
2302     hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
2303     ok(hres == S_OK, "Abort failed: %08x\n", hres);
2304
2305     hres = IInternetProtocol_Abort(protocol, E_FAIL, 0);
2306     ok(hres == S_OK, "Abort failed: %08x\n", hres);
2307
2308     IInternetProtocol_Release(protocol);
2309 }
2310
2311 static BOOL file_protocol_start(IInternetProtocol *protocol, LPCWSTR url,
2312         IInternetProtocolEx *protocolex, IUri *uri, BOOL is_first)
2313 {
2314     HRESULT hres;
2315
2316     SET_EXPECT(GetBindInfo);
2317     if(!(bindf & BINDF_FROMURLMON))
2318        SET_EXPECT(ReportProgress_DIRECTBIND);
2319     if(is_first) {
2320         SET_EXPECT(ReportProgress_SENDINGREQUEST);
2321         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2322         if(bindf & BINDF_FROMURLMON)
2323             SET_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
2324         else
2325             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
2326     }
2327     SET_EXPECT(ReportData);
2328     if(is_first)
2329         SET_EXPECT(ReportResult);
2330
2331     expect_hrResult = S_OK;
2332
2333     if(protocolex) {
2334         hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, 0, 0);
2335         ok(hres == S_OK, "StartEx failed: %08x\n", hres);
2336     }else {
2337         hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
2338         if(hres == INET_E_RESOURCE_NOT_FOUND) {
2339             win_skip("Start failed\n");
2340             return FALSE;
2341         }
2342         ok(hres == S_OK, "Start failed: %08x\n", hres);
2343     }
2344
2345     CHECK_CALLED(GetBindInfo);
2346     if(!(bindf & BINDF_FROMURLMON))
2347        CHECK_CALLED(ReportProgress_DIRECTBIND);
2348     if(is_first) {
2349         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2350         CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
2351         if(bindf & BINDF_FROMURLMON)
2352             CHECK_CALLED(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
2353         else
2354             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
2355     }
2356     CHECK_CALLED(ReportData);
2357     if(is_first)
2358         CHECK_CALLED(ReportResult);
2359
2360     return TRUE;
2361 }
2362
2363 static void test_file_protocol_url(LPCWSTR url)
2364 {
2365     IInternetProtocolInfo *protocol_info;
2366     IUnknown *unk;
2367     IClassFactory *factory;
2368     IInternetProtocol *protocol;
2369     BYTE buf[512];
2370     ULONG cb;
2371     HRESULT hres;
2372
2373     hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
2374             &IID_IUnknown, (void**)&unk);
2375     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2376     if(FAILED(hres))
2377         return;
2378
2379     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2380     ok(hres == E_NOINTERFACE,
2381             "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2382
2383     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2384     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2385     IUnknown_Release(unk);
2386     if(FAILED(hres))
2387         return;
2388
2389     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
2390     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2391
2392     if(SUCCEEDED(hres)) {
2393         if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
2394             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2395             ok(hres == S_OK, "Read failed: %08x\n", hres);
2396             ok(cb == 2, "cb=%u expected 2\n", cb);
2397             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
2398             ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2399             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
2400             ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
2401             ok(cb == 0, "cb=%u expected 0\n", cb);
2402             hres = IInternetProtocol_UnlockRequest(protocol);
2403             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2404         }
2405
2406         if(file_protocol_start(protocol, url, NULL, NULL, FALSE)) {
2407             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2408             ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2409             hres = IInternetProtocol_LockRequest(protocol, 0);
2410             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2411             hres = IInternetProtocol_UnlockRequest(protocol);
2412             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2413         }
2414
2415         IInternetProtocol_Release(protocol);
2416     }
2417
2418     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
2419     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2420     if(SUCCEEDED(hres)) {
2421         if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
2422             hres = IInternetProtocol_LockRequest(protocol, 0);
2423             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2424             hres = IInternetProtocol_Terminate(protocol, 0);
2425             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2426             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2427             ok(hres == S_OK, "Read failed: %08x\n\n", hres);
2428             hres = IInternetProtocol_UnlockRequest(protocol);
2429             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2430             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2431             ok(hres == S_OK, "Read failed: %08x\n", hres);
2432             hres = IInternetProtocol_Terminate(protocol, 0);
2433             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2434         }
2435
2436         IInternetProtocol_Release(protocol);
2437     }
2438
2439     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
2440     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2441     if(SUCCEEDED(hres)) {
2442         if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
2443             hres = IInternetProtocol_Terminate(protocol, 0);
2444             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2445             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
2446             ok(hres == S_OK, "Read failed: %08x\n", hres);
2447             ok(cb == 2, "cb=%u expected 2\n", cb);
2448         }
2449
2450         IInternetProtocol_Release(protocol);
2451     }
2452
2453     if(pCreateUri) {
2454         IInternetProtocolEx *protocolex;
2455         IUri *uri;
2456
2457         hres = pCreateUri(url, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
2458         ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
2459
2460         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocolEx, (void**)&protocolex);
2461         ok(hres == S_OK, "Could not get IInternetProtocolEx: %08x\n", hres);
2462
2463         if(file_protocol_start(NULL, NULL, protocolex, uri, TRUE)) {
2464             hres = IInternetProtocolEx_Read(protocolex, buf, 2, &cb);
2465             ok(hres == S_OK, "Read failed: %08x\n", hres);
2466             hres = IInternetProtocolEx_LockRequest(protocolex, 0);
2467             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2468             hres = IInternetProtocolEx_UnlockRequest(protocolex);
2469             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2470         }
2471
2472         IUri_Release(uri);
2473         IInternetProtocolEx_Release(protocolex);
2474     }else {
2475         win_skip("Skipping file protocol StartEx tests\n");
2476     }
2477
2478     IClassFactory_Release(factory);
2479 }
2480
2481 static void test_file_protocol_fail(void)
2482 {
2483     IInternetProtocol *protocol;
2484     HRESULT hres;
2485
2486     static const WCHAR index_url2[] =
2487         {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
2488
2489     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2490             &IID_IInternetProtocol, (void**)&protocol);
2491     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2492     if(FAILED(hres))
2493         return;
2494
2495     SET_EXPECT(GetBindInfo);
2496     expect_hrResult = MK_E_SYNTAX;
2497     hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
2498     ok(hres == MK_E_SYNTAX ||
2499        hres == E_INVALIDARG,
2500        "Start failed: %08x, expected MK_E_SYNTAX or E_INVALIDARG\n", hres);
2501     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
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     hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
2510     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2511             "Start failed: %08x expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2512     CHECK_CALLED(GetBindInfo);
2513     if(!(bindf & BINDF_FROMURLMON))
2514         CHECK_CALLED(ReportProgress_DIRECTBIND);
2515     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2516     CHECK_CALLED(ReportResult);
2517
2518     IInternetProtocol_Release(protocol);
2519
2520     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2521             &IID_IInternetProtocol, (void**)&protocol);
2522     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2523     if(FAILED(hres))
2524         return;
2525
2526     SET_EXPECT(GetBindInfo);
2527     if(!(bindf & BINDF_FROMURLMON))
2528         SET_EXPECT(ReportProgress_DIRECTBIND);
2529     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2530     SET_EXPECT(ReportResult);
2531     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2532
2533     hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
2534     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2535             "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2536     CHECK_CALLED(GetBindInfo);
2537     if(!(bindf & BINDF_FROMURLMON))
2538         CHECK_CALLED(ReportProgress_DIRECTBIND);
2539     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2540     CHECK_CALLED(ReportResult);
2541
2542     SET_EXPECT(GetBindInfo);
2543     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
2544     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2545     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2546
2547     SET_EXPECT(GetBindInfo);
2548     hres = IInternetProtocol_Start(protocol, emptyW, &protocol_sink, &bind_info, 0, 0);
2549     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2550     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2551
2552     IInternetProtocol_Release(protocol);
2553 }
2554
2555 static void test_file_protocol(void) {
2556     WCHAR buf[INTERNET_MAX_URL_LENGTH], file_name_buf[MAX_PATH];
2557     DWORD size;
2558     ULONG len;
2559     HANDLE file;
2560
2561     static const WCHAR wszFile[] = {'f','i','l','e',':',0};
2562     static const WCHAR wszFile2[] = {'f','i','l','e',':','/','/',0};
2563     static const WCHAR wszFile3[] = {'f','i','l','e',':','/','/','/',0};
2564     static const WCHAR wszFile4[] = {'f','i','l','e',':','\\','\\',0};
2565     static const char html_doc[] = "<HTML></HTML>";
2566
2567     trace("Testing file protocol...\n");
2568     init_test(FILE_TEST, 0);
2569
2570     SetLastError(0xdeadbeef);
2571     file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2572             FILE_ATTRIBUTE_NORMAL, NULL);
2573     if(!file && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2574     {
2575         win_skip("Detected Win9x or WinMe\n");
2576         return;
2577     }
2578     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
2579     if(file == INVALID_HANDLE_VALUE)
2580         return;
2581     WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
2582     CloseHandle(file);
2583
2584     file_name = wszIndexHtml;
2585     bindf = 0;
2586     test_file_protocol_url(index_url);
2587     bindf = BINDF_FROMURLMON;
2588     test_file_protocol_url(index_url);
2589     bindf = BINDF_FROMURLMON | BINDF_NEEDFILE;
2590     test_file_protocol_url(index_url);
2591
2592     memcpy(buf, wszFile, sizeof(wszFile));
2593     len = sizeof(wszFile)/sizeof(WCHAR)-1;
2594     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2595     buf[len++] = '\\';
2596     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2597
2598     file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
2599     bindf = 0;
2600     test_file_protocol_url(buf);
2601     bindf = BINDF_FROMURLMON;
2602     test_file_protocol_url(buf);
2603
2604     memcpy(buf, wszFile2, sizeof(wszFile2));
2605     len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
2606     file_name_buf[len++] = '\\';
2607     memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2608     lstrcpyW(buf+sizeof(wszFile2)/sizeof(WCHAR)-1, file_name_buf);
2609     file_name = file_name_buf;
2610     bindf = 0;
2611     test_file_protocol_url(buf);
2612     bindf = BINDF_FROMURLMON;
2613     test_file_protocol_url(buf);
2614
2615     buf[sizeof(wszFile2)/sizeof(WCHAR)] = '|';
2616     test_file_protocol_url(buf);
2617
2618     memcpy(buf, wszFile3, sizeof(wszFile3));
2619     len = sizeof(wszFile3)/sizeof(WCHAR)-1;
2620     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2621     buf[len++] = '\\';
2622     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2623
2624     file_name = buf + sizeof(wszFile3)/sizeof(WCHAR)-1;
2625     bindf = 0;
2626     test_file_protocol_url(buf);
2627     bindf = BINDF_FROMURLMON;
2628     test_file_protocol_url(buf);
2629
2630     memcpy(buf, wszFile4, sizeof(wszFile4));
2631     len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
2632     file_name_buf[len++] = '\\';
2633     memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2634     lstrcpyW(buf+sizeof(wszFile4)/sizeof(WCHAR)-1, file_name_buf);
2635     file_name = file_name_buf;
2636     bindf = 0;
2637     test_file_protocol_url(buf);
2638     bindf = BINDF_FROMURLMON;
2639     test_file_protocol_url(buf);
2640
2641     buf[sizeof(wszFile4)/sizeof(WCHAR)] = '|';
2642     test_file_protocol_url(buf);
2643
2644     DeleteFileW(wszIndexHtml);
2645
2646     bindf = 0;
2647     test_file_protocol_fail();
2648     bindf = BINDF_FROMURLMON;
2649     test_file_protocol_fail();
2650 }
2651
2652 static BOOL http_protocol_start(LPCWSTR url, BOOL use_iuri)
2653 {
2654     static BOOL got_user_agent = FALSE;
2655     IUri *uri = NULL;
2656     HRESULT hres;
2657
2658     if(use_iuri && pCreateUri) {
2659         hres = pCreateUri(url, 0, 0, &uri);
2660         ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
2661     }
2662
2663     SET_EXPECT(GetBindInfo);
2664     if (!(bindf & BINDF_FROMURLMON))
2665         SET_EXPECT(ReportProgress_DIRECTBIND);
2666     if(!got_user_agent)
2667         SET_EXPECT(GetBindString_USER_AGENT);
2668     SET_EXPECT(GetBindString_ACCEPT_MIMES);
2669     SET_EXPECT(QueryService_HttpNegotiate);
2670     SET_EXPECT(BeginningTransaction);
2671     SET_EXPECT(GetRootSecurityId);
2672     if(http_post_test) {
2673         SET_EXPECT(GetBindString_POST_COOKIE);
2674         if(http_post_test == TYMED_ISTREAM)
2675             SET_EXPECT(Stream_Seek);
2676     }
2677
2678     if(uri) {
2679         IInternetProtocolEx *protocolex;
2680
2681         hres = IInternetProtocol_QueryInterface(async_protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
2682         ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);
2683
2684         hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, 0, 0);
2685         ok(hres == S_OK, "Start failed: %08x\n", hres);
2686
2687         IInternetProtocolEx_Release(protocolex);
2688         IUri_Release(uri);
2689     }else {
2690         hres = IInternetProtocol_Start(async_protocol, url, &protocol_sink, &bind_info, 0, 0);
2691         ok(hres == S_OK, "Start failed: %08x\n", hres);
2692     }
2693     if(FAILED(hres))
2694         return FALSE;
2695
2696     CHECK_CALLED(GetBindInfo);
2697     if (!(bindf & BINDF_FROMURLMON))
2698         CHECK_CALLED(ReportProgress_DIRECTBIND);
2699     if (!got_user_agent)
2700     {
2701         CHECK_CALLED(GetBindString_USER_AGENT);
2702         got_user_agent = TRUE;
2703     }
2704     CHECK_CALLED(GetBindString_ACCEPT_MIMES);
2705     CHECK_CALLED(QueryService_HttpNegotiate);
2706     CHECK_CALLED(BeginningTransaction);
2707     /* GetRootSecurityId called on WinXP but not on Win98 */
2708     CLEAR_CALLED(GetRootSecurityId);
2709     if(http_post_test) {
2710         CHECK_CALLED(GetBindString_POST_COOKIE);
2711         if(http_post_test == TYMED_ISTREAM)
2712             CHECK_CALLED(Stream_Seek);
2713     }
2714
2715     return TRUE;
2716 }
2717
2718 static void test_protocol_terminate(IInternetProtocol *protocol)
2719 {
2720     BYTE buf[3600];
2721     DWORD cb;
2722     HRESULT hres;
2723
2724     hres = IInternetProtocol_LockRequest(protocol, 0);
2725     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2726
2727     hres = IInternetProtocol_Read(protocol, buf, 1, &cb);
2728     ok(hres == test_abort ? S_OK : S_FALSE, "Read failed: %08x\n", hres);
2729
2730     hres = IInternetProtocol_Terminate(protocol, 0);
2731     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2732
2733     /* This wait is to give the internet handles being freed in Terminate
2734      * enough time to actually terminate in all cases. Internet handles
2735      * terminate asynchronously and native reuses the main InternetOpen
2736      * handle. The only case in which this seems to be necessary is on
2737      * wine with native wininet and urlmon, resulting in the next time
2738      * test_http_protocol_url being called the first data notification actually
2739      * being an extra last data notification from the previous connection
2740      * about once out of every ten times. */
2741     Sleep(100);
2742
2743     hres = IInternetProtocol_UnlockRequest(protocol);
2744     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2745 }
2746
2747 static void test_http_info(IInternetProtocol *protocol)
2748 {
2749     IWinInetHttpInfo *info;
2750     HRESULT hres;
2751
2752     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&info);
2753     ok(hres == S_OK, "Could not get IWinInterHttpInfo iface: %08x\n", hres);
2754
2755     /* TODO */
2756
2757     IWinInetHttpInfo_Release(info);
2758 }
2759
2760 /* is_first refers to whether this is the first call to this function
2761  * _for this url_ */
2762 static void test_http_protocol_url(LPCWSTR url, int prot, DWORD flags, DWORD tymed)
2763 {
2764     IInternetProtocolInfo *protocol_info;
2765     IClassFactory *factory;
2766     IUnknown *unk;
2767     HRESULT hres;
2768
2769     init_test(prot, flags);
2770     http_url = url;
2771     http_post_test = tymed;
2772
2773     hres = CoGetClassObject(prot == HTTPS_TEST ? &CLSID_HttpSProtocol : &CLSID_HttpProtocol,
2774             CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2775     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2776     if(FAILED(hres))
2777         return;
2778
2779     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2780     ok(hres == E_NOINTERFACE,
2781         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
2782         hres);
2783
2784     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2785     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2786     IUnknown_Release(unk);
2787     if(FAILED(hres))
2788         return;
2789
2790     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2791                                         (void**)&async_protocol);
2792     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2793     if(SUCCEEDED(hres)) {
2794         BYTE buf[3600];
2795         DWORD cb;
2796         ULONG ref;
2797
2798         test_priority(async_protocol);
2799         test_http_info(async_protocol);
2800
2801         SET_EXPECT(ReportProgress_COOKIE_SENT);
2802         SET_EXPECT(ReportProgress_FINDINGRESOURCE);
2803         SET_EXPECT(ReportProgress_CONNECTING);
2804         SET_EXPECT(ReportProgress_SENDINGREQUEST);
2805         if(test_redirect)
2806             SET_EXPECT(ReportProgress_REDIRECTING);
2807         SET_EXPECT(ReportProgress_PROXYDETECTING);
2808         if(prot == HTTP_TEST)
2809             SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2810         else
2811             SET_EXPECT(QueryService_HttpSecurity);
2812         if(!(bindf & BINDF_FROMURLMON)) {
2813             SET_EXPECT(OnResponse);
2814             SET_EXPECT(ReportProgress_RAWMIMETYPE);
2815             SET_EXPECT(ReportData);
2816         } else {
2817             SET_EXPECT(Switch);
2818         }
2819
2820         if(!http_protocol_start(url, (flags & TEST_USEIURI) != 0))
2821             return;
2822
2823         if(!direct_read && !test_abort)
2824             SET_EXPECT(ReportResult);
2825         expect_hrResult = test_abort ? E_ABORT : S_OK;
2826
2827         if(direct_read) {
2828             SET_EXPECT(Switch);
2829             while(wait_for_switch) {
2830                 WaitForSingleObject(event_continue, INFINITE);
2831                 CHECK_CALLED(Switch); /* Set in ReportData */
2832                 call_continue(&continue_protdata);
2833                 SetEvent(event_continue_done);
2834             }
2835         }else {
2836             hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2837             ok((hres == E_PENDING && cb==0) ||
2838                (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2839
2840             WaitForSingleObject(event_complete, INFINITE);
2841             if(bindf & BINDF_FROMURLMON)
2842                 CHECK_CALLED(Switch);
2843             else
2844                 CHECK_CALLED(ReportData);
2845             if(prot == HTTPS_TEST)
2846                 CLEAR_CALLED(QueryService_HttpSecurity);
2847
2848             while(1) {
2849                 if(bindf & BINDF_FROMURLMON)
2850                     SET_EXPECT(Switch);
2851                 else
2852                     SET_EXPECT(ReportData);
2853                 hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
2854                 if(hres == E_PENDING) {
2855                     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2856                     ok((hres == E_PENDING && cb==0) ||
2857                        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2858                     WaitForSingleObject(event_complete, INFINITE);
2859                     if(bindf & BINDF_FROMURLMON)
2860                         CHECK_CALLED(Switch);
2861                     else
2862                         CHECK_CALLED(ReportData);
2863
2864                     if(test_abort) {
2865                         HRESULT hres;
2866
2867                         SET_EXPECT(ReportResult);
2868                         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2869                         ok(hres == S_OK, "Abort failed: %08x\n", hres);
2870                         CHECK_CALLED(ReportResult);
2871
2872                         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2873                         ok(hres == INET_E_RESULT_DISPATCHED, "Abort failed: %08x\n", hres);
2874                         break;
2875                     }
2876                 }else {
2877                     if(bindf & BINDF_FROMURLMON)
2878                         CHECK_NOT_CALLED(Switch);
2879                     else
2880                         CHECK_NOT_CALLED(ReportData);
2881                     if(cb == 0) break;
2882                 }
2883             }
2884             if(!test_abort) {
2885                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2886                 CHECK_CALLED(ReportResult);
2887             }
2888         }
2889         if(prot == HTTPS_TEST)
2890             CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
2891
2892         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2893         ok(hres == INET_E_RESULT_DISPATCHED, "Abort failed: %08x\n", hres);
2894
2895         test_protocol_terminate(async_protocol);
2896
2897         hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
2898         ok(hres == S_OK, "Abort failed: %08x\n", hres);
2899
2900         ref = IInternetProtocol_Release(async_protocol);
2901         ok(!ref, "ref=%x\n", ref);
2902     }
2903
2904     IClassFactory_Release(factory);
2905 }
2906
2907 static void test_http_protocol(void)
2908 {
2909     static const WCHAR winehq_url[] =
2910         {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
2911             'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
2912     static const WCHAR posttest_url[] =
2913         {'h','t','t','p',':','/','/','c','r','o','s','s','o','v','e','r','.',
2914          'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m','/',
2915          'p','o','s','t','t','e','s','t','.','p','h','p',0};
2916     static const WCHAR redirect_url[] =
2917         {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
2918          't','e','s','t','r','e','d','i','r','e','c','t',0};
2919     static const WCHAR winetest_url[] =
2920         {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/','d','a','t','a','/',0};
2921
2922     trace("Testing http protocol (not from urlmon)...\n");
2923     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
2924     test_http_protocol_url(winehq_url, HTTP_TEST, TEST_FIRST_HTTP, TYMED_NULL);
2925
2926     trace("Testing http protocol (from urlmon)...\n");
2927     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2928     test_http_protocol_url(winehq_url, HTTP_TEST, 0, TYMED_NULL);
2929
2930     trace("Testing http protocol (to file)...\n");
2931     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
2932     test_http_protocol_url(winehq_url, HTTP_TEST, 0, TYMED_NULL);
2933
2934     trace("Testing http protocol (post data)...\n");
2935     /* Without this flag we get a ReportProgress_CACHEFILENAMEAVAILABLE
2936      * notification with BINDVERB_POST */
2937     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2938     test_http_protocol_url(posttest_url, HTTP_TEST, TEST_FIRST_HTTP|TEST_POST, TYMED_HGLOBAL);
2939
2940     trace("Testing http protocol (post data stream)...\n");
2941     test_http_protocol_url(posttest_url, HTTP_TEST, TEST_FIRST_HTTP|TEST_POST|TEST_ASYNCREQ, TYMED_ISTREAM);
2942
2943     trace("Testing http protocol (direct read)...\n");
2944     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2945     test_http_protocol_url(winehq_url, HTTP_TEST, TEST_DIRECT_READ|TEST_USEIURI, TYMED_NULL);
2946
2947     trace("Testing http protocol (redirected)...\n");
2948     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2949     test_http_protocol_url(redirect_url, HTTP_TEST, TEST_REDIRECT, TYMED_NULL);
2950
2951     trace("Testing http protocol abort...\n");
2952     test_http_protocol_url(winetest_url, HTTP_TEST, TEST_ABORT, TYMED_NULL);
2953
2954     test_early_abort(&CLSID_HttpProtocol);
2955     test_early_abort(&CLSID_HttpSProtocol);
2956 }
2957
2958 static void test_https_protocol(void)
2959 {
2960     static const WCHAR codeweavers_url[] =
2961         {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
2962          '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0};
2963
2964     trace("Testing https protocol (from urlmon)...\n");
2965     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2966     test_http_protocol_url(codeweavers_url, HTTPS_TEST, TEST_FIRST_HTTP, TYMED_NULL);
2967 }
2968
2969
2970 static void test_ftp_protocol(void)
2971 {
2972     IInternetProtocolInfo *protocol_info;
2973     IClassFactory *factory;
2974     IUnknown *unk;
2975     BYTE buf[4096];
2976     ULONG ref;
2977     DWORD cb;
2978     HRESULT hres;
2979
2980     static const WCHAR ftp_urlW[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
2981     '/','p','u','b','/','o','t','h','e','r','/',
2982     'w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0};
2983
2984     trace("Testing ftp protocol...\n");
2985
2986     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2987     state = STATE_STARTDOWNLOADING;
2988     tested_protocol = FTP_TEST;
2989     first_data_notif = TRUE;
2990     expect_hrResult = E_PENDING;
2991
2992     hres = CoGetClassObject(&CLSID_FtpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2993     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2994     if(FAILED(hres))
2995         return;
2996
2997     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2998     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2999
3000     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
3001     ok(hres == S_OK, "Could not get IClassFactory interface\n");
3002     IUnknown_Release(unk);
3003     if(FAILED(hres))
3004         return;
3005
3006     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
3007                                         (void**)&async_protocol);
3008     IClassFactory_Release(factory);
3009     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
3010
3011     test_priority(async_protocol);
3012     test_http_info(async_protocol);
3013
3014     SET_EXPECT(GetBindInfo);
3015     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
3016     SET_EXPECT(ReportProgress_CONNECTING);
3017     SET_EXPECT(ReportProgress_SENDINGREQUEST);
3018     SET_EXPECT(Switch);
3019
3020     hres = IInternetProtocol_Start(async_protocol, ftp_urlW, &protocol_sink, &bind_info, 0, 0);
3021     ok(hres == S_OK, "Start failed: %08x\n", hres);
3022     CHECK_CALLED(GetBindInfo);
3023
3024     SET_EXPECT(ReportResult);
3025
3026     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
3027     ok((hres == E_PENDING && cb==0) ||
3028        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
3029
3030     WaitForSingleObject(event_complete, INFINITE);
3031
3032     while(1) {
3033         hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
3034         if(hres == E_PENDING)
3035             WaitForSingleObject(event_complete, INFINITE);
3036         else
3037             if(cb == 0) break;
3038     }
3039
3040     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
3041     CHECK_CALLED(ReportResult);
3042     CHECK_CALLED(Switch);
3043
3044     test_protocol_terminate(async_protocol);
3045
3046     if(pCreateUri) {
3047         IInternetProtocolEx *protocolex;
3048
3049         hres = IInternetProtocol_QueryInterface(async_protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
3050         ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);
3051         IInternetProtocolEx_Release(protocolex);
3052     }
3053
3054     ref = IInternetProtocol_Release(async_protocol);
3055     ok(!ref, "ref=%d\n", ref);
3056
3057     test_early_abort(&CLSID_FtpProtocol);
3058 }
3059
3060 static void test_gopher_protocol(void)
3061 {
3062     IInternetProtocolInfo *protocol_info;
3063     IClassFactory *factory;
3064     IUnknown *unk;
3065     HRESULT hres;
3066
3067     trace("Testing gopher protocol...\n");
3068
3069     hres = CoGetClassObject(&CLSID_GopherProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
3070     ok(hres == S_OK ||
3071        hres == REGDB_E_CLASSNOTREG, /* Gopher protocol has been removed as of Vista */
3072        "CoGetClassObject failed: %08x\n", hres);
3073     if(FAILED(hres))
3074         return;
3075
3076     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
3077     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
3078
3079     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
3080     ok(hres == S_OK, "Could not get IClassFactory interface\n");
3081     IUnknown_Release(unk);
3082     if(FAILED(hres))
3083         return;
3084
3085     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
3086                                         (void**)&async_protocol);
3087     IClassFactory_Release(factory);
3088     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
3089
3090     test_priority(async_protocol);
3091
3092     IInternetProtocol_Release(async_protocol);
3093
3094     test_early_abort(&CLSID_GopherProtocol);
3095 }
3096
3097 static void test_mk_protocol(void)
3098 {
3099     IInternetProtocolInfo *protocol_info;
3100     IInternetProtocol *protocol;
3101     IClassFactory *factory;
3102     IUnknown *unk;
3103     HRESULT hres;
3104
3105     static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
3106                                        ':',':','/','t','e','s','t','.','h','t','m','l',0};
3107     static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};
3108
3109     trace("Testing mk protocol...\n");
3110     init_test(MK_TEST, 0);
3111
3112     hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
3113             &IID_IUnknown, (void**)&unk);
3114     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
3115
3116     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
3117     ok(hres == E_NOINTERFACE,
3118         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
3119         hres);
3120
3121     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
3122     ok(hres == S_OK, "Could not get IClassFactory interface\n");
3123     IUnknown_Release(unk);
3124     if(FAILED(hres))
3125         return;
3126
3127     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
3128                                         (void**)&protocol);
3129     IClassFactory_Release(factory);
3130     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
3131
3132     SET_EXPECT(GetBindInfo);
3133     hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
3134     ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
3135        "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
3136     CLEAR_CALLED(GetBindInfo);
3137
3138     SET_EXPECT(GetBindInfo);
3139     SET_EXPECT(ReportProgress_DIRECTBIND);
3140     SET_EXPECT(ReportProgress_SENDINGREQUEST);
3141     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
3142     SET_EXPECT(ReportResult);
3143     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
3144
3145     hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
3146     ok(hres == INET_E_RESOURCE_NOT_FOUND ||
3147        hres == INET_E_INVALID_URL, /* win2k3 */
3148        "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
3149
3150     if (hres == INET_E_RESOURCE_NOT_FOUND) {
3151         CHECK_CALLED(GetBindInfo);
3152         CLEAR_CALLED(ReportProgress_DIRECTBIND);
3153         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
3154         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
3155         CHECK_CALLED(ReportResult);
3156     }else {
3157         CLEAR_CALLED(GetBindInfo);
3158         CLEAR_CALLED(ReportProgress_DIRECTBIND);
3159         CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
3160         CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
3161         CLEAR_CALLED(ReportResult);
3162     }
3163
3164     IInternetProtocol_Release(protocol);
3165 }
3166
3167 static void test_CreateBinding(void)
3168 {
3169     IInternetProtocol *protocol;
3170     IInternetPriority *priority;
3171     IInternetSession *session;
3172     IWinInetHttpInfo *http_info;
3173     IWinInetInfo *inet_info;
3174     LONG p;
3175     BYTE buf[1000];
3176     DWORD read;
3177     HRESULT hres;
3178
3179     static const WCHAR test_url[] =
3180         {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
3181     static const WCHAR wsz_test[] = {'t','e','s','t',0};
3182
3183     trace("Testing CreateBinding...\n");
3184     init_test(BIND_TEST, TEST_BINDING);
3185
3186     hres = pCoInternetGetSession(0, &session, 0);
3187     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
3188
3189     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
3190     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
3191
3192     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
3193     binding_protocol = protocol;
3194     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3195     ok(protocol != NULL, "protocol == NULL\n");
3196
3197     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
3198     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
3199
3200     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
3201     ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);
3202
3203     hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
3204     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
3205     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
3206     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
3207     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
3208     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
3209
3210     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
3211     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
3212
3213     p = 0xdeadbeef;
3214     hres = IInternetPriority_GetPriority(priority, &p);
3215     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
3216     ok(!p, "p=%d\n", p);
3217
3218     ex_priority = 100;
3219     hres = IInternetPriority_SetPriority(priority, 100);
3220     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
3221
3222     p = 0xdeadbeef;
3223     hres = IInternetPriority_GetPriority(priority, &p);
3224     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
3225     ok(p == 100, "p=%d\n", p);
3226
3227     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
3228     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3229
3230     SET_EXPECT(QueryService_InternetProtocol);
3231     SET_EXPECT(CreateInstance);
3232     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
3233     SET_EXPECT(SetPriority);
3234     SET_EXPECT(Start);
3235
3236     expect_hrResult = S_OK;
3237     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
3238     ok(hres == S_OK, "Start failed: %08x\n", hres);
3239
3240     CHECK_CALLED(QueryService_InternetProtocol);
3241     CHECK_CALLED(CreateInstance);
3242     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
3243     CHECK_CALLED(SetPriority);
3244     CHECK_CALLED(Start);
3245
3246     SET_EXPECT(QueryInterface_IWinInetInfo);
3247     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
3248     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3249     CHECK_CALLED(QueryInterface_IWinInetInfo);
3250
3251     SET_EXPECT(QueryInterface_IWinInetInfo);
3252     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
3253     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3254     CHECK_CALLED(QueryInterface_IWinInetInfo);
3255
3256     SET_EXPECT(QueryInterface_IWinInetHttpInfo);
3257     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&http_info);
3258     ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
3259     CHECK_CALLED(QueryInterface_IWinInetHttpInfo);
3260
3261     SET_EXPECT(Read);
3262     read = 0xdeadbeef;
3263     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
3264     ok(hres == S_OK, "Read failed: %08x\n", hres);
3265     ok(read == 100, "read = %d\n", read);
3266     CHECK_CALLED(Read);
3267
3268     SET_EXPECT(Read);
3269     read = 0xdeadbeef;
3270     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
3271     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
3272     ok(!read, "read = %d\n", read);
3273     CHECK_CALLED(Read);
3274
3275     p = 0xdeadbeef;
3276     hres = IInternetPriority_GetPriority(priority, &p);
3277     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
3278     ok(p == 100, "p=%d\n", p);
3279
3280     hres = IInternetPriority_SetPriority(priority, 101);
3281     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
3282
3283     SET_EXPECT(Terminate);
3284     hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
3285     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
3286     CHECK_CALLED(Terminate);
3287
3288     SET_EXPECT(Continue);
3289     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
3290     ok(hres == S_OK, "Switch failed: %08x\n", hres);
3291     CHECK_CALLED(Continue);
3292
3293     hres = IInternetProtocolSink_ReportProgress(binding_sink,
3294             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
3295     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
3296
3297     hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
3298     ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);
3299
3300     hres = IInternetProtocolSink_ReportData(binding_sink, 0, 0, 0);
3301     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
3302
3303     IInternetProtocolSink_Release(binding_sink);
3304     IInternetPriority_Release(priority);
3305     IInternetBindInfo_Release(prot_bind_info);
3306     IInternetProtocol_Release(protocol);
3307
3308     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
3309     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3310     ok(protocol != NULL, "protocol == NULL\n");
3311
3312     hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
3313     ok(hres == S_OK, "Abort failed: %08x\n", hres);
3314
3315     hres = IInternetProtocol_Abort(protocol, E_FAIL, 0);
3316     ok(hres == S_OK, "Abort failed: %08x\n", hres);
3317
3318     IInternetProtocol_Release(protocol);
3319
3320     hres = IInternetSession_UnregisterNameSpace(session, &ClassFactory, wsz_test);
3321     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
3322
3323     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
3324     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3325     ok(protocol != NULL, "protocol == NULL\n");
3326
3327     SET_EXPECT(QueryService_InternetProtocol);
3328     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
3329     ok(hres == MK_E_SYNTAX, "Start failed: %08x, expected MK_E_SYNTAX\n", hres);
3330     CHECK_CALLED(QueryService_InternetProtocol);
3331
3332     IInternetProtocol_Release(protocol);
3333
3334     IInternetSession_Release(session);
3335 }
3336
3337 static void test_binding(int prot, DWORD grf_pi, DWORD test_flags)
3338 {
3339     IInternetProtocolEx *protocolex = NULL;
3340     IInternetProtocol *protocol;
3341     IInternetSession *session;
3342     IUri *uri = NULL;
3343     ULONG ref;
3344     HRESULT hres;
3345
3346     pi = grf_pi;
3347
3348     init_test(prot, test_flags|TEST_BINDING);
3349
3350     hres = pCoInternetGetSession(0, &session, 0);
3351     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
3352
3353     if(test_flags & TEST_EMULATEPROT) {
3354         hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
3355         ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
3356     }
3357
3358     hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
3359     binding_protocol = protocol;
3360     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
3361     ok(protocol != NULL, "protocol == NULL\n");
3362
3363     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
3364     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
3365
3366     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
3367     ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
3368
3369     if(test_flags & TEST_USEIURI) {
3370         hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
3371         ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);
3372
3373         hres = pCreateUri(binding_urls[prot], Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
3374         ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
3375     }
3376
3377     ex_priority = 0;
3378     SET_EXPECT(QueryService_InternetProtocol);
3379     SET_EXPECT(CreateInstance);
3380     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
3381     SET_EXPECT(SetPriority);
3382     if(impl_protex)
3383         SET_EXPECT(StartEx);
3384     else
3385         SET_EXPECT(Start);
3386
3387     expect_hrResult = S_OK;
3388
3389     if(protocolex) {
3390         hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, pi, 0);
3391         ok(hres == S_OK, "StartEx failed: %08x\n", hres);
3392     }else {
3393         hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, pi, 0);
3394         ok(hres == S_OK, "Start failed: %08x\n", hres);
3395     }
3396
3397     CHECK_CALLED(QueryService_InternetProtocol);
3398     CHECK_CALLED(CreateInstance);
3399     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
3400     CHECK_CALLED(SetPriority);
3401     if(impl_protex)
3402         CHECK_CALLED(StartEx);
3403     else
3404         CHECK_CALLED(Start);
3405
3406     if(protocolex)
3407         IInternetProtocolEx_Release(protocolex);
3408     if(uri)
3409         IUri_Release(uri);
3410
3411     if(prot == HTTP_TEST || prot == HTTPS_TEST) {
3412         while(prot_state < 4) {
3413             WaitForSingleObject(event_complete, INFINITE);
3414             if(mimefilter_test && filtered_protocol) {
3415                 SET_EXPECT(Continue);
3416                 IInternetProtocol_Continue(filtered_protocol, pdata);
3417                 CHECK_CALLED(Continue);
3418             }else {
3419                 SET_EXPECT(Continue);
3420                 IInternetProtocol_Continue(protocol, pdata);
3421                 CHECK_CALLED(Continue);
3422             }
3423             if(test_abort && prot_state == 2) {
3424                 SET_EXPECT(Abort);
3425                 hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
3426                 ok(hres == S_OK, "Abort failed: %08x\n", hres);
3427                 CHECK_CALLED(Abort);
3428
3429                 hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
3430                 ok(hres == S_OK, "Abort failed: %08x\n", hres);
3431                 SetEvent(event_complete2);
3432                 break;
3433             }
3434             SetEvent(event_complete2);
3435         }
3436         if(direct_read)
3437             CHECK_CALLED(ReportData); /* Set in ReportResult */
3438         WaitForSingleObject(event_complete, INFINITE);
3439     }else {
3440         if(mimefilter_test)
3441             SET_EXPECT(MimeFilter_LockRequest);
3442         else
3443             SET_EXPECT(LockRequest);
3444         hres = IInternetProtocol_LockRequest(protocol, 0);
3445         ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
3446         if(mimefilter_test)
3447             CHECK_CALLED(MimeFilter_LockRequest);
3448         else
3449             CHECK_CALLED(LockRequest);
3450
3451         if(mimefilter_test)
3452             SET_EXPECT(MimeFilter_UnlockRequest);
3453         else
3454             SET_EXPECT(UnlockRequest);
3455         hres = IInternetProtocol_UnlockRequest(protocol);
3456         ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
3457         if(mimefilter_test)
3458             CHECK_CALLED(MimeFilter_UnlockRequest);
3459         else
3460             CHECK_CALLED(UnlockRequest);
3461     }
3462
3463     if(mimefilter_test)
3464         SET_EXPECT(MimeFilter_Terminate);
3465     else
3466         SET_EXPECT(Terminate);
3467     hres = IInternetProtocol_Terminate(protocol, 0);
3468     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
3469     if(mimefilter_test)
3470         CLEAR_CALLED(MimeFilter_Terminate);
3471     else
3472         CHECK_CALLED(Terminate);
3473
3474     if(filtered_protocol)
3475         IInternetProtocol_Release(filtered_protocol);
3476     IInternetBindInfo_Release(prot_bind_info);
3477     IInternetProtocolSink_Release(binding_sink);
3478     ref = IInternetProtocol_Release(protocol);
3479     ok(!ref, "ref=%u, expected 0\n", ref);
3480
3481     if(test_flags & TEST_EMULATEPROT) {
3482         hres = IInternetSession_UnregisterNameSpace(session, &ClassFactory, protocol_names[prot]);
3483         ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
3484     }
3485
3486     IInternetSession_Release(session);
3487 }
3488
3489 START_TEST(protocol)
3490 {
3491     HMODULE hurlmon;
3492
3493     hurlmon = GetModuleHandle("urlmon.dll");
3494     pCoInternetGetSession = (void*) GetProcAddress(hurlmon, "CoInternetGetSession");
3495     pReleaseBindInfo = (void*) GetProcAddress(hurlmon, "ReleaseBindInfo");
3496     pCreateUri = (void*) GetProcAddress(hurlmon, "CreateUri");
3497
3498     if(!GetProcAddress(hurlmon, "CompareSecurityIds")) {
3499         win_skip("Various needed functions not present, too old IE\n");
3500         return;
3501     }
3502
3503     if(!pCreateUri)
3504         win_skip("CreateUri not supported\n");
3505
3506     OleInitialize(NULL);
3507
3508     event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
3509     event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
3510     event_continue = CreateEvent(NULL, FALSE, FALSE, NULL);
3511     event_continue_done = CreateEvent(NULL, FALSE, FALSE, NULL);
3512     thread_id = GetCurrentThreadId();
3513
3514     test_file_protocol();
3515     test_http_protocol();
3516     test_https_protocol();
3517     test_ftp_protocol();
3518     test_gopher_protocol();
3519     test_mk_protocol();
3520     test_CreateBinding();
3521
3522     bindf &= ~BINDF_FROMURLMON;
3523     trace("Testing file binding (mime verification, emulate prot)...\n");
3524     test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3525     trace("Testing http binding (mime verification, emulate prot)...\n");
3526     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3527     trace("Testing its binding (mime verification, emulate prot)...\n");
3528     test_binding(ITS_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3529     trace("Testing http binding (mime verification, emulate prot, short read, direct read)...\n");
3530     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_SHORT_READ|TEST_DIRECT_READ);
3531     trace("Testing file binding (mime verification, emulate prot, mime filter)...\n");
3532     test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_FILTER);
3533     trace("Testing http binding (mime verification, emulate prot, mime filter)...\n");
3534     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_FILTER);
3535     trace("Testing http binding (mime verification, emulate prot, direct read)...\n");
3536     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_DIRECT_READ);
3537     trace("Testing http binding (mime verification, emulate prot, abort)...\n");
3538     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_ABORT);
3539     if(pCreateUri) {
3540         trace("Testing file binding (use IUri, mime verification, emulate prot)...\n");
3541         test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_USEIURI);
3542         trace("Testing file binding (use IUri, impl StartEx, mime verification, emulate prot)...\n");
3543         test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_USEIURI|TEST_IMPLPROTEX);
3544         trace("Testing file binding (impl StartEx, mime verification, emulate prot)...\n");
3545         test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_IMPLPROTEX);
3546     }
3547
3548     CloseHandle(event_complete);
3549     CloseHandle(event_complete2);
3550     CloseHandle(event_continue);
3551     CloseHandle(event_continue_done);
3552
3553     OleUninitialize();
3554 }