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