oleaut32: Make some functions static.
[wine] / dlls / urlmon / tests / url.c
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
36 #include "wine/test.h"
37
38 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
39 DEFINE_GUID(CLSID_IdentityUnmarshal,0x0000001b,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
40 DEFINE_GUID(IID_IBindStatusCallbackHolder,0x79eac9cc,0xbaf9,0x11ce,0x8c,0x82,0x00,0xaa,0x00,0x4b,0xa9,0x0b);
41
42 #define DEFINE_EXPECT(func) \
43     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
44
45 #define SET_EXPECT(func) \
46     do { called_ ## func = FALSE; expect_ ## func = TRUE; } while(0)
47
48 #define CHECK_EXPECT2(func) \
49     do { \
50         ok(expect_ ##func, "unexpected call " #func "\n"); \
51         called_ ## func = TRUE; \
52     }while(0)
53
54 #define CHECK_EXPECT(func) \
55     do { \
56         CHECK_EXPECT2(func); \
57         expect_ ## func = FALSE; \
58     }while(0)
59
60 #define CHECK_CALLED(func) \
61     do { \
62         ok(called_ ## func, "expected " #func "\n"); \
63         expect_ ## func = called_ ## func = FALSE; \
64     }while(0)
65
66 #define CHECK_NOT_CALLED(func) \
67     do { \
68         ok(!called_ ## func, "unexpected " #func "\n"); \
69         expect_ ## func = called_ ## func = FALSE; \
70     }while(0)
71
72 #define CLEAR_CALLED(func) \
73     expect_ ## func = called_ ## func = FALSE
74
75 DEFINE_EXPECT(QueryInterface_IServiceProvider);
76 DEFINE_EXPECT(QueryInterface_IHttpNegotiate);
77 DEFINE_EXPECT(QueryInterface_IBindStatusCallback);
78 DEFINE_EXPECT(QueryInterface_IBindStatusCallbackHolder);
79 DEFINE_EXPECT(QueryInterface_IInternetBindInfo);
80 DEFINE_EXPECT(QueryInterface_IAuthenticate);
81 DEFINE_EXPECT(QueryInterface_IInternetProtocol);
82 DEFINE_EXPECT(QueryService_IAuthenticate);
83 DEFINE_EXPECT(QueryService_IInternetProtocol);
84 DEFINE_EXPECT(QueryService_IInternetBindInfo);
85 DEFINE_EXPECT(BeginningTransaction);
86 DEFINE_EXPECT(OnResponse);
87 DEFINE_EXPECT(QueryInterface_IHttpNegotiate2);
88 DEFINE_EXPECT(GetRootSecurityId);
89 DEFINE_EXPECT(GetBindInfo);
90 DEFINE_EXPECT(OnStartBinding);
91 DEFINE_EXPECT(OnProgress_FINDINGRESOURCE);
92 DEFINE_EXPECT(OnProgress_CONNECTING);
93 DEFINE_EXPECT(OnProgress_SENDINGREQUEST);
94 DEFINE_EXPECT(OnProgress_MIMETYPEAVAILABLE);
95 DEFINE_EXPECT(OnProgress_BEGINDOWNLOADDATA);
96 DEFINE_EXPECT(OnProgress_DOWNLOADINGDATA);
97 DEFINE_EXPECT(OnProgress_ENDDOWNLOADDATA);
98 DEFINE_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
99 DEFINE_EXPECT(OnStopBinding);
100 DEFINE_EXPECT(OnDataAvailable);
101 DEFINE_EXPECT(OnObjectAvailable);
102 DEFINE_EXPECT(Obj_OnStartBinding);
103 DEFINE_EXPECT(Obj_OnStopBinding);
104 DEFINE_EXPECT(Obj_GetBindInfo);
105 DEFINE_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
106 DEFINE_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
107 DEFINE_EXPECT(Obj_OnProgress_SENDINGREQUEST);
108 DEFINE_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
109 DEFINE_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
110 DEFINE_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
111 DEFINE_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
112 DEFINE_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
113 DEFINE_EXPECT(Obj_OnProgress_CONNECTING);
114 DEFINE_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
115 DEFINE_EXPECT(Start);
116 DEFINE_EXPECT(Read);
117 DEFINE_EXPECT(LockRequest);
118 DEFINE_EXPECT(Terminate);
119 DEFINE_EXPECT(UnlockRequest);
120 DEFINE_EXPECT(Continue);
121 DEFINE_EXPECT(CreateInstance);
122 DEFINE_EXPECT(Load);
123 DEFINE_EXPECT(PutProperty_MIMETYPEPROP);
124 DEFINE_EXPECT(PutProperty_CLASSIDPROP);
125
126 static const WCHAR TEST_URL_1[] = {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/','\0'};
127 static const WCHAR TEST_PART_URL_1[] = {'/','t','e','s','t','/','\0'};
128
129 static const WCHAR WINE_ABOUT_URL[] = {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
130                                        'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
131 static const WCHAR SHORT_RESPONSE_URL[] =
132         {'h','t','t','p',':','/','/','c','r','o','s','s','o','v','e','r','.',
133          'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m','/',
134          'p','o','s','t','t','e','s','t','.','p','h','p',0};
135 static const WCHAR ABOUT_BLANK[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
136 static WCHAR INDEX_HTML[MAX_PATH];
137 static const WCHAR ITS_URL[] =
138     {'i','t','s',':','t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
139 static const WCHAR MK_URL[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
140     't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
141
142 static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
143
144 static WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
145
146 static const WCHAR wszWineHQSite[] =
147     {'w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
148 static const WCHAR wszWineHQIP[] =
149     {'2','0','9','.','3','2','.','1','4','1','.','3',0};
150 static const CHAR wszIndexHtmlA[] = "index.html";
151 static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
152 static const WCHAR cache_fileW[] = {'c',':','\\','c','a','c','h','e','.','h','t','m',0};
153 static const CHAR dwl_htmlA[] = "dwl.html";
154 static const WCHAR dwl_htmlW[] = {'d','w','l','.','h','t','m','l',0};
155 static const WCHAR emptyW[] = {0};
156
157 static BOOL stopped_binding = FALSE, stopped_obj_binding = FALSE, emulate_protocol = FALSE,
158     data_available = FALSE, http_is_first = TRUE, bind_to_object = FALSE, filedwl_api;
159 static DWORD read = 0, bindf = 0, prot_state = 0, thread_id, tymed;
160 static CHAR mime_type[512];
161 static IInternetProtocolSink *protocol_sink = NULL;
162 static HANDLE complete_event, complete_event2;
163 static HRESULT binding_hres;
164 static BOOL have_IHttpNegotiate2;
165
166 static LPCWSTR urls[] = {
167     WINE_ABOUT_URL,
168     ABOUT_BLANK,
169     INDEX_HTML,
170     ITS_URL,
171     MK_URL
172 };
173
174 static WCHAR file_url[INTERNET_MAX_URL_LENGTH];
175
176 static enum {
177     HTTP_TEST,
178     ABOUT_TEST,
179     FILE_TEST,
180     ITS_TEST,
181     MK_TEST
182 } test_protocol;
183
184 static enum {
185     BEFORE_DOWNLOAD,
186     DOWNLOADING,
187     END_DOWNLOAD
188 } download_state;
189
190 static const char *debugstr_w(LPCWSTR str)
191 {
192     static char buf[1024];
193     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
194     return buf;
195 }
196
197 static const char *debugstr_guid(REFIID riid)
198 {
199     static char buf[50];
200
201     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
202             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
203             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
204             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
205
206     return buf;
207 }
208
209 static void test_CreateURLMoniker(LPCWSTR url1, LPCWSTR url2)
210 {
211     HRESULT hr;
212     IMoniker *mon1 = NULL;
213     IMoniker *mon2 = NULL;
214
215     hr = CreateURLMoniker(NULL, url1, &mon1);
216     ok(SUCCEEDED(hr), "failed to create moniker: 0x%08x\n", hr);
217     if(SUCCEEDED(hr)) {
218         hr = CreateURLMoniker(mon1, url2, &mon2);
219         ok(SUCCEEDED(hr), "failed to create moniker: 0x%08x\n", hr);
220     }
221     if(mon1) IMoniker_Release(mon1);
222     if(mon2) IMoniker_Release(mon2);
223 }
224
225 static void test_create(void)
226 {
227     test_CreateURLMoniker(TEST_URL_1, TEST_PART_URL_1);
228 }
229
230 static HRESULT WINAPI Protocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
231 {
232     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
233         *ppv = iface;
234         return S_OK;
235     }
236
237     *ppv = NULL;
238     return E_NOINTERFACE;
239 }
240
241 static ULONG WINAPI Protocol_AddRef(IInternetProtocol *iface)
242 {
243     return 2;
244 }
245
246 static ULONG WINAPI Protocol_Release(IInternetProtocol *iface)
247 {
248     return 1;
249 }
250
251 static DWORD WINAPI thread_proc(PVOID arg)
252 {
253     PROTOCOLDATA protocoldata;
254     HRESULT hres;
255
256     if(bind_to_object)
257         SET_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
258     else
259         SET_EXPECT(OnProgress_FINDINGRESOURCE);
260     hres = IInternetProtocolSink_ReportProgress(protocol_sink,
261             BINDSTATUS_FINDINGRESOURCE, wszWineHQSite);
262     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
263     WaitForSingleObject(complete_event, INFINITE);
264     if(bind_to_object)
265         CHECK_CALLED(Obj_OnProgress_FINDINGRESOURCE);
266     else
267         CHECK_CALLED(OnProgress_FINDINGRESOURCE);
268
269     if(bind_to_object)
270         SET_EXPECT(Obj_OnProgress_CONNECTING);
271     else
272         SET_EXPECT(OnProgress_CONNECTING);
273     hres = IInternetProtocolSink_ReportProgress(protocol_sink,
274             BINDSTATUS_CONNECTING, wszWineHQIP);
275     ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
276     WaitForSingleObject(complete_event, INFINITE);
277     if(bind_to_object)
278         CHECK_CALLED(Obj_OnProgress_CONNECTING);
279     else
280         CHECK_CALLED(OnProgress_CONNECTING);
281
282     if(bind_to_object)
283         SET_EXPECT(Obj_OnProgress_SENDINGREQUEST);
284     else
285         SET_EXPECT(OnProgress_SENDINGREQUEST);
286     hres = IInternetProtocolSink_ReportProgress(protocol_sink,
287             BINDSTATUS_SENDINGREQUEST, NULL);
288     ok(hres == S_OK, "ReportProxgress failed: %08x\n", hres);
289     WaitForSingleObject(complete_event, INFINITE);
290     if(bind_to_object)
291         CHECK_CALLED(Obj_OnProgress_SENDINGREQUEST);
292     else
293         CHECK_CALLED(OnProgress_SENDINGREQUEST);
294
295     SET_EXPECT(Continue);
296     prot_state = 1;
297     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
298     ok(hres == S_OK, "Switch failed: %08x\n", hres);
299     WaitForSingleObject(complete_event, INFINITE);
300
301     CHECK_CALLED(Continue);
302     CHECK_CALLED(Read);
303     if(bind_to_object) {
304         CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
305         CHECK_CALLED(Obj_OnProgress_BEGINDOWNLOADDATA);
306         CHECK_CALLED(Obj_OnProgress_CLASSIDAVAILABLE);
307         CHECK_CALLED(Obj_OnProgress_BEGINSYNCOPERATION);
308         CHECK_CALLED(CreateInstance);
309         CHECK_CALLED(PutProperty_MIMETYPEPROP);
310         CLEAR_CALLED(PutProperty_CLASSIDPROP);
311         CHECK_CALLED(Load);
312         CHECK_CALLED(Obj_OnProgress_ENDSYNCOPERATION);
313         CHECK_CALLED(OnObjectAvailable);
314         CHECK_CALLED(Obj_OnStopBinding);
315     }else {
316         CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
317         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
318         CHECK_CALLED(LockRequest);
319         CHECK_CALLED(OnDataAvailable);
320     }
321
322     SET_EXPECT(Continue);
323     prot_state = 2;
324     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
325     ok(hres == S_OK, "Switch failed: %08x\n", hres);
326     WaitForSingleObject(complete_event, INFINITE);
327     CHECK_CALLED(Continue);
328     CHECK_CALLED(Read);
329     CHECK_CALLED(OnProgress_DOWNLOADINGDATA);
330     CHECK_CALLED(OnDataAvailable);
331
332     SET_EXPECT(Continue);
333     prot_state = 2;
334     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
335     ok(hres == S_OK, "Switch failed: %08x\n", hres);
336     WaitForSingleObject(complete_event, INFINITE);
337     CHECK_CALLED(Continue);
338     CHECK_CALLED(Read);
339     CHECK_CALLED(OnProgress_DOWNLOADINGDATA);
340     CHECK_CALLED(OnDataAvailable);
341
342     SET_EXPECT(Continue);
343     prot_state = 3;
344     hres = IInternetProtocolSink_Switch(protocol_sink, &protocoldata);
345     ok(hres == S_OK, "Switch failed: %08x\n", hres);
346     WaitForSingleObject(complete_event, INFINITE);
347     CHECK_CALLED(Continue);
348     CHECK_CALLED(Read);
349     CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
350     CHECK_CALLED(OnDataAvailable);
351     CHECK_CALLED(OnStopBinding);
352
353     SET_EXPECT(Read);
354
355     SetEvent(complete_event2);
356     return 0;
357 }
358
359 static HRESULT WINAPI Protocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
360         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
361         DWORD grfPI, DWORD dwReserved)
362 {
363     BINDINFO bindinfo;
364     DWORD bindf, bscf = BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION;
365     HRESULT hres;
366
367     static const STGMEDIUM stgmed_zero = {0};
368     static const SECURITY_ATTRIBUTES sa_zero = {0};
369
370     CHECK_EXPECT(Start);
371
372     read = 0;
373
374     if(!filedwl_api) /* FIXME */
375         ok(szUrl && !lstrcmpW(szUrl, urls[test_protocol]), "wrong url %s\n", debugstr_w(szUrl));
376     ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
377     ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
378     ok(grfPI == 0, "grfPI=%d, expected 0\n", grfPI);
379     ok(dwReserved == 0, "dwReserved=%d, expected 0\n", dwReserved);
380
381     if(!filedwl_api && binding_hres != S_OK) {
382         SET_EXPECT(OnStopBinding);
383         SET_EXPECT(Terminate);
384         hres = IInternetProtocolSink_ReportResult(pOIProtSink, binding_hres, 0, NULL);
385         ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
386         CHECK_CALLED(OnStopBinding);
387         CHECK_CALLED(Terminate);
388
389         return S_OK;
390     }
391
392     memset(&bindinfo, 0, sizeof(bindinfo));
393     bindinfo.cbSize = sizeof(bindinfo);
394     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
395     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
396
397     if(filedwl_api) {
398         ok(bindf == (BINDF_PULLDATA|BINDF_FROMURLMON|BINDF_NEEDFILE), "bindf=%08x\n", bindf);
399     }else if(tymed == TYMED_ISTREAM
400        && (test_protocol == FILE_TEST || test_protocol == MK_TEST || test_protocol == HTTP_TEST)) {
401         ok(bindf == (BINDF_ASYNCHRONOUS|BINDF_ASYNCSTORAGE|BINDF_PULLDATA
402                      |BINDF_FROMURLMON),
403            "bindf=%08x\n", bindf);
404     }else {
405         ok(bindf == (BINDF_ASYNCHRONOUS|BINDF_ASYNCSTORAGE|BINDF_PULLDATA
406                      |BINDF_FROMURLMON|BINDF_NEEDFILE),
407            "bindf=%08x\n", bindf);
408     }
409
410     ok(bindinfo.cbSize == sizeof(bindinfo), "bindinfo.cbSize = %d\n", bindinfo.cbSize);
411     ok(!bindinfo.szExtraInfo, "bindinfo.szExtraInfo = %p\n", bindinfo.szExtraInfo);
412     ok(!memcmp(&bindinfo.stgmedData, &stgmed_zero, sizeof(STGMEDIUM)), "wrong stgmedData\n");
413     ok(!bindinfo.grfBindInfoF, "bindinfo.grfBindInfoF = %d\n", bindinfo.grfBindInfoF);
414     ok(!bindinfo.dwBindVerb, "bindinfo.dwBindVerb = %d\n", bindinfo.dwBindVerb);
415     ok(!bindinfo.szCustomVerb, "bindinfo.szCustomVerb = %p\n", bindinfo.szCustomVerb);
416     ok(!bindinfo.cbstgmedData, "bindinfo.cbstgmedData = %d\n", bindinfo.cbstgmedData);
417     ok(bindinfo.dwOptions == (bind_to_object ? 0x100000 : 0), "bindinfo.dwOptions = %x\n", bindinfo.dwOptions);
418     ok(!bindinfo.dwOptionsFlags, "bindinfo.dwOptionsFlags = %d\n", bindinfo.dwOptionsFlags);
419     ok(!bindinfo.dwCodePage, "bindinfo.dwCodePage = %d\n", bindinfo.dwCodePage);
420     ok(!memcmp(&bindinfo.securityAttributes, &sa_zero, sizeof(sa_zero)), "wrong bindinfo.securityAttributes\n");
421     ok(IsEqualGUID(&bindinfo.iid, &IID_NULL), "wrong bindinfo.iid\n");
422     ok(!bindinfo.pUnk, "bindinfo.pUnk = %p\n", bindinfo.pUnk);
423     ok(!bindinfo.dwReserved, "bindinfo.dwReserved = %d\n", bindinfo.dwReserved);
424
425     switch(test_protocol) {
426     case MK_TEST:
427         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
428                 BINDSTATUS_DIRECTBIND, NULL);
429         ok(hres == S_OK,
430            "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
431
432     case FILE_TEST:
433     case ITS_TEST:
434         if(bind_to_object)
435             SET_EXPECT(Obj_OnProgress_SENDINGREQUEST);
436         else
437             SET_EXPECT(OnProgress_SENDINGREQUEST);
438         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
439                 BINDSTATUS_SENDINGREQUEST, emptyW);
440         ok(hres == S_OK,
441            "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
442         if(bind_to_object)
443             CHECK_CALLED(Obj_OnProgress_SENDINGREQUEST);
444         else
445             CHECK_CALLED(OnProgress_SENDINGREQUEST);
446     default:
447         break;
448     }
449
450     if(test_protocol == HTTP_TEST) {
451         IServiceProvider *service_provider;
452         IHttpNegotiate *http_negotiate;
453         IHttpNegotiate2 *http_negotiate2;
454         LPWSTR ua = (LPWSTR)0xdeadbeef, accept_mimes[256];
455         LPWSTR additional_headers = (LPWSTR)0xdeadbeef;
456         BYTE sec_id[100];
457         DWORD fetched = 256, size = 100;
458         DWORD tid;
459
460         static const WCHAR wszMimes[] = {'*','/','*',0};
461
462         SET_EXPECT(QueryInterface_IInternetBindInfo);
463         SET_EXPECT(QueryService_IInternetBindInfo);
464         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT,
465                                                &ua, 1, &fetched);
466         todo_wine {
467         CHECK_CALLED(QueryInterface_IInternetBindInfo);
468         CHECK_CALLED(QueryService_IInternetBindInfo);
469         }
470         ok(hres == E_NOINTERFACE,
471            "GetBindString(BINDSTRING_USER_AGETNT) failed: %08x\n", hres);
472         ok(fetched == 256, "fetched = %d, expected 254\n", fetched);
473         ok(ua == (LPWSTR)0xdeadbeef, "ua =  %p\n", ua);
474
475         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
476                                                accept_mimes, 256, &fetched);
477         ok(hres == S_OK,
478            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
479         ok(fetched == 1, "fetched = %d, expected 1\n", fetched);
480         ok(!lstrcmpW(wszMimes, accept_mimes[0]), "unexpected mimes\n");
481
482         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
483                                                NULL, 256, &fetched);
484         ok(hres == E_INVALIDARG,
485            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
486
487         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
488                                                accept_mimes, 256, NULL);
489         ok(hres == E_INVALIDARG,
490            "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
491
492         hres = IInternetBindInfo_QueryInterface(pOIBindInfo, &IID_IServiceProvider,
493                                                 (void**)&service_provider);
494         ok(hres == S_OK, "QueryInterface failed: %08x\n", hres);
495
496         SET_EXPECT(QueryInterface_IHttpNegotiate);
497         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
498                 &IID_IHttpNegotiate, (void**)&http_negotiate);
499         CHECK_CALLED(QueryInterface_IHttpNegotiate);
500         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
501
502         SET_EXPECT(BeginningTransaction);
503         hres = IHttpNegotiate_BeginningTransaction(http_negotiate, urls[test_protocol],
504                                                    NULL, 0, &additional_headers);
505         CHECK_CALLED(BeginningTransaction);
506         IHttpNegotiate_Release(http_negotiate);
507         ok(hres == S_OK, "BeginningTransction failed: %08x\n", hres);
508         ok(additional_headers == NULL, "additional_headers=%p\n", additional_headers);
509
510         SET_EXPECT(QueryInterface_IHttpNegotiate2);
511         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
512                 &IID_IHttpNegotiate2, (void**)&http_negotiate2);
513         CHECK_CALLED(QueryInterface_IHttpNegotiate2);
514         ok(hres == S_OK, "QueryService failed: %08x\n", hres);
515
516         size = 512;
517         SET_EXPECT(GetRootSecurityId);
518         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, sec_id, &size, 0);
519         CHECK_CALLED(GetRootSecurityId);
520         IHttpNegotiate2_Release(http_negotiate2);
521         ok(hres == E_FAIL, "GetRootSecurityId failed: %08x, expected E_FAIL\n", hres);
522         ok(size == 13, "size=%d\n", size);
523
524         IServiceProvider_Release(service_provider);
525
526         IInternetProtocolSink_AddRef(pOIProtSink);
527         protocol_sink = pOIProtSink;
528         CreateThread(NULL, 0, thread_proc, NULL, 0, &tid);
529
530         return S_OK;
531     }
532
533     if(test_protocol == FILE_TEST) {
534         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
535                 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_url+8);
536         ok(hres == S_OK,
537            "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
538
539         if(bind_to_object)
540             SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
541         else
542             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
543         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
544                 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE, wszTextHtml);
545         ok(hres == S_OK,
546            "ReportProgress(BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE) failed: %08x\n", hres);
547         if(bind_to_object)
548             CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
549         else
550             CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
551     }else {
552         hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
553                 BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
554         ok(hres == S_OK,
555            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
556     }
557
558     if(test_protocol == ABOUT_TEST)
559         bscf |= BSCF_DATAFULLYAVAILABLE;
560     if(test_protocol == ITS_TEST)
561         bscf = BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE;
562
563     SET_EXPECT(Read);
564     if(bind_to_object) {
565         if(test_protocol != FILE_TEST && test_protocol != MK_TEST)
566             SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
567         SET_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
568         if(test_protocol == FILE_TEST)
569             SET_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
570         SET_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
571         SET_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
572         SET_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
573         SET_EXPECT(CreateInstance);
574         SET_EXPECT(PutProperty_MIMETYPEPROP);
575         SET_EXPECT(PutProperty_CLASSIDPROP);
576         SET_EXPECT(Load);
577         SET_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
578         SET_EXPECT(OnObjectAvailable);
579         SET_EXPECT(Obj_OnStopBinding);
580     }else {
581         if(test_protocol != FILE_TEST && test_protocol != MK_TEST)
582             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
583         SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
584         if(test_protocol == FILE_TEST)
585             SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
586         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
587         SET_EXPECT(LockRequest);
588         if(!filedwl_api)
589             SET_EXPECT(OnDataAvailable);
590         SET_EXPECT(OnStopBinding);
591     }
592
593     hres = IInternetProtocolSink_ReportData(pOIProtSink, bscf, 13, 13);
594     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
595
596     CHECK_CALLED(Read);
597     if(bind_to_object) {
598         if(test_protocol != FILE_TEST && test_protocol != MK_TEST)
599             CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
600         CHECK_CALLED(Obj_OnProgress_BEGINDOWNLOADDATA);
601         if(test_protocol == FILE_TEST)
602             CHECK_CALLED(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
603         CHECK_CALLED(Obj_OnProgress_ENDDOWNLOADDATA);
604         CHECK_CALLED(Obj_OnProgress_CLASSIDAVAILABLE);
605         CHECK_CALLED(Obj_OnProgress_BEGINSYNCOPERATION);
606         CHECK_CALLED(CreateInstance);
607         CHECK_CALLED(PutProperty_MIMETYPEPROP);
608         CLEAR_CALLED(PutProperty_CLASSIDPROP);
609         CHECK_CALLED(Load);
610         CHECK_CALLED(Obj_OnProgress_ENDSYNCOPERATION);
611         CHECK_CALLED(OnObjectAvailable);
612         CHECK_CALLED(Obj_OnStopBinding);
613     }else {
614         if(test_protocol != FILE_TEST && test_protocol != MK_TEST)
615             CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
616         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
617         if(test_protocol == FILE_TEST)
618             CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
619         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
620         CHECK_CALLED(LockRequest);
621         if(!filedwl_api)
622             CHECK_CALLED(OnDataAvailable);
623         CHECK_CALLED(OnStopBinding);
624     }
625
626     if(test_protocol == ITS_TEST) {
627         SET_EXPECT(Read);
628         hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
629         ok(hres == S_OK, "ReportProgress(BINDSTATUS_BEGINDOWNLOADDATA) failed: %08x\n", hres);
630         CHECK_CALLED(Read);
631     }
632
633     SET_EXPECT(Terminate);
634     hres = IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
635     ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
636     CHECK_CALLED(Terminate);
637
638     return S_OK;
639 }
640
641 static HRESULT WINAPI Protocol_Continue(IInternetProtocol *iface,
642         PROTOCOLDATA *pProtocolData)
643 {
644     DWORD bscf = 0;
645     HRESULT hres;
646
647     CHECK_EXPECT(Continue);
648
649     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
650
651     ok(pProtocolData != NULL, "pProtocolData == NULL\n");
652     if(!pProtocolData)
653         return S_OK;
654
655     switch(prot_state) {
656     case 1: {
657         IServiceProvider *service_provider;
658         IHttpNegotiate *http_negotiate;
659         static WCHAR header[] = {'?',0};
660
661         hres = IInternetProtocolSink_QueryInterface(protocol_sink, &IID_IServiceProvider,
662                                                     (void**)&service_provider);
663         ok(hres == S_OK, "Could not get IServiceProvicder\n");
664
665         hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
666                                              &IID_IHttpNegotiate, (void**)&http_negotiate);
667         ok(hres == S_OK, "Could not get IHttpNegotiate\n");
668
669         SET_EXPECT(OnResponse);
670         hres = IHttpNegotiate_OnResponse(http_negotiate, 200, header, NULL, NULL);
671         CHECK_CALLED(OnResponse);
672         IHttpNegotiate_Release(http_negotiate);
673         ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
674
675         hres = IInternetProtocolSink_ReportProgress(protocol_sink,
676                 BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
677         ok(hres == S_OK,
678            "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
679
680         if(tymed == TYMED_FILE) {
681             hres = IInternetProtocolSink_ReportProgress(protocol_sink,
682                     BINDSTATUS_CACHEFILENAMEAVAILABLE, cache_fileW);
683             ok(hres == S_OK,
684                    "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
685         }
686
687         bscf |= BSCF_FIRSTDATANOTIFICATION;
688         break;
689     }
690     case 2:
691     case 3:
692         bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
693         break;
694     }
695
696     hres = IInternetProtocolSink_ReportData(protocol_sink, bscf, 100, 400);
697     ok(hres == S_OK, "ReportData failed: %08x\n", hres);
698
699     SET_EXPECT(Read);
700     switch(prot_state) {
701     case 1:
702         if(bind_to_object) {
703             SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
704             SET_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
705             SET_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
706             SET_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
707             SET_EXPECT(CreateInstance);
708             SET_EXPECT(PutProperty_MIMETYPEPROP);
709             SET_EXPECT(PutProperty_CLASSIDPROP);
710             SET_EXPECT(Load);
711             SET_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
712             SET_EXPECT(OnObjectAvailable);
713             SET_EXPECT(Obj_OnStopBinding);
714         }else {
715             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
716             SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
717             SET_EXPECT(LockRequest);
718         }
719         break;
720     case 2:
721         SET_EXPECT(OnProgress_DOWNLOADINGDATA);
722         break;
723     case 3:
724         SET_EXPECT(OnProgress_DOWNLOADINGDATA);
725         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
726     }
727     if(!bind_to_object || prot_state >= 2)
728         SET_EXPECT(OnDataAvailable);
729     if(prot_state == 3)
730         SET_EXPECT(OnStopBinding);
731
732     return S_OK;
733 }
734
735 static HRESULT WINAPI Protocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
736         DWORD dwOptions)
737 {
738     ok(0, "unexpected call\n");
739     return E_NOTIMPL;
740 }
741
742 static HRESULT WINAPI Protocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
743 {
744     CHECK_EXPECT(Terminate);
745
746     ok(dwOptions == 0, "dwOptions=%d, expected 0\n", dwOptions);
747
748     if(protocol_sink) {
749         IInternetProtocolSink_Release(protocol_sink);
750         protocol_sink = NULL;
751     }
752
753     return S_OK;
754 }
755
756 static HRESULT WINAPI Protocol_Suspend(IInternetProtocol *iface)
757 {
758     ok(0, "unexpected call\n");
759     return E_NOTIMPL;
760 }
761
762 static HRESULT WINAPI Protocol_Resume(IInternetProtocol *iface)
763 {
764     ok(0, "unexpected call\n");
765     return E_NOTIMPL;
766 }
767
768 static HRESULT WINAPI Protocol_Read(IInternetProtocol *iface, void *pv,
769         ULONG cb, ULONG *pcbRead)
770 {
771     static const char data[] = "<HTML></HTML>";
772
773     CHECK_EXPECT2(Read);
774
775     if(test_protocol == HTTP_TEST) {
776         HRESULT hres;
777
778         static BOOL pending = TRUE;
779
780         pending = !pending;
781
782         switch(prot_state) {
783         case 1:
784         case 2:
785             if(pending) {
786                 *pcbRead = 10;
787                 memset(pv, '?', 10);
788                 return E_PENDING;
789             }else {
790                 memset(pv, '?', cb);
791                 *pcbRead = cb;
792                 read++;
793                 return S_OK;
794             }
795         case 3:
796             prot_state++;
797
798             *pcbRead = 0;
799
800             hres = IInternetProtocolSink_ReportData(protocol_sink,
801                     BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION, 2000, 2000);
802             ok(hres == S_OK, "ReportData failed: %08x\n", hres);
803
804             hres = IInternetProtocolSink_ReportResult(protocol_sink, S_OK, 0, NULL);
805             ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
806
807             return S_FALSE;
808         case 4:
809             *pcbRead = 0;
810             return S_FALSE;
811         }
812     }
813
814     if(read) {
815         *pcbRead = 0;
816         return S_FALSE;
817     }
818
819     ok(pv != NULL, "pv == NULL\n");
820     ok(cb != 0, "cb == 0\n");
821     ok(pcbRead != NULL, "pcbRead == NULL\n");
822     if(pcbRead) {
823         ok(*pcbRead == 0, "*pcbRead=%d, expected 0\n", *pcbRead);
824         read += *pcbRead = sizeof(data)-1;
825     }
826     if(pv)
827         memcpy(pv, data, sizeof(data));
828
829     return S_OK;
830 }
831
832 static HRESULT WINAPI Protocol_Seek(IInternetProtocol *iface,
833         LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
834 {
835     ok(0, "unexpected call\n");
836     return E_NOTIMPL;
837 }
838
839 static HRESULT WINAPI Protocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
840 {
841     CHECK_EXPECT(LockRequest);
842     return S_OK;
843 }
844
845 static HRESULT WINAPI Protocol_UnlockRequest(IInternetProtocol *iface)
846 {
847     CHECK_EXPECT(UnlockRequest);
848     return S_OK;
849 }
850
851 static const IInternetProtocolVtbl ProtocolVtbl = {
852     Protocol_QueryInterface,
853     Protocol_AddRef,
854     Protocol_Release,
855     Protocol_Start,
856     Protocol_Continue,
857     Protocol_Abort,
858     Protocol_Terminate,
859     Protocol_Suspend,
860     Protocol_Resume,
861     Protocol_Read,
862     Protocol_Seek,
863     Protocol_LockRequest,
864     Protocol_UnlockRequest
865 };
866
867 static IInternetProtocol Protocol = { &ProtocolVtbl };
868
869 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface, REFIID riid, void **ppv)
870 {
871     if(IsEqualGUID(&IID_IUnknown, riid)
872             || IsEqualGUID(&IID_IHttpNegotiate, riid)
873             || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
874         *ppv = iface;
875         return S_OK;
876     }
877
878     ok(0, "unexpected call\n");
879     return E_NOINTERFACE;
880 }
881
882 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
883 {
884     return 2;
885 }
886
887 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
888 {
889     return 1;
890 }
891
892 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface, LPCWSTR szURL,
893         LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
894 {
895     CHECK_EXPECT(BeginningTransaction);
896
897     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
898
899     ok(!lstrcmpW(szURL, urls[test_protocol]), "szURL != urls[test_protocol]\n");
900     ok(!dwReserved, "dwReserved=%d, expected 0\n", dwReserved);
901     ok(pszAdditionalHeaders != NULL, "pszAdditionalHeaders == NULL\n");
902     if(pszAdditionalHeaders)
903         ok(*pszAdditionalHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
904
905     return S_OK;
906 }
907
908 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
909         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
910 {
911     CHECK_EXPECT(OnResponse);
912
913     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
914
915     ok(dwResponseCode == 200, "dwResponseCode=%d, expected 200\n", dwResponseCode);
916     ok(szResponseHeaders != NULL, "szResponseHeaders == NULL\n");
917     ok(szRequestHeaders == NULL, "szRequestHeaders != NULL\n");
918     /* Note: in protocol.c tests, OnResponse pszAdditionalRequestHeaders _is_ NULL */
919     ok(pszAdditionalRequestHeaders != NULL, "pszAdditionalHeaders == NULL\n");
920     if(pszAdditionalRequestHeaders)
921         ok(*pszAdditionalRequestHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
922
923     return S_OK;
924 }
925
926 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
927         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
928 {
929     static const BYTE sec_id[] = {'h','t','t','p',':','t','e','s','t',1,0,0,0};
930
931     CHECK_EXPECT(GetRootSecurityId);
932
933     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
934
935     ok(!dwReserved, "dwReserved=%ld, expected 0\n", dwReserved);
936     ok(pbSecurityId != NULL, "pbSecurityId == NULL\n");
937     ok(pcbSecurityId != NULL, "pcbSecurityId == NULL\n");
938
939     if(pbSecurityId == (void*)0xdeadbeef)
940         return E_NOTIMPL;
941
942     if(pcbSecurityId) {
943         ok(*pcbSecurityId == 512, "*pcbSecurityId=%d, expected 512\n", *pcbSecurityId);
944         *pcbSecurityId = sizeof(sec_id);
945     }
946
947     if(pbSecurityId)
948         memcpy(pbSecurityId, sec_id, sizeof(sec_id));
949
950     return E_FAIL;
951 }
952
953 static IHttpNegotiate2Vtbl HttpNegotiateVtbl = {
954     HttpNegotiate_QueryInterface,
955     HttpNegotiate_AddRef,
956     HttpNegotiate_Release,
957     HttpNegotiate_BeginningTransaction,
958     HttpNegotiate_OnResponse,
959     HttpNegotiate_GetRootSecurityId
960 };
961
962 static IHttpNegotiate2 HttpNegotiate = { &HttpNegotiateVtbl };
963
964 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
965 {
966     ok(0, "unexpected call\n");
967     return E_NOINTERFACE;
968 }
969
970 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
971 {
972     return 2;
973 }
974
975 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
976 {
977     return 1;
978 }
979
980 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
981         REFGUID guidService, REFIID riid, void **ppv)
982 {
983     if(IsEqualGUID(&IID_IAuthenticate, guidService)) {
984         CHECK_EXPECT(QueryService_IAuthenticate);
985         return E_NOTIMPL;
986     }
987
988     if(IsEqualGUID(&IID_IInternetProtocol, guidService)) {
989         CHECK_EXPECT2(QueryService_IInternetProtocol);
990         return E_NOTIMPL;
991     }
992
993     if(IsEqualGUID(&IID_IInternetBindInfo, guidService)) {
994         CHECK_EXPECT(QueryService_IInternetBindInfo);
995         return E_NOTIMPL;
996     }
997
998     ok(0, "unexpected service %s\n", debugstr_guid(guidService));
999     return E_NOINTERFACE;
1000 }
1001
1002 static IServiceProviderVtbl ServiceProviderVtbl = {
1003     ServiceProvider_QueryInterface,
1004     ServiceProvider_AddRef,
1005     ServiceProvider_Release,
1006     ServiceProvider_QueryService
1007 };
1008
1009 static IServiceProvider ServiceProvider = { &ServiceProviderVtbl };
1010
1011 static IBindStatusCallback objbsc;
1012
1013 static HRESULT WINAPI statusclb_QueryInterface(IBindStatusCallback *iface, REFIID riid, void **ppv)
1014 {
1015     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1016
1017     if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
1018         CHECK_EXPECT2(QueryInterface_IInternetProtocol);
1019         if(emulate_protocol) {
1020             *ppv = &Protocol;
1021             return S_OK;
1022         }else {
1023             return E_NOINTERFACE;
1024         }
1025     }
1026     else if (IsEqualGUID(&IID_IServiceProvider, riid))
1027     {
1028         CHECK_EXPECT2(QueryInterface_IServiceProvider);
1029         *ppv = &ServiceProvider;
1030         return S_OK;
1031     }
1032     else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
1033     {
1034         CHECK_EXPECT(QueryInterface_IHttpNegotiate);
1035         *ppv = &HttpNegotiate;
1036         return S_OK;
1037     }
1038     else if (IsEqualGUID(&IID_IHttpNegotiate2, riid))
1039     {
1040         CHECK_EXPECT(QueryInterface_IHttpNegotiate2);
1041         *ppv = &HttpNegotiate;
1042         return S_OK;
1043     }
1044     else if (IsEqualGUID(&IID_IAuthenticate, riid))
1045     {
1046         CHECK_EXPECT(QueryInterface_IAuthenticate);
1047         return E_NOINTERFACE;
1048     }
1049     else if(IsEqualGUID(&IID_IBindStatusCallback, riid))
1050     {
1051         CHECK_EXPECT2(QueryInterface_IBindStatusCallback);
1052         *ppv = iface;
1053         return S_OK;
1054     }
1055     else if(IsEqualGUID(&IID_IBindStatusCallbackHolder, riid))
1056     {
1057         CHECK_EXPECT2(QueryInterface_IBindStatusCallbackHolder);
1058         return E_NOINTERFACE;
1059     }
1060     else if(IsEqualGUID(&IID_IInternetBindInfo, riid))
1061     {
1062         /* TODO */
1063         CHECK_EXPECT2(QueryInterface_IInternetBindInfo);
1064     }
1065     else
1066     {
1067         ok(0, "unexpected interface %s\n", debugstr_guid(riid));
1068     }
1069
1070     return E_NOINTERFACE;
1071 }
1072
1073 static ULONG WINAPI statusclb_AddRef(IBindStatusCallback *iface)
1074 {
1075     return 2;
1076 }
1077
1078 static ULONG WINAPI statusclb_Release(IBindStatusCallback *iface)
1079 {
1080     return 1;
1081 }
1082
1083 static HRESULT WINAPI statusclb_OnStartBinding(IBindStatusCallback *iface, DWORD dwReserved,
1084         IBinding *pib)
1085 {
1086     IWinInetHttpInfo *http_info;
1087     HRESULT hres;
1088     IMoniker *mon;
1089
1090     if(iface == &objbsc)
1091         CHECK_EXPECT(Obj_OnStartBinding);
1092     else
1093         CHECK_EXPECT(OnStartBinding);
1094
1095     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1096
1097     ok(pib != NULL, "pib should not be NULL\n");
1098     ok(dwReserved == 0xff, "dwReserved=%x\n", dwReserved);
1099
1100     if(pib == (void*)0xdeadbeef)
1101         return S_OK;
1102
1103     hres = IBinding_QueryInterface(pib, &IID_IMoniker, (void**)&mon);
1104     ok(hres == E_NOINTERFACE, "IBinding should not have IMoniker interface\n");
1105     if(SUCCEEDED(hres))
1106         IMoniker_Release(mon);
1107
1108     hres = IBinding_QueryInterface(pib, &IID_IWinInetHttpInfo, (void**)&http_info);
1109     ok(hres == E_NOINTERFACE, "Could not get IID_IWinInetHttpInfo: %08x\n", hres);
1110
1111     return S_OK;
1112 }
1113
1114 static HRESULT WINAPI statusclb_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
1115 {
1116     ok(0, "unexpected call\n");
1117     return E_NOTIMPL;
1118 }
1119
1120 static HRESULT WINAPI statusclb_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
1121 {
1122     ok(0, "unexpected call\n");
1123     return E_NOTIMPL;
1124 }
1125
1126 static HRESULT WINAPI statusclb_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
1127         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
1128 {
1129     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1130
1131     switch(ulStatusCode) {
1132     case BINDSTATUS_FINDINGRESOURCE:
1133         if(iface == &objbsc)
1134             CHECK_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
1135         else
1136             CHECK_EXPECT(OnProgress_FINDINGRESOURCE);
1137         if((bindf & BINDF_ASYNCHRONOUS) && emulate_protocol)
1138             SetEvent(complete_event);
1139         break;
1140     case BINDSTATUS_CONNECTING:
1141         if(iface == &objbsc)
1142             CHECK_EXPECT(Obj_OnProgress_CONNECTING);
1143         else
1144             CHECK_EXPECT(OnProgress_CONNECTING);
1145         if((bindf & BINDF_ASYNCHRONOUS) && emulate_protocol)
1146             SetEvent(complete_event);
1147         break;
1148     case BINDSTATUS_SENDINGREQUEST:
1149         if(iface == &objbsc)
1150             CHECK_EXPECT(Obj_OnProgress_SENDINGREQUEST);
1151         else
1152             CHECK_EXPECT(OnProgress_SENDINGREQUEST);
1153         if((bindf & BINDF_ASYNCHRONOUS) && emulate_protocol)
1154             SetEvent(complete_event);
1155         break;
1156     case BINDSTATUS_MIMETYPEAVAILABLE:
1157         if(iface == &objbsc)
1158             CHECK_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
1159         else
1160             CHECK_EXPECT(OnProgress_MIMETYPEAVAILABLE);
1161         if(!bind_to_object)
1162             ok(download_state == BEFORE_DOWNLOAD, "Download state was %d, expected BEFORE_DOWNLOAD\n",
1163                download_state);
1164         WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, mime_type, sizeof(mime_type)-1, NULL, NULL);
1165         break;
1166     case BINDSTATUS_BEGINDOWNLOADDATA:
1167         if(iface == &objbsc)
1168             CHECK_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
1169         else
1170             CHECK_EXPECT(OnProgress_BEGINDOWNLOADDATA);
1171         ok(szStatusText != NULL, "szStatusText == NULL\n");
1172         if(szStatusText) {
1173             if(filedwl_api) {
1174                 /* FIXME */
1175             }else {
1176                 ok(!lstrcmpW(szStatusText, urls[test_protocol]), "wrong szStatusText %s\n", debugstr_w(szStatusText));
1177             }
1178         }
1179         if(!bind_to_object)
1180             ok(download_state == BEFORE_DOWNLOAD, "Download state was %d, expected BEFORE_DOWNLOAD\n",
1181                download_state);
1182         download_state = DOWNLOADING;
1183         break;
1184     case BINDSTATUS_DOWNLOADINGDATA:
1185         CHECK_EXPECT2(OnProgress_DOWNLOADINGDATA);
1186         if(iface == &objbsc)
1187             todo_wine ok(0, "unexpected call\n");
1188         ok(download_state == DOWNLOADING, "Download state was %d, expected DOWNLOADING\n",
1189            download_state);
1190         break;
1191     case BINDSTATUS_ENDDOWNLOADDATA:
1192         if(iface == &objbsc)
1193             CHECK_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
1194         else
1195             CHECK_EXPECT(OnProgress_ENDDOWNLOADDATA);
1196         ok(szStatusText != NULL, "szStatusText == NULL\n");
1197         if(szStatusText) {
1198             if(filedwl_api) {
1199                 /* FIXME */
1200             }else {
1201                 ok(!lstrcmpW(szStatusText, urls[test_protocol]), "wrong szStatusText %s\n", debugstr_w(szStatusText));
1202             }
1203         }
1204         ok(download_state == DOWNLOADING, "Download state was %d, expected DOWNLOADING\n",
1205            download_state);
1206         download_state = END_DOWNLOAD;
1207         break;
1208     case BINDSTATUS_CACHEFILENAMEAVAILABLE:
1209         if(test_protocol != HTTP_TEST) {
1210             if(iface == &objbsc)
1211                 CHECK_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
1212             else
1213                 CHECK_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
1214         }else {  /* FIXME */
1215             CLEAR_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
1216             CLEAR_CALLED(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
1217         }
1218
1219         ok(szStatusText != NULL, "szStatusText == NULL\n");
1220         if(szStatusText && test_protocol == FILE_TEST)
1221             ok(!lstrcmpW(file_url+8, szStatusText), "wrong szStatusText %s\n", debugstr_w(szStatusText));
1222         break;
1223     case BINDSTATUS_CLASSIDAVAILABLE:
1224     {
1225         CLSID clsid;
1226         HRESULT hr;
1227         if(iface != &objbsc)
1228             ok(0, "unexpected call\n");
1229         else if(1||emulate_protocol)
1230             CHECK_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
1231         else
1232             todo_wine CHECK_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
1233         hr = CLSIDFromString((LPOLESTR)szStatusText, &clsid);
1234         ok(hr == S_OK, "CLSIDFromString failed with error 0x%08x\n", hr);
1235         ok(IsEqualCLSID(&clsid, &CLSID_HTMLDocument),
1236             "Expected clsid to be CLSID_HTMLDocument instead of %s\n", debugstr_guid(&clsid));
1237         break;
1238     }
1239     case BINDSTATUS_BEGINSYNCOPERATION:
1240         CHECK_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
1241         if(iface != &objbsc)
1242             ok(0, "unexpected call\n");
1243         ok(szStatusText == NULL, "Expected szStatusText to be NULL\n");
1244         break;
1245     case BINDSTATUS_ENDSYNCOPERATION:
1246         CHECK_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
1247         if(iface != &objbsc)
1248             ok(0, "unexpected call\n");
1249         ok(szStatusText == NULL, "Expected szStatusText to be NULL\n");
1250         break;
1251     default:
1252         ok(0, "unexpected code %d\n", ulStatusCode);
1253     };
1254     return S_OK;
1255 }
1256
1257 static HRESULT WINAPI statusclb_OnStopBinding(IBindStatusCallback *iface, HRESULT hresult, LPCWSTR szError)
1258 {
1259     if(iface == &objbsc) {
1260         CHECK_EXPECT(Obj_OnStopBinding);
1261         stopped_obj_binding = TRUE;
1262     }else {
1263         CHECK_EXPECT(OnStopBinding);
1264         stopped_binding = TRUE;
1265     }
1266
1267     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1268
1269     /* ignore DNS failure */
1270     if (hresult == HRESULT_FROM_WIN32(ERROR_INTERNET_NAME_NOT_RESOLVED))
1271         return S_OK;
1272
1273     if(filedwl_api)
1274         ok(SUCCEEDED(hresult), "binding failed: %08x\n", hresult);
1275     else
1276         ok(hresult == binding_hres, "binding failed: %08x, expected %08x\n", hresult, binding_hres);
1277     ok(szError == NULL, "szError should be NULL\n");
1278
1279     if(test_protocol == HTTP_TEST && emulate_protocol) {
1280         SetEvent(complete_event);
1281         if(iface != &objbsc)
1282             WaitForSingleObject(complete_event2, INFINITE);
1283     }
1284
1285     return S_OK;
1286 }
1287
1288 static HRESULT WINAPI statusclb_GetBindInfo(IBindStatusCallback *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
1289 {
1290     DWORD cbSize;
1291
1292     if(iface == &objbsc)
1293         CHECK_EXPECT(Obj_GetBindInfo);
1294     else
1295         CHECK_EXPECT(GetBindInfo);
1296
1297     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1298
1299     *grfBINDF = bindf;
1300     cbSize = pbindinfo->cbSize;
1301     memset(pbindinfo, 0, cbSize);
1302     pbindinfo->cbSize = cbSize;
1303
1304     return S_OK;
1305 }
1306
1307 static HRESULT WINAPI statusclb_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
1308         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
1309 {
1310     HRESULT hres;
1311     DWORD readed;
1312     BYTE buf[512];
1313     CHAR clipfmt[512];
1314
1315     if(iface == &objbsc)
1316         ok(0, "unexpected call\n");
1317
1318     CHECK_EXPECT2(OnDataAvailable);
1319
1320     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1321
1322     ok(download_state == DOWNLOADING || download_state == END_DOWNLOAD,
1323        "Download state was %d, expected DOWNLOADING or END_DOWNLOAD\n",
1324        download_state);
1325     data_available = TRUE;
1326
1327     ok(pformatetc != NULL, "pformatetx == NULL\n");
1328     if(pformatetc) {
1329         if (mime_type[0]) {
1330             clipfmt[0] = 0;
1331             ok(GetClipboardFormatName(pformatetc->cfFormat, clipfmt, sizeof(clipfmt)-1),
1332                "GetClipboardFormatName failed, error %d\n", GetLastError());
1333             ok(!lstrcmp(clipfmt, mime_type), "clipformat %x != mime_type, \"%s\" != \"%s\"\n",
1334                pformatetc->cfFormat, clipfmt, mime_type);
1335         } else {
1336             ok(pformatetc->cfFormat == 0, "clipformat=%x\n", pformatetc->cfFormat);
1337         }
1338         ok(pformatetc->ptd == NULL, "ptd = %p\n", pformatetc->ptd);
1339         ok(pformatetc->dwAspect == 1, "dwAspect=%u\n", pformatetc->dwAspect);
1340         ok(pformatetc->lindex == -1, "lindex=%d\n", pformatetc->lindex);
1341         ok(pformatetc->tymed == tymed, "tymed=%u, expected %u\n", pformatetc->tymed, tymed);
1342     }
1343
1344     ok(pstgmed != NULL, "stgmeg == NULL\n");
1345     ok(pstgmed->tymed == tymed, "tymed=%u, expected %u\n", pstgmed->tymed, tymed);
1346     ok(pstgmed->pUnkForRelease != NULL, "pUnkForRelease == NULL\n");
1347
1348     switch(pstgmed->tymed) {
1349     case TYMED_ISTREAM:
1350         if(grfBSCF & BSCF_FIRSTDATANOTIFICATION) {
1351             hres = IStream_Write(U(*pstgmed).pstm, buf, 10, NULL);
1352             ok(hres == STG_E_ACCESSDENIED,
1353                "Write failed: %08x, expected STG_E_ACCESSDENIED\n", hres);
1354
1355             hres = IStream_Commit(U(*pstgmed).pstm, 0);
1356             ok(hres == E_NOTIMPL, "Commit failed: %08x, expected E_NOTIMPL\n", hres);
1357
1358             hres = IStream_Revert(U(*pstgmed).pstm);
1359             ok(hres == E_NOTIMPL, "Revert failed: %08x, expected E_NOTIMPL\n", hres);
1360         }
1361
1362         ok(U(*pstgmed).pstm != NULL, "U(*pstgmed).pstm == NULL\n");
1363         do hres = IStream_Read(U(*pstgmed).pstm, buf, 512, &readed);
1364         while(hres == S_OK);
1365         ok(hres == S_FALSE || hres == E_PENDING, "IStream_Read returned %08x\n", hres);
1366         break;
1367
1368     case TYMED_FILE:
1369         if(test_protocol == FILE_TEST)
1370             ok(!lstrcmpW(pstgmed->u.lpszFileName, INDEX_HTML+7),
1371                "unexpected file name %s\n", debugstr_w(pstgmed->u.lpszFileName));
1372         else if(emulate_protocol)
1373             ok(!lstrcmpW(pstgmed->u.lpszFileName, cache_fileW),
1374                "unexpected file name %s\n", debugstr_w(pstgmed->u.lpszFileName));
1375         else
1376             ok(pstgmed->u.lpszFileName != NULL, "lpszFileName == NULL\n");
1377     }
1378
1379     if(test_protocol == HTTP_TEST && emulate_protocol && prot_state < 4 && (!bind_to_object || prot_state > 1))
1380         SetEvent(complete_event);
1381
1382     return S_OK;
1383 }
1384
1385 static HRESULT WINAPI statusclb_OnObjectAvailable(IBindStatusCallback *iface, REFIID riid, IUnknown *punk)
1386 {
1387     CHECK_EXPECT(OnObjectAvailable);
1388
1389     if(iface != &objbsc)
1390         ok(0, "unexpected call\n");
1391
1392     ok(IsEqualGUID(&IID_IUnknown, riid), "riid = %s\n", debugstr_guid(riid));
1393     ok(punk != NULL, "punk == NULL\n");
1394
1395     return S_OK;
1396 }
1397
1398 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
1399     statusclb_QueryInterface,
1400     statusclb_AddRef,
1401     statusclb_Release,
1402     statusclb_OnStartBinding,
1403     statusclb_GetPriority,
1404     statusclb_OnLowResource,
1405     statusclb_OnProgress,
1406     statusclb_OnStopBinding,
1407     statusclb_GetBindInfo,
1408     statusclb_OnDataAvailable,
1409     statusclb_OnObjectAvailable
1410 };
1411
1412 static IBindStatusCallback bsc = { &BindStatusCallbackVtbl };
1413 static IBindStatusCallback objbsc = { &BindStatusCallbackVtbl };
1414
1415 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppv)
1416 {
1417     *ppv = NULL;
1418     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1419     return E_NOINTERFACE;
1420 }
1421
1422 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
1423 {
1424     return 2;
1425 }
1426
1427 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
1428 {
1429     return 1;
1430 }
1431
1432 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
1433 {
1434     switch(mkp) {
1435     case MIMETYPEPROP:
1436         CHECK_EXPECT(PutProperty_MIMETYPEPROP);
1437         ok(!lstrcmpW(val, wszTextHtml), "val = %s\n", debugstr_w(val));
1438         break;
1439     case CLASSIDPROP:
1440         CHECK_EXPECT(PutProperty_CLASSIDPROP);
1441         break;
1442     default:
1443         break;
1444     }
1445
1446     return S_OK;
1447 }
1448
1449 static const IMonikerPropVtbl MonikerPropVtbl = {
1450     MonikerProp_QueryInterface,
1451     MonikerProp_AddRef,
1452     MonikerProp_Release,
1453     MonikerProp_PutProperty
1454 };
1455
1456 static IMonikerProp MonikerProp = { &MonikerPropVtbl };
1457
1458 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid, void **ppv)
1459 {
1460     *ppv = NULL;
1461
1462     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IPersistMoniker, riid))
1463         *ppv = iface;
1464     else if(IsEqualGUID(&IID_IMonikerProp, riid))
1465         *ppv = &MonikerProp;
1466
1467     if(*ppv)
1468         return S_OK;
1469
1470     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1471     return E_NOINTERFACE;
1472 }
1473
1474 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
1475 {
1476     return 2;
1477 }
1478
1479 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
1480 {
1481     return 1;
1482 }
1483
1484 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
1485 {
1486     ok(0, "unexpected call\n");
1487     return E_NOTIMPL;
1488 }
1489
1490 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
1491 {
1492     ok(0, "unexpected call\n");
1493     return E_NOTIMPL;
1494 }
1495
1496 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
1497                                           IMoniker *pimkName, LPBC pibc, DWORD grfMode)
1498 {
1499     IUnknown *unk;
1500     HRESULT hres;
1501
1502     static WCHAR cbinding_contextW[] =
1503         {'C','B','i','n','d','i','n','g',' ','C','o','n','t','e','x','t',0};
1504
1505     CHECK_EXPECT(Load);
1506     ok(GetCurrentThreadId() == thread_id, "wrong thread %d\n", GetCurrentThreadId());
1507
1508     if(test_protocol == HTTP_TEST)
1509         ok(!fFullyAvailable, "fFulyAvailable = %x\n", fFullyAvailable);
1510     else
1511         ok(fFullyAvailable, "fFulyAvailable = %x\n", fFullyAvailable);
1512     ok(pimkName != NULL, "pimkName == NULL\n");
1513     ok(pibc != NULL, "pibc == NULL\n");
1514     ok(grfMode == 0x12, "grfMode = %x\n", grfMode);
1515
1516     hres = IBindCtx_GetObjectParam(pibc, cbinding_contextW, &unk);
1517     ok(hres == S_OK, "GetObjectParam(CBinding Context) failed: %08x\n", hres);
1518     if(SUCCEEDED(hres)) {
1519         IBinding *binding;
1520
1521         hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void**)&binding);
1522         ok(hres == S_OK, "Could not get IBinding: %08x\n", hres);
1523
1524         IBinding_Release(binding);
1525         IUnknown_Release(unk);
1526     }
1527
1528     SET_EXPECT(QueryInterface_IServiceProvider);
1529     hres = RegisterBindStatusCallback(pibc, &bsc, NULL, 0);
1530     ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
1531     CHECK_CALLED(QueryInterface_IServiceProvider);
1532
1533     SET_EXPECT(GetBindInfo);
1534     SET_EXPECT(OnStartBinding);
1535     SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
1536     if(test_protocol == FILE_TEST)
1537         SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
1538     if(test_protocol != HTTP_TEST)
1539         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
1540     SET_EXPECT(LockRequest);
1541     SET_EXPECT(OnDataAvailable);
1542     if(test_protocol != HTTP_TEST)
1543         SET_EXPECT(OnStopBinding);
1544
1545     hres = IMoniker_BindToStorage(pimkName, pibc, NULL, &IID_IStream, (void**)&unk);
1546     ok(hres == S_OK, "Load failed: %08x\n", hres);
1547
1548     CHECK_CALLED(GetBindInfo);
1549     CHECK_CALLED(OnStartBinding);
1550     CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
1551     if(test_protocol == FILE_TEST)
1552         CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
1553     if(test_protocol != HTTP_TEST)
1554         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
1555     CHECK_CALLED(LockRequest);
1556     CHECK_CALLED(OnDataAvailable);
1557     if(test_protocol != HTTP_TEST)
1558         CHECK_CALLED(OnStopBinding);
1559
1560     if(unk)
1561         IUnknown_Release(unk);
1562
1563     return S_OK;
1564 }
1565
1566 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName, LPBC pbc, BOOL fRemember)
1567 {
1568     ok(0, "unexpected call\n");
1569     return E_NOTIMPL;
1570 }
1571
1572 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
1573 {
1574     ok(0, "unexpected call\n");
1575     return E_NOTIMPL;
1576 }
1577
1578 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **pimkName)
1579 {
1580     ok(0, "unexpected call\n");
1581     return E_NOTIMPL;
1582 }
1583
1584 static const IPersistMonikerVtbl PersistMonikerVtbl = {
1585     PersistMoniker_QueryInterface,
1586     PersistMoniker_AddRef,
1587     PersistMoniker_Release,
1588     PersistMoniker_GetClassID,
1589     PersistMoniker_IsDirty,
1590     PersistMoniker_Load,
1591     PersistMoniker_Save,
1592     PersistMoniker_SaveCompleted,
1593     PersistMoniker_GetCurMoniker
1594 };
1595
1596 static IPersistMoniker PersistMoniker = { &PersistMonikerVtbl };
1597
1598 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
1599 {
1600     *ppv = NULL;
1601
1602     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
1603         *ppv = iface;
1604         return S_OK;
1605     }
1606
1607     if(IsEqualGUID(&IID_IMarshal, riid))
1608         return E_NOINTERFACE;
1609     if(IsEqualGUID(&CLSID_IdentityUnmarshal, riid))
1610         return E_NOINTERFACE;
1611
1612     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1613     return E_NOTIMPL;
1614 }
1615
1616 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
1617 {
1618     return 2;
1619 }
1620
1621 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
1622 {
1623     return 1;
1624 }
1625
1626 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
1627 {
1628     CHECK_EXPECT(CreateInstance);
1629     ok(!outer, "outer = %p\n", outer);
1630     ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", debugstr_guid(riid));
1631     *ppv = &PersistMoniker;
1632     return S_OK;
1633 }
1634
1635 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
1636 {
1637     ok(0, "unexpected call\n");
1638     return S_OK;
1639 }
1640
1641 static const IClassFactoryVtbl ClassFactoryVtbl = {
1642     ClassFactory_QueryInterface,
1643     ClassFactory_AddRef,
1644     ClassFactory_Release,
1645     ClassFactory_CreateInstance,
1646     ClassFactory_LockServer
1647 };
1648
1649 static IClassFactory mime_cf = { &ClassFactoryVtbl };
1650
1651 static void test_CreateAsyncBindCtx(void)
1652 {
1653     IBindCtx *bctx = (IBindCtx*)0x0ff00ff0;
1654     IUnknown *unk;
1655     HRESULT hres;
1656     ULONG ref;
1657     BIND_OPTS bindopts;
1658
1659     hres = CreateAsyncBindCtx(0, NULL, NULL, &bctx);
1660     ok(hres == E_INVALIDARG, "CreateAsyncBindCtx failed. expected: E_INVALIDARG, got: %08x\n", hres);
1661     ok(bctx == (IBindCtx*)0x0ff00ff0, "bctx should not be changed\n");
1662
1663     hres = CreateAsyncBindCtx(0, NULL, NULL, NULL);
1664     ok(hres == E_INVALIDARG, "CreateAsyncBindCtx failed. expected: E_INVALIDARG, got: %08x\n", hres);
1665
1666     SET_EXPECT(QueryInterface_IServiceProvider);
1667     hres = CreateAsyncBindCtx(0, &bsc, NULL, &bctx);
1668     ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n", hres);
1669     CHECK_CALLED(QueryInterface_IServiceProvider);
1670
1671     bindopts.cbStruct = sizeof(bindopts);
1672     hres = IBindCtx_GetBindOptions(bctx, &bindopts);
1673     ok(SUCCEEDED(hres), "IBindCtx_GetBindOptions failed: %08x\n", hres);
1674     ok(bindopts.grfFlags == BIND_MAYBOTHERUSER,
1675                 "bindopts.grfFlags = %08x, expected: BIND_MAYBOTHERUSER\n", bindopts.grfFlags);
1676     ok(bindopts.grfMode == (STGM_READWRITE | STGM_SHARE_EXCLUSIVE),
1677                 "bindopts.grfMode = %08x, expected: STGM_READWRITE | STGM_SHARE_EXCLUSIVE\n",
1678                 bindopts.grfMode);
1679     ok(bindopts.dwTickCountDeadline == 0,
1680                 "bindopts.dwTickCountDeadline = %08x, expected: 0\n", bindopts.dwTickCountDeadline);
1681
1682     hres = IBindCtx_QueryInterface(bctx, &IID_IAsyncBindCtx, (void**)&unk);
1683     ok(hres == E_NOINTERFACE, "QueryInterface(IID_IAsyncBindCtx) failed: %08x, expected E_NOINTERFACE\n", hres);
1684     if(SUCCEEDED(hres))
1685         IUnknown_Release(unk);
1686
1687     ref = IBindCtx_Release(bctx);
1688     ok(ref == 0, "bctx should be destroyed here\n");
1689 }
1690
1691 static void test_CreateAsyncBindCtxEx(void)
1692 {
1693     IBindCtx *bctx = NULL, *bctx2 = NULL, *bctx_arg = NULL;
1694     IUnknown *unk;
1695     BIND_OPTS bindopts;
1696     HRESULT hres;
1697
1698     static WCHAR testW[] = {'t','e','s','t',0};
1699
1700     hres = CreateAsyncBindCtxEx(NULL, 0, NULL, NULL, NULL, 0);
1701     ok(hres == E_INVALIDARG, "CreateAsyncBindCtx failed: %08x, expected E_INVALIDARG\n", hres);
1702
1703     hres = CreateAsyncBindCtxEx(NULL, 0, NULL, NULL, &bctx, 0);
1704     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
1705
1706     if(SUCCEEDED(hres)) {
1707         bindopts.cbStruct = sizeof(bindopts);
1708         hres = IBindCtx_GetBindOptions(bctx, &bindopts);
1709         ok(SUCCEEDED(hres), "IBindCtx_GetBindOptions failed: %08x\n", hres);
1710         ok(bindopts.grfFlags == BIND_MAYBOTHERUSER,
1711                 "bindopts.grfFlags = %08x, expected: BIND_MAYBOTHERUSER\n", bindopts.grfFlags);
1712         ok(bindopts.grfMode == (STGM_READWRITE | STGM_SHARE_EXCLUSIVE),
1713                 "bindopts.grfMode = %08x, expected: STGM_READWRITE | STGM_SHARE_EXCLUSIVE\n",
1714                 bindopts.grfMode);
1715         ok(bindopts.dwTickCountDeadline == 0,
1716                 "bindopts.dwTickCountDeadline = %08x, expected: 0\n", bindopts.dwTickCountDeadline);
1717
1718         IBindCtx_Release(bctx);
1719     }
1720
1721     CreateBindCtx(0, &bctx_arg);
1722     hres = CreateAsyncBindCtxEx(NULL, 0, NULL, NULL, &bctx, 0);
1723     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
1724
1725     if(SUCCEEDED(hres)) {
1726         bindopts.cbStruct = sizeof(bindopts);
1727         hres = IBindCtx_GetBindOptions(bctx, &bindopts);
1728         ok(SUCCEEDED(hres), "IBindCtx_GetBindOptions failed: %08x\n", hres);
1729         ok(bindopts.grfFlags == BIND_MAYBOTHERUSER,
1730                 "bindopts.grfFlags = %08x, expected: BIND_MAYBOTHERUSER\n", bindopts.grfFlags);
1731         ok(bindopts.grfMode == (STGM_READWRITE | STGM_SHARE_EXCLUSIVE),
1732                 "bindopts.grfMode = %08x, expected: STGM_READWRITE | STGM_SHARE_EXCLUSIVE\n",
1733                 bindopts.grfMode);
1734         ok(bindopts.dwTickCountDeadline == 0,
1735                 "bindopts.dwTickCountDeadline = %08x, expected: 0\n", bindopts.dwTickCountDeadline);
1736
1737         IBindCtx_Release(bctx);
1738     }
1739
1740     IBindCtx_Release(bctx_arg);
1741
1742     SET_EXPECT(QueryInterface_IServiceProvider);
1743     hres = CreateAsyncBindCtxEx(NULL, 0, &bsc, NULL, &bctx, 0);
1744     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
1745     CHECK_CALLED(QueryInterface_IServiceProvider);
1746
1747     hres = IBindCtx_QueryInterface(bctx, &IID_IAsyncBindCtx, (void**)&unk);
1748     ok(hres == S_OK, "QueryInterface(IID_IAsyncBindCtx) failed: %08x\n", hres);
1749     if(SUCCEEDED(hres))
1750         IUnknown_Release(unk);
1751
1752     IBindCtx_Release(bctx);
1753
1754     hres = CreateBindCtx(0, &bctx2);
1755     ok(hres == S_OK, "CreateBindCtx failed: %08x\n", hres);
1756
1757     hres = CreateAsyncBindCtxEx(bctx2, 0, NULL, NULL, &bctx, 0);
1758     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
1759
1760     hres = IBindCtx_RegisterObjectParam(bctx2, testW, (IUnknown*)&Protocol);
1761     ok(hres == S_OK, "RegisterObjectParam failed: %08x\n", hres);
1762
1763     hres = IBindCtx_GetObjectParam(bctx, testW, &unk);
1764     ok(hres == S_OK, "GetObjectParam failed: %08x\n", hres);
1765     ok(unk == (IUnknown*)&Protocol, "unexpected unk %p\n", unk);
1766
1767     IBindCtx_Release(bctx);
1768     IBindCtx_Release(bctx2);
1769 }
1770
1771 static void test_bscholder(IBindStatusCallback *holder)
1772 {
1773     IServiceProvider *serv_prov;
1774     IHttpNegotiate *http_negotiate, *http_negotiate_serv;
1775     IHttpNegotiate2 *http_negotiate2, *http_negotiate2_serv;
1776     IAuthenticate *authenticate, *authenticate_serv;
1777     IInternetProtocol *protocol;
1778     BINDINFO bindinfo = {sizeof(bindinfo)};
1779     LPWSTR wstr;
1780     DWORD dw;
1781     HRESULT hres;
1782
1783     hres = IBindStatusCallback_QueryInterface(holder, &IID_IServiceProvider, (void**)&serv_prov);
1784     ok(hres == S_OK, "Could not get IServiceProvider interface: %08x\n", hres);
1785
1786     dw = 0xdeadbeef;
1787     SET_EXPECT(GetBindInfo);
1788     hres = IBindStatusCallback_GetBindInfo(holder, &dw, &bindinfo);
1789     ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
1790     CHECK_CALLED(GetBindInfo);
1791
1792     SET_EXPECT(OnStartBinding);
1793     hres = IBindStatusCallback_OnStartBinding(holder, 0, (void*)0xdeadbeef);
1794     ok(hres == S_OK, "OnStartBinding failed: %08x\n", hres);
1795     CHECK_CALLED(OnStartBinding);
1796
1797     hres = IBindStatusCallback_QueryInterface(holder, &IID_IHttpNegotiate, (void**)&http_negotiate);
1798     ok(hres == S_OK, "Could not get IHttpNegotiate interface: %08x\n", hres);
1799
1800     wstr = (void*)0xdeadbeef;
1801     hres = IHttpNegotiate_BeginningTransaction(http_negotiate, urls[test_protocol], (void*)0xdeadbeef, 0xff, &wstr);
1802     ok(hres == S_OK, "BeginningTransaction failed: %08x\n", hres);
1803     ok(wstr == NULL, "wstr = %p\n", wstr);
1804
1805     SET_EXPECT(QueryInterface_IHttpNegotiate);
1806     hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpNegotiate, &IID_IHttpNegotiate,
1807                                          (void**)&http_negotiate_serv);
1808     ok(hres == S_OK, "Could not get IHttpNegotiate service: %08x\n", hres);
1809     CHECK_CALLED(QueryInterface_IHttpNegotiate);
1810
1811     ok(http_negotiate == http_negotiate_serv, "http_negotiate != http_negotiate_serv\n");
1812
1813     wstr = (void*)0xdeadbeef;
1814     SET_EXPECT(BeginningTransaction);
1815     hres = IHttpNegotiate_BeginningTransaction(http_negotiate_serv, urls[test_protocol], emptyW, 0, &wstr);
1816     CHECK_CALLED(BeginningTransaction);
1817     ok(hres == S_OK, "BeginningTransaction failed: %08x\n", hres);
1818     ok(wstr == NULL, "wstr = %p\n", wstr);
1819
1820     IHttpNegotiate_Release(http_negotiate_serv);
1821
1822     hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpNegotiate, &IID_IHttpNegotiate,
1823                                          (void**)&http_negotiate_serv);
1824     ok(hres == S_OK, "Could not get IHttpNegotiate service: %08x\n", hres);
1825     ok(http_negotiate == http_negotiate_serv, "http_negotiate != http_negotiate_serv\n");
1826     IHttpNegotiate_Release(http_negotiate_serv);
1827
1828     hres = IBindStatusCallback_QueryInterface(holder, &IID_IHttpNegotiate2, (void**)&http_negotiate2);
1829     if(SUCCEEDED(hres)) {
1830         have_IHttpNegotiate2 = TRUE;
1831         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, (void*)0xdeadbeef, (void*)0xdeadbeef, 0);
1832         ok(hres == E_FAIL, "GetRootSecurityId failed: %08x\n", hres);
1833
1834         SET_EXPECT(QueryInterface_IHttpNegotiate2);
1835         hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpNegotiate2, &IID_IHttpNegotiate2,
1836                                              (void**)&http_negotiate2_serv);
1837         ok(hres == S_OK, "Could not get IHttpNegotiate2 service: %08x\n", hres);
1838         CHECK_CALLED(QueryInterface_IHttpNegotiate2);
1839         ok(http_negotiate2 == http_negotiate2_serv, "http_negotiate != http_negotiate_serv\n");
1840
1841         SET_EXPECT(GetRootSecurityId);
1842         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, (void*)0xdeadbeef, (void*)0xdeadbeef, 0);
1843         ok(hres == E_NOTIMPL, "GetRootSecurityId failed: %08x\n", hres);
1844         CHECK_CALLED(GetRootSecurityId);
1845
1846         IHttpNegotiate_Release(http_negotiate2_serv);
1847         IHttpNegotiate_Release(http_negotiate2);
1848     }else {
1849         skip("Could not get IHttpNegotiate2\n");
1850     }
1851
1852     SET_EXPECT(OnProgress_FINDINGRESOURCE);
1853     hres = IBindStatusCallback_OnProgress(holder, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
1854     ok(hres == S_OK, "OnProgress failed: %08x\n", hres);
1855     CHECK_CALLED(OnProgress_FINDINGRESOURCE);
1856
1857     SET_EXPECT(OnResponse);
1858     wstr = (void*)0xdeadbeef;
1859     hres = IHttpNegotiate_OnResponse(http_negotiate, 200, emptyW, NULL, NULL);
1860     ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
1861     CHECK_CALLED(OnResponse);
1862
1863     IHttpNegotiate_Release(http_negotiate);
1864
1865     hres = IBindStatusCallback_QueryInterface(holder, &IID_IAuthenticate, (void**)&authenticate);
1866     ok(hres == S_OK, "Could not get IAuthenticate interface: %08x\n", hres);
1867
1868     SET_EXPECT(QueryInterface_IAuthenticate);
1869     SET_EXPECT(QueryService_IAuthenticate);
1870     hres = IServiceProvider_QueryService(serv_prov, &IID_IAuthenticate, &IID_IAuthenticate,
1871                                          (void**)&authenticate_serv);
1872     ok(hres == S_OK, "Could not get IAuthenticate service: %08x\n", hres);
1873     CHECK_CALLED(QueryInterface_IAuthenticate);
1874     CHECK_CALLED(QueryService_IAuthenticate);
1875     ok(authenticate == authenticate_serv, "authenticate != authenticate_serv\n");
1876     IAuthenticate_Release(authenticate_serv);
1877
1878     hres = IServiceProvider_QueryService(serv_prov, &IID_IAuthenticate, &IID_IAuthenticate,
1879                                          (void**)&authenticate_serv);
1880     ok(hres == S_OK, "Could not get IAuthenticate service: %08x\n", hres);
1881     ok(authenticate == authenticate_serv, "authenticate != authenticate_serv\n");
1882
1883     IAuthenticate_Release(authenticate);
1884     IAuthenticate_Release(authenticate_serv);
1885
1886     SET_EXPECT(OnStopBinding);
1887     hres = IBindStatusCallback_OnStopBinding(holder, S_OK, NULL);
1888     ok(hres == S_OK, "OnStopBinding failed: %08x\n", hres);
1889     CHECK_CALLED(OnStopBinding);
1890
1891     SET_EXPECT(QueryInterface_IInternetProtocol);
1892     SET_EXPECT(QueryService_IInternetProtocol);
1893     hres = IServiceProvider_QueryService(serv_prov, &IID_IInternetProtocol, &IID_IInternetProtocol,
1894                                          (void**)&protocol);
1895     ok(hres == E_NOINTERFACE, "QueryService(IInternetProtocol) failed: %08x\n", hres);
1896     CHECK_CALLED(QueryInterface_IInternetProtocol);
1897     CHECK_CALLED(QueryService_IInternetProtocol);
1898
1899     IServiceProvider_Release(serv_prov);
1900 }
1901
1902 static void test_RegisterBindStatusCallback(void)
1903 {
1904     IBindStatusCallback *prevbsc, *clb;
1905     IBindCtx *bindctx;
1906     IUnknown *unk;
1907     HRESULT hres;
1908
1909     hres = CreateBindCtx(0, &bindctx);
1910     ok(hres == S_OK, "BindCtx failed: %08x\n", hres);
1911
1912     SET_EXPECT(QueryInterface_IServiceProvider);
1913
1914     hres = IBindCtx_RegisterObjectParam(bindctx, BSCBHolder, (IUnknown*)&bsc);
1915     ok(hres == S_OK, "RegisterObjectParam failed: %08x\n", hres);
1916
1917     SET_EXPECT(QueryInterface_IBindStatusCallback);
1918     SET_EXPECT(QueryInterface_IBindStatusCallbackHolder);
1919     prevbsc = (void*)0xdeadbeef;
1920     hres = RegisterBindStatusCallback(bindctx, &bsc, &prevbsc, 0);
1921     ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
1922     ok(prevbsc == &bsc, "prevbsc=%p\n", prevbsc);
1923     CHECK_CALLED(QueryInterface_IBindStatusCallback);
1924     CHECK_CALLED(QueryInterface_IBindStatusCallbackHolder);
1925
1926     CHECK_CALLED(QueryInterface_IServiceProvider);
1927
1928     hres = IBindCtx_GetObjectParam(bindctx, BSCBHolder, &unk);
1929     ok(hres == S_OK, "GetObjectParam failed: %08x\n", hres);
1930
1931     hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)&clb);
1932     IUnknown_Release(unk);
1933     ok(hres == S_OK, "QueryInterface(IID_IBindStatusCallback) failed: %08x\n", hres);
1934     ok(clb != &bsc, "bsc == clb\n");
1935
1936     test_bscholder(clb);
1937
1938     IBindStatusCallback_Release(clb);
1939
1940     hres = RevokeBindStatusCallback(bindctx, &bsc);
1941     ok(hres == S_OK, "RevokeBindStatusCallback failed: %08x\n", hres);
1942
1943     unk = (void*)0xdeadbeef;
1944     hres = IBindCtx_GetObjectParam(bindctx, BSCBHolder, &unk);
1945     ok(hres == E_FAIL, "GetObjectParam failed: %08x\n", hres);
1946     ok(unk == NULL, "unk != NULL\n");
1947
1948     if(unk)
1949         IUnknown_Release(unk);
1950
1951     hres = RevokeBindStatusCallback(bindctx, (void*)0xdeadbeef);
1952     ok(hres == S_OK, "RevokeBindStatusCallback failed: %08x\n", hres);
1953
1954     hres = RevokeBindStatusCallback(NULL, (void*)0xdeadbeef);
1955     ok(hres == E_INVALIDARG, "RevokeBindStatusCallback failed: %08x\n", hres);
1956
1957     hres = RevokeBindStatusCallback(bindctx, NULL);
1958     ok(hres == E_INVALIDARG, "RevokeBindStatusCallback failed: %08x\n", hres);
1959
1960     IBindCtx_Release(bindctx);
1961 }
1962
1963 #define BINDTEST_EMULATE     1
1964 #define BINDTEST_TOOBJECT    2
1965 #define BINDTEST_FILEDWLAPI  4
1966
1967 static void init_bind_test(int protocol, DWORD flags, DWORD t)
1968 {
1969     test_protocol = protocol;
1970     emulate_protocol = (flags & BINDTEST_EMULATE) != 0;
1971     download_state = BEFORE_DOWNLOAD;
1972     stopped_binding = FALSE;
1973     stopped_obj_binding = FALSE;
1974     data_available = FALSE;
1975     mime_type[0] = 0;
1976     binding_hres = S_OK;
1977     bind_to_object = (flags & BINDTEST_TOOBJECT) != 0;
1978     tymed = t;
1979     filedwl_api = (flags & BINDTEST_FILEDWLAPI) != 0;
1980 }
1981
1982 static void test_BindToStorage(int protocol, BOOL emul, DWORD t)
1983 {
1984     IMoniker *mon;
1985     HRESULT hres;
1986     LPOLESTR display_name;
1987     IBindCtx *bctx;
1988     MSG msg;
1989     IBindStatusCallback *previousclb;
1990     IUnknown *unk = (IUnknown*)0x00ff00ff;
1991     IBinding *bind;
1992
1993     init_bind_test(protocol, emul ? BINDTEST_EMULATE : 0, t);
1994
1995     SET_EXPECT(QueryInterface_IServiceProvider);
1996     hres = CreateAsyncBindCtx(0, &bsc, NULL, &bctx);
1997     ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n\n", hres);
1998     CHECK_CALLED(QueryInterface_IServiceProvider);
1999     if(FAILED(hres))
2000         return;
2001
2002     SET_EXPECT(QueryInterface_IServiceProvider);
2003     hres = RegisterBindStatusCallback(bctx, &bsc, &previousclb, 0);
2004     ok(hres == S_OK, "RegisterBindStatusCallback failed: %08x\n", hres);
2005     ok(previousclb == &bsc, "previousclb(%p) != sclb(%p)\n", previousclb, &bsc);
2006     CHECK_CALLED(QueryInterface_IServiceProvider);
2007     if(previousclb)
2008         IBindStatusCallback_Release(previousclb);
2009
2010     hres = CreateURLMoniker(NULL, test_protocol == FILE_TEST ? file_url : urls[test_protocol], &mon);
2011     ok(SUCCEEDED(hres), "failed to create moniker: %08x\n", hres);
2012     if(FAILED(hres)) {
2013         IBindCtx_Release(bctx);
2014         return;
2015     }
2016
2017     hres = IMoniker_QueryInterface(mon, &IID_IBinding, (void**)&bind);
2018     ok(hres == E_NOINTERFACE, "IMoniker should not have IBinding interface\n");
2019     if(SUCCEEDED(hres))
2020         IBinding_Release(bind);
2021
2022     hres = IMoniker_GetDisplayName(mon, bctx, NULL, &display_name);
2023     ok(hres == S_OK, "GetDisplayName failed %08x\n", hres);
2024     ok(!lstrcmpW(display_name, urls[test_protocol]),
2025        "GetDisplayName got wrong name %s\n", debugstr_w(display_name));
2026     CoTaskMemFree(display_name);
2027
2028     if(tymed == TYMED_FILE && (test_protocol == ABOUT_TEST || test_protocol == ITS_TEST))
2029         binding_hres = INET_E_DATA_NOT_AVAILABLE;
2030
2031     SET_EXPECT(GetBindInfo);
2032     SET_EXPECT(QueryInterface_IInternetProtocol);
2033     if(!emulate_protocol)
2034         SET_EXPECT(QueryService_IInternetProtocol);
2035     SET_EXPECT(OnStartBinding);
2036     if(emulate_protocol) {
2037         SET_EXPECT(Start);
2038         if(test_protocol == HTTP_TEST)
2039             SET_EXPECT(Terminate);
2040         if(tymed != TYMED_FILE || (test_protocol != ABOUT_TEST && test_protocol != ITS_TEST))
2041             SET_EXPECT(UnlockRequest);
2042     }else {
2043         if(test_protocol == HTTP_TEST) {
2044             SET_EXPECT(QueryInterface_IInternetBindInfo);
2045             SET_EXPECT(QueryService_IInternetBindInfo);
2046             SET_EXPECT(QueryInterface_IHttpNegotiate);
2047             SET_EXPECT(BeginningTransaction);
2048             SET_EXPECT(QueryInterface_IHttpNegotiate2);
2049             SET_EXPECT(GetRootSecurityId);
2050             SET_EXPECT(OnProgress_FINDINGRESOURCE);
2051             SET_EXPECT(OnProgress_CONNECTING);
2052         }
2053         if(test_protocol == HTTP_TEST || test_protocol == FILE_TEST)
2054             SET_EXPECT(OnProgress_SENDINGREQUEST);
2055         if(test_protocol == HTTP_TEST)
2056             SET_EXPECT(OnResponse);
2057         SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
2058         SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
2059         if(test_protocol == FILE_TEST)
2060             SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
2061         if(test_protocol == HTTP_TEST)
2062             SET_EXPECT(OnProgress_DOWNLOADINGDATA);
2063         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
2064         if(tymed != TYMED_FILE || test_protocol != ABOUT_TEST)
2065             SET_EXPECT(OnDataAvailable);
2066         SET_EXPECT(OnStopBinding);
2067     }
2068
2069     hres = IMoniker_BindToStorage(mon, bctx, NULL, tymed == TYMED_ISTREAM ? &IID_IStream : &IID_IUnknown, (void**)&unk);
2070     if (test_protocol == HTTP_TEST && hres == HRESULT_FROM_WIN32(ERROR_INTERNET_NAME_NOT_RESOLVED))
2071     {
2072         skip("Network unreachable, skipping tests\n");
2073         return;
2074     }
2075
2076     if(((bindf & BINDF_ASYNCHRONOUS) && !data_available)
2077        || (tymed == TYMED_FILE && test_protocol == FILE_TEST)) {
2078         ok(hres == MK_S_ASYNCHRONOUS, "IMoniker_BindToStorage failed: %08x\n", hres);
2079         ok(unk == NULL, "istr should be NULL\n");
2080     }else if(tymed == TYMED_FILE && test_protocol == ABOUT_TEST) {
2081         ok(hres == INET_E_DATA_NOT_AVAILABLE,
2082            "IMoniker_BindToStorage failed: %08x, expected INET_E_DATA_NOT_AVAILABLE\n", hres);
2083         ok(unk == NULL, "istr should be NULL\n");
2084     }else {
2085         ok(hres == S_OK, "IMoniker_BindToStorage failed: %08x\n", hres);
2086         ok(unk != NULL, "unk == NULL\n");
2087     }
2088     if(unk)
2089         IUnknown_Release(unk);
2090
2091     if(FAILED(hres))
2092         return;
2093
2094     while((bindf & BINDF_ASYNCHRONOUS) &&
2095           !stopped_binding && GetMessage(&msg,NULL,0,0)) {
2096         TranslateMessage(&msg);
2097         DispatchMessage(&msg);
2098     }
2099
2100     CHECK_CALLED(GetBindInfo);
2101     CHECK_CALLED(QueryInterface_IInternetProtocol);
2102     if(!emulate_protocol)
2103         CHECK_CALLED(QueryService_IInternetProtocol);
2104     CHECK_CALLED(OnStartBinding);
2105     if(emulate_protocol) {
2106         CHECK_CALLED(Start);
2107         if(test_protocol == HTTP_TEST) {
2108             if(tymed == TYMED_FILE)
2109                 CLEAR_CALLED(Read);
2110             CHECK_CALLED(Terminate);
2111         }
2112         if(tymed != TYMED_FILE || (test_protocol != ABOUT_TEST && test_protocol != ITS_TEST))
2113             CHECK_CALLED(UnlockRequest);
2114     }else {
2115         if(test_protocol == HTTP_TEST) {
2116             CLEAR_CALLED(QueryInterface_IInternetBindInfo);
2117             CLEAR_CALLED(QueryService_IInternetBindInfo);
2118             CHECK_CALLED(QueryInterface_IHttpNegotiate);
2119             CHECK_CALLED(BeginningTransaction);
2120             if (have_IHttpNegotiate2)
2121             {
2122                 CHECK_CALLED(QueryInterface_IHttpNegotiate2);
2123                 CHECK_CALLED(GetRootSecurityId);
2124             }
2125             if(http_is_first) {
2126                 CHECK_CALLED(OnProgress_FINDINGRESOURCE);
2127                 CHECK_CALLED(OnProgress_CONNECTING);
2128             }else todo_wine {
2129                 CHECK_NOT_CALLED(OnProgress_FINDINGRESOURCE);
2130                 /* IE7 does call this */
2131                 CLEAR_CALLED(OnProgress_CONNECTING);
2132             }
2133         }
2134         if(test_protocol == HTTP_TEST || test_protocol == FILE_TEST)
2135             CHECK_CALLED(OnProgress_SENDINGREQUEST);
2136         if(test_protocol == HTTP_TEST)
2137             CHECK_CALLED(OnResponse);
2138         CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
2139         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
2140         if(test_protocol == FILE_TEST)
2141             CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
2142         if(test_protocol == HTTP_TEST)
2143             CLEAR_CALLED(OnProgress_DOWNLOADINGDATA);
2144         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
2145         if(tymed != TYMED_FILE || test_protocol != ABOUT_TEST)
2146             CHECK_CALLED(OnDataAvailable);
2147         CHECK_CALLED(OnStopBinding);
2148     }
2149
2150     ok(IMoniker_Release(mon) == 0, "mon should be destroyed here\n");
2151     ok(IBindCtx_Release(bctx) == 0, "bctx should be destroyed here\n");
2152
2153     if(test_protocol == HTTP_TEST)
2154         http_is_first = FALSE;
2155 }
2156
2157 static void test_BindToObject(int protocol, BOOL emul)
2158 {
2159     IMoniker *mon;
2160     HRESULT hres;
2161     LPOLESTR display_name;
2162     IBindCtx *bctx;
2163     DWORD regid;
2164     MSG msg;
2165     IUnknown *unk = (IUnknown*)0x00ff00ff;
2166     IBinding *bind;
2167
2168     init_bind_test(protocol, BINDTEST_TOOBJECT | (emul ? BINDTEST_EMULATE : 0), TYMED_ISTREAM);
2169
2170     if(emul)
2171         CoRegisterClassObject(&CLSID_HTMLDocument, (IUnknown *)&mime_cf,
2172                               CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
2173
2174     SET_EXPECT(QueryInterface_IServiceProvider);
2175     hres = CreateAsyncBindCtx(0, &objbsc, NULL, &bctx);
2176     ok(SUCCEEDED(hres), "CreateAsyncBindCtx failed: %08x\n\n", hres);
2177     CHECK_CALLED(QueryInterface_IServiceProvider);
2178     if(FAILED(hres))
2179         return;
2180
2181     hres = CreateURLMoniker(NULL, test_protocol == FILE_TEST ? file_url : urls[test_protocol], &mon);
2182     ok(SUCCEEDED(hres), "failed to create moniker: %08x\n", hres);
2183     if(FAILED(hres)) {
2184         IBindCtx_Release(bctx);
2185         return;
2186     }
2187
2188     hres = IMoniker_QueryInterface(mon, &IID_IBinding, (void**)&bind);
2189     ok(hres == E_NOINTERFACE, "IMoniker should not have IBinding interface\n");
2190     if(SUCCEEDED(hres))
2191         IBinding_Release(bind);
2192
2193     hres = IMoniker_GetDisplayName(mon, bctx, NULL, &display_name);
2194     ok(hres == S_OK, "GetDisplayName failed %08x\n", hres);
2195     ok(!lstrcmpW(display_name, urls[test_protocol]), "GetDisplayName got wrong name\n");
2196
2197     SET_EXPECT(Obj_GetBindInfo);
2198     SET_EXPECT(QueryInterface_IInternetProtocol);
2199     if(!emulate_protocol)
2200         SET_EXPECT(QueryService_IInternetProtocol);
2201     SET_EXPECT(Obj_OnStartBinding);
2202     if(emulate_protocol) {
2203         SET_EXPECT(Start);
2204         if(test_protocol == HTTP_TEST)
2205             SET_EXPECT(Terminate);
2206         if(test_protocol == FILE_TEST)
2207             SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
2208         SET_EXPECT(UnlockRequest);
2209     }else {
2210         if(test_protocol == HTTP_TEST) {
2211             SET_EXPECT(QueryInterface_IHttpNegotiate);
2212             SET_EXPECT(BeginningTransaction);
2213             SET_EXPECT(QueryInterface_IHttpNegotiate2);
2214             SET_EXPECT(GetRootSecurityId);
2215             SET_EXPECT(Obj_OnProgress_FINDINGRESOURCE);
2216             SET_EXPECT(Obj_OnProgress_CONNECTING);
2217         }
2218         if(test_protocol == HTTP_TEST || test_protocol == FILE_TEST)
2219             SET_EXPECT(Obj_OnProgress_SENDINGREQUEST);
2220         if(test_protocol == HTTP_TEST)
2221             SET_EXPECT(OnResponse);
2222         SET_EXPECT(Obj_OnProgress_MIMETYPEAVAILABLE);
2223         SET_EXPECT(Obj_OnProgress_BEGINDOWNLOADDATA);
2224         if(test_protocol == FILE_TEST)
2225             SET_EXPECT(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
2226         if(test_protocol == HTTP_TEST)
2227             SET_EXPECT(OnProgress_DOWNLOADINGDATA);
2228         SET_EXPECT(Obj_OnProgress_ENDDOWNLOADDATA);
2229         SET_EXPECT(Obj_OnProgress_CLASSIDAVAILABLE);
2230         SET_EXPECT(Obj_OnProgress_BEGINSYNCOPERATION);
2231         SET_EXPECT(Obj_OnProgress_ENDSYNCOPERATION);
2232         SET_EXPECT(OnObjectAvailable);
2233         SET_EXPECT(Obj_OnStopBinding);
2234     }
2235
2236     hres = IMoniker_BindToObject(mon, bctx, NULL, &IID_IUnknown, (void**)&unk);
2237
2238     if (test_protocol == HTTP_TEST && hres == HRESULT_FROM_WIN32(ERROR_INTERNET_NAME_NOT_RESOLVED))
2239     {
2240         skip( "Network unreachable, skipping tests\n" );
2241         return;
2242     }
2243
2244     /* no point testing the calls if binding didn't even work */
2245     if (FAILED(hres)) return;
2246
2247     if(bindf & BINDF_ASYNCHRONOUS) {
2248         ok(hres == MK_S_ASYNCHRONOUS, "IMoniker_BindToObject failed: %08x\n", hres);
2249         ok(unk == NULL, "istr should be NULL\n");
2250     }else {
2251         ok(hres == S_OK, "IMoniker_BindToStorage failed: %08x\n", hres);
2252         ok(unk != NULL, "unk == NULL\n");
2253         if(emul)
2254             ok(unk == (IUnknown*)&PersistMoniker, "unk != PersistMoniker\n");
2255     }
2256     if(unk)
2257         IUnknown_Release(unk);
2258
2259     while((bindf & BINDF_ASYNCHRONOUS) &&
2260           !((!emul || stopped_binding) && stopped_obj_binding) && GetMessage(&msg,NULL,0,0)) {
2261         TranslateMessage(&msg);
2262         DispatchMessage(&msg);
2263     }
2264
2265     CHECK_CALLED(Obj_GetBindInfo);
2266     CHECK_CALLED(QueryInterface_IInternetProtocol);
2267     if(!emulate_protocol)
2268         CHECK_CALLED(QueryService_IInternetProtocol);
2269     CHECK_CALLED(Obj_OnStartBinding);
2270     if(emulate_protocol) {
2271         CHECK_CALLED(Start);
2272         if(test_protocol == HTTP_TEST)
2273             CHECK_CALLED(Terminate);
2274         if(test_protocol == FILE_TEST)
2275             CLEAR_CALLED(OnProgress_MIMETYPEAVAILABLE); /* not called in IE7 */
2276         CHECK_CALLED(UnlockRequest);
2277     }else {
2278         if(test_protocol == HTTP_TEST) {
2279             CHECK_CALLED(QueryInterface_IHttpNegotiate);
2280             CHECK_CALLED(BeginningTransaction);
2281             if (have_IHttpNegotiate2)
2282             {
2283                 CHECK_CALLED(QueryInterface_IHttpNegotiate2);
2284                 CHECK_CALLED(GetRootSecurityId);
2285             }
2286             if(http_is_first) {
2287                 CHECK_CALLED(Obj_OnProgress_FINDINGRESOURCE);
2288                 CHECK_CALLED(Obj_OnProgress_CONNECTING);
2289             }else todo_wine {
2290                 CHECK_NOT_CALLED(Obj_OnProgress_FINDINGRESOURCE);
2291                 /* IE7 does call this */
2292                 CLEAR_CALLED(Obj_OnProgress_CONNECTING);
2293             }
2294         }
2295         if(test_protocol == HTTP_TEST || test_protocol == FILE_TEST) {
2296             if(urls[test_protocol] == SHORT_RESPONSE_URL)
2297                 CLEAR_CALLED(Obj_OnProgress_SENDINGREQUEST);
2298             else
2299                 CHECK_CALLED(Obj_OnProgress_SENDINGREQUEST);
2300         }
2301         if(test_protocol == HTTP_TEST)
2302             CHECK_CALLED(OnResponse);
2303         CHECK_CALLED(Obj_OnProgress_MIMETYPEAVAILABLE);
2304         CHECK_CALLED(Obj_OnProgress_BEGINDOWNLOADDATA);
2305         if(test_protocol == FILE_TEST)
2306             CHECK_CALLED(Obj_OnProgress_CACHEFILENAMEAVAILABLE);
2307         if(test_protocol == HTTP_TEST)
2308             CLEAR_CALLED(OnProgress_DOWNLOADINGDATA);
2309         CLEAR_CALLED(Obj_OnProgress_ENDDOWNLOADDATA);
2310         CHECK_CALLED(Obj_OnProgress_CLASSIDAVAILABLE);
2311         CHECK_CALLED(Obj_OnProgress_BEGINSYNCOPERATION);
2312         CHECK_CALLED(Obj_OnProgress_ENDSYNCOPERATION);
2313         CHECK_CALLED(OnObjectAvailable);
2314         CHECK_CALLED(Obj_OnStopBinding);
2315     }
2316
2317     if(test_protocol != HTTP_TEST || emul || urls[test_protocol] == SHORT_RESPONSE_URL) {
2318         ok(IMoniker_Release(mon) == 0, "mon should be destroyed here\n");
2319         ok(IBindCtx_Release(bctx) == 0, "bctx should be destroyed here\n");
2320     }else {
2321         todo_wine ok(IMoniker_Release(mon) == 0, "mon should be destroyed here\n");
2322
2323         if(bindf & BINDF_ASYNCHRONOUS)
2324             ok(IBindCtx_Release(bctx) != 0, "bctx should not be destroyed here\n");
2325         else
2326             todo_wine ok(IBindCtx_Release(bctx) == 0, "bctx should be destroyed here\n");
2327     }
2328
2329     if(emul)
2330         CoRevokeClassObject(regid);
2331
2332     if(test_protocol == HTTP_TEST)
2333         http_is_first = FALSE;
2334 }
2335
2336 static void test_URLDownloadToFile(DWORD prot, BOOL emul)
2337 {
2338     BOOL res;
2339     HRESULT hres;
2340
2341     init_bind_test(prot, BINDTEST_FILEDWLAPI | (emul ? BINDTEST_EMULATE : 0), TYMED_FILE);
2342
2343     SET_EXPECT(GetBindInfo);
2344     SET_EXPECT(QueryInterface_IInternetProtocol);
2345     if(!emulate_protocol) {
2346         SET_EXPECT(QueryInterface_IServiceProvider);
2347         SET_EXPECT(QueryService_IInternetProtocol);
2348     }
2349     SET_EXPECT(OnStartBinding);
2350     if(emulate_protocol) {
2351         SET_EXPECT(Start);
2352         SET_EXPECT(UnlockRequest);
2353     }else {
2354         if(test_protocol == HTTP_TEST) {
2355             SET_EXPECT(QueryInterface_IHttpNegotiate);
2356             SET_EXPECT(BeginningTransaction);
2357             SET_EXPECT(QueryInterface_IHttpNegotiate2);
2358             SET_EXPECT(GetRootSecurityId);
2359         }
2360         if(test_protocol == HTTP_TEST || test_protocol == FILE_TEST)
2361             SET_EXPECT(OnProgress_SENDINGREQUEST);
2362         if(test_protocol == HTTP_TEST)
2363             SET_EXPECT(OnResponse);
2364         SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
2365         SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
2366         if(test_protocol == FILE_TEST)
2367             SET_EXPECT(OnProgress_CACHEFILENAMEAVAILABLE);
2368         if(test_protocol == HTTP_TEST)
2369             SET_EXPECT(OnProgress_DOWNLOADINGDATA);
2370         SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
2371         SET_EXPECT(OnStopBinding);
2372     }
2373
2374     hres = URLDownloadToFileW(NULL, test_protocol == FILE_TEST ? file_url : urls[test_protocol], dwl_htmlW, 0, &bsc);
2375     ok(hres == S_OK, "URLDownloadToFile failed: %08x\n", hres);
2376
2377     CHECK_CALLED(GetBindInfo);
2378     CHECK_CALLED(QueryInterface_IInternetProtocol);
2379     if(!emulate_protocol) {
2380         CHECK_CALLED(QueryInterface_IServiceProvider);
2381         CHECK_CALLED(QueryService_IInternetProtocol);
2382     }
2383     CHECK_CALLED(OnStartBinding);
2384     if(emulate_protocol) {
2385         CHECK_CALLED(Start);
2386         CHECK_CALLED(UnlockRequest);
2387     }else {
2388         if(test_protocol == HTTP_TEST) {
2389             CHECK_CALLED(QueryInterface_IHttpNegotiate);
2390             CHECK_CALLED(BeginningTransaction);
2391             if (have_IHttpNegotiate2)
2392             {
2393                 CHECK_CALLED(QueryInterface_IHttpNegotiate2);
2394                 CHECK_CALLED(GetRootSecurityId);
2395             }
2396         }
2397         if(test_protocol == FILE_TEST)
2398             CHECK_CALLED(OnProgress_SENDINGREQUEST);
2399         else if(test_protocol == HTTP_TEST)
2400             CLEAR_CALLED(OnProgress_SENDINGREQUEST); /* not called by IE7 */
2401         if(test_protocol == HTTP_TEST)
2402             CHECK_CALLED(OnResponse);
2403         CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
2404         CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
2405         if(test_protocol == FILE_TEST)
2406             CHECK_CALLED(OnProgress_CACHEFILENAMEAVAILABLE);
2407         if(test_protocol == HTTP_TEST)
2408             CLEAR_CALLED(OnProgress_DOWNLOADINGDATA);
2409         CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
2410         CHECK_CALLED(OnStopBinding);
2411     }
2412
2413     res = DeleteFileA(dwl_htmlA);
2414     ok(res, "DeleteFile failed: %u\n", GetLastError());
2415
2416     if(prot != FILE_TEST || emul)
2417         return;
2418
2419     hres = URLDownloadToFileW(NULL, urls[test_protocol], dwl_htmlW, 0, NULL);
2420     ok(hres == S_OK, "URLDownloadToFile failed: %08x\n", hres);
2421
2422     res = DeleteFileA(dwl_htmlA);
2423     ok(res, "DeleteFile failed: %u\n", GetLastError());
2424 }
2425
2426 static void set_file_url(char *path)
2427 {
2428     CHAR file_urlA[INTERNET_MAX_URL_LENGTH];
2429     CHAR INDEX_HTMLA[MAX_PATH];
2430
2431     lstrcpyA(file_urlA, "file:///");
2432     lstrcatA(file_urlA, path);
2433     MultiByteToWideChar(CP_ACP, 0, file_urlA, -1, file_url, INTERNET_MAX_URL_LENGTH);
2434
2435     lstrcpyA(INDEX_HTMLA, "file://");
2436     lstrcatA(INDEX_HTMLA, path);
2437     MultiByteToWideChar(CP_ACP, 0, INDEX_HTMLA, -1, INDEX_HTML, MAX_PATH);
2438 }
2439
2440 static void create_file(void)
2441 {
2442     HANDLE file;
2443     DWORD size;
2444     CHAR path[MAX_PATH];
2445
2446     static const char html_doc[] = "<HTML></HTML>";
2447
2448     file = CreateFileA(wszIndexHtmlA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2449             FILE_ATTRIBUTE_NORMAL, NULL);
2450     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
2451     if(file == INVALID_HANDLE_VALUE)
2452         return;
2453
2454     WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
2455     CloseHandle(file);
2456
2457     GetCurrentDirectoryA(MAX_PATH, path);
2458     lstrcatA(path, "\\");
2459     lstrcatA(path, wszIndexHtmlA);
2460     set_file_url(path);
2461 }
2462
2463 static void test_ReportResult(HRESULT exhres)
2464 {
2465     IMoniker *mon = NULL;
2466     IBindCtx *bctx = NULL;
2467     IUnknown *unk = (void*)0xdeadbeef;
2468     HRESULT hres;
2469
2470     init_bind_test(ABOUT_TEST, BINDTEST_EMULATE, TYMED_ISTREAM);
2471     binding_hres = exhres;
2472
2473     hres = CreateURLMoniker(NULL, ABOUT_BLANK, &mon);
2474     ok(hres == S_OK, "CreateURLMoniker failed: %08x\n", hres);
2475
2476     SET_EXPECT(QueryInterface_IServiceProvider);
2477     hres = CreateAsyncBindCtx(0, &bsc, NULL, &bctx);
2478     ok(hres == S_OK, "CreateAsyncBindCtx failed: %08x\n\n", hres);
2479     CHECK_CALLED(QueryInterface_IServiceProvider);
2480
2481     SET_EXPECT(GetBindInfo);
2482     SET_EXPECT(QueryInterface_IInternetProtocol);
2483     SET_EXPECT(OnStartBinding);
2484     SET_EXPECT(Start);
2485
2486     hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&unk);
2487     if(SUCCEEDED(exhres))
2488         ok(hres == S_OK || hres == MK_S_ASYNCHRONOUS, "BindToStorage failed: %08x\n", hres);
2489     else
2490         ok(hres == exhres || hres == MK_S_ASYNCHRONOUS,
2491            "BindToStorage failed: %08x, expected %08x or MK_S_ASYNCHRONOUS\n", hres, exhres);
2492
2493     CHECK_CALLED(GetBindInfo);
2494     CHECK_CALLED(QueryInterface_IInternetProtocol);
2495     CHECK_CALLED(OnStartBinding);
2496     CHECK_CALLED(Start);
2497
2498     ok(unk == NULL, "unk=%p\n", unk);
2499
2500     IBindCtx_Release(bctx);
2501     IMoniker_Release(mon);
2502 }
2503
2504 static void test_BindToStorage_fail(void)
2505 {
2506     IMoniker *mon = NULL;
2507     IBindCtx *bctx = NULL;
2508     IUnknown *unk;
2509     HRESULT hres;
2510
2511     hres = CreateURLMoniker(NULL, ABOUT_BLANK, &mon);
2512     ok(hres == S_OK, "CreateURLMoniker failed: %08x\n", hres);
2513     if(FAILED(hres))
2514         return;
2515
2516     hres = CreateAsyncBindCtxEx(NULL, 0, NULL, NULL, &bctx, 0);
2517     ok(hres == S_OK, "CreateAsyncBindCtxEx failed: %08x\n", hres);
2518
2519     hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&unk);
2520     ok(hres == MK_E_SYNTAX || hres == INET_E_DATA_NOT_AVAILABLE,
2521        "hres=%08x, expected MK_E_SYNTAX or INET_E_DATA_NOT_AVAILABLE\n", hres);
2522
2523     IBindCtx_Release(bctx);
2524
2525     IMoniker_Release(mon);
2526
2527     test_ReportResult(E_NOTIMPL);
2528     test_ReportResult(S_FALSE);
2529 }
2530
2531 static void gecko_installer_workaround(BOOL disable)
2532 {
2533     HKEY hkey;
2534     DWORD res;
2535
2536     static BOOL has_url = FALSE;
2537     static char url[2048];
2538
2539     if(!disable && !has_url)
2540         return;
2541
2542     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
2543     if(res != ERROR_SUCCESS)
2544         return;
2545
2546     if(disable) {
2547         DWORD type, size = sizeof(url);
2548
2549         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
2550         if(res == ERROR_SUCCESS && type == REG_SZ)
2551             has_url = TRUE;
2552
2553         RegDeleteValue(hkey, "GeckoUrl");
2554     }else {
2555         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
2556     }
2557
2558     RegCloseKey(hkey);
2559 }
2560
2561 START_TEST(url)
2562 {
2563     gecko_installer_workaround(TRUE);
2564
2565     complete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
2566     complete_event2 = CreateEvent(NULL, FALSE, FALSE, NULL);
2567     thread_id = GetCurrentThreadId();
2568     create_file();
2569
2570     test_create();
2571     test_CreateAsyncBindCtx();
2572     test_CreateAsyncBindCtxEx();
2573     test_RegisterBindStatusCallback();
2574     test_BindToStorage_fail();
2575
2576     trace("synchronous http test (COM not initialised)...\n");
2577     test_BindToStorage(HTTP_TEST, FALSE, TYMED_ISTREAM);
2578
2579     CoInitialize(NULL);
2580
2581     trace("synchronous http test...\n");
2582     test_BindToStorage(HTTP_TEST, FALSE, TYMED_ISTREAM);
2583
2584     trace("synchronous http test (to object)...\n");
2585     test_BindToObject(HTTP_TEST, FALSE);
2586
2587     trace("synchronous file test...\n");
2588     test_BindToStorage(FILE_TEST, FALSE, TYMED_ISTREAM);
2589
2590     trace("synchronous file test (to object)...\n");
2591     test_BindToObject(FILE_TEST, FALSE);
2592
2593     bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
2594
2595     trace("http test...\n");
2596     test_BindToStorage(HTTP_TEST, FALSE, TYMED_ISTREAM);
2597
2598     trace("http test (to file)...\n");
2599     test_BindToStorage(HTTP_TEST, FALSE, TYMED_FILE);
2600
2601     trace("http test (to object)...\n");
2602     test_BindToObject(HTTP_TEST, FALSE);
2603
2604     trace("http test (short response)...\n");
2605     http_is_first = TRUE;
2606     urls[HTTP_TEST] = SHORT_RESPONSE_URL;
2607     test_BindToStorage(HTTP_TEST, FALSE, TYMED_ISTREAM);
2608
2609     trace("http test (short response, to object)...\n");
2610     test_BindToObject(HTTP_TEST, FALSE);
2611
2612     trace("emulated http test...\n");
2613     test_BindToStorage(HTTP_TEST, TRUE, TYMED_ISTREAM);
2614
2615     trace("emulated http test (to object)...\n");
2616     test_BindToObject(HTTP_TEST, TRUE);
2617
2618     trace("emulated http test (to file)...\n");
2619     test_BindToStorage(HTTP_TEST, TRUE, TYMED_FILE);
2620
2621     trace("about test...\n");
2622     test_BindToStorage(ABOUT_TEST, FALSE, TYMED_ISTREAM);
2623
2624     trace("about test (to file)...\n");
2625     test_BindToStorage(ABOUT_TEST, FALSE, TYMED_FILE);
2626
2627     trace("about test (to object)...\n");
2628     test_BindToObject(ABOUT_TEST, FALSE);
2629
2630     trace("emulated about test...\n");
2631     test_BindToStorage(ABOUT_TEST, TRUE, TYMED_ISTREAM);
2632
2633     trace("emulated about test (to file)...\n");
2634     test_BindToStorage(ABOUT_TEST, TRUE, TYMED_FILE);
2635
2636     trace("emulated about test (to object)...\n");
2637     test_BindToObject(ABOUT_TEST, TRUE);
2638
2639     trace("file test...\n");
2640     test_BindToStorage(FILE_TEST, FALSE, TYMED_ISTREAM);
2641
2642     trace("file test (to file)...\n");
2643     test_BindToStorage(FILE_TEST, FALSE, TYMED_FILE);
2644
2645     trace("file test (to object)...\n");
2646     test_BindToObject(FILE_TEST, FALSE);
2647
2648     trace("emulated file test...\n");
2649     test_BindToStorage(FILE_TEST, TRUE, TYMED_ISTREAM);
2650
2651     trace("emulated file test (to file)...\n");
2652     test_BindToStorage(FILE_TEST, TRUE, TYMED_FILE);
2653
2654     trace("emulated file test (to object)...\n");
2655     test_BindToObject(FILE_TEST, TRUE);
2656
2657     trace("emulated its test...\n");
2658     test_BindToStorage(ITS_TEST, TRUE, TYMED_ISTREAM);
2659
2660     trace("emulated its test (to file)...\n");
2661     test_BindToStorage(ITS_TEST, TRUE, TYMED_FILE);
2662
2663     trace("emulated mk test...\n");
2664     test_BindToStorage(MK_TEST, TRUE, TYMED_ISTREAM);
2665
2666     trace("test URLDownloadToFile for file protocol...\n");
2667     test_URLDownloadToFile(FILE_TEST, FALSE);
2668
2669     trace("test URLDownloadToFile for emulated file protocol...\n");
2670     test_URLDownloadToFile(FILE_TEST, TRUE);
2671
2672     trace("test URLDownloadToFile for http protocol...\n");
2673     test_URLDownloadToFile(HTTP_TEST, FALSE);
2674
2675     trace("test failures...\n");
2676     test_BindToStorage_fail();
2677
2678     DeleteFileA(wszIndexHtmlA);
2679     CloseHandle(complete_event);
2680     CloseHandle(complete_event2);
2681     CoUninitialize();
2682
2683     gecko_installer_workaround(FALSE);
2684 }