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