shell32: Update Slovenian translation.
[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     *ppv = NULL;
857
858     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid))
859         *ppv = &protocol_sink;
860     if(IsEqualGUID(&IID_IServiceProvider, riid))
861         *ppv = &service_provider;
862     if(IsEqualGUID(&IID_IUriContainer, riid))
863         return E_NOINTERFACE; /* TODO */
864
865     if(*ppv)
866         return S_OK;
867
868     ok(0, "unexpected call %s\n", debugstr_guid(riid));
869     return E_NOINTERFACE;
870 }
871
872 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
873 {
874     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
875         *ppv = iface;
876         return S_OK;
877     }
878     return E_NOINTERFACE;
879 }
880
881 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
882 {
883     return 2;
884 }
885
886 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
887 {
888     return 1;
889 }
890
891 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
892 {
893     DWORD cbSize;
894
895     static const CHAR szPostData[] = "mode=Test";
896
897     CHECK_EXPECT(GetBindInfo);
898
899     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
900     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
901     ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
902
903     *grfBINDF = bindf;
904     if(binding_test)
905         *grfBINDF |= BINDF_FROMURLMON;
906     cbSize = pbindinfo->cbSize;
907     memset(pbindinfo, 0, cbSize);
908     pbindinfo->cbSize = cbSize;
909
910     if (http_post_test)
911     {
912         /* Must be GMEM_FIXED, GMEM_MOVABLE does not work properly
913          * with urlmon on native (Win98 and WinXP) */
914         U(pbindinfo->stgmedData).hGlobal = GlobalAlloc(GPTR, sizeof(szPostData));
915         if (!U(pbindinfo->stgmedData).hGlobal)
916         {
917             http_post_test = FALSE;
918             skip("Out of memory\n");
919             return E_OUTOFMEMORY;
920         }
921         lstrcpy((LPSTR)U(pbindinfo->stgmedData).hGlobal, szPostData);
922         pbindinfo->cbstgmedData = sizeof(szPostData)-1;
923         pbindinfo->dwBindVerb = BINDVERB_POST;
924         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
925     }
926
927     return S_OK;
928 }
929
930 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType,
931         LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
932 {
933     ok(ppwzStr != NULL, "ppwzStr == NULL\n");
934     ok(pcElFetched != NULL, "pcElFetched == NULL\n");
935
936     switch(ulStringType) {
937     case BINDSTRING_ACCEPT_MIMES:
938         CHECK_EXPECT(GetBindString_ACCEPT_MIMES);
939         ok(cEl == 256, "cEl=%d, expected 256\n", cEl);
940         if(pcElFetched) {
941             ok(*pcElFetched == 256, "*pcElFetched=%d, expected 256\n", *pcElFetched);
942             *pcElFetched = 1;
943         }
944         if(ppwzStr) {
945             *ppwzStr = CoTaskMemAlloc(sizeof(acc_mimeW));
946             memcpy(*ppwzStr, acc_mimeW, sizeof(acc_mimeW));
947         }
948         return S_OK;
949     case BINDSTRING_USER_AGENT:
950         CHECK_EXPECT(GetBindString_USER_AGENT);
951         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
952         if(pcElFetched) {
953             ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
954             *pcElFetched = 1;
955         }
956         if(ppwzStr) {
957             *ppwzStr = CoTaskMemAlloc(sizeof(user_agentW));
958             memcpy(*ppwzStr, user_agentW, sizeof(user_agentW));
959         }
960         return S_OK;
961     case BINDSTRING_POST_COOKIE:
962         CHECK_EXPECT(GetBindString_POST_COOKIE);
963         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
964         if(pcElFetched)
965             ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
966         return S_OK;
967     case BINDSTRING_URL: {
968         DWORD size;
969
970         CHECK_EXPECT(GetBindString_URL);
971         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
972         ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
973         *pcElFetched = 1;
974
975         size = (lstrlenW(binding_urls[tested_protocol])+1)*sizeof(WCHAR);
976         *ppwzStr = CoTaskMemAlloc(size);
977         memcpy(*ppwzStr, binding_urls[tested_protocol], size);
978         return S_OK;
979     }
980     default:
981         ok(0, "unexpected call\n");
982     }
983
984     return E_NOTIMPL;
985 }
986
987 static IInternetBindInfoVtbl bind_info_vtbl = {
988     BindInfo_QueryInterface,
989     BindInfo_AddRef,
990     BindInfo_Release,
991     BindInfo_GetBindInfo,
992     BindInfo_GetBindString
993 };
994
995 static IInternetBindInfo bind_info = { &bind_info_vtbl };
996
997 static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
998                                                   REFIID riid, void **ppv)
999 {
1000     ok(0, "unexpected call\n");
1001     return E_NOINTERFACE;
1002 }
1003
1004 static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
1005 {
1006     return 2;
1007 }
1008
1009 static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
1010 {
1011     return 1;
1012 }
1013
1014 static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
1015 {
1016     CHECK_EXPECT(SetPriority);
1017     ok(nPriority == ex_priority, "nPriority=%d\n", nPriority);
1018     return S_OK;
1019 }
1020
1021 static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
1022 {
1023     ok(0, "unexpected call\n");
1024     return E_NOTIMPL;
1025 }
1026
1027
1028 static const IInternetPriorityVtbl InternetPriorityVtbl = {
1029     InternetPriority_QueryInterface,
1030     InternetPriority_AddRef,
1031     InternetPriority_Release,
1032     InternetPriority_SetPriority,
1033     InternetPriority_GetPriority
1034 };
1035
1036 static IInternetPriority InternetPriority = { &InternetPriorityVtbl };
1037
1038 static ULONG WINAPI Protocol_AddRef(IInternetProtocol *iface)
1039 {
1040     return 2;
1041 }
1042
1043 static ULONG WINAPI Protocol_Release(IInternetProtocol *iface)
1044 {
1045     return 1;
1046 }
1047
1048 static HRESULT WINAPI Protocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
1049         DWORD dwOptions)
1050 {
1051     ok(0, "unexpected call\n");
1052     return E_NOTIMPL;
1053 }
1054
1055 static HRESULT WINAPI Protocol_Suspend(IInternetProtocol *iface)
1056 {
1057     ok(0, "unexpected call\n");
1058     return E_NOTIMPL;
1059 }
1060
1061 static HRESULT WINAPI Protocol_Resume(IInternetProtocol *iface)
1062 {
1063     ok(0, "unexpected call\n");
1064     return E_NOTIMPL;
1065 }
1066
1067 static HRESULT WINAPI Protocol_Seek(IInternetProtocol *iface,
1068         LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
1069 {
1070     ok(0, "unexpected call\n");
1071     return E_NOTIMPL;
1072 }
1073
1074 static HRESULT WINAPI ProtocolEmul_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
1075 {
1076     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
1077         *ppv = iface;
1078         return S_OK;
1079     }
1080
1081     if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
1082         trace("IID_IInternetProtocolEx not supported\n");
1083         *ppv = NULL;
1084         return E_NOINTERFACE;
1085     }
1086
1087     if(IsEqualGUID(&IID_IInternetPriority, riid)) {
1088         *ppv = &InternetPriority;
1089         return S_OK;
1090     }
1091
1092     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1093     *ppv = NULL;
1094     return E_NOINTERFACE;
1095 }
1096
1097 static DWORD WINAPI thread_proc(PVOID arg)
1098 {
1099     HRESULT hres;
1100
1101     memset(&protocoldata, -1, sizeof(protocoldata));
1102
1103     prot_state = 0;
1104
1105     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
1106     hres = IInternetProtocolSink_ReportProgress(binding_sink,
1107             BINDSTATUS_FINDINGRESOURCE, hostW);
1108     CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
1109     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
1110
1111     SET_EXPECT(ReportProgress_CONNECTING);
1112     hres = IInternetProtocolSink_ReportProgress(binding_sink,
1113             BINDSTATUS_CONNECTING, winehq_ipW);
1114     CHECK_CALLED(ReportProgress_CONNECTING);
1115     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
1116
1117     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1118     hres = IInternetProtocolSink_ReportProgress(binding_sink,
1119             BINDSTATUS_SENDINGREQUEST, NULL);
1120     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1121     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
1122
1123     prot_state = 1;
1124     SET_EXPECT(Switch);
1125     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1126     CHECK_CALLED(Switch);
1127     ok(hres == S_OK, "Switch failed: %08x\n", hres);
1128
1129     prot_state = 2;
1130     if(mimefilter_test)
1131         SET_EXPECT(MimeFilter_Switch);
1132     else
1133         SET_EXPECT(Switch);
1134     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1135     ok(hres == S_OK, "Switch failed: %08x\n", hres);
1136     if(mimefilter_test)
1137         CHECK_CALLED(MimeFilter_Switch);
1138     else
1139         CHECK_CALLED(Switch);
1140
1141     prot_state = 2;
1142     if(mimefilter_test)
1143         SET_EXPECT(MimeFilter_Switch);
1144     else
1145         SET_EXPECT(Switch);
1146     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1147     ok(hres == S_OK, "Switch failed: %08x\n", hres);
1148     if(mimefilter_test)
1149         CHECK_CALLED(MimeFilter_Switch);
1150     else
1151         CHECK_CALLED(Switch);
1152
1153     prot_state = 3;
1154     if(mimefilter_test)
1155         SET_EXPECT(MimeFilter_Switch);
1156     else
1157         SET_EXPECT(Switch);
1158     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
1159     ok(hres == S_OK, "Switch failed: %08x\n", hres);
1160     if(mimefilter_test)
1161         CHECK_CALLED(MimeFilter_Switch);
1162     else
1163         CHECK_CALLED(Switch);
1164
1165     SetEvent(event_complete);
1166
1167     return 0;
1168 }
1169
1170 static HRESULT WINAPI ProtocolEmul_Start(IInternetProtocol *iface, LPCWSTR szUrl,
1171         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
1172         DWORD grfPI, HANDLE_PTR dwReserved)
1173 {
1174     BINDINFO bindinfo, exp_bindinfo;
1175     DWORD cbindf = 0;
1176     HRESULT hres;
1177
1178     CHECK_EXPECT(Start);
1179
1180     ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
1181     ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
1182     ok(pOIProtSink != &protocol_sink, "unexpected pOIProtSink\n");
1183     ok(pOIBindInfo != &bind_info, "unexpected pOIBindInfo\n");
1184     ok(!grfPI, "grfPI = %x\n", grfPI);
1185     ok(!dwReserved, "dwReserved = %lx\n", dwReserved);
1186
1187     if(binding_test)
1188         ok(pOIProtSink == binding_sink, "pOIProtSink != binding_sink\n");
1189
1190     memset(&bindinfo, 0, sizeof(bindinfo));
1191     bindinfo.cbSize = sizeof(bindinfo);
1192     memcpy(&exp_bindinfo, &bindinfo, sizeof(bindinfo));
1193     SET_EXPECT(GetBindInfo);
1194     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
1195     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
1196     CHECK_CALLED(GetBindInfo);
1197     ok(cbindf == (bindf|BINDF_FROMURLMON), "bindf = %x, expected %x\n",
1198        cbindf, (bindf|BINDF_FROMURLMON));
1199     ok(!memcmp(&exp_bindinfo, &bindinfo, sizeof(bindinfo)), "unexpected bindinfo\n");
1200     ReleaseBindInfo(&bindinfo);
1201
1202     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1203     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, emptyW);
1204     ok(hres == S_OK, "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
1205     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1206
1207     if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
1208         IServiceProvider *service_provider;
1209         IHttpNegotiate *http_negotiate;
1210         IHttpNegotiate2 *http_negotiate2;
1211         LPWSTR ua = (LPWSTR)0xdeadbeef, accept_mimes[256];
1212         LPWSTR additional_headers = NULL;
1213         BYTE sec_id[100];
1214         DWORD fetched = 0, size = 100;
1215         DWORD tid;
1216
1217         SET_EXPECT(GetBindString_USER_AGENT);
1218         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT,
1219                                                &ua, 1, &fetched);
1220         CHECK_CALLED(GetBindString_USER_AGENT);
1221         ok(hres == S_OK, "GetBindString(BINDSTRING_USER_AGETNT) failed: %08x\n", hres);
1222         ok(fetched == 1, "fetched = %d, expected 254\n", fetched);
1223         ok(ua != NULL, "ua =  %p\n", ua);
1224         ok(!strcmp_ww(ua, user_agentW), "unexpected user agent %s\n", debugstr_w(ua));
1225         CoTaskMemFree(ua);
1226
1227         fetched = 256;
1228         SET_EXPECT(GetBindString_ACCEPT_MIMES);
1229         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
1230                                                accept_mimes, 256, &fetched);
1231         CHECK_CALLED(GetBindString_ACCEPT_MIMES);
1232
1233         ok(hres == S_OK,
1234            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
1235         ok(fetched == 1, "fetched = %d, expected 1\n", fetched);
1236         ok(!strcmp_ww(acc_mimeW, accept_mimes[0]), "unexpected mimes %s\n", debugstr_w(accept_mimes[0]));
1237
1238         hres = IInternetBindInfo_QueryInterface(pOIBindInfo, &IID_IServiceProvider,
1239                                                 (void**)&service_provider);
1240         ok(hres == S_OK, "QueryInterface failed: %08x\n", hres);
1241
1242         SET_EXPECT(QueryService_HttpNegotiate);
1243         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
1244                 &IID_IHttpNegotiate, (void**)&http_negotiate);
1245         CHECK_CALLED(QueryService_HttpNegotiate);
1246         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
1247
1248         SET_EXPECT(BeginningTransaction);
1249         hres = IHttpNegotiate_BeginningTransaction(http_negotiate, binding_urls[tested_protocol],
1250                                                    NULL, 0, &additional_headers);
1251         CHECK_CALLED(BeginningTransaction);
1252         IHttpNegotiate_Release(http_negotiate);
1253         ok(hres == S_OK, "BeginningTransction failed: %08x\n", hres);
1254         ok(additional_headers == NULL, "additional_headers=%p\n", additional_headers);
1255
1256         SET_EXPECT(QueryService_HttpNegotiate);
1257         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
1258                 &IID_IHttpNegotiate2, (void**)&http_negotiate2);
1259         CHECK_CALLED(QueryService_HttpNegotiate);
1260         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
1261
1262         size = 512;
1263         SET_EXPECT(GetRootSecurityId);
1264         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, sec_id, &size, 0);
1265         CHECK_CALLED(GetRootSecurityId);
1266         IHttpNegotiate2_Release(http_negotiate2);
1267         ok(hres == E_FAIL, "GetRootSecurityId failed: %08x, expected E_FAIL\n", hres);
1268         ok(size == 13, "size=%d\n", size);
1269
1270         IServiceProvider_Release(service_provider);
1271
1272         CreateThread(NULL, 0, thread_proc, NULL, 0, &tid);
1273
1274         return S_OK;
1275     }
1276
1277     SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1278     hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
1279             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
1280     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
1281     CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
1282
1283     if(mimefilter_test) {
1284         SET_EXPECT(MimeFilter_CreateInstance);
1285         SET_EXPECT(MimeFilter_Start);
1286         SET_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
1287     }
1288     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1289     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
1290             mimefilter_test ? gzipW : (expect_wsz = text_htmlW));
1291     ok(hres == S_OK,
1292        "ReportProgress(BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE) failed: %08x\n", hres);
1293     if(mimefilter_test) {
1294         CHECK_CALLED(MimeFilter_CreateInstance);
1295         CHECK_CALLED(MimeFilter_Start);
1296         CHECK_CALLED(ReportProgress_LOADINGMIMEHANDLER);
1297         todo_wine CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1298     }else {
1299         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1300     }
1301
1302     if(mimefilter_test)
1303         SET_EXPECT(MimeFilter_ReportData);
1304     else
1305         SET_EXPECT(ReportData);
1306     hres = IInternetProtocolSink_ReportData(pOIProtSink,
1307             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION, 13, 13);
1308     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1309     if(mimefilter_test)
1310         CHECK_CALLED(MimeFilter_ReportData);
1311     else
1312         CHECK_CALLED(ReportData);
1313
1314     if(tested_protocol == BIND_TEST) {
1315         hres = IInternetProtocol_Terminate(binding_protocol, 0);
1316         ok(hres == E_FAIL, "Termiante failed: %08x\n", hres);
1317     }
1318
1319     if(mimefilter_test)
1320         SET_EXPECT(MimeFilter_ReportResult);
1321     else
1322         SET_EXPECT(ReportResult);
1323     hres = IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
1324     ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1325     if(mimefilter_test)
1326         CHECK_CALLED(MimeFilter_ReportResult);
1327     else
1328         CHECK_CALLED(ReportResult);
1329
1330     return S_OK;
1331 }
1332
1333 static HRESULT WINAPI ProtocolEmul_Continue(IInternetProtocol *iface,
1334         PROTOCOLDATA *pProtocolData)
1335 {
1336     DWORD bscf = 0, pr;
1337     HRESULT hres;
1338
1339     CHECK_EXPECT(Continue);
1340
1341     ok(pProtocolData != NULL, "pProtocolData == NULL\n");
1342     if(!pProtocolData || tested_protocol == BIND_TEST)
1343         return S_OK;
1344     if(binding_test) {
1345         ok(pProtocolData != &protocoldata, "pProtocolData == &protocoldata\n");
1346         ok(pProtocolData->grfFlags == protocoldata.grfFlags, "grfFlags wrong %x/%x\n",
1347            pProtocolData->grfFlags, protocoldata.grfFlags );
1348         ok(pProtocolData->dwState == protocoldata.dwState, "dwState wrong %x/%x\n",
1349            pProtocolData->dwState, protocoldata.dwState );
1350         ok(pProtocolData->pData == protocoldata.pData, "pData wrong %p/%p\n",
1351            pProtocolData->pData, protocoldata.pData );
1352         ok(pProtocolData->cbData == protocoldata.cbData, "cbData wrong %x/%x\n",
1353            pProtocolData->cbData, protocoldata.cbData );
1354     }
1355
1356     switch(prot_state) {
1357     case 1: {
1358         IServiceProvider *service_provider;
1359         IHttpNegotiate *http_negotiate;
1360         static WCHAR header[] = {'?',0};
1361
1362         hres = IInternetProtocolSink_QueryInterface(binding_sink, &IID_IServiceProvider,
1363                                                     (void**)&service_provider);
1364         ok(hres == S_OK, "Could not get IServiceProvicder\n");
1365
1366         SET_EXPECT(QueryService_HttpNegotiate);
1367         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
1368                                              &IID_IHttpNegotiate, (void**)&http_negotiate);
1369         IServiceProvider_Release(service_provider);
1370         CHECK_CALLED(QueryService_HttpNegotiate);
1371         ok(hres == S_OK, "Could not get IHttpNegotiate\n");
1372
1373         SET_EXPECT(OnResponse);
1374         hres = IHttpNegotiate_OnResponse(http_negotiate, 200, header, NULL, NULL);
1375         IHttpNegotiate_Release(http_negotiate);
1376         CHECK_CALLED(OnResponse);
1377         IHttpNegotiate_Release(http_negotiate);
1378         ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
1379
1380         if(mimefilter_test) {
1381             SET_EXPECT(MimeFilter_CreateInstance);
1382             SET_EXPECT(MimeFilter_Start);
1383             SET_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
1384         }else if(!(pi & PI_MIMEVERIFICATION)) {
1385             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1386         }
1387         hres = IInternetProtocolSink_ReportProgress(binding_sink,
1388                 BINDSTATUS_MIMETYPEAVAILABLE, mimefilter_test ? gzipW : text_htmlW);
1389         if(mimefilter_test) {
1390             CHECK_CALLED(MimeFilter_CreateInstance);
1391             CHECK_CALLED(MimeFilter_Start);
1392             CHECK_CALLED(ReportProgress_LOADINGMIMEHANDLER);
1393         }else if(!(pi & PI_MIMEVERIFICATION)) {
1394             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1395         }
1396         ok(hres == S_OK,
1397            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
1398
1399         bscf |= BSCF_FIRSTDATANOTIFICATION;
1400         break;
1401     }
1402     case 2:
1403     case 3:
1404         bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
1405         break;
1406     }
1407
1408     pr = prot_read;
1409     if(mimefilter_test) {
1410         SET_EXPECT(MimeFilter_ReportData);
1411     }else if(pi & PI_MIMEVERIFICATION) {
1412         if(pr < 200)
1413             SET_EXPECT(Read);
1414         if(pr == 200) {
1415             SET_EXPECT(Read);
1416             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1417         }
1418         if(pr >= 200)
1419             SET_EXPECT(ReportData);
1420     }else {
1421         SET_EXPECT(ReportData);
1422     }
1423
1424     hres = IInternetProtocolSink_ReportData(binding_sink, bscf, 100, 400);
1425     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1426
1427     if(mimefilter_test) {
1428         SET_EXPECT(MimeFilter_ReportData);
1429     }else if(pi & PI_MIMEVERIFICATION) {
1430         if(pr < 200)
1431             CHECK_CALLED(Read);
1432         if(pr == 200) {
1433             CLEAR_CALLED(Read);
1434             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1435         }
1436     }else {
1437         CHECK_CALLED(ReportData);
1438     }
1439
1440     if(prot_state == 3)
1441         prot_state = 4;
1442
1443     return S_OK;
1444 }
1445
1446 static HRESULT WINAPI ProtocolEmul_Terminate(IInternetProtocol *iface, DWORD dwOptions)
1447 {
1448     CHECK_EXPECT(Terminate);
1449     ok(!dwOptions, "dwOptions=%d\n", dwOptions);
1450     return S_OK;
1451 }
1452
1453 static HRESULT WINAPI ProtocolEmul_Read(IInternetProtocol *iface, void *pv,
1454         ULONG cb, ULONG *pcbRead)
1455 {
1456     if(mimefilter_test) {
1457         CHECK_EXPECT2(Read);
1458     }else if((pi & PI_MIMEVERIFICATION)) {
1459         CHECK_EXPECT2(Read);
1460
1461         if(prot_read < 300) {
1462             ok(pv != expect_pv, "pv == expect_pv\n");
1463             if(prot_read < 300)
1464                 ok(cb == 2048-prot_read, "cb=%d\n", cb);
1465             else
1466                 ok(cb == 700, "cb=%d\n", cb);
1467         }else {
1468             ok(expect_pv <= pv && (BYTE*)pv < (BYTE*)expect_pv + cb, "pv != expect_pv\n");
1469         }
1470     }else {
1471         CHECK_EXPECT(Read);
1472
1473         ok(pv == expect_pv, "pv != expect_pv\n");
1474         ok(cb == 1000, "cb=%d\n", cb);
1475         ok(!*pcbRead, "*pcbRead = %d\n", *pcbRead);
1476     }
1477     ok(pcbRead != NULL, "pcbRead == NULL\n");
1478
1479     if(prot_state == 3) {
1480         HRESULT hres;
1481
1482         if(mimefilter_test)
1483             SET_EXPECT(MimeFilter_ReportResult);
1484         else
1485             SET_EXPECT(ReportResult);
1486         hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
1487         ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1488         if(mimefilter_test)
1489             CHECK_CALLED(MimeFilter_ReportResult);
1490         else
1491             CHECK_CALLED(ReportResult);
1492
1493         return S_FALSE;
1494     }
1495
1496     if((async_read_pending = !async_read_pending)) {
1497         *pcbRead = 0;
1498         return tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST ? E_PENDING : S_FALSE;
1499     }
1500
1501     memset(pv, 'x', 100);
1502     prot_read += *pcbRead = 100;
1503     return S_OK;
1504 }
1505
1506 static HRESULT WINAPI ProtocolEmul_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
1507 {
1508     CHECK_EXPECT(LockRequest);
1509     ok(dwOptions == 0, "dwOptions=%x\n", dwOptions);
1510     return S_OK;
1511 }
1512
1513 static HRESULT WINAPI ProtocolEmul_UnlockRequest(IInternetProtocol *iface)
1514 {
1515     CHECK_EXPECT(UnlockRequest);
1516     return S_OK;
1517 }
1518
1519 static const IInternetProtocolVtbl ProtocolVtbl = {
1520     ProtocolEmul_QueryInterface,
1521     Protocol_AddRef,
1522     Protocol_Release,
1523     ProtocolEmul_Start,
1524     ProtocolEmul_Continue,
1525     Protocol_Abort,
1526     ProtocolEmul_Terminate,
1527     Protocol_Suspend,
1528     Protocol_Resume,
1529     ProtocolEmul_Read,
1530     Protocol_Seek,
1531     ProtocolEmul_LockRequest,
1532     ProtocolEmul_UnlockRequest
1533 };
1534
1535 static IInternetProtocol Protocol = { &ProtocolVtbl };
1536
1537 static HRESULT WINAPI MimeProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
1538 {
1539     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
1540         *ppv = iface;
1541         return S_OK;
1542     }
1543
1544     if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
1545         *ppv = &mime_protocol_sink;
1546         return S_OK;
1547     }
1548
1549     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1550     *ppv = NULL;
1551     return E_NOINTERFACE;
1552 }
1553
1554 static HRESULT WINAPI MimeProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
1555         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
1556         DWORD grfPI, HANDLE_PTR dwReserved)
1557 {
1558     PROTOCOLFILTERDATA *data;
1559     LPOLESTR url_str = NULL;
1560     DWORD fetched = 0;
1561     BINDINFO bindinfo;
1562     DWORD cbindf = 0;
1563     HRESULT hres;
1564
1565     CHECK_EXPECT(MimeFilter_Start);
1566
1567     ok(!strcmp_ww(szUrl, gzipW), "wrong url %s\n", debugstr_w(szUrl));
1568     ok(grfPI == (PI_FILTER_MODE|PI_FORCE_ASYNC), "grfPI=%x, expected PI_FILTER_MODE|PI_FORCE_ASYNC\n", grfPI);
1569     ok(dwReserved, "dwReserved == 0\n");
1570     ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
1571     ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
1572
1573     if(binding_test) {
1574         ok(pOIProtSink != binding_sink, "pOIProtSink == protocol_sink\n");
1575         ok(pOIBindInfo == prot_bind_info, "pOIBindInfo != bind_info\n");
1576     }else {
1577         ok(pOIProtSink == &protocol_sink, "pOIProtSink != protocol_sink\n");
1578         ok(pOIBindInfo == &bind_info, "pOIBindInfo != bind_info\n");
1579     }
1580
1581     data = (void*)dwReserved;
1582     ok(data->cbSize == sizeof(*data), "data->cbSize = %d\n", data->cbSize);
1583     ok(!data->pProtocolSink, "data->pProtocolSink != NULL\n");
1584     ok(data->pProtocol != NULL, "data->pProtocol == NULL\n");
1585     ok(!data->pUnk, "data->pUnk != NULL\n");
1586     ok(!data->dwFilterFlags, "data->dwProtocolFlags = %x\n", data->dwFilterFlags);
1587     if(binding_test) {
1588         IInternetProtocolSink *prot_sink;
1589
1590         IInternetProtocol_QueryInterface(data->pProtocol, &IID_IInternetProtocolSink, (void**)&prot_sink);
1591         ok(prot_sink == pOIProtSink, "QI(data->pProtocol, IID_IInternetProtocolSink) != pOIProtSink\n");
1592         IInternetProtocolSink_Release(prot_sink);
1593
1594         ok(data->pProtocol != binding_protocol, "data->pProtocol == binding_protocol\n");
1595
1596         filtered_protocol = data->pProtocol;
1597         IInternetProtocol_AddRef(filtered_protocol);
1598     }else {
1599         IInternetProtocol *prot;
1600
1601         IInternetProtocol_QueryInterface(data->pProtocol, &IID_IInternetProtocol, (void**)&prot);
1602         ok(prot == async_protocol, "QI(data->pProtocol, IID_IInternetProtocol) != async_protocol\n");
1603         IInternetProtocol_Release(prot);
1604
1605         ok(data->pProtocol != async_protocol, "data->pProtocol == async_protocol\n");
1606     }
1607
1608     filtered_sink = pOIProtSink;
1609
1610     SET_EXPECT(ReportProgress_DECODING);
1611     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DECODING, gzipW);
1612     ok(hres == S_OK, "ReportProgress(BINDSTATUS_DECODING) failed: %08x\n", hres);
1613     CHECK_CALLED(ReportProgress_DECODING);
1614
1615     SET_EXPECT(GetBindInfo);
1616     memset(&bindinfo, 0, sizeof(bindinfo));
1617     bindinfo.cbSize = sizeof(bindinfo);
1618     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
1619     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
1620     ok(cbindf == bindf, "cbindf = %x\n", cbindf);
1621     CHECK_CALLED(GetBindInfo);
1622
1623     SET_EXPECT(GetBindString_URL);
1624     hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_URL, &url_str, 1, &fetched);
1625     ok(hres == S_OK, "GetBindString(BINDSTRING_URL) failed: %08x\n", hres);
1626     ok(fetched == 1, "fetched = %d\n", fetched);
1627     ok(!strcmp_ww(url_str, binding_urls[tested_protocol]), "wrong url_str %s\n", debugstr_w(url_str));
1628     CoTaskMemFree(url_str);
1629     CHECK_CALLED(GetBindString_URL);
1630
1631     return S_OK;
1632 }
1633
1634 static HRESULT WINAPI Protocol_Continue(IInternetProtocol *iface,
1635         PROTOCOLDATA *pProtocolData)
1636 {
1637     CHECK_EXPECT(MimeFilter_Continue);
1638     return E_NOTIMPL;
1639 }
1640
1641 static HRESULT WINAPI MimeProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
1642 {
1643     HRESULT hres;
1644
1645     CHECK_EXPECT(MimeFilter_Terminate);
1646
1647     ok(!dwOptions, "dwOptions = %x\n", dwOptions);
1648
1649     SET_EXPECT(Terminate);
1650     hres = IInternetProtocol_Terminate(filtered_protocol, dwOptions);
1651     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1652     CHECK_CALLED(Terminate);
1653
1654     return S_OK;
1655 }
1656
1657 static HRESULT WINAPI MimeProtocol_Read(IInternetProtocol *iface, void *pv,
1658         ULONG cb, ULONG *pcbRead)
1659 {
1660     BYTE buf[2096];
1661     DWORD read = 0;
1662     HRESULT hres;
1663
1664     CHECK_EXPECT(MimeFilter_Read);
1665
1666     ok(pv != NULL, "pv == NULL\n");
1667     ok(cb != 0, "cb == 0\n");
1668     ok(pcbRead != NULL, "pcbRead == NULL\n");
1669
1670     SET_EXPECT(Read);
1671     hres = IInternetProtocol_Read(filtered_protocol, buf, sizeof(buf), &read);
1672     ok(hres == S_OK || hres == S_FALSE || hres == E_PENDING, "Read failed: %08x\n", hres);
1673     CHECK_CALLED(Read);
1674
1675     if(pcbRead) {
1676         ok(*pcbRead == 0, "*pcbRead=%d, expected 0\n", *pcbRead);
1677         *pcbRead = read;
1678     }
1679
1680     memset(pv, 'x', read);
1681     return hres;
1682 }
1683
1684 static HRESULT WINAPI MimeProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
1685 {
1686     HRESULT hres;
1687
1688     CHECK_EXPECT(MimeFilter_LockRequest);
1689
1690     ok(!dwOptions, "dwOptions = %x\n", dwOptions);
1691
1692     SET_EXPECT(LockRequest);
1693     hres = IInternetProtocol_LockRequest(filtered_protocol, dwOptions);
1694     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1695     CHECK_CALLED(LockRequest);
1696
1697     return S_OK;
1698 }
1699
1700 static HRESULT WINAPI MimeProtocol_UnlockRequest(IInternetProtocol *iface)
1701 {
1702     HRESULT hres;
1703
1704     CHECK_EXPECT(MimeFilter_UnlockRequest);
1705
1706     SET_EXPECT(UnlockRequest);
1707     hres = IInternetProtocol_UnlockRequest(filtered_protocol);
1708     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1709     CHECK_CALLED(UnlockRequest);
1710
1711     return S_OK;
1712 }
1713
1714 static const IInternetProtocolVtbl MimeProtocolVtbl = {
1715     MimeProtocol_QueryInterface,
1716     Protocol_AddRef,
1717     Protocol_Release,
1718     MimeProtocol_Start,
1719     Protocol_Continue,
1720     Protocol_Abort,
1721     MimeProtocol_Terminate,
1722     Protocol_Suspend,
1723     Protocol_Resume,
1724     MimeProtocol_Read,
1725     Protocol_Seek,
1726     MimeProtocol_LockRequest,
1727     MimeProtocol_UnlockRequest
1728 };
1729
1730 static IInternetProtocol MimeProtocol = { &MimeProtocolVtbl };
1731
1732 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
1733 {
1734     ok(0, "unexpected call\n");
1735     return E_NOINTERFACE;
1736 }
1737
1738 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
1739 {
1740     return 2;
1741 }
1742
1743 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
1744 {
1745     return 1;
1746 }
1747
1748 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
1749                                         REFIID riid, void **ppv)
1750 {
1751     CHECK_EXPECT(CreateInstance);
1752
1753     ok(pOuter == (IUnknown*)prot_bind_info, "pOuter != protocol_unk\n");
1754     ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", debugstr_guid(riid));
1755     ok(ppv != NULL, "ppv == NULL\n");
1756
1757     *ppv = &Protocol;
1758     return S_OK;
1759 }
1760
1761 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
1762 {
1763     ok(0, "unexpected call\n");
1764     return S_OK;
1765 }
1766
1767 static const IClassFactoryVtbl ClassFactoryVtbl = {
1768     ClassFactory_QueryInterface,
1769     ClassFactory_AddRef,
1770     ClassFactory_Release,
1771     ClassFactory_CreateInstance,
1772     ClassFactory_LockServer
1773 };
1774
1775 static IClassFactory ClassFactory = { &ClassFactoryVtbl };
1776
1777 static HRESULT WINAPI MimeFilter_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
1778 {
1779     CHECK_EXPECT(MimeFilter_CreateInstance);
1780
1781     ok(!outer, "outer = %p\n", outer);
1782     ok(IsEqualGUID(&IID_IInternetProtocol, riid), "unexpected riid %s\n", debugstr_guid(riid));
1783
1784     *ppv = &MimeProtocol;
1785     return S_OK;
1786 }
1787
1788 static const IClassFactoryVtbl MimeFilterCFVtbl = {
1789     ClassFactory_QueryInterface,
1790     ClassFactory_AddRef,
1791     ClassFactory_Release,
1792     MimeFilter_CreateInstance,
1793     ClassFactory_LockServer
1794 };
1795
1796 static IClassFactory mimefilter_cf = { &MimeFilterCFVtbl };
1797
1798 #define TEST_BINDING   1
1799 #define TEST_FILTER    2
1800
1801 static void init_test(int prot, DWORD flags)
1802 {
1803     tested_protocol = prot;
1804     binding_test = (flags & TEST_BINDING) != 0;
1805     first_data_notif = TRUE;
1806     prot_read = 0;
1807     prot_state = 0;
1808     async_read_pending = TRUE;
1809     mimefilter_test = (flags & TEST_FILTER) != 0;
1810     filter_state = 0;
1811     ResetEvent(event_complete);
1812     ResetEvent(event_complete2);
1813     async_protocol = binding_protocol = filtered_protocol = NULL;
1814     filtered_sink = NULL;
1815 }
1816
1817 static void test_priority(IInternetProtocol *protocol)
1818 {
1819     IInternetPriority *priority;
1820     LONG pr;
1821     HRESULT hres;
1822
1823     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority,
1824                                             (void**)&priority);
1825     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
1826     if(FAILED(hres))
1827         return;
1828
1829     hres = IInternetPriority_GetPriority(priority, &pr);
1830     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1831     ok(pr == 0, "pr=%d, expected 0\n", pr);
1832
1833     hres = IInternetPriority_SetPriority(priority, 1);
1834     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
1835
1836     hres = IInternetPriority_GetPriority(priority, &pr);
1837     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1838     ok(pr == 1, "pr=%d, expected 1\n", pr);
1839
1840     IInternetPriority_Release(priority);
1841 }
1842
1843 static BOOL file_protocol_start(IInternetProtocol *protocol, LPCWSTR url, BOOL is_first)
1844 {
1845     HRESULT hres;
1846
1847     SET_EXPECT(GetBindInfo);
1848     if(!(bindf & BINDF_FROMURLMON))
1849        SET_EXPECT(ReportProgress_DIRECTBIND);
1850     if(is_first) {
1851         SET_EXPECT(ReportProgress_SENDINGREQUEST);
1852         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1853         if(bindf & BINDF_FROMURLMON)
1854             SET_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1855         else
1856             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1857     }
1858     SET_EXPECT(ReportData);
1859     if(is_first)
1860         SET_EXPECT(ReportResult);
1861
1862     expect_hrResult = S_OK;
1863
1864     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
1865     if(hres == INET_E_RESOURCE_NOT_FOUND) {
1866         win_skip("Start failed\n");
1867         return FALSE;
1868     }
1869     ok(hres == S_OK, "Start failed: %08x\n", hres);
1870
1871     CHECK_CALLED(GetBindInfo);
1872     if(!(bindf & BINDF_FROMURLMON))
1873        CHECK_CALLED(ReportProgress_DIRECTBIND);
1874     if(is_first) {
1875         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1876         CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
1877         if(bindf & BINDF_FROMURLMON)
1878             CHECK_CALLED(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1879         else
1880             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1881     }
1882     CHECK_CALLED(ReportData);
1883     if(is_first)
1884         CHECK_CALLED(ReportResult);
1885
1886     return TRUE;
1887 }
1888
1889 static void test_file_protocol_url(LPCWSTR url)
1890 {
1891     IInternetProtocolInfo *protocol_info;
1892     IUnknown *unk;
1893     IClassFactory *factory;
1894     HRESULT hres;
1895
1896     hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
1897             &IID_IUnknown, (void**)&unk);
1898     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1899     if(FAILED(hres))
1900         return;
1901
1902     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1903     ok(hres == E_NOINTERFACE,
1904             "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1905
1906     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1907     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1908     if(SUCCEEDED(hres)) {
1909         IInternetProtocol *protocol;
1910         BYTE buf[512];
1911         ULONG cb;
1912         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1913         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1914
1915         if(SUCCEEDED(hres)) {
1916             if(file_protocol_start(protocol, url, TRUE)) {
1917                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1918                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1919                 ok(cb == 2, "cb=%u expected 2\n", cb);
1920                 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1921                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1922                 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1923                 ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
1924                 ok(cb == 0, "cb=%u expected 0\n", cb);
1925                 hres = IInternetProtocol_UnlockRequest(protocol);
1926                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1927             }
1928
1929             if(file_protocol_start(protocol, url, FALSE)) {
1930                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1931                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1932                 hres = IInternetProtocol_LockRequest(protocol, 0);
1933                 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1934                 hres = IInternetProtocol_UnlockRequest(protocol);
1935                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1936             }
1937
1938             IInternetProtocol_Release(protocol);
1939         }
1940
1941         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1942         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1943
1944         if(SUCCEEDED(hres)) {
1945             if(file_protocol_start(protocol, url, TRUE)) {
1946                 hres = IInternetProtocol_LockRequest(protocol, 0);
1947                 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1948                 hres = IInternetProtocol_Terminate(protocol, 0);
1949                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1950                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1951                 ok(hres == S_OK, "Read failed: %08x\n\n", hres);
1952                 hres = IInternetProtocol_UnlockRequest(protocol);
1953                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1954                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1955                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1956                 hres = IInternetProtocol_Terminate(protocol, 0);
1957                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1958             }
1959
1960             IInternetProtocol_Release(protocol);
1961         }
1962
1963         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1964         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1965
1966         if(SUCCEEDED(hres)) {
1967             if(file_protocol_start(protocol, url, TRUE)) {
1968                 hres = IInternetProtocol_Terminate(protocol, 0);
1969                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1970                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1971                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1972                 ok(cb == 2, "cb=%u expected 2\n", cb);
1973             }
1974
1975             IInternetProtocol_Release(protocol);
1976         }
1977
1978         IClassFactory_Release(factory);
1979     }
1980
1981     IUnknown_Release(unk);
1982 }
1983
1984 static void test_file_protocol_fail(void)
1985 {
1986     IInternetProtocol *protocol;
1987     HRESULT hres;
1988
1989     static const WCHAR index_url2[] =
1990         {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
1991
1992     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1993             &IID_IInternetProtocol, (void**)&protocol);
1994     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
1995     if(FAILED(hres))
1996         return;
1997
1998     SET_EXPECT(GetBindInfo);
1999     expect_hrResult = MK_E_SYNTAX;
2000     hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
2001     ok(hres == MK_E_SYNTAX ||
2002        hres == E_INVALIDARG,
2003        "Start failed: %08x, expected MK_E_SYNTAX or E_INVALIDARG\n", hres);
2004     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2005
2006     SET_EXPECT(GetBindInfo);
2007     if(!(bindf & BINDF_FROMURLMON))
2008         SET_EXPECT(ReportProgress_DIRECTBIND);
2009     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2010     SET_EXPECT(ReportResult);
2011     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2012     hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
2013     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2014             "Start failed: %08x expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2015     CHECK_CALLED(GetBindInfo);
2016     if(!(bindf & BINDF_FROMURLMON))
2017         CHECK_CALLED(ReportProgress_DIRECTBIND);
2018     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2019     CHECK_CALLED(ReportResult);
2020
2021     IInternetProtocol_Release(protocol);
2022
2023     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2024             &IID_IInternetProtocol, (void**)&protocol);
2025     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2026     if(FAILED(hres))
2027         return;
2028
2029     SET_EXPECT(GetBindInfo);
2030     if(!(bindf & BINDF_FROMURLMON))
2031         SET_EXPECT(ReportProgress_DIRECTBIND);
2032     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2033     SET_EXPECT(ReportResult);
2034     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2035
2036     hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
2037     ok(hres == INET_E_RESOURCE_NOT_FOUND,
2038             "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2039     CHECK_CALLED(GetBindInfo);
2040     if(!(bindf & BINDF_FROMURLMON))
2041         CHECK_CALLED(ReportProgress_DIRECTBIND);
2042     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2043     CHECK_CALLED(ReportResult);
2044
2045     SET_EXPECT(GetBindInfo);
2046     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
2047     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2048     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2049
2050     SET_EXPECT(GetBindInfo);
2051     hres = IInternetProtocol_Start(protocol, emptyW, &protocol_sink, &bind_info, 0, 0);
2052     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2053     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2054
2055     IInternetProtocol_Release(protocol);
2056 }
2057
2058 static void test_file_protocol(void) {
2059     WCHAR buf[INTERNET_MAX_URL_LENGTH], file_name_buf[MAX_PATH];
2060     DWORD size;
2061     ULONG len;
2062     HANDLE file;
2063
2064     static const WCHAR wszFile[] = {'f','i','l','e',':',0};
2065     static const WCHAR wszFile2[] = {'f','i','l','e',':','/','/',0};
2066     static const WCHAR wszFile3[] = {'f','i','l','e',':','/','/','/',0};
2067     static const char html_doc[] = "<HTML></HTML>";
2068
2069     trace("Testing file protocol...\n");
2070     init_test(FILE_TEST, 0);
2071
2072     SetLastError(0xdeadbeef);
2073     file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2074             FILE_ATTRIBUTE_NORMAL, NULL);
2075     if(!file && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2076     {
2077         win_skip("Detected Win9x or WinMe\n");
2078         return;
2079     }
2080     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
2081     if(file == INVALID_HANDLE_VALUE)
2082         return;
2083     WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
2084     CloseHandle(file);
2085
2086     file_name = wszIndexHtml;
2087     bindf = 0;
2088     test_file_protocol_url(index_url);
2089     bindf = BINDF_FROMURLMON;
2090     test_file_protocol_url(index_url);
2091     bindf = BINDF_FROMURLMON | BINDF_NEEDFILE;
2092     test_file_protocol_url(index_url);
2093
2094     memcpy(buf, wszFile, sizeof(wszFile));
2095     len = sizeof(wszFile)/sizeof(WCHAR)-1;
2096     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2097     buf[len++] = '\\';
2098     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2099
2100     file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
2101     bindf = 0;
2102     test_file_protocol_url(buf);
2103     bindf = BINDF_FROMURLMON;
2104     test_file_protocol_url(buf);
2105
2106     memcpy(buf, wszFile2, sizeof(wszFile2));
2107     len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
2108     file_name_buf[len++] = '\\';
2109     memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2110     lstrcpyW(buf+sizeof(wszFile2)/sizeof(WCHAR)-1, file_name_buf);
2111     file_name = file_name_buf;
2112     bindf = 0;
2113     test_file_protocol_url(buf);
2114     bindf = BINDF_FROMURLMON;
2115     test_file_protocol_url(buf);
2116
2117     buf[sizeof(wszFile2)/sizeof(WCHAR)] = '|';
2118     test_file_protocol_url(buf);
2119
2120     memcpy(buf, wszFile3, sizeof(wszFile3));
2121     len = sizeof(wszFile3)/sizeof(WCHAR)-1;
2122     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
2123     buf[len++] = '\\';
2124     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2125
2126     file_name = buf + sizeof(wszFile3)/sizeof(WCHAR)-1;
2127     bindf = 0;
2128     test_file_protocol_url(buf);
2129     bindf = BINDF_FROMURLMON;
2130     test_file_protocol_url(buf);
2131
2132     DeleteFileW(wszIndexHtml);
2133
2134     bindf = 0;
2135     test_file_protocol_fail();
2136     bindf = BINDF_FROMURLMON;
2137     test_file_protocol_fail();
2138 }
2139
2140 static BOOL http_protocol_start(LPCWSTR url, BOOL is_first)
2141 {
2142     static BOOL got_user_agent = FALSE;
2143     HRESULT hres;
2144
2145     first_data_notif = TRUE;
2146     state = 0;
2147
2148     SET_EXPECT(GetBindInfo);
2149     if (!(bindf & BINDF_FROMURLMON))
2150         SET_EXPECT(ReportProgress_DIRECTBIND);
2151     SET_EXPECT(GetBindString_USER_AGENT);
2152     SET_EXPECT(GetBindString_ACCEPT_MIMES);
2153     SET_EXPECT(QueryService_HttpNegotiate);
2154     SET_EXPECT(BeginningTransaction);
2155     SET_EXPECT(GetRootSecurityId);
2156     if (http_post_test)
2157         SET_EXPECT(GetBindString_POST_COOKIE);
2158
2159     hres = IInternetProtocol_Start(async_protocol, url, &protocol_sink, &bind_info, 0, 0);
2160     ok(hres == S_OK, "Start failed: %08x\n", hres);
2161     if(FAILED(hres))
2162         return FALSE;
2163
2164     CHECK_CALLED(GetBindInfo);
2165     if (!(bindf & BINDF_FROMURLMON))
2166         CHECK_CALLED(ReportProgress_DIRECTBIND);
2167     if (!got_user_agent)
2168     {
2169         CHECK_CALLED(GetBindString_USER_AGENT);
2170         got_user_agent = TRUE;
2171     }
2172     else todo_wine
2173     {
2174         /* user agent only retrieved once, even with different URLs */
2175         CHECK_NOT_CALLED(GetBindString_USER_AGENT);
2176     }
2177     CHECK_CALLED(GetBindString_ACCEPT_MIMES);
2178     CHECK_CALLED(QueryService_HttpNegotiate);
2179     CHECK_CALLED(BeginningTransaction);
2180     /* GetRootSecurityId called on WinXP but not on Win98 */
2181     CLEAR_CALLED(GetRootSecurityId);
2182     if (http_post_test)
2183         CHECK_CALLED(GetBindString_POST_COOKIE);
2184
2185     return TRUE;
2186 }
2187
2188 static void test_protocol_terminate(IInternetProtocol *protocol)
2189 {
2190     BYTE buf[3600];
2191     DWORD cb;
2192     HRESULT hres;
2193
2194     hres = IInternetProtocol_LockRequest(protocol, 0);
2195     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2196
2197     hres = IInternetProtocol_Read(protocol, buf, 1, &cb);
2198     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2199
2200     hres = IInternetProtocol_Terminate(protocol, 0);
2201     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2202
2203     /* This wait is to give the internet handles being freed in Terminate
2204      * enough time to actually terminate in all cases. Internet handles
2205      * terminate asynchronously and native reuses the main InternetOpen
2206      * handle. The only case in which this seems to be necessary is on
2207      * wine with native wininet and urlmon, resulting in the next time
2208      * test_http_protocol_url being called the first data notification actually
2209      * being an extra last data notification from the previous connection
2210      * about once out of every ten times. */
2211     Sleep(100);
2212
2213     hres = IInternetProtocol_UnlockRequest(protocol);
2214     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2215 }
2216
2217 static void test_http_info(IInternetProtocol *protocol)
2218 {
2219     IWinInetHttpInfo *info;
2220     HRESULT hres;
2221
2222     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&info);
2223     ok(hres == S_OK, "Could not get IWinInterHttpInfo iface: %08x\n", hres);
2224
2225     /* TODO */
2226
2227     IWinInetHttpInfo_Release(info);
2228 }
2229
2230 /* is_first refers to whether this is the first call to this function
2231  * _for this url_ */
2232 static void test_http_protocol_url(LPCWSTR url, BOOL is_https, BOOL is_first)
2233 {
2234     IInternetProtocolInfo *protocol_info;
2235     IClassFactory *factory;
2236     IUnknown *unk;
2237     HRESULT hres;
2238
2239     http_url = url;
2240     http_is_first = is_first;
2241
2242     hres = CoGetClassObject(is_https ? &CLSID_HttpSProtocol : &CLSID_HttpProtocol,
2243             CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2244     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2245     if(FAILED(hres))
2246         return;
2247
2248     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2249     ok(hres == E_NOINTERFACE,
2250         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
2251         hres);
2252
2253     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2254     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2255     IUnknown_Release(unk);
2256     if(FAILED(hres))
2257         return;
2258
2259     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2260                                         (void**)&async_protocol);
2261     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2262     if(SUCCEEDED(hres)) {
2263         BYTE buf[3600];
2264         DWORD cb;
2265         ULONG ref;
2266
2267         test_priority(async_protocol);
2268         test_http_info(async_protocol);
2269
2270         SET_EXPECT(ReportProgress_FINDINGRESOURCE);
2271         SET_EXPECT(ReportProgress_CONNECTING);
2272         SET_EXPECT(ReportProgress_SENDINGREQUEST);
2273         SET_EXPECT(ReportProgress_PROXYDETECTING);
2274         if(! is_https)
2275             SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2276         else
2277             SET_EXPECT(QueryService_HttpSecurity);
2278         if(!(bindf & BINDF_FROMURLMON)) {
2279             SET_EXPECT(OnResponse);
2280             SET_EXPECT(ReportProgress_RAWMIMETYPE);
2281             SET_EXPECT(ReportData);
2282         } else {
2283             SET_EXPECT(Switch);
2284         }
2285
2286         if(!http_protocol_start(url, is_first))
2287             return;
2288
2289         SET_EXPECT(ReportResult);
2290         expect_hrResult = S_OK;
2291
2292         hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2293         ok((hres == E_PENDING && cb==0) ||
2294            (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2295
2296         WaitForSingleObject(event_complete, INFINITE);
2297         if(bindf & BINDF_FROMURLMON)
2298             CHECK_CALLED(Switch);
2299         else
2300             CHECK_CALLED(ReportData);
2301         if (is_https)
2302             CLEAR_CALLED(QueryService_HttpSecurity);
2303
2304         while(1) {
2305             if(bindf & BINDF_FROMURLMON)
2306                 SET_EXPECT(Switch);
2307             else
2308                 SET_EXPECT(ReportData);
2309             hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
2310             if(hres == E_PENDING) {
2311                 hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2312                 ok((hres == E_PENDING && cb==0) ||
2313                    (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2314                 WaitForSingleObject(event_complete, INFINITE);
2315                 if(bindf & BINDF_FROMURLMON)
2316                     CHECK_CALLED(Switch);
2317                 else
2318                     CHECK_CALLED(ReportData);
2319             }else {
2320                 if(bindf & BINDF_FROMURLMON)
2321                     CHECK_NOT_CALLED(Switch);
2322                 else
2323                     CHECK_NOT_CALLED(ReportData);
2324                 if(cb == 0) break;
2325             }
2326         }
2327         ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2328         CHECK_CALLED(ReportResult);
2329         if (is_https)
2330             CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
2331
2332         test_protocol_terminate(async_protocol);
2333         ref = IInternetProtocol_Release(async_protocol);
2334         ok(!ref, "ref=%x\n", hres);
2335     }
2336
2337     IClassFactory_Release(factory);
2338 }
2339
2340 static void test_http_protocol(void)
2341 {
2342     static const WCHAR winehq_url[] =
2343         {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
2344             'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
2345     static const WCHAR posttest_url[] =
2346         {'h','t','t','p',':','/','/','c','r','o','s','s','o','v','e','r','.',
2347          'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m','/',
2348          'p','o','s','t','t','e','s','t','.','p','h','p',0};
2349
2350     trace("Testing http protocol (not from urlmon)...\n");
2351     tested_protocol = HTTP_TEST;
2352     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
2353     test_http_protocol_url(winehq_url, FALSE, TRUE);
2354
2355     trace("Testing http protocol (from urlmon)...\n");
2356     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
2357     test_http_protocol_url(winehq_url, FALSE, FALSE);
2358
2359     trace("Testing http protocol (to file)...\n");
2360     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
2361     test_http_protocol_url(winehq_url, FALSE, FALSE);
2362
2363     trace("Testing http protocol (post data)...\n");
2364     http_post_test = TRUE;
2365     /* Without this flag we get a ReportProgress_CACHEFILENAMEAVAILABLE
2366      * notification with BINDVERB_POST */
2367     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2368     test_http_protocol_url(posttest_url, FALSE, TRUE);
2369     http_post_test = FALSE;
2370 }
2371
2372 static void test_https_protocol(void)
2373 {
2374     static const WCHAR codeweavers_url[] =
2375         {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
2376          '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0};
2377
2378     trace("Testing https protocol (from urlmon)...\n");
2379     init_test(HTTPS_TEST, 0);
2380     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2381     test_http_protocol_url(codeweavers_url, TRUE, TRUE);
2382 }
2383
2384
2385 static void test_ftp_protocol(void)
2386 {
2387     IInternetProtocolInfo *protocol_info;
2388     IClassFactory *factory;
2389     IUnknown *unk;
2390     BYTE buf[4096];
2391     ULONG ref;
2392     DWORD cb;
2393     HRESULT hres;
2394
2395     static const WCHAR ftp_urlW[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
2396     '/','p','u','b','/','o','t','h','e','r','/',
2397     'w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0};
2398
2399     trace("Testing ftp protocol...\n");
2400
2401     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
2402     state = 0;
2403     tested_protocol = FTP_TEST;
2404     first_data_notif = TRUE;
2405     expect_hrResult = E_PENDING;
2406
2407     hres = CoGetClassObject(&CLSID_FtpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2408     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2409     if(FAILED(hres))
2410         return;
2411
2412     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2413     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2414
2415     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2416     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2417     IUnknown_Release(unk);
2418     if(FAILED(hres))
2419         return;
2420
2421     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2422                                         (void**)&async_protocol);
2423     IClassFactory_Release(factory);
2424     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2425
2426     test_priority(async_protocol);
2427     test_http_info(async_protocol);
2428
2429     SET_EXPECT(GetBindInfo);
2430     SET_EXPECT(GetBindString_USER_AGENT);
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     todo_wine CHECK_NOT_CALLED(GetBindString_USER_AGENT);
2440
2441     SET_EXPECT(ReportResult);
2442
2443     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
2444     ok((hres == E_PENDING && cb==0) ||
2445        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2446
2447     WaitForSingleObject(event_complete, INFINITE);
2448
2449     while(1) {
2450
2451         hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
2452         if(hres == E_PENDING)
2453             WaitForSingleObject(event_complete, INFINITE);
2454         else
2455             if(cb == 0) break;
2456     }
2457
2458     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2459     CHECK_CALLED(ReportResult);
2460     CHECK_CALLED(Switch);
2461
2462     test_protocol_terminate(async_protocol);
2463
2464     ref = IInternetProtocol_Release(async_protocol);
2465     ok(!ref, "ref=%d\n", ref);
2466 }
2467
2468 static void test_gopher_protocol(void)
2469 {
2470     IInternetProtocolInfo *protocol_info;
2471     IClassFactory *factory;
2472     IUnknown *unk;
2473     HRESULT hres;
2474
2475     trace("Testing gopher protocol...\n");
2476
2477     hres = CoGetClassObject(&CLSID_GopherProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2478     ok(hres == S_OK ||
2479        hres == REGDB_E_CLASSNOTREG, /* Gopher protocol has been removed as of Vista */
2480        "CoGetClassObject failed: %08x\n", hres);
2481     if(FAILED(hres))
2482         return;
2483
2484     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2485     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2486
2487     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2488     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2489     IUnknown_Release(unk);
2490     if(FAILED(hres))
2491         return;
2492
2493     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2494                                         (void**)&async_protocol);
2495     IClassFactory_Release(factory);
2496     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2497
2498     test_priority(async_protocol);
2499
2500     IInternetProtocol_Release(async_protocol);
2501 }
2502
2503 static void test_mk_protocol(void)
2504 {
2505     IInternetProtocolInfo *protocol_info;
2506     IInternetProtocol *protocol;
2507     IClassFactory *factory;
2508     IUnknown *unk;
2509     HRESULT hres;
2510
2511     static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
2512                                        ':',':','/','t','e','s','t','.','h','t','m','l',0};
2513     static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};
2514
2515     trace("Testing mk protocol...\n");
2516     init_test(MK_TEST, 0);
2517
2518     hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
2519             &IID_IUnknown, (void**)&unk);
2520     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2521
2522     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
2523     ok(hres == E_NOINTERFACE,
2524         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
2525         hres);
2526
2527     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
2528     ok(hres == S_OK, "Could not get IClassFactory interface\n");
2529     IUnknown_Release(unk);
2530     if(FAILED(hres))
2531         return;
2532
2533     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2534                                         (void**)&protocol);
2535     IClassFactory_Release(factory);
2536     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2537
2538     SET_EXPECT(GetBindInfo);
2539     hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
2540     ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
2541        "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
2542     CLEAR_CALLED(GetBindInfo);
2543
2544     SET_EXPECT(GetBindInfo);
2545     SET_EXPECT(ReportProgress_DIRECTBIND);
2546     SET_EXPECT(ReportProgress_SENDINGREQUEST);
2547     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
2548     SET_EXPECT(ReportResult);
2549     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2550
2551     hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
2552     ok(hres == INET_E_RESOURCE_NOT_FOUND ||
2553        hres == INET_E_INVALID_URL, /* win2k3 */
2554        "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
2555
2556     if (hres == INET_E_RESOURCE_NOT_FOUND) {
2557         CHECK_CALLED(GetBindInfo);
2558         CLEAR_CALLED(ReportProgress_DIRECTBIND);
2559         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2560         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
2561         CHECK_CALLED(ReportResult);
2562     }else {
2563         CLEAR_CALLED(GetBindInfo);
2564         CLEAR_CALLED(ReportProgress_DIRECTBIND);
2565         CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
2566         CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
2567         CLEAR_CALLED(ReportResult);
2568     }
2569
2570     IInternetProtocol_Release(protocol);
2571 }
2572
2573 static void test_CreateBinding(void)
2574 {
2575     IInternetProtocol *protocol;
2576     IInternetPriority *priority;
2577     IInternetSession *session;
2578     LONG p;
2579     BYTE buf[1000];
2580     DWORD read;
2581     HRESULT hres;
2582
2583     static const WCHAR test_url[] =
2584         {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
2585     static const WCHAR wsz_test[] = {'t','e','s','t',0};
2586
2587     trace("Testing CreateBinding...\n");
2588     init_test(BIND_TEST, TEST_BINDING);
2589
2590     hres = CoInternetGetSession(0, &session, 0);
2591     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2592
2593     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
2594     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
2595
2596     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
2597     binding_protocol = protocol;
2598     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
2599     ok(protocol != NULL, "protocol == NULL\n");
2600
2601     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
2602     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
2603
2604     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
2605     ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);
2606
2607     hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
2608     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2609     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
2610     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2611     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
2612     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2613
2614     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
2615     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
2616
2617     p = 0xdeadbeef;
2618     hres = IInternetPriority_GetPriority(priority, &p);
2619     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2620     ok(!p, "p=%d\n", p);
2621
2622     ex_priority = 100;
2623     hres = IInternetPriority_SetPriority(priority, 100);
2624     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2625
2626     p = 0xdeadbeef;
2627     hres = IInternetPriority_GetPriority(priority, &p);
2628     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2629     ok(p == 100, "p=%d\n", p);
2630
2631     SET_EXPECT(QueryService_InternetProtocol);
2632     SET_EXPECT(CreateInstance);
2633     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2634     SET_EXPECT(SetPriority);
2635     SET_EXPECT(Start);
2636
2637     expect_hrResult = S_OK;
2638     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
2639     ok(hres == S_OK, "Start failed: %08x\n", hres);
2640
2641     CHECK_CALLED(QueryService_InternetProtocol);
2642     CHECK_CALLED(CreateInstance);
2643     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2644     CHECK_CALLED(SetPriority);
2645     CHECK_CALLED(Start);
2646
2647     SET_EXPECT(Read);
2648     read = 0xdeadbeef;
2649     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2650     ok(hres == S_OK, "Read failed: %08x\n", hres);
2651     ok(read == 100, "read = %d\n", read);
2652     CHECK_CALLED(Read);
2653
2654     SET_EXPECT(Read);
2655     read = 0xdeadbeef;
2656     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2657     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2658     ok(!read, "read = %d\n", read);
2659     CHECK_CALLED(Read);
2660
2661     p = 0xdeadbeef;
2662     hres = IInternetPriority_GetPriority(priority, &p);
2663     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2664     ok(p == 100, "p=%d\n", p);
2665
2666     hres = IInternetPriority_SetPriority(priority, 101);
2667     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2668
2669     SET_EXPECT(Terminate);
2670     hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
2671     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2672     CHECK_CALLED(Terminate);
2673
2674     SET_EXPECT(Continue);
2675     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
2676     ok(hres == S_OK, "Switch failed: %08x\n", hres);
2677     CHECK_CALLED(Continue);
2678
2679     hres = IInternetProtocolSink_ReportProgress(binding_sink,
2680             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
2681     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
2682
2683     hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
2684     ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);
2685
2686     hres = IInternetProtocolSink_ReportData(binding_sink, 0, 0, 0);
2687     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
2688
2689     IInternetProtocolSink_Release(binding_sink);
2690     IInternetPriority_Release(priority);
2691     IInternetBindInfo_Release(prot_bind_info);
2692     IInternetProtocol_Release(protocol);
2693     IInternetSession_Release(session);
2694 }
2695
2696 static void test_binding(int prot, DWORD grf_pi, BOOL test_filter)
2697 {
2698     IInternetProtocol *protocol;
2699     IInternetSession *session;
2700     ULONG ref;
2701     HRESULT hres;
2702
2703     trace("Testing %s binding (grfPI %x%s)...\n", debugstr_w(protocol_names[prot]), grf_pi,
2704           test_filter ? " testing MIME filter" : "");
2705     init_test(prot, TEST_BINDING | (test_filter ? TEST_FILTER : 0));
2706     pi = grf_pi;
2707
2708     hres = CoInternetGetSession(0, &session, 0);
2709     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2710
2711     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
2712     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
2713
2714     hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
2715     binding_protocol = protocol;
2716     IInternetSession_Release(session);
2717     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
2718     ok(protocol != NULL, "protocol == NULL\n");
2719
2720     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
2721     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
2722
2723     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
2724     ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
2725
2726     ex_priority = 0;
2727     SET_EXPECT(QueryService_InternetProtocol);
2728     SET_EXPECT(CreateInstance);
2729     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2730     SET_EXPECT(SetPriority);
2731     SET_EXPECT(Start);
2732
2733     expect_hrResult = S_OK;
2734     hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, pi, 0);
2735     ok(hres == S_OK, "Start failed: %08x\n", hres);
2736
2737     CHECK_CALLED(QueryService_InternetProtocol);
2738     CHECK_CALLED(CreateInstance);
2739     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2740     CHECK_CALLED(SetPriority);
2741     CHECK_CALLED(Start);
2742
2743     if(prot == HTTP_TEST || prot == HTTPS_TEST) {
2744         while(prot_state < 4) {
2745             WaitForSingleObject(event_complete, INFINITE);
2746             if(mimefilter_test && filtered_protocol) {
2747                 SET_EXPECT(Continue);
2748                 IInternetProtocol_Continue(filtered_protocol, pdata);
2749                 CHECK_CALLED(Continue);
2750             }else {
2751                 SET_EXPECT(Continue);
2752                 IInternetProtocol_Continue(protocol, pdata);
2753                 CHECK_CALLED(Continue);
2754             }
2755             SetEvent(event_complete2);
2756         }
2757         WaitForSingleObject(event_complete, INFINITE);
2758     }else {
2759         if(mimefilter_test)
2760             SET_EXPECT(MimeFilter_LockRequest);
2761         else
2762             SET_EXPECT(LockRequest);
2763         hres = IInternetProtocol_LockRequest(protocol, 0);
2764         ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2765         if(mimefilter_test)
2766             CHECK_CALLED(MimeFilter_LockRequest);
2767         else
2768             CHECK_CALLED(LockRequest);
2769
2770         if(mimefilter_test)
2771             SET_EXPECT(MimeFilter_UnlockRequest);
2772         else
2773             SET_EXPECT(UnlockRequest);
2774         hres = IInternetProtocol_UnlockRequest(protocol);
2775         ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2776         if(mimefilter_test)
2777             CHECK_CALLED(MimeFilter_UnlockRequest);
2778         else
2779             CHECK_CALLED(UnlockRequest);
2780     }
2781
2782     if(mimefilter_test)
2783         SET_EXPECT(MimeFilter_Terminate);
2784     else
2785         SET_EXPECT(Terminate);
2786     hres = IInternetProtocol_Terminate(protocol, 0);
2787     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2788     if(mimefilter_test)
2789         CHECK_CALLED(MimeFilter_Terminate);
2790     else
2791         CHECK_CALLED(Terminate);
2792
2793     if(filtered_protocol)
2794         IInternetProtocol_Release(filtered_protocol);
2795     IInternetBindInfo_Release(prot_bind_info);
2796     IInternetProtocolSink_Release(binding_sink);
2797     ref = IInternetProtocol_Release(protocol);
2798     ok(!ref, "ref=%u, expected 0\n", ref);
2799 }
2800
2801 static void register_filter(void)
2802 {
2803     IInternetSession *session;
2804     HRESULT hres;
2805
2806     static const WCHAR gzipW[] = {'g','z','i','p',0};
2807
2808     CoInternetGetSession(0, &session, 0);
2809
2810     hres = IInternetSession_RegisterMimeFilter(session, &mimefilter_cf, &IID_IInternetProtocol, gzipW);
2811     ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
2812
2813     IInternetSession_Release(session);
2814 }
2815
2816 START_TEST(protocol)
2817 {
2818     OleInitialize(NULL);
2819
2820     event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
2821     event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
2822
2823     register_filter();
2824
2825     test_file_protocol();
2826     test_http_protocol();
2827     test_https_protocol();
2828     test_ftp_protocol();
2829     test_gopher_protocol();
2830     test_mk_protocol();
2831     test_CreateBinding();
2832     test_binding(FILE_TEST, 0, FALSE);
2833     test_binding(HTTP_TEST, 0, FALSE);
2834     test_binding(FILE_TEST, PI_MIMEVERIFICATION, FALSE);
2835     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, FALSE);
2836     test_binding(FILE_TEST, PI_MIMEVERIFICATION, TRUE);
2837     test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TRUE);
2838
2839     CloseHandle(event_complete);
2840     CloseHandle(event_complete2);
2841
2842     OleUninitialize();
2843 }