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