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