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