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