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