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