shdocvw: Support URLs passed by reference in WebBrowser_Navigate2.
[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     0x01
1805 #define TEST_FILTER      0x02
1806 #define TEST_FIRST_HTTP  0x04
1807
1808 static void init_test(int prot, DWORD flags)
1809 {
1810     tested_protocol = prot;
1811     binding_test = (flags & TEST_BINDING) != 0;
1812     first_data_notif = TRUE;
1813     prot_read = 0;
1814     prot_state = 0;
1815     async_read_pending = TRUE;
1816     mimefilter_test = (flags & TEST_FILTER) != 0;
1817     filter_state = 0;
1818     ResetEvent(event_complete);
1819     ResetEvent(event_complete2);
1820     async_protocol = binding_protocol = filtered_protocol = NULL;
1821     filtered_sink = NULL;
1822     http_is_first = (flags & TEST_FIRST_HTTP) != 0;
1823     first_data_notif = TRUE;
1824     state = 0;
1825 }
1826
1827 static void test_priority(IInternetProtocol *protocol)
1828 {
1829     IInternetPriority *priority;
1830     LONG pr;
1831     HRESULT hres;
1832
1833     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority,
1834                                             (void**)&priority);
1835     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
1836     if(FAILED(hres))
1837         return;
1838
1839     hres = IInternetPriority_GetPriority(priority, &pr);
1840     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1841     ok(pr == 0, "pr=%d, expected 0\n", pr);
1842
1843     hres = IInternetPriority_SetPriority(priority, 1);
1844     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
1845
1846     hres = IInternetPriority_GetPriority(priority, &pr);
1847     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1848     ok(pr == 1, "pr=%d, expected 1\n", pr);
1849
1850     IInternetPriority_Release(priority);
1851 }
1852
1853 static BOOL file_protocol_start(IInternetProtocol *protocol, LPCWSTR url, BOOL is_first)
1854 {
1855     HRESULT hres;
1856
1857     SET_EXPECT(GetBindInfo);
1858     if(!(bindf & BINDF_FROMURLMON))
1859        SET_EXPECT(ReportProgress_DIRECTBIND);
1860     if(is_first) {
1861         SET_EXPECT(ReportProgress_SENDINGREQUEST);
1862         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1863         if(bindf & BINDF_FROMURLMON)
1864             SET_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1865         else
1866             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1867     }
1868     SET_EXPECT(ReportData);
1869     if(is_first)
1870         SET_EXPECT(ReportResult);
1871
1872     expect_hrResult = S_OK;
1873
1874     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
1875     if(hres == INET_E_RESOURCE_NOT_FOUND) {
1876         win_skip("Start failed\n");
1877         return FALSE;
1878     }
1879     ok(hres == S_OK, "Start failed: %08x\n", hres);
1880
1881     CHECK_CALLED(GetBindInfo);
1882     if(!(bindf & BINDF_FROMURLMON))
1883        CHECK_CALLED(ReportProgress_DIRECTBIND);
1884     if(is_first) {
1885         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1886         CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
1887         if(bindf & BINDF_FROMURLMON)
1888             CHECK_CALLED(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1889         else
1890             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1891     }
1892     CHECK_CALLED(ReportData);
1893     if(is_first)
1894         CHECK_CALLED(ReportResult);
1895
1896     return TRUE;
1897 }
1898
1899 static void test_file_protocol_url(LPCWSTR url)
1900 {
1901     IInternetProtocolInfo *protocol_info;
1902     IUnknown *unk;
1903     IClassFactory *factory;
1904     HRESULT hres;
1905
1906     hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
1907             &IID_IUnknown, (void**)&unk);
1908     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1909     if(FAILED(hres))
1910         return;
1911
1912     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1913     ok(hres == E_NOINTERFACE,
1914             "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1915
1916     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1917     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1918     if(SUCCEEDED(hres)) {
1919         IInternetProtocol *protocol;
1920         BYTE buf[512];
1921         ULONG cb;
1922         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1923         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1924
1925         if(SUCCEEDED(hres)) {
1926             if(file_protocol_start(protocol, url, TRUE)) {
1927                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1928                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1929                 ok(cb == 2, "cb=%u expected 2\n", cb);
1930                 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1931                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1932                 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1933                 ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
1934                 ok(cb == 0, "cb=%u expected 0\n", cb);
1935                 hres = IInternetProtocol_UnlockRequest(protocol);
1936                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1937             }
1938
1939             if(file_protocol_start(protocol, url, FALSE)) {
1940                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1941                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1942                 hres = IInternetProtocol_LockRequest(protocol, 0);
1943                 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1944                 hres = IInternetProtocol_UnlockRequest(protocol);
1945                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1946             }
1947
1948             IInternetProtocol_Release(protocol);
1949         }
1950
1951         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1952         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1953
1954         if(SUCCEEDED(hres)) {
1955             if(file_protocol_start(protocol, url, TRUE)) {
1956                 hres = IInternetProtocol_LockRequest(protocol, 0);
1957                 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1958                 hres = IInternetProtocol_Terminate(protocol, 0);
1959                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1960                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1961                 ok(hres == S_OK, "Read failed: %08x\n\n", hres);
1962                 hres = IInternetProtocol_UnlockRequest(protocol);
1963                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1964                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1965                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1966                 hres = IInternetProtocol_Terminate(protocol, 0);
1967                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1968             }
1969
1970             IInternetProtocol_Release(protocol);
1971         }
1972
1973         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1974         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1975
1976         if(SUCCEEDED(hres)) {
1977             if(file_protocol_start(protocol, url, TRUE)) {
1978                 hres = IInternetProtocol_Terminate(protocol, 0);
1979                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1980                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1981                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1982                 ok(cb == 2, "cb=%u expected 2\n", cb);
1983             }
1984
1985             IInternetProtocol_Release(protocol);
1986         }
1987
1988         IClassFactory_Release(factory);
1989     }
1990
1991     IUnknown_Release(unk);
1992 }
1993
1994 static void test_file_protocol_fail(void)
1995 {
1996     IInternetProtocol *protocol;
1997     HRESULT hres;
1998
1999     static const WCHAR index_url2[] =
2000         {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
2001
2002     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2003             &IID_IInternetProtocol, (void**)&protocol);
2004     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2005     if(FAILED(hres))
2006         return;
2007
2008     SET_EXPECT(GetBindInfo);
2009     expect_hrResult = MK_E_SYNTAX;
2010     hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
2011     ok(hres == MK_E_SYNTAX ||
2012        hres == E_INVALIDARG,
2013        "Start failed: %08x, expected MK_E_SYNTAX or E_INVALIDARG\n", hres);
2014     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2015
2016     SET_EXPECT(GetBindInfo);
2017     if(!(bindf & BINDF_FROMURLMON))
2018         SET_EXPECT(ReportProgress_DIRECTBIND);
2019     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2020     SET_EXPECT(ReportResult);
2021     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2022     hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
2023     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2024             "Start failed: %08x expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2025     CHECK_CALLED(GetBindInfo);
2026     if(!(bindf & BINDF_FROMURLMON))
2027         CHECK_CALLED(ReportProgress_DIRECTBIND);
2028     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2029     CHECK_CALLED(ReportResult);
2030
2031     IInternetProtocol_Release(protocol);
2032
2033     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2034             &IID_IInternetProtocol, (void**)&protocol);
2035     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2036     if(FAILED(hres))
2037         return;
2038
2039     SET_EXPECT(GetBindInfo);
2040     if(!(bindf & BINDF_FROMURLMON))
2041         SET_EXPECT(ReportProgress_DIRECTBIND);
2042     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2043     SET_EXPECT(ReportResult);
2044     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2045
2046     hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
2047     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2048             "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2049     CHECK_CALLED(GetBindInfo);
2050     if(!(bindf & BINDF_FROMURLMON))
2051         CHECK_CALLED(ReportProgress_DIRECTBIND);
2052     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2053     CHECK_CALLED(ReportResult);
2054
2055     SET_EXPECT(GetBindInfo);
2056     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
2057     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2058     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2059
2060     SET_EXPECT(GetBindInfo);
2061     hres = IInternetProtocol_Start(protocol, emptyW, &protocol_sink, &bind_info, 0, 0);
2062     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2063     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2064
2065     IInternetProtocol_Release(protocol);
2066 }
2067
2068 static void test_file_protocol(void) {
2069     WCHAR buf[INTERNET_MAX_URL_LENGTH], file_name_buf[MAX_PATH];
2070     DWORD size;
2071     ULONG len;
2072     HANDLE file;
2073
2074     static const WCHAR wszFile[] = {'f','i','l','e',':',0};
2075     static const WCHAR wszFile2[] = {'f','i','l','e',':','/','/',0};
2076     static const WCHAR wszFile3[] = {'f','i','l','e',':','/','/','/',0};
2077     static const char html_doc[] = "<HTML></HTML>";
2078
2079     trace("Testing file protocol...\n");
2080     init_test(FILE_TEST, 0);
2081
2082     SetLastError(0xdeadbeef);
2083     file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2084             FILE_ATTRIBUTE_NORMAL, NULL);
2085     if(!file && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2086     {
2087         win_skip("Detected Win9x or WinMe\n");
2088         return;
2089     }
2090     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
2091     if(file == INVALID_HANDLE_VALUE)
2092         return;
2093     WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
2094     CloseHandle(file);
2095
2096     file_name = wszIndexHtml;
2097     bindf = 0;
2098     test_file_protocol_url(index_url);
2099     bindf = BINDF_FROMURLMON;
2100     test_file_protocol_url(index_url);
2101     bindf = BINDF_FROMURLMON | BINDF_NEEDFILE;
2102     test_file_protocol_url(index_url);
2103
2104     memcpy(buf, wszFile, sizeof(wszFile));
2105     len = sizeof(wszFile)/sizeof(WCHAR)-1;
2106     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2107     buf[len++] = '\\';
2108     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2109
2110     file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
2111     bindf = 0;
2112     test_file_protocol_url(buf);
2113     bindf = BINDF_FROMURLMON;
2114     test_file_protocol_url(buf);
2115
2116     memcpy(buf, wszFile2, sizeof(wszFile2));
2117     len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
2118     file_name_buf[len++] = '\\';
2119     memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2120     lstrcpyW(buf+sizeof(wszFile2)/sizeof(WCHAR)-1, file_name_buf);
2121     file_name = file_name_buf;
2122     bindf = 0;
2123     test_file_protocol_url(buf);
2124     bindf = BINDF_FROMURLMON;
2125     test_file_protocol_url(buf);
2126
2127     buf[sizeof(wszFile2)/sizeof(WCHAR)] = '|';
2128     test_file_protocol_url(buf);
2129
2130     memcpy(buf, wszFile3, sizeof(wszFile3));
2131     len = sizeof(wszFile3)/sizeof(WCHAR)-1;
2132     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2133     buf[len++] = '\\';
2134     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2135
2136     file_name = buf + sizeof(wszFile3)/sizeof(WCHAR)-1;
2137     bindf = 0;
2138     test_file_protocol_url(buf);
2139     bindf = BINDF_FROMURLMON;
2140     test_file_protocol_url(buf);
2141
2142     DeleteFileW(wszIndexHtml);
2143
2144     bindf = 0;
2145     test_file_protocol_fail();
2146     bindf = BINDF_FROMURLMON;
2147     test_file_protocol_fail();
2148 }
2149
2150 static BOOL http_protocol_start(LPCWSTR url)
2151 {
2152     static BOOL got_user_agent = FALSE;
2153     HRESULT hres;
2154
2155     SET_EXPECT(GetBindInfo);
2156     if (!(bindf & BINDF_FROMURLMON))
2157         SET_EXPECT(ReportProgress_DIRECTBIND);
2158     if(!got_user_agent)
2159         SET_EXPECT(GetBindString_USER_AGENT);
2160     SET_EXPECT(GetBindString_ACCEPT_MIMES);
2161     SET_EXPECT(QueryService_HttpNegotiate);
2162     SET_EXPECT(BeginningTransaction);
2163     SET_EXPECT(GetRootSecurityId);
2164     if (http_post_test)
2165         SET_EXPECT(GetBindString_POST_COOKIE);
2166
2167     hres = IInternetProtocol_Start(async_protocol, url, &protocol_sink, &bind_info, 0, 0);
2168     ok(hres == S_OK, "Start failed: %08x\n", hres);
2169     if(FAILED(hres))
2170         return FALSE;
2171
2172     CHECK_CALLED(GetBindInfo);
2173     if (!(bindf & BINDF_FROMURLMON))
2174         CHECK_CALLED(ReportProgress_DIRECTBIND);
2175     if (!got_user_agent)
2176     {
2177         CHECK_CALLED(GetBindString_USER_AGENT);
2178         got_user_agent = TRUE;
2179     }
2180     CHECK_CALLED(GetBindString_ACCEPT_MIMES);
2181     CHECK_CALLED(QueryService_HttpNegotiate);
2182     CHECK_CALLED(BeginningTransaction);
2183     /* GetRootSecurityId called on WinXP but not on Win98 */
2184     CLEAR_CALLED(GetRootSecurityId);
2185     if (http_post_test)
2186         CHECK_CALLED(GetBindString_POST_COOKIE);
2187
2188     return TRUE;
2189 }
2190
2191 static void test_protocol_terminate(IInternetProtocol *protocol)
2192 {
2193     BYTE buf[3600];
2194     DWORD cb;
2195     HRESULT hres;
2196
2197     hres = IInternetProtocol_LockRequest(protocol, 0);
2198     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2199
2200     hres = IInternetProtocol_Read(protocol, buf, 1, &cb);
2201     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2202
2203     hres = IInternetProtocol_Terminate(protocol, 0);
2204     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2205
2206     /* This wait is to give the internet handles being freed in Terminate
2207      * enough time to actually terminate in all cases. Internet handles
2208      * terminate asynchronously and native reuses the main InternetOpen
2209      * handle. The only case in which this seems to be necessary is on
2210      * wine with native wininet and urlmon, resulting in the next time
2211      * test_http_protocol_url being called the first data notification actually
2212      * being an extra last data notification from the previous connection
2213      * about once out of every ten times. */
2214     Sleep(100);
2215
2216     hres = IInternetProtocol_UnlockRequest(protocol);
2217     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2218 }
2219
2220 static void test_http_info(IInternetProtocol *protocol)
2221 {
2222     IWinInetHttpInfo *info;
2223     HRESULT hres;
2224
2225     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&info);
2226     ok(hres == S_OK, "Could not get IWinInterHttpInfo iface: %08x\n", hres);
2227
2228     /* TODO */
2229
2230     IWinInetHttpInfo_Release(info);
2231 }
2232
2233 /* is_first refers to whether this is the first call to this function
2234  * _for this url_ */
2235 static void test_http_protocol_url(LPCWSTR url, int prot, DWORD flags)
2236 {
2237     IInternetProtocolInfo *protocol_info;
2238     IClassFactory *factory;
2239     IUnknown *unk;
2240     HRESULT hres;
2241
2242     init_test(prot, flags);
2243     http_url = url;
2244
2245     hres = CoGetClassObject(prot == HTTPS_TEST ? &CLSID_HttpSProtocol : &CLSID_HttpProtocol,
2246             CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2247     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2248     if(FAILED(hres))
2249         return;
2250
2251     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2252     ok(hres == E_NOINTERFACE,
2253         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
2254         hres);
2255
2256     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2257     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2258     IUnknown_Release(unk);
2259     if(FAILED(hres))
2260         return;
2261
2262     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2263                                         (void**)&async_protocol);
2264     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2265     if(SUCCEEDED(hres)) {
2266         BYTE buf[3600];
2267         DWORD cb;
2268         ULONG ref;
2269
2270         test_priority(async_protocol);
2271         test_http_info(async_protocol);
2272
2273         SET_EXPECT(ReportProgress_FINDINGRESOURCE);
2274         SET_EXPECT(ReportProgress_CONNECTING);
2275         SET_EXPECT(ReportProgress_SENDINGREQUEST);
2276         SET_EXPECT(ReportProgress_PROXYDETECTING);
2277         if(prot == HTTP_TEST)
2278             SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2279         else
2280             SET_EXPECT(QueryService_HttpSecurity);
2281         if(!(bindf & BINDF_FROMURLMON)) {
2282             SET_EXPECT(OnResponse);
2283             SET_EXPECT(ReportProgress_RAWMIMETYPE);
2284             SET_EXPECT(ReportData);
2285         } else {
2286             SET_EXPECT(Switch);
2287         }
2288
2289         if(!http_protocol_start(url))
2290             return;
2291
2292         SET_EXPECT(ReportResult);
2293         expect_hrResult = S_OK;
2294
2295         hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2296         ok((hres == E_PENDING && cb==0) ||
2297            (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2298
2299         WaitForSingleObject(event_complete, INFINITE);
2300         if(bindf & BINDF_FROMURLMON)
2301             CHECK_CALLED(Switch);
2302         else
2303             CHECK_CALLED(ReportData);
2304         if(prot == HTTPS_TEST)
2305             CLEAR_CALLED(QueryService_HttpSecurity);
2306
2307         while(1) {
2308             if(bindf & BINDF_FROMURLMON)
2309                 SET_EXPECT(Switch);
2310             else
2311                 SET_EXPECT(ReportData);
2312             hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
2313             if(hres == E_PENDING) {
2314                 hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2315                 ok((hres == E_PENDING && cb==0) ||
2316                    (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2317                 WaitForSingleObject(event_complete, INFINITE);
2318                 if(bindf & BINDF_FROMURLMON)
2319                     CHECK_CALLED(Switch);
2320                 else
2321                     CHECK_CALLED(ReportData);
2322             }else {
2323                 if(bindf & BINDF_FROMURLMON)
2324                     CHECK_NOT_CALLED(Switch);
2325                 else
2326                     CHECK_NOT_CALLED(ReportData);
2327                 if(cb == 0) break;
2328             }
2329         }
2330         ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2331         CHECK_CALLED(ReportResult);
2332         if(prot == HTTPS_TEST)
2333             CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
2334
2335         test_protocol_terminate(async_protocol);
2336         ref = IInternetProtocol_Release(async_protocol);
2337         ok(!ref, "ref=%x\n", hres);
2338     }
2339
2340     IClassFactory_Release(factory);
2341 }
2342
2343 static void test_http_protocol(void)
2344 {
2345     static const WCHAR winehq_url[] =
2346         {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
2347             'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
2348     static const WCHAR posttest_url[] =
2349         {'h','t','t','p',':','/','/','c','r','o','s','s','o','v','e','r','.',
2350          'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m','/',
2351          'p','o','s','t','t','e','s','t','.','p','h','p',0};
2352
2353     trace("Testing http protocol (not from urlmon)...\n");
2354     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
2355     test_http_protocol_url(winehq_url, HTTP_TEST, TEST_FIRST_HTTP);
2356
2357     trace("Testing http protocol (from urlmon)...\n");
2358     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2359     test_http_protocol_url(winehq_url, HTTP_TEST, 0);
2360
2361     trace("Testing http protocol (to file)...\n");
2362     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
2363     test_http_protocol_url(winehq_url, HTTP_TEST, 0);
2364
2365     trace("Testing http protocol (post data)...\n");
2366     http_post_test = TRUE;
2367     /* Without this flag we get a ReportProgress_CACHEFILENAMEAVAILABLE
2368      * notification with BINDVERB_POST */
2369     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2370     test_http_protocol_url(posttest_url, HTTP_TEST, TEST_FIRST_HTTP);
2371     http_post_test = FALSE;
2372 }
2373
2374 static void test_https_protocol(void)
2375 {
2376     static const WCHAR codeweavers_url[] =
2377         {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
2378          '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0};
2379
2380     trace("Testing https protocol (from urlmon)...\n");
2381     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2382     test_http_protocol_url(codeweavers_url, HTTPS_TEST, TEST_FIRST_HTTP);
2383 }
2384
2385
2386 static void test_ftp_protocol(void)
2387 {
2388     IInternetProtocolInfo *protocol_info;
2389     IClassFactory *factory;
2390     IUnknown *unk;
2391     BYTE buf[4096];
2392     ULONG ref;
2393     DWORD cb;
2394     HRESULT hres;
2395
2396     static const WCHAR ftp_urlW[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
2397     '/','p','u','b','/','o','t','h','e','r','/',
2398     'w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0};
2399
2400     trace("Testing ftp protocol...\n");
2401
2402     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2403     state = 0;
2404     tested_protocol = FTP_TEST;
2405     first_data_notif = TRUE;
2406     expect_hrResult = E_PENDING;
2407
2408     hres = CoGetClassObject(&CLSID_FtpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2409     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2410     if(FAILED(hres))
2411         return;
2412
2413     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2414     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2415
2416     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2417     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2418     IUnknown_Release(unk);
2419     if(FAILED(hres))
2420         return;
2421
2422     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2423                                         (void**)&async_protocol);
2424     IClassFactory_Release(factory);
2425     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2426
2427     test_priority(async_protocol);
2428     test_http_info(async_protocol);
2429
2430     SET_EXPECT(GetBindInfo);
2431     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
2432     SET_EXPECT(ReportProgress_CONNECTING);
2433     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2434     SET_EXPECT(Switch);
2435
2436     hres = IInternetProtocol_Start(async_protocol, ftp_urlW, &protocol_sink, &bind_info, 0, 0);
2437     ok(hres == S_OK, "Start failed: %08x\n", hres);
2438     CHECK_CALLED(GetBindInfo);
2439
2440     SET_EXPECT(ReportResult);
2441
2442     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2443     ok((hres == E_PENDING && cb==0) ||
2444        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2445
2446     WaitForSingleObject(event_complete, INFINITE);
2447
2448     while(1) {
2449
2450         hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
2451         if(hres == E_PENDING)
2452             WaitForSingleObject(event_complete, INFINITE);
2453         else
2454             if(cb == 0) break;
2455     }
2456
2457     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2458     CHECK_CALLED(ReportResult);
2459     CHECK_CALLED(Switch);
2460
2461     test_protocol_terminate(async_protocol);
2462
2463     ref = IInternetProtocol_Release(async_protocol);
2464     ok(!ref, "ref=%d\n", ref);
2465 }
2466
2467 static void test_gopher_protocol(void)
2468 {
2469     IInternetProtocolInfo *protocol_info;
2470     IClassFactory *factory;
2471     IUnknown *unk;
2472     HRESULT hres;
2473
2474     trace("Testing gopher protocol...\n");
2475
2476     hres = CoGetClassObject(&CLSID_GopherProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2477     ok(hres == S_OK ||
2478        hres == REGDB_E_CLASSNOTREG, /* Gopher protocol has been removed as of Vista */
2479        "CoGetClassObject failed: %08x\n", hres);
2480     if(FAILED(hres))
2481         return;
2482
2483     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2484     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2485
2486     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2487     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2488     IUnknown_Release(unk);
2489     if(FAILED(hres))
2490         return;
2491
2492     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2493                                         (void**)&async_protocol);
2494     IClassFactory_Release(factory);
2495     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2496
2497     test_priority(async_protocol);
2498
2499     IInternetProtocol_Release(async_protocol);
2500 }
2501
2502 static void test_mk_protocol(void)
2503 {
2504     IInternetProtocolInfo *protocol_info;
2505     IInternetProtocol *protocol;
2506     IClassFactory *factory;
2507     IUnknown *unk;
2508     HRESULT hres;
2509
2510     static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
2511                                        ':',':','/','t','e','s','t','.','h','t','m','l',0};
2512     static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};
2513
2514     trace("Testing mk protocol...\n");
2515     init_test(MK_TEST, 0);
2516
2517     hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
2518             &IID_IUnknown, (void**)&unk);
2519     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2520
2521     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2522     ok(hres == E_NOINTERFACE,
2523         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
2524         hres);
2525
2526     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2527     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2528     IUnknown_Release(unk);
2529     if(FAILED(hres))
2530         return;
2531
2532     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2533                                         (void**)&protocol);
2534     IClassFactory_Release(factory);
2535     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2536
2537     SET_EXPECT(GetBindInfo);
2538     hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
2539     ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
2540        "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
2541     CLEAR_CALLED(GetBindInfo);
2542
2543     SET_EXPECT(GetBindInfo);
2544     SET_EXPECT(ReportProgress_DIRECTBIND);
2545     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2546     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
2547     SET_EXPECT(ReportResult);
2548     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2549
2550     hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
2551     ok(hres == INET_E_RESOURCE_NOT_FOUND ||
2552        hres == INET_E_INVALID_URL, /* win2k3 */
2553        "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
2554
2555     if (hres == INET_E_RESOURCE_NOT_FOUND) {
2556         CHECK_CALLED(GetBindInfo);
2557         CLEAR_CALLED(ReportProgress_DIRECTBIND);
2558         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2559         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
2560         CHECK_CALLED(ReportResult);
2561     }else {
2562         CLEAR_CALLED(GetBindInfo);
2563         CLEAR_CALLED(ReportProgress_DIRECTBIND);
2564         CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
2565         CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
2566         CLEAR_CALLED(ReportResult);
2567     }
2568
2569     IInternetProtocol_Release(protocol);
2570 }
2571
2572 static void test_CreateBinding(void)
2573 {
2574     IInternetProtocol *protocol;
2575     IInternetPriority *priority;
2576     IInternetSession *session;
2577     LONG p;
2578     BYTE buf[1000];
2579     DWORD read;
2580     HRESULT hres;
2581
2582     static const WCHAR test_url[] =
2583         {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
2584     static const WCHAR wsz_test[] = {'t','e','s','t',0};
2585
2586     trace("Testing CreateBinding...\n");
2587     init_test(BIND_TEST, TEST_BINDING);
2588
2589     hres = CoInternetGetSession(0, &session, 0);
2590     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2591
2592     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
2593     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
2594
2595     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
2596     binding_protocol = protocol;
2597     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
2598     ok(protocol != NULL, "protocol == NULL\n");
2599
2600     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
2601     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
2602
2603     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
2604     ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);
2605
2606     hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
2607     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2608     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
2609     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2610     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
2611     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2612
2613     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
2614     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
2615
2616     p = 0xdeadbeef;
2617     hres = IInternetPriority_GetPriority(priority, &p);
2618     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2619     ok(!p, "p=%d\n", p);
2620
2621     ex_priority = 100;
2622     hres = IInternetPriority_SetPriority(priority, 100);
2623     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2624
2625     p = 0xdeadbeef;
2626     hres = IInternetPriority_GetPriority(priority, &p);
2627     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2628     ok(p == 100, "p=%d\n", p);
2629
2630     SET_EXPECT(QueryService_InternetProtocol);
2631     SET_EXPECT(CreateInstance);
2632     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2633     SET_EXPECT(SetPriority);
2634     SET_EXPECT(Start);
2635
2636     expect_hrResult = S_OK;
2637     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
2638     ok(hres == S_OK, "Start failed: %08x\n", hres);
2639
2640     CHECK_CALLED(QueryService_InternetProtocol);
2641     CHECK_CALLED(CreateInstance);
2642     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2643     CHECK_CALLED(SetPriority);
2644     CHECK_CALLED(Start);
2645
2646     SET_EXPECT(Read);
2647     read = 0xdeadbeef;
2648     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2649     ok(hres == S_OK, "Read failed: %08x\n", hres);
2650     ok(read == 100, "read = %d\n", read);
2651     CHECK_CALLED(Read);
2652
2653     SET_EXPECT(Read);
2654     read = 0xdeadbeef;
2655     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2656     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2657     ok(!read, "read = %d\n", read);
2658     CHECK_CALLED(Read);
2659
2660     p = 0xdeadbeef;
2661     hres = IInternetPriority_GetPriority(priority, &p);
2662     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2663     ok(p == 100, "p=%d\n", p);
2664
2665     hres = IInternetPriority_SetPriority(priority, 101);
2666     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2667
2668     SET_EXPECT(Terminate);
2669     hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
2670     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2671     CHECK_CALLED(Terminate);
2672
2673     SET_EXPECT(Continue);
2674     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
2675     ok(hres == S_OK, "Switch failed: %08x\n", hres);
2676     CHECK_CALLED(Continue);
2677
2678     hres = IInternetProtocolSink_ReportProgress(binding_sink,
2679             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
2680     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
2681
2682     hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
2683     ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);
2684
2685     hres = IInternetProtocolSink_ReportData(binding_sink, 0, 0, 0);
2686     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
2687
2688     IInternetProtocolSink_Release(binding_sink);
2689     IInternetPriority_Release(priority);
2690     IInternetBindInfo_Release(prot_bind_info);
2691     IInternetProtocol_Release(protocol);
2692     IInternetSession_Release(session);
2693 }
2694
2695 static void test_binding(int prot, DWORD grf_pi, BOOL test_filter)
2696 {
2697     IInternetProtocol *protocol;
2698     IInternetSession *session;
2699     ULONG ref;
2700     HRESULT hres;
2701
2702     trace("Testing %s binding (grfPI %x%s)...\n", debugstr_w(protocol_names[prot]), grf_pi,
2703           test_filter ? " testing MIME filter" : "");
2704     init_test(prot, TEST_BINDING | (test_filter ? TEST_FILTER : 0));
2705     pi = grf_pi;
2706
2707     hres = CoInternetGetSession(0, &session, 0);
2708     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2709
2710     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
2711     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
2712
2713     hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
2714     binding_protocol = protocol;
2715     IInternetSession_Release(session);
2716     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
2717     ok(protocol != NULL, "protocol == NULL\n");
2718
2719     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
2720     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
2721
2722     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
2723     ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
2724
2725     ex_priority = 0;
2726     SET_EXPECT(QueryService_InternetProtocol);
2727     SET_EXPECT(CreateInstance);
2728     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2729     SET_EXPECT(SetPriority);
2730     SET_EXPECT(Start);
2731
2732     expect_hrResult = S_OK;
2733     hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, pi, 0);
2734     ok(hres == S_OK, "Start failed: %08x\n", hres);
2735
2736     CHECK_CALLED(QueryService_InternetProtocol);
2737     CHECK_CALLED(CreateInstance);
2738     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2739     CHECK_CALLED(SetPriority);
2740     CHECK_CALLED(Start);
2741
2742     if(prot == HTTP_TEST || prot == HTTPS_TEST) {
2743         while(prot_state < 4) {
2744             WaitForSingleObject(event_complete, INFINITE);
2745             if(mimefilter_test && filtered_protocol) {
2746                 SET_EXPECT(Continue);
2747                 IInternetProtocol_Continue(filtered_protocol, pdata);
2748                 CHECK_CALLED(Continue);
2749             }else {
2750                 SET_EXPECT(Continue);
2751                 IInternetProtocol_Continue(protocol, pdata);
2752                 CHECK_CALLED(Continue);
2753             }
2754             SetEvent(event_complete2);
2755         }
2756         WaitForSingleObject(event_complete, INFINITE);
2757     }else {
2758         if(mimefilter_test)
2759             SET_EXPECT(MimeFilter_LockRequest);
2760         else
2761             SET_EXPECT(LockRequest);
2762         hres = IInternetProtocol_LockRequest(protocol, 0);
2763         ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2764         if(mimefilter_test)
2765             CHECK_CALLED(MimeFilter_LockRequest);
2766         else
2767             CHECK_CALLED(LockRequest);
2768
2769         if(mimefilter_test)
2770             SET_EXPECT(MimeFilter_UnlockRequest);
2771         else
2772             SET_EXPECT(UnlockRequest);
2773         hres = IInternetProtocol_UnlockRequest(protocol);
2774         ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2775         if(mimefilter_test)
2776             CHECK_CALLED(MimeFilter_UnlockRequest);
2777         else
2778             CHECK_CALLED(UnlockRequest);
2779     }
2780
2781     if(mimefilter_test)
2782         SET_EXPECT(MimeFilter_Terminate);
2783     else
2784         SET_EXPECT(Terminate);
2785     hres = IInternetProtocol_Terminate(protocol, 0);
2786     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2787     if(mimefilter_test)
2788         CHECK_CALLED(MimeFilter_Terminate);
2789     else
2790         CHECK_CALLED(Terminate);
2791
2792     if(filtered_protocol)
2793         IInternetProtocol_Release(filtered_protocol);
2794     IInternetBindInfo_Release(prot_bind_info);
2795     IInternetProtocolSink_Release(binding_sink);
2796     ref = IInternetProtocol_Release(protocol);
2797     ok(!ref, "ref=%u, expected 0\n", ref);
2798 }
2799
2800 static void register_filter(void)
2801 {
2802     IInternetSession *session;
2803     HRESULT hres;
2804
2805     static const WCHAR gzipW[] = {'g','z','i','p',0};
2806
2807     CoInternetGetSession(0, &session, 0);
2808
2809     hres = IInternetSession_RegisterMimeFilter(session, &mimefilter_cf, &IID_IInternetProtocol, gzipW);
2810     ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
2811
2812     IInternetSession_Release(session);
2813 }
2814
2815 START_TEST(protocol)
2816 {
2817     OleInitialize(NULL);
2818
2819     event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
2820     event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
2821
2822     register_filter();
2823
2824     test_file_protocol();
2825     test_http_protocol();
2826     test_https_protocol();
2827     test_ftp_protocol();
2828     test_gopher_protocol();
2829     test_mk_protocol();
2830     test_CreateBinding();
2831     test_binding(FILE_TEST, 0, FALSE);
2832     test_binding(HTTP_TEST, 0, FALSE);
2833     test_binding(FILE_TEST, PI_MIMEVERIFICATION, FALSE);
2834     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, FALSE);
2835     test_binding(FILE_TEST, PI_MIMEVERIFICATION, TRUE);
2836     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TRUE);
2837
2838     CloseHandle(event_complete);
2839     CloseHandle(event_complete2);
2840
2841     OleUninitialize();
2842 }