urlmon: Added IWinInetHttpInfo stub implementation to HttpProtocol object.
[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 static void test_http_info(IInternetProtocol *protocol)
1614 {
1615     IWinInetHttpInfo *info;
1616     HRESULT hres;
1617
1618     hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&info);
1619     ok(hres == S_OK, "Could not get IWinInterHttpInfo iface: %08x\n", hres);
1620
1621     /* TODO */
1622
1623     IWinInetHttpInfo_Release(info);
1624 }
1625
1626 /* is_first refers to whether this is the first call to this function
1627  * _for this url_ */
1628 static void test_http_protocol_url(LPCWSTR url, BOOL is_https, BOOL is_first)
1629 {
1630     IInternetProtocolInfo *protocol_info;
1631     IClassFactory *factory;
1632     IUnknown *unk;
1633     HRESULT hres;
1634
1635     http_url = url;
1636     http_is_first = is_first;
1637
1638     hres = CoGetClassObject(is_https ? &CLSID_HttpSProtocol : &CLSID_HttpProtocol,
1639             CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1640     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1641     if(FAILED(hres))
1642         return;
1643
1644     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1645     ok(hres == E_NOINTERFACE,
1646         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
1647         hres);
1648
1649     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1650     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1651     IUnknown_Release(unk);
1652     if(FAILED(hres))
1653         return;
1654
1655     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1656                                         (void**)&async_protocol);
1657     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1658     if(SUCCEEDED(hres)) {
1659         BYTE buf[3600];
1660         DWORD cb;
1661         ULONG ref;
1662
1663         test_priority(async_protocol);
1664         test_http_info(async_protocol);
1665
1666         SET_EXPECT(ReportProgress_FINDINGRESOURCE);
1667         SET_EXPECT(ReportProgress_CONNECTING);
1668         SET_EXPECT(ReportProgress_SENDINGREQUEST);
1669         SET_EXPECT(ReportProgress_PROXYDETECTING);
1670         if(! is_https)
1671             SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1672         if(!(bindf & BINDF_FROMURLMON)) {
1673             SET_EXPECT(OnResponse);
1674             SET_EXPECT(ReportProgress_RAWMIMETYPE);
1675             SET_EXPECT(ReportData);
1676         } else {
1677             SET_EXPECT(Switch);
1678         }
1679
1680         if(!http_protocol_start(url, is_first))
1681             return;
1682
1683         SET_EXPECT(ReportResult);
1684         expect_hrResult = S_OK;
1685
1686         hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
1687         ok((hres == E_PENDING && cb==0) ||
1688            (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1689
1690         WaitForSingleObject(event_complete, INFINITE);
1691         if(bindf & BINDF_FROMURLMON)
1692             CHECK_CALLED(Switch);
1693         else
1694             CHECK_CALLED(ReportData);
1695
1696         while(1) {
1697             if(bindf & BINDF_FROMURLMON)
1698                 SET_EXPECT(Switch);
1699             else
1700                 SET_EXPECT(ReportData);
1701             hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
1702             if(hres == E_PENDING) {
1703                 hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
1704                 ok((hres == E_PENDING && cb==0) ||
1705                    (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1706                 WaitForSingleObject(event_complete, INFINITE);
1707                 if(bindf & BINDF_FROMURLMON)
1708                     CHECK_CALLED(Switch);
1709                 else
1710                     CHECK_CALLED(ReportData);
1711             }else {
1712                 if(bindf & BINDF_FROMURLMON)
1713                     CHECK_NOT_CALLED(Switch);
1714                 else
1715                     CHECK_NOT_CALLED(ReportData);
1716                 if(cb == 0) break;
1717             }
1718         }
1719         ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1720         CHECK_CALLED(ReportResult);
1721
1722         test_protocol_terminate(async_protocol);
1723         ref = IInternetProtocol_Release(async_protocol);
1724         ok(!ref, "ref=%x\n", hres);
1725     }
1726
1727     IClassFactory_Release(factory);
1728 }
1729
1730 static void test_http_protocol(void)
1731 {
1732     static const WCHAR winehq_url[] =
1733         {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
1734             'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
1735     static const WCHAR posttest_url[] =
1736         {'h','t','t','p',':','/','/','c','r','o','s','s','o','v','e','r','.',
1737          'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m','/',
1738          'p','o','s','t','t','e','s','t','.','p','h','p',0};
1739
1740     trace("Testing http protocol (not from urlmon)...\n");
1741     tested_protocol = HTTP_TEST;
1742     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
1743     test_http_protocol_url(winehq_url, FALSE, TRUE);
1744
1745     trace("Testing http protocol (from urlmon)...\n");
1746     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
1747     test_http_protocol_url(winehq_url, FALSE, FALSE);
1748
1749     trace("Testing http protocol (to file)...\n");
1750     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
1751     test_http_protocol_url(winehq_url, FALSE, FALSE);
1752
1753     trace("Testing http protocol (post data)...\n");
1754     http_post_test = TRUE;
1755     /* Without this flag we get a ReportProgress_CACHEFILENAMEAVAILABLE
1756      * notification with BINDVERB_POST */
1757     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
1758     test_http_protocol_url(posttest_url, FALSE, TRUE);
1759     http_post_test = FALSE;
1760 }
1761
1762 static void test_https_protocol(void)
1763 {
1764     static const WCHAR codeweavers_url[] =
1765         {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
1766          '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0};
1767
1768     trace("Testing https protocol (from urlmon)...\n");
1769     tested_protocol = HTTPS_TEST;
1770     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
1771     test_http_protocol_url(codeweavers_url, TRUE, TRUE);
1772 }
1773
1774
1775 static void test_ftp_protocol(void)
1776 {
1777     IInternetProtocolInfo *protocol_info;
1778     IClassFactory *factory;
1779     IUnknown *unk;
1780     BYTE buf[4096];
1781     ULONG ref;
1782     DWORD cb;
1783     HRESULT hres;
1784
1785     static const WCHAR ftp_urlW[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
1786     '/','p','u','b','/','o','t','h','e','r','/',
1787     'w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0};
1788
1789     trace("Testing ftp protocol...\n");
1790
1791     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
1792     state = 0;
1793     tested_protocol = FTP_TEST;
1794     first_data_notif = TRUE;
1795     expect_hrResult = E_PENDING;
1796
1797     hres = CoGetClassObject(&CLSID_FtpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1798     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1799     if(FAILED(hres))
1800         return;
1801
1802     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1803     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1804
1805     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1806     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1807     IUnknown_Release(unk);
1808     if(FAILED(hres))
1809         return;
1810
1811     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1812                                         (void**)&async_protocol);
1813     IClassFactory_Release(factory);
1814     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1815
1816     test_priority(async_protocol);
1817
1818     SET_EXPECT(GetBindInfo);
1819     SET_EXPECT(GetBindString_USER_AGENT);
1820     SET_EXPECT(ReportProgress_FINDINGRESOURCE);
1821     SET_EXPECT(ReportProgress_CONNECTING);
1822     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1823     SET_EXPECT(Switch);
1824
1825     hres = IInternetProtocol_Start(async_protocol, ftp_urlW, &protocol_sink, &bind_info, 0, 0);
1826     ok(hres == S_OK, "Start failed: %08x\n", hres);
1827     CHECK_CALLED(GetBindInfo);
1828     todo_wine CHECK_NOT_CALLED(GetBindString_USER_AGENT);
1829
1830     SET_EXPECT(ReportResult);
1831
1832     hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
1833     ok((hres == E_PENDING && cb==0) ||
1834        (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1835
1836     WaitForSingleObject(event_complete, INFINITE);
1837     CHECK_CALLED(Switch);
1838
1839     while(1) {
1840         SET_EXPECT(Switch);
1841
1842         hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
1843         if(hres == E_PENDING) {
1844             WaitForSingleObject(event_complete, INFINITE);
1845             CHECK_CALLED(Switch);
1846         }else {
1847             CHECK_NOT_CALLED(Switch);
1848             if(cb == 0) break;
1849         }
1850     }
1851
1852     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1853     CHECK_CALLED(ReportResult);
1854
1855     test_protocol_terminate(async_protocol);
1856
1857     ref = IInternetProtocol_Release(async_protocol);
1858     ok(!ref, "ref=%d\n", ref);
1859 }
1860
1861 static void test_gopher_protocol(void)
1862 {
1863     IInternetProtocolInfo *protocol_info;
1864     IClassFactory *factory;
1865     IUnknown *unk;
1866     HRESULT hres;
1867
1868     trace("Testing gopher protocol...\n");
1869
1870     hres = CoGetClassObject(&CLSID_GopherProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1871     ok(hres == S_OK ||
1872        hres == REGDB_E_CLASSNOTREG, /* Gopher protocol has been removed as of Vista */
1873        "CoGetClassObject failed: %08x\n", hres);
1874     if(FAILED(hres))
1875         return;
1876
1877     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1878     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1879
1880     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1881     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1882     IUnknown_Release(unk);
1883     if(FAILED(hres))
1884         return;
1885
1886     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1887                                         (void**)&async_protocol);
1888     IClassFactory_Release(factory);
1889     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1890
1891     test_priority(async_protocol);
1892
1893     IInternetProtocol_Release(async_protocol);
1894 }
1895
1896 static void test_mk_protocol(void)
1897 {
1898     IInternetProtocolInfo *protocol_info;
1899     IInternetProtocol *protocol;
1900     IClassFactory *factory;
1901     IUnknown *unk;
1902     HRESULT hres;
1903
1904     static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
1905                                        ':',':','/','t','e','s','t','.','h','t','m','l',0};
1906     static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};
1907
1908     trace("Testing mk protocol...\n");
1909     tested_protocol = MK_TEST;
1910
1911     hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
1912             &IID_IUnknown, (void**)&unk);
1913     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1914
1915     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1916     ok(hres == E_NOINTERFACE,
1917         "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
1918         hres);
1919
1920     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1921     ok(hres == S_OK, "Could not get IClassFactory interface\n");
1922     IUnknown_Release(unk);
1923     if(FAILED(hres))
1924         return;
1925
1926     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1927                                         (void**)&protocol);
1928     IClassFactory_Release(factory);
1929     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1930
1931     SET_EXPECT(GetBindInfo);
1932     hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
1933     ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
1934        "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
1935     CLEAR_CALLED(GetBindInfo);
1936
1937     SET_EXPECT(GetBindInfo);
1938     SET_EXPECT(ReportProgress_DIRECTBIND);
1939     SET_EXPECT(ReportProgress_SENDINGREQUEST);
1940     SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1941     SET_EXPECT(ReportResult);
1942     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
1943
1944     hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
1945     ok(hres == INET_E_RESOURCE_NOT_FOUND ||
1946        hres == INET_E_INVALID_URL, /* win2k3 */
1947        "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
1948
1949     if (hres == INET_E_RESOURCE_NOT_FOUND) {
1950         CHECK_CALLED(GetBindInfo);
1951         CLEAR_CALLED(ReportProgress_DIRECTBIND);
1952         CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1953         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1954         CHECK_CALLED(ReportResult);
1955     }else {
1956         CLEAR_CALLED(GetBindInfo);
1957         CLEAR_CALLED(ReportProgress_DIRECTBIND);
1958         CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
1959         CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1960         CLEAR_CALLED(ReportResult);
1961     }
1962
1963     IInternetProtocol_Release(protocol);
1964 }
1965
1966 static void test_CreateBinding(void)
1967 {
1968     IInternetProtocolSink *sink;
1969     IInternetProtocol *protocol;
1970     IInternetPriority *priority;
1971     IInternetSession *session;
1972     LONG p;
1973     BYTE buf[1000];
1974     DWORD read;
1975     HRESULT hres;
1976
1977     static const WCHAR test_url[] =
1978         {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
1979     static const WCHAR wsz_test[] = {'t','e','s','t',0};
1980
1981     trace("Testing CreateBinding...\n");
1982     tested_protocol = BIND_TEST;
1983     binding_test = TRUE;
1984
1985     hres = CoInternetGetSession(0, &session, 0);
1986     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
1987
1988     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
1989     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
1990
1991     hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
1992     binding_protocol = protocol;
1993     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
1994     ok(protocol != NULL, "protocol == NULL\n");
1995
1996     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
1997     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
1998
1999     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&sink);
2000     ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);
2001
2002     hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
2003     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2004     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
2005     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2006     hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
2007     ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2008
2009     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
2010     ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
2011
2012     p = 0xdeadbeef;
2013     hres = IInternetPriority_GetPriority(priority, &p);
2014     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2015     ok(!p, "p=%d\n", p);
2016
2017     ex_priority = 100;
2018     hres = IInternetPriority_SetPriority(priority, 100);
2019     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2020
2021     p = 0xdeadbeef;
2022     hres = IInternetPriority_GetPriority(priority, &p);
2023     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2024     ok(p == 100, "p=%d\n", p);
2025
2026     SET_EXPECT(QueryService_InternetProtocol);
2027     SET_EXPECT(CreateInstance);
2028     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2029     SET_EXPECT(SetPriority);
2030     SET_EXPECT(Start);
2031
2032     expect_hrResult = S_OK;
2033     hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
2034     ok(hres == S_OK, "Start failed: %08x\n", hres);
2035
2036     CHECK_CALLED(QueryService_InternetProtocol);
2037     CHECK_CALLED(CreateInstance);
2038     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2039     CHECK_CALLED(SetPriority);
2040     CHECK_CALLED(Start);
2041
2042     SET_EXPECT(Read);
2043     read = 0xdeadbeef;
2044     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2045     ok(hres == S_OK, "Read failed: %08x\n", hres);
2046     ok(read == 100, "read = %d\n", read);
2047     CHECK_CALLED(Read);
2048
2049     SET_EXPECT(Read);
2050     read = 0xdeadbeef;
2051     hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
2052     ok(hres == S_FALSE, "Read failed: %08x\n", hres);
2053     ok(!read, "read = %d\n", read);
2054     CHECK_CALLED(Read);
2055
2056     p = 0xdeadbeef;
2057     hres = IInternetPriority_GetPriority(priority, &p);
2058     ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
2059     ok(p == 100, "p=%d\n", p);
2060
2061     hres = IInternetPriority_SetPriority(priority, 101);
2062     ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2063
2064     SET_EXPECT(Terminate);
2065     hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
2066     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2067     CHECK_CALLED(Terminate);
2068
2069     SET_EXPECT(Continue);
2070     hres = IInternetProtocolSink_Switch(sink, &protocoldata);
2071     ok(hres == S_OK, "Switch failed: %08x\n", hres);
2072     CHECK_CALLED(Continue);
2073
2074     hres = IInternetProtocolSink_ReportProgress(sink,
2075             BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
2076     ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
2077
2078     hres = IInternetProtocolSink_ReportResult(sink, S_OK, ERROR_SUCCESS, NULL);
2079     ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);
2080
2081     hres = IInternetProtocolSink_ReportData(sink, 0, 0, 0);
2082     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
2083
2084     IInternetProtocolSink_Release(sink);
2085     IInternetPriority_Release(priority);
2086     IInternetBindInfo_Release(prot_bind_info);
2087     IInternetProtocol_Release(protocol);
2088     IInternetSession_Release(session);
2089 }
2090
2091 static void test_binding(int prot)
2092 {
2093     IInternetProtocol *protocol;
2094     IInternetSession *session;
2095     ULONG ref;
2096     HRESULT hres;
2097
2098     trace("Testing %s binding...\n", debugstr_w(protocol_names[prot]));
2099
2100     tested_protocol = prot;
2101     binding_test = TRUE;
2102     first_data_notif = TRUE;
2103     prot_read = 0;
2104
2105     hres = CoInternetGetSession(0, &session, 0);
2106     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
2107
2108     hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
2109     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
2110
2111     hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
2112     binding_protocol = protocol;
2113     IInternetSession_Release(session);
2114     ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
2115     ok(protocol != NULL, "protocol == NULL\n");
2116
2117     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
2118     ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
2119
2120     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
2121     ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
2122
2123     ex_priority = 0;
2124     SET_EXPECT(QueryService_InternetProtocol);
2125     SET_EXPECT(CreateInstance);
2126     SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
2127     SET_EXPECT(SetPriority);
2128     SET_EXPECT(Start);
2129
2130     expect_hrResult = S_OK;
2131     hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, 0, 0);
2132     ok(hres == S_OK, "Start failed: %08x\n", hres);
2133
2134     CHECK_CALLED(QueryService_InternetProtocol);
2135     CHECK_CALLED(CreateInstance);
2136     CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
2137     CHECK_CALLED(SetPriority);
2138     CHECK_CALLED(Start);
2139
2140     if(prot == HTTP_TEST || prot == HTTPS_TEST) {
2141         while(prot_state < 4) {
2142             WaitForSingleObject(event_complete, INFINITE);
2143             SET_EXPECT(Continue);
2144             IInternetProtocol_Continue(protocol, pdata);
2145             CHECK_CALLED(Continue);
2146             SetEvent(event_complete2);
2147         }
2148
2149         WaitForSingleObject(event_complete, INFINITE);
2150     }else {
2151         SET_EXPECT(LockRequest);
2152         hres = IInternetProtocol_LockRequest(protocol, 0);
2153         ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
2154         CHECK_CALLED(LockRequest);
2155
2156         SET_EXPECT(UnlockRequest);
2157         hres = IInternetProtocol_UnlockRequest(protocol);
2158         ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2159         CHECK_CALLED(UnlockRequest);
2160     }
2161
2162     SET_EXPECT(Terminate);
2163     hres = IInternetProtocol_Terminate(protocol, 0);
2164     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2165     CHECK_CALLED(Terminate);
2166
2167     IInternetBindInfo_Release(prot_bind_info);
2168     IInternetProtocolSink_Release(binding_sink);
2169     ref = IInternetProtocol_Release(protocol);
2170     ok(!ref, "ref=%u, expected 0\n", ref);
2171 }
2172
2173 START_TEST(protocol)
2174 {
2175     OleInitialize(NULL);
2176
2177     event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
2178     event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
2179
2180     test_file_protocol();
2181     test_http_protocol();
2182     test_https_protocol();
2183     test_ftp_protocol();
2184     test_gopher_protocol();
2185     test_mk_protocol();
2186     test_CreateBinding();
2187     test_binding(FILE_TEST);
2188     test_binding(HTTP_TEST);
2189
2190     CloseHandle(event_complete);
2191     CloseHandle(event_complete2);
2192
2193     OleUninitialize();
2194 }