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