winhttp, wininet: Load i2d_X509 from libcrypto.so.
[wine] / dlls / urlmon / tests / protocol.c
1 /*
2  * Copyright 2005-2007 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(ReportData);
81 DEFINE_EXPECT(ReportResult);
82 DEFINE_EXPECT(GetBindString_ACCEPT_MIMES);
83 DEFINE_EXPECT(GetBindString_USER_AGENT);
84 DEFINE_EXPECT(GetBindString_POST_COOKIE);
85 DEFINE_EXPECT(QueryService_HttpNegotiate);
86 DEFINE_EXPECT(QueryService_InternetProtocol);
87 DEFINE_EXPECT(BeginningTransaction);
88 DEFINE_EXPECT(GetRootSecurityId);
89 DEFINE_EXPECT(OnResponse);
90 DEFINE_EXPECT(Switch);
91 DEFINE_EXPECT(Continue);
92 DEFINE_EXPECT(CreateInstance);
93 DEFINE_EXPECT(Start);
94 DEFINE_EXPECT(Terminate);
95 DEFINE_EXPECT(Read);
96 DEFINE_EXPECT(SetPriority);
97 DEFINE_EXPECT(LockRequest);
98 DEFINE_EXPECT(UnlockRequest);
99
100 static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
101 static const WCHAR index_url[] =
102     {'f','i','l','e',':','i','n','d','e','x','.','h','t','m','l',0};
103
104 static const WCHAR acc_mimeW[] = {'*','/','*',0};
105 static const WCHAR user_agentW[] = {'W','i','n','e',0};
106 static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
107 static const WCHAR hostW[] = {'w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
108 static const WCHAR winehq_ipW[] = {'2','0','9','.','4','6','.','2','5','.','1','3','4',0};
109 static const WCHAR emptyW[] = {0};
110
111 static HRESULT expect_hrResult;
112 static LPCWSTR file_name, http_url, expect_wsz;
113 static IInternetProtocol *async_protocol = NULL;
114 static BOOL first_data_notif = FALSE, http_is_first = FALSE,
115     http_post_test = FALSE;
116 static int state = 0, prot_state;
117 static DWORD bindf = 0, ex_priority = 0;
118 static IInternetProtocol *binding_protocol;
119 static IInternetBindInfo *prot_bind_info;
120 static IInternetProtocolSink *binding_sink;
121 static void *expect_pv;
122 static HANDLE event_complete, event_complete2;
123 static BOOL binding_test;
124 static PROTOCOLDATA protocoldata, *pdata;
125 static DWORD prot_read;
126
127 static enum {
128     FILE_TEST,
129     HTTP_TEST,
130     HTTPS_TEST,
131     FTP_TEST,
132     MK_TEST,
133     BIND_TEST
134 } tested_protocol;
135
136 static const WCHAR protocol_names[][10] = {
137     {'f','i','l','e',0},
138     {'h','t','t','p',0},
139     {'h','t','t','p','s',0},
140     {'f','t','p',0},
141     {'m','k',0},
142     {'t','e','s','t',0}
143 };
144
145 static const WCHAR binding_urls[][130] = {
146     {'f','i','l','e',':','t','e','s','t','.','h','t','m','l',0},
147     {'h','t','t','p',':','/','/','t','e','s','t','/','t','e','s','t','.','h','t','m','l',0},
148     {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
149      '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0},
150     {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
151      '/','p','u','b','/','o','t','h','e','r',
152      '/','w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0},
153     {'m','k',':','t','e','s','t',0},
154     {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0}
155 };
156
157 static const char *debugstr_w(LPCWSTR str)
158 {
159     static char buf[512];
160     if(!str)
161         return "(null)";
162     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
163     return buf;
164 }
165
166 static const char *debugstr_guid(REFIID riid)
167 {
168     static char buf[50];
169
170     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
171             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
172             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
173             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
174
175     return buf;
176 }
177
178 static int strcmp_wa(LPCWSTR strw, const char *stra)
179 {
180     WCHAR buf[512];
181     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
182     return lstrcmpW(strw, buf);
183 }
184
185 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface, REFIID riid, void **ppv)
186 {
187     if(IsEqualGUID(&IID_IUnknown, riid)
188             || IsEqualGUID(&IID_IHttpNegotiate, riid)
189             || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
190         *ppv = iface;
191         return S_OK;
192     }
193
194     ok(0, "unexpected call\n");
195     return E_NOINTERFACE;
196 }
197
198 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
199 {
200     return 2;
201 }
202
203 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
204 {
205     return 1;
206 }
207
208 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface, LPCWSTR szURL,
209         LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
210 {
211     LPWSTR addl_headers;
212
213     static const WCHAR wszHeaders[] =
214         {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ','a','p','p','l','i','c','a','t',
215          'i','o','n','/','x','-','w','w','w','-','f','o','r','m','-','u','r','l','e','n','c','o',
216          'd','e','d','\r','\n',0};
217
218     CHECK_EXPECT(BeginningTransaction);
219
220     if(binding_test)
221         ok(!lstrcmpW(szURL, binding_urls[tested_protocol]), "szURL != http_url\n");
222     else
223         ok(!lstrcmpW(szURL, http_url), "szURL != http_url\n");
224     ok(!dwReserved, "dwReserved=%d, expected 0\n", dwReserved);
225     ok(pszAdditionalHeaders != NULL, "pszAdditionalHeaders == NULL\n");
226     if(pszAdditionalHeaders)
227     {
228         ok(*pszAdditionalHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
229         if (http_post_test)
230         {
231             addl_headers = CoTaskMemAlloc(sizeof(wszHeaders));
232             if (!addl_headers)
233             {
234                 http_post_test = FALSE;
235                 skip("Out of memory\n");
236                 return E_OUTOFMEMORY;
237             }
238             lstrcpyW(addl_headers, wszHeaders);
239             *pszAdditionalHeaders = addl_headers;
240         }
241     }
242
243     return S_OK;
244 }
245
246 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
247         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
248 {
249     CHECK_EXPECT(OnResponse);
250
251     ok(dwResponseCode == 200, "dwResponseCode=%d, expected 200\n", dwResponseCode);
252     ok(szResponseHeaders != NULL, "szResponseHeaders == NULL\n");
253     ok(szRequestHeaders == NULL, "szRequestHeaders != NULL\n");
254     ok(pszAdditionalRequestHeaders == NULL, "pszAdditionalHeaders != NULL\n");
255
256     return S_OK;
257 }
258
259 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
260         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
261 {
262     static const BYTE sec_id[] = {'h','t','t','p',':','t','e','s','t',1,0,0,0};
263     
264     CHECK_EXPECT(GetRootSecurityId);
265
266     ok(!dwReserved, "dwReserved=%ld, expected 0\n", dwReserved);
267     ok(pbSecurityId != NULL, "pbSecurityId == NULL\n");
268     ok(pcbSecurityId != NULL, "pcbSecurityId == NULL\n");
269
270     if(pcbSecurityId) {
271         ok(*pcbSecurityId == 512, "*pcbSecurityId=%d, expected 512\n", *pcbSecurityId);
272         *pcbSecurityId = sizeof(sec_id);
273     }
274
275     if(pbSecurityId)
276         memcpy(pbSecurityId, sec_id, sizeof(sec_id));
277
278     return E_FAIL;
279 }
280
281 static IHttpNegotiate2Vtbl HttpNegotiateVtbl = {
282     HttpNegotiate_QueryInterface,
283     HttpNegotiate_AddRef,
284     HttpNegotiate_Release,
285     HttpNegotiate_BeginningTransaction,
286     HttpNegotiate_OnResponse,
287     HttpNegotiate_GetRootSecurityId
288 };
289
290 static IHttpNegotiate2 http_negotiate = { &HttpNegotiateVtbl };
291
292 static HRESULT QueryInterface(REFIID,void**);
293
294 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
295 {
296     return QueryInterface(riid, ppv);
297 }
298
299 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
300 {
301     return 2;
302 }
303
304 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
305 {
306     return 1;
307 }
308
309 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
310         REFIID riid, void **ppv)
311 {
312     if(IsEqualGUID(&IID_IHttpNegotiate, guidService) || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
313         CHECK_EXPECT2(QueryService_HttpNegotiate);
314         return IHttpNegotiate2_QueryInterface(&http_negotiate, riid, ppv);
315     }
316
317     if(IsEqualGUID(&IID_IInternetProtocol, guidService)) {
318         ok(IsEqualGUID(&IID_IInternetProtocol, riid), "unexpected riid\n");
319         CHECK_EXPECT(QueryService_InternetProtocol);
320         return E_NOINTERFACE;
321     }
322
323     ok(0, "unexpected service %s\n", debugstr_guid(guidService));
324     return E_FAIL;
325 }
326
327 static const IServiceProviderVtbl ServiceProviderVtbl = {
328     ServiceProvider_QueryInterface,
329     ServiceProvider_AddRef,
330     ServiceProvider_Release,
331     ServiceProvider_QueryService
332 };
333
334 static IServiceProvider service_provider = { &ServiceProviderVtbl };
335
336 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
337 {
338     return QueryInterface(riid, ppv);
339 }
340
341 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
342 {
343     return 2;
344 }
345
346 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
347 {
348     return 1;
349 }
350
351 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
352 {
353     HRESULT hres;
354
355     CHECK_EXPECT(Switch);
356     ok(pProtocolData != NULL, "pProtocolData == NULL\n");
357
358     pdata = pProtocolData;
359
360     if(binding_test) {
361         SetEvent(event_complete);
362         WaitForSingleObject(event_complete2, INFINITE);
363         return S_OK;
364     }
365
366     if (!state) {
367         if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST || tested_protocol == FTP_TEST) {
368             if (http_is_first) {
369                 CLEAR_CALLED(ReportProgress_FINDINGRESOURCE);
370                 CLEAR_CALLED(ReportProgress_CONNECTING);
371                 CLEAR_CALLED(ReportProgress_PROXYDETECTING);
372             } else todo_wine {
373                     CHECK_NOT_CALLED(ReportProgress_FINDINGRESOURCE);
374                     /* IE7 does call this */
375                     CLEAR_CALLED(ReportProgress_CONNECTING);
376                 }
377         }
378         if(tested_protocol == FTP_TEST)
379             todo_wine CHECK_CALLED(ReportProgress_SENDINGREQUEST);
380         else
381             CHECK_CALLED(ReportProgress_SENDINGREQUEST);
382         if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
383             SET_EXPECT(OnResponse);
384             if(tested_protocol == HTTPS_TEST)
385                 SET_EXPECT(ReportProgress_ACCEPTRANGES);
386             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
387             if(bindf & BINDF_NEEDFILE)
388                 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
389         }
390     }
391
392     SET_EXPECT(ReportData);
393     hres = IInternetProtocol_Continue(async_protocol, pProtocolData);
394     ok(hres == S_OK, "Continue failed: %08x\n", hres);
395     if(tested_protocol == FTP_TEST)
396         CLEAR_CALLED(ReportData);
397     else
398         CHECK_CALLED(ReportData);
399
400     if (!state) {
401         state = 1;
402         if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
403             CHECK_CALLED(OnResponse);
404             if(tested_protocol == HTTPS_TEST)
405                 CHECK_CALLED(ReportProgress_ACCEPTRANGES);
406             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
407             if(bindf & BINDF_NEEDFILE)
408                 CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
409         }
410     }
411
412     SetEvent(event_complete);
413
414     return S_OK;
415 }
416
417 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
418         LPCWSTR szStatusText)
419 {
420     static const WCHAR null_guid[] = {'{','0','0','0','0','0','0','0','0','-','0','0','0','0','-',
421         '0','0','0','0','-','0','0','0','0','-','0','0','0','0','0','0','0','0','0','0','0','0','}',0};
422     static const WCHAR text_plain[] = {'t','e','x','t','/','p','l','a','i','n',0};
423
424     switch(ulStatusCode) {
425     case BINDSTATUS_MIMETYPEAVAILABLE:
426         CHECK_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
427         ok(szStatusText != NULL, "szStatusText == NULL\n");
428         if(szStatusText) {
429             if(tested_protocol == BIND_TEST)
430                 ok(szStatusText == expect_wsz, "unexpected szStatusText\n");
431             else if (http_post_test)
432                 ok(lstrlenW(text_plain) <= lstrlenW(szStatusText) &&
433                    !memcmp(szStatusText, text_plain, lstrlenW(text_plain)*sizeof(WCHAR)),
434                    "szStatusText != text/plain\n");
435             else
436                 ok(lstrlenW(text_htmlW) <= lstrlenW(szStatusText) &&
437                    !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
438                    "szStatusText != text/html\n");
439         }
440         break;
441     case BINDSTATUS_DIRECTBIND:
442         CHECK_EXPECT2(ReportProgress_DIRECTBIND);
443         ok(szStatusText == NULL, "szStatusText != NULL\n");
444         break;
445     case BINDSTATUS_RAWMIMETYPE:
446         CHECK_EXPECT2(ReportProgress_RAWMIMETYPE);
447         ok(szStatusText != NULL, "szStatusText == NULL\n");
448         if(szStatusText)
449             ok(lstrlenW(szStatusText) < lstrlenW(text_htmlW) ||
450                !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
451                "szStatusText != text/html\n");
452         break;
453     case BINDSTATUS_CACHEFILENAMEAVAILABLE:
454         CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
455         ok(szStatusText != NULL, "szStatusText == NULL\n");
456         if(szStatusText) {
457             if(binding_test)
458                 ok(!lstrcmpW(szStatusText, expect_wsz), "unexpected szStatusText\n");
459             else if(tested_protocol == FILE_TEST)
460                 ok(!lstrcmpW(szStatusText, file_name), "szStatusText = \"%s\"\n", debugstr_w(szStatusText));
461             else
462                 ok(szStatusText != NULL, "szStatusText == NULL\n");
463         }
464         break;
465     case BINDSTATUS_FINDINGRESOURCE:
466         CHECK_EXPECT(ReportProgress_FINDINGRESOURCE);
467         ok(szStatusText != NULL, "szStatusText == NULL\n");
468         break;
469     case BINDSTATUS_CONNECTING:
470         CHECK_EXPECT(ReportProgress_CONNECTING);
471         ok(szStatusText != NULL, "szStatusText == NULL\n");
472         break;
473     case BINDSTATUS_SENDINGREQUEST:
474         CHECK_EXPECT2(ReportProgress_SENDINGREQUEST);
475         if(tested_protocol == FILE_TEST) {
476             ok(szStatusText != NULL, "szStatusText == NULL\n");
477             if(szStatusText)
478                 ok(!*szStatusText, "wrong szStatusText\n");
479         }
480         break;
481     case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
482         CHECK_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
483         ok(szStatusText != NULL, "szStatusText == NULL\n");
484         if(szStatusText)
485             ok(!lstrcmpW(szStatusText, text_htmlW), "szStatusText != text/html\n");
486         break;
487     case BINDSTATUS_PROTOCOLCLASSID:
488         CHECK_EXPECT(ReportProgress_PROTOCOLCLASSID);
489         ok(szStatusText != NULL, "szStatusText == NULL\n");
490         ok(!lstrcmpW(szStatusText, null_guid), "unexpected szStatusText\n");
491         break;
492     case BINDSTATUS_COOKIE_SENT:
493         CHECK_EXPECT(ReportProgress_COOKIE_SENT);
494         ok(szStatusText == NULL, "szStatusText != NULL\n");
495         break;
496     case BINDSTATUS_REDIRECTING:
497         CHECK_EXPECT(ReportProgress_REDIRECTING);
498         ok(szStatusText == NULL, "szStatusText = %s\n", debugstr_w(szStatusText));
499         break;
500     case BINDSTATUS_ENCODING:
501         CHECK_EXPECT(ReportProgress_ENCODING);
502         ok(!strcmp_wa(szStatusText, "gzip"), "szStatusText = %s\n", debugstr_w(szStatusText));
503         break;
504     case BINDSTATUS_ACCEPTRANGES:
505         CHECK_EXPECT(ReportProgress_ACCEPTRANGES);
506         ok(!szStatusText, "szStatusText = %s\n", debugstr_w(szStatusText));
507         break;
508     case BINDSTATUS_PROXYDETECTING:
509         CHECK_EXPECT(ReportProgress_PROXYDETECTING);
510         SET_EXPECT(ReportProgress_CONNECTING);
511         ok(!szStatusText, "szStatusText = %s\n", debugstr_w(szStatusText));
512         break;
513     default:
514         ok(0, "Unexpected status %d\n", ulStatusCode);
515     };
516
517     return S_OK;
518 }
519
520 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
521         ULONG ulProgress, ULONG ulProgressMax)
522 {
523     if(tested_protocol == FILE_TEST) {
524         CHECK_EXPECT2(ReportData);
525
526         ok(ulProgress == ulProgressMax, "ulProgress (%d) != ulProgressMax (%d)\n",
527            ulProgress, ulProgressMax);
528         ok(ulProgressMax == 13, "ulProgressMax=%d, expected 13\n", ulProgressMax);
529         ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION),
530                 "grcfBSCF = %08x\n", grfBSCF);
531     }else if(!binding_test && (tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST || tested_protocol == FTP_TEST)) {
532         if(!(grfBSCF & BSCF_LASTDATANOTIFICATION) || (grfBSCF & BSCF_DATAFULLYAVAILABLE))
533             CHECK_EXPECT(ReportData);
534         else if (http_post_test)
535             ok(ulProgress == 13, "Read %u bytes instead of 13\n", ulProgress);
536
537         ok(ulProgress, "ulProgress == 0\n");
538
539         if(first_data_notif) {
540             ok(grfBSCF == BSCF_FIRSTDATANOTIFICATION
541                || grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE),
542                "grcfBSCF = %08x\n", grfBSCF);
543             first_data_notif = FALSE;
544         } else {
545             ok(grfBSCF == BSCF_INTERMEDIATEDATANOTIFICATION
546                || grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION)
547                || broken(grfBSCF == (BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION)),
548                "grcfBSCF = %08x\n", grfBSCF);
549         }
550
551         if(!(bindf & BINDF_FROMURLMON) &&
552            !(grfBSCF & BSCF_LASTDATANOTIFICATION)) {
553             if(!state) {
554                 state = 1;
555                 if(http_is_first) {
556                     CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
557                     CHECK_CALLED(ReportProgress_CONNECTING);
558                 } else todo_wine {
559                     CHECK_NOT_CALLED(ReportProgress_FINDINGRESOURCE);
560                     CHECK_NOT_CALLED(ReportProgress_CONNECTING);
561                 }
562                 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
563                 CHECK_CALLED(OnResponse);
564                 CHECK_CALLED(ReportProgress_RAWMIMETYPE);
565             }
566             SetEvent(event_complete);
567         }
568     }else {
569         BYTE buf[1000];
570         ULONG read;
571         HRESULT hres;
572
573         CHECK_EXPECT(ReportData);
574
575         if(tested_protocol == BIND_TEST)
576             return S_OK;
577
578         do {
579             SET_EXPECT(Read);
580             hres = IInternetProtocol_Read(binding_protocol, expect_pv=buf, sizeof(buf), &read);
581             CHECK_CALLED(Read);
582         }while(hres == S_OK);
583     }
584
585     return S_OK;
586 }
587
588 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
589         DWORD dwError, LPCWSTR szResult)
590 {
591     CHECK_EXPECT(ReportResult);
592
593     if(tested_protocol == FTP_TEST)
594         ok(hrResult == E_PENDING || hrResult == S_OK, "hrResult = %08x, expected E_PENDING or S_OK\n", hrResult);
595     else
596         ok(hrResult == expect_hrResult, "hrResult = %08x, expected: %08x\n",
597            hrResult, expect_hrResult);
598     if(SUCCEEDED(hrResult) || tested_protocol == FTP_TEST)
599         ok(dwError == ERROR_SUCCESS, "dwError = %d, expected ERROR_SUCCESS\n", dwError);
600     else
601         ok(dwError != ERROR_SUCCESS, "dwError == ERROR_SUCCESS\n");
602     ok(!szResult, "szResult != NULL\n");
603
604     return S_OK;
605 }
606
607 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
608     ProtocolSink_QueryInterface,
609     ProtocolSink_AddRef,
610     ProtocolSink_Release,
611     ProtocolSink_Switch,
612     ProtocolSink_ReportProgress,
613     ProtocolSink_ReportData,
614     ProtocolSink_ReportResult
615 };
616
617 static IInternetProtocolSink protocol_sink = { &protocol_sink_vtbl };
618
619 static HRESULT QueryInterface(REFIID riid, void **ppv)
620 {
621     *ppv = NULL;
622
623     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid))
624         *ppv = &protocol_sink;
625     if(IsEqualGUID(&IID_IServiceProvider, riid))
626         *ppv = &service_provider;
627
628     if(*ppv)
629         return S_OK;
630
631     return E_NOINTERFACE;
632 }
633
634 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
635 {
636     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
637         *ppv = iface;
638         return S_OK;
639     }
640     return E_NOINTERFACE;
641 }
642
643 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
644 {
645     return 2;
646 }
647
648 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
649 {
650     return 1;
651 }
652
653 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
654 {
655     DWORD cbSize;
656
657     static const CHAR szPostData[] = "mode=Test";
658
659     CHECK_EXPECT(GetBindInfo);
660
661     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
662     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
663     ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
664
665     *grfBINDF = bindf;
666     if(binding_test)
667         *grfBINDF |= BINDF_FROMURLMON;
668     cbSize = pbindinfo->cbSize;
669     memset(pbindinfo, 0, cbSize);
670     pbindinfo->cbSize = cbSize;
671
672     if (http_post_test)
673     {
674         /* Must be GMEM_FIXED, GMEM_MOVABLE does not work properly
675          * with urlmon on native (Win98 and WinXP) */
676         U(pbindinfo->stgmedData).hGlobal = GlobalAlloc(GPTR, sizeof(szPostData));
677         if (!U(pbindinfo->stgmedData).hGlobal)
678         {
679             http_post_test = FALSE;
680             skip("Out of memory\n");
681             return E_OUTOFMEMORY;
682         }
683         lstrcpy((LPSTR)U(pbindinfo->stgmedData).hGlobal, szPostData);
684         pbindinfo->cbstgmedData = sizeof(szPostData)-1;
685         pbindinfo->dwBindVerb = BINDVERB_POST;
686         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
687     }
688
689     return S_OK;
690 }
691
692 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType,
693         LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
694 {
695     ok(ppwzStr != NULL, "ppwzStr == NULL\n");
696     ok(pcElFetched != NULL, "pcElFetched == NULL\n");
697
698     switch(ulStringType) {
699     case BINDSTRING_ACCEPT_MIMES:
700         CHECK_EXPECT(GetBindString_ACCEPT_MIMES);
701         ok(cEl == 256, "cEl=%d, expected 256\n", cEl);
702         if(pcElFetched) {
703             ok(*pcElFetched == 256, "*pcElFetched=%d, expected 256\n", *pcElFetched);
704             *pcElFetched = 1;
705         }
706         if(ppwzStr) {
707             *ppwzStr = CoTaskMemAlloc(sizeof(acc_mimeW));
708             memcpy(*ppwzStr, acc_mimeW, sizeof(acc_mimeW));
709         }
710         return S_OK;
711     case BINDSTRING_USER_AGENT:
712         CHECK_EXPECT(GetBindString_USER_AGENT);
713         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
714         if(pcElFetched) {
715             ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
716             *pcElFetched = 1;
717         }
718         if(ppwzStr) {
719             *ppwzStr = CoTaskMemAlloc(sizeof(user_agentW));
720             memcpy(*ppwzStr, user_agentW, sizeof(user_agentW));
721         }
722         return S_OK;
723     case BINDSTRING_POST_COOKIE:
724         CHECK_EXPECT(GetBindString_POST_COOKIE);
725         ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
726         if(pcElFetched)
727             ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
728         return S_OK;
729     default:
730         ok(0, "unexpected call\n");
731     }
732
733     return E_NOTIMPL;
734 }
735
736 static IInternetBindInfoVtbl bind_info_vtbl = {
737     BindInfo_QueryInterface,
738     BindInfo_AddRef,
739     BindInfo_Release,
740     BindInfo_GetBindInfo,
741     BindInfo_GetBindString
742 };
743
744 static IInternetBindInfo bind_info = { &bind_info_vtbl };
745
746 static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
747                                                   REFIID riid, void **ppv)
748 {
749     ok(0, "unexpected call\n");
750     return E_NOINTERFACE;
751 }
752
753 static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
754 {
755     return 2;
756 }
757
758 static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
759 {
760     return 1;
761 }
762
763 static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
764 {
765     CHECK_EXPECT(SetPriority);
766     ok(nPriority == ex_priority, "nPriority=%d\n", nPriority);
767     return S_OK;
768 }
769
770 static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
771 {
772     ok(0, "unexpected call\n");
773     return E_NOTIMPL;
774 }
775
776
777 static const IInternetPriorityVtbl InternetPriorityVtbl = {
778     InternetPriority_QueryInterface,
779     InternetPriority_AddRef,
780     InternetPriority_Release,
781     InternetPriority_SetPriority,
782     InternetPriority_GetPriority
783 };
784
785 static IInternetPriority InternetPriority = { &InternetPriorityVtbl };
786
787 static HRESULT WINAPI Protocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
788 {
789     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
790         *ppv = iface;
791         return S_OK;
792     }
793
794     if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
795         trace("IID_IInternetProtocolEx not supported\n");
796         *ppv = NULL;
797         return E_NOINTERFACE;
798     }
799
800     if(IsEqualGUID(&IID_IInternetPriority, riid)) {
801         *ppv = &InternetPriority;
802         return S_OK;
803     }
804
805     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
806     *ppv = NULL;
807     return E_NOINTERFACE;
808 }
809
810 static ULONG WINAPI Protocol_AddRef(IInternetProtocol *iface)
811 {
812     return 2;
813 }
814
815 static ULONG WINAPI Protocol_Release(IInternetProtocol *iface)
816 {
817     return 1;
818 }
819
820 static DWORD WINAPI thread_proc(PVOID arg)
821 {
822     HRESULT hres;
823
824     memset(&protocoldata, -1, sizeof(protocoldata));
825
826     prot_state = 0;
827
828     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
829     hres = IInternetProtocolSink_ReportProgress(binding_sink,
830             BINDSTATUS_FINDINGRESOURCE, hostW);
831     CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
832     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
833
834     SET_EXPECT(ReportProgress_CONNECTING);
835     hres = IInternetProtocolSink_ReportProgress(binding_sink,
836             BINDSTATUS_CONNECTING, winehq_ipW);
837     CHECK_CALLED(ReportProgress_CONNECTING);
838     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
839
840     SET_EXPECT(ReportProgress_SENDINGREQUEST);
841     hres = IInternetProtocolSink_ReportProgress(binding_sink,
842             BINDSTATUS_SENDINGREQUEST, NULL);
843     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
844     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
845
846     prot_state = 1;
847     SET_EXPECT(Switch);
848     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
849     CHECK_CALLED(Switch);
850     ok(hres == S_OK, "Switch failed: %08x\n", hres);
851
852     prot_state = 2;
853     SET_EXPECT(Switch);
854     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
855     CHECK_CALLED(Switch);
856     ok(hres == S_OK, "Switch failed: %08x\n", hres);
857
858     prot_state = 2;
859     SET_EXPECT(Switch);
860     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
861     CHECK_CALLED(Switch);
862     ok(hres == S_OK, "Switch failed: %08x\n", hres);
863
864     prot_state = 3;
865     SET_EXPECT(Switch);
866     hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
867     CHECK_CALLED(Switch);
868     ok(hres == S_OK, "Switch failed: %08x\n", hres);
869
870     SetEvent(event_complete);
871
872     return 0;
873 }
874
875 static HRESULT WINAPI Protocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
876         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
877         DWORD grfPI, DWORD dwReserved)
878 {
879     BINDINFO bindinfo, exp_bindinfo;
880     DWORD cbindf = 0;
881     HRESULT hres;
882
883     CHECK_EXPECT(Start);
884
885     ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
886     ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
887     ok(pOIProtSink != &protocol_sink, "unexpected pOIProtSink\n");
888     ok(pOIBindInfo != &bind_info, "unexpected pOIBindInfo\n");
889     ok(!grfPI, "grfPI = %x\n", grfPI);
890     ok(!dwReserved, "dwReserved = %d\n", dwReserved);
891
892     memset(&bindinfo, 0, sizeof(bindinfo));
893     bindinfo.cbSize = sizeof(bindinfo);
894     memcpy(&exp_bindinfo, &bindinfo, sizeof(bindinfo));
895     SET_EXPECT(GetBindInfo);
896     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
897     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
898     CHECK_CALLED(GetBindInfo);
899     ok(cbindf == (bindf|BINDF_FROMURLMON), "bindf = %x, expected %x\n",
900        cbindf, (bindf|BINDF_FROMURLMON));
901     ok(!memcmp(&exp_bindinfo, &bindinfo, sizeof(bindinfo)), "unexpected bindinfo\n");
902     ReleaseBindInfo(&bindinfo);
903
904     SET_EXPECT(ReportProgress_SENDINGREQUEST);
905     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, emptyW);
906     ok(hres == S_OK, "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
907     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
908
909     if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
910         IServiceProvider *service_provider;
911         IHttpNegotiate *http_negotiate;
912         IHttpNegotiate2 *http_negotiate2;
913         LPWSTR ua = (LPWSTR)0xdeadbeef, accept_mimes[256];
914         LPWSTR additional_headers = NULL;
915         BYTE sec_id[100];
916         DWORD fetched = 0, size = 100;
917         DWORD tid;
918
919         SET_EXPECT(GetBindString_USER_AGENT);
920         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT,
921                                                &ua, 1, &fetched);
922         CHECK_CALLED(GetBindString_USER_AGENT);
923         ok(hres == S_OK, "GetBindString(BINDSTRING_USER_AGETNT) failed: %08x\n", hres);
924         ok(fetched == 1, "fetched = %d, expected 254\n", fetched);
925         ok(ua != NULL, "ua =  %p\n", ua);
926         ok(!lstrcmpW(ua, user_agentW), "unexpected user agent %s\n", debugstr_w(ua));
927         CoTaskMemFree(ua);
928
929         fetched = 256;
930         SET_EXPECT(GetBindString_ACCEPT_MIMES);
931         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
932                                                accept_mimes, 256, &fetched);
933         CHECK_CALLED(GetBindString_ACCEPT_MIMES);
934
935         ok(hres == S_OK,
936            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
937         ok(fetched == 1, "fetched = %d, expected 1\n", fetched);
938         ok(!lstrcmpW(acc_mimeW, accept_mimes[0]), "unexpected mimes %s\n", debugstr_w(accept_mimes[0]));
939
940         hres = IInternetBindInfo_QueryInterface(pOIBindInfo, &IID_IServiceProvider,
941                                                 (void**)&service_provider);
942         ok(hres == S_OK, "QueryInterface failed: %08x\n", hres);
943
944         SET_EXPECT(QueryService_HttpNegotiate);
945         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
946                 &IID_IHttpNegotiate, (void**)&http_negotiate);
947         CHECK_CALLED(QueryService_HttpNegotiate);
948         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
949
950         SET_EXPECT(BeginningTransaction);
951         hres = IHttpNegotiate_BeginningTransaction(http_negotiate, binding_urls[tested_protocol],
952                                                    NULL, 0, &additional_headers);
953         CHECK_CALLED(BeginningTransaction);
954         IHttpNegotiate_Release(http_negotiate);
955         ok(hres == S_OK, "BeginningTransction failed: %08x\n", hres);
956         ok(additional_headers == NULL, "additional_headers=%p\n", additional_headers);
957
958         SET_EXPECT(QueryService_HttpNegotiate);
959         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
960                 &IID_IHttpNegotiate2, (void**)&http_negotiate2);
961         CHECK_CALLED(QueryService_HttpNegotiate);
962         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
963
964         size = 512;
965         SET_EXPECT(GetRootSecurityId);
966         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, sec_id, &size, 0);
967         CHECK_CALLED(GetRootSecurityId);
968         IHttpNegotiate2_Release(http_negotiate2);
969         ok(hres == E_FAIL, "GetRootSecurityId failed: %08x, expected E_FAIL\n", hres);
970         ok(size == 13, "size=%d\n", size);
971
972         IServiceProvider_Release(service_provider);
973
974         CreateThread(NULL, 0, thread_proc, NULL, 0, &tid);
975
976         return S_OK;
977     }
978
979     SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
980     hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
981             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
982     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
983     CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
984
985     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
986     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
987                                                 expect_wsz = text_htmlW);
988     ok(hres == S_OK,
989        "ReportProgress(BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE) failed: %08x\n", hres);
990     CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
991
992     SET_EXPECT(ReportData);
993     hres = IInternetProtocolSink_ReportData(pOIProtSink,
994             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION, 13, 13);
995     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
996     CHECK_CALLED(ReportData);
997
998     if(tested_protocol == BIND_TEST) {
999         hres = IInternetProtocol_Terminate(binding_protocol, 0);
1000         ok(hres == E_FAIL, "Termiante failed: %08x\n", hres);
1001     }
1002
1003     SET_EXPECT(ReportResult);
1004     hres = IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
1005     ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1006     CHECK_CALLED(ReportResult);
1007
1008     return S_OK;
1009 }
1010
1011 static HRESULT WINAPI Protocol_Continue(IInternetProtocol *iface,
1012         PROTOCOLDATA *pProtocolData)
1013 {
1014     DWORD bscf = 0;
1015     HRESULT hres;
1016
1017     CHECK_EXPECT(Continue);
1018
1019     ok(pProtocolData != NULL, "pProtocolData == NULL\n");
1020     if(!pProtocolData || tested_protocol == BIND_TEST)
1021         return S_OK;
1022
1023     switch(prot_state) {
1024     case 1: {
1025         IServiceProvider *service_provider;
1026         IHttpNegotiate *http_negotiate;
1027         static WCHAR header[] = {'?',0};
1028
1029         hres = IInternetProtocolSink_QueryInterface(binding_sink, &IID_IServiceProvider,
1030                                                     (void**)&service_provider);
1031         ok(hres == S_OK, "Could not get IServiceProvicder\n");
1032
1033         SET_EXPECT(QueryService_HttpNegotiate);
1034         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
1035                                              &IID_IHttpNegotiate, (void**)&http_negotiate);
1036         IServiceProvider_Release(service_provider);
1037         CHECK_CALLED(QueryService_HttpNegotiate);
1038         ok(hres == S_OK, "Could not get IHttpNegotiate\n");
1039
1040         SET_EXPECT(OnResponse);
1041         hres = IHttpNegotiate_OnResponse(http_negotiate, 200, header, NULL, NULL);
1042         IHttpNegotiate_Release(http_negotiate);
1043         CHECK_CALLED(OnResponse);
1044         IHttpNegotiate_Release(http_negotiate);
1045         ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
1046
1047         SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1048         hres = IInternetProtocolSink_ReportProgress(binding_sink,
1049                 BINDSTATUS_MIMETYPEAVAILABLE, text_htmlW);
1050         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1051         ok(hres == S_OK,
1052            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
1053
1054         bscf |= BSCF_FIRSTDATANOTIFICATION;
1055         break;
1056     }
1057     case 2:
1058     case 3:
1059         bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
1060         break;
1061     }
1062
1063     SET_EXPECT(ReportData);
1064     hres = IInternetProtocolSink_ReportData(binding_sink, bscf, 100, 400);
1065     CHECK_CALLED(ReportData);
1066     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1067
1068     if(prot_state == 3)
1069         prot_state = 4;
1070
1071     return S_OK;
1072 }
1073
1074 static HRESULT WINAPI Protocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
1075         DWORD dwOptions)
1076 {
1077     ok(0, "unexpected call\n");
1078     return E_NOTIMPL;
1079 }
1080
1081 static HRESULT WINAPI Protocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
1082 {
1083     CHECK_EXPECT(Terminate);
1084     ok(!dwOptions, "dwOptions=%d\n", dwOptions);
1085     return S_OK;
1086 }
1087
1088 static HRESULT WINAPI Protocol_Suspend(IInternetProtocol *iface)
1089 {
1090     ok(0, "unexpected call\n");
1091     return E_NOTIMPL;
1092 }
1093
1094 static HRESULT WINAPI Protocol_Resume(IInternetProtocol *iface)
1095 {
1096     ok(0, "unexpected call\n");
1097     return E_NOTIMPL;
1098 }
1099
1100 static HRESULT WINAPI Protocol_Read(IInternetProtocol *iface, void *pv,
1101         ULONG cb, ULONG *pcbRead)
1102 {
1103     static BOOL b = TRUE;
1104
1105     CHECK_EXPECT(Read);
1106
1107     ok(pv == expect_pv, "pv != expect_pv\n");
1108     ok(cb == 1000, "cb=%d\n", cb);
1109     ok(pcbRead != NULL, "pcbRead == NULL\n");
1110     ok(!*pcbRead, "*pcbRead = %d\n", *pcbRead);
1111
1112     if(prot_state == 3) {
1113         HRESULT hres;
1114
1115         SET_EXPECT(ReportResult);
1116         hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
1117         CHECK_CALLED(ReportResult);
1118
1119         return S_FALSE;
1120     }
1121
1122     if((b = !b))
1123         return tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST ? E_PENDING : S_FALSE;
1124
1125     memset(pv, 'x', 100);
1126     prot_read += *pcbRead = 100;
1127     return S_OK;
1128 }
1129
1130 static HRESULT WINAPI Protocol_Seek(IInternetProtocol *iface,
1131         LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
1132 {
1133     ok(0, "unexpected call\n");
1134     return E_NOTIMPL;
1135 }
1136
1137 static HRESULT WINAPI Protocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
1138 {
1139     CHECK_EXPECT(LockRequest);
1140     ok(dwOptions == 0, "dwOptions=%x\n", dwOptions);
1141     return S_OK;
1142 }
1143
1144 static HRESULT WINAPI Protocol_UnlockRequest(IInternetProtocol *iface)
1145 {
1146     CHECK_EXPECT(UnlockRequest);
1147     return S_OK;
1148 }
1149
1150 static const IInternetProtocolVtbl ProtocolVtbl = {
1151     Protocol_QueryInterface,
1152     Protocol_AddRef,
1153     Protocol_Release,
1154     Protocol_Start,
1155     Protocol_Continue,
1156     Protocol_Abort,
1157     Protocol_Terminate,
1158     Protocol_Suspend,
1159     Protocol_Resume,
1160     Protocol_Read,
1161     Protocol_Seek,
1162     Protocol_LockRequest,
1163     Protocol_UnlockRequest
1164 };
1165
1166 static IInternetProtocol Protocol = { &ProtocolVtbl };
1167
1168 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
1169 {
1170     ok(0, "unexpected call\n");
1171     return E_NOINTERFACE;
1172 }
1173
1174 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
1175 {
1176     return 2;
1177 }
1178
1179 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
1180 {
1181     return 1;
1182 }
1183
1184 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
1185                                         REFIID riid, void **ppv)
1186 {
1187     CHECK_EXPECT(CreateInstance);
1188
1189     ok(pOuter == (IUnknown*)prot_bind_info, "pOuter != protocol_unk\n");
1190     ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", debugstr_guid(riid));
1191     ok(ppv != NULL, "ppv == NULL\n");
1192
1193     *ppv = &Protocol;
1194     return S_OK;
1195 }
1196
1197 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
1198 {
1199     ok(0, "unexpected call\n");
1200     return S_OK;
1201 }
1202
1203 static const IClassFactoryVtbl ClassFactoryVtbl = {
1204     ClassFactory_QueryInterface,
1205     ClassFactory_AddRef,
1206     ClassFactory_Release,
1207     ClassFactory_CreateInstance,
1208     ClassFactory_LockServer
1209 };
1210
1211 static IClassFactory ClassFactory = { &ClassFactoryVtbl };
1212
1213 static void test_priority(IInternetProtocol *protocol)
1214 {
1215     IInternetPriority *priority;
1216     LONG pr;
1217     HRESULT hres;
1218
1219     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority,
1220                                             (void**)&priority);
1221     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
1222     if(FAILED(hres))
1223         return;
1224
1225     hres = IInternetPriority_GetPriority(priority, &pr);
1226     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1227     ok(pr == 0, "pr=%d, expected 0\n", pr);
1228
1229     hres = IInternetPriority_SetPriority(priority, 1);
1230     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
1231
1232     hres = IInternetPriority_GetPriority(priority, &pr);
1233     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1234     ok(pr == 1, "pr=%d, expected 1\n", pr);
1235
1236     IInternetPriority_Release(priority);
1237 }
1238
1239 static BOOL file_protocol_start(IInternetProtocol *protocol, LPCWSTR url, BOOL is_first)
1240 {
1241     HRESULT hres;
1242
1243     SET_EXPECT(GetBindInfo);
1244     if(!(bindf & BINDF_FROMURLMON))
1245        SET_EXPECT(ReportProgress_DIRECTBIND);
1246     if(is_first) {
1247         SET_EXPECT(ReportProgress_SENDINGREQUEST);
1248         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1249         if(bindf & BINDF_FROMURLMON)
1250             SET_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1251         else
1252             SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1253     }
1254     SET_EXPECT(ReportData);
1255     if(is_first)
1256         SET_EXPECT(ReportResult);
1257
1258     expect_hrResult = S_OK;
1259
1260     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
1261     if(hres == INET_E_RESOURCE_NOT_FOUND) {
1262         win_skip("Start failed\n");
1263         return FALSE;
1264     }
1265     ok(hres == S_OK, "Start failed: %08x\n", hres);
1266
1267     CHECK_CALLED(GetBindInfo);
1268     if(!(bindf & BINDF_FROMURLMON))
1269        CHECK_CALLED(ReportProgress_DIRECTBIND);
1270     if(is_first) {
1271         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1272         CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
1273         if(bindf & BINDF_FROMURLMON)
1274             CHECK_CALLED(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1275         else
1276             CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1277     }
1278     CHECK_CALLED(ReportData);
1279     if(is_first)
1280         CHECK_CALLED(ReportResult);
1281
1282     return TRUE;
1283 }
1284
1285 static void test_file_protocol_url(LPCWSTR url)
1286 {
1287     IInternetProtocolInfo *protocol_info;
1288     IUnknown *unk;
1289     IClassFactory *factory;
1290     HRESULT hres;
1291
1292     hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
1293             &IID_IUnknown, (void**)&unk);
1294     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1295     if(FAILED(hres))
1296         return;
1297
1298     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1299     ok(hres == E_NOINTERFACE,
1300             "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1301
1302     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1303     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1304     if(SUCCEEDED(hres)) {
1305         IInternetProtocol *protocol;
1306         BYTE buf[512];
1307         ULONG cb;
1308         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1309         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1310
1311         if(SUCCEEDED(hres)) {
1312             if(file_protocol_start(protocol, url, TRUE)) {
1313                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1314                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1315                 ok(cb == 2, "cb=%u expected 2\n", cb);
1316                 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1317                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1318                 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1319                 ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
1320                 ok(cb == 0, "cb=%u expected 0\n", cb);
1321                 hres = IInternetProtocol_UnlockRequest(protocol);
1322                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1323             }
1324
1325             if(file_protocol_start(protocol, url, FALSE)) {
1326                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1327                 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1328                 hres = IInternetProtocol_LockRequest(protocol, 0);
1329                 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1330                 hres = IInternetProtocol_UnlockRequest(protocol);
1331                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1332             }
1333
1334             IInternetProtocol_Release(protocol);
1335         }
1336
1337         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1338         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1339
1340         if(SUCCEEDED(hres)) {
1341             if(file_protocol_start(protocol, url, TRUE)) {
1342                 hres = IInternetProtocol_LockRequest(protocol, 0);
1343                 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1344                 hres = IInternetProtocol_Terminate(protocol, 0);
1345                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1346                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1347                 ok(hres == S_OK, "Read failed: %08x\n\n", hres);
1348                 hres = IInternetProtocol_UnlockRequest(protocol);
1349                 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1350                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1351                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1352                 hres = IInternetProtocol_Terminate(protocol, 0);
1353                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1354             }
1355
1356             IInternetProtocol_Release(protocol);
1357         }
1358
1359         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1360         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1361
1362         if(SUCCEEDED(hres)) {
1363             if(file_protocol_start(protocol, url, TRUE)) {
1364                 hres = IInternetProtocol_Terminate(protocol, 0);
1365                 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1366                 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1367                 ok(hres == S_OK, "Read failed: %08x\n", hres);
1368                 ok(cb == 2, "cb=%u expected 2\n", cb);
1369             }
1370
1371             IInternetProtocol_Release(protocol);
1372         }
1373
1374         IClassFactory_Release(factory);
1375     }
1376
1377     IUnknown_Release(unk);
1378 }
1379
1380 static void test_file_protocol_fail(void)
1381 {
1382     IInternetProtocol *protocol;
1383     HRESULT hres;
1384
1385     static const WCHAR index_url2[] =
1386         {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
1387
1388     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1389             &IID_IInternetProtocol, (void**)&protocol);
1390     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
1391     if(FAILED(hres))
1392         return;
1393
1394     SET_EXPECT(GetBindInfo);
1395     expect_hrResult = MK_E_SYNTAX;
1396     hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
1397     ok(hres == MK_E_SYNTAX ||
1398        hres == E_INVALIDARG,
1399        "Start failed: %08x, expected MK_E_SYNTAX or E_INVALIDARG\n", hres);
1400     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
1401
1402     SET_EXPECT(GetBindInfo);
1403     if(!(bindf & BINDF_FROMURLMON))
1404         SET_EXPECT(ReportProgress_DIRECTBIND);
1405     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1406     SET_EXPECT(ReportResult);
1407     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
1408     hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
1409     ok(hres == INET_E_RESOURCE_NOT_FOUND,
1410             "Start failed: %08x expected INET_E_RESOURCE_NOT_FOUND\n", hres);
1411     CHECK_CALLED(GetBindInfo);
1412     if(!(bindf & BINDF_FROMURLMON))
1413         CHECK_CALLED(ReportProgress_DIRECTBIND);
1414     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1415     CHECK_CALLED(ReportResult);
1416
1417     IInternetProtocol_Release(protocol);
1418
1419     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1420             &IID_IInternetProtocol, (void**)&protocol);
1421     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
1422     if(FAILED(hres))
1423         return;
1424
1425     SET_EXPECT(GetBindInfo);
1426     if(!(bindf & BINDF_FROMURLMON))
1427         SET_EXPECT(ReportProgress_DIRECTBIND);
1428     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1429     SET_EXPECT(ReportResult);
1430     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
1431
1432     hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
1433     ok(hres == INET_E_RESOURCE_NOT_FOUND,
1434             "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
1435     CHECK_CALLED(GetBindInfo);
1436     if(!(bindf & BINDF_FROMURLMON))
1437         CHECK_CALLED(ReportProgress_DIRECTBIND);
1438     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1439     CHECK_CALLED(ReportResult);
1440
1441     SET_EXPECT(GetBindInfo);
1442     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
1443     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1444     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
1445
1446     SET_EXPECT(GetBindInfo);
1447     hres = IInternetProtocol_Start(protocol, emptyW, &protocol_sink, &bind_info, 0, 0);
1448     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1449     CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
1450
1451     IInternetProtocol_Release(protocol);
1452 }
1453
1454 static void test_file_protocol(void) {
1455     WCHAR buf[INTERNET_MAX_URL_LENGTH], file_name_buf[MAX_PATH];
1456     DWORD size;
1457     ULONG len;
1458     HANDLE file;
1459
1460     static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1461     static const WCHAR wszFile2[] = {'f','i','l','e',':','/','/',0};
1462     static const WCHAR wszFile3[] = {'f','i','l','e',':','/','/','/',0};
1463     static const char html_doc[] = "<HTML></HTML>";
1464
1465     trace("Testing file protocol...\n");
1466     tested_protocol = FILE_TEST;
1467
1468     SetLastError(0xdeadbeef);
1469     file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1470             FILE_ATTRIBUTE_NORMAL, NULL);
1471     if(!file && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1472     {
1473         win_skip("Detected Win9x or WinMe\n");
1474         return;
1475     }
1476     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1477     if(file == INVALID_HANDLE_VALUE)
1478         return;
1479     WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
1480     CloseHandle(file);
1481
1482     file_name = wszIndexHtml;
1483     bindf = 0;
1484     test_file_protocol_url(index_url);
1485     bindf = BINDF_FROMURLMON;
1486     test_file_protocol_url(index_url);
1487     bindf = BINDF_FROMURLMON | BINDF_NEEDFILE;
1488     test_file_protocol_url(index_url);
1489
1490     memcpy(buf, wszFile, sizeof(wszFile));
1491     len = sizeof(wszFile)/sizeof(WCHAR)-1;
1492     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
1493     buf[len++] = '\\';
1494     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
1495
1496     file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
1497     bindf = 0;
1498     test_file_protocol_url(buf);
1499     bindf = BINDF_FROMURLMON;
1500     test_file_protocol_url(buf);
1501
1502     memcpy(buf, wszFile2, sizeof(wszFile2));
1503     len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
1504     file_name_buf[len++] = '\\';
1505     memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
1506     lstrcpyW(buf+sizeof(wszFile2)/sizeof(WCHAR)-1, file_name_buf);
1507     file_name = file_name_buf;
1508     bindf = 0;
1509     test_file_protocol_url(buf);
1510     bindf = BINDF_FROMURLMON;
1511     test_file_protocol_url(buf);
1512
1513     buf[sizeof(wszFile2)/sizeof(WCHAR)] = '|';
1514     test_file_protocol_url(buf);
1515
1516     memcpy(buf, wszFile3, sizeof(wszFile3));
1517     len = sizeof(wszFile3)/sizeof(WCHAR)-1;
1518     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
1519     buf[len++] = '\\';
1520     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
1521
1522     file_name = buf + sizeof(wszFile3)/sizeof(WCHAR)-1;
1523     bindf = 0;
1524     test_file_protocol_url(buf);
1525     bindf = BINDF_FROMURLMON;
1526     test_file_protocol_url(buf);
1527
1528     DeleteFileW(wszIndexHtml);
1529
1530     bindf = 0;
1531     test_file_protocol_fail();
1532     bindf = BINDF_FROMURLMON;
1533     test_file_protocol_fail();
1534 }
1535
1536 static BOOL http_protocol_start(LPCWSTR url, BOOL is_first)
1537 {
1538     static BOOL got_user_agent = FALSE;
1539     HRESULT hres;
1540
1541     first_data_notif = TRUE;
1542     state = 0;
1543
1544     SET_EXPECT(GetBindInfo);
1545     if (!(bindf & BINDF_FROMURLMON))
1546         SET_EXPECT(ReportProgress_DIRECTBIND);
1547     SET_EXPECT(GetBindString_USER_AGENT);
1548     SET_EXPECT(GetBindString_ACCEPT_MIMES);
1549     SET_EXPECT(QueryService_HttpNegotiate);
1550     SET_EXPECT(BeginningTransaction);
1551     SET_EXPECT(GetRootSecurityId);
1552     if (http_post_test)
1553         SET_EXPECT(GetBindString_POST_COOKIE);
1554
1555     hres = IInternetProtocol_Start(async_protocol, url, &protocol_sink, &bind_info, 0, 0);
1556     ok(hres == S_OK, "Start failed: %08x\n", hres);
1557     if(FAILED(hres))
1558         return FALSE;
1559
1560     CHECK_CALLED(GetBindInfo);
1561     if (!(bindf & BINDF_FROMURLMON))
1562         CHECK_CALLED(ReportProgress_DIRECTBIND);
1563     if (!got_user_agent)
1564     {
1565         CHECK_CALLED(GetBindString_USER_AGENT);
1566         got_user_agent = TRUE;
1567     }
1568     else todo_wine
1569     {
1570         /* user agent only retrieved once, even with different URLs */
1571         CHECK_NOT_CALLED(GetBindString_USER_AGENT);
1572     }
1573     CHECK_CALLED(GetBindString_ACCEPT_MIMES);
1574     CHECK_CALLED(QueryService_HttpNegotiate);
1575     CHECK_CALLED(BeginningTransaction);
1576     /* GetRootSecurityId called on WinXP but not on Win98 */
1577     CLEAR_CALLED(GetRootSecurityId);
1578     if (http_post_test)
1579         CHECK_CALLED(GetBindString_POST_COOKIE);
1580
1581     return TRUE;
1582 }
1583
1584 static void test_protocol_terminate(IInternetProtocol *protocol)
1585 {
1586     BYTE buf[3600];
1587     DWORD cb;
1588     HRESULT hres;
1589
1590     hres = IInternetProtocol_LockRequest(protocol, 0);
1591     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1592
1593     hres = IInternetProtocol_Read(protocol, buf, 1, &cb);
1594     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1595
1596     hres = IInternetProtocol_Terminate(protocol, 0);
1597     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1598
1599     /* This wait is to give the internet handles being freed in Terminate
1600      * enough time to actually terminate in all cases. Internet handles
1601      * terminate asynchronously and native reuses the main InternetOpen
1602      * handle. The only case in which this seems to be necessary is on
1603      * wine with native wininet and urlmon, resulting in the next time
1604      * test_http_protocol_url being called the first data notification actually
1605      * being an extra last data notification from the previous connection
1606      * about once out of every ten times. */
1607     Sleep(100);
1608
1609     hres = IInternetProtocol_UnlockRequest(protocol);
1610     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1611 }
1612
1613 /* is_first refers to whether this is the first call to this function
1614  * _for this url_ */
1615 static void test_http_protocol_url(LPCWSTR url, BOOL is_https, BOOL is_first)
1616 {
1617     IInternetProtocolInfo *protocol_info;
1618     IClassFactory *factory;
1619     IUnknown *unk;
1620     HRESULT hres;
1621
1622     http_url = url;
1623     http_is_first = is_first;
1624
1625     hres = CoGetClassObject(is_https ? &CLSID_HttpSProtocol : &CLSID_HttpProtocol,
1626             CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1627     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1628     if(FAILED(hres))
1629         return;
1630
1631     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1632     ok(hres == E_NOINTERFACE,
1633         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
1634         hres);
1635
1636     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1637     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1638     IUnknown_Release(unk);
1639     if(FAILED(hres))
1640         return;
1641
1642     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1643                                         (void**)&async_protocol);
1644     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1645     if(SUCCEEDED(hres)) {
1646         BYTE buf[3600];
1647         DWORD cb;
1648         ULONG ref;
1649
1650         test_priority(async_protocol);
1651
1652         SET_EXPECT(ReportProgress_FINDINGRESOURCE);
1653         SET_EXPECT(ReportProgress_CONNECTING);
1654         SET_EXPECT(ReportProgress_SENDINGREQUEST);
1655         SET_EXPECT(ReportProgress_PROXYDETECTING);
1656         if(! is_https)
1657             SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1658         if(!(bindf & BINDF_FROMURLMON)) {
1659             SET_EXPECT(OnResponse);
1660             SET_EXPECT(ReportProgress_RAWMIMETYPE);
1661             SET_EXPECT(ReportData);
1662         } else {
1663             SET_EXPECT(Switch);
1664         }
1665
1666         if(!http_protocol_start(url, is_first))
1667             return;
1668
1669         SET_EXPECT(ReportResult);
1670         expect_hrResult = S_OK;
1671
1672         hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
1673         ok((hres == E_PENDING && cb==0) ||
1674            (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1675
1676         WaitForSingleObject(event_complete, INFINITE);
1677         if(bindf & BINDF_FROMURLMON)
1678             CHECK_CALLED(Switch);
1679         else
1680             CHECK_CALLED(ReportData);
1681
1682         while(1) {
1683             if(bindf & BINDF_FROMURLMON)
1684                 SET_EXPECT(Switch);
1685             else
1686                 SET_EXPECT(ReportData);
1687             hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
1688             if(hres == E_PENDING) {
1689                 hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
1690                 ok((hres == E_PENDING && cb==0) ||
1691                    (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1692                 WaitForSingleObject(event_complete, INFINITE);
1693                 if(bindf & BINDF_FROMURLMON)
1694                     CHECK_CALLED(Switch);
1695                 else
1696                     CHECK_CALLED(ReportData);
1697             }else {
1698                 if(bindf & BINDF_FROMURLMON)
1699                     CHECK_NOT_CALLED(Switch);
1700                 else
1701                     CHECK_NOT_CALLED(ReportData);
1702                 if(cb == 0) break;
1703             }
1704         }
1705         ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1706         CHECK_CALLED(ReportResult);
1707
1708         test_protocol_terminate(async_protocol);
1709         ref = IInternetProtocol_Release(async_protocol);
1710         ok(!ref, "ref=%x\n", hres);
1711     }
1712
1713     IClassFactory_Release(factory);
1714 }
1715
1716 static void test_http_protocol(void)
1717 {
1718     static const WCHAR winehq_url[] =
1719         {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
1720             'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
1721     static const WCHAR posttest_url[] =
1722         {'h','t','t','p',':','/','/','c','r','o','s','s','o','v','e','r','.',
1723          'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m','/',
1724          'p','o','s','t','t','e','s','t','.','p','h','p',0};
1725
1726     trace("Testing http protocol (not from urlmon)...\n");
1727     tested_protocol = HTTP_TEST;
1728     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
1729     test_http_protocol_url(winehq_url, FALSE, TRUE);
1730
1731     trace("Testing http protocol (from urlmon)...\n");
1732     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
1733     test_http_protocol_url(winehq_url, FALSE, FALSE);
1734
1735     trace("Testing http protocol (to file)...\n");
1736     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
1737     test_http_protocol_url(winehq_url, FALSE, FALSE);
1738
1739     trace("Testing http protocol (post data)...\n");
1740     http_post_test = TRUE;
1741     /* Without this flag we get a ReportProgress_CACHEFILENAMEAVAILABLE
1742      * notification with BINDVERB_POST */
1743     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
1744     test_http_protocol_url(posttest_url, FALSE, TRUE);
1745     http_post_test = FALSE;
1746 }
1747
1748 static void test_https_protocol(void)
1749 {
1750     static const WCHAR codeweavers_url[] =
1751         {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
1752          '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0};
1753
1754     trace("Testing https protocol (from urlmon)...\n");
1755     tested_protocol = HTTPS_TEST;
1756     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
1757     test_http_protocol_url(codeweavers_url, TRUE, TRUE);
1758 }
1759
1760
1761 static void test_ftp_protocol(void)
1762 {
1763     IInternetProtocolInfo *protocol_info;
1764     IClassFactory *factory;
1765     IUnknown *unk;
1766     BYTE buf[4096];
1767     ULONG ref;
1768     DWORD cb;
1769     HRESULT hres;
1770
1771     static const WCHAR ftp_urlW[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
1772     '/','p','u','b','/','o','t','h','e','r','/',
1773     'w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0};
1774
1775     trace("Testing ftp protocol...\n");
1776
1777     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
1778     state = 0;
1779     tested_protocol = FTP_TEST;
1780     first_data_notif = TRUE;
1781     expect_hrResult = E_PENDING;
1782
1783     hres = CoGetClassObject(&CLSID_FtpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1784     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1785     if(FAILED(hres))
1786         return;
1787
1788     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1789     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1790
1791     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1792     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1793     IUnknown_Release(unk);
1794     if(FAILED(hres))
1795         return;
1796
1797     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1798                                         (void**)&async_protocol);
1799     IClassFactory_Release(factory);
1800     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1801
1802     test_priority(async_protocol);
1803
1804     SET_EXPECT(GetBindInfo);
1805     SET_EXPECT(GetBindString_USER_AGENT);
1806     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
1807     SET_EXPECT(ReportProgress_CONNECTING);
1808     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1809     SET_EXPECT(Switch);
1810
1811     hres = IInternetProtocol_Start(async_protocol, ftp_urlW, &protocol_sink, &bind_info, 0, 0);
1812     ok(hres == S_OK, "Start failed: %08x\n", hres);
1813     CHECK_CALLED(GetBindInfo);
1814     todo_wine CHECK_NOT_CALLED(GetBindString_USER_AGENT);
1815
1816     SET_EXPECT(ReportResult);
1817
1818     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
1819     ok((hres == E_PENDING && cb==0) ||
1820        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1821
1822     WaitForSingleObject(event_complete, INFINITE);
1823     CHECK_CALLED(Switch);
1824
1825     while(1) {
1826         SET_EXPECT(Switch);
1827
1828         hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
1829         if(hres == E_PENDING) {
1830             WaitForSingleObject(event_complete, INFINITE);
1831             CHECK_CALLED(Switch);
1832         }else {
1833             CHECK_NOT_CALLED(Switch);
1834             if(cb == 0) break;
1835         }
1836     }
1837
1838     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1839     CHECK_CALLED(ReportResult);
1840
1841     test_protocol_terminate(async_protocol);
1842
1843     ref = IInternetProtocol_Release(async_protocol);
1844     ok(!ref, "ref=%d\n", ref);
1845 }
1846
1847 static void test_gopher_protocol(void)
1848 {
1849     IInternetProtocolInfo *protocol_info;
1850     IClassFactory *factory;
1851     IUnknown *unk;
1852     HRESULT hres;
1853
1854     trace("Testing gopher protocol...\n");
1855
1856     hres = CoGetClassObject(&CLSID_GopherProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1857     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1858     if(FAILED(hres))
1859         return;
1860
1861     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1862     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1863
1864     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1865     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1866     IUnknown_Release(unk);
1867     if(FAILED(hres))
1868         return;
1869
1870     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1871                                         (void**)&async_protocol);
1872     IClassFactory_Release(factory);
1873     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1874
1875     test_priority(async_protocol);
1876
1877     IInternetProtocol_Release(async_protocol);
1878 }
1879
1880 static void test_mk_protocol(void)
1881 {
1882     IInternetProtocolInfo *protocol_info;
1883     IInternetProtocol *protocol;
1884     IClassFactory *factory;
1885     IUnknown *unk;
1886     HRESULT hres;
1887
1888     static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
1889                                        ':',':','/','t','e','s','t','.','h','t','m','l',0};
1890     static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};
1891
1892     trace("Testing mk protocol...\n");
1893     tested_protocol = MK_TEST;
1894
1895     hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
1896             &IID_IUnknown, (void**)&unk);
1897     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1898
1899     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1900     ok(hres == E_NOINTERFACE,
1901         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
1902         hres);
1903
1904     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1905     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1906     IUnknown_Release(unk);
1907     if(FAILED(hres))
1908         return;
1909
1910     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1911                                         (void**)&protocol);
1912     IClassFactory_Release(factory);
1913     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1914
1915     SET_EXPECT(GetBindInfo);
1916     hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
1917     ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
1918        "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
1919     CLEAR_CALLED(GetBindInfo);
1920
1921     SET_EXPECT(GetBindInfo);
1922     SET_EXPECT(ReportProgress_DIRECTBIND);
1923     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1924     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1925     SET_EXPECT(ReportResult);
1926     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
1927
1928     hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
1929     ok(hres == INET_E_RESOURCE_NOT_FOUND ||
1930        hres == INET_E_INVALID_URL, /* win2k3 */
1931        "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
1932
1933     if (hres == INET_E_RESOURCE_NOT_FOUND) {
1934         CHECK_CALLED(GetBindInfo);
1935         CLEAR_CALLED(ReportProgress_DIRECTBIND);
1936         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1937         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1938         CHECK_CALLED(ReportResult);
1939     }else {
1940         CLEAR_CALLED(GetBindInfo);
1941         CLEAR_CALLED(ReportProgress_DIRECTBIND);
1942         CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
1943         CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1944         CLEAR_CALLED(ReportResult);
1945     }
1946
1947     IInternetProtocol_Release(protocol);
1948 }
1949
1950 static void test_CreateBinding(void)
1951 {
1952     IInternetProtocolSink *sink;
1953     IInternetProtocol *protocol;
1954     IInternetPriority *priority;
1955     IInternetSession *session;
1956     LONG p;
1957     BYTE buf[1000];
1958     DWORD read;
1959     HRESULT hres;
1960
1961     static const WCHAR test_url[] =
1962         {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
1963     static const WCHAR wsz_test[] = {'t','e','s','t',0};
1964
1965     trace("Testing CreateBinding...\n");
1966     tested_protocol = BIND_TEST;
1967     binding_test = TRUE;
1968
1969     hres = CoInternetGetSession(0, &session, 0);
1970     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
1971
1972     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
1973     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
1974
1975     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
1976     binding_protocol = protocol;
1977     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
1978     ok(protocol != NULL, "protocol == NULL\n");
1979
1980     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
1981     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
1982
1983     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&sink);
1984     ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);
1985
1986     hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
1987     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1988     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
1989     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1990     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
1991     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1992
1993     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
1994     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
1995
1996     p = 0xdeadbeef;
1997     hres = IInternetPriority_GetPriority(priority, &p);
1998     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1999     ok(!p, "p=%d\n", p);
2000
2001     ex_priority = 100;
2002     hres = IInternetPriority_SetPriority(priority, 100);
2003     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2004
2005     p = 0xdeadbeef;
2006     hres = IInternetPriority_GetPriority(priority, &p);
2007     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2008     ok(p == 100, "p=%d\n", p);
2009
2010     SET_EXPECT(QueryService_InternetProtocol);
2011     SET_EXPECT(CreateInstance);
2012     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2013     SET_EXPECT(SetPriority);
2014     SET_EXPECT(Start);
2015
2016     expect_hrResult = S_OK;
2017     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
2018     ok(hres == S_OK, "Start failed: %08x\n", hres);
2019
2020     CHECK_CALLED(QueryService_InternetProtocol);
2021     CHECK_CALLED(CreateInstance);
2022     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2023     CHECK_CALLED(SetPriority);
2024     CHECK_CALLED(Start);
2025
2026     SET_EXPECT(Read);
2027     read = 0xdeadbeef;
2028     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2029     ok(hres == S_OK, "Read failed: %08x\n", hres);
2030     ok(read == 100, "read = %d\n", read);
2031     CHECK_CALLED(Read);
2032
2033     SET_EXPECT(Read);
2034     read = 0xdeadbeef;
2035     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2036     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2037     ok(!read, "read = %d\n", read);
2038     CHECK_CALLED(Read);
2039
2040     p = 0xdeadbeef;
2041     hres = IInternetPriority_GetPriority(priority, &p);
2042     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2043     ok(p == 100, "p=%d\n", p);
2044
2045     hres = IInternetPriority_SetPriority(priority, 101);
2046     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2047
2048     SET_EXPECT(Terminate);
2049     hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
2050     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2051     CHECK_CALLED(Terminate);
2052
2053     SET_EXPECT(Continue);
2054     hres = IInternetProtocolSink_Switch(sink, &protocoldata);
2055     ok(hres == S_OK, "Switch failed: %08x\n", hres);
2056     CHECK_CALLED(Continue);
2057
2058     hres = IInternetProtocolSink_ReportProgress(sink,
2059             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
2060     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
2061
2062     hres = IInternetProtocolSink_ReportResult(sink, S_OK, ERROR_SUCCESS, NULL);
2063     ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);
2064
2065     hres = IInternetProtocolSink_ReportData(sink, 0, 0, 0);
2066     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
2067
2068     IInternetProtocolSink_Release(sink);
2069     IInternetPriority_Release(priority);
2070     IInternetBindInfo_Release(prot_bind_info);
2071     IInternetProtocol_Release(protocol);
2072     IInternetSession_Release(session);
2073 }
2074
2075 static void test_binding(int prot)
2076 {
2077     IInternetProtocol *protocol;
2078     IInternetSession *session;
2079     ULONG ref;
2080     HRESULT hres;
2081
2082     trace("Testing %s binding...\n", debugstr_w(protocol_names[prot]));
2083
2084     tested_protocol = prot;
2085     binding_test = TRUE;
2086     first_data_notif = TRUE;
2087     prot_read = 0;
2088
2089     hres = CoInternetGetSession(0, &session, 0);
2090     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2091
2092     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
2093     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
2094
2095     hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
2096     binding_protocol = protocol;
2097     IInternetSession_Release(session);
2098     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
2099     ok(protocol != NULL, "protocol == NULL\n");
2100
2101     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
2102     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
2103
2104     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
2105     ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
2106
2107     ex_priority = 0;
2108     SET_EXPECT(QueryService_InternetProtocol);
2109     SET_EXPECT(CreateInstance);
2110     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2111     SET_EXPECT(SetPriority);
2112     SET_EXPECT(Start);
2113
2114     expect_hrResult = S_OK;
2115     hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, 0, 0);
2116     ok(hres == S_OK, "Start failed: %08x\n", hres);
2117
2118     CHECK_CALLED(QueryService_InternetProtocol);
2119     CHECK_CALLED(CreateInstance);
2120     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2121     CHECK_CALLED(SetPriority);
2122     CHECK_CALLED(Start);
2123
2124     if(prot == HTTP_TEST || prot == HTTPS_TEST) {
2125         while(prot_state < 4) {
2126             WaitForSingleObject(event_complete, INFINITE);
2127             SET_EXPECT(Continue);
2128             IInternetProtocol_Continue(protocol, pdata);
2129             CHECK_CALLED(Continue);
2130             SetEvent(event_complete2);
2131         }
2132
2133         WaitForSingleObject(event_complete, INFINITE);
2134     }else {
2135         SET_EXPECT(LockRequest);
2136         hres = IInternetProtocol_LockRequest(protocol, 0);
2137         ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2138         CHECK_CALLED(LockRequest);
2139
2140         SET_EXPECT(UnlockRequest);
2141         hres = IInternetProtocol_UnlockRequest(protocol);
2142         ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2143         CHECK_CALLED(UnlockRequest);
2144     }
2145
2146     SET_EXPECT(Terminate);
2147     hres = IInternetProtocol_Terminate(protocol, 0);
2148     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2149     CHECK_CALLED(Terminate);
2150
2151     IInternetBindInfo_Release(prot_bind_info);
2152     IInternetProtocolSink_Release(binding_sink);
2153     ref = IInternetProtocol_Release(protocol);
2154     ok(!ref, "ref=%u, expected 0\n", ref);
2155 }
2156
2157 START_TEST(protocol)
2158 {
2159     OleInitialize(NULL);
2160
2161     event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
2162     event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
2163
2164     test_file_protocol();
2165     test_http_protocol();
2166     test_https_protocol();
2167     test_ftp_protocol();
2168     test_gopher_protocol();
2169     test_mk_protocol();
2170     test_CreateBinding();
2171     test_binding(FILE_TEST);
2172     test_binding(HTTP_TEST);
2173
2174     CloseHandle(event_complete);
2175     CloseHandle(event_complete2);
2176
2177     OleUninitialize();
2178 }