1 /*
2  * UrlMon URL tests
3  *
4  * Copyright 2004 Kevin Koltzau
5  * Copyright 2004-2007 Jacek Caban for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 
22 #include <stdarg.h>
23 #include <stdio.h>
24 
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define CONST_VTABLE
28 
29 #include "windef.h"
30 #include "winbase.h"
31 #include "initguid.h"
32 #include "urlmon.h"
33 #include "wininet.h"
34 #include "mshtml.h"
35 #include "shlwapi.h"
36 
37 #include "wine/test.h"
38 
39 static HRESULT (WINAPI *pCreateAsyncBindCtxEx)(IBindCtx *, DWORD,
40                 IBindStatusCallback *, IEnumFORMATETC *, IBindCtx **, DWORD);
41 static HRESULT (WINAPI *pCreateUri)(LPCWSTR, DWORD, DWORD_PTR, IUri**);
42 
43 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
44 DEFINE_GUID(CLSID_IdentityUnmarshal,0x0000001b,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
45 DEFINE_GUID(IID_IBindStatusCallbackHolder,0x79eac9cc,0xbaf9,0x11ce,0x8c,0x82,0x00,0xaa,0x00,0x4b,0xa9,0x0b);
46 static const IID IID_undocumentedIE11 = {0xd5ae15f6,0x2032,0x488e,{0x8f,0x96,0xf9,0x24,0x06,0xd8,0xd8,0xb4}};
47 extern CLSID CLSID_AboutProtocol;
48 
49 #define DEFINE_EXPECT(func) \
50     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
51 
52 #define SET_EXPECT(func) \
53     do { called_ ## func = FALSE; expect_ ## func = TRUE; } while(0)
54 
55 #define CHECK_EXPECT2(func) \
56     do { \
57         ok(expect_ ##func, "unexpected call " #func "\n"); \
58         called_ ## func = TRUE; \
59     }while(0)
60 
61 #define CHECK_EXPECT(func) \
62     do { \
63         CHECK_EXPECT2(func); \
64         expect_ ## func = FALSE; \
65     }while(0)
66 
67 #define CHECK_CALLED(func) \
68     do { \
69         ok(called_ ## func, "expected " #func "\n"); \
70         expect_ ## func = called_ ## func = FALSE; \
71     }while(0)
72 
73 #define CHECK_NOT_CALLED(func) \
74     do { \
75         ok(!called_ ## func, "unexpected " #func "\n"); \
76         expect_ ## func = called_ ## func = FALSE; \
77     }while(0)
78 
79 #define CHECK_CALLED_BROKEN(func) \
80     do { \
81         ok(called_ ## func || broken(!called_ ## func), "expected " #func "\n"); \
82         expect_ ## func = called_ ## func = FALSE; \
83     }while(0)
84 
85 #define CLEAR_CALLED(func) \
86     expect_ ## func = called_ ## func = FALSE
87 
88 DEFINE_EXPECT(QueryInterface_IServiceProvider);
89 DEFINE_EXPECT(QueryInterface_IHttpNegotiate);
90 DEFINE_EXPECT(QueryInterface_IBindStatusCallback);
91 DEFINE_EXPECT(QueryInterface_IBindStatusCallbackEx);
92 DEFINE_EXPECT(QueryInterface_IBindStatusCallbackHolder);
93 DEFINE_EXPECT(QueryInterface_IAuthenticate);
94 DEFINE_EXPECT(QueryInterface_IInternetProtocol);
95 DEFINE_EXPECT(QueryInterface_IWindowForBindingUI);
96 DEFINE_EXPECT(QueryInterface_IHttpSecurity);
97 DEFINE_EXPECT(QueryService_IAuthenticate);
98 DEFINE_EXPECT(QueryService_IInternetProtocol);
99 DEFINE_EXPECT(QueryService_IInternetBindInfo);
100 DEFINE_EXPECT(QueryService_IWindowForBindingUI);
101 DEFINE_EXPECT(QueryService_IHttpSecurity);
102 DEFINE_EXPECT(BeginningTransaction);
103 DEFINE_EXPECT(OnResponse);
104 DEFINE_EXPECT(QueryInterface_IHttpNegotiate2);
105 DEFINE_EXPECT(GetRootSecurityId);
106 DEFINE_EXPECT(GetBindInfo);
107 DEFINE_EXPECT(GetBindInfoEx);
108 DEFINE_EXPECT(OnStartBinding);
109 DEFINE_EXPECT(OnProgress_FINDINGRESOURCE);
110 DEFINE_EXPECT(OnProgress_CONNECTING);
111 DEFINE_EXPECT(OnProgress_REDIRECTING);
112 DEFINE_EXPECT(OnProgress_SENDINGREQUEST);
113 DEFINE_EXPECT(OnProgress_MIMETYPEAVAILABLE);
114 DEFINE_EXPECT(OnProgress_BEGINDOWNLOADDATA);
115 DEFINE_EXPECT(OnProgress_DOWNLOADINGDATA);
116 DEFINE_EXPECT(OnProgress_ENDDOWNLOADDATA);
117 DEFINE_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
118 DEFINE_EXPECT(OnStopBinding);
119 DEFINE_EXPECT(OnDataAvailable);
120 DEFINE_EXPECT(OnObjectAvailable);
121 DEFINE_EXPECT(Obj_OnStartBinding);
122 DEFINE_EXPECT(Obj_OnStopBinding);
123 DEFINE_EXPECT(Obj_GetBindInfo);
124 DEFINE_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
125 DEFINE_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
126 DEFINE_EXPECT(Obj_OnProgress_SENDINGREQUEST);
127 DEFINE_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
128 DEFINE_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
129 DEFINE_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
130 DEFINE_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
131 DEFINE_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
132 DEFINE_EXPECT(Obj_OnProgress_CONNECTING);
133 DEFINE_EXPECT(Obj_OnProgress_REDIRECTING);
134 DEFINE_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
135 DEFINE_EXPECT(Start);
136 DEFINE_EXPECT(Read);
137 DEFINE_EXPECT(LockRequest);
138 DEFINE_EXPECT(Terminate);
139 DEFINE_EXPECT(UnlockRequest);
140 DEFINE_EXPECT(Continue);
141 DEFINE_EXPECT(Abort);
142 DEFINE_EXPECT(CreateInstance);
143 DEFINE_EXPECT(Load);
144 DEFINE_EXPECT(PutProperty_MIMETYPEPROP);
145 DEFINE_EXPECT(PutProperty_CLASSIDPROP);
146 DEFINE_EXPECT(SetPriority);
147 DEFINE_EXPECT(GetWindow_IHttpSecurity);
148 DEFINE_EXPECT(GetWindow_IWindowForBindingUI);
149 DEFINE_EXPECT(GetWindow_ICodeInstall);
150 DEFINE_EXPECT(OnSecurityProblem);
151 
152 static const WCHAR winetest_data_urlW[] =
153     {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
154      't','e','s','t','s','/','d','a','t','a','.','p','h','p',0};
155 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
156 
157 static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
158 
159 static WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
160 
161 static const WCHAR wszWineHQSite[] =
162     {'w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
163 static const WCHAR wszWineHQIP[] =
164     {'4','.','1','5','.','1','8','4','.','7','7',0};
165 static const CHAR wszIndexHtmlA[] = "index.html";
166 static const WCHAR cache_fileW[] = {'c',':','\\','c','a','c','h','e','.','h','t','m',0};
167 static const CHAR dwl_htmlA[] = "dwl.html";
168 static const WCHAR dwl_htmlW[] = {'d','w','l','.','h','t','m','l',0};
169 static const CHAR test_txtA[] = "test.txt";
170 static const WCHAR emptyW[] = {0};
171 
172 static BOOL stopped_binding = FALSE, stopped_obj_binding = FALSE, emulate_protocol = FALSE,
173     data_available = FALSE, http_is_first = TRUE, bind_to_object = FALSE, filedwl_api, post_test;
174 static DWORD read = 0, bindf = 0, prot_state = 0, thread_id, tymed, security_problem;
175 static const WCHAR *reported_url;
176 static CHAR mime_type[512];
177 static IInternetProtocolSink *protocol_sink = NULL;
178 static IBinding *current_binding;
179 static HANDLE complete_event, complete_event2;
180 static HRESULT binding_hres;
181 static HRESULT onsecurityproblem_hres;
182 static HRESULT abort_hres;
183 static BOOL have_IHttpNegotiate2, use_bscex, is_async_prot;
184 static BOOL test_redirect, use_cache_file, callback_read, no_callback, test_abort;
185 static WCHAR cache_file_name[MAX_PATH];
186 static WCHAR http_cache_file[MAX_PATH];
187 static BOOL only_check_prot_args = FALSE;
188 static BOOL invalid_cn_accepted = FALSE;
189 static BOOL abort_start = FALSE;
190 static BOOL abort_progress = FALSE;
191 static BOOL async_switch = FALSE;
192 static BOOL strict_bsc_qi;
193 static DWORD bindtest_flags;
194 static const char *test_file;
195 
196 static WCHAR file_url[INTERNET_MAX_URL_LENGTH], current_url[INTERNET_MAX_URL_LENGTH];
197 
198 static enum {
199     HTTP_TEST,
200     ABOUT_TEST,
201     FILE_TEST,
202     ITS_TEST,
203     MK_TEST,
204     HTTPS_TEST,
205     FTP_TEST,
206     WINETEST_TEST,
207     WINETEST_SYNC_TEST
208 } test_protocol;
209 
210 static enum {
211     BEFORE_DOWNLOAD,
212     DOWNLOADING,
213     END_DOWNLOAD
214 } download_state;
215 
216 static BOOL proxy_active(void)
217 {
218     HKEY internet_settings;
219     DWORD proxy_enable;
220     DWORD size;
221 
222     if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
223                       0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
224         return FALSE;
225 
226     size = sizeof(DWORD);
227     if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
228         proxy_enable = 0;
229 
230     RegCloseKey(internet_settings);
231 
232     return proxy_enable != 0;
233 }
234 
235 static BOOL is_urlmon_protocol(int prot)
236 {
237     return prot == FILE_TEST || prot == HTTP_TEST || prot == HTTPS_TEST || prot == FTP_TEST || prot == MK_TEST;
238 }
239 
240 static void test_CreateURLMoniker(LPCWSTR url1, LPCWSTR url2)
241 {
242     HRESULT hr;
243     IMoniker *mon1 = NULL;
244     IMoniker *mon2 = NULL;
245 
246     hr = CreateURLMoniker(NULL, NULL, NULL);
247     ok(hr == E_INVALIDARG,
248        "Expected CreateURLMoniker to return E_INVALIDARG, got 0x%08x\n", hr);
249 
250     mon1 = (IMoniker *)0xdeadbeef;
251     hr = CreateURLMoniker(NULL, NULL, &mon1);
252     ok(hr == E_INVALIDARG,
253        "Expected CreateURLMoniker to return E_INVALIDARG, got 0x%08x\n", hr);
254     ok(mon1 == NULL, "Expected the output pointer to be NULL, got %p\n", mon1);
255 
256     hr = CreateURLMoniker(NULL, emptyW, NULL);
257     ok(hr == E_INVALIDARG,
258        "Expected CreateURLMoniker to return E_INVALIDARG, got 0x%08x\n", hr);
259 
260     hr = CreateURLMoniker(NULL, emptyW, &mon1);
261     ok(hr == S_OK ||
262        broken(hr == MK_E_SYNTAX), /* IE5/IE5.01/IE6 SP2 */
263        "Expected CreateURLMoniker to return S_OK, got 0x%08x\n", hr);
264     if(mon1) IMoniker_Release(mon1);
265 
266     hr = CreateURLMoniker(NULL, url1, &mon1);
267     ok(hr == S_OK, "failed to create moniker: 0x%08x\n", hr);
268     if(hr == S_OK) {
269         hr = CreateURLMoniker(mon1, url2, &mon2);
270         ok(hr == S_OK, "failed to create moniker: 0x%08x\n", hr);
271     }
272     if(mon1) IMoniker_Release(mon1);
273     if(mon2) IMoniker_Release(mon2);
274 }
275 
276 static void test_create(void)
277 {
278     static const WCHAR relativeW[] = {'a','/','b','.','t','x','t',0};
279     IStream *stream;
280     IMoniker *mon;
281     IBindCtx *bctx;
282     HRESULT hr;
283 
284     static const WCHAR TEST_PART_URL_1[] = {'/','t','e','s','t','s','/','d','a','t','a','.','p','h','p',0};
285 
286     test_CreateURLMoniker(winetest_data_urlW, TEST_PART_URL_1);
287 
288     mon = (void*)0xdeadbeef;
289     hr = CreateURLMoniker(NULL, relativeW, &mon);
290     ok(hr == S_OK, "got 0x%08x\n", hr);
291 
292     hr = CreateBindCtx(0, &bctx);
293     ok(hr == S_OK, "got 0x%08x\n", hr);
294 
295     stream = (void*)0xdeadbeef;
296     hr = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&stream);
297     todo_wine ok(hr == INET_E_UNKNOWN_PROTOCOL, "got 0x%08x\n", hr);
298     ok(stream == NULL, "got %p\n", stream);
299 
300     hr = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, NULL);
301     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
302 
303     stream = (void*)0xdeadbeef;
304     hr = IMoniker_BindToStorage(mon, NULL, NULL, &IID_IStream, (void**)&stream);
305     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
306     ok(stream == NULL || broken(stream == (void*)0xdeadbeef) /* starting XP SP3 it's set to null */,
307         "got %p\n", stream);
308 
309     IMoniker_Release(mon);
310 
311     mon = (void*)0xdaedbeef;
312     hr = CreateURLMoniker(NULL, winetest_data_urlW, &mon);
313     ok(hr == S_OK, "got 0x%08x\n", hr);
314 
315     stream = (void*)0xdeadbeef;
316     hr = IMoniker_BindToStorage(mon, NULL, NULL, &IID_IStream, (void**)&stream);
317     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
318     ok(stream == NULL || broken(stream == (void*)0xdeadbeef) /* starting XP SP3 it's set to null */,
319         "got %p\n", stream);
320 
321     hr = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, NULL);
322     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
323 
324     IMoniker_Release(mon);
325     IBindCtx_Release(bctx);
326 }
327 
328 static HRESULT WINAPI Priority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
329 {
330     ok(0, "unexpected call\n");
331     return E_NOINTERFACE;
332 }
333 
334 static ULONG WINAPI Priority_AddRef(IInternetPriority *iface)
335 {
336     return 2;
337 }
338 
339 static ULONG WINAPI Priority_Release(IInternetPriority *iface)
340 {
341     return 1;
342 }
343 
344 static HRESULT WINAPI Priority_SetPriority(IInternetPriority *iface, LONG nPriority)
345 {
346     CHECK_EXPECT(SetPriority);
347     ok(!nPriority, "nPriority = %d\n", nPriority);
348     return S_OK;
349 }
350 
351 static HRESULT WINAPI Priority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
352 {
353     ok(0, "unexpected call\n");
354     return S_OK;
355 }
356 
357 static const IInternetPriorityVtbl InternetPriorityVtbl = {
358     Priority_QueryInterface,
359     Priority_AddRef,
360     Priority_Release,
361     Priority_SetPriority,
362     Priority_GetPriority
363 };
364 
365 static IInternetPriority InternetPriority = { &InternetPriorityVtbl };
366 
367 static HRESULT WINAPI Protocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
368 {
369     static const IID IID_undocumentedIE10 = {0x7daf9908,0x8415,0x4005,{0x95,0xae,0xbd,0x27,0xf6,0xe3,0xdc,0x00}};
370 
371     *ppv = NULL;
372 
373     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
374         *ppv = iface;
375         return S_OK;
376     }
377 
378     if(IsEqualGUID(&IID_IInternetPriority, riid)) {
379         if(!is_urlmon_protocol(test_protocol))
380             return E_NOINTERFACE;
381 
382         *ppv = &InternetPriority;
383         return S_OK;
384     }
385 
386     if(IsEqualGUID(&IID_IInternetProtocolEx, riid))
387         return E_NOINTERFACE; /* TODO */
388 
389     if(IsEqualGUID(&IID_undocumentedIE10, riid)) {
390         trace("QI(%s)\n", wine_dbgstr_guid(riid));
391         return E_NOINTERFACE; /* TODO */
392     }
393 
394     ok(0, "unexpected call %s\n", wine_dbgstr_guid(riid));
395     return E_NOINTERFACE;
396 }
397 
398 static ULONG WINAPI Protocol_AddRef(IInternetProtocol *iface)
399 {
400     return 2;
401 }
402 
403 static ULONG WINAPI Protocol_Release(IInternetProtocol *iface)
404 {
405     return 1;
406 }
407 
408 static void test_switch_fail(void)
409 {
410     IInternetProtocolSink *binding_sink;
411     PROTOCOLDATA protocoldata = {0};
412     HRESULT hres;
413 
414     static BOOL tested_switch_fail;
415 
416     if(tested_switch_fail)
417         return;
418 
419     tested_switch_fail = TRUE;
420 
421     hres = IBinding_QueryInterface(current_binding, &IID_IInternetProtocolSink, (void**)&binding_sink);
422     ok(hres == S_OK, "Could not get IInternetProtocolSink iface: %08x\n", hres);
423     if(SUCCEEDED(hres)) {
424         hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
425         ok(hres == E_FAIL, "Switch failed: %08x, expected E_FAIL\n", hres);
426         IInternetProtocolSink_Release(binding_sink);
427     }
428 }
429 
430 static DWORD WINAPI thread_proc(PVOID arg)
431 {
432     PROTOCOLDATA protocoldata = {0};
433     HRESULT hres;
434 
435     if(!no_callback) {
436         if(bind_to_object)
437             SET_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
438         else
439             SET_EXPECT(OnProgress_FINDINGRESOURCE);
440     }
441     hres = IInternetProtocolSink_ReportProgress(protocol_sink,
442             BINDSTATUS_FINDINGRESOURCE, wszWineHQSite);
443     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
444     if(!no_callback) {
445         ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
446         if(bind_to_object)
447             CHECK_CALLED(Obj_OnProgress_FINDINGRESOURCE);
448         else
449             CHECK_CALLED(OnProgress_FINDINGRESOURCE);
450     }
451 
452     if(!no_callback) {
453         if(bind_to_object)
454             SET_EXPECT(Obj_OnProgress_CONNECTING);
455         else
456             SET_EXPECT(OnProgress_CONNECTING);
457     }
458     hres = IInternetProtocolSink_ReportProgress(protocol_sink,
459             BINDSTATUS_CONNECTING, wszWineHQIP);
460     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
461     if(!no_callback) {
462         ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
463         if(bind_to_object)
464             CHECK_CALLED(Obj_OnProgress_CONNECTING);
465         else
466             CHECK_CALLED(OnProgress_CONNECTING);
467     }
468 
469     if(!no_callback) {
470         if(bind_to_object)
471             SET_EXPECT(Obj_OnProgress_SENDINGREQUEST);
472         else
473             SET_EXPECT(OnProgress_SENDINGREQUEST);
474     }
475     hres = IInternetProtocolSink_ReportProgress(protocol_sink,
476             BINDSTATUS_SENDINGREQUEST, NULL);
477     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
478     if(!no_callback) {
479         ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
480         if(bind_to_object)
481             CHECK_CALLED(Obj_OnProgress_SENDINGREQUEST);
482         else
483             CHECK_CALLED(OnProgress_SENDINGREQUEST);
484     }
485 
486     if(test_redirect) {
487         if(bind_to_object)
488             SET_EXPECT(Obj_OnProgress_REDIRECTING);
489         else
490             SET_EXPECT(OnProgress_REDIRECTING);
491         hres = IInternetProtocolSink_ReportProgress(protocol_sink, BINDSTATUS_REDIRECTING, winetest_data_urlW);
492         ok(hres == S_OK, "ReportProgress(BINDSTATUS_REFIRECTING) failed: %08x\n", hres);
493         ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
494         if(bind_to_object)
495             CHECK_CALLED(Obj_OnProgress_REDIRECTING);
496         else
497             CHECK_CALLED(OnProgress_REDIRECTING);
498     }
499 
500     test_switch_fail();
501 
502     SET_EXPECT(Continue);
503     prot_state = 1;
504     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
505     ok(hres == S_OK, "Switch failed: %08x\n", hres);
506     ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
507 
508     CHECK_CALLED(Continue);
509     CHECK_CALLED(Read);
510     if(bind_to_object) {
511         CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
512         CHECK_CALLED(Obj_OnProgress_BEGINDOWNLOADDATA);
513         CHECK_CALLED(Obj_OnProgress_CLASSIDAVAILABLE);
514         CHECK_CALLED(Obj_OnProgress_BEGINSYNCOPERATION);
515         CHECK_CALLED(CreateInstance);
516         CHECK_CALLED(PutProperty_MIMETYPEPROP);
517         CHECK_CALLED_BROKEN(PutProperty_CLASSIDPROP);
518         CHECK_CALLED(Load);
519         CHECK_CALLED(Obj_OnProgress_ENDSYNCOPERATION);
520         CHECK_CALLED(OnObjectAvailable);
521         CHECK_CALLED(Obj_OnStopBinding);
522     }else if(!no_callback) {
523         CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
524         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
525         CHECK_CALLED(OnDataAvailable);
526     }else {
527         CHECK_CALLED(LockRequest);
528     }
529 
530     SET_EXPECT(Continue);
531     prot_state = 2;
532     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
533     ok(hres == S_OK, "Switch failed: %08x\n", hres);
534     ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
535     CHECK_CALLED(Continue);
536     if(test_abort) {
537         CHECK_CALLED(OnProgress_DOWNLOADINGDATA);
538         CHECK_CALLED(OnStopBinding);
539         SetEvent(complete_event2);
540         return 0;
541     }else {
542         CHECK_CALLED(Read);
543         if(!no_callback) {
544             CHECK_CALLED(OnProgress_DOWNLOADINGDATA);
545             CHECK_CALLED(OnDataAvailable);
546         }
547     }
548 
549     SET_EXPECT(Continue);
550     prot_state = 2;
551     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
552     ok(hres == S_OK, "Switch failed: %08x\n", hres);
553     ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
554     CHECK_CALLED(Continue);
555     CHECK_CALLED(Read);
556     if(!no_callback) {
557         CHECK_CALLED(OnProgress_DOWNLOADINGDATA);
558         CHECK_CALLED(OnDataAvailable);
559     }
560 
561     SET_EXPECT(Continue);
562     prot_state = 3;
563     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
564     ok(hres == S_OK, "Switch failed: %08x\n", hres);
565     ok( WaitForSingleObject(complete_event, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
566     CHECK_CALLED(Continue);
567     CHECK_CALLED(Read);
568     if(!no_callback) {
569         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
570         CHECK_CALLED(OnDataAvailable);
571         CHECK_CALLED(OnStopBinding);
572     }
573 
574     SET_EXPECT(Read);
575 
576     SetEvent(complete_event2);
577     return 0;
578 }
579 
580 static HRESULT WINAPI Protocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
581         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
582         DWORD grfPI, HANDLE_PTR dwReserved)
583 {
584     BINDINFO bindinfo;
585     DWORD bind_info, bscf = BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION;
586     HRESULT hres;
587 
588     static const STGMEDIUM stgmed_zero = {0};
589     static const SECURITY_ATTRIBUTES sa_zero = {0};
590 
591     CHECK_EXPECT(Start);
592 
593     read = 0;
594 
595     reported_url = szUrl;
596     if(!filedwl_api) /* FIXME */
597         ok(szUrl && !lstrcmpW(szUrl, current_url), "wrong url %s\n", wine_dbgstr_w(szUrl));
598     ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
599     ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
600     ok(grfPI == 0, "grfPI=%d, expected 0\n", grfPI);
601     ok(dwReserved == 0, "dwReserved=%lx, expected 0\n", dwReserved);
602 
603     if(!filedwl_api && binding_hres != S_OK) {
604         SET_EXPECT(OnStopBinding);
605         SET_EXPECT(Terminate);
606         hres = IInternetProtocolSink_ReportResult(pOIProtSink, binding_hres, 0, NULL);
607         ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
608         CHECK_CALLED(OnStopBinding);
609         CHECK_CALLED(Terminate);
610 
611         return S_OK;
612     }
613 
614     memset(&bindinfo, 0, sizeof(bindinfo));
615     bindinfo.cbSize = 0;
616     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bind_info, &bindinfo);
617     ok(hres == E_INVALIDARG, "GetBindInfo returned: %08x, expected E_INVALIDARG\n", hres);
618 
619     memset(&bindinfo, 0, sizeof(bindinfo));
620     bindinfo.cbSize = sizeof(bindinfo);
621     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bind_info, &bindinfo);
622     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
623 
624     ok(bind_info & BINDF_FROMURLMON, "BINDF_FROMURLMON is not set\n");
625 
626     if(filedwl_api || !is_urlmon_protocol(test_protocol) || tymed != TYMED_ISTREAM ||
627        !(bindf&BINDF_ASYNCSTORAGE) || !(bindf&BINDF_PULLDATA))
628         ok(bind_info & BINDF_NEEDFILE, "BINDF_NEEDFILE is not set\n");
629     else if(test_protocol != MK_TEST) /* IE10 sets BINDF_NEEDFILE for mk: protocol */
630         ok(!(bind_info & BINDF_NEEDFILE), "BINDF_NEEDFILE is set\n");
631 
632     bind_info &= ~(BINDF_NEEDFILE|BINDF_FROMURLMON);
633     if(filedwl_api || no_callback)
634         ok(bind_info == BINDF_PULLDATA, "bind_info = %x, expected BINDF_PULLDATA\n", bind_info);
635     else
636         ok(bind_info == (bindf & ~(BINDF_NEEDFILE|BINDF_FROMURLMON)), "bind_info = %x, expected %x\n",
637            bind_info, (bindf & ~(BINDF_NEEDFILE|BINDF_FROMURLMON)));
638 
639     ok(bindinfo.cbSize == sizeof(bindinfo), "bindinfo.cbSize = %d\n", bindinfo.cbSize);
640     ok(!bindinfo.szExtraInfo, "bindinfo.szExtraInfo = %p\n", bindinfo.szExtraInfo);
641     ok(!memcmp(&bindinfo.stgmedData, &stgmed_zero, sizeof(STGMEDIUM)), "wrong stgmedData\n");
642     ok(!bindinfo.grfBindInfoF, "bindinfo.grfBindInfoF = %d\n", bindinfo.grfBindInfoF);
643     ok(!bindinfo.dwBindVerb, "bindinfo.dwBindVerb = %d\n", bindinfo.dwBindVerb);
644     ok(!bindinfo.szCustomVerb, "bindinfo.szCustomVerb = %p\n", bindinfo.szCustomVerb);
645     ok(!bindinfo.cbstgmedData, "bindinfo.cbstgmedData = %d\n", bindinfo.cbstgmedData);
646     ok(bindinfo.dwOptions == (bind_to_object ? 0x100000 : 0), "bindinfo.dwOptions = %x\n", bindinfo.dwOptions);
647     ok(!bindinfo.dwOptionsFlags, "bindinfo.dwOptionsFlags = %d\n", bindinfo.dwOptionsFlags);
648     ok(!bindinfo.dwCodePage, "bindinfo.dwCodePage = %d\n", bindinfo.dwCodePage);
649     ok(!memcmp(&bindinfo.securityAttributes, &sa_zero, sizeof(sa_zero)), "wrong bindinfo.securityAttributes\n");
650     ok(IsEqualGUID(&bindinfo.iid, &IID_NULL), "wrong bindinfo.iid\n");
651     ok(!bindinfo.pUnk, "bindinfo.pUnk = %p\n", bindinfo.pUnk);
652     ok(!bindinfo.dwReserved, "bindinfo.dwReserved = %d\n", bindinfo.dwReserved);
653 
654     if(only_check_prot_args)
655         return E_FAIL;
656 
657     switch(test_protocol) {
658     case MK_TEST:
659         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
660                 BINDSTATUS_DIRECTBIND, NULL);
661         ok(hres == S_OK,
662            "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
663 
664     case FILE_TEST:
665     case ITS_TEST:
666         if(bind_to_object)
667             SET_EXPECT(Obj_OnProgress_SENDINGREQUEST);
668         else
669             SET_EXPECT(OnProgress_SENDINGREQUEST);
670         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
671                 BINDSTATUS_SENDINGREQUEST, emptyW);
672         ok(hres == S_OK,
673            "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
674         if(bind_to_object)
675             CHECK_CALLED(Obj_OnProgress_SENDINGREQUEST);
676         else
677             CHECK_CALLED(OnProgress_SENDINGREQUEST);
678     case WINETEST_SYNC_TEST:
679         IInternetProtocolSink_AddRef(pOIProtSink);
680         protocol_sink = pOIProtSink;
681     default:
682         break;
683     }
684 
685     if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST) {
686         IServiceProvider *service_provider;
687         IHttpNegotiate *http_negotiate;
688         IHttpNegotiate2 *http_negotiate2;
689         IHttpSecurity *http_security;
690         LPWSTR ua = (LPWSTR)0xdeadbeef, accept_mimes[256];
691         LPWSTR additional_headers = (LPWSTR)0xdeadbeef;
692         BYTE sec_id[100];
693         DWORD fetched = 256, size = 100;
694         DWORD tid;
695 
696         static const WCHAR wszMimes[] = {'*','/','*',0};
697 
698         SET_EXPECT(QueryService_IInternetBindInfo);
699         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT,
700                                                &ua, 1, &fetched);
701         CLEAR_CALLED(QueryService_IInternetBindInfo); /* IE <8 */
702 
703         ok(hres == E_NOINTERFACE,
704            "GetBindString(BINDSTRING_USER_AGETNT) failed: %08x\n", hres);
705         ok(fetched == 256, "fetched = %d, expected 254\n", fetched);
706         ok(ua == (LPWSTR)0xdeadbeef, "ua =  %p\n", ua);
707 
708         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
709                                                accept_mimes, 256, &fetched);
710         ok(hres == S_OK,
711            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
712         ok(fetched == 1, "fetched = %d, expected 1\n", fetched);
713         ok(!lstrcmpW(wszMimes, accept_mimes[0]), "unexpected mimes\n");
714         CoTaskMemFree(accept_mimes[0]);
715 
716         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
717                                                NULL, 256, &fetched);
718         ok(hres == E_INVALIDARG,
719            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
720 
721         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
722                                                accept_mimes, 256, NULL);
723         ok(hres == E_INVALIDARG,
724            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
725 
726         hres = IInternetBindInfo_QueryInterface(pOIBindInfo, &IID_IServiceProvider,
727                                                 (void**)&service_provider);
728         ok(hres == S_OK, "QueryInterface failed: %08x\n", hres);
729 
730         SET_EXPECT(QueryInterface_IHttpNegotiate);
731         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
732                 &IID_IHttpNegotiate, (void**)&http_negotiate);
733         CLEAR_CALLED(QueryInterface_IHttpNegotiate); /* IE <8 */
734         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
735 
736         if(!no_callback) {
737             SET_EXPECT(BeginningTransaction);
738             SET_EXPECT(QueryInterface_IHttpNegotiate);
739         }
740         hres = IHttpNegotiate_BeginningTransaction(http_negotiate, current_url,
741                                                    NULL, 0, &additional_headers);
742         if(!no_callback) {
743             CHECK_CALLED_BROKEN(QueryInterface_IHttpNegotiate);
744             CHECK_CALLED(BeginningTransaction);
745         }
746         IHttpNegotiate_Release(http_negotiate);
747         ok(hres == S_OK, "BeginningTransction failed: %08x\n", hres);
748         ok(additional_headers == NULL, "additional_headers=%p\n", additional_headers);
749 
750         SET_EXPECT(QueryInterface_IHttpNegotiate2);
751         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
752                 &IID_IHttpNegotiate2, (void**)&http_negotiate2);
753         CLEAR_CALLED(QueryInterface_IHttpNegotiate2); /* IE <8 */
754         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
755 
756         size = 512;
757         if(!no_callback) {
758             SET_EXPECT(QueryInterface_IHttpNegotiate2);
759             SET_EXPECT(GetRootSecurityId);
760         }
761         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, sec_id, &size, 0);
762         if(!no_callback) {
763             CHECK_CALLED_BROKEN(QueryInterface_IHttpNegotiate2);
764             CHECK_CALLED(GetRootSecurityId);
765         }
766         IHttpNegotiate2_Release(http_negotiate2);
767         ok(hres == E_FAIL, "GetRootSecurityId failed: %08x, expected E_FAIL\n", hres);
768         ok(size == (no_callback ? 512 : 13), "size=%d\n", size);
769 
770         if(!no_callback) {
771             SET_EXPECT(QueryService_IHttpSecurity);
772             SET_EXPECT(QueryInterface_IHttpSecurity);
773         }
774         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpSecurity,
775                 &IID_IHttpSecurity, (void**)&http_security);
776         ok(hres == (no_callback ? E_NOINTERFACE : S_OK), "QueryService failed: 0x%08x\n", hres);
777         if(!no_callback) {
778             CHECK_CALLED(QueryService_IHttpSecurity);
779             CHECK_CALLED(QueryInterface_IHttpSecurity);
780         }
781 
782         IServiceProvider_Release(service_provider);
783 
784         IInternetProtocolSink_AddRef(pOIProtSink);
785         protocol_sink = pOIProtSink;
786 
787         if(async_switch) {
788             PROTOCOLDATA data;
789 
790             memset(&data, 0, sizeof(data));
791             data.grfFlags = PI_FORCE_ASYNC;
792             prot_state = 0;
793             hres = IInternetProtocolSink_Switch(pOIProtSink, &data);
794             ok(hres == S_OK, "Switch failed: %08x\n", hres);
795             SET_EXPECT(Continue);
796             SetEvent(complete_event2);
797             return E_PENDING;
798         } else {
799             CreateThread(NULL, 0, thread_proc, NULL, 0, &tid);
800             return S_OK;
801         }
802     }
803 
804     if(test_protocol == FILE_TEST) {
805         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
806                 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_url+7);
807         ok(hres == S_OK,
808            "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
809 
810         if(bind_to_object)
811             SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
812         else
813             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
814         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
815                 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE, wszTextHtml);
816         ok(hres == S_OK,
817            "ReportProgress(BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE) failed: %08x\n", hres);
818         if(bind_to_object)
819             CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
820         else
821             CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
822     }else if(test_protocol == WINETEST_SYNC_TEST) {
823         SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
824         hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE, wszTextHtml);
825         ok(hres == S_OK,
826            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
827         CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
828     }else {
829         hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
830         ok(hres == S_OK,
831            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
832     }
833 
834     if(test_protocol == ABOUT_TEST)
835         bscf |= BSCF_DATAFULLYAVAILABLE;
836     if(test_protocol == ITS_TEST)
837         bscf = BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE;
838 
839     SET_EXPECT(Read);
840     if(bind_to_object) {
841         if(test_protocol != FILE_TEST && test_protocol != MK_TEST && test_protocol != WINETEST_SYNC_TEST)
842             SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
843         SET_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
844         if(test_protocol == FILE_TEST)
845             SET_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
846         SET_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
847         SET_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
848         SET_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
849         SET_EXPECT(CreateInstance);
850         SET_EXPECT(PutProperty_MIMETYPEPROP);
851         SET_EXPECT(PutProperty_CLASSIDPROP);
852         SET_EXPECT(Load);
853         SET_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
854         SET_EXPECT(OnObjectAvailable);
855         SET_EXPECT(Obj_OnStopBinding);
856     }else {
857         if(test_protocol != FILE_TEST && test_protocol != MK_TEST)
858             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
859         SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
860         if(test_protocol == FILE_TEST)
861             SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
862         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
863         SET_EXPECT(LockRequest);
864         if(!filedwl_api)
865             SET_EXPECT(OnDataAvailable);
866         if(test_protocol != WINETEST_SYNC_TEST) /* Set in Read after ReportResult call */
867             SET_EXPECT(OnStopBinding);
868     }
869 
870     hres = IInternetProtocolSink_ReportData(pOIProtSink, bscf, 13, 13);
871     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
872 
873     CHECK_CALLED(Read);
874     if(bind_to_object) {
875         if(test_protocol != FILE_TEST && test_protocol != MK_TEST)
876             CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
877         CHECK_CALLED(Obj_OnProgress_BEGINDOWNLOADDATA);
878         if(test_protocol == FILE_TEST)
879             CHECK_CALLED(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
880         CHECK_CALLED(Obj_OnProgress_ENDDOWNLOADDATA);
881         CHECK_CALLED(Obj_OnProgress_CLASSIDAVAILABLE);
882         CHECK_CALLED(Obj_OnProgress_BEGINSYNCOPERATION);
883         CHECK_CALLED(CreateInstance);
884         CHECK_CALLED(PutProperty_MIMETYPEPROP);
885         CHECK_CALLED_BROKEN(PutProperty_CLASSIDPROP);
886         CHECK_CALLED(Load);
887         CHECK_CALLED(Obj_OnProgress_ENDSYNCOPERATION);
888         CHECK_CALLED(OnObjectAvailable);
889         CHECK_CALLED(Obj_OnStopBinding);
890     }else {
891         if(test_protocol != FILE_TEST && test_protocol != MK_TEST && test_protocol != WINETEST_SYNC_TEST)
892             CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
893         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
894         if(test_protocol == FILE_TEST)
895             CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
896         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
897         CHECK_CALLED(LockRequest);
898         if(!filedwl_api)
899             CHECK_CALLED(OnDataAvailable);
900         CHECK_CALLED(OnStopBinding);
901     }
902 
903     if(test_protocol == ITS_TEST) {
904         SET_EXPECT(Read);
905         hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
906         ok(hres == S_OK, "ReportProgress(BINDSTATUS_BEGINDOWNLOADDATA) failed: %08x\n", hres);
907         CHECK_CALLED(Read);
908     }else if(!bind_to_object && test_protocol == FILE_TEST) {
909         SET_EXPECT(Read);
910         hres = IInternetProtocolSink_ReportData(pOIProtSink, bscf, 13, 13);
911         ok(hres == S_OK, "ReportData failed: %08x\n", hres);
912         CHECK_CALLED(Read);
913     }
914 
915     if(test_protocol != WINETEST_SYNC_TEST) {
916         SET_EXPECT(Terminate);
917         hres = IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
918         ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
919         CHECK_CALLED(Terminate);
920     }
921 
922     return S_OK;
923 }
924 
925 static HRESULT WINAPI Protocol_Continue(IInternetProtocol *iface,
926         PROTOCOLDATA *pProtocolData)
927 {
928     DWORD bscf = 0;
929     HRESULT hres;
930 
931     CHECK_EXPECT(Continue);
932 
933     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
934 
935     if(!bind_to_object)
936         ok(reported_url && !lstrcmpW(reported_url, current_url), "wrong url %s\n", wine_dbgstr_w(reported_url));
937 
938     ok(pProtocolData != NULL, "pProtocolData == NULL\n");
939     if(!pProtocolData)
940         return S_OK;
941 
942     switch(prot_state) {
943     case 0:
944         hres = IInternetProtocolSink_ReportProgress(protocol_sink,
945                     BINDSTATUS_SENDINGREQUEST, NULL);
946         ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
947 
948         hres = IInternetProtocolSink_ReportProgress(protocol_sink,
949                 BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
950         ok(hres == S_OK,
951                 "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
952 
953         bscf |= BSCF_FIRSTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION;
954         break;
955     case 1: {
956         IServiceProvider *service_provider;
957         IHttpNegotiate *http_negotiate;
958         static const WCHAR header[] = {'?',0};
959 
960         hres = IInternetProtocolSink_QueryInterface(protocol_sink, &IID_IServiceProvider,
961                                                     (void**)&service_provider);
962         ok(hres == S_OK, "Could not get IServiceProvicder\n");
963 
964         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
965                                              &IID_IHttpNegotiate, (void**)&http_negotiate);
966         ok(hres == S_OK, "Could not get IHttpNegotiate\n");
967 
968         if(!no_callback) {
969             SET_EXPECT(QueryInterface_IHttpNegotiate);
970             SET_EXPECT(OnResponse);
971         }
972         hres = IHttpNegotiate_OnResponse(http_negotiate, 200, header, NULL, NULL);
973         if(!no_callback) {
974             CHECK_CALLED_BROKEN(QueryInterface_IHttpNegotiate);
975             CHECK_CALLED(OnResponse);
976         }
977         IHttpNegotiate_Release(http_negotiate);
978         ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
979 
980         if(test_protocol == HTTPS_TEST || test_redirect) {
981             hres = IInternetProtocolSink_ReportProgress(protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
982             ok(hres == S_OK, "ReportProgress(BINDSTATUS_ACCEPTRANGES) failed: %08x\n", hres);
983         }
984 
985         hres = IInternetProtocolSink_ReportProgress(protocol_sink,
986                 BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
987         ok(hres == S_OK,
988            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
989 
990         hres = IInternetProtocolSink_ReportProgress(protocol_sink,
991             BINDSTATUS_CACHEFILENAMEAVAILABLE, use_cache_file ? cache_file_name : cache_fileW);
992         ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
993 
994         bscf |= BSCF_FIRSTDATANOTIFICATION;
995         break;
996     }
997     case 2:
998     case 3:
999         bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
1000         break;
1001     }
1002 
1003     hres = IInternetProtocolSink_ReportData(protocol_sink, bscf, 100, 400);
1004     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1005 
1006     if(prot_state != 2 || !test_abort)
1007         SET_EXPECT(Read);
1008     switch(prot_state) {
1009     case 0:
1010         hres = IInternetProtocolSink_ReportResult(protocol_sink, S_OK, 0, NULL);
1011         ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1012         SET_EXPECT(OnProgress_SENDINGREQUEST);
1013         SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
1014         SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
1015         SET_EXPECT(LockRequest);
1016         SET_EXPECT(OnStopBinding);
1017         break;
1018     case 1:
1019         if(bind_to_object) {
1020             SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
1021             SET_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
1022             SET_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
1023             SET_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
1024             SET_EXPECT(CreateInstance);
1025             SET_EXPECT(PutProperty_MIMETYPEPROP);
1026             SET_EXPECT(PutProperty_CLASSIDPROP);
1027             SET_EXPECT(Load);
1028             SET_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
1029             SET_EXPECT(OnObjectAvailable);
1030             SET_EXPECT(Obj_OnStopBinding);
1031         }else if(!no_callback) {
1032             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
1033             SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
1034             SET_EXPECT(LockRequest);
1035         }else {
1036             SET_EXPECT(LockRequest);
1037         }
1038         break;
1039     case 2:
1040         if(!no_callback)
1041             SET_EXPECT(OnProgress_DOWNLOADINGDATA);
1042         break;
1043     case 3:
1044         SET_EXPECT(OnProgress_DOWNLOADINGDATA);
1045         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
1046     }
1047     if(!no_callback) {
1048         if((!bind_to_object || prot_state >= 2) && (!test_abort || prot_state != 2))
1049             SET_EXPECT(OnDataAvailable);
1050         if(prot_state == 3 || (test_abort && prot_state == 2))
1051             SET_EXPECT(OnStopBinding);
1052     }
1053     return S_OK;
1054 }
1055 
1056 static HRESULT WINAPI Protocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
1057         DWORD dwOptions)
1058 {
1059     HRESULT hres;
1060 
1061     CHECK_EXPECT(Abort);
1062 
1063     ok(hrReason == E_ABORT, "hrReason = %08x\n", hrReason);
1064     ok(!dwOptions, "dwOptions = %x\n", dwOptions);
1065 
1066     hres = IInternetProtocolSink_ReportResult(protocol_sink, E_ABORT, ERROR_SUCCESS, NULL);
1067     ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1068 
1069     return S_OK;
1070 }
1071 
1072 static HRESULT WINAPI Protocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
1073 {
1074     CHECK_EXPECT(Terminate);
1075 
1076     ok(dwOptions == 0, "dwOptions=%d, expected 0\n", dwOptions);
1077 
1078     if(protocol_sink) {
1079         IInternetProtocolSink_Release(protocol_sink);
1080         protocol_sink = NULL;
1081     }
1082 
1083     if(no_callback)
1084         SetEvent(complete_event);
1085     return S_OK;
1086 }
1087 
1088 static HRESULT WINAPI Protocol_Suspend(IInternetProtocol *iface)
1089 {
1090     ok(0, "unexpected call\n");
1091     return E_NOTIMPL;
1092 }
1093 
1094 static HRESULT WINAPI Protocol_Resume(IInternetProtocol *iface)
1095 {
1096     ok(0, "unexpected call\n");
1097     return E_NOTIMPL;
1098 }
1099 
1100 static HRESULT WINAPI Protocol_Read(IInternetProtocol *iface, void *pv,
1101         ULONG cb, ULONG *pcbRead)
1102 {
1103     HRESULT hres;
1104 
1105     static const char data[] = "<HTML></HTML>";
1106 
1107     CHECK_EXPECT2(Read);
1108 
1109     ok(pv != NULL, "pv == NULL\n");
1110     ok(cb != 0, "cb == 0\n");
1111     ok(pcbRead != NULL, "pcbRead == NULL\n");
1112 
1113     if(async_switch) {
1114         if(prot_state++ > 1) {
1115             *pcbRead = 0;
1116             return S_FALSE;
1117         } else {
1118             memset(pv, '?', cb);
1119             *pcbRead = cb;
1120             return S_OK;
1121         }
1122     }
1123 
1124     if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST) {
1125         static BOOL pending = TRUE;
1126 
1127         pending = !pending;
1128 
1129         switch(prot_state) {
1130         case 1:
1131         case 2:
1132             if(pending) {
1133                 *pcbRead = 10;
1134                 memset(pv, '?', 10);
1135                 if(prot_state == 2 && no_callback)
1136                     SetEvent(complete_event);
1137                 return E_PENDING;
1138             }else {
1139                 memset(pv, '?', cb);
1140                 *pcbRead = cb;
1141                 read++;
1142                 return S_OK;
1143             }
1144         case 3:
1145             prot_state++;
1146 
1147             *pcbRead = 0;
1148 
1149             hres = IInternetProtocolSink_ReportData(protocol_sink,
1150                     BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION, 2000, 2000);
1151             ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1152 
1153             hres = IInternetProtocolSink_ReportResult(protocol_sink, S_OK, 0, NULL);
1154             ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1155 
1156             return S_FALSE;
1157         case 4:
1158             *pcbRead = 0;
1159             return S_FALSE;
1160         }
1161     }
1162 
1163     if(read) {
1164         *pcbRead = 0;
1165         return S_FALSE;
1166     }
1167 
1168     if(test_protocol == WINETEST_SYNC_TEST) {
1169         hres = IInternetProtocolSink_ReportResult(protocol_sink, S_OK, 0, NULL);
1170         ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1171 
1172         SET_EXPECT(OnStopBinding);
1173     }
1174 
1175     ok(*pcbRead == 0, "*pcbRead=%d, expected 0\n", *pcbRead);
1176     read += *pcbRead = sizeof(data)-1;
1177     memcpy(pv, data, sizeof(data));
1178     return S_OK;
1179 }
1180 
1181 static HRESULT WINAPI Protocol_Seek(IInternetProtocol *iface,
1182         LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
1183 {
1184     ok(0, "unexpected call\n");
1185     return E_NOTIMPL;
1186 }
1187 
1188 static HRESULT WINAPI Protocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
1189 {
1190     CHECK_EXPECT(LockRequest);
1191     if(no_callback)
1192         SetEvent(complete_event);
1193     return S_OK;
1194 }
1195 
1196 static HRESULT WINAPI Protocol_UnlockRequest(IInternetProtocol *iface)
1197 {
1198     CHECK_EXPECT(UnlockRequest);
1199     return S_OK;
1200 }
1201 
1202 static const IInternetProtocolVtbl ProtocolVtbl = {
1203     Protocol_QueryInterface,
1204     Protocol_AddRef,
1205     Protocol_Release,
1206     Protocol_Start,
1207     Protocol_Continue,
1208     Protocol_Abort,
1209     Protocol_Terminate,
1210     Protocol_Suspend,
1211     Protocol_Resume,
1212     Protocol_Read,
1213     Protocol_Seek,
1214     Protocol_LockRequest,
1215     Protocol_UnlockRequest
1216 };
1217 
1218 static IInternetProtocol Protocol = { &ProtocolVtbl };
1219 
1220 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface, REFIID riid, void **ppv)
1221 {
1222     if(IsEqualGUID(&IID_IUnknown, riid)
1223             || IsEqualGUID(&IID_IHttpNegotiate, riid)
1224             || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
1225         *ppv = iface;
1226         return S_OK;
1227     }
1228 
1229     ok(0, "unexpected call\n");
1230     return E_NOINTERFACE;
1231 }
1232 
1233 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
1234 {
1235     return 2;
1236 }
1237 
1238 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
1239 {
1240     return 1;
1241 }
1242 
1243 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface, LPCWSTR szURL,
1244         LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
1245 {
1246     if(onsecurityproblem_hres == S_OK)
1247         CHECK_EXPECT2(BeginningTransaction);
1248     else
1249         CHECK_EXPECT(BeginningTransaction);
1250 
1251     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1252 
1253     ok(!lstrcmpW(szURL, current_url), "szURL != current_url\n");
1254     ok(!dwReserved, "dwReserved=%d, expected 0\n", dwReserved);
1255     ok(pszAdditionalHeaders != NULL, "pszAdditionalHeaders == NULL\n");
1256     if(pszAdditionalHeaders)
1257         ok(*pszAdditionalHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
1258 
1259     return S_OK;
1260 }
1261 
1262 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
1263         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
1264 {
1265     CHECK_EXPECT(OnResponse);
1266 
1267     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1268 
1269     ok(dwResponseCode == 200, "dwResponseCode=%d, expected 200\n", dwResponseCode);
1270     ok(szResponseHeaders != NULL, "szResponseHeaders == NULL\n");
1271     ok(szRequestHeaders == NULL, "szRequestHeaders != NULL\n");
1272     /* Note: in protocol.c tests, OnResponse pszAdditionalRequestHeaders _is_ NULL */
1273     ok(pszAdditionalRequestHeaders != NULL, "pszAdditionalHeaders == NULL\n");
1274     if(pszAdditionalRequestHeaders)
1275         ok(*pszAdditionalRequestHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
1276 
1277     return S_OK;
1278 }
1279 
1280 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
1281         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
1282 {
1283     static const BYTE sec_id[] = {'h','t','t','p',':','t','e','s','t',1,0,0,0};
1284 
1285     CHECK_EXPECT(GetRootSecurityId);
1286 
1287     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1288 
1289     ok(!dwReserved, "dwReserved=%ld, expected 0\n", dwReserved);
1290     ok(pbSecurityId != NULL, "pbSecurityId == NULL\n");
1291     ok(pcbSecurityId != NULL, "pcbSecurityId == NULL\n");
1292 
1293     if(pbSecurityId == (void*)0xdeadbeef)
1294         return E_NOTIMPL;
1295 
1296     if(pcbSecurityId) {
1297         ok(*pcbSecurityId == 512, "*pcbSecurityId=%d, expected 512\n", *pcbSecurityId);
1298         *pcbSecurityId = sizeof(sec_id);
1299     }
1300 
1301     if(pbSecurityId)
1302         memcpy(pbSecurityId, sec_id, sizeof(sec_id));
1303 
1304     return E_FAIL;
1305 }
1306 
1307 static IHttpNegotiate2Vtbl HttpNegotiateVtbl = {
1308     HttpNegotiate_QueryInterface,
1309     HttpNegotiate_AddRef,
1310     HttpNegotiate_Release,
1311     HttpNegotiate_BeginningTransaction,
1312     HttpNegotiate_OnResponse,
1313     HttpNegotiate_GetRootSecurityId
1314 };
1315 
1316 static IHttpNegotiate2 HttpNegotiate = { &HttpNegotiateVtbl };
1317 
1318 static HRESULT WINAPI HttpSecurity_QueryInterface(IHttpSecurity *iface, REFIID riid, void **ppv)
1319 {
1320     ok(0, "Unexpected call\n");
1321     *ppv = NULL;
1322     if(IsEqualGUID(&IID_IHttpSecurity, riid) ||
1323        IsEqualGUID(&IID_IWindowForBindingUI, riid) ||
1324        IsEqualGUID(&IID_IUnknown, riid))
1325     {
1326         *ppv = iface;
1327         return S_OK;
1328     }
1329 
1330     ok(0, "Unexpected interface requested.\n");
1331 
1332     return E_NOINTERFACE;
1333 }
1334 
1335 static ULONG WINAPI HttpSecurity_AddRef(IHttpSecurity *iface)
1336 {
1337     return 2;
1338 }
1339 
1340 static ULONG WINAPI HttpSecurity_Release(IHttpSecurity *iface)
1341 {
1342     return 1;
1343 }
1344 
1345 static HRESULT WINAPI HttpSecurity_GetWindow(IHttpSecurity *iface, REFGUID rguidReason, HWND *phwnd)
1346 {
1347     if(IsEqualGUID(rguidReason, &IID_IHttpSecurity))
1348         CHECK_EXPECT(GetWindow_IHttpSecurity);
1349     else if(IsEqualGUID(rguidReason, &IID_IWindowForBindingUI))
1350         CHECK_EXPECT2(GetWindow_IWindowForBindingUI);
1351     else if(IsEqualGUID(rguidReason, &IID_ICodeInstall))
1352         CHECK_EXPECT(GetWindow_ICodeInstall);
1353     else
1354         ok(0, "Unexpected rguidReason: %s\n", wine_dbgstr_guid(rguidReason));
1355 
1356     *phwnd = NULL;
1357     return S_OK;
1358 }
1359 
1360 static HRESULT WINAPI HttpSecurity_OnSecurityProblem(IHttpSecurity *iface, DWORD dwProblem)
1361 {
1362     CHECK_EXPECT(OnSecurityProblem);
1363     if(!security_problem) {
1364         ok(dwProblem == ERROR_INTERNET_SEC_CERT_CN_INVALID ||
1365            broken(dwProblem == ERROR_INTERNET_SEC_CERT_ERRORS) /* Some versions of IE6 */,
1366            "Got problem: %d\n", dwProblem);
1367         security_problem = dwProblem;
1368 
1369         if(dwProblem == ERROR_INTERNET_SEC_CERT_ERRORS)
1370             binding_hres = INET_E_SECURITY_PROBLEM;
1371     }else
1372         ok(dwProblem == security_problem, "Got problem: %d\n", dwProblem);
1373 
1374     return onsecurityproblem_hres;
1375 }
1376 
1377 static const IHttpSecurityVtbl HttpSecurityVtbl = {
1378     HttpSecurity_QueryInterface,
1379     HttpSecurity_AddRef,
1380     HttpSecurity_Release,
1381     HttpSecurity_GetWindow,
1382     HttpSecurity_OnSecurityProblem
1383 };
1384 
1385 static IHttpSecurity HttpSecurity = { &HttpSecurityVtbl };
1386 
1387 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
1388 {
1389     ok(0, "unexpected call\n");
1390     return E_NOINTERFACE;
1391 }
1392 
1393 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1394 {
1395     return 2;
1396 }
1397 
1398 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1399 {
1400     return 1;
1401 }
1402 
1403 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1404         REFGUID guidService, REFIID riid, void **ppv)
1405 {
1406     if(IsEqualGUID(&IID_IAuthenticate, guidService)) {
1407         CHECK_EXPECT(QueryService_IAuthenticate);
1408         return E_NOTIMPL;
1409     }
1410 
1411     if(IsEqualGUID(&IID_IInternetProtocol, guidService)) {
1412         CHECK_EXPECT2(QueryService_IInternetProtocol);
1413         return E_NOTIMPL;
1414     }
1415 
1416     if(IsEqualGUID(&IID_IInternetBindInfo, guidService)) {
1417         CHECK_EXPECT(QueryService_IInternetBindInfo);
1418         return E_NOTIMPL;
1419     }
1420 
1421     if(IsEqualGUID(&IID_IWindowForBindingUI, guidService)) {
1422         CHECK_EXPECT2(QueryService_IWindowForBindingUI);
1423         *ppv = &HttpSecurity;
1424         return S_OK;
1425     }
1426 
1427     if(IsEqualGUID(&IID_IHttpSecurity, guidService)) {
1428         CHECK_EXPECT(QueryService_IHttpSecurity);
1429         *ppv = &HttpSecurity;
1430         return S_OK;
1431     }
1432 
1433     if(IsEqualGUID(&IID_IGetBindHandle, guidService)) {
1434         trace("QueryService(IID_IGetBindHandle)\n");
1435         *ppv = NULL;
1436         return E_NOINTERFACE;
1437     }
1438 
1439     if(IsEqualGUID(&IID_undocumentedIE11, guidService)) {
1440         trace("QueryService(IID_undocumentedIE11)\n");
1441         *ppv = NULL;
1442         return E_NOINTERFACE;
1443     }
1444 
1445     ok(0, "unexpected service %s\n", wine_dbgstr_guid(guidService));
1446     return E_NOINTERFACE;
1447 }
1448 
1449 static IServiceProviderVtbl ServiceProviderVtbl = {
1450     ServiceProvider_QueryInterface,
1451     ServiceProvider_AddRef,
1452     ServiceProvider_Release,
1453     ServiceProvider_QueryService
1454 };
1455 
1456 static IServiceProvider ServiceProvider = { &ServiceProviderVtbl };
1457 
1458 static IBindStatusCallbackEx objbsc;
1459 
1460 static void test_WinInetHttpInfo(IWinInetHttpInfo *http_info, DWORD progress)
1461 {
1462     DWORD status, size;
1463     HRESULT hres, expect;
1464 
1465     /* QueryInfo changes its behavior during this request */
1466     if(progress == BINDSTATUS_SENDINGREQUEST)
1467         return;
1468 
1469     if(test_protocol==FTP_TEST && download_state==BEFORE_DOWNLOAD
1470             && progress!=BINDSTATUS_MIMETYPEAVAILABLE)
1471         expect = E_FAIL;
1472     else if(test_protocol == FTP_TEST)
1473         expect = S_FALSE;
1474     else
1475         expect = S_OK;
1476 
1477     size = sizeof(DWORD);
1478     hres = IWinInetHttpInfo_QueryInfo(http_info, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
1479             &status, &size, NULL, NULL);
1480     ok(hres == expect || ((progress == BINDSTATUS_COOKIE_SENT || progress == BINDSTATUS_PROXYDETECTING) && hres == S_FALSE),
1481        "progress %u: hres = %x, expected %x\n", progress, hres, expect);
1482     if(hres == S_OK) {
1483         if(download_state == BEFORE_DOWNLOAD && progress != BINDSTATUS_MIMETYPEAVAILABLE && progress != BINDSTATUS_DECODING)
1484             ok(status == 0, "progress %u: status = %d\n", progress, status);
1485         else
1486             ok(status == HTTP_STATUS_OK, "progress %u: status = %d\n", progress, status);
1487         ok(size == sizeof(DWORD), "size = %d\n", size);
1488     }
1489 
1490     size = sizeof(DWORD);
1491     hres = IWinInetHttpInfo_QueryOption(http_info, INTERNET_OPTION_HANDLE_TYPE, &status, &size);
1492     if(test_protocol == FTP_TEST) {
1493         if(download_state==BEFORE_DOWNLOAD && progress!=BINDSTATUS_MIMETYPEAVAILABLE)
1494             ok(hres == E_FAIL, "hres = %x\n", hres);
1495         else
1496             ok(hres == S_OK, "hres = %x\n", hres);
1497 
1498         if(hres == S_OK)
1499             ok(status == INTERNET_HANDLE_TYPE_FTP_FILE, "status = %d\n", status);
1500     } else {
1501         ok(hres == S_OK, "hres = %x\n", hres);
1502         ok(status == INTERNET_HANDLE_TYPE_HTTP_REQUEST, "status = %d\n", status);
1503     }
1504 }
1505 
1506 static HRESULT WINAPI statusclb_QueryInterface(IBindStatusCallbackEx *iface, REFIID riid, void **ppv)
1507 {
1508     static const IID IID_undocumentedIE10 = {0xf286fa56,0xc1fd,0x4270,{0x8e,0x67,0xb3,0xeb,0x79,0x0a,0x81,0xe8}};
1509 
1510     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1511 
1512     if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
1513         CHECK_EXPECT2(QueryInterface_IInternetProtocol);
1514         if(emulate_protocol) {
1515             *ppv = &Protocol;
1516             return S_OK;
1517         }else {
1518             return E_NOINTERFACE;
1519         }
1520     }else if (IsEqualGUID(&IID_IServiceProvider, riid)) {
1521         CHECK_EXPECT2(QueryInterface_IServiceProvider);
1522         *ppv = &ServiceProvider;
1523         return S_OK;
1524     }else if (IsEqualGUID(&IID_IHttpNegotiate, riid)) {
1525         CHECK_EXPECT2(QueryInterface_IHttpNegotiate);
1526         *ppv = &HttpNegotiate;
1527         return S_OK;
1528     }else if (IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
1529         CHECK_EXPECT(QueryInterface_IHttpNegotiate2);
1530         *ppv = &HttpNegotiate;
1531         return S_OK;
1532     }else if (IsEqualGUID(&IID_IAuthenticate, riid)) {
1533         CHECK_EXPECT(QueryInterface_IAuthenticate);
1534         return E_NOINTERFACE;
1535     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
1536         if(strict_bsc_qi)
1537             CHECK_EXPECT2(QueryInterface_IBindStatusCallback);
1538         *ppv = iface;
1539         return S_OK;
1540     }else if(IsEqualGUID(&IID_IBindStatusCallbackHolder, riid)) {
1541         CHECK_EXPECT2(QueryInterface_IBindStatusCallbackHolder);
1542         return E_NOINTERFACE;
1543     }else if(IsEqualGUID(&IID_IBindStatusCallbackEx, riid)) {
1544         CHECK_EXPECT(QueryInterface_IBindStatusCallbackEx);
1545         if(!use_bscex)
1546             return E_NOINTERFACE;
1547         *ppv = iface;
1548         return S_OK;
1549     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
1550         /* TODO */
1551     }else if(IsEqualGUID(&IID_IWindowForBindingUI, riid)) {
1552         CHECK_EXPECT2(QueryInterface_IWindowForBindingUI);
1553         return E_NOINTERFACE;
1554     }else if(IsEqualGUID(&IID_IHttpSecurity, riid)) {
1555         CHECK_EXPECT2(QueryInterface_IHttpSecurity);
1556         return E_NOINTERFACE;
1557     }else if(IsEqualGUID(&IID_IGetBindHandle, riid)) {
1558         trace("QI(IID_IGetBindHandle)\n");
1559         *ppv = NULL;
1560         return E_NOINTERFACE;
1561     }else if(IsEqualGUID(&IID_undocumentedIE10, riid)) {
1562         trace("QI(IID_undocumentedIE10)\n");
1563         *ppv = NULL;
1564         return E_NOINTERFACE;
1565     }else if(IsEqualGUID(&IID_undocumentedIE11, riid)) {
1566         trace("QI(IID_undocumentedIE11)\n");
1567         *ppv = NULL;
1568         return E_NOINTERFACE;
1569     }else {
1570         ok(0, "unexpected interface %s\n", wine_dbgstr_guid(riid));
1571     }
1572 
1573     return E_NOINTERFACE;
1574 }
1575 
1576 static ULONG WINAPI statusclb_AddRef(IBindStatusCallbackEx *iface)
1577 {
1578     return 2;
1579 }
1580 
1581 static ULONG WINAPI statusclb_Release(IBindStatusCallbackEx *iface)
1582 {
1583     return 1;
1584 }
1585 
1586 static HRESULT WINAPI statusclb_OnStartBinding(IBindStatusCallbackEx *iface, DWORD dwReserved,
1587         IBinding *pib)
1588 {
1589     IWinInetHttpInfo *http_info;
1590     HRESULT hres;
1591     IMoniker *mon;
1592     DWORD res;
1593     CLSID clsid;
1594     LPOLESTR res_str;
1595 
1596     if(iface == &objbsc)
1597         CHECK_EXPECT(Obj_OnStartBinding);
1598     else
1599         CHECK_EXPECT(OnStartBinding);
1600 
1601     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1602 
1603     ok(pib != NULL, "pib should not be NULL\n");
1604     ok(dwReserved == 0xff, "dwReserved=%x\n", dwReserved);
1605 
1606     if(pib == (void*)0xdeadbeef)
1607         return S_OK;
1608 
1609     current_binding = pib;
1610 
1611     hres = IBinding_QueryInterface(pib, &IID_IMoniker, (void**)&mon);
1612     ok(hres == E_NOINTERFACE, "IBinding should not have IMoniker interface\n");
1613     if(SUCCEEDED(hres))
1614         IMoniker_Release(mon);
1615 
1616     hres = IBinding_QueryInterface(pib, &IID_IWinInetHttpInfo, (void**)&http_info);
1617     ok(hres == E_NOINTERFACE, "Could not get IID_IWinInetHttpInfo: %08x\n", hres);
1618 
1619     if(0) { /* crashes with native urlmon */
1620         hres = IBinding_GetBindResult(pib, NULL, &res, &res_str, NULL);
1621         ok(hres == E_INVALIDARG, "GetBindResult failed: %08x\n", hres);
1622     }
1623     hres = IBinding_GetBindResult(pib, &clsid, NULL, &res_str, NULL);
1624     ok(hres == E_INVALIDARG, "GetBindResult failed: %08x\n", hres);
1625     hres = IBinding_GetBindResult(pib, &clsid, &res, NULL, NULL);
1626     ok(hres == E_INVALIDARG, "GetBindResult failed: %08x\n", hres);
1627     hres = IBinding_GetBindResult(pib, &clsid, &res, &res_str, (void*)0xdeadbeef);
1628     ok(hres == E_INVALIDARG, "GetBindResult failed: %08x\n", hres);
1629 
1630     hres = IBinding_GetBindResult(pib, &clsid, &res, &res_str, NULL);
1631     ok(hres == S_OK, "GetBindResult failed: %08x, expected S_OK\n", hres);
1632     ok(IsEqualCLSID(&clsid, &CLSID_NULL), "incorrect clsid: %s\n", wine_dbgstr_guid(&clsid));
1633     ok(!res, "incorrect res: %x\n", res);
1634     ok(!res_str, "incorrect res_str: %s\n", wine_dbgstr_w(res_str));
1635 
1636     if(abort_start) {
1637         binding_hres = abort_hres;
1638         return abort_hres;
1639     }
1640 
1641     return S_OK;
1642 }
1643 
1644 static HRESULT WINAPI statusclb_GetPriority(IBindStatusCallbackEx *iface, LONG *pnPriority)
1645 {
1646     ok(0, "unexpected call\n");
1647     return E_NOTIMPL;
1648 }
1649 
1650 static HRESULT WINAPI statusclb_OnLowResource(IBindStatusCallbackEx *iface, DWORD reserved)
1651 {
1652     ok(0, "unexpected call\n");
1653     return E_NOTIMPL;
1654 }
1655 
1656 static HRESULT WINAPI statusclb_OnProgress(IBindStatusCallbackEx *iface, ULONG ulProgress,
1657         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
1658 {
1659     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1660 
1661     switch(ulStatusCode) {
1662     case BINDSTATUS_FINDINGRESOURCE:
1663         if(iface == &objbsc)
1664             CHECK_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
1665         else if(test_protocol == FTP_TEST)
1666             todo_wine CHECK_EXPECT(OnProgress_FINDINGRESOURCE);
1667         else if(test_protocol == HTTPS_TEST && !bindtest_flags)
1668             todo_wine CHECK_EXPECT(OnProgress_FINDINGRESOURCE);
1669         else
1670             CHECK_EXPECT(OnProgress_FINDINGRESOURCE);
1671         if(emulate_protocol && (test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST))
1672             SetEvent(complete_event);
1673         break;
1674     case BINDSTATUS_CONNECTING:
1675         if(iface == &objbsc)
1676             CHECK_EXPECT(Obj_OnProgress_CONNECTING);
1677         else if(test_protocol == FTP_TEST)
1678             todo_wine CHECK_EXPECT(OnProgress_CONNECTING);
1679         else if(onsecurityproblem_hres == S_OK)
1680             CHECK_EXPECT2(OnProgress_CONNECTING);
1681         else
1682             CHECK_EXPECT(OnProgress_CONNECTING);
1683         if(emulate_protocol && (test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST))
1684             SetEvent(complete_event);
1685         break;
1686     case BINDSTATUS_REDIRECTING:
1687         if(iface == &objbsc)
1688             CHECK_EXPECT(Obj_OnProgress_REDIRECTING);
1689         else
1690             CHECK_EXPECT(OnProgress_REDIRECTING);
1691         ok(!lstrcmpW(szStatusText, winetest_data_urlW), "unexpected status text %s\n",
1692            wine_dbgstr_w(szStatusText));
1693         if(emulate_protocol && (test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST)
1694                 && (!bind_to_object || iface == &objbsc))
1695             SetEvent(complete_event);
1696         break;
1697     case BINDSTATUS_SENDINGREQUEST:
1698         if(iface == &objbsc)
1699             CHECK_EXPECT(Obj_OnProgress_SENDINGREQUEST);
1700         else if(test_protocol == FTP_TEST)
1701             CHECK_EXPECT2(OnProgress_SENDINGREQUEST);
1702         else
1703             CHECK_EXPECT(OnProgress_SENDINGREQUEST);
1704         if(emulate_protocol && (test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST))
1705             SetEvent(complete_event);
1706 
1707         if(abort_progress) {
1708             if(filedwl_api)
1709                 binding_hres = E_ABORT;
1710             return E_ABORT;
1711         }
1712 
1713         break;
1714     case BINDSTATUS_MIMETYPEAVAILABLE:
1715         if(iface == &objbsc)
1716             CHECK_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
1717         else
1718             CHECK_EXPECT(OnProgress_MIMETYPEAVAILABLE);
1719         if(!bind_to_object)
1720             ok(download_state == BEFORE_DOWNLOAD, "Download state was %d, expected BEFORE_DOWNLOAD\n",
1721                download_state);
1722         WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, mime_type, sizeof(mime_type)-1, NULL, NULL);
1723         break;
1724     case BINDSTATUS_BEGINDOWNLOADDATA:
1725         if(iface == &objbsc)
1726             CHECK_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
1727         else
1728             CHECK_EXPECT(OnProgress_BEGINDOWNLOADDATA);
1729         ok(szStatusText != NULL, "szStatusText == NULL\n");
1730         if(szStatusText) {
1731             if(filedwl_api) {
1732                 /* FIXME */
1733             }else {
1734                 ok(!lstrcmpW(szStatusText, current_url), "wrong szStatusText %s\n", wine_dbgstr_w(szStatusText));
1735             }
1736         }
1737         if(!bind_to_object)
1738             ok(download_state == BEFORE_DOWNLOAD, "Download state was %d, expected BEFORE_DOWNLOAD\n",
1739                download_state);
1740         download_state = DOWNLOADING;
1741         break;
1742     case BINDSTATUS_DOWNLOADINGDATA:
1743         CHECK_EXPECT2(OnProgress_DOWNLOADINGDATA);
1744         ok(iface != &objbsc, "unexpected call\n");
1745         ok(download_state == DOWNLOADING, "Download state was %d, expected DOWNLOADING\n",
1746            download_state);
1747         if(test_abort) {
1748             HRESULT hres;
1749 
1750             SET_EXPECT(Abort);
1751             hres = IBinding_Abort(current_binding);
1752             ok(hres == S_OK, "Abort failed: %08x\n", hres);
1753             CHECK_CALLED(Abort);
1754 
1755             hres = IBinding_Abort(current_binding);
1756             ok(hres == E_FAIL, "Abort failed: %08x\n", hres);
1757 
1758             binding_hres = E_ABORT;
1759         }
1760         break;
1761     case BINDSTATUS_ENDDOWNLOADDATA:
1762         if(iface == &objbsc)
1763             CHECK_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
1764         else
1765             CHECK_EXPECT(OnProgress_ENDDOWNLOADDATA);
1766         ok(szStatusText != NULL, "szStatusText == NULL\n");
1767         if(szStatusText) {
1768             if(filedwl_api) {
1769                 /* FIXME */
1770             }else {
1771                 ok(!lstrcmpW(szStatusText, current_url), "wrong szStatusText %s\n", wine_dbgstr_w(szStatusText));
1772             }
1773         }
1774         ok(download_state == DOWNLOADING, "Download state was %d, expected DOWNLOADING\n",
1775            download_state);
1776         download_state = END_DOWNLOAD;
1777         break;
1778     case BINDSTATUS_CACHEFILENAMEAVAILABLE:
1779         if(test_protocol != HTTP_TEST && test_protocol != HTTPS_TEST && test_protocol != WINETEST_TEST) {
1780             if(iface == &objbsc)
1781                 CHECK_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
1782             else
1783                 CHECK_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
1784         }else {  /* FIXME */
1785             CLEAR_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
1786             CLEAR_CALLED(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
1787         }
1788 
1789         ok(szStatusText != NULL, "szStatusText == NULL\n");
1790         if(szStatusText && test_protocol == FILE_TEST)
1791             ok(!lstrcmpW(file_url+7, szStatusText), "wrong szStatusText %s\n", wine_dbgstr_w(szStatusText));
1792         break;
1793     case BINDSTATUS_CLASSIDAVAILABLE:
1794     {
1795         CLSID clsid;
1796         HRESULT hr;
1797         if(iface != &objbsc)
1798             ok(0, "unexpected call\n");
1799         else
1800             CHECK_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
1801         hr = CLSIDFromString((LPCOLESTR)szStatusText, &clsid);
1802         ok(hr == S_OK, "CLSIDFromString failed with error 0x%08x\n", hr);
1803         ok(IsEqualCLSID(&clsid, &CLSID_HTMLDocument),
1804             "Expected clsid to be CLSID_HTMLDocument instead of %s\n", wine_dbgstr_guid(&clsid));
1805         break;
1806     }
1807     case BINDSTATUS_BEGINSYNCOPERATION:
1808         CHECK_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
1809         if(iface != &objbsc)
1810             ok(0, "unexpected call\n");
1811         ok(szStatusText == NULL, "Expected szStatusText to be NULL\n");
1812         break;
1813     case BINDSTATUS_ENDSYNCOPERATION:
1814         CHECK_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
1815         if(iface != &objbsc)
1816             ok(0, "unexpected call\n");
1817         ok(szStatusText == NULL, "Expected szStatusText to be NULL\n");
1818         break;
1819     case BINDSTATUS_PROXYDETECTING:
1820         trace("BINDSTATUS_PROXYDETECTING\n");
1821         break;
1822     case BINDSTATUS_COOKIE_SENT:
1823         trace("BINDSTATUS_COOKIE_SENT\n");
1824         break;
1825     case BINDSTATUS_DECODING:
1826         trace("BINDSTATUS_DECODING\n");
1827         break;
1828     default:
1829         ok(0, "unexpected code %d\n", ulStatusCode);
1830     };
1831 
1832     if(current_binding) {
1833         IWinInetHttpInfo *http_info;
1834         HRESULT hres;
1835 
1836         hres = IBinding_QueryInterface(current_binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1837         if(!emulate_protocol && test_protocol != FILE_TEST && is_urlmon_protocol(test_protocol)) {
1838             ok(hres == S_OK, "Could not get IWinInetHttpInfo iface: %08x\n", hres);
1839             test_WinInetHttpInfo(http_info, ulStatusCode);
1840         } else
1841             ok(hres == E_NOINTERFACE,
1842                "QueryInterface(IID_IWinInetHttpInfo) returned: %08x, expected E_NOINTERFACE\n", hres);
1843         if(SUCCEEDED(hres))
1844             IWinInetHttpInfo_Release(http_info);
1845     }
1846 
1847     return S_OK;
1848 }
1849 
1850 static HRESULT WINAPI statusclb_OnStopBinding(IBindStatusCallbackEx *iface, HRESULT hresult, LPCWSTR szError)
1851 {
1852     if(iface == &objbsc) {
1853         CHECK_EXPECT(Obj_OnStopBinding);
1854         stopped_obj_binding = TRUE;
1855     }else {
1856         CHECK_EXPECT(OnStopBinding);
1857         stopped_binding = TRUE;
1858     }
1859 
1860     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1861 
1862     if(only_check_prot_args) {
1863         todo_wine ok(hresult == S_OK, "Got %08x\n", hresult);
1864         return S_OK;
1865     }
1866 
1867     /* ignore DNS failure */
1868     if (hresult == HRESULT_FROM_WIN32(ERROR_INTERNET_NAME_NOT_RESOLVED))
1869         return S_OK;
1870 
1871     if(filedwl_api) {
1872         if(!abort_progress && !abort_start)
1873             ok(SUCCEEDED(hresult), "binding failed: %08x\n", hresult);
1874         else if(abort_start && abort_hres == E_NOTIMPL)
1875             todo_wine ok(hresult == S_FALSE, "binding failed: %08x, expected S_FALSE\n", hresult);
1876         else
1877             ok(hresult == E_ABORT, "binding failed: %08x, expected E_ABORT\n", hresult);
1878     } else
1879         ok(hresult == binding_hres, "binding failed: %08x, expected %08x\n", hresult, binding_hres);
1880     ok(szError == NULL, "szError should be NULL\n");
1881 
1882     if(current_binding) {
1883         CLSID clsid;
1884         DWORD res;
1885         LPOLESTR res_str;
1886         HRESULT hres;
1887 
1888         hres = IBinding_GetBindResult(current_binding, &clsid, &res, &res_str, NULL);
1889         ok(hres == S_OK, "GetBindResult failed: %08x, expected S_OK\n", hres);
1890         ok(res == hresult, "res = %08x, expected %08x\n", res, binding_hres);
1891         ok(!res_str, "incorrect res_str = %s\n", wine_dbgstr_w(res_str));
1892 
1893         if(hresult==S_OK || (abort_start && hresult!=S_FALSE) || hresult == REGDB_E_CLASSNOTREG) {
1894             ok(IsEqualCLSID(&clsid, &CLSID_NULL),
1895                     "incorrect protocol CLSID: %s, expected CLSID_NULL\n",
1896                     wine_dbgstr_guid(&clsid));
1897         }else if(emulate_protocol) {
1898             todo_wine ok(IsEqualCLSID(&clsid, &CLSID_FtpProtocol),
1899                     "incorrect protocol CLSID: %s, expected CLSID_FtpProtocol\n",
1900                     wine_dbgstr_guid(&clsid));
1901         }else if(test_protocol == FTP_TEST) {
1902             ok(IsEqualCLSID(&clsid, &CLSID_FtpProtocol),
1903                     "incorrect protocol CLSID: %s, expected CLSID_FtpProtocol\n",
1904                     wine_dbgstr_guid(&clsid));
1905         }else if(test_protocol == FILE_TEST) {
1906             ok(IsEqualCLSID(&clsid, &CLSID_FileProtocol),
1907                     "incorrect protocol CLSID: %s, expected CLSID_FileProtocol\n",
1908                     wine_dbgstr_guid(&clsid));
1909         }else if(test_protocol == HTTP_TEST) {
1910             ok(IsEqualCLSID(&clsid, &CLSID_HttpProtocol),
1911                     "incorrect protocol CLSID: %s, expected CLSID_HttpProtocol\n",
1912                     wine_dbgstr_guid(&clsid));
1913         }else if(test_protocol == HTTPS_TEST) {
1914             ok(IsEqualCLSID(&clsid, &CLSID_HttpSProtocol),
1915                     "incorrect protocol CLSID: %s, expected CLSID_HttpSProtocol\n",
1916                     wine_dbgstr_guid(&clsid));
1917         }else if(test_protocol == ABOUT_TEST) {
1918             ok(IsEqualCLSID(&clsid, &CLSID_AboutProtocol),
1919                     "incorrect protocol CLSID: %s, expected CLSID_AboutProtocol\n",
1920                     wine_dbgstr_guid(&clsid));
1921         }else {
1922             ok(0, "unexpected (%d)\n", test_protocol);
1923         }
1924     }
1925 
1926     if((test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST) && emulate_protocol) {
1927         SetEvent(complete_event);
1928         if(iface != &objbsc)
1929             ok( WaitForSingleObject(complete_event2, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
1930     }
1931 
1932     if(test_protocol == HTTP_TEST && !emulate_protocol && http_cache_file[0]) {
1933         HANDLE file = CreateFileW(http_cache_file, DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1934                                   NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1935         ok(file == INVALID_HANDLE_VALUE, "expected INVALID_HANDLE_VALUE, got %p\n", file);
1936         ok(GetLastError() == ERROR_SHARING_VIOLATION, "expected ERROR_SHARING_VIOLATION, got %u\n", GetLastError());
1937         http_cache_file[0] = 0;
1938     }
1939 
1940     return S_OK;
1941 }
1942 
1943 static HRESULT WINAPI statusclb_GetBindInfo(IBindStatusCallbackEx *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
1944 {
1945     DWORD cbSize;
1946 
1947     if(iface == &objbsc)
1948         CHECK_EXPECT(Obj_GetBindInfo);
1949     else
1950         CHECK_EXPECT(GetBindInfo);
1951 
1952     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1953 
1954     *grfBINDF = bindf;
1955     cbSize = pbindinfo->cbSize;
1956     memset(pbindinfo, 0, cbSize);
1957     pbindinfo->cbSize = cbSize;
1958 
1959     return S_OK;
1960 }
1961 
1962 static void test_stream_seek(IStream *stream)
1963 {
1964     ULARGE_INTEGER new_pos;
1965     LARGE_INTEGER pos;
1966     HRESULT hres;
1967 
1968     pos.QuadPart = 0;
1969     new_pos.QuadPart = 0xdeadbeef;
1970     hres = IStream_Seek(stream, pos, STREAM_SEEK_SET, &new_pos);
1971     ok(hres == S_OK, "Seek failed: %08x\n", hres);
1972     ok(!new_pos.QuadPart, "new_pos.QuadPart != 0\n");
1973 
1974     pos.QuadPart = 0;
1975     new_pos.QuadPart = 0xdeadbeef;
1976     hres = IStream_Seek(stream, pos, STREAM_SEEK_END, &new_pos);
1977     ok(hres == S_OK, "Seek failed: %08x\n", hres);
1978     ok(new_pos.QuadPart, "new_pos.QuadPart = 0\n");
1979 
1980     pos.QuadPart = 0;
1981     new_pos.QuadPart = 0xdeadbeef;
1982     hres = IStream_Seek(stream, pos, 100, &new_pos);
1983     ok(hres == E_FAIL, "Seek failed: %08x\n", hres);
1984     ok(new_pos.QuadPart == 0xdeadbeef, "unexpected new_pos.QuadPart\n");
1985 }
1986 
1987 static HRESULT WINAPI statusclb_OnDataAvailable(IBindStatusCallbackEx *iface, DWORD grfBSCF,
1988         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
1989 {
1990     HRESULT hres;
1991     DWORD readed;
1992     BYTE buf[512];
1993     CHAR clipfmt[512];
1994 
1995     if(iface == &objbsc)
1996         ok(0, "unexpected call\n");
1997 
1998     CHECK_EXPECT2(OnDataAvailable);
1999 
2000     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
2001 
2002     ok(download_state == DOWNLOADING || download_state == END_DOWNLOAD,
2003        "Download state was %d, expected DOWNLOADING or END_DOWNLOAD\n",
2004        download_state);
2005     data_available = TRUE;
2006 
2007     if(bind_to_object && !is_async_prot)
2008         ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION), "grfBSCF = %x\n", grfBSCF);
2009 
2010     ok(pformatetc != NULL, "pformatetx == NULL\n");
2011     if(pformatetc) {
2012         if (mime_type[0]) {
2013             INT ret;
2014             clipfmt[0] = 0;
2015             ret = GetClipboardFormatNameA(pformatetc->cfFormat, clipfmt, sizeof(clipfmt)-1);
2016             ok(ret, "GetClipboardFormatName failed, error %d\n", GetLastError());
2017             ok(!strcmp(clipfmt, mime_type), "clipformat %x != mime_type, \"%s\" != \"%s\"\n",
2018                pformatetc->cfFormat, clipfmt, mime_type);
2019         } else {
2020             ok(pformatetc->cfFormat == 0, "clipformat=%x\n", pformatetc->cfFormat);
2021         }
2022         ok(pformatetc->ptd == NULL, "ptd = %p\n", pformatetc->ptd);
2023         ok(pformatetc->dwAspect == 1, "dwAspect=%u\n", pformatetc->dwAspect);
2024         ok(pformatetc->lindex == -1, "lindex=%d\n", pformatetc->lindex);
2025         ok(pformatetc->tymed == tymed, "tymed=%u, expected %u\n", pformatetc->tymed, tymed);
2026     }
2027 
2028     ok(pstgmed != NULL, "stgmeg == NULL\n");
2029     ok(pstgmed->tymed == tymed, "tymed=%u, expected %u\n", pstgmed->tymed, tymed);
2030     ok(pstgmed->pUnkForRelease != NULL, "pUnkForRelease == NULL\n");
2031 
2032     switch(pstgmed->tymed) {
2033     case TYMED_ISTREAM: {
2034         IStream *stream = U(*pstgmed).pstm;
2035 
2036         ok(stream != NULL, "U(*pstgmed).pstm == NULL\n");
2037 
2038         if(grfBSCF & BSCF_FIRSTDATANOTIFICATION) {
2039             STATSTG stat;
2040 
2041             hres = IStream_Write(stream, buf, 10, NULL);
2042             ok(hres == STG_E_ACCESSDENIED,
2043                "Write failed: %08x, expected STG_E_ACCESSDENIED\n", hres);
2044 
2045             hres = IStream_Commit(stream, 0);
2046             ok(hres == E_NOTIMPL, "Commit failed: %08x, expected E_NOTIMPL\n", hres);
2047 
2048             hres = IStream_Revert(stream);
2049             ok(hres == E_NOTIMPL, "Revert failed: %08x, expected E_NOTIMPL\n", hres);
2050 
2051             hres = IStream_Stat(stream, NULL, STATFLAG_NONAME);
2052             ok(hres == E_FAIL, "hres = %x\n", hres);
2053             if(use_cache_file && emulate_protocol) {
2054                 hres = IStream_Stat(stream, &stat, STATFLAG_DEFAULT);
2055                 ok(hres == S_OK, "hres = %x\n", hres);
2056                 ok(!lstrcmpW(stat.pwcsName, cache_file_name),
2057                         "stat.pwcsName = %s, cache_file_name = %s\n",
2058                         wine_dbgstr_w(stat.pwcsName), wine_dbgstr_w(cache_file_name));
2059                 CoTaskMemFree(stat.pwcsName);
2060                 ok(U(stat.cbSize).LowPart == (bindf&BINDF_ASYNCHRONOUS?0:6500),
2061                         "stat.cbSize.LowPart = %u\n", U(stat.cbSize).LowPart);
2062             } else {
2063                 hres = IStream_Stat(stream, &stat, STATFLAG_NONAME);
2064                 ok(hres == S_OK, "hres = %x\n", hres);
2065                 ok(!stat.pwcsName || broken(stat.pwcsName!=NULL),
2066                         "stat.pwcsName = %s\n", wine_dbgstr_w(stat.pwcsName));
2067             }
2068             ok(stat.type == STGTY_STREAM, "stat.type = %x\n", stat.type);
2069             ok(U(stat.cbSize).HighPart == 0, "stat.cbSize.HighPart != 0\n");
2070             ok(stat.grfMode == (U(stat.cbSize).LowPart?GENERIC_READ:0), "stat.grfMode = %x\n", stat.grfMode);
2071             ok(stat.grfLocksSupported == 0, "stat.grfLocksSupported = %x\n", stat.grfLocksSupported);
2072             ok(stat.grfStateBits == 0, "stat.grfStateBits = %x\n", stat.grfStateBits);
2073             ok(stat.reserved == 0, "stat.reserved = %x\n", stat.reserved);
2074         }
2075 
2076         if(callback_read) {
2077             do {
2078                 hres = IStream_Read(stream, buf, 512, &readed);
2079                 if(test_protocol == HTTP_TEST && emulate_protocol && readed)
2080                     ok(buf[0] == (use_cache_file && !(bindf&BINDF_ASYNCHRONOUS) ? 'X' : '?'), "buf[0] = '%c'\n", buf[0]);
2081             }while(hres == S_OK);
2082             ok(hres == S_FALSE || hres == E_PENDING, "IStream_Read returned %08x\n", hres);
2083         }
2084 
2085         if(use_cache_file && (grfBSCF & BSCF_FIRSTDATANOTIFICATION) && !(bindf & BINDF_PULLDATA))
2086             test_stream_seek(stream);
2087         break;
2088     }
2089     case TYMED_FILE:
2090         if(test_protocol == FILE_TEST)
2091             ok(!lstrcmpW(pstgmed->u.lpszFileName, file_url+7),
2092                "unexpected file name %s\n", wine_dbgstr_w(pstgmed->u.lpszFileName));
2093         else if(emulate_protocol)
2094             ok(!lstrcmpW(pstgmed->u.lpszFileName, cache_fileW),
2095                "unexpected file name %s\n", wine_dbgstr_w(pstgmed->u.lpszFileName));
2096         else if(test_protocol == HTTP_TEST)
2097             lstrcpyW(http_cache_file, pstgmed->u.lpszFileName);
2098         else
2099             ok(pstgmed->u.lpszFileName != NULL, "lpszFileName == NULL\n");
2100     }
2101 
2102     if((test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST)
2103        && emulate_protocol && prot_state < 4 && (!bind_to_object || prot_state > 1))
2104         SetEvent(complete_event);
2105 
2106     return S_OK;
2107 }
2108 
2109 static HRESULT WINAPI statusclb_OnObjectAvailable(IBindStatusCallbackEx *iface, REFIID riid, IUnknown *punk)
2110 {
2111     CHECK_EXPECT(OnObjectAvailable);
2112 
2113     if(iface != &objbsc)
2114         ok(0, "unexpected call\n");
2115 
2116     ok(IsEqualGUID(&IID_IUnknown, riid), "riid = %s\n", wine_dbgstr_guid(riid));
2117     ok(punk != NULL, "punk == NULL\n");
2118 
2119     return S_OK;
2120 }
2121 
2122 static HRESULT WINAPI statusclb_GetBindInfoEx(IBindStatusCallbackEx *iface, DWORD *grfBINDF, BINDINFO *pbindinfo,
2123         DWORD *grfBINDF2, DWORD *pdwReserved)
2124 {
2125     CHECK_EXPECT(GetBindInfoEx);
2126 
2127     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
2128     ok(grfBINDF2 != NULL, "grfBINDF2 == NULL\n");
2129     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
2130     ok(pdwReserved != NULL, "dwReserved == NULL\n");
2131 
2132     return S_OK;
2133 }
2134 
2135 static const IBindStatusCallbackExVtbl BindStatusCallbackVtbl = {
2136     statusclb_QueryInterface,
2137     statusclb_AddRef,
2138     statusclb_Release,
2139     statusclb_OnStartBinding,
2140     statusclb_GetPriority,
2141     statusclb_OnLowResource,
2142     statusclb_OnProgress,
2143     statusclb_OnStopBinding,
2144     statusclb_GetBindInfo,
2145     statusclb_OnDataAvailable,
2146     statusclb_OnObjectAvailable,
2147     statusclb_GetBindInfoEx
2148 };
2149 
2150 static IBindStatusCallbackEx bsc = { &BindStatusCallbackVtbl };
2151 static IBindStatusCallbackEx bsc2 = { &BindStatusCallbackVtbl };
2152 static IBindStatusCallbackEx objbsc = { &BindStatusCallbackVtbl };
2153 
2154 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppv)
2155 {
2156     *ppv = NULL;
2157     ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
2158     return E_NOINTERFACE;
2159 }
2160 
2161 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
2162 {
2163     return 2;
2164 }
2165 
2166 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
2167 {
2168     return 1;
2169 }
2170 
2171 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
2172 {
2173     switch(mkp) {
2174     case MIMETYPEPROP:
2175         CHECK_EXPECT(PutProperty_MIMETYPEPROP);
2176         ok(!lstrcmpW(val, wszTextHtml), "val = %s\n", wine_dbgstr_w(val));
2177         break;
2178     case CLASSIDPROP:
2179         CHECK_EXPECT(PutProperty_CLASSIDPROP);
2180         break;
2181     default:
2182         break;
2183     }
2184 
2185     return S_OK;
2186 }
2187 
2188 static const IMonikerPropVtbl MonikerPropVtbl = {
2189     MonikerProp_QueryInterface,
2190     MonikerProp_AddRef,
2191     MonikerProp_Release,
2192     MonikerProp_PutProperty
2193 };
2194 
2195 static IMonikerProp MonikerProp = { &MonikerPropVtbl };
2196 
2197 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid, void **ppv)
2198 {
2199     *ppv = NULL;
2200 
2201     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IPersistMoniker, riid))
2202         *ppv = iface;
2203     else if(IsEqualGUID(&IID_IMonikerProp, riid))
2204         *ppv = &MonikerProp;
2205 
2206     if(*ppv)
2207         return S_OK;
2208 
2209     ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
2210     return E_NOINTERFACE;
2211 }
2212 
2213 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
2214 {
2215     return 2;
2216 }
2217 
2218 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
2219 {
2220     return 1;
2221 }
2222 
2223 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
2224 {
2225     ok(0, "unexpected call\n");
2226     return E_NOTIMPL;
2227 }
2228 
2229 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
2230 {
2231     ok(0, "unexpected call\n");
2232     return E_NOTIMPL;
2233 }
2234 
2235 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
2236                                           IMoniker *pimkName, LPBC pibc, DWORD grfMode)
2237 {
2238     IUnknown *unk;
2239     HRESULT hres;
2240 
2241     static WCHAR cbinding_contextW[] =
2242         {'C','B','i','n','d','i','n','g',' ','C','o','n','t','e','x','t',0};
2243 
2244     CHECK_EXPECT(Load);
2245     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
2246 
2247     if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
2248         ok(!fFullyAvailable, "fFullyAvailable = %x\n", fFullyAvailable);
2249     else
2250         ok(fFullyAvailable, "fFullyAvailable = %x\n", fFullyAvailable);
2251     ok(pimkName != NULL, "pimkName == NULL\n");
2252     ok(pibc != NULL, "pibc == NULL\n");
2253     ok(grfMode == 0x12, "grfMode = %x\n", grfMode);
2254 
2255     hres = IBindCtx_GetObjectParam(pibc, cbinding_contextW, &unk);
2256     ok(hres == S_OK, "GetObjectParam(CBinding Context) failed: %08x\n", hres);
2257     if(SUCCEEDED(hres)) {
2258         IBinding *binding;
2259 
2260         hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void**)&binding);
2261         ok(hres == S_OK, "Could not get IBinding: %08x\n", hres);
2262 
2263         IBinding_Release(binding);
2264         IUnknown_Release(unk);
2265     }
2266 
2267     SET_EXPECT(QueryInterface_IServiceProvider);
2268     hres = RegisterBindStatusCallback(pibc, (IBindStatusCallback*)&bsc, NULL, 0);
2269     ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
2270     CHECK_CALLED(QueryInterface_IServiceProvider);
2271 
2272     SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
2273     SET_EXPECT(GetBindInfo);
2274     SET_EXPECT(OnStartBinding);
2275     if(test_redirect)
2276         SET_EXPECT(OnProgress_REDIRECTING);
2277     SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
2278     if(test_protocol == FILE_TEST)
2279         SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
2280     if(test_protocol != HTTP_TEST && test_protocol != HTTPS_TEST)
2281         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
2282     SET_EXPECT(LockRequest);
2283     SET_EXPECT(OnDataAvailable);
2284     if(test_protocol != HTTP_TEST && test_protocol != HTTPS_TEST)
2285         SET_EXPECT(OnStopBinding);
2286 
2287     hres = IMoniker_BindToStorage(pimkName, pibc, NULL, &IID_IStream, (void**)&unk);
2288     ok(hres == S_OK, "Load failed: %08x\n", hres);
2289 
2290     CLEAR_CALLED(QueryInterface_IBindStatusCallbackEx); /* IE 8 */
2291     CHECK_CALLED(GetBindInfo);
2292     CHECK_CALLED(OnStartBinding);
2293     if(test_redirect)
2294         CHECK_CALLED(OnProgress_REDIRECTING);
2295     CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
2296     if(test_protocol == FILE_TEST)
2297         CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
2298     if(test_protocol != HTTP_TEST && test_protocol != HTTPS_TEST)
2299         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
2300     CHECK_CALLED(LockRequest);
2301     CHECK_CALLED(OnDataAvailable);
2302     if(test_protocol != HTTP_TEST && test_protocol != HTTPS_TEST)
2303         CHECK_CALLED(OnStopBinding);
2304 
2305     if(unk)
2306         IUnknown_Release(unk);
2307 
2308     return S_OK;
2309 }
2310 
2311 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName, LPBC pbc, BOOL fRemember)
2312 {
2313     ok(0, "unexpected call\n");
2314     return E_NOTIMPL;
2315 }
2316 
2317 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
2318 {
2319     ok(0, "unexpected call\n");
2320     return E_NOTIMPL;
2321 }
2322 
2323 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **pimkName)
2324 {
2325     ok(0, "unexpected call\n");
2326     return E_NOTIMPL;
2327 }
2328 
2329 static const IPersistMonikerVtbl PersistMonikerVtbl = {
2330     PersistMoniker_QueryInterface,
2331     PersistMoniker_AddRef,
2332     PersistMoniker_Release,
2333     PersistMoniker_GetClassID,
2334     PersistMoniker_IsDirty,
2335     PersistMoniker_Load,
2336     PersistMoniker_Save,
2337     PersistMoniker_SaveCompleted,
2338     PersistMoniker_GetCurMoniker
2339 };
2340 
2341 static IPersistMoniker PersistMoniker = { &PersistMonikerVtbl };
2342 
2343 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
2344 {
2345     *ppv = NULL;
2346 
2347     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
2348         *ppv = iface;
2349         return S_OK;
2350     }
2351 
2352     if(IsEqualGUID(&IID_IMarshal, riid))
2353         return E_NOINTERFACE;
2354     if(IsEqualGUID(&CLSID_IdentityUnmarshal, riid))
2355         return E_NOINTERFACE;
2356 
2357     ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
2358     return E_NOTIMPL;
2359 }
2360 
2361 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
2362 {
2363     return 2;
2364 }
2365 
2366 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
2367 {
2368     return 1;
2369 }
2370 
2371 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
2372 {
2373     CHECK_EXPECT(CreateInstance);
2374     ok(!outer, "outer = %p\n", outer);
2375     ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", wine_dbgstr_guid(riid));
2376     *ppv = &PersistMoniker;
2377     return S_OK;
2378 }
2379 
2380 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
2381 {
2382     ok(0, "unexpected call\n");
2383     return S_OK;
2384 }
2385 
2386 static const IClassFactoryVtbl ClassFactoryVtbl = {
2387     ClassFactory_QueryInterface,
2388     ClassFactory_AddRef,
2389     ClassFactory_Release,
2390     ClassFactory_CreateInstance,
2391     ClassFactory_LockServer
2392 };
2393 
2394 static IClassFactory mime_cf = { &ClassFactoryVtbl };
2395 
2396 static HRESULT WINAPI ProtocolCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
2397 {
2398     *ppv = NULL;
2399 
2400     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
2401         *ppv = iface;
2402         return S_OK;
2403     }
2404 
2405     if(IsEqualGUID(&IID_IInternetProtocolInfo, riid))
2406         return E_NOINTERFACE;
2407 
2408     ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
2409     return E_NOTIMPL;
2410 }
2411 
2412 static HRESULT WINAPI ProtocolCF_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
2413 {
2414     if(IsEqualGUID(&IID_IInternetProtocolInfo, riid))
2415         return E_NOINTERFACE;
2416 
2417     ok(outer != NULL, "outer == NULL\n");
2418     ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", wine_dbgstr_guid(riid));
2419     *ppv = &Protocol;
2420     return S_OK;
2421 }
2422 
2423 static const IClassFactoryVtbl ProtocolCFVtbl = {
2424     ProtocolCF_QueryInterface,
2425     ClassFactory_AddRef,
2426     ClassFactory_Release,
2427     ProtocolCF_CreateInstance,
2428     ClassFactory_LockServer
2429 };
2430 
2431 static IClassFactory protocol_cf = { &ProtocolCFVtbl };
2432 
2433 static void test_CreateAsyncBindCtx(void)
2434 {
2435     IBindCtx *bctx = (IBindCtx*)0x0ff00ff0;
2436     IUnknown *unk;
2437     HRESULT hres;
2438     ULONG ref;
2439     BIND_OPTS bindopts;
2440 
2441     hres = CreateAsyncBindCtx(0, NULL, NULL, &bctx);
2442     ok(hres == E_INVALIDARG, "CreateAsyncBindCtx failed. expected: E_INVALIDARG, got: %08x\n", hres);
2443     ok(bctx == (IBindCtx*)0x0ff00ff0, "bctx should not be changed\n");
2444 
2445     hres = CreateAsyncBindCtx(0, NULL, NULL, NULL);
2446     ok(hres == E_INVALIDARG, "CreateAsyncBindCtx failed. expected: E_INVALIDARG, got: %08x\n", hres);
2447 
2448     SET_EXPECT(QueryInterface_IServiceProvider);
2449     hres = CreateAsyncBindCtx(0, (IBindStatusCallback*)&bsc, NULL, &bctx);
2450     ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n", hres);
2451     CHECK_CALLED(QueryInterface_IServiceProvider);
2452 
2453     bindopts.cbStruct = sizeof(bindopts);
2454     hres = IBindCtx_GetBindOptions(bctx, &bindopts);
2455     ok(hres == S_OK, "IBindCtx_GetBindOptions failed: %08x\n", hres);
2456     ok(bindopts.grfFlags == BIND_MAYBOTHERUSER,
2457                 "bindopts.grfFlags = %08x, expected: BIND_MAYBOTHERUSER\n", bindopts.grfFlags);
2458     ok(bindopts.grfMode == (STGM_READWRITE | STGM_SHARE_EXCLUSIVE),
2459                 "bindopts.grfMode = %08x, expected: STGM_READWRITE | STGM_SHARE_EXCLUSIVE\n",
2460                 bindopts.grfMode);
2461     ok(bindopts.dwTickCountDeadline == 0,
2462                 "bindopts.dwTickCountDeadline = %08x, expected: 0\n", bindopts.dwTickCountDeadline);
2463 
2464     hres = IBindCtx_QueryInterface(bctx, &IID_IAsyncBindCtx, (void**)&unk);
2465     ok(hres == E_NOINTERFACE, "QueryInterface(IID_IAsyncBindCtx) failed: %08x, expected E_NOINTERFACE\n", hres);
2466     if(SUCCEEDED(hres))
2467         IUnknown_Release(unk);
2468 
2469     ref = IBindCtx_Release(bctx);
2470     ok(ref == 0, "bctx should be destroyed here\n");
2471 }
2472 
2473 static void test_CreateAsyncBindCtxEx(void)
2474 {
2475     IBindCtx *bctx = NULL, *bctx2 = NULL, *bctx_arg = NULL;
2476     IUnknown *unk;
2477     BIND_OPTS bindopts;
2478     HRESULT hres;
2479 
2480     static WCHAR testW[] = {'t','e','s','t',0};
2481 
2482     if (!pCreateAsyncBindCtxEx) {
2483         win_skip("CreateAsyncBindCtxEx not present\n");
2484         return;
2485     }
2486 
2487     hres = pCreateAsyncBindCtxEx(NULL, 0, NULL, NULL, NULL, 0);
2488     ok(hres == E_INVALIDARG, "CreateAsyncBindCtx failed: %08x, expected E_INVALIDARG\n", hres);
2489 
2490     hres = pCreateAsyncBindCtxEx(NULL, 0, NULL, NULL, &bctx, 0);
2491     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
2492 
2493     if(SUCCEEDED(hres)) {
2494         bindopts.cbStruct = sizeof(bindopts);
2495         hres = IBindCtx_GetBindOptions(bctx, &bindopts);
2496         ok(hres == S_OK, "IBindCtx_GetBindOptions failed: %08x\n", hres);
2497         ok(bindopts.grfFlags == BIND_MAYBOTHERUSER,
2498                 "bindopts.grfFlags = %08x, expected: BIND_MAYBOTHERUSER\n", bindopts.grfFlags);
2499         ok(bindopts.grfMode == (STGM_READWRITE | STGM_SHARE_EXCLUSIVE),
2500                 "bindopts.grfMode = %08x, expected: STGM_READWRITE | STGM_SHARE_EXCLUSIVE\n",
2501                 bindopts.grfMode);
2502         ok(bindopts.dwTickCountDeadline == 0,
2503                 "bindopts.dwTickCountDeadline = %08x, expected: 0\n", bindopts.dwTickCountDeadline);
2504 
2505         IBindCtx_Release(bctx);
2506     }
2507 
2508     CreateBindCtx(0, &bctx_arg);
2509     hres = pCreateAsyncBindCtxEx(NULL, 0, NULL, NULL, &bctx, 0);
2510     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
2511 
2512     if(SUCCEEDED(hres)) {
2513         bindopts.cbStruct = sizeof(bindopts);
2514         hres = IBindCtx_GetBindOptions(bctx, &bindopts);
2515         ok(hres == S_OK, "IBindCtx_GetBindOptions failed: %08x\n", hres);
2516         ok(bindopts.grfFlags == BIND_MAYBOTHERUSER,
2517                 "bindopts.grfFlags = %08x, expected: BIND_MAYBOTHERUSER\n", bindopts.grfFlags);
2518         ok(bindopts.grfMode == (STGM_READWRITE | STGM_SHARE_EXCLUSIVE),
2519                 "bindopts.grfMode = %08x, expected: STGM_READWRITE | STGM_SHARE_EXCLUSIVE\n",
2520                 bindopts.grfMode);
2521         ok(bindopts.dwTickCountDeadline == 0,
2522                 "bindopts.dwTickCountDeadline = %08x, expected: 0\n", bindopts.dwTickCountDeadline);
2523 
2524         IBindCtx_Release(bctx);
2525     }
2526 
2527     IBindCtx_Release(bctx_arg);
2528 
2529     SET_EXPECT(QueryInterface_IServiceProvider);
2530     hres = pCreateAsyncBindCtxEx(NULL, 0, (IBindStatusCallback*)&bsc, NULL, &bctx, 0);
2531     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
2532     CHECK_CALLED(QueryInterface_IServiceProvider);
2533 
2534     hres = IBindCtx_QueryInterface(bctx, &IID_IAsyncBindCtx, (void**)&unk);
2535     ok(hres == S_OK, "QueryInterface(IID_IAsyncBindCtx) failed: %08x\n", hres);
2536     if(SUCCEEDED(hres))
2537         IUnknown_Release(unk);
2538 
2539     IBindCtx_Release(bctx);
2540 
2541     hres = CreateBindCtx(0, &bctx2);
2542     ok(hres == S_OK, "CreateBindCtx failed: %08x\n", hres);
2543 
2544     hres = pCreateAsyncBindCtxEx(bctx2, 0, NULL, NULL, &bctx, 0);
2545     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
2546 
2547     hres = IBindCtx_RegisterObjectParam(bctx2, testW, (IUnknown*)&Protocol);
2548     ok(hres == S_OK, "RegisterObjectParam failed: %08x\n", hres);
2549 
2550     hres = IBindCtx_GetObjectParam(bctx, testW, &unk);
2551     ok(hres == S_OK, "GetObjectParam failed: %08x\n", hres);
2552     ok(unk == (IUnknown*)&Protocol, "unexpected unk %p\n", unk);
2553 
2554     IBindCtx_Release(bctx);
2555     IBindCtx_Release(bctx2);
2556 }
2557 
2558 static void test_GetBindInfoEx(IBindStatusCallback *holder)
2559 {
2560     IBindStatusCallbackEx *bscex;
2561     BINDINFO bindinfo = {sizeof(bindinfo)};
2562     DWORD bindf, bindf2, dw;
2563     HRESULT hres;
2564 
2565     hres = IBindStatusCallback_QueryInterface(holder, &IID_IBindStatusCallbackEx, (void**)&bscex);
2566     if(FAILED(hres)) {
2567         win_skip("IBindStatusCallbackEx not supported\n");
2568         return;
2569     }
2570 
2571     use_bscex = TRUE;
2572 
2573     bindf = 0;
2574     SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
2575     SET_EXPECT(GetBindInfoEx);
2576     hres = IBindStatusCallback_GetBindInfo(holder, &bindf, &bindinfo);
2577     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
2578     CHECK_CALLED(QueryInterface_IBindStatusCallbackEx);
2579     CHECK_CALLED(GetBindInfoEx);
2580 
2581     bindf = bindf2 = dw = 0;
2582     SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
2583     SET_EXPECT(GetBindInfoEx);
2584     hres = IBindStatusCallbackEx_GetBindInfoEx(bscex, &bindf, &bindinfo, &bindf2, &dw);
2585     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
2586     CHECK_CALLED(QueryInterface_IBindStatusCallbackEx);
2587     CHECK_CALLED(GetBindInfoEx);
2588 
2589     use_bscex = FALSE;
2590 
2591     bindf = bindf2 = dw = 0xdeadbeef;
2592     SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
2593     SET_EXPECT(GetBindInfo);
2594     hres = IBindStatusCallbackEx_GetBindInfoEx(bscex, &bindf, &bindinfo, &bindf2, &dw);
2595     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
2596     CHECK_CALLED(QueryInterface_IBindStatusCallbackEx);
2597     CHECK_CALLED(GetBindInfo);
2598     ok(bindf2 == 0xdeadbeef, "bindf2 = %x\n", bindf2);
2599     ok(dw == 0xdeadbeef, "dw = %x\n", dw);
2600 
2601     IBindStatusCallbackEx_Release(bscex);
2602 }
2603 
2604 static BOOL test_bscholder(IBindStatusCallback *holder)
2605 {
2606     IServiceProvider *serv_prov;
2607     IHttpNegotiate *http_negotiate, *http_negotiate_serv;
2608     IHttpNegotiate2 *http_negotiate2, *http_negotiate2_serv;
2609     IAuthenticate *authenticate, *authenticate_serv;
2610     IInternetBindInfo *bind_info;
2611     IInternetProtocol *protocol;
2612     BINDINFO bindinfo = {sizeof(bindinfo)};
2613     BOOL ret = TRUE;
2614     LPWSTR wstr;
2615     DWORD dw;
2616     HRESULT hres;
2617 
2618     hres = IBindStatusCallback_QueryInterface(holder, &IID_IServiceProvider, (void**)&serv_prov);
2619     ok(hres == S_OK, "Could not get IServiceProvider interface: %08x\n", hres);
2620 
2621     dw = 0xdeadbeef;
2622     SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
2623     SET_EXPECT(GetBindInfo);
2624     hres = IBindStatusCallback_GetBindInfo(holder, &dw, &bindinfo);
2625     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
2626     CLEAR_CALLED(QueryInterface_IBindStatusCallbackEx); /* IE 8 */
2627     CHECK_CALLED(GetBindInfo);
2628 
2629     test_GetBindInfoEx(holder);
2630 
2631     SET_EXPECT(OnStartBinding);
2632     hres = IBindStatusCallback_OnStartBinding(holder, 0, (void*)0xdeadbeef);
2633     ok(hres == S_OK, "OnStartBinding failed: %08x\n", hres);
2634     CHECK_CALLED(OnStartBinding);
2635 
2636     hres = IBindStatusCallback_QueryInterface(holder, &IID_IHttpNegotiate, (void**)&http_negotiate);
2637     ok(hres == S_OK, "Could not get IHttpNegotiate interface: %08x\n", hres);
2638 
2639     SET_EXPECT(QueryInterface_IHttpNegotiate);
2640     hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpNegotiate, &IID_IHttpNegotiate,
2641                                          (void**)&http_negotiate_serv);
2642     ok(hres == S_OK, "Could not get IHttpNegotiate service: %08x\n", hres);
2643     CLEAR_CALLED(QueryInterface_IHttpNegotiate); /* IE <8 */
2644 
2645     ok(http_negotiate == http_negotiate_serv, "http_negotiate != http_negotiate_serv\n");
2646 
2647     wstr = (void*)0xdeadbeef;
2648     SET_EXPECT(QueryInterface_IHttpNegotiate);
2649     SET_EXPECT(BeginningTransaction);
2650     hres = IHttpNegotiate_BeginningTransaction(http_negotiate_serv, current_url, emptyW, 0, &wstr);
2651     CHECK_CALLED_BROKEN(QueryInterface_IHttpNegotiate); /* IE8 */
2652     CHECK_CALLED(BeginningTransaction);
2653     ok(hres == S_OK, "BeginningTransaction failed: %08x\n", hres);
2654     ok(wstr == NULL, "wstr = %p\n", wstr);
2655 
2656     IHttpNegotiate_Release(http_negotiate_serv);
2657 
2658     hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpNegotiate, &IID_IHttpNegotiate,
2659                                          (void**)&http_negotiate_serv);
2660     ok(hres == S_OK, "Could not get IHttpNegotiate service: %08x\n", hres);
2661     ok(http_negotiate == http_negotiate_serv, "http_negotiate != http_negotiate_serv\n");
2662     IHttpNegotiate_Release(http_negotiate_serv);
2663 
2664     hres = IBindStatusCallback_QueryInterface(holder, &IID_IHttpNegotiate2, (void**)&http_negotiate2);
2665     if(SUCCEEDED(hres)) {
2666         have_IHttpNegotiate2 = TRUE;
2667 
2668         SET_EXPECT(QueryInterface_IHttpNegotiate2);
2669         hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpNegotiate2, &IID_IHttpNegotiate2,
2670                                              (void**)&http_negotiate2_serv);
2671         ok(hres == S_OK, "Could not get IHttpNegotiate2 service: %08x\n", hres);
2672         CLEAR_CALLED(QueryInterface_IHttpNegotiate2); /* IE <8 */
2673         ok(http_negotiate2 == http_negotiate2_serv, "http_negotiate != http_negotiate_serv\n");
2674 
2675         SET_EXPECT(QueryInterface_IHttpNegotiate2);
2676         SET_EXPECT(GetRootSecurityId);
2677         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, (void*)0xdeadbeef, (void*)0xdeadbeef, 0);
2678         ok(hres == E_NOTIMPL, "GetRootSecurityId failed: %08x\n", hres);
2679         CHECK_CALLED_BROKEN(QueryInterface_IHttpNegotiate2); /* IE8 */
2680         CHECK_CALLED(GetRootSecurityId);
2681 
2682         IHttpNegotiate2_Release(http_negotiate2_serv);
2683         IHttpNegotiate2_Release(http_negotiate2);
2684     }else {
2685         skip("Could not get IHttpNegotiate2\n");
2686         ret = FALSE;
2687     }
2688 
2689     SET_EXPECT(OnProgress_FINDINGRESOURCE);
2690     hres = IBindStatusCallback_OnProgress(holder, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
2691     ok(hres == S_OK, "OnProgress failed: %08x\n", hres);
2692     CHECK_CALLED(OnProgress_FINDINGRESOURCE);
2693 
2694     SET_EXPECT(QueryInterface_IHttpNegotiate);
2695     SET_EXPECT(OnResponse);
2696     wstr = (void*)0xdeadbeef;
2697     hres = IHttpNegotiate_OnResponse(http_negotiate, 200, emptyW, NULL, NULL);
2698     ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
2699     CHECK_CALLED_BROKEN(QueryInterface_IHttpNegotiate); /* IE8 */
2700     CHECK_CALLED(OnResponse);
2701 
2702     IHttpNegotiate_Release(http_negotiate);
2703 
2704     hres = IBindStatusCallback_QueryInterface(holder, &IID_IAuthenticate, (void**)&authenticate);
2705     ok(hres == S_OK, "Could not get IAuthenticate interface: %08x\n", hres);
2706 
2707     SET_EXPECT(QueryInterface_IAuthenticate);
2708     SET_EXPECT(QueryService_IAuthenticate);
2709     hres = IServiceProvider_QueryService(serv_prov, &IID_IAuthenticate, &IID_IAuthenticate,
2710                                          (void**)&authenticate_serv);
2711     ok(hres == S_OK, "Could not get IAuthenticate service: %08x\n", hres);
2712     CLEAR_CALLED(QueryInterface_IAuthenticate); /* IE <8 */
2713     CLEAR_CALLED(QueryService_IAuthenticate); /* IE <8 */
2714     ok(authenticate == authenticate_serv, "authenticate != authenticate_serv\n");
2715     IAuthenticate_Release(authenticate_serv);
2716 
2717     hres = IServiceProvider_QueryService(serv_prov, &IID_IAuthenticate, &IID_IAuthenticate,
2718                                          (void**)&authenticate_serv);
2719     ok(hres == S_OK, "Could not get IAuthenticate service: %08x\n", hres);
2720     ok(authenticate == authenticate_serv, "authenticate != authenticate_serv\n");
2721 
2722     IAuthenticate_Release(authenticate);
2723     IAuthenticate_Release(authenticate_serv);
2724 
2725     hres = IBindStatusCallback_QueryInterface(holder, &IID_IInternetBindInfo, (void**)&bind_info);
2726     ok(hres == S_OK || broken(hres == E_NOINTERFACE /* win2k */), "Could not get IInternetBindInfo interface: %08x\n", hres);
2727 
2728     if(SUCCEEDED(hres)) {
2729         hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_USER_AGENT, &wstr, 1, &dw);
2730         ok(hres == E_NOINTERFACE, "GetBindString(BINDSTRING_USER_AGENT) failed: %08x\n", hres);
2731 
2732         IInternetBindInfo_Release(bind_info);
2733     }
2734 
2735     SET_EXPECT(OnStopBinding);
2736     hres = IBindStatusCallback_OnStopBinding(holder, S_OK, NULL);
2737     ok(hres == S_OK, "OnStopBinding failed: %08x\n", hres);
2738     CHECK_CALLED(OnStopBinding);
2739 
2740     SET_EXPECT(QueryInterface_IInternetProtocol);
2741     SET_EXPECT(QueryService_IInternetProtocol);
2742     hres = IServiceProvider_QueryService(serv_prov, &IID_IInternetProtocol, &IID_IInternetProtocol,
2743                                          (void**)&protocol);
2744     ok(hres == E_NOINTERFACE, "QueryService(IInternetProtocol) failed: %08x\n", hres);
2745     CHECK_CALLED(QueryInterface_IInternetProtocol);
2746     CHECK_CALLED(QueryService_IInternetProtocol);
2747 
2748     IServiceProvider_Release(serv_prov);
2749     return ret;
2750 }
2751 
2752 static BOOL test_RegisterBindStatusCallback(void)
2753 {
2754     IBindStatusCallback *prevbsc, *clb, *prev_clb;
2755     IBindCtx *bindctx;
2756     BOOL ret = TRUE;
2757     IUnknown *unk;
2758     HRESULT hres;
2759 
2760     strict_bsc_qi = TRUE;
2761 
2762     hres = CreateBindCtx(0, &bindctx);
2763     ok(hres == S_OK, "BindCtx failed: %08x\n", hres);
2764 
2765     SET_EXPECT(QueryInterface_IServiceProvider);
2766 
2767     hres = IBindCtx_RegisterObjectParam(bindctx, BSCBHolder, (IUnknown*)&bsc);
2768     ok(hres == S_OK, "RegisterObjectParam failed: %08x\n", hres);
2769 
2770     SET_EXPECT(QueryInterface_IBindStatusCallback);
2771     SET_EXPECT(QueryInterface_IBindStatusCallbackHolder);
2772     prevbsc = (void*)0xdeadbeef;
2773     hres = RegisterBindStatusCallback(bindctx, (IBindStatusCallback*)&bsc, &prevbsc, 0);
2774     ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
2775     ok(prevbsc == (IBindStatusCallback*)&bsc, "prevbsc=%p\n", prevbsc);
2776     CHECK_CALLED(QueryInterface_IBindStatusCallback);
2777     CHECK_CALLED(QueryInterface_IBindStatusCallbackHolder);
2778 
2779     CHECK_CALLED(QueryInterface_IServiceProvider);
2780 
2781     hres = IBindCtx_GetObjectParam(bindctx, BSCBHolder, &unk);
2782     ok(hres == S_OK, "GetObjectParam failed: %08x\n", hres);
2783 
2784     hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)&clb);
2785     IUnknown_Release(unk);
2786     ok(hres == S_OK, "QueryInterface(IID_IBindStatusCallback) failed: %08x\n", hres);
2787     ok(clb != (IBindStatusCallback*)&bsc, "bsc == clb\n");
2788 
2789     if(!test_bscholder(clb))
2790         ret = FALSE;
2791 
2792     IBindStatusCallback_Release(clb);
2793 
2794     hres = RevokeBindStatusCallback(bindctx, (IBindStatusCallback*)&bsc);
2795     ok(hres == S_OK, "RevokeBindStatusCallback failed: %08x\n", hres);
2796 
2797     unk = (void*)0xdeadbeef;
2798     hres = IBindCtx_GetObjectParam(bindctx, BSCBHolder, &unk);
2799     ok(hres == E_FAIL, "GetObjectParam failed: %08x\n", hres);
2800     ok(unk == NULL, "unk != NULL\n");
2801 
2802     if(unk)
2803         IUnknown_Release(unk);
2804 
2805     hres = RevokeBindStatusCallback(bindctx, (void*)0xdeadbeef);
2806     ok(hres == S_OK, "RevokeBindStatusCallback failed: %08x\n", hres);
2807 
2808     hres = RevokeBindStatusCallback(NULL, (void*)0xdeadbeef);
2809     ok(hres == E_INVALIDARG, "RevokeBindStatusCallback failed: %08x\n", hres);
2810 
2811     hres = RevokeBindStatusCallback(bindctx, NULL);
2812     ok(hres == E_INVALIDARG, "RevokeBindStatusCallback failed: %08x\n", hres);
2813 
2814     SET_EXPECT(QueryInterface_IServiceProvider);
2815     prevbsc = (void*)0xdeadbeef;
2816     hres = RegisterBindStatusCallback(bindctx, (IBindStatusCallback*)&bsc, &prevbsc, 0);
2817     ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
2818     ok(!prevbsc, "prevbsc=%p\n", prevbsc);
2819     CHECK_CALLED(QueryInterface_IServiceProvider);
2820 
2821     hres = IBindCtx_GetObjectParam(bindctx, BSCBHolder, &unk);
2822     ok(hres == S_OK, "GetObjectParam failed: %08x\n", hres);
2823 
2824     hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)&prev_clb);
2825     IUnknown_Release(unk);
2826     ok(hres == S_OK, "QueryInterface(IID_IBindStatusCallback) failed: %08x\n", hres);
2827     ok(prev_clb != (IBindStatusCallback*)&bsc, "bsc == clb\n");
2828 
2829     SET_EXPECT(QueryInterface_IServiceProvider);
2830     prevbsc = (void*)0xdeadbeef;
2831     hres = RegisterBindStatusCallback(bindctx, (IBindStatusCallback*)&bsc2, &prevbsc, 0);
2832     ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
2833     ok(prevbsc == (IBindStatusCallback*)&bsc, "prevbsc != bsc\n");
2834     CHECK_CALLED(QueryInterface_IServiceProvider);
2835 
2836     hres = IBindCtx_GetObjectParam(bindctx, BSCBHolder, &unk);
2837     ok(hres == S_OK, "GetObjectParam failed: %08x\n", hres);
2838 
2839     hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)&clb);
2840     IUnknown_Release(unk);
2841     ok(hres == S_OK, "QueryInterface(IID_IBindStatusCallback) failed: %08x\n", hres);
2842     ok(prev_clb  == clb, "bsc != clb\n");
2843 
2844     IBindStatusCallback_Release(clb);
2845     IBindStatusCallback_Release(prev_clb);
2846 
2847     IBindCtx_Release(bindctx);
2848 
2849     strict_bsc_qi = FALSE;
2850     return ret;
2851 }
2852 
2853 #define BINDTEST_EMULATE            0x0001
2854 #define BINDTEST_TOOBJECT           0x0002
2855 #define BINDTEST_FILEDWLAPI         0x0004
2856 #define BINDTEST_HTTPRESPONSE       0x0008
2857 #define BINDTEST_REDIRECT           0x0010
2858 #define BINDTEST_USE_CACHE          0x0020
2859 #define BINDTEST_NO_CALLBACK_READ   0x0040
2860 #define BINDTEST_NO_CALLBACK        0x0080
2861 #define BINDTEST_ABORT              0x0100
2862 #define BINDTEST_INVALID_CN         0x0200
2863 #define BINDTEST_ABORT_START        0x0400
2864 #define BINDTEST_ABORT_PROGRESS     0x0800
2865 #define BINDTEST_ASYNC_SWITCH       0x1000
2866 #define BINDTEST_ALLOW_FINDINGRESOURCE  0x2000
2867 
2868 static void init_bind_test(int protocol, DWORD flags, DWORD t)
2869 {
2870     const char *url_a = NULL;
2871 
2872     test_protocol = protocol;
2873     bindtest_flags = flags;
2874     emulate_protocol = (flags & BINDTEST_EMULATE) != 0;
2875     download_state = BEFORE_DOWNLOAD;
2876     stopped_binding = FALSE;
2877     stopped_obj_binding = FALSE;
2878     data_available = FALSE;
2879     mime_type[0] = 0;
2880     binding_hres = S_OK;
2881     bind_to_object = (flags & BINDTEST_TOOBJECT) != 0;
2882     tymed = t;
2883     filedwl_api = (flags & BINDTEST_FILEDWLAPI) != 0;
2884     post_test = (flags & BINDTEST_HTTPRESPONSE) != 0;
2885 
2886     switch(protocol) {
2887     case HTTP_TEST:
2888         if(post_test)
2889             url_a = "http://test.winehq.org/tests/post.php";
2890         else
2891             lstrcpyW(current_url, winetest_data_urlW);
2892         break;
2893     case ABOUT_TEST:
2894         url_a = "about:blank";
2895         break;
2896     case FILE_TEST:
2897         lstrcpyW(current_url, file_url);
2898         break;
2899     case MK_TEST:
2900         url_a = "mk:@MSITStore:test.chm::/blank.html";
2901         break;
2902     case ITS_TEST:
2903         url_a = "its:test.chm::/blank.html";
2904         break;
2905     case HTTPS_TEST:
2906         url_a = (flags & BINDTEST_INVALID_CN) ? "https://4.15.184.77/favicon.ico" : "https://test.winehq.org/tests/hello.html";
2907         break;
2908     case FTP_TEST:
2909         url_a = "ftp://ftp.winehq.org/welcome%2emsg";
2910         break;
2911     default:
2912         url_a = "winetest:test";
2913     }
2914 
2915     if(url_a)
2916         MultiByteToWideChar(CP_ACP, 0, url_a, -1, current_url, ARRAY_SIZE(current_url));
2917 
2918     test_redirect = (flags & BINDTEST_REDIRECT) != 0;
2919     use_cache_file = (flags & BINDTEST_USE_CACHE) != 0;
2920     callback_read = !(flags & BINDTEST_NO_CALLBACK_READ);
2921     no_callback = (flags & BINDTEST_NO_CALLBACK) != 0;
2922     test_abort = (flags & BINDTEST_ABORT) != 0;
2923     abort_start = (flags & BINDTEST_ABORT_START) != 0;
2924     abort_progress = (flags & BINDTEST_ABORT_PROGRESS) != 0;
2925     async_switch = (flags & BINDTEST_ASYNC_SWITCH) != 0;
2926     is_async_prot = protocol == HTTP_TEST || protocol == HTTPS_TEST || protocol == FTP_TEST || protocol == WINETEST_TEST;
2927     prot_state = 0;
2928     ResetEvent(complete_event);
2929 
2930     trace("URL: %s\n", wine_dbgstr_w(current_url));
2931 }
2932 
2933 static void test_BindToStorage(int protocol, DWORD flags, DWORD t)
2934 {
2935     IMoniker *mon;
2936     HRESULT hres;
2937     LPOLESTR display_name;
2938     IBindCtx *bctx = NULL;
2939     MSG msg;
2940     IBindStatusCallback *previousclb;
2941     IUnknown *unk = (IUnknown*)0x00ff00ff;
2942     BOOL allow_finding_resource;
2943     IBinding *bind;
2944 
2945     init_bind_test(protocol, flags, t);
2946     allow_finding_resource = (flags & BINDTEST_ALLOW_FINDINGRESOURCE) != 0;
2947 
2948     if(no_callback) {
2949         hres = CreateBindCtx(0, &bctx);
2950         ok(hres == S_OK, "CreateBindCtx failed: %08x\n", hres);
2951     }else {
2952         SET_EXPECT(QueryInterface_IServiceProvider);
2953         hres = CreateAsyncBindCtx(0, (IBindStatusCallback*)&bsc, NULL, &bctx);
2954         ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n\n", hres);
2955         CHECK_CALLED(QueryInterface_IServiceProvider);
2956         if(FAILED(hres))
2957             return;
2958 
2959         SET_EXPECT(QueryInterface_IServiceProvider);
2960         hres = RegisterBindStatusCallback(bctx, (IBindStatusCallback*)&bsc, &previousclb, 0);
2961         ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
2962         ok(previousclb == (IBindStatusCallback*)&bsc, "previousclb(%p) != sclb(%p)\n", previousclb, &bsc);
2963         CHECK_CALLED(QueryInterface_IServiceProvider);
2964         if(previousclb)
2965             IBindStatusCallback_Release(previousclb);
2966     }
2967 
2968     hres = CreateURLMoniker(NULL, current_url, &mon);
2969     ok(hres == S_OK, "failed to create moniker: %08x\n", hres);
2970     if(FAILED(hres))
2971         return;
2972 
2973     if(protocol == FTP_TEST)
2974     {
2975         /* FTP urls don't have any escape characters so convert the url to what is expected */
2976         DWORD size = 0;
2977         UrlUnescapeW(current_url, NULL, &size, URL_UNESCAPE_INPLACE);
2978     }
2979 
2980     hres = IMoniker_QueryInterface(mon, &IID_IBinding, (void**)&bind);
2981     ok(hres == E_NOINTERFACE, "IMoniker should not have IBinding interface\n");
2982     if(SUCCEEDED(hres))
2983         IBinding_Release(bind);
2984 
2985     hres = IMoniker_GetDisplayName(mon, bctx, NULL, &display_name);
2986     ok(hres == S_OK, "GetDisplayName failed %08x\n", hres);
2987     ok(!lstrcmpW(display_name, current_url), "GetDisplayName got wrong name %s, expected %s\n",
2988        wine_dbgstr_w(display_name), wine_dbgstr_w(current_url));
2989     CoTaskMemFree(display_name);
2990 
2991     if(tymed == TYMED_FILE && (test_protocol == ABOUT_TEST || test_protocol == ITS_TEST))
2992         binding_hres = INET_E_DATA_NOT_AVAILABLE;
2993     if((flags & BINDTEST_INVALID_CN) && !invalid_cn_accepted &&
2994        (onsecurityproblem_hres != S_OK || security_problem == ERROR_INTERNET_SEC_CERT_ERRORS)) {
2995         if(security_problem == ERROR_INTERNET_SEC_CERT_ERRORS)
2996             binding_hres = INET_E_SECURITY_PROBLEM;
2997         else
2998             binding_hres = INET_E_INVALID_CERTIFICATE;
2999     }
3000 
3001 
3002     if(only_check_prot_args)
3003         SET_EXPECT(OnStopBinding);
3004     if(!no_callback) {
3005         SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
3006         SET_EXPECT(GetBindInfo);
3007         SET_EXPECT(QueryInterface_IInternetProtocol);
3008         if(!emulate_protocol)
3009             SET_EXPECT(QueryService_IInternetProtocol);
3010         SET_EXPECT(OnStartBinding);
3011     }
3012     if(emulate_protocol) {
3013         if(is_urlmon_protocol(test_protocol))
3014             SET_EXPECT(SetPriority);
3015         SET_EXPECT(Start);
3016         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST
3017            || test_protocol == WINETEST_SYNC_TEST)
3018             SET_EXPECT(Terminate);
3019         if(tymed != TYMED_FILE || (test_protocol != ABOUT_TEST && test_protocol != ITS_TEST))
3020             SET_EXPECT(UnlockRequest);
3021     }else {
3022         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST) {
3023             SET_EXPECT(QueryService_IInternetBindInfo);
3024             if(!abort_start)
3025                 SET_EXPECT(QueryInterface_IHttpNegotiate);
3026             SET_EXPECT(QueryInterface_IWindowForBindingUI);
3027             SET_EXPECT(QueryService_IWindowForBindingUI);
3028             SET_EXPECT(GetWindow_IWindowForBindingUI);
3029             if(!abort_start) {
3030                 SET_EXPECT(BeginningTransaction);
3031                 SET_EXPECT(QueryInterface_IHttpNegotiate2);
3032                 SET_EXPECT(GetRootSecurityId);
3033                 if(http_is_first || allow_finding_resource)
3034                     SET_EXPECT(OnProgress_FINDINGRESOURCE);
3035                 SET_EXPECT(OnProgress_CONNECTING);
3036             }
3037             if(flags & BINDTEST_INVALID_CN) {
3038                 SET_EXPECT(QueryInterface_IHttpSecurity);
3039                 SET_EXPECT(QueryService_IHttpSecurity);
3040                 SET_EXPECT(OnSecurityProblem);
3041                 if(SUCCEEDED(onsecurityproblem_hres))
3042                     SET_EXPECT(GetWindow_IHttpSecurity);
3043             }
3044         }
3045         if(!no_callback) {
3046             if((test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == FTP_TEST
3047                || test_protocol == FILE_TEST || test_protocol == WINETEST_TEST) && !abort_start)
3048                 SET_EXPECT(OnProgress_SENDINGREQUEST);
3049             if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST) {
3050                 SET_EXPECT(QueryInterface_IHttpNegotiate);
3051                 SET_EXPECT(OnResponse);
3052             }
3053             if(!abort_start) {
3054                 SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
3055                 SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
3056             }
3057             if(test_protocol == FILE_TEST)
3058                 SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
3059             if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == FTP_TEST || test_protocol == WINETEST_TEST)
3060                 SET_EXPECT(OnProgress_DOWNLOADINGDATA);
3061             if(!abort_start)
3062                 SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
3063             if((tymed != TYMED_FILE || test_protocol != ABOUT_TEST) && !abort_start)
3064                 SET_EXPECT(OnDataAvailable);
3065             SET_EXPECT(OnStopBinding);
3066         }
3067     }
3068 
3069     hres = IMoniker_BindToStorage(mon, bctx, NULL, tymed == TYMED_ISTREAM ? &IID_IStream : &IID_IUnknown, (void**)&unk);
3070     if ((test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3071         && hres == HRESULT_FROM_WIN32(ERROR_INTERNET_NAME_NOT_RESOLVED))
3072     {
3073         skip("Network unreachable, skipping tests\n");
3074         return;
3075     }
3076 
3077     if(only_check_prot_args) {
3078         ok(hres == E_FAIL, "Got %08x\n", hres);
3079         CHECK_CALLED(OnStopBinding);
3080     } else if(abort_start)
3081         ok(hres == abort_hres, "IMoniker_BindToStorage failed: %08x, expected %08x\n", hres, abort_hres);
3082     else if(abort_progress)
3083         ok(hres == MK_S_ASYNCHRONOUS, "IMoniker_BindToStorage failed: %08x\n", hres);
3084     else if(no_callback) {
3085         if(emulate_protocol)
3086             ok( WaitForSingleObject(complete_event2, 90000) == WAIT_OBJECT_0, "wait timed out\n" );
3087         ok(hres == S_OK, "IMoniker_BindToStorage failed: %08x\n", hres);
3088         ok(unk != NULL, "unk == NULL\n");
3089     }else if(!(bindf & BINDF_ASYNCHRONOUS) && tymed == TYMED_FILE) {
3090         ok(hres == S_OK, "IMoniker_BindToStorage failed: %08x\n", hres);
3091         ok(unk == NULL, "unk != NULL\n");
3092     }else if(((bindf & BINDF_ASYNCHRONOUS) && !data_available)
3093        || (tymed == TYMED_FILE && test_protocol == FILE_TEST)) {
3094         ok(hres == MK_S_ASYNCHRONOUS, "IMoniker_BindToStorage failed: %08x\n", hres);
3095         ok(unk == NULL, "istr should be NULL\n");
3096     }else if(tymed == TYMED_FILE && test_protocol == ABOUT_TEST) {
3097         ok(hres == INET_E_DATA_NOT_AVAILABLE,
3098            "IMoniker_BindToStorage failed: %08x, expected INET_E_DATA_NOT_AVAILABLE\n", hres);
3099         ok(unk == NULL, "istr should be NULL\n");
3100     }else if((flags & BINDTEST_INVALID_CN) && binding_hres != S_OK) {
3101         ok(hres == binding_hres, "Got %08x\n", hres);
3102         ok(unk == NULL, "Got %p\n", unk);
3103     }else if((flags & BINDTEST_INVALID_CN) && invalid_cn_accepted) {
3104         ok(hres == S_OK, "IMoniker_BindToStorage failed: %08x\n", hres);
3105         ok(unk != NULL, "unk == NULL\n");
3106         if(unk == NULL) {
3107             ok(0, "Expected security problem to be ignored.\n");
3108             invalid_cn_accepted = FALSE;
3109             binding_hres = INET_E_INVALID_CERTIFICATE;
3110         }
3111     }else {
3112         ok(hres == S_OK, "IMoniker_BindToStorage failed: %08x\n", hres);
3113         ok(unk != NULL, "unk == NULL\n");
3114     }
3115     if(unk && callback_read && !no_callback) {
3116         IUnknown_Release(unk);
3117         unk = NULL;
3118     }
3119 
3120     if(FAILED(hres) && !(flags & BINDTEST_INVALID_CN) && !(flags & BINDTEST_ABORT_START))
3121         return;
3122 
3123     if((bindf & BINDF_ASYNCHRONOUS) && !no_callback) {
3124         while(!stopped_binding && GetMessageA(&msg,NULL,0,0)) {
3125             TranslateMessage(&msg);
3126             DispatchMessageA(&msg);
3127         }
3128     }
3129 
3130     if(async_switch) {
3131         CHECK_CALLED(OnProgress_SENDINGREQUEST);
3132         CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
3133         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
3134         CHECK_CALLED(LockRequest);
3135         CHECK_CALLED(OnStopBinding);
3136     }
3137     if(!no_callback) {
3138         CLEAR_CALLED(QueryInterface_IBindStatusCallbackEx); /* IE 8 */
3139         CHECK_CALLED(GetBindInfo);
3140         todo_wine_if(abort_start)
3141             CHECK_CALLED(QueryInterface_IInternetProtocol);
3142         if(!emulate_protocol) {
3143             todo_wine_if(abort_start)
3144                 CHECK_CALLED(QueryService_IInternetProtocol);
3145         }
3146         CHECK_CALLED(OnStartBinding);
3147     }
3148     if(emulate_protocol) {
3149         if(is_urlmon_protocol(test_protocol))
3150             CLEAR_CALLED(SetPriority); /* Not called by IE11 */
3151         CHECK_CALLED(Start);
3152         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST
3153            || test_protocol == WINETEST_SYNC_TEST) {
3154             if(tymed == TYMED_FILE)
3155                 CLEAR_CALLED(Read);
3156             CHECK_CALLED(Terminate);
3157         }
3158         if(tymed != TYMED_FILE || (test_protocol != ABOUT_TEST && test_protocol != ITS_TEST))
3159             CHECK_CALLED(UnlockRequest);
3160     }else {
3161         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST) {
3162             CLEAR_CALLED(QueryService_IInternetBindInfo);
3163             if(!abort_start)
3164                 CHECK_CALLED(QueryInterface_IHttpNegotiate);
3165             CLEAR_CALLED(QueryInterface_IWindowForBindingUI);
3166             CLEAR_CALLED(QueryService_IWindowForBindingUI);
3167             CLEAR_CALLED(GetWindow_IWindowForBindingUI);
3168             if(!abort_start)
3169                 CHECK_CALLED(BeginningTransaction);
3170             if (have_IHttpNegotiate2 && !abort_start)
3171             {
3172                 CHECK_CALLED(QueryInterface_IHttpNegotiate2);
3173                 CHECK_CALLED(GetRootSecurityId);
3174             }
3175             if(http_is_first) {
3176                 if (! proxy_active())
3177                 {
3178                     CHECK_CALLED(OnProgress_FINDINGRESOURCE);
3179                     CHECK_CALLED(OnProgress_CONNECTING);
3180                 }
3181                 else
3182                 {
3183                     CLEAR_CALLED(OnProgress_FINDINGRESOURCE);
3184                     CLEAR_CALLED(OnProgress_CONNECTING);
3185                 }
3186             }else if(!abort_start) {
3187                 if(allow_finding_resource)
3188                     CLEAR_CALLED(OnProgress_FINDINGRESOURCE);
3189                 /* IE7 does call this */
3190                 CLEAR_CALLED(OnProgress_CONNECTING);
3191             }
3192             if((flags & BINDTEST_INVALID_CN) && !invalid_cn_accepted)  {
3193                 CHECK_CALLED(QueryInterface_IHttpSecurity);
3194                 CHECK_CALLED(QueryService_IHttpSecurity);
3195                 CHECK_CALLED(OnSecurityProblem);
3196             }else {
3197                 CHECK_NOT_CALLED(QueryInterface_IHttpSecurity);
3198                 CHECK_NOT_CALLED(QueryService_IHttpSecurity);
3199                 CHECK_NOT_CALLED(OnSecurityProblem);
3200             }
3201         }
3202         if(!no_callback) {
3203             if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == FILE_TEST || test_protocol == WINETEST_TEST) {
3204                 if(flags & BINDTEST_INVALID_CN)
3205                     CLEAR_CALLED(OnProgress_SENDINGREQUEST);
3206                 else if(!abort_start)
3207                     CHECK_CALLED(OnProgress_SENDINGREQUEST);
3208             } else if(test_protocol == FTP_TEST)
3209                 todo_wine CHECK_CALLED(OnProgress_SENDINGREQUEST);
3210             if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == WINETEST_TEST) {
3211                 CLEAR_CALLED(QueryInterface_IHttpNegotiate);
3212                 if((!(flags & BINDTEST_INVALID_CN) || (binding_hres == S_OK)) && !abort_start) {
3213                     CHECK_CALLED(OnResponse);
3214                 }
3215             }
3216             if((!(flags & BINDTEST_INVALID_CN) || binding_hres == S_OK) && !abort_start) {
3217                 CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
3218                 CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
3219                 CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
3220             }
3221             if(test_protocol == FILE_TEST)
3222                 CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
3223             if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == FTP_TEST  || test_protocol == WINETEST_TEST)
3224                 CLEAR_CALLED(OnProgress_DOWNLOADINGDATA);
3225             if((flags & BINDTEST_INVALID_CN)) {
3226                 if(binding_hres == S_OK)
3227                     CHECK_CALLED(OnDataAvailable);
3228                 else
3229                     CHECK_NOT_CALLED(OnDataAvailable);
3230             }else if((tymed != TYMED_FILE || test_protocol != ABOUT_TEST) && !abort_start)
3231                 CHECK_CALLED(OnDataAvailable);
3232             CHECK_CALLED(OnStopBinding);
3233         }
3234     }
3235 
3236     ok(IMoniker_Release(mon) == 0, "mon should be destroyed here\n");
3237     if(bctx)
3238         ok(IBindCtx_Release(bctx) == 0, "bctx should be destroyed here\n");
3239 
3240     if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3241         http_is_first = FALSE;
3242 
3243     if((flags & BINDTEST_INVALID_CN) && onsecurityproblem_hres == S_OK && security_problem != ERROR_INTERNET_SEC_CERT_ERRORS)
3244         invalid_cn_accepted = TRUE;
3245 
3246     if(unk) {
3247         BYTE buf[512];
3248         DWORD readed;
3249         IStream *stream;
3250 
3251         hres = IUnknown_QueryInterface(unk, &IID_IStream, (void**)&stream);
3252         ok(hres == S_OK, "Could not get IStream iface: %08x\n", hres);
3253         IUnknown_Release(unk);
3254 
3255         do {
3256             readed = 0xdeadbeef;
3257             hres = IStream_Read(stream, buf, sizeof(buf), &readed);
3258             ok(readed != 0xdeadbeef, "readed = 0xdeadbeef\n");
3259             if(emulate_protocol && test_protocol == HTTP_TEST && readed)
3260                 ok(buf[0] == (use_cache_file && !(bindf&BINDF_ASYNCHRONOUS) ? 'X' : '?'), "buf[0] = '%c'\n", buf[0]);
3261         }while(hres == S_OK);
3262         ok(hres == S_FALSE, "IStream_Read returned %08x\n", hres);
3263         ok(!readed, "readed = %d\n", readed);
3264 
3265         IStream_Release(stream);
3266     }
3267 }
3268 
3269 static void test_BindToObject(int protocol, DWORD flags, HRESULT exhres)
3270 {
3271     IMoniker *mon;
3272     HRESULT hres;
3273     LPOLESTR display_name;
3274     IBindCtx *bctx;
3275     DWORD regid;
3276     MSG msg;
3277     IUnknown *unk = (IUnknown*)0x00ff00ff;
3278     IBinding *bind;
3279 
3280     init_bind_test(protocol, BINDTEST_TOOBJECT|flags, TYMED_ISTREAM);
3281     binding_hres = exhres;
3282 
3283     if(emulate_protocol)
3284         CoRegisterClassObject(&CLSID_HTMLDocument, (IUnknown *)&mime_cf,
3285                               CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
3286 
3287     SET_EXPECT(QueryInterface_IServiceProvider);
3288     hres = CreateAsyncBindCtx(0, (IBindStatusCallback*)&objbsc, NULL, &bctx);
3289     ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n\n", hres);
3290     CHECK_CALLED(QueryInterface_IServiceProvider);
3291     if(FAILED(hres))
3292         return;
3293 
3294     hres = CreateURLMoniker(NULL, current_url, &mon);
3295     ok(hres == S_OK, "failed to create moniker: %08x\n", hres);
3296     if(FAILED(hres)) {
3297         IBindCtx_Release(bctx);
3298         return;
3299     }
3300 
3301     hres = IMoniker_QueryInterface(mon, &IID_IBinding, (void**)&bind);
3302     ok(hres == E_NOINTERFACE, "IMoniker should not have IBinding interface\n");
3303     if(SUCCEEDED(hres))
3304         IBinding_Release(bind);
3305 
3306     hres = IMoniker_GetDisplayName(mon, bctx, NULL, &display_name);
3307     ok(hres == S_OK, "GetDisplayName failed %08x\n", hres);
3308     ok(!lstrcmpW(display_name, current_url), "GetDisplayName got wrong name\n");
3309     CoTaskMemFree(display_name);
3310 
3311     SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
3312     SET_EXPECT(Obj_GetBindInfo);
3313     SET_EXPECT(QueryInterface_IInternetProtocol);
3314     if(!emulate_protocol)
3315         SET_EXPECT(QueryService_IInternetProtocol);
3316     SET_EXPECT(Obj_OnStartBinding);
3317     if(emulate_protocol) {
3318         if(is_urlmon_protocol(test_protocol))
3319             SET_EXPECT(SetPriority);
3320         SET_EXPECT(Start);
3321         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3322             SET_EXPECT(Terminate);
3323         if(test_protocol == FILE_TEST)
3324             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
3325         SET_EXPECT(UnlockRequest);
3326     }else {
3327         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3328             SET_EXPECT(QueryInterface_IHttpNegotiate);
3329             SET_EXPECT(BeginningTransaction);
3330             SET_EXPECT(QueryInterface_IHttpNegotiate2);
3331             SET_EXPECT(GetRootSecurityId);
3332             if(http_is_first)
3333                 SET_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
3334             SET_EXPECT(Obj_OnProgress_CONNECTING);
3335             SET_EXPECT(QueryInterface_IWindowForBindingUI);
3336             SET_EXPECT(QueryService_IWindowForBindingUI);
3337             SET_EXPECT(GetWindow_IWindowForBindingUI);
3338         }
3339         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == FILE_TEST)
3340             SET_EXPECT(Obj_OnProgress_SENDINGREQUEST);
3341         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3342             SET_EXPECT(QueryInterface_IHttpNegotiate);
3343             SET_EXPECT(OnResponse);
3344         }
3345         SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
3346         SET_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
3347         if(test_protocol == FILE_TEST)
3348             SET_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
3349         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3350             SET_EXPECT(OnProgress_DOWNLOADINGDATA);
3351         SET_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
3352         if(SUCCEEDED(hres))
3353             SET_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
3354         SET_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
3355         if(exhres == REGDB_E_CLASSNOTREG) {
3356             SET_EXPECT(QueryInterface_IWindowForBindingUI);
3357             SET_EXPECT(QueryService_IWindowForBindingUI);
3358             SET_EXPECT(GetWindow_ICodeInstall);
3359         }
3360         SET_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
3361         if(SUCCEEDED(hres))
3362             SET_EXPECT(OnObjectAvailable);
3363         SET_EXPECT(Obj_OnStopBinding);
3364     }
3365 
3366     hres = IMoniker_BindToObject(mon, bctx, NULL, &IID_IUnknown, (void**)&unk);
3367 
3368     if ((test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3369         && hres == HRESULT_FROM_WIN32(ERROR_INTERNET_NAME_NOT_RESOLVED))
3370     {
3371         skip( "Network unreachable, skipping tests\n" );
3372         return;
3373     }
3374 
3375     if(FAILED(exhres)) {
3376         ok(hres == exhres, "BindToObject failed:  %08x, expected %08x\n", hres, exhres);
3377         ok(!unk, "unk = %p, expected NULL\n", unk);
3378     }else if(bindf & BINDF_ASYNCHRONOUS) {
3379         ok(hres == MK_S_ASYNCHRONOUS, "IMoniker_BindToObject failed: %08x\n", hres);
3380         ok(unk == NULL, "istr should be NULL\n");
3381     }else {
3382         ok(hres == S_OK, "IMoniker_BindToStorage failed: %08x\n", hres);
3383         ok(unk != NULL, "unk == NULL\n");
3384         if(emulate_protocol)
3385             ok(unk == (IUnknown*)&PersistMoniker, "unk != PersistMoniker\n");
3386     }
3387     if(unk)
3388         IUnknown_Release(unk);
3389 
3390     while((bindf & BINDF_ASYNCHRONOUS) &&
3391           !((!emulate_protocol || stopped_binding) && stopped_obj_binding) && GetMessageA(&msg,NULL,0,0)) {
3392         TranslateMessage(&msg);
3393         DispatchMessageA(&msg);
3394     }
3395 
3396     CLEAR_CALLED(QueryInterface_IBindStatusCallbackEx);
3397     CHECK_CALLED(Obj_GetBindInfo);
3398     CHECK_CALLED(QueryInterface_IInternetProtocol);
3399     if(!emulate_protocol)
3400         CHECK_CALLED(QueryService_IInternetProtocol);
3401     CHECK_CALLED(Obj_OnStartBinding);
3402     if(emulate_protocol) {
3403         if(is_urlmon_protocol(test_protocol))
3404             CLEAR_CALLED(SetPriority); /* Not called by IE11 */
3405         CHECK_CALLED(Start);
3406         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3407             CHECK_CALLED(Terminate);
3408         if(test_protocol == FILE_TEST)
3409             CLEAR_CALLED(OnProgress_MIMETYPEAVAILABLE); /* not called in IE7 */
3410         CHECK_CALLED(UnlockRequest);
3411     }else {
3412         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3413             CHECK_CALLED(QueryInterface_IHttpNegotiate);
3414             CHECK_CALLED(BeginningTransaction);
3415             if (have_IHttpNegotiate2)
3416             {
3417                 CHECK_CALLED(QueryInterface_IHttpNegotiate2);
3418                 CHECK_CALLED(GetRootSecurityId);
3419             }
3420             if(http_is_first) {
3421                 CHECK_CALLED(Obj_OnProgress_FINDINGRESOURCE);
3422                 CHECK_CALLED(Obj_OnProgress_CONNECTING);
3423             }else {
3424                 /* IE7 does call this */
3425                 CLEAR_CALLED(Obj_OnProgress_CONNECTING);
3426             }
3427             CLEAR_CALLED(QueryInterface_IWindowForBindingUI);
3428             CLEAR_CALLED(QueryService_IWindowForBindingUI);
3429             CLEAR_CALLED(GetWindow_IWindowForBindingUI);
3430         }
3431         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == FILE_TEST) {
3432             if(post_test)
3433                 CLEAR_CALLED(Obj_OnProgress_SENDINGREQUEST);
3434             else
3435                 CHECK_CALLED(Obj_OnProgress_SENDINGREQUEST);
3436         }
3437         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3438             CLEAR_CALLED(QueryInterface_IHttpNegotiate);
3439             CHECK_CALLED(OnResponse);
3440         }
3441         CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
3442         CHECK_CALLED(Obj_OnProgress_BEGINDOWNLOADDATA);
3443         if(test_protocol == FILE_TEST)
3444             CHECK_CALLED(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
3445         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3446             CLEAR_CALLED(OnProgress_DOWNLOADINGDATA);
3447         CLEAR_CALLED(Obj_OnProgress_ENDDOWNLOADDATA);
3448         if(SUCCEEDED(hres))
3449             CHECK_CALLED(Obj_OnProgress_CLASSIDAVAILABLE);
3450         CHECK_CALLED(Obj_OnProgress_BEGINSYNCOPERATION);
3451         if(exhres == REGDB_E_CLASSNOTREG) {
3452             todo_wine CHECK_CALLED(QueryInterface_IWindowForBindingUI);
3453             todo_wine CHECK_CALLED(QueryService_IWindowForBindingUI);
3454             todo_wine CHECK_CALLED(GetWindow_ICodeInstall);
3455         }
3456         CHECK_CALLED(Obj_OnProgress_ENDSYNCOPERATION);
3457         if(SUCCEEDED(hres))
3458             CHECK_CALLED(OnObjectAvailable);
3459         CHECK_CALLED(Obj_OnStopBinding);
3460     }
3461 
3462     ok(IMoniker_Release(mon) == 0, "mon should be destroyed here\n");
3463     IBindCtx_Release(bctx);
3464 
3465     if(emulate_protocol)
3466         CoRevokeClassObject(regid);
3467 
3468     if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3469         http_is_first = FALSE;
3470 }
3471 
3472 static void test_URLDownloadToFile(DWORD prot, BOOL emul)
3473 {
3474     BOOL res;
3475     HRESULT hres;
3476 
3477     init_bind_test(prot, BINDTEST_FILEDWLAPI | (emul ? BINDTEST_EMULATE : 0), TYMED_FILE);
3478 
3479     SET_EXPECT(GetBindInfo);
3480     SET_EXPECT(QueryInterface_IInternetProtocol);
3481     SET_EXPECT(QueryInterface_IServiceProvider);
3482     if(!emulate_protocol)
3483         SET_EXPECT(QueryService_IInternetProtocol);
3484     SET_EXPECT(OnStartBinding);
3485     if(emulate_protocol) {
3486         if(is_urlmon_protocol(test_protocol))
3487             SET_EXPECT(SetPriority);
3488         SET_EXPECT(Start);
3489         SET_EXPECT(UnlockRequest);
3490     }else {
3491         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3492             SET_EXPECT(QueryInterface_IHttpNegotiate);
3493             SET_EXPECT(BeginningTransaction);
3494             SET_EXPECT(QueryInterface_IHttpNegotiate2);
3495             SET_EXPECT(GetRootSecurityId);
3496             SET_EXPECT(QueryInterface_IWindowForBindingUI);
3497             SET_EXPECT(OnProgress_CONNECTING);
3498         }
3499         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST || test_protocol == FILE_TEST)
3500             SET_EXPECT(OnProgress_SENDINGREQUEST);
3501         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3502             SET_EXPECT(QueryInterface_IHttpNegotiate);
3503             SET_EXPECT(OnResponse);
3504         }
3505         SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
3506         SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
3507         if(test_protocol == FILE_TEST)
3508             SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
3509         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3510             SET_EXPECT(OnProgress_DOWNLOADINGDATA);
3511         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
3512         SET_EXPECT(OnStopBinding);
3513     }
3514 
3515     hres = URLDownloadToFileW(NULL, current_url, dwl_htmlW, 0, (IBindStatusCallback*)&bsc);
3516     ok(hres == S_OK, "URLDownloadToFile failed: %08x\n", hres);
3517 
3518     CHECK_CALLED(GetBindInfo);
3519     CHECK_CALLED(QueryInterface_IInternetProtocol);
3520     if(!emulate_protocol) {
3521         CHECK_CALLED(QueryInterface_IServiceProvider);
3522         CHECK_CALLED(QueryService_IInternetProtocol);
3523     }else {
3524         CLEAR_CALLED(QueryInterface_IServiceProvider);
3525     }
3526     CHECK_CALLED(OnStartBinding);
3527     if(emulate_protocol) {
3528         if(is_urlmon_protocol(test_protocol))
3529             CLEAR_CALLED(SetPriority); /* Not called by IE11 */
3530         CHECK_CALLED(Start);
3531         CHECK_CALLED(UnlockRequest);
3532     }else {
3533         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3534             CHECK_CALLED(QueryInterface_IHttpNegotiate);
3535             CHECK_CALLED(BeginningTransaction);
3536             if (have_IHttpNegotiate2)
3537             {
3538                 CHECK_CALLED(QueryInterface_IHttpNegotiate2);
3539                 CHECK_CALLED(GetRootSecurityId);
3540             }
3541             CLEAR_CALLED(QueryInterface_IWindowForBindingUI);
3542             CLEAR_CALLED(OnProgress_CONNECTING);
3543         }
3544         if(test_protocol == FILE_TEST)
3545             CHECK_CALLED(OnProgress_SENDINGREQUEST);
3546         else if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3547             CLEAR_CALLED(OnProgress_SENDINGREQUEST); /* not called by IE7 */
3548         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST) {
3549             CLEAR_CALLED(QueryInterface_IHttpNegotiate);
3550             CHECK_CALLED(OnResponse);
3551         }
3552         CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
3553         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
3554         if(test_protocol == FILE_TEST)
3555             CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
3556         if(test_protocol == HTTP_TEST || test_protocol == HTTPS_TEST)
3557             CLEAR_CALLED(OnProgress_DOWNLOADINGDATA);
3558         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
3559         CHECK_CALLED(OnStopBinding);
3560     }
3561 
3562     res = DeleteFileA(dwl_htmlA);
3563     ok(res, "DeleteFile failed: %u\n", GetLastError());
3564 
3565     if(prot != FILE_TEST || emul)
3566         return;
3567 
3568     hres = URLDownloadToFileW(NULL, current_url, dwl_htmlW, 0, NULL);
3569     ok(hres == S_OK, "URLDownloadToFile failed: %08x\n", hres);
3570 
3571     res = DeleteFileA(dwl_htmlA);
3572     ok(res, "DeleteFile failed: %u\n", GetLastError());
3573 }
3574 
3575 static void test_URLDownloadToFile_abort(void)
3576 {
3577     HRESULT hres;
3578 
3579     init_bind_test(HTTP_TEST, BINDTEST_FILEDWLAPI|BINDTEST_ABORT_PROGRESS, TYMED_FILE);
3580 
3581     SET_EXPECT(GetBindInfo);
3582     SET_EXPECT(QueryInterface_IInternetProtocol);
3583     SET_EXPECT(QueryInterface_IServiceProvider);
3584     SET_EXPECT(QueryService_IInternetProtocol);
3585     SET_EXPECT(OnStartBinding);
3586     SET_EXPECT(QueryInterface_IHttpNegotiate);
3587     SET_EXPECT(QueryInterface_IHttpNegotiate2);
3588     SET_EXPECT(BeginningTransaction);
3589     SET_EXPECT(GetRootSecurityId);
3590     SET_EXPECT(QueryInterface_IWindowForBindingUI);
3591     SET_EXPECT(OnProgress_CONNECTING);
3592     SET_EXPECT(OnProgress_SENDINGREQUEST);
3593     SET_EXPECT(OnStopBinding);
3594 
3595     hres = URLDownloadToFileW(NULL, current_url, dwl_htmlW, 0, (IBindStatusCallback*)&bsc);
3596     ok(hres == E_ABORT, "URLDownloadToFile failed: %08x, expected E_ABORT\n", hres);
3597 
3598     CHECK_CALLED(GetBindInfo);
3599     CHECK_CALLED(QueryInterface_IInternetProtocol);
3600     CHECK_CALLED(QueryInterface_IServiceProvider);
3601     CHECK_CALLED(QueryService_IInternetProtocol);
3602     CHECK_CALLED(OnStartBinding);
3603     CHECK_CALLED(QueryInterface_IHttpNegotiate);
3604     CHECK_CALLED(QueryInterface_IHttpNegotiate2);
3605     CHECK_CALLED(BeginningTransaction);
3606     CHECK_CALLED(GetRootSecurityId);
3607     CLEAR_CALLED(QueryInterface_IWindowForBindingUI);
3608     CHECK_CALLED(OnProgress_SENDINGREQUEST);
3609     CLEAR_CALLED(OnProgress_CONNECTING);
3610     CHECK_CALLED(OnStopBinding);
3611 
3612     init_bind_test(HTTP_TEST, BINDTEST_FILEDWLAPI|BINDTEST_ABORT_START, TYMED_FILE);
3613 
3614     SET_EXPECT(GetBindInfo);
3615     SET_EXPECT(QueryInterface_IInternetProtocol);
3616     SET_EXPECT(QueryInterface_IServiceProvider);
3617     SET_EXPECT(QueryService_IInternetProtocol);
3618     SET_EXPECT(OnStartBinding);
3619     SET_EXPECT(OnStopBinding);
3620 
3621     abort_hres = E_ABORT;
3622     hres = URLDownloadToFileW(NULL, current_url, dwl_htmlW, 0, (IBindStatusCallback*)&bsc);
3623     ok(hres == E_ABORT, "URLDownloadToFile failed: %08x, expected E_ABORT\n", hres);
3624 
3625     CHECK_CALLED(GetBindInfo);
3626     todo_wine CHECK_CALLED(QueryInterface_IInternetProtocol);
3627     todo_wine CHECK_CALLED(QueryInterface_IServiceProvider);
3628     todo_wine CHECK_CALLED(QueryService_IInternetProtocol);
3629     CHECK_CALLED(OnStartBinding);
3630     CHECK_CALLED(OnStopBinding);
3631 
3632     init_bind_test(HTTP_TEST, BINDTEST_FILEDWLAPI|BINDTEST_ABORT_START, TYMED_FILE);
3633 
3634     SET_EXPECT(GetBindInfo);
3635     SET_EXPECT(QueryInterface_IInternetProtocol);
3636     SET_EXPECT(QueryInterface_IServiceProvider);
3637     SET_EXPECT(QueryService_IInternetProtocol);
3638     SET_EXPECT(OnStartBinding);
3639     SET_EXPECT(QueryInterface_IHttpNegotiate);
3640     SET_EXPECT(QueryInterface_IHttpNegotiate2);
3641     SET_EXPECT(BeginningTransaction);
3642     SET_EXPECT(GetRootSecurityId);
3643     SET_EXPECT(QueryInterface_IWindowForBindingUI);
3644     SET_EXPECT(OnResponse);
3645     SET_EXPECT(OnProgress_CONNECTING);
3646     SET_EXPECT(OnProgress_SENDINGREQUEST);
3647     SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
3648     SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
3649     SET_EXPECT(OnProgress_DOWNLOADINGDATA);
3650     SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
3651     SET_EXPECT(OnStopBinding);
3652 
3653     /* URLDownloadToFile doesn't abort if E_NOTIMPL is returned from the
3654      * IBindStatusCallback's OnStartBinding function.
3655      */
3656     abort_hres = E_NOTIMPL;
3657     hres = URLDownloadToFileW(NULL, current_url, dwl_htmlW, 0, (IBindStatusCallback*)&bsc);
3658     ok(hres == S_OK, "URLDownloadToFile failed: %08x\n", hres);
3659 
3660     CHECK_CALLED(GetBindInfo);
3661     CHECK_CALLED(QueryInterface_IInternetProtocol);
3662     CHECK_CALLED(QueryInterface_IServiceProvider);
3663     CHECK_CALLED(QueryService_IInternetProtocol);
3664     CHECK_CALLED(OnStartBinding);
3665     CHECK_CALLED(QueryInterface_IHttpNegotiate);
3666     CHECK_CALLED(QueryInterface_IHttpNegotiate2);
3667     CHECK_CALLED(BeginningTransaction);
3668     CHECK_CALLED(GetRootSecurityId);
3669     CLEAR_CALLED(QueryInterface_IWindowForBindingUI);
3670     CHECK_CALLED(OnResponse);
3671     CLEAR_CALLED(OnProgress_CONNECTING);
3672     CHECK_CALLED(OnProgress_SENDINGREQUEST);
3673     CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
3674     CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
3675     CHECK_CALLED(OnProgress_DOWNLOADINGDATA);
3676     CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
3677     CHECK_CALLED(OnStopBinding);
3678 
3679     DeleteFileA(dwl_htmlA);
3680 }
3681 
3682 static void set_file_url(char *path)
3683 {
3684     CHAR file_urlA[INTERNET_MAX_URL_LENGTH];
3685 
3686     lstrcpyA(file_urlA, "file://");
3687     lstrcatA(file_urlA, path);
3688     MultiByteToWideChar(CP_ACP, 0, file_urlA, -1, file_url, INTERNET_MAX_URL_LENGTH);
3689 }
3690 
3691 static void create_file(const char *file_name, const char *content)
3692 {
3693     HANDLE file;
3694     DWORD size;
3695     CHAR path[MAX_PATH];
3696 
3697     file = CreateFileA(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
3698             FILE_ATTRIBUTE_NORMAL, NULL);
3699     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
3700     if(file == INVALID_HANDLE_VALUE)
3701         return;
3702 
3703     if(test_file)
3704         DeleteFileA(test_file);
3705     test_file = file_name;
3706     WriteFile(file, content, strlen(content), &size, NULL);
3707     CloseHandle(file);
3708 
3709     GetCurrentDirectoryA(MAX_PATH, path);
3710     lstrcatA(path, "\\");
3711     lstrcatA(path, file_name);
3712     set_file_url(path);
3713 }
3714 
3715 static void create_html_file(void)
3716 {
3717     create_file(wszIndexHtmlA, "<HTML></HTML>");
3718 }
3719 
3720 static void create_cache_file(void)
3721 {
3722     char buf[6500], curdir[MAX_PATH];
3723     HANDLE file;
3724     DWORD size;
3725 
3726     file = CreateFileA(test_txtA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
3727             FILE_ATTRIBUTE_NORMAL, NULL);
3728     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
3729     if(file == INVALID_HANDLE_VALUE)
3730         return;
3731 
3732     memset(buf, 'X', sizeof(buf));
3733     WriteFile(file, buf, sizeof(buf), &size, NULL);
3734     CloseHandle(file);
3735 
3736     memset(curdir, 0, sizeof(curdir));
3737     GetCurrentDirectoryA(MAX_PATH, curdir);
3738     lstrcatA(curdir, "\\");
3739     lstrcatA(curdir, test_txtA);
3740 
3741     MultiByteToWideChar(CP_ACP, 0, curdir, -1, cache_file_name, MAX_PATH);
3742 }
3743 
3744 static void test_ReportResult(HRESULT exhres)
3745 {
3746     IMoniker *mon = NULL;
3747     IBindCtx *bctx = NULL;
3748     IUnknown *unk = (void*)0xdeadbeef;
3749     HRESULT hres;
3750 
3751     init_bind_test(ABOUT_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
3752     binding_hres = exhres;
3753 
3754     hres = CreateURLMoniker(NULL, about_blankW, &mon);
3755     ok(hres == S_OK, "CreateURLMoniker failed: %08x\n", hres);
3756 
3757     SET_EXPECT(QueryInterface_IServiceProvider);
3758     hres = CreateAsyncBindCtx(0, (IBindStatusCallback*)&bsc, NULL, &bctx);
3759     ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n\n", hres);
3760     CHECK_CALLED(QueryInterface_IServiceProvider);
3761 
3762     SET_EXPECT(QueryInterface_IBindStatusCallbackEx);
3763     SET_EXPECT(GetBindInfo);
3764     SET_EXPECT(QueryInterface_IInternetProtocol);
3765     SET_EXPECT(OnStartBinding);
3766     if(is_urlmon_protocol(test_protocol))
3767         SET_EXPECT(SetPriority);
3768     SET_EXPECT(Start);
3769 
3770     hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&unk);
3771     if(SUCCEEDED(exhres))
3772         ok(hres == S_OK || hres == MK_S_ASYNCHRONOUS, "BindToStorage failed: %08x\n", hres);
3773     else
3774         ok(hres == exhres || hres == MK_S_ASYNCHRONOUS,
3775            "BindToStorage failed: %08x, expected %08x or MK_S_ASYNCHRONOUS\n", hres, exhres);
3776 
3777     CLEAR_CALLED(QueryInterface_IBindStatusCallbackEx); /* IE 8 */
3778     CHECK_CALLED(GetBindInfo);
3779     CHECK_CALLED(QueryInterface_IInternetProtocol);
3780     CHECK_CALLED(OnStartBinding);
3781     if(is_urlmon_protocol(test_protocol))
3782         CLEAR_CALLED(SetPriority); /* Not called by IE11 */
3783     CHECK_CALLED(Start);
3784 
3785     ok(unk == NULL, "unk=%p\n", unk);
3786 
3787     IBindCtx_Release(bctx);
3788     IMoniker_Release(mon);
3789 }
3790 
3791 static void test_BindToStorage_fail(void)
3792 {
3793     IMoniker *mon = NULL;
3794     IBindCtx *bctx = NULL;
3795     IUnknown *unk;
3796     HRESULT hres;
3797 
3798     hres = CreateURLMoniker(NULL, about_blankW, &mon);
3799     ok(hres == S_OK, "CreateURLMoniker failed: %08x\n", hres);
3800     if(FAILED(hres))
3801         return;
3802 
3803     hres = pCreateAsyncBindCtxEx(NULL, 0, NULL, NULL, &bctx, 0);
3804     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
3805 
3806     unk = (void*)0xdeadbeef;
3807     hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&unk);
3808     ok(hres == MK_E_SYNTAX || hres == INET_E_DATA_NOT_AVAILABLE,
3809        "hres=%08x, expected MK_E_SYNTAX or INET_E_DATA_NOT_AVAILABLE\n", hres);
3810     ok(unk == NULL, "got %p\n", unk);
3811 
3812     IBindCtx_Release(bctx);
3813 
3814     IMoniker_Release(mon);
3815 
3816     test_ReportResult(E_NOTIMPL);
3817     test_ReportResult(S_FALSE);
3818 }
3819 
3820 static void test_StdURLMoniker(void)
3821 {
3822     IMoniker *mon, *async_mon;
3823     LPOLESTR display_name;
3824     IBindCtx *bctx;
3825     IUnknown *unk;
3826     HRESULT hres;
3827 
3828     hres = CoCreateInstance(&IID_IInternet, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
3829             &IID_IMoniker, (void**)&mon);
3830     ok(hres == S_OK, "Could not create IInternet instance: %08x\n", hres);
3831     if(FAILED(hres))
3832         return;
3833 
3834     hres = IMoniker_QueryInterface(mon, &IID_IAsyncMoniker, (void**)&async_mon);
3835     ok(hres == S_OK, "Could not get IAsyncMoniker iface: %08x\n", hres);
3836     ok(mon == async_mon, "mon != async_mon\n");
3837     IMoniker_Release(async_mon);
3838 
3839     hres = IMoniker_GetDisplayName(mon, NULL, NULL, &display_name);
3840     ok(hres == E_OUTOFMEMORY, "GetDisplayName failed: %08x, expected E_OUTOFMEMORY\n", hres);
3841 
3842     if(pCreateUri) {
3843       IUriContainer *uri_container;
3844       IUri *uri;
3845 
3846       hres = IMoniker_QueryInterface(mon, &IID_IUriContainer, (void**)&uri_container);
3847       ok(hres == S_OK, "Could not get IUriMoniker iface: %08x\n", hres);
3848 
3849 
3850       uri = (void*)0xdeadbeef;
3851       hres = IUriContainer_GetIUri(uri_container, &uri);
3852       ok(hres == S_FALSE, "GetIUri failed: %08x\n", hres);
3853       ok(!uri, "uri = %p, expected NULL\n", uri);
3854 
3855       IUriContainer_Release(uri_container);
3856     }
3857 
3858     SET_EXPECT(QueryInterface_IServiceProvider);
3859     hres = CreateAsyncBindCtx(0, (IBindStatusCallback*)&bsc, NULL, &bctx);
3860     ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n\n", hres);
3861     CHECK_CALLED(QueryInterface_IServiceProvider);
3862 
3863     if(pCreateUri) { /* Skip these tests on old IEs */
3864         unk = (void*)0xdeadbeef;
3865         hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&unk);
3866         ok(hres == MK_E_SYNTAX, "BindToStorage failed: %08x, expected MK_E_SYNTAX\n", hres);
3867         ok(!unk, "unk = %p\n", unk);
3868 
3869         unk = (void*)0xdeadbeef;
3870         hres = IMoniker_BindToObject(mon, bctx, NULL, &IID_IUnknown, (void**)&unk);
3871         ok(hres == MK_E_SYNTAX, "BindToStorage failed: %08x, expected MK_E_SYNTAX\n", hres);
3872         ok(!unk, "unk = %p\n", unk);
3873     }
3874 
3875     IMoniker_Release(mon);
3876 }
3877 
3878 static void register_protocols(void)
3879 {
3880     IInternetSession *session;
3881     HRESULT hres;
3882 
3883     static const WCHAR winetestW[] = {'w','i','n','e','t','e','s','t',0};
3884 
3885     hres = CoInternetGetSession(0, &session, 0);
3886     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
3887     if(FAILED(hres))
3888         return;
3889 
3890     hres = IInternetSession_RegisterNameSpace(session, &protocol_cf, &IID_NULL,
3891             winetestW, 0, NULL, 0);
3892     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
3893 
3894     IInternetSession_Release(session);
3895 }
3896 
3897 static BOOL can_do_https(void)
3898 {
3899     HINTERNET ses, con, req;
3900     BOOL ret;
3901 
3902     ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
3903     ok(ses != NULL, "InternetOpen failed\n");
3904 
3905     con = InternetConnectA(ses, "test.winehq.org", INTERNET_DEFAULT_HTTPS_PORT,
3906             NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3907     ok(con != NULL, "InternetConnect failed\n");
3908 
3909     req = HttpOpenRequestA(con, "GET", "/tests/hello.html", NULL, NULL, NULL,
3910             INTERNET_FLAG_SECURE, 0);
3911     ok(req != NULL, "HttpOpenRequest failed\n");
3912 
3913     ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3914     ok(ret || broken(GetLastError() == ERROR_INTERNET_CANNOT_CONNECT),
3915         "request failed: %u\n", GetLastError());
3916 
3917     InternetCloseHandle(req);
3918     InternetCloseHandle(con);
3919     InternetCloseHandle(ses);
3920     return ret;
3921 }
3922 
3923 START_TEST(url)
3924 {
3925     HMODULE hurlmon;
3926 
3927     if (!winetest_interactive)
3928     {
3929         win_skip("Skipping urlmon:url due to hang ROSTESTS-358\n");
3930         return;
3931     }
3932 
3933     hurlmon = GetModuleHandleA("urlmon.dll");
3934     pCreateAsyncBindCtxEx = (void*) GetProcAddress(hurlmon, "CreateAsyncBindCtxEx");
3935 
3936     if(!GetProcAddress(hurlmon, "CompareSecurityIds")) {
3937         win_skip("Too old IE\n");
3938         return;
3939     }
3940 
3941     pCreateUri = (void*) GetProcAddress(hurlmon, "CreateUri");
3942     if(!pCreateUri)
3943         win_skip("IUri not supported\n");
3944 
3945     complete_event = CreateEventW(NULL, FALSE, FALSE, NULL);
3946     complete_event2 = CreateEventW(NULL, FALSE, FALSE, NULL);
3947     thread_id = GetCurrentThreadId();
3948     create_html_file();
3949     create_cache_file();
3950     register_protocols();
3951 
3952     test_create();
3953 
3954     trace("test CreateAsyncBindCtx...\n");
3955     test_CreateAsyncBindCtx();
3956 
3957     trace("test CreateAsyncBindCtxEx...\n");
3958     test_CreateAsyncBindCtxEx();
3959 
3960     trace("test RegisterBindStatusCallback...\n");
3961     if(test_RegisterBindStatusCallback()) {
3962         trace("test BindToStorage failures...\n");
3963         test_BindToStorage_fail();
3964 
3965         trace("synchronous http test (COM not initialised)...\n");
3966         test_BindToStorage(HTTP_TEST, 0, TYMED_ISTREAM);
3967 
3968         CoInitialize(NULL);
3969 
3970         trace("test StdURLMoniker...\n");
3971         test_StdURLMoniker();
3972 
3973         trace("synchronous http test...\n");
3974         test_BindToStorage(HTTP_TEST, 0, TYMED_ISTREAM);
3975 
3976         trace("emulated synchronous http test (to file)...\n");
3977         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE, TYMED_FILE);
3978 
3979         trace("synchronous http test (to object)...\n");
3980         test_BindToObject(HTTP_TEST, 0, S_OK);
3981 
3982         trace("emulated synchronous http test (with cache)...\n");
3983         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE|BINDTEST_USE_CACHE, TYMED_ISTREAM);
3984 
3985         trace("emulated synchronous http test (with cache, no read)...\n");
3986         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE|BINDTEST_USE_CACHE|BINDTEST_NO_CALLBACK_READ, TYMED_ISTREAM);
3987 
3988         trace("synchronous http test (with cache, no read)...\n");
3989         test_BindToStorage(HTTP_TEST, BINDTEST_USE_CACHE|BINDTEST_NO_CALLBACK_READ, TYMED_ISTREAM);
3990 
3991         trace("synchronous file test...\n");
3992         test_BindToStorage(FILE_TEST, 0, TYMED_ISTREAM);
3993 
3994         trace("emulated synchronous file test (to file)...\n");
3995         test_BindToStorage(FILE_TEST, BINDTEST_EMULATE, TYMED_FILE);
3996 
3997         trace("synchronous file test (to object)...\n");
3998         test_BindToObject(FILE_TEST, 0, S_OK);
3999 
4000         trace("bind to an object of not registered MIME type...\n");
4001         create_file("test.winetest", "\x01\x02\x03xxxxxxxxxxxxxxxxxxxxxxxxx");
4002         test_BindToObject(FILE_TEST, 0, REGDB_E_CLASSNOTREG);
4003         create_html_file();
4004 
4005         trace("file test (no callback)...\n");
4006         test_BindToStorage(FILE_TEST, BINDTEST_NO_CALLBACK, TYMED_ISTREAM);
4007 
4008         if(can_do_https()) {
4009             trace("synchronous https test (invalid CN, dialog)\n");
4010             onsecurityproblem_hres = S_FALSE;
4011             http_is_first = TRUE;
4012             test_BindToStorage(HTTPS_TEST, BINDTEST_INVALID_CN, TYMED_ISTREAM);
4013 
4014             bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
4015 
4016             trace("asynchronous https test (invalid CN, fail)\n");
4017             onsecurityproblem_hres = E_FAIL;
4018             test_BindToStorage(HTTPS_TEST, BINDTEST_INVALID_CN, TYMED_ISTREAM);
4019 
4020             trace("asynchronous https test (invalid CN, accept)\n");
4021             onsecurityproblem_hres = S_OK;
4022             test_BindToStorage(HTTPS_TEST, BINDTEST_INVALID_CN, TYMED_ISTREAM);
4023 
4024             trace("asynchronous https test (invalid CN, dialog 2)\n");
4025             onsecurityproblem_hres = S_FALSE;
4026             test_BindToStorage(HTTPS_TEST, BINDTEST_INVALID_CN, TYMED_ISTREAM);
4027             invalid_cn_accepted = FALSE;
4028 
4029             trace("asynchronous https test...\n");
4030             test_BindToStorage(HTTPS_TEST, 0, TYMED_ISTREAM);
4031         }else {
4032             win_skip("Skipping https testt\n");
4033         }
4034 
4035         bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
4036 
4037         trace("winetest test (async switch)...\n");
4038         test_BindToStorage(WINETEST_TEST, BINDTEST_EMULATE|BINDTEST_ASYNC_SWITCH, TYMED_ISTREAM);
4039 
4040         trace("about test (no read)...\n");
4041         test_BindToStorage(ABOUT_TEST, BINDTEST_NO_CALLBACK_READ, TYMED_ISTREAM);
4042 
4043         trace("http test...\n");
4044         test_BindToStorage(HTTP_TEST, 0, TYMED_ISTREAM);
4045 
4046         trace("http test (to file)...\n");
4047         test_BindToStorage(HTTP_TEST, 0, TYMED_FILE);
4048 
4049         trace("http test (to object)...\n");
4050         test_BindToObject(HTTP_TEST, 0, S_OK);
4051 
4052         trace("http test (short response)...\n");
4053         test_BindToStorage(HTTP_TEST, BINDTEST_HTTPRESPONSE|BINDTEST_ALLOW_FINDINGRESOURCE, TYMED_ISTREAM);
4054 
4055         trace("http test (short response, to object)...\n");
4056         test_BindToObject(HTTP_TEST, 0, S_OK);
4057 
4058         trace("http test (abort start binding E_NOTIMPL)...\n");
4059         abort_hres = E_NOTIMPL;
4060         test_BindToStorage(HTTP_TEST, BINDTEST_ABORT_START, TYMED_FILE);
4061 
4062         trace("http test (abort start binding E_ABORT)...\n");
4063         abort_hres = E_ABORT;
4064         test_BindToStorage(HTTP_TEST, BINDTEST_ABORT_START, TYMED_FILE);
4065 
4066         trace("http test (abort progress)...\n");
4067         test_BindToStorage(HTTP_TEST, BINDTEST_ABORT_PROGRESS|BINDTEST_ALLOW_FINDINGRESOURCE, TYMED_FILE);
4068 
4069         trace("emulated http test...\n");
4070         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4071 
4072         trace("emulated http test (to object)...\n");
4073         test_BindToObject(HTTP_TEST, BINDTEST_EMULATE, S_OK);
4074 
4075         trace("emulated http test (to object, redirect)...\n");
4076         test_BindToObject(HTTP_TEST, BINDTEST_EMULATE|BINDTEST_REDIRECT, S_OK);
4077 
4078         trace("emulated http test (to file)...\n");
4079         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE, TYMED_FILE);
4080 
4081         trace("emulated http test (redirect)...\n");
4082         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE|BINDTEST_REDIRECT, TYMED_ISTREAM);
4083 
4084         trace("emulated http test (with cache)...\n");
4085         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE|BINDTEST_USE_CACHE, TYMED_ISTREAM);
4086 
4087         trace("winetest test (no callback)...\n");
4088         test_BindToStorage(WINETEST_TEST, BINDTEST_EMULATE|BINDTEST_NO_CALLBACK|BINDTEST_USE_CACHE, TYMED_ISTREAM);
4089 
4090         trace("emulated https test...\n");
4091         test_BindToStorage(HTTPS_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4092 
4093         trace("about test...\n");
4094         test_BindToStorage(ABOUT_TEST, 0, TYMED_ISTREAM);
4095 
4096         trace("about test (to file)...\n");
4097         test_BindToStorage(ABOUT_TEST, 0, TYMED_FILE);
4098 
4099         trace("about test (to object)...\n");
4100         test_BindToObject(ABOUT_TEST, 0, S_OK);
4101 
4102         trace("emulated about test...\n");
4103         test_BindToStorage(ABOUT_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4104 
4105         trace("emulated about test (to file)...\n");
4106         test_BindToStorage(ABOUT_TEST, BINDTEST_EMULATE, TYMED_FILE);
4107 
4108         trace("emulated about test (to object)...\n");
4109         test_BindToObject(ABOUT_TEST, BINDTEST_EMULATE, S_OK);
4110 
4111         trace("emulalated test reporting result in read...\n");
4112         test_BindToStorage(WINETEST_SYNC_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4113 
4114         trace("file test...\n");
4115         test_BindToStorage(FILE_TEST, 0, TYMED_ISTREAM);
4116 
4117         trace("file test (to file)...\n");
4118         test_BindToStorage(FILE_TEST, 0, TYMED_FILE);
4119 
4120         trace("file test (to object)...\n");
4121         test_BindToObject(FILE_TEST, 0, S_OK);
4122 
4123         trace("emulated file test...\n");
4124         test_BindToStorage(FILE_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4125 
4126         trace("emulated file test (to file)...\n");
4127         test_BindToStorage(FILE_TEST, BINDTEST_EMULATE, TYMED_FILE);
4128 
4129         trace("emulated file test (to object)...\n");
4130         test_BindToObject(FILE_TEST, BINDTEST_EMULATE, S_OK);
4131 
4132         trace("emulated its test...\n");
4133         test_BindToStorage(ITS_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4134 
4135         trace("emulated its test (to file)...\n");
4136         test_BindToStorage(ITS_TEST, BINDTEST_EMULATE, TYMED_FILE);
4137 
4138         trace("emulated mk test...\n");
4139         test_BindToStorage(MK_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4140 
4141         trace("test URLDownloadToFile for file protocol...\n");
4142         test_URLDownloadToFile(FILE_TEST, FALSE);
4143 
4144         trace("test URLDownloadToFile for emulated file protocol...\n");
4145         test_URLDownloadToFile(FILE_TEST, TRUE);
4146 
4147         trace("test URLDownloadToFile for http protocol...\n");
4148         test_URLDownloadToFile(HTTP_TEST, FALSE);
4149 
4150         trace("test URLDownloadToFile abort...\n");
4151         test_URLDownloadToFile_abort();
4152 
4153         trace("test emulated http abort...\n");
4154         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE|BINDTEST_ABORT, TYMED_ISTREAM);
4155 
4156         bindf |= BINDF_NOWRITECACHE;
4157 
4158         trace("ftp test...\n");
4159         test_BindToStorage(FTP_TEST, 0, TYMED_ISTREAM);
4160 
4161         trace("test failures...\n");
4162         test_BindToStorage_fail();
4163 
4164         bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE;
4165         only_check_prot_args = TRUE; /* Fail after checking arguments to Protocol_Start */
4166 
4167         trace("check emulated http protocol arguments...\n");
4168         test_BindToStorage(HTTP_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
4169     }
4170 
4171     DeleteFileA(test_file);
4172     DeleteFileA(test_txtA);
4173     CloseHandle(complete_event);
4174     CloseHandle(complete_event2);
4175     CoUninitialize();
4176 }
4177