protocol.c 128 KB
Newer Older
1
/*
2
 * Copyright 2005-2011 Jacek Caban for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18 19
 */

#define COBJMACROS
20
#define CONST_VTABLE
21 22 23

#include <wine/test.h>
#include <stdarg.h>
24
#include <stdio.h>
25 26 27 28 29

#include "windef.h"
#include "winbase.h"
#include "ole2.h"
#include "urlmon.h"
30
#include "wininet.h"
31

32 33
static HRESULT (WINAPI *pCoInternetGetSession)(DWORD, IInternetSession **, DWORD);
static HRESULT (WINAPI *pReleaseBindInfo)(BINDINFO*);
34
static HRESULT (WINAPI *pCreateUri)(LPCWSTR, DWORD, DWORD_PTR, IUri**);
35

36 37 38 39 40 41
#define DEFINE_EXPECT(func) \
    static BOOL expect_ ## func = FALSE, called_ ## func = FALSE

#define SET_EXPECT(func) \
    expect_ ## func = TRUE

42
#define CHECK_EXPECT2(func) \
43
    do { \
44
        ok(expect_ ##func, "unexpected call " #func  "\n"); \
45 46
        called_ ## func = TRUE; \
    }while(0)
47

48
#define CHECK_EXPECT(func) \
49
    do { \
50 51
        CHECK_EXPECT2(func);     \
        expect_ ## func = FALSE; \
52
    }while(0)
53 54

#define CHECK_CALLED(func) \
55 56 57 58
    do { \
        ok(called_ ## func, "expected " #func "\n"); \
        expect_ ## func = called_ ## func = FALSE; \
    }while(0)
59

60 61 62 63 64 65
#define CHECK_NOT_CALLED(func) \
    do { \
        ok(!called_ ## func, "unexpected " #func "\n"); \
        expect_ ## func = called_ ## func = FALSE; \
    }while(0)

66 67 68
#define CLEAR_CALLED(func) \
    expect_ ## func = called_ ## func = FALSE

69 70 71
DEFINE_EXPECT(GetBindInfo);
DEFINE_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
DEFINE_EXPECT(ReportProgress_DIRECTBIND);
72
DEFINE_EXPECT(ReportProgress_RAWMIMETYPE);
73 74
DEFINE_EXPECT(ReportProgress_FINDINGRESOURCE);
DEFINE_EXPECT(ReportProgress_CONNECTING);
75
DEFINE_EXPECT(ReportProgress_SENDINGREQUEST);
76
DEFINE_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
77
DEFINE_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
78
DEFINE_EXPECT(ReportProgress_PROTOCOLCLASSID);
79 80
DEFINE_EXPECT(ReportProgress_COOKIE_SENT);
DEFINE_EXPECT(ReportProgress_REDIRECTING);
81 82
DEFINE_EXPECT(ReportProgress_ENCODING);
DEFINE_EXPECT(ReportProgress_ACCEPTRANGES);
83
DEFINE_EXPECT(ReportProgress_PROXYDETECTING);
84 85
DEFINE_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
DEFINE_EXPECT(ReportProgress_DECODING);
86
DEFINE_EXPECT(ReportData);
87
DEFINE_EXPECT(ReportData2);
88
DEFINE_EXPECT(ReportResult);
89 90
DEFINE_EXPECT(GetBindString_ACCEPT_MIMES);
DEFINE_EXPECT(GetBindString_USER_AGENT);
91
DEFINE_EXPECT(GetBindString_POST_COOKIE);
92
DEFINE_EXPECT(GetBindString_URL);
93
DEFINE_EXPECT(QueryService_HttpNegotiate);
94
DEFINE_EXPECT(QueryService_InternetProtocol);
95
DEFINE_EXPECT(QueryService_HttpSecurity);
96 97
DEFINE_EXPECT(QueryInterface_IWinInetInfo);
DEFINE_EXPECT(QueryInterface_IWinInetHttpInfo);
98 99 100 101
DEFINE_EXPECT(BeginningTransaction);
DEFINE_EXPECT(GetRootSecurityId);
DEFINE_EXPECT(OnResponse);
DEFINE_EXPECT(Switch);
102
DEFINE_EXPECT(Continue);
103 104
DEFINE_EXPECT(CreateInstance);
DEFINE_EXPECT(Start);
105
DEFINE_EXPECT(StartEx);
106 107
DEFINE_EXPECT(Terminate);
DEFINE_EXPECT(Read);
108
DEFINE_EXPECT(Read2);
109
DEFINE_EXPECT(SetPriority);
110 111
DEFINE_EXPECT(LockRequest);
DEFINE_EXPECT(UnlockRequest);
112
DEFINE_EXPECT(Abort);
113 114 115 116 117 118 119 120 121 122
DEFINE_EXPECT(MimeFilter_CreateInstance);
DEFINE_EXPECT(MimeFilter_Start);
DEFINE_EXPECT(MimeFilter_ReportData);
DEFINE_EXPECT(MimeFilter_ReportResult);
DEFINE_EXPECT(MimeFilter_Terminate);
DEFINE_EXPECT(MimeFilter_LockRequest);
DEFINE_EXPECT(MimeFilter_UnlockRequest);
DEFINE_EXPECT(MimeFilter_Read);
DEFINE_EXPECT(MimeFilter_Switch);
DEFINE_EXPECT(MimeFilter_Continue);
123 124
DEFINE_EXPECT(Stream_Seek);
DEFINE_EXPECT(Stream_Read);
125

126 127 128 129
static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
static const WCHAR index_url[] =
    {'f','i','l','e',':','i','n','d','e','x','.','h','t','m','l',0};

130 131 132 133 134
static const WCHAR acc_mimeW[] = {'*','/','*',0};
static const WCHAR user_agentW[] = {'W','i','n','e',0};
static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
static const WCHAR hostW[] = {'w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
static const WCHAR winehq_ipW[] = {'2','0','9','.','4','6','.','2','5','.','1','3','4',0};
135
static const WCHAR emptyW[] = {0};
136 137
static const WCHAR pjpegW[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
static const WCHAR gifW[] = {'i','m','a','g','e','/','g','i','f',0};
138

139
static HRESULT expect_hrResult;
140
static LPCWSTR file_name, http_url, expect_wsz;
141
static IInternetProtocol *async_protocol = NULL;
142 143
static BOOL first_data_notif, http_is_first, test_redirect;
static int prot_state, read_report_data, post_stream_read;
144
static DWORD bindf, ex_priority , pi;
145
static IInternetProtocol *binding_protocol, *filtered_protocol;
146
static IInternetBindInfo *prot_bind_info;
147
static IInternetProtocolSink *binding_sink, *filtered_sink;
148
static void *expect_pv;
149
static HANDLE event_complete, event_complete2, event_continue, event_continue_done;
150
static BOOL binding_test;
151
static PROTOCOLDATA protocoldata, *pdata, continue_protdata;
152
static DWORD prot_read, filter_state, http_post_test, thread_id;
153
static BOOL security_problem, test_async_req, impl_protex;
Jacek Caban's avatar
Jacek Caban committed
154
static BOOL async_read_pending, mimefilter_test, direct_read, wait_for_switch, emulate_prot, short_read, test_abort;
155
static BOOL empty_file, no_mime;
156

157 158 159 160 161 162 163
enum {
    STATE_CONNECTING,
    STATE_SENDINGREQUEST,
    STATE_STARTDOWNLOADING,
    STATE_DOWNLOADING
} state;

164 165
static enum {
    FILE_TEST,
166
    HTTP_TEST,
167
    HTTPS_TEST,
168
    FTP_TEST,
169
    MK_TEST,
170
    ITS_TEST,
171
    BIND_TEST
172 173
} tested_protocol;

174 175 176
static const WCHAR protocol_names[][10] = {
    {'f','i','l','e',0},
    {'h','t','t','p',0},
177 178
    {'h','t','t','p','s',0},
    {'f','t','p',0},
179
    {'m','k',0},
180
    {'i','t','s',0},
181 182 183
    {'t','e','s','t',0}
};

184
static const WCHAR binding_urls[][130] = {
185
    {'f','i','l','e',':','t','e','s','t','.','h','t','m','l',0},
186 187
    {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
     'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0},
188 189 190 191 192
    {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
     '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0},
    {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
     '/','p','u','b','/','o','t','h','e','r',
     '/','w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0},
193
    {'m','k',':','t','e','s','t',0},
194
    {'i','t','s',':','t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0},
195 196 197
    {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0}
};

198 199
static const CHAR post_data[] = "mode=Test";

200 201 202 203 204 205 206 207 208 209 210 211
static const char *debugstr_guid(REFIID riid)
{
    static char buf[50];

    sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
            riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
            riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
            riid->Data4[5], riid->Data4[6], riid->Data4[7]);

    return buf;
}

212 213
static int strcmp_wa(LPCWSTR strw, const char *stra)
{
214 215 216 217 218
    CHAR buf[512];
    WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
    return lstrcmpA(stra, buf);
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
static HRESULT WINAPI HttpSecurity_QueryInterface(IHttpSecurity *iface, REFIID riid, void **ppv)
{
    if(IsEqualGUID(&IID_IUnknown, riid)
            || IsEqualGUID(&IID_IHttpSecurity, riid)) {
        *ppv = iface;
        return S_OK;
    }

    ok(0, "unexpected call\n");
    return E_NOINTERFACE;
}

static ULONG WINAPI HttpSecurity_AddRef(IHttpSecurity *iface)
{
    return 2;
}

static ULONG WINAPI HttpSecurity_Release(IHttpSecurity *iface)
{
    return 1;
}

static  HRESULT WINAPI HttpSecurity_GetWindow(IHttpSecurity* iface, REFGUID rguidReason, HWND *phwnd)
{
    trace("HttpSecurity_GetWindow\n");

    return S_FALSE;
}

static HRESULT WINAPI HttpSecurity_OnSecurityProblem(IHttpSecurity *iface, DWORD dwProblem)
{
    trace("Security problem: %u\n", dwProblem);
    ok(dwProblem == ERROR_INTERNET_SEC_CERT_REV_FAILED, "Expected ERROR_INTERNET_SEC_CERT_REV_FAILED got %u\n", dwProblem);

    /* Only retry once */
    if (security_problem)
        return E_ABORT;

    security_problem = TRUE;
    SET_EXPECT(BeginningTransaction);

    return RPC_E_RETRY;
}

static IHttpSecurityVtbl HttpSecurityVtbl = {
    HttpSecurity_QueryInterface,
    HttpSecurity_AddRef,
    HttpSecurity_Release,
    HttpSecurity_GetWindow,
    HttpSecurity_OnSecurityProblem
};

static IHttpSecurity http_security = { &HttpSecurityVtbl };

273
static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface, REFIID riid, void **ppv)
274
{
275 276 277
    if(IsEqualGUID(&IID_IUnknown, riid)
            || IsEqualGUID(&IID_IHttpNegotiate, riid)
            || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
278 279 280
        *ppv = iface;
        return S_OK;
    }
281 282

    ok(0, "unexpected call\n");
283 284 285
    return E_NOINTERFACE;
}

286 287 288 289 290 291 292 293 294 295 296 297 298
static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
{
    return 2;
}

static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
{
    return 1;
}

static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface, LPCWSTR szURL,
        LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
{
299 300 301 302 303 304 305
    LPWSTR addl_headers;

    static const WCHAR wszHeaders[] =
        {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ','a','p','p','l','i','c','a','t',
         'i','o','n','/','x','-','w','w','w','-','f','o','r','m','-','u','r','l','e','n','c','o',
         'd','e','d','\r','\n',0};

306 307
    CHECK_EXPECT(BeginningTransaction);

308
    if(binding_test)
309
        ok(!lstrcmpW(szURL, binding_urls[tested_protocol]), "szURL != http_url\n");
310
    else
311
        ok(!lstrcmpW(szURL, http_url), "szURL != http_url\n");
312
    ok(!dwReserved, "dwReserved=%d, expected 0\n", dwReserved);
313 314
    ok(pszAdditionalHeaders != NULL, "pszAdditionalHeaders == NULL\n");
    if(pszAdditionalHeaders)
315
    {
316
        ok(*pszAdditionalHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
317 318 319
        if (http_post_test)
        {
            addl_headers = CoTaskMemAlloc(sizeof(wszHeaders));
320
            memcpy(addl_headers, wszHeaders, sizeof(wszHeaders));
321 322 323
            *pszAdditionalHeaders = addl_headers;
        }
    }
324 325 326 327 328 329 330 331 332

    return S_OK;
}

static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
        LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
{
    CHECK_EXPECT(OnResponse);

333
    ok(dwResponseCode == 200, "dwResponseCode=%d, expected 200\n", dwResponseCode);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    ok(szResponseHeaders != NULL, "szResponseHeaders == NULL\n");
    ok(szRequestHeaders == NULL, "szRequestHeaders != NULL\n");
    ok(pszAdditionalRequestHeaders == NULL, "pszAdditionalHeaders != NULL\n");

    return S_OK;
}

static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
        BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
{
    static const BYTE sec_id[] = {'h','t','t','p',':','t','e','s','t',1,0,0,0};
    
    CHECK_EXPECT(GetRootSecurityId);

    ok(!dwReserved, "dwReserved=%ld, expected 0\n", dwReserved);
    ok(pbSecurityId != NULL, "pbSecurityId == NULL\n");
    ok(pcbSecurityId != NULL, "pcbSecurityId == NULL\n");

    if(pcbSecurityId) {
353
        ok(*pcbSecurityId == 512, "*pcbSecurityId=%d, expected 512\n", *pcbSecurityId);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        *pcbSecurityId = sizeof(sec_id);
    }

    if(pbSecurityId)
        memcpy(pbSecurityId, sec_id, sizeof(sec_id));

    return E_FAIL;
}

static IHttpNegotiate2Vtbl HttpNegotiateVtbl = {
    HttpNegotiate_QueryInterface,
    HttpNegotiate_AddRef,
    HttpNegotiate_Release,
    HttpNegotiate_BeginningTransaction,
    HttpNegotiate_OnResponse,
    HttpNegotiate_GetRootSecurityId
};

static IHttpNegotiate2 http_negotiate = { &HttpNegotiateVtbl };

static HRESULT QueryInterface(REFIID,void**);

static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
{
    return QueryInterface(riid, ppv);
}

static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
{
    return 2;
}

static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
{
    return 1;
}

static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
        REFIID riid, void **ppv)
{
    if(IsEqualGUID(&IID_IHttpNegotiate, guidService) || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
        CHECK_EXPECT2(QueryService_HttpNegotiate);
        return IHttpNegotiate2_QueryInterface(&http_negotiate, riid, ppv);
    }

399 400 401 402 403 404
    if(IsEqualGUID(&IID_IInternetProtocol, guidService)) {
        ok(IsEqualGUID(&IID_IInternetProtocol, riid), "unexpected riid\n");
        CHECK_EXPECT(QueryService_InternetProtocol);
        return E_NOINTERFACE;
    }

405 406 407 408 409 410
    if(IsEqualGUID(&IID_IHttpSecurity, guidService)) {
        ok(IsEqualGUID(&IID_IHttpSecurity, riid), "unexpected riid\n");
        CHECK_EXPECT(QueryService_HttpSecurity);
        return IHttpSecurity_QueryInterface(&http_security, riid, ppv);
    }

411
    ok(0, "unexpected service %s\n", debugstr_guid(guidService));
412 413 414 415 416 417 418 419 420 421 422 423
    return E_FAIL;
}

static const IServiceProviderVtbl ServiceProviderVtbl = {
    ServiceProvider_QueryInterface,
    ServiceProvider_AddRef,
    ServiceProvider_Release,
    ServiceProvider_QueryService
};

static IServiceProvider service_provider = { &ServiceProviderVtbl };

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
static HRESULT WINAPI Stream_QueryInterface(IStream *iface, REFIID riid, void **ppv)
{
    ok(0, "unexpected call\n");
    return E_NOINTERFACE;
}

static ULONG WINAPI Stream_AddRef(IStream *iface)
{
    return 2;
}

static ULONG WINAPI Stream_Release(IStream *iface)
{
    return 1;
}

static HRESULT WINAPI Stream_Read(IStream *iface, void *pv,
        ULONG cb, ULONG *pcbRead)
{
    CHECK_EXPECT2(Stream_Read);

    ok(GetCurrentThreadId() != thread_id, "wrong thread %d\n", GetCurrentThreadId());

    ok(pv != NULL, "pv == NULL\n");
    ok(cb == 0x20000 || broken(cb == 0x2000), "cb = %d\n", cb);
    ok(pcbRead != NULL, "pcbRead == NULL\n");

    if(post_stream_read) {
        *pcbRead = 0;
        return S_FALSE;
    }

    memcpy(pv, post_data, sizeof(post_data)-1);
    post_stream_read += *pcbRead = sizeof(post_data)-1;
    return S_OK;
}

static HRESULT WINAPI Stream_Write(IStream *iface, const void *pv,
        ULONG cb, ULONG *pcbWritten)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
        DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
{
    CHECK_EXPECT(Stream_Seek);

    ok(!dlibMove.QuadPart, "dlibMove != 0\n");
    ok(dwOrigin == STREAM_SEEK_SET, "dwOrigin = %d\n", dwOrigin);
    ok(!plibNewPosition, "plibNewPosition == NULL\n");

    return S_OK;
}

static HRESULT WINAPI Stream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_CopyTo(IStream *iface, IStream *pstm,
        ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_Commit(IStream *iface, DWORD grfCommitFlags)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_Revert(IStream *iface)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
        ULARGE_INTEGER cb, DWORD dwLockType)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_UnlockRegion(IStream *iface,
        ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_Stat(IStream *iface, STATSTG *pstatstg,
        DWORD dwStatFlag)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Stream_Clone(IStream *iface, IStream **ppstm)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static const IStreamVtbl StreamVtbl = {
    Stream_QueryInterface,
    Stream_AddRef,
    Stream_Release,
    Stream_Read,
    Stream_Write,
    Stream_Seek,
    Stream_SetSize,
    Stream_CopyTo,
    Stream_Commit,
    Stream_Revert,
    Stream_LockRegion,
    Stream_UnlockRegion,
    Stream_Stat,
    Stream_Clone
};

static IStream Stream = { &StreamVtbl };

551 552 553 554 555
static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
{
    return QueryInterface(riid, ppv);
}

556 557 558 559 560 561 562 563 564 565
static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
{
    return 2;
}

static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
{
    return 1;
}

566
static void call_continue(PROTOCOLDATA *protocol_data)
567
{
568 569
    HRESULT hres;

570
    if(state == STATE_CONNECTING) {
571
        if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST || tested_protocol == FTP_TEST) {
572
            if (http_is_first){
573 574
                CLEAR_CALLED(ReportProgress_FINDINGRESOURCE);
                CLEAR_CALLED(ReportProgress_PROXYDETECTING);
575
            }
576
            CLEAR_CALLED(ReportProgress_CONNECTING);
577 578 579
        }
        if(tested_protocol == FTP_TEST)
            todo_wine CHECK_CALLED(ReportProgress_SENDINGREQUEST);
580
        else if (tested_protocol != HTTPS_TEST)
581
            CHECK_CALLED(ReportProgress_SENDINGREQUEST);
582 583
        if(test_redirect)
            CHECK_CALLED(ReportProgress_REDIRECTING);
584 585 586 587 588 589 590 591 592
        state = test_async_req ? STATE_SENDINGREQUEST : STATE_STARTDOWNLOADING;
    }

    switch(state) {
    case STATE_SENDINGREQUEST:
        SET_EXPECT(Stream_Read);
        SET_EXPECT(ReportProgress_SENDINGREQUEST);
        break;
    case STATE_STARTDOWNLOADING:
593 594
        if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
            SET_EXPECT(OnResponse);
595
            if(tested_protocol == HTTPS_TEST || test_redirect || test_abort || empty_file)
596
                SET_EXPECT(ReportProgress_ACCEPTRANGES);
597
            SET_EXPECT(ReportProgress_ENCODING);
598 599 600
            SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
            if(bindf & BINDF_NEEDFILE)
                SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
601
        }
602 603
    default:
        break;
604 605
    }

606 607
    if(state != STATE_SENDINGREQUEST)
        SET_EXPECT(ReportData);
608
    hres = IInternetProtocol_Continue(async_protocol, protocol_data);
609
    ok(hres == S_OK, "Continue failed: %08x\n", hres);
610
    if(tested_protocol == FTP_TEST || security_problem)
611
        CLEAR_CALLED(ReportData);
612
    else if(state != STATE_SENDINGREQUEST)
613
        CHECK_CALLED(ReportData);
614

615 616 617 618 619 620 621
    switch(state) {
    case STATE_SENDINGREQUEST:
        CHECK_CALLED(Stream_Read);
        CHECK_CALLED(ReportProgress_SENDINGREQUEST);
        state = STATE_STARTDOWNLOADING;
        break;
    case STATE_STARTDOWNLOADING:
622 623
        if (! security_problem)
        {
624
            state = STATE_DOWNLOADING;
625 626
            if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
                CHECK_CALLED(OnResponse);
627
                if(tested_protocol == HTTPS_TEST || empty_file)
628
                    CHECK_CALLED(ReportProgress_ACCEPTRANGES);
629
                else if(test_redirect || test_abort)
630
                    CLEAR_CALLED(ReportProgress_ACCEPTRANGES);
631
                CLEAR_CALLED(ReportProgress_ENCODING);
632 633 634 635 636 637 638 639 640
                CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
                if(bindf & BINDF_NEEDFILE)
                    CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
            }
        }
        else
        {
            security_problem = FALSE;
            SET_EXPECT(ReportProgress_CONNECTING);
641
        }
642 643
    default:
        break;
644
    }
645
}
646

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
{
    if(tested_protocol == FTP_TEST)
        CHECK_EXPECT2(Switch);
    else
        CHECK_EXPECT(Switch);

    ok(pProtocolData != NULL, "pProtocolData == NULL\n");
    if(binding_test) {
        ok(pProtocolData != &protocoldata, "pProtocolData == &protocoldata\n");
        ok(pProtocolData->grfFlags == protocoldata.grfFlags, "grfFlags wrong %x/%x\n",
           pProtocolData->grfFlags, protocoldata.grfFlags );
        ok(pProtocolData->dwState == protocoldata.dwState, "dwState wrong %x/%x\n",
           pProtocolData->dwState, protocoldata.dwState );
        ok(pProtocolData->pData == protocoldata.pData, "pData wrong %p/%p\n",
           pProtocolData->pData, protocoldata.pData );
        ok(pProtocolData->cbData == protocoldata.cbData, "cbData wrong %x/%x\n",
           pProtocolData->cbData, protocoldata.cbData );
    }

    pdata = pProtocolData;

    if(binding_test) {
        SetEvent(event_complete);
        WaitForSingleObject(event_complete2, INFINITE);
        return S_OK;
    }if(direct_read) {
        continue_protdata = *pProtocolData;
        SetEvent(event_continue);
        WaitForSingleObject(event_continue_done, INFINITE);
    }else {
        call_continue(pProtocolData);
        SetEvent(event_complete);
    }
681

682
    return S_OK;
683 684
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
static const char *status_names[] =
{
    "0",
    "FINDINGRESOURCE",
    "CONNECTING",
    "REDIRECTING",
    "BEGINDOWNLOADDATA",
    "DOWNLOADINGDATA",
    "ENDDOWNLOADDATA",
    "BEGINDOWNLOADCOMPONENTS",
    "INSTALLINGCOMPONENTS",
    "ENDDOWNLOADCOMPONENTS",
    "USINGCACHEDCOPY",
    "SENDINGREQUEST",
    "CLASSIDAVAILABLE",
    "MIMETYPEAVAILABLE",
    "CACHEFILENAMEAVAILABLE",
    "BEGINSYNCOPERATION",
    "ENDSYNCOPERATION",
    "BEGINUPLOADDATA",
    "UPLOADINGDATA",
    "ENDUPLOADINGDATA",
    "PROTOCOLCLASSID",
    "ENCODING",
    "VERIFIEDMIMETYPEAVAILABLE",
    "CLASSINSTALLLOCATION",
    "DECODING",
    "LOADINGMIMEHANDLER",
    "CONTENTDISPOSITIONATTACH",
    "FILTERREPORTMIMETYPE",
    "CLSIDCANINSTANTIATE",
    "IUNKNOWNAVAILABLE",
    "DIRECTBIND",
    "RAWMIMETYPE",
    "PROXYDETECTING",
    "ACCEPTRANGES",
    "COOKIE_SENT",
    "COMPACT_POLICY_RECEIVED",
    "COOKIE_SUPPRESSED",
    "COOKIE_STATE_UNKNOWN",
    "COOKIE_STATE_ACCEPT",
    "COOKIE_STATE_REJECT",
    "COOKIE_STATE_PROMPT",
    "COOKIE_STATE_LEASH",
    "COOKIE_STATE_DOWNGRADE",
    "POLICY_HREF",
    "P3P_HEADER",
    "SESSION_COOKIE_RECEIVED",
    "PERSISTENT_COOKIE_RECEIVED",
    "SESSION_COOKIES_ALLOWED",
    "CACHECONTROL",
    "CONTENTDISPOSITIONFILENAME",
    "MIMETEXTPLAINMISMATCH",
    "PUBLISHERAVAILABLE",
    "DISPLAYNAMEAVAILABLE"
};

742 743 744
static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
        LPCWSTR szStatusText)
{
745 746
    static const WCHAR null_guid[] = {'{','0','0','0','0','0','0','0','0','-','0','0','0','0','-',
        '0','0','0','0','-','0','0','0','0','-','0','0','0','0','0','0','0','0','0','0','0','0','}',0};
747
    static const WCHAR text_plain[] = {'t','e','x','t','/','p','l','a','i','n',0};
748

749 750 751 752 753
    if (ulStatusCode < sizeof(status_names)/sizeof(status_names[0]))
        trace( "progress: %s %s\n", status_names[ulStatusCode], wine_dbgstr_w(szStatusText) );
    else
          trace( "progress: %u %s\n", ulStatusCode, wine_dbgstr_w(szStatusText) );

754
    switch(ulStatusCode) {
755
    case BINDSTATUS_MIMETYPEAVAILABLE:
756
        CHECK_EXPECT2(ReportProgress_MIMETYPEAVAILABLE);
757
        if(tested_protocol != FILE_TEST && tested_protocol != ITS_TEST && !mimefilter_test && (pi & PI_MIMEVERIFICATION)) {
758 759 760 761 762
            if(!short_read || !direct_read)
                CHECK_CALLED(Read); /* set in Continue */
            else if(short_read)
                CHECK_CALLED(Read2); /* set in Read */
        }
763
        ok(szStatusText != NULL, "szStatusText == NULL\n");
764 765
        if(szStatusText) {
            if(tested_protocol == BIND_TEST)
766
                ok(!lstrcmpW(szStatusText, expect_wsz), "unexpected szStatusText %s\n", wine_dbgstr_w(szStatusText));
767 768 769 770
            else if (http_post_test)
                ok(lstrlenW(text_plain) <= lstrlenW(szStatusText) &&
                   !memcmp(szStatusText, text_plain, lstrlenW(text_plain)*sizeof(WCHAR)),
                   "szStatusText != text/plain\n");
771 772
            else if(empty_file)
                ok(!strcmp_wa(szStatusText, "application/javascript"), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
773 774 775 776 777
            else if((pi & PI_MIMEVERIFICATION) && emulate_prot && !mimefilter_test
                    && tested_protocol==HTTP_TEST && !short_read)
                ok(lstrlenW(gifW) <= lstrlenW(szStatusText) &&
                   !memcmp(szStatusText, gifW, lstrlenW(gifW)*sizeof(WCHAR)),
                   "szStatusText != image/gif\n");
778
            else if(!mimefilter_test)
779 780
                ok(lstrlenW(text_htmlW) <= lstrlenW(szStatusText) &&
                   !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
781
                   "szStatusText != text/html\n");
782
        }
783 784 785 786 787
        break;
    case BINDSTATUS_DIRECTBIND:
        CHECK_EXPECT2(ReportProgress_DIRECTBIND);
        ok(szStatusText == NULL, "szStatusText != NULL\n");
        break;
788 789 790 791
    case BINDSTATUS_RAWMIMETYPE:
        CHECK_EXPECT2(ReportProgress_RAWMIMETYPE);
        ok(szStatusText != NULL, "szStatusText == NULL\n");
        if(szStatusText)
792 793
            ok(lstrlenW(szStatusText) < lstrlenW(text_htmlW) ||
               !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
794 795
               "szStatusText != text/html\n");
        break;
796 797 798
    case BINDSTATUS_CACHEFILENAMEAVAILABLE:
        CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
        ok(szStatusText != NULL, "szStatusText == NULL\n");
799
        if(szStatusText) {
800
            if(binding_test)
801
                ok(!lstrcmpW(szStatusText, expect_wsz), "unexpected szStatusText\n");
802
            else if(tested_protocol == FILE_TEST)
803
                ok(!lstrcmpW(szStatusText, file_name), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
804
            else
805
                ok(szStatusText != NULL, "szStatusText == NULL\n");
806
        }
807 808
        break;
    case BINDSTATUS_FINDINGRESOURCE:
809
        CHECK_EXPECT2(ReportProgress_FINDINGRESOURCE);
810 811 812
        ok(szStatusText != NULL, "szStatusText == NULL\n");
        break;
    case BINDSTATUS_CONNECTING:
813
        CHECK_EXPECT2(ReportProgress_CONNECTING);
814 815 816
        ok(szStatusText != NULL, "szStatusText == NULL\n");
        break;
    case BINDSTATUS_SENDINGREQUEST:
817
        CHECK_EXPECT2(ReportProgress_SENDINGREQUEST);
818
        if(tested_protocol == FILE_TEST || tested_protocol == ITS_TEST) {
819 820 821
            ok(szStatusText != NULL, "szStatusText == NULL\n");
            if(szStatusText)
                ok(!*szStatusText, "wrong szStatusText\n");
822 823 824 825 826 827
        }
        break;
    case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
        CHECK_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
        ok(szStatusText != NULL, "szStatusText == NULL\n");
        if(szStatusText)
828
            ok(!strcmp_wa(szStatusText, "text/html"), "szStatusText != text/html\n");
829
        break;
830 831 832
    case BINDSTATUS_PROTOCOLCLASSID:
        CHECK_EXPECT(ReportProgress_PROTOCOLCLASSID);
        ok(szStatusText != NULL, "szStatusText == NULL\n");
833
        ok(!lstrcmpW(szStatusText, null_guid), "unexpected classid %s\n", wine_dbgstr_w(szStatusText));
834
        break;
835
    case BINDSTATUS_COOKIE_SENT:
836
        CHECK_EXPECT2(ReportProgress_COOKIE_SENT);
837 838 839 840
        ok(szStatusText == NULL, "szStatusText != NULL\n");
        break;
    case BINDSTATUS_REDIRECTING:
        CHECK_EXPECT(ReportProgress_REDIRECTING);
841
        if(test_redirect)
842
            ok(!strcmp_wa(szStatusText, "http://test.winehq.org/tests/hello.html"), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
843 844
        else
            ok(szStatusText == NULL, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
845 846 847
        break;
    case BINDSTATUS_ENCODING:
        CHECK_EXPECT(ReportProgress_ENCODING);
848
        ok(!strcmp_wa(szStatusText, "gzip"), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
849 850 851
        break;
    case BINDSTATUS_ACCEPTRANGES:
        CHECK_EXPECT(ReportProgress_ACCEPTRANGES);
852
        ok(!szStatusText, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
853
        break;
854
    case BINDSTATUS_PROXYDETECTING:
855 856 857
        if(!called_ReportProgress_PROXYDETECTING)
            SET_EXPECT(ReportProgress_CONNECTING);
        CHECK_EXPECT2(ReportProgress_PROXYDETECTING);
858
        ok(!szStatusText, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
859
        break;
860 861
    case BINDSTATUS_LOADINGMIMEHANDLER:
        CHECK_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
862
        ok(!szStatusText, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
863 864 865
        break;
    case BINDSTATUS_DECODING:
        CHECK_EXPECT(ReportProgress_DECODING);
866
        ok(!lstrcmpW(szStatusText, pjpegW), "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
867
        break;
868
    default:
869
        ok(0, "Unexpected status %d\n", ulStatusCode);
870 871 872 873 874 875 876 877
    };

    return S_OK;
}

static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
        ULONG ulProgress, ULONG ulProgressMax)
{
878 879 880 881 882
    HRESULT hres;

    static int rec_depth;
    rec_depth++;

883
    if(!mimefilter_test && (tested_protocol == FILE_TEST || tested_protocol == ITS_TEST)) {
884 885
        CHECK_EXPECT2(ReportData);

886
        ok(ulProgress == ulProgressMax, "ulProgress (%d) != ulProgressMax (%d)\n",
887
           ulProgress, ulProgressMax);
888
        ok(ulProgressMax == 13, "ulProgressMax=%d, expected 13\n", ulProgressMax);
889
        /* BSCF_SKIPDRAINDATAFORFILEURLS added in IE8 */
890 891 892 893 894 895
        if(tested_protocol == FILE_TEST)
            ok((grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION)) ||
               (grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_SKIPDRAINDATAFORFILEURLS)),
               "grcfBSCF = %08x\n", grfBSCF);
        else
            ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE), "grcfBSCF = %08x\n", grfBSCF);
896 897 898 899 900 901 902 903 904 905
    }else if(direct_read) {
        BYTE buf[14096];
        ULONG read;

        if(!read_report_data && rec_depth == 1) {
            BOOL reported_all_data = called_ReportData2;

            CHECK_EXPECT2(ReportData);

            if(short_read) {
906 907
                ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE)
                   || grfBSCF == BSCF_FIRSTDATANOTIFICATION, /* < IE8 */
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
                   "grcfBSCF = %08x\n", grfBSCF);
                CHECK_CALLED(Read); /* Set in Continue */
                first_data_notif = FALSE;
            }else if(first_data_notif) {
                ok(grfBSCF == BSCF_FIRSTDATANOTIFICATION, "grcfBSCF = %08x\n", grfBSCF);
                first_data_notif = FALSE;
            }else if(reported_all_data) {
                ok(grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION),
                   "grcfBSCF = %08x\n", grfBSCF);
            }else if(!direct_read) {
                ok(grfBSCF == BSCF_INTERMEDIATEDATANOTIFICATION, "grcfBSCF = %08x\n", grfBSCF);
            }

            do {
                read = 0;
                if(emulate_prot)
                    SET_EXPECT(Read);
                else
                    SET_EXPECT(ReportData2);
                SET_EXPECT(ReportResult);
928 929
                if(!emulate_prot)
                    SET_EXPECT(Switch);
930
                hres = IInternetProtocol_Read(binding_test ? binding_protocol : async_protocol, expect_pv = buf, sizeof(buf), &read);
931
                ok(hres == E_PENDING || hres == S_FALSE || hres == S_OK, "Read failed: %08x\n", hres);
932 933
                if(hres == S_OK)
                    ok(read, "read == 0\n");
934
                if(reported_all_data)
935
                    ok(hres == S_FALSE, "Read failed: %08x, expected S_FALSE\n", hres);
936 937
                if(!emulate_prot && hres != E_PENDING)
                    CHECK_NOT_CALLED(Switch); /* otherwise checked in wait_for_switch loop */
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
                if(emulate_prot)
                    CHECK_CALLED(Read);
                if(!reported_all_data && called_ReportData2) {
                    if(!emulate_prot)
                        CHECK_CALLED(ReportData2);
                    CHECK_CALLED(ReportResult);
                    reported_all_data = TRUE;
                }else {
                    if(!emulate_prot)
                        CHECK_NOT_CALLED(ReportData2);
                    CHECK_NOT_CALLED(ReportResult);
                }
            }while(hres == S_OK);
            if(hres == S_FALSE)
                wait_for_switch = FALSE;
        }else {
            CHECK_EXPECT(ReportData2);

            ok(grfBSCF & BSCF_LASTDATANOTIFICATION, "grfBSCF = %08x\n", grfBSCF);

            read = 0xdeadbeef;
            if(emulate_prot)
                SET_EXPECT(Read2);
            hres = IInternetProtocol_Read(binding_test ? binding_protocol : async_protocol, expect_pv = buf, sizeof(buf), &read);
            if(emulate_prot)
                CHECK_CALLED(Read2);
            ok(hres == S_FALSE, "Read returned: %08x, expected E_FALSE\n", hres);
            ok(!read, "read = %d\n", read);
        }
    }else if(!binding_test && (tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST
            || tested_protocol == FTP_TEST)) {
969 970 971
        if(empty_file)
            CHECK_EXPECT2(ReportData);
        else if(!(grfBSCF & BSCF_LASTDATANOTIFICATION) || (grfBSCF & BSCF_DATAFULLYAVAILABLE))
972
            CHECK_EXPECT(ReportData);
973
        else if (http_post_test)
974
            ok(ulProgress == 13, "Read %u bytes instead of 13\n", ulProgress);
975

976 977 978 979 980 981
        if(empty_file) {
            ok(!ulProgress, "ulProgress = %d\n", ulProgress);
            ok(!ulProgressMax, "ulProgressMax = %d\n", ulProgressMax);
        }else {
            ok(ulProgress, "ulProgress == 0\n");
        }
982

983 984 985 986 987
        if(empty_file) {
            ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION),
               "grcfBSCF = %08x\n", grfBSCF);
            first_data_notif = FALSE;
        }else if(first_data_notif) {
988 989 990
            ok(grfBSCF == BSCF_FIRSTDATANOTIFICATION
               || grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE),
               "grcfBSCF = %08x\n", grfBSCF);
991 992 993
            first_data_notif = FALSE;
        } else {
            ok(grfBSCF == BSCF_INTERMEDIATEDATANOTIFICATION
994 995
               || grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION)
               || broken(grfBSCF == (BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION)),
996
               "grcfBSCF = %08x\n", grfBSCF);
997
        }
998

999 1000
        if(!(bindf & BINDF_FROMURLMON) &&
           !(grfBSCF & BSCF_LASTDATANOTIFICATION)) {
1001 1002
            if(state == STATE_CONNECTING) {
                state = STATE_DOWNLOADING;
1003 1004 1005 1006 1007 1008 1009 1010
                if(http_is_first) {
                    CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
                    CHECK_CALLED(ReportProgress_CONNECTING);
                }
                CHECK_CALLED(ReportProgress_SENDINGREQUEST);
                CHECK_CALLED(OnResponse);
                CHECK_CALLED(ReportProgress_RAWMIMETYPE);
            }
1011
            SetEvent(event_complete);
1012
        }
1013
    }else if(!read_report_data) {
1014 1015 1016 1017
        BYTE buf[1000];
        ULONG read;
        HRESULT hres;

1018
        CHECK_EXPECT(ReportData);
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
        if(tested_protocol != BIND_TEST) {
            do {
                if(mimefilter_test)
                    SET_EXPECT(MimeFilter_Read);
                else if(rec_depth > 1)
                    SET_EXPECT(Read2);
                else
                    SET_EXPECT(Read);
                hres = IInternetProtocol_Read(binding_protocol, expect_pv=buf, sizeof(buf), &read);
                if(mimefilter_test)
                    CHECK_CALLED(MimeFilter_Read);
                else if(rec_depth > 1)
                    CHECK_CALLED(Read2);
                else
                    CHECK_CALLED(Read);
            }while(hres == S_OK);
        }
1037
    }
1038

1039
    rec_depth--;
1040 1041 1042 1043 1044 1045 1046 1047
    return S_OK;
}

static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
        DWORD dwError, LPCWSTR szResult)
{
    CHECK_EXPECT(ReportResult);

1048 1049 1050 1051 1052
    if(tested_protocol == FTP_TEST)
        ok(hrResult == E_PENDING || hrResult == S_OK, "hrResult = %08x, expected E_PENDING or S_OK\n", hrResult);
    else
        ok(hrResult == expect_hrResult, "hrResult = %08x, expected: %08x\n",
           hrResult, expect_hrResult);
Jacek Caban's avatar
Jacek Caban committed
1053
    if(SUCCEEDED(hrResult) || tested_protocol == FTP_TEST || test_abort)
1054
        ok(dwError == ERROR_SUCCESS, "dwError = %d, expected ERROR_SUCCESS\n", dwError);
1055
    else
1056
        ok(dwError != ERROR_SUCCESS ||
1057
           broken(tested_protocol == MK_TEST), /* WinME and NT4 */
1058
           "dwError == ERROR_SUCCESS\n");
1059 1060
    ok(!szResult, "szResult != NULL\n");

1061 1062 1063
    if(direct_read)
        SET_EXPECT(ReportData); /* checked after main loop */

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    return S_OK;
}

static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
    ProtocolSink_QueryInterface,
    ProtocolSink_AddRef,
    ProtocolSink_Release,
    ProtocolSink_Switch,
    ProtocolSink_ReportProgress,
    ProtocolSink_ReportData,
    ProtocolSink_ReportResult
};

static IInternetProtocolSink protocol_sink = { &protocol_sink_vtbl };

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
static HRESULT WINAPI MimeProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static ULONG WINAPI MimeProtocolSink_AddRef(IInternetProtocolSink *iface)
{
    return 2;
}

static ULONG WINAPI MimeProtocolSink_Release(IInternetProtocolSink *iface)
{
    return 1;
}

static HRESULT WINAPI MimeProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
{
    HRESULT hres;

    CHECK_EXPECT(MimeFilter_Switch);

    SET_EXPECT(Switch);
    hres = IInternetProtocolSink_Switch(filtered_sink, pProtocolData);
    ok(hres == S_OK, "Switch failed: %08x\n", hres);
    CHECK_CALLED(Switch);

    return S_OK;
}

static HRESULT WINAPI MimeProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
        LPCWSTR szStatusText)
{
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
    switch(ulStatusCode) {
    case BINDSTATUS_LOADINGMIMEHANDLER:
        /*
         * IE9 for some reason (bug?) calls this on mime handler's protocol sink instead of the
         * main protocol sink. We check ReportProgress_LOADINGMIMEHANDLER both here and in
         * ProtocolSink_ReportProgress to workaround it.
         */
        CHECK_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
        ok(!szStatusText, "szStatusText = %s\n", wine_dbgstr_w(szStatusText));
        break;
    default:
        ok(0, "Unexpected status code %d\n", ulStatusCode);
    }

1126 1127 1128 1129 1130 1131 1132 1133 1134
    return S_OK;
}

static HRESULT WINAPI MimeProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
        ULONG ulProgress, ULONG ulProgressMax)
{
    DWORD read = 0;
    BYTE buf[8192];
    HRESULT hres;
1135
    BOOL report_mime = FALSE;
1136 1137 1138

    CHECK_EXPECT(MimeFilter_ReportData);

1139
    if(!filter_state && !no_mime) {
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        SET_EXPECT(Read);
        hres = IInternetProtocol_Read(filtered_protocol, buf, sizeof(buf), &read);
        if(tested_protocol == HTTP_TEST)
            ok(hres == S_OK || hres == E_PENDING || hres == S_FALSE, "Read failed: %08x\n", hres);
        else
            ok(hres == S_OK, "Read failed: %08x\n", hres);
        CHECK_CALLED(Read);

        SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
        hres = IInternetProtocolSink_ReportProgress(filtered_sink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE, text_htmlW);
        ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
        CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);

1153 1154 1155 1156 1157
        SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
        hres = IInternetProtocolSink_ReportProgress(filtered_sink, BINDSTATUS_MIMETYPEAVAILABLE, text_htmlW);
        ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
        CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);

1158
        /* FIXME: test BINDSTATUS_CACHEFILENAMEAVAILABLE */
1159 1160
    }

1161 1162 1163 1164 1165 1166
    if(no_mime && prot_read<200) {
        SET_EXPECT(Read);
    }else if(no_mime && prot_read<300) {
        report_mime = TRUE;
        SET_EXPECT(Read);
        SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1167
        SET_EXPECT(ReportData);
1168 1169 1170
    }else if(!read_report_data) {
        SET_EXPECT(ReportData);
    }
1171 1172
    hres = IInternetProtocolSink_ReportData(filtered_sink, grfBSCF, ulProgress, ulProgressMax);
    ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1173 1174 1175 1176
    if(no_mime && prot_read<=200) {
        CHECK_CALLED(Read);
    }else if(report_mime) {
        CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1177
        CHECK_CALLED(ReportData);
1178 1179 1180
    }else if(!read_report_data) {
        CHECK_CALLED(ReportData);
    }
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

    if(!filter_state)
        filter_state = 1;

    return S_OK;
}

static HRESULT WINAPI MimeProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
        DWORD dwError, LPCWSTR szResult)
{
    HRESULT hres;

    CHECK_EXPECT(MimeFilter_ReportResult);

    ok(hrResult == S_OK, "hrResult = %08x\n", hrResult);
    ok(dwError == ERROR_SUCCESS, "dwError = %u\n", dwError);
1197
    ok(!szResult, "szResult = %s\n", wine_dbgstr_w(szResult));
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

    SET_EXPECT(ReportResult);
    hres = IInternetProtocolSink_ReportResult(filtered_sink, hrResult, dwError, szResult);
    ok(SUCCEEDED(hres), "ReportResult failed: %08x\n", hres);
    CHECK_CALLED(ReportResult);

    return S_OK;
}

static IInternetProtocolSinkVtbl mime_protocol_sink_vtbl = {
    MimeProtocolSink_QueryInterface,
    MimeProtocolSink_AddRef,
    MimeProtocolSink_Release,
    MimeProtocolSink_Switch,
    MimeProtocolSink_ReportProgress,
    MimeProtocolSink_ReportData,
    MimeProtocolSink_ReportResult
};

static IInternetProtocolSink mime_protocol_sink = { &mime_protocol_sink_vtbl };

1219 1220
static HRESULT QueryInterface(REFIID riid, void **ppv)
{
1221 1222
    static const IID IID_undocumented = {0x58DFC7D0,0x5381,0x43E5,{0x9D,0x72,0x4C,0xDD,0xE4,0xCB,0x0F,0x1A}};

1223 1224 1225 1226 1227 1228
    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid))
        *ppv = &protocol_sink;
    if(IsEqualGUID(&IID_IServiceProvider, riid))
        *ppv = &service_provider;
1229 1230
    if(IsEqualGUID(&IID_IUriContainer, riid))
        return E_NOINTERFACE; /* TODO */
1231

1232 1233 1234 1235
    /* NOTE: IE8 queries for undocumented {58DFC7D0-5381-43E5-9D72-4CDDE4CB0F1A} interface. */
    if(IsEqualGUID(&IID_undocumented, riid))
        return E_NOINTERFACE;

1236 1237 1238
    if(*ppv)
        return S_OK;

1239
    ok(0, "unexpected call %s\n", debugstr_guid(riid));
1240 1241 1242
    return E_NOINTERFACE;
}

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
{
    if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
        *ppv = iface;
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
{
    return 2;
}

static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
{
    return 1;
}

static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
{
1264 1265
    DWORD cbSize;

1266 1267 1268 1269
    CHECK_EXPECT(GetBindInfo);

    ok(grfBINDF != NULL, "grfBINDF == NULL\n");
    ok(pbindinfo != NULL, "pbindinfo == NULL\n");
1270
    ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
1271 1272

    *grfBINDF = bindf;
1273 1274
    if(binding_test)
        *grfBINDF |= BINDF_FROMURLMON;
1275 1276 1277
    cbSize = pbindinfo->cbSize;
    memset(pbindinfo, 0, cbSize);
    pbindinfo->cbSize = cbSize;
1278

1279
    if(http_post_test)
1280
    {
1281
        pbindinfo->cbstgmedData = sizeof(post_data)-1;
1282
        pbindinfo->dwBindVerb = BINDVERB_POST;
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
        pbindinfo->stgmedData.tymed = http_post_test;

        if(http_post_test == TYMED_HGLOBAL) {
            HGLOBAL data;

            /* Must be GMEM_FIXED, GMEM_MOVABLE does not work properly */
            data = GlobalAlloc(GPTR, sizeof(post_data));
            memcpy(data, post_data, sizeof(post_data));
            U(pbindinfo->stgmedData).hGlobal = data;
        }else {
            IStream *post_stream;
            HGLOBAL data;
            HRESULT hres;

            if(0) {
            /* Must be GMEM_FIXED, GMEM_MOVABLE does not work properly */
            data = GlobalAlloc(GPTR, sizeof(post_data));
            memcpy(data, post_data, sizeof(post_data));
            U(pbindinfo->stgmedData).hGlobal = data;

            hres = CreateStreamOnHGlobal(data, FALSE, &post_stream);
            ok(hres == S_OK, "CreateStreamOnHGlobal failed: %08x\n", hres);

            U(pbindinfo->stgmedData).pstm =post_stream;/* &Stream; */
            }
            U(pbindinfo->stgmedData).pstm = &Stream;
        }
1310 1311
    }

1312 1313 1314 1315 1316 1317
    return S_OK;
}

static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType,
        LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
{
1318 1319 1320 1321 1322 1323
    ok(ppwzStr != NULL, "ppwzStr == NULL\n");
    ok(pcElFetched != NULL, "pcElFetched == NULL\n");

    switch(ulStringType) {
    case BINDSTRING_ACCEPT_MIMES:
        CHECK_EXPECT(GetBindString_ACCEPT_MIMES);
1324
        ok(cEl == 256, "cEl=%d, expected 256\n", cEl);
1325
        if(pcElFetched) {
1326
            ok(*pcElFetched == 256, "*pcElFetched=%d, expected 256\n", *pcElFetched);
1327 1328 1329
            *pcElFetched = 1;
        }
        if(ppwzStr) {
1330 1331
            *ppwzStr = CoTaskMemAlloc(sizeof(acc_mimeW));
            memcpy(*ppwzStr, acc_mimeW, sizeof(acc_mimeW));
1332 1333 1334 1335
        }
        return S_OK;
    case BINDSTRING_USER_AGENT:
        CHECK_EXPECT(GetBindString_USER_AGENT);
1336
        ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
1337
        if(pcElFetched) {
1338
            ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
1339 1340 1341
            *pcElFetched = 1;
        }
        if(ppwzStr) {
1342 1343
            *ppwzStr = CoTaskMemAlloc(sizeof(user_agentW));
            memcpy(*ppwzStr, user_agentW, sizeof(user_agentW));
1344 1345
        }
        return S_OK;
1346 1347 1348 1349 1350 1351
    case BINDSTRING_POST_COOKIE:
        CHECK_EXPECT(GetBindString_POST_COOKIE);
        ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
        if(pcElFetched)
            ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
        return S_OK;
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
    case BINDSTRING_URL: {
        DWORD size;

        CHECK_EXPECT(GetBindString_URL);
        ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
        ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
        *pcElFetched = 1;

        size = (lstrlenW(binding_urls[tested_protocol])+1)*sizeof(WCHAR);
        *ppwzStr = CoTaskMemAlloc(size);
        memcpy(*ppwzStr, binding_urls[tested_protocol], size);
        return S_OK;
    }
1365 1366 1367 1368
    default:
        ok(0, "unexpected call\n");
    }

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
    return E_NOTIMPL;
}

static IInternetBindInfoVtbl bind_info_vtbl = {
    BindInfo_QueryInterface,
    BindInfo_AddRef,
    BindInfo_Release,
    BindInfo_GetBindInfo,
    BindInfo_GetBindString
};

static IInternetBindInfo bind_info = { &bind_info_vtbl };

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
                                                  REFIID riid, void **ppv)
{
    ok(0, "unexpected call\n");
    return E_NOINTERFACE;
}

static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
{
    return 2;
}

static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
{
    return 1;
}

static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
{
    CHECK_EXPECT(SetPriority);
1402
    ok(nPriority == ex_priority, "nPriority=%d\n", nPriority);
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
    return S_OK;
}

static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}


static const IInternetPriorityVtbl InternetPriorityVtbl = {
    InternetPriority_QueryInterface,
    InternetPriority_AddRef,
    InternetPriority_Release,
    InternetPriority_SetPriority,
    InternetPriority_GetPriority
};

static IInternetPriority InternetPriority = { &InternetPriorityVtbl };

1423
static ULONG WINAPI Protocol_AddRef(IInternetProtocolEx *iface)
1424 1425 1426 1427
{
    return 2;
}

1428
static ULONG WINAPI Protocol_Release(IInternetProtocolEx *iface)
1429 1430 1431 1432
{
    return 1;
}

1433
static HRESULT WINAPI Protocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
1434 1435
        DWORD dwOptions)
{
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
    HRESULT hres;

    CHECK_EXPECT(Abort);

    SET_EXPECT(ReportResult);
    hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
    ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
    CHECK_CALLED(ReportResult);

    return S_OK;
1446 1447
}

1448
static HRESULT WINAPI Protocol_Suspend(IInternetProtocolEx *iface)
1449 1450 1451 1452 1453
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

1454
static HRESULT WINAPI Protocol_Resume(IInternetProtocolEx *iface)
1455 1456 1457 1458 1459
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

1460
static HRESULT WINAPI Protocol_Seek(IInternetProtocolEx *iface,
1461 1462 1463 1464 1465 1466
        LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

1467
static HRESULT WINAPI ProtocolEmul_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
1468 1469 1470 1471 1472 1473
{
    if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
        *ppv = iface;
        return S_OK;
    }

1474
    if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
1475 1476 1477 1478
        if(impl_protex) {
            *ppv = iface;
            return S_OK;
        }
1479 1480 1481 1482
        *ppv = NULL;
        return E_NOINTERFACE;
    }

1483 1484 1485 1486
    if(IsEqualGUID(&IID_IInternetPriority, riid)) {
        *ppv = &InternetPriority;
        return S_OK;
    }
1487

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
    if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
        CHECK_EXPECT(QueryInterface_IWinInetInfo);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
        CHECK_EXPECT(QueryInterface_IWinInetHttpInfo);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

1500
    ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1501 1502 1503 1504
    *ppv = NULL;
    return E_NOINTERFACE;
}

1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
static DWORD WINAPI thread_proc(PVOID arg)
{
    HRESULT hres;

    memset(&protocoldata, -1, sizeof(protocoldata));

    prot_state = 0;

    SET_EXPECT(ReportProgress_FINDINGRESOURCE);
    hres = IInternetProtocolSink_ReportProgress(binding_sink,
            BINDSTATUS_FINDINGRESOURCE, hostW);
    CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
    ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);

    SET_EXPECT(ReportProgress_CONNECTING);
    hres = IInternetProtocolSink_ReportProgress(binding_sink,
            BINDSTATUS_CONNECTING, winehq_ipW);
    CHECK_CALLED(ReportProgress_CONNECTING);
    ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);

    SET_EXPECT(ReportProgress_SENDINGREQUEST);
    hres = IInternetProtocolSink_ReportProgress(binding_sink,
            BINDSTATUS_SENDINGREQUEST, NULL);
    CHECK_CALLED(ReportProgress_SENDINGREQUEST);
    ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);

    prot_state = 1;
    SET_EXPECT(Switch);
    hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
    CHECK_CALLED(Switch);
    ok(hres == S_OK, "Switch failed: %08x\n", hres);

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    if(!short_read) {
        prot_state = 2;
        if(mimefilter_test)
            SET_EXPECT(MimeFilter_Switch);
        else
            SET_EXPECT(Switch);
        hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
        ok(hres == S_OK, "Switch failed: %08x\n", hres);
        if(mimefilter_test)
            CHECK_CALLED(MimeFilter_Switch);
        else
            CHECK_CALLED(Switch);
1549

1550 1551 1552 1553 1554
        if(test_abort) {
            SetEvent(event_complete);
            return 0;
        }

1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
        prot_state = 2;
        if(mimefilter_test)
            SET_EXPECT(MimeFilter_Switch);
        else
            SET_EXPECT(Switch);
        hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
        ok(hres == S_OK, "Switch failed: %08x\n", hres);
        if(mimefilter_test)
            CHECK_CALLED(MimeFilter_Switch);
        else
            CHECK_CALLED(Switch);
1566

1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
        prot_state = 3;
        if(mimefilter_test)
            SET_EXPECT(MimeFilter_Switch);
        else
            SET_EXPECT(Switch);
        hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
        ok(hres == S_OK, "Switch failed: %08x\n", hres);
        if(mimefilter_test)
            CHECK_CALLED(MimeFilter_Switch);
        else
            CHECK_CALLED(Switch);
    }
1579

1580 1581
    SetEvent(event_complete);

1582 1583 1584
    return 0;
}

1585
static void protocol_start(IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo, DWORD pi)
1586 1587 1588 1589 1590 1591 1592
{
    BINDINFO bindinfo, exp_bindinfo;
    DWORD cbindf = 0;
    HRESULT hres;

    ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
    ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
1593 1594
    ok(pOIProtSink != &protocol_sink, "unexpected pOIProtSink\n");
    ok(pOIBindInfo != &bind_info, "unexpected pOIBindInfo\n");
1595
    ok(!pi, "pi = %x\n", pi);
1596

1597 1598 1599
    if(binding_test)
        ok(pOIProtSink == binding_sink, "pOIProtSink != binding_sink\n");

1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
    memset(&bindinfo, 0, sizeof(bindinfo));
    bindinfo.cbSize = sizeof(bindinfo);
    memcpy(&exp_bindinfo, &bindinfo, sizeof(bindinfo));
    SET_EXPECT(GetBindInfo);
    hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
    ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
    CHECK_CALLED(GetBindInfo);
    ok(cbindf == (bindf|BINDF_FROMURLMON), "bindf = %x, expected %x\n",
       cbindf, (bindf|BINDF_FROMURLMON));
    ok(!memcmp(&exp_bindinfo, &bindinfo, sizeof(bindinfo)), "unexpected bindinfo\n");
1610
    pReleaseBindInfo(&bindinfo);
1611 1612

    SET_EXPECT(ReportProgress_SENDINGREQUEST);
1613
    hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, emptyW);
1614 1615 1616
    ok(hres == S_OK, "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
    CHECK_CALLED(ReportProgress_SENDINGREQUEST);

1617
    if(tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST) {
1618 1619 1620 1621 1622 1623 1624
        IServiceProvider *service_provider;
        IHttpNegotiate *http_negotiate;
        IHttpNegotiate2 *http_negotiate2;
        LPWSTR ua = (LPWSTR)0xdeadbeef, accept_mimes[256];
        LPWSTR additional_headers = NULL;
        BYTE sec_id[100];
        DWORD fetched = 0, size = 100;
1625
        DWORD tid;
1626 1627 1628 1629 1630 1631 1632 1633

        SET_EXPECT(GetBindString_USER_AGENT);
        hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT,
                                               &ua, 1, &fetched);
        CHECK_CALLED(GetBindString_USER_AGENT);
        ok(hres == S_OK, "GetBindString(BINDSTRING_USER_AGETNT) failed: %08x\n", hres);
        ok(fetched == 1, "fetched = %d, expected 254\n", fetched);
        ok(ua != NULL, "ua =  %p\n", ua);
1634
        ok(!lstrcmpW(ua, user_agentW), "unexpected user agent %s\n", wine_dbgstr_w(ua));
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
        CoTaskMemFree(ua);

        fetched = 256;
        SET_EXPECT(GetBindString_ACCEPT_MIMES);
        hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
                                               accept_mimes, 256, &fetched);
        CHECK_CALLED(GetBindString_ACCEPT_MIMES);

        ok(hres == S_OK,
           "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
        ok(fetched == 1, "fetched = %d, expected 1\n", fetched);
1646
        ok(!lstrcmpW(acc_mimeW, accept_mimes[0]), "unexpected mimes %s\n", wine_dbgstr_w(accept_mimes[0]));
1647
        CoTaskMemFree(accept_mimes[0]);
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682

        hres = IInternetBindInfo_QueryInterface(pOIBindInfo, &IID_IServiceProvider,
                                                (void**)&service_provider);
        ok(hres == S_OK, "QueryInterface failed: %08x\n", hres);

        SET_EXPECT(QueryService_HttpNegotiate);
        hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
                &IID_IHttpNegotiate, (void**)&http_negotiate);
        CHECK_CALLED(QueryService_HttpNegotiate);
        ok(hres == S_OK, "QueryService failed: %08x\n", hres);

        SET_EXPECT(BeginningTransaction);
        hres = IHttpNegotiate_BeginningTransaction(http_negotiate, binding_urls[tested_protocol],
                                                   NULL, 0, &additional_headers);
        CHECK_CALLED(BeginningTransaction);
        IHttpNegotiate_Release(http_negotiate);
        ok(hres == S_OK, "BeginningTransction failed: %08x\n", hres);
        ok(additional_headers == NULL, "additional_headers=%p\n", additional_headers);

        SET_EXPECT(QueryService_HttpNegotiate);
        hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
                &IID_IHttpNegotiate2, (void**)&http_negotiate2);
        CHECK_CALLED(QueryService_HttpNegotiate);
        ok(hres == S_OK, "QueryService failed: %08x\n", hres);

        size = 512;
        SET_EXPECT(GetRootSecurityId);
        hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, sec_id, &size, 0);
        CHECK_CALLED(GetRootSecurityId);
        IHttpNegotiate2_Release(http_negotiate2);
        ok(hres == E_FAIL, "GetRootSecurityId failed: %08x, expected E_FAIL\n", hres);
        ok(size == 13, "size=%d\n", size);

        IServiceProvider_Release(service_provider);

1683
        CreateThread(NULL, 0, thread_proc, NULL, 0, &tid);
1684
        return;
1685 1686
    }

1687 1688
    SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
    hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
1689
            BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
1690 1691 1692
    ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
    CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);

1693 1694 1695 1696 1697
    if(mimefilter_test) {
        SET_EXPECT(MimeFilter_CreateInstance);
        SET_EXPECT(MimeFilter_Start);
        SET_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
    }
1698
    SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1699
    hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
1700
            mimefilter_test ? pjpegW : (expect_wsz = text_htmlW));
1701 1702
    ok(hres == S_OK,
       "ReportProgress(BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE) failed: %08x\n", hres);
1703 1704 1705 1706
    if(mimefilter_test) {
        CHECK_CALLED(MimeFilter_CreateInstance);
        CHECK_CALLED(MimeFilter_Start);
        CHECK_CALLED(ReportProgress_LOADINGMIMEHANDLER);
1707
        CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1708 1709 1710
    }else {
        CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
    }
1711

1712 1713 1714 1715
    if(mimefilter_test)
        SET_EXPECT(MimeFilter_ReportData);
    else
        SET_EXPECT(ReportData);
1716
    hres = IInternetProtocolSink_ReportData(pOIProtSink,
1717 1718
            BSCF_FIRSTDATANOTIFICATION | (tested_protocol == ITS_TEST ? BSCF_DATAFULLYAVAILABLE : BSCF_LASTDATANOTIFICATION),
            13, 13);
1719
    ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1720 1721 1722 1723
    if(mimefilter_test)
        CHECK_CALLED(MimeFilter_ReportData);
    else
        CHECK_CALLED(ReportData);
1724

1725 1726 1727 1728 1729 1730 1731
    if(tested_protocol == ITS_TEST) {
        SET_EXPECT(ReportData);
        hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
        ok(hres == S_OK, "ReportProgress(BINDSTATUS_BEGINDOWNLOADDATA) failed: %08x\n", hres);
        CHECK_CALLED(ReportData);
    }

1732 1733 1734 1735 1736
    if(tested_protocol == BIND_TEST) {
        hres = IInternetProtocol_Terminate(binding_protocol, 0);
        ok(hres == E_FAIL, "Termiante failed: %08x\n", hres);
    }

1737 1738 1739 1740
    if(mimefilter_test)
        SET_EXPECT(MimeFilter_ReportResult);
    else
        SET_EXPECT(ReportResult);
1741 1742
    hres = IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
    ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
1743 1744 1745 1746
    if(mimefilter_test)
        CHECK_CALLED(MimeFilter_ReportResult);
    else
        CHECK_CALLED(ReportResult);
1747 1748 1749 1750 1751 1752 1753
}

static HRESULT WINAPI ProtocolEmul_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
        DWORD grfPI, HANDLE_PTR dwReserved)
{
    CHECK_EXPECT(Start);
1754

1755 1756
    ok(!dwReserved, "dwReserved = %lx\n", dwReserved);
    protocol_start(pOIProtSink, pOIBindInfo, grfPI);
1757 1758 1759
    return S_OK;
}

1760
static HRESULT WINAPI ProtocolEmul_Continue(IInternetProtocolEx *iface,
1761 1762
        PROTOCOLDATA *pProtocolData)
{
1763
    DWORD bscf = 0, pr;
1764 1765 1766 1767 1768
    HRESULT hres;

    CHECK_EXPECT(Continue);

    ok(pProtocolData != NULL, "pProtocolData == NULL\n");
1769
    if(!pProtocolData || tested_protocol == BIND_TEST)
1770
        return S_OK;
1771 1772
    if(binding_test) {
        ok(pProtocolData != &protocoldata, "pProtocolData == &protocoldata\n");
1773 1774 1775 1776 1777 1778 1779 1780
        ok(pProtocolData->grfFlags == protocoldata.grfFlags, "grfFlags wrong %x/%x\n",
           pProtocolData->grfFlags, protocoldata.grfFlags );
        ok(pProtocolData->dwState == protocoldata.dwState, "dwState wrong %x/%x\n",
           pProtocolData->dwState, protocoldata.dwState );
        ok(pProtocolData->pData == protocoldata.pData, "pData wrong %p/%p\n",
           pProtocolData->pData, protocoldata.pData );
        ok(pProtocolData->cbData == protocoldata.cbData, "cbData wrong %x/%x\n",
           pProtocolData->cbData, protocoldata.cbData );
1781
    }
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795

    switch(prot_state) {
    case 1: {
        IServiceProvider *service_provider;
        IHttpNegotiate *http_negotiate;
        static WCHAR header[] = {'?',0};

        hres = IInternetProtocolSink_QueryInterface(binding_sink, &IID_IServiceProvider,
                                                    (void**)&service_provider);
        ok(hres == S_OK, "Could not get IServiceProvicder\n");

        SET_EXPECT(QueryService_HttpNegotiate);
        hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
                                             &IID_IHttpNegotiate, (void**)&http_negotiate);
1796
        IServiceProvider_Release(service_provider);
1797 1798 1799 1800 1801
        CHECK_CALLED(QueryService_HttpNegotiate);
        ok(hres == S_OK, "Could not get IHttpNegotiate\n");

        SET_EXPECT(OnResponse);
        hres = IHttpNegotiate_OnResponse(http_negotiate, 200, header, NULL, NULL);
1802
        IHttpNegotiate_Release(http_negotiate);
1803 1804 1805 1806
        CHECK_CALLED(OnResponse);
        IHttpNegotiate_Release(http_negotiate);
        ok(hres == S_OK, "OnResponse failed: %08x\n", hres);

1807 1808 1809 1810 1811
        if(mimefilter_test) {
            SET_EXPECT(MimeFilter_CreateInstance);
            SET_EXPECT(MimeFilter_Start);
            SET_EXPECT(ReportProgress_LOADINGMIMEHANDLER);
        }else if(!(pi & PI_MIMEVERIFICATION)) {
1812
            SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1813
        }
1814
        hres = IInternetProtocolSink_ReportProgress(binding_sink,
1815
                BINDSTATUS_MIMETYPEAVAILABLE, mimefilter_test ? pjpegW : text_htmlW);
1816 1817 1818 1819 1820
        if(mimefilter_test) {
            CHECK_CALLED(MimeFilter_CreateInstance);
            CHECK_CALLED(MimeFilter_Start);
            CHECK_CALLED(ReportProgress_LOADINGMIMEHANDLER);
        }else if(!(pi & PI_MIMEVERIFICATION)) {
1821
            CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1822
        }
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
        ok(hres == S_OK,
           "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);

        bscf |= BSCF_FIRSTDATANOTIFICATION;
        break;
    }
    case 2:
    case 3:
        bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
        break;
    }

1835
    pr = prot_read;
1836
    if(mimefilter_test)
1837
        SET_EXPECT(MimeFilter_ReportData);
1838
    if((!mimefilter_test || no_mime) && (pi & PI_MIMEVERIFICATION)) {
1839
        if(pr < 200)
1840
            SET_EXPECT(Read); /* checked in ReportData for short_read */
1841
        if(pr == 200) {
1842 1843
            if(!mimefilter_test)
                SET_EXPECT(Read); /* checked in BINDSTATUS_MIMETYPEAVAILABLE or ReportData */
1844
            SET_EXPECT(GetBindInfo);
1845 1846
            SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
        }
1847 1848 1849
        if(pr >= 200)
            SET_EXPECT(ReportData);
    }else {
1850
        SET_EXPECT(ReportData);
1851
    }
1852

1853
    hres = IInternetProtocolSink_ReportData(binding_sink, bscf, pr, 400);
1854 1855
    ok(hres == S_OK, "ReportData failed: %08x\n", hres);

1856 1857 1858
    if(mimefilter_test) {
        SET_EXPECT(MimeFilter_ReportData);
    }else if(pi & PI_MIMEVERIFICATION) {
1859
        if(!short_read && pr < 200)
1860 1861
            CHECK_CALLED(Read);
        if(pr == 200) {
1862
            CLEAR_CALLED(GetBindInfo); /* IE9 */
1863 1864
            CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
        }
1865
    }else {
1866
        CHECK_CALLED(ReportData);
1867
    }
1868

1869 1870 1871
    if(prot_state == 3)
        prot_state = 4;

1872
    return S_OK;
1873 1874
}

1875
static HRESULT WINAPI ProtocolEmul_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
1876 1877 1878 1879 1880 1881
{
    CHECK_EXPECT(Terminate);
    ok(!dwOptions, "dwOptions=%d\n", dwOptions);
    return S_OK;
}

1882
static HRESULT WINAPI ProtocolEmul_Read(IInternetProtocolEx *iface, void *pv,
1883 1884
        ULONG cb, ULONG *pcbRead)
{
1885 1886 1887 1888 1889 1890
    if(read_report_data)
        CHECK_EXPECT2(Read2);

    if(mimefilter_test || short_read) {
        if(!read_report_data)
            CHECK_EXPECT2(Read);
1891
    }else if((pi & PI_MIMEVERIFICATION)) {
1892 1893
        if(!read_report_data)
            CHECK_EXPECT2(Read);
1894

1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
        if(prot_read < 300) {
            ok(pv != expect_pv, "pv == expect_pv\n");
            if(prot_read < 300)
                ok(cb == 2048-prot_read, "cb=%d\n", cb);
            else
                ok(cb == 700, "cb=%d\n", cb);
        }else {
            ok(expect_pv <= pv && (BYTE*)pv < (BYTE*)expect_pv + cb, "pv != expect_pv\n");
        }
    }else {
1905 1906
        if(!read_report_data)
            CHECK_EXPECT(Read);
1907

1908 1909 1910 1911
        ok(pv == expect_pv, "pv != expect_pv\n");
        ok(cb == 1000, "cb=%d\n", cb);
        ok(!*pcbRead, "*pcbRead = %d\n", *pcbRead);
    }
1912 1913
    ok(pcbRead != NULL, "pcbRead == NULL\n");

1914
    if(prot_state == 3 || (short_read && prot_state != 4)) {
1915 1916
        HRESULT hres;

1917 1918 1919
        prot_state = 4;
        if(short_read) {
            SET_EXPECT(Read2); /* checked in BINDSTATUS_MIMETYPEAVAILABLE */
1920
            SET_EXPECT(GetBindInfo);
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
            SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
        }
        if(mimefilter_test)
            SET_EXPECT(MimeFilter_ReportData);
        else if(direct_read)
            SET_EXPECT(ReportData2);
        read_report_data++;
        hres = IInternetProtocolSink_ReportData(binding_sink,
                BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION, 0, 0);
        read_report_data--;
        ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1932 1933
        if(short_read) {
            CLEAR_CALLED(GetBindInfo); /* IE9 */
1934
            CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1935
        }
1936 1937 1938 1939 1940
        if(mimefilter_test)
            CHECK_CALLED(MimeFilter_ReportData);
        else if(direct_read)
            CHECK_CALLED(ReportData2);

1941 1942 1943 1944
        if(mimefilter_test)
            SET_EXPECT(MimeFilter_ReportResult);
        else
            SET_EXPECT(ReportResult);
1945
        hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
1946 1947 1948 1949 1950
        ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
        if(mimefilter_test)
            CHECK_CALLED(MimeFilter_ReportResult);
        else
            CHECK_CALLED(ReportResult);
1951

1952 1953 1954 1955 1956 1957
        if(cb > 100)
            cb = 100;
        memset(pv, 'x', cb);
        if(cb>6)
            memcpy(pv, "gif87a", 6);
        prot_read += *pcbRead = cb;
1958 1959 1960
        return S_OK;
    }if(prot_state == 4) {
        *pcbRead = 0;
1961
        return S_FALSE;
1962 1963
    }

1964 1965
    if((async_read_pending = !async_read_pending)) {
        *pcbRead = 0;
1966
        return tested_protocol == HTTP_TEST || tested_protocol == HTTPS_TEST ? E_PENDING : S_FALSE;
1967
    }
1968

1969 1970 1971 1972 1973 1974
    if(cb > 100)
        cb = 100;
    memset(pv, 'x', cb);
    if(cb>6)
        memcpy(pv, "gif87a", 6);
    prot_read += *pcbRead = cb;
1975 1976 1977
    return S_OK;
}

1978
static HRESULT WINAPI ProtocolEmul_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
1979
{
1980 1981
    CHECK_EXPECT(LockRequest);
    ok(dwOptions == 0, "dwOptions=%x\n", dwOptions);
1982 1983 1984
    return S_OK;
}

1985
static HRESULT WINAPI ProtocolEmul_UnlockRequest(IInternetProtocolEx *iface)
1986
{
1987
    CHECK_EXPECT(UnlockRequest);
1988 1989 1990
    return S_OK;
}

1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
static HRESULT WINAPI ProtocolEmul_StartEx(IInternetProtocolEx *iface, IUri *pUri,
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
        DWORD grfPI, HANDLE *dwReserved)
{
    CHECK_EXPECT(StartEx);
    ok(!dwReserved, "dwReserved = %p\n", dwReserved);
    protocol_start(pOIProtSink, pOIBindInfo, grfPI);
    return S_OK;
}

static const IInternetProtocolExVtbl ProtocolVtbl = {
2002
    ProtocolEmul_QueryInterface,
2003 2004
    Protocol_AddRef,
    Protocol_Release,
2005 2006
    ProtocolEmul_Start,
    ProtocolEmul_Continue,
2007
    Protocol_Abort,
2008
    ProtocolEmul_Terminate,
2009 2010
    Protocol_Suspend,
    Protocol_Resume,
2011
    ProtocolEmul_Read,
2012
    Protocol_Seek,
2013
    ProtocolEmul_LockRequest,
2014 2015
    ProtocolEmul_UnlockRequest,
    ProtocolEmul_StartEx
2016 2017
};

2018
static IInternetProtocolEx Protocol = { &ProtocolVtbl };
2019

2020
static HRESULT WINAPI MimeProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
{
    if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
        *ppv = iface;
        return S_OK;
    }

    if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
        *ppv = &mime_protocol_sink;
        return S_OK;
    }

    ok(0, "unexpected riid %s\n", debugstr_guid(riid));
    *ppv = NULL;
    return E_NOINTERFACE;
}

2037
static HRESULT WINAPI MimeProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
        DWORD grfPI, HANDLE_PTR dwReserved)
{
    PROTOCOLFILTERDATA *data;
    LPOLESTR url_str = NULL;
    DWORD fetched = 0;
    BINDINFO bindinfo;
    DWORD cbindf = 0;
    HRESULT hres;

    CHECK_EXPECT(MimeFilter_Start);

2050
    ok(!lstrcmpW(szUrl, pjpegW), "wrong url %s\n", wine_dbgstr_w(szUrl));
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
    ok(grfPI == (PI_FILTER_MODE|PI_FORCE_ASYNC), "grfPI=%x, expected PI_FILTER_MODE|PI_FORCE_ASYNC\n", grfPI);
    ok(dwReserved, "dwReserved == 0\n");
    ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
    ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");

    if(binding_test) {
        ok(pOIProtSink != binding_sink, "pOIProtSink == protocol_sink\n");
        ok(pOIBindInfo == prot_bind_info, "pOIBindInfo != bind_info\n");
    }else {
        ok(pOIProtSink == &protocol_sink, "pOIProtSink != protocol_sink\n");
        ok(pOIBindInfo == &bind_info, "pOIBindInfo != bind_info\n");
    }

    data = (void*)dwReserved;
    ok(data->cbSize == sizeof(*data), "data->cbSize = %d\n", data->cbSize);
    ok(!data->pProtocolSink, "data->pProtocolSink != NULL\n");
    ok(data->pProtocol != NULL, "data->pProtocol == NULL\n");
    ok(!data->pUnk, "data->pUnk != NULL\n");
    ok(!data->dwFilterFlags, "data->dwProtocolFlags = %x\n", data->dwFilterFlags);
    if(binding_test) {
        IInternetProtocolSink *prot_sink;

        IInternetProtocol_QueryInterface(data->pProtocol, &IID_IInternetProtocolSink, (void**)&prot_sink);
        ok(prot_sink == pOIProtSink, "QI(data->pProtocol, IID_IInternetProtocolSink) != pOIProtSink\n");
        IInternetProtocolSink_Release(prot_sink);

        ok(data->pProtocol != binding_protocol, "data->pProtocol == binding_protocol\n");

        filtered_protocol = data->pProtocol;
        IInternetProtocol_AddRef(filtered_protocol);
    }else {
        IInternetProtocol *prot;

        IInternetProtocol_QueryInterface(data->pProtocol, &IID_IInternetProtocol, (void**)&prot);
        ok(prot == async_protocol, "QI(data->pProtocol, IID_IInternetProtocol) != async_protocol\n");
        IInternetProtocol_Release(prot);

        ok(data->pProtocol != async_protocol, "data->pProtocol == async_protocol\n");
    }

    filtered_sink = pOIProtSink;

    SET_EXPECT(ReportProgress_DECODING);
2094
    hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DECODING, pjpegW);
2095 2096 2097 2098 2099 2100 2101 2102
    ok(hres == S_OK, "ReportProgress(BINDSTATUS_DECODING) failed: %08x\n", hres);
    CHECK_CALLED(ReportProgress_DECODING);

    SET_EXPECT(GetBindInfo);
    memset(&bindinfo, 0, sizeof(bindinfo));
    bindinfo.cbSize = sizeof(bindinfo);
    hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
    ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
2103
    ok(cbindf == (bindf|BINDF_FROMURLMON), "cbindf = %x, expected %x\n", cbindf, bindf);
2104 2105 2106 2107 2108 2109
    CHECK_CALLED(GetBindInfo);

    SET_EXPECT(GetBindString_URL);
    hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_URL, &url_str, 1, &fetched);
    ok(hres == S_OK, "GetBindString(BINDSTRING_URL) failed: %08x\n", hres);
    ok(fetched == 1, "fetched = %d\n", fetched);
2110
    ok(!lstrcmpW(url_str, binding_urls[tested_protocol]), "wrong url_str %s\n", wine_dbgstr_w(url_str));
2111 2112 2113 2114 2115 2116
    CoTaskMemFree(url_str);
    CHECK_CALLED(GetBindString_URL);

    return S_OK;
}

2117
static HRESULT WINAPI Protocol_Continue(IInternetProtocolEx *iface,
2118 2119 2120 2121 2122 2123
        PROTOCOLDATA *pProtocolData)
{
    CHECK_EXPECT(MimeFilter_Continue);
    return E_NOTIMPL;
}

2124
static HRESULT WINAPI MimeProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
{
    HRESULT hres;

    CHECK_EXPECT(MimeFilter_Terminate);

    ok(!dwOptions, "dwOptions = %x\n", dwOptions);

    SET_EXPECT(Terminate);
    hres = IInternetProtocol_Terminate(filtered_protocol, dwOptions);
    ok(hres == S_OK, "Terminate failed: %08x\n", hres);
    CHECK_CALLED(Terminate);

    return S_OK;
}

2140
static HRESULT WINAPI MimeProtocol_Read(IInternetProtocolEx *iface, void *pv,
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
        ULONG cb, ULONG *pcbRead)
{
    BYTE buf[2096];
    DWORD read = 0;
    HRESULT hres;

    CHECK_EXPECT(MimeFilter_Read);

    ok(pv != NULL, "pv == NULL\n");
    ok(cb != 0, "cb == 0\n");
    ok(pcbRead != NULL, "pcbRead == NULL\n");

2153 2154 2155 2156
    if(read_report_data)
        SET_EXPECT(Read2);
    else
        SET_EXPECT(Read);
2157 2158
    hres = IInternetProtocol_Read(filtered_protocol, buf, sizeof(buf), &read);
    ok(hres == S_OK || hres == S_FALSE || hres == E_PENDING, "Read failed: %08x\n", hres);
2159 2160 2161 2162
    if(read_report_data)
        CHECK_CALLED(Read2);
    else
        CHECK_CALLED(Read);
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172

    if(pcbRead) {
        ok(*pcbRead == 0, "*pcbRead=%d, expected 0\n", *pcbRead);
        *pcbRead = read;
    }

    memset(pv, 'x', read);
    return hres;
}

2173
static HRESULT WINAPI MimeProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
{
    HRESULT hres;

    CHECK_EXPECT(MimeFilter_LockRequest);

    ok(!dwOptions, "dwOptions = %x\n", dwOptions);

    SET_EXPECT(LockRequest);
    hres = IInternetProtocol_LockRequest(filtered_protocol, dwOptions);
    ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
    CHECK_CALLED(LockRequest);

    return S_OK;
}

2189
static HRESULT WINAPI MimeProtocol_UnlockRequest(IInternetProtocolEx *iface)
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
{
    HRESULT hres;

    CHECK_EXPECT(MimeFilter_UnlockRequest);

    SET_EXPECT(UnlockRequest);
    hres = IInternetProtocol_UnlockRequest(filtered_protocol);
    ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
    CHECK_CALLED(UnlockRequest);

    return S_OK;
}

2203
static const IInternetProtocolExVtbl MimeProtocolVtbl = {
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
    MimeProtocol_QueryInterface,
    Protocol_AddRef,
    Protocol_Release,
    MimeProtocol_Start,
    Protocol_Continue,
    Protocol_Abort,
    MimeProtocol_Terminate,
    Protocol_Suspend,
    Protocol_Resume,
    MimeProtocol_Read,
    Protocol_Seek,
    MimeProtocol_LockRequest,
    MimeProtocol_UnlockRequest
};

2219
static IInternetProtocolEx MimeProtocol = { &MimeProtocolVtbl };
2220

2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
{
    ok(0, "unexpected call\n");
    return E_NOINTERFACE;
}

static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
{
    return 2;
}

static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
{
    return 1;
}

static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
                                        REFIID riid, void **ppv)
{
    CHECK_EXPECT(CreateInstance);

    ok(pOuter == (IUnknown*)prot_bind_info, "pOuter != protocol_unk\n");
2243
    ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", debugstr_guid(riid));
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
    ok(ppv != NULL, "ppv == NULL\n");

    *ppv = &Protocol;
    return S_OK;
}

static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
{
    ok(0, "unexpected call\n");
    return S_OK;
}

static const IClassFactoryVtbl ClassFactoryVtbl = {
    ClassFactory_QueryInterface,
    ClassFactory_AddRef,
    ClassFactory_Release,
    ClassFactory_CreateInstance,
    ClassFactory_LockServer
};

static IClassFactory ClassFactory = { &ClassFactoryVtbl };

2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
static HRESULT WINAPI MimeFilter_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
{
    CHECK_EXPECT(MimeFilter_CreateInstance);

    ok(!outer, "outer = %p\n", outer);
    ok(IsEqualGUID(&IID_IInternetProtocol, riid), "unexpected riid %s\n", debugstr_guid(riid));

    *ppv = &MimeProtocol;
    return S_OK;
}

static const IClassFactoryVtbl MimeFilterCFVtbl = {
    ClassFactory_QueryInterface,
    ClassFactory_AddRef,
    ClassFactory_Release,
    MimeFilter_CreateInstance,
    ClassFactory_LockServer
};

static IClassFactory mimefilter_cf = { &MimeFilterCFVtbl };

Jacek Caban's avatar
Jacek Caban committed
2287 2288 2289 2290 2291 2292 2293 2294 2295
#define TEST_BINDING     0x0001
#define TEST_FILTER      0x0002
#define TEST_FIRST_HTTP  0x0004
#define TEST_DIRECT_READ 0x0008
#define TEST_POST        0x0010
#define TEST_EMULATEPROT 0x0020
#define TEST_SHORT_READ  0x0040
#define TEST_REDIRECT    0x0080
#define TEST_ABORT       0x0100
2296
#define TEST_ASYNCREQ    0x0200
2297
#define TEST_USEIURI     0x0400
2298
#define TEST_IMPLPROTEX  0x0800
2299
#define TEST_EMPTY       0x1000
2300
#define TEST_NOMIME      0x2000
2301

2302 2303 2304 2305 2306 2307 2308 2309 2310
static void register_filter(BOOL do_register)
{
    IInternetSession *session;
    HRESULT hres;

    hres = pCoInternetGetSession(0, &session, 0);
    ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);

    if(do_register) {
2311 2312 2313
        hres = IInternetSession_RegisterMimeFilter(session, &mimefilter_cf, &IID_IInternetProtocol, pjpegW);
        ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
        hres = IInternetSession_RegisterMimeFilter(session, &mimefilter_cf, &IID_IInternetProtocol, gifW);
2314 2315
        ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
    }else {
2316 2317 2318
        hres = IInternetSession_UnregisterMimeFilter(session, &mimefilter_cf, pjpegW);
        ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
        hres = IInternetSession_UnregisterMimeFilter(session, &mimefilter_cf, gifW);
2319 2320 2321 2322 2323 2324
        ok(hres == S_OK, "RegisterMimeFilter failed: %08x\n", hres);
    }

    IInternetSession_Release(session);
}

2325
static void init_test(int prot, DWORD flags)
2326 2327
{
    tested_protocol = prot;
2328
    binding_test = (flags & TEST_BINDING) != 0;
2329 2330 2331 2332
    first_data_notif = TRUE;
    prot_read = 0;
    prot_state = 0;
    async_read_pending = TRUE;
2333
    mimefilter_test = (flags & TEST_FILTER) != 0;
2334
    no_mime = (flags & TEST_NOMIME) != 0;
2335
    filter_state = 0;
2336
    post_stream_read = 0;
2337 2338
    ResetEvent(event_complete);
    ResetEvent(event_complete2);
2339 2340
    ResetEvent(event_continue);
    ResetEvent(event_continue_done);
2341 2342
    async_protocol = binding_protocol = filtered_protocol = NULL;
    filtered_sink = NULL;
2343 2344
    http_is_first = (flags & TEST_FIRST_HTTP) != 0;
    first_data_notif = TRUE;
2345 2346
    state = STATE_CONNECTING;
    test_async_req = (flags & TEST_ASYNCREQ) != 0;
2347 2348 2349 2350
    direct_read = (flags & TEST_DIRECT_READ) != 0;
    emulate_prot = (flags & TEST_EMULATEPROT) != 0;
    wait_for_switch = TRUE;
    short_read = (flags & TEST_SHORT_READ) != 0;
2351
    http_post_test = TYMED_NULL;
2352
    test_redirect = (flags & TEST_REDIRECT) != 0;
Jacek Caban's avatar
Jacek Caban committed
2353
    test_abort = (flags & TEST_ABORT) != 0;
2354
    impl_protex = (flags & TEST_IMPLPROTEX) != 0;
2355
    empty_file = (flags & TEST_EMPTY) != 0;
2356 2357

    register_filter(mimefilter_test);
2358 2359
}

2360 2361 2362 2363 2364 2365
static void test_priority(IInternetProtocol *protocol)
{
    IInternetPriority *priority;
    LONG pr;
    HRESULT hres;

2366 2367
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority,
                                            (void**)&priority);
2368
    ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
2369 2370 2371 2372
    if(FAILED(hres))
        return;

    hres = IInternetPriority_GetPriority(priority, &pr);
2373 2374
    ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
    ok(pr == 0, "pr=%d, expected 0\n", pr);
2375 2376

    hres = IInternetPriority_SetPriority(priority, 1);
2377
    ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
2378 2379

    hres = IInternetPriority_GetPriority(priority, &pr);
2380 2381
    ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
    ok(pr == 1, "pr=%d, expected 1\n", pr);
2382 2383 2384 2385

    IInternetPriority_Release(priority);
}

Jacek Caban's avatar
Jacek Caban committed
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
static void test_early_abort(const CLSID *clsid)
{
    IInternetProtocol *protocol;
    HRESULT hres;

    hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
            &IID_IInternetProtocol, (void**)&protocol);
    ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);

    hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
    ok(hres == S_OK, "Abort failed: %08x\n", hres);

    hres = IInternetProtocol_Abort(protocol, E_FAIL, 0);
    ok(hres == S_OK, "Abort failed: %08x\n", hres);

    IInternetProtocol_Release(protocol);
}

2404 2405
static BOOL file_protocol_start(IInternetProtocol *protocol, LPCWSTR url,
        IInternetProtocolEx *protocolex, IUri *uri, BOOL is_first)
2406 2407 2408 2409
{
    HRESULT hres;

    SET_EXPECT(GetBindInfo);
2410 2411
    if(!(bindf & BINDF_FROMURLMON))
       SET_EXPECT(ReportProgress_DIRECTBIND);
2412
    if(is_first) {
2413
        SET_EXPECT(ReportProgress_SENDINGREQUEST);
2414
        SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2415 2416 2417 2418
        if(bindf & BINDF_FROMURLMON)
            SET_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
        else
            SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
2419 2420
    }
    SET_EXPECT(ReportData);
2421 2422
    if(is_first)
        SET_EXPECT(ReportResult);
2423

2424 2425
    expect_hrResult = S_OK;

2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
    if(protocolex) {
        hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, 0, 0);
        ok(hres == S_OK, "StartEx failed: %08x\n", hres);
    }else {
        hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
        if(hres == INET_E_RESOURCE_NOT_FOUND) {
            win_skip("Start failed\n");
            return FALSE;
        }
        ok(hres == S_OK, "Start failed: %08x\n", hres);
2436
    }
2437 2438

    CHECK_CALLED(GetBindInfo);
2439 2440
    if(!(bindf & BINDF_FROMURLMON))
       CHECK_CALLED(ReportProgress_DIRECTBIND);
2441
    if(is_first) {
2442
        CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2443
        CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
2444 2445 2446 2447
        if(bindf & BINDF_FROMURLMON)
            CHECK_CALLED(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
        else
            CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
2448 2449
    }
    CHECK_CALLED(ReportData);
2450 2451
    if(is_first)
        CHECK_CALLED(ReportResult);
2452 2453

    return TRUE;
2454 2455 2456 2457 2458 2459 2460
}

static void test_file_protocol_url(LPCWSTR url)
{
    IInternetProtocolInfo *protocol_info;
    IUnknown *unk;
    IClassFactory *factory;
2461 2462 2463
    IInternetProtocol *protocol;
    BYTE buf[512];
    ULONG cb;
2464 2465 2466 2467
    HRESULT hres;

    hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
            &IID_IUnknown, (void**)&unk);
2468
    ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2469
    if(FAILED(hres))
2470 2471 2472 2473
        return;

    hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
    ok(hres == E_NOINTERFACE,
2474
            "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
2475 2476 2477

    hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
    ok(hres == S_OK, "Could not get IClassFactory interface\n");
2478 2479 2480 2481 2482 2483 2484
    IUnknown_Release(unk);
    if(FAILED(hres))
        return;

    hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
    ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);

2485
    if(SUCCEEDED(hres)) {
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
        if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
            hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
            ok(hres == S_OK, "Read failed: %08x\n", hres);
            ok(cb == 2, "cb=%u expected 2\n", cb);
            hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
            ok(hres == S_FALSE, "Read failed: %08x\n", hres);
            hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
            ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
            ok(cb == 0, "cb=%u expected 0\n", cb);
            hres = IInternetProtocol_UnlockRequest(protocol);
            ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
        }
2498

2499 2500 2501 2502 2503 2504 2505 2506
        if(file_protocol_start(protocol, url, NULL, NULL, FALSE)) {
            hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
            ok(hres == S_FALSE, "Read failed: %08x\n", hres);
            hres = IInternetProtocol_LockRequest(protocol, 0);
            ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
            hres = IInternetProtocol_UnlockRequest(protocol);
            ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
        }
2507

2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
        IInternetProtocol_Release(protocol);
    }

    hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
    ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
    if(SUCCEEDED(hres)) {
        if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
            hres = IInternetProtocol_LockRequest(protocol, 0);
            ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
            hres = IInternetProtocol_Terminate(protocol, 0);
            ok(hres == S_OK, "Terminate failed: %08x\n", hres);
            hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
            ok(hres == S_OK, "Read failed: %08x\n\n", hres);
            hres = IInternetProtocol_UnlockRequest(protocol);
            ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
            hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
            ok(hres == S_OK, "Read failed: %08x\n", hres);
            hres = IInternetProtocol_Terminate(protocol, 0);
            ok(hres == S_OK, "Terminate failed: %08x\n", hres);
2527 2528
        }

2529 2530
        IInternetProtocol_Release(protocol);
    }
2531

2532 2533 2534 2535 2536 2537 2538 2539 2540
    hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
    ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
    if(SUCCEEDED(hres)) {
        if(file_protocol_start(protocol, url, NULL, NULL, TRUE)) {
            hres = IInternetProtocol_Terminate(protocol, 0);
            ok(hres == S_OK, "Terminate failed: %08x\n", hres);
            hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
            ok(hres == S_OK, "Read failed: %08x\n", hres);
            ok(cb == 2, "cb=%u expected 2\n", cb);
2541 2542
        }

2543 2544
        IInternetProtocol_Release(protocol);
    }
2545

2546 2547 2548 2549 2550 2551 2552 2553 2554
    if(pCreateUri) {
        IInternetProtocolEx *protocolex;
        IUri *uri;

        hres = pCreateUri(url, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
        ok(hres == S_OK, "CreateUri failed: %08x\n", hres);

        hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocolEx, (void**)&protocolex);
        ok(hres == S_OK, "Could not get IInternetProtocolEx: %08x\n", hres);
2555

2556 2557 2558 2559
        if(file_protocol_start(NULL, NULL, protocolex, uri, TRUE)) {
            hres = IInternetProtocolEx_Read(protocolex, buf, 2, &cb);
            ok(hres == S_OK, "Read failed: %08x\n", hres);
            hres = IInternetProtocolEx_LockRequest(protocolex, 0);
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
            ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
            hres = IInternetProtocolEx_UnlockRequest(protocolex);
            ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
        }

        IUri_Release(uri);
        IInternetProtocolEx_Release(protocolex);

        hres = pCreateUri(url, 0, 0, &uri);
        ok(hres == S_OK, "CreateUri failed: %08x\n", hres);

        hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocolEx, (void**)&protocolex);
        ok(hres == S_OK, "Could not get IInternetProtocolEx: %08x\n", hres);

        if(file_protocol_start(NULL, NULL, protocolex, uri, TRUE)) {
            hres = IInternetProtocolEx_Read(protocolex, buf, 2, &cb);
            ok(hres == S_OK, "Read failed: %08x\n", hres);
            hres = IInternetProtocolEx_LockRequest(protocolex, 0);
2578 2579 2580
            ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
            hres = IInternetProtocolEx_UnlockRequest(protocolex);
            ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
2581 2582
        }

2583 2584 2585 2586
        IUri_Release(uri);
        IInternetProtocolEx_Release(protocolex);
    }else {
        win_skip("Skipping file protocol StartEx tests\n");
2587 2588
    }

2589
    IClassFactory_Release(factory);
2590 2591
}

2592 2593
static void test_file_protocol_fail(void)
{
2594 2595 2596 2597 2598 2599 2600 2601
    IInternetProtocol *protocol;
    HRESULT hres;

    static const WCHAR index_url2[] =
        {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};

    hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
            &IID_IInternetProtocol, (void**)&protocol);
2602
    ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2603 2604 2605 2606 2607 2608
    if(FAILED(hres))
        return;

    SET_EXPECT(GetBindInfo);
    expect_hrResult = MK_E_SYNTAX;
    hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
2609 2610 2611 2612
    ok(hres == MK_E_SYNTAX ||
       hres == E_INVALIDARG,
       "Start failed: %08x, expected MK_E_SYNTAX or E_INVALIDARG\n", hres);
    CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2613 2614

    SET_EXPECT(GetBindInfo);
2615 2616
    if(!(bindf & BINDF_FROMURLMON))
        SET_EXPECT(ReportProgress_DIRECTBIND);
2617
    SET_EXPECT(ReportProgress_SENDINGREQUEST);
2618 2619 2620 2621
    SET_EXPECT(ReportResult);
    expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
    hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
    ok(hres == INET_E_RESOURCE_NOT_FOUND,
2622
            "Start failed: %08x expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2623
    CHECK_CALLED(GetBindInfo);
2624 2625
    if(!(bindf & BINDF_FROMURLMON))
        CHECK_CALLED(ReportProgress_DIRECTBIND);
2626
    CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2627 2628 2629 2630 2631 2632
    CHECK_CALLED(ReportResult);

    IInternetProtocol_Release(protocol);

    hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
            &IID_IInternetProtocol, (void**)&protocol);
2633
    ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2634 2635 2636 2637
    if(FAILED(hres))
        return;

    SET_EXPECT(GetBindInfo);
2638 2639
    if(!(bindf & BINDF_FROMURLMON))
        SET_EXPECT(ReportProgress_DIRECTBIND);
2640
    SET_EXPECT(ReportProgress_SENDINGREQUEST);
2641 2642
    SET_EXPECT(ReportResult);
    expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
2643

2644 2645
    hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
    ok(hres == INET_E_RESOURCE_NOT_FOUND,
2646
            "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
2647
    CHECK_CALLED(GetBindInfo);
2648 2649
    if(!(bindf & BINDF_FROMURLMON))
        CHECK_CALLED(ReportProgress_DIRECTBIND);
2650
    CHECK_CALLED(ReportProgress_SENDINGREQUEST);
2651 2652
    CHECK_CALLED(ReportResult);

2653 2654 2655
    SET_EXPECT(GetBindInfo);
    hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
    ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2656
    CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2657 2658 2659 2660

    SET_EXPECT(GetBindInfo);
    hres = IInternetProtocol_Start(protocol, emptyW, &protocol_sink, &bind_info, 0, 0);
    ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
2661
    CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
2662

2663 2664 2665
    IInternetProtocol_Release(protocol);
}

2666
static void test_file_protocol(void) {
2667
    WCHAR buf[INTERNET_MAX_URL_LENGTH], file_name_buf[MAX_PATH];
2668 2669 2670 2671 2672
    DWORD size;
    ULONG len;
    HANDLE file;

    static const WCHAR wszFile[] = {'f','i','l','e',':',0};
2673 2674
    static const WCHAR wszFile2[] = {'f','i','l','e',':','/','/',0};
    static const WCHAR wszFile3[] = {'f','i','l','e',':','/','/','/',0};
2675
    static const WCHAR wszFile4[] = {'f','i','l','e',':','\\','\\',0};
2676 2677
    static const char html_doc[] = "<HTML></HTML>";

2678
    trace("Testing file protocol...\n");
2679
    init_test(FILE_TEST, 0);
2680

2681
    SetLastError(0xdeadbeef);
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
    file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
    if(file == INVALID_HANDLE_VALUE)
        return;
    WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
    CloseHandle(file);

    file_name = wszIndexHtml;
    bindf = 0;
    test_file_protocol_url(index_url);
    bindf = BINDF_FROMURLMON;
    test_file_protocol_url(index_url);
2695 2696
    bindf = BINDF_FROMURLMON | BINDF_NEEDFILE;
    test_file_protocol_url(index_url);
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710

    memcpy(buf, wszFile, sizeof(wszFile));
    len = sizeof(wszFile)/sizeof(WCHAR)-1;
    len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
    buf[len++] = '\\';
    memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));

    file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
    bindf = 0;
    test_file_protocol_url(buf);
    bindf = BINDF_FROMURLMON;
    test_file_protocol_url(buf);

    memcpy(buf, wszFile2, sizeof(wszFile2));
2711 2712 2713 2714 2715
    len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
    file_name_buf[len++] = '\\';
    memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
    lstrcpyW(buf+sizeof(wszFile2)/sizeof(WCHAR)-1, file_name_buf);
    file_name = file_name_buf;
2716 2717 2718 2719 2720
    bindf = 0;
    test_file_protocol_url(buf);
    bindf = BINDF_FROMURLMON;
    test_file_protocol_url(buf);

2721 2722 2723
    buf[sizeof(wszFile2)/sizeof(WCHAR)] = '|';
    test_file_protocol_url(buf);

2724 2725 2726 2727 2728
    memcpy(buf, wszFile3, sizeof(wszFile3));
    len = sizeof(wszFile3)/sizeof(WCHAR)-1;
    len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
    buf[len++] = '\\';
    memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
2729

2730
    file_name = buf + sizeof(wszFile3)/sizeof(WCHAR)-1;
2731 2732 2733 2734 2735
    bindf = 0;
    test_file_protocol_url(buf);
    bindf = BINDF_FROMURLMON;
    test_file_protocol_url(buf);

2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
    memcpy(buf, wszFile4, sizeof(wszFile4));
    len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
    file_name_buf[len++] = '\\';
    memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
    lstrcpyW(buf+sizeof(wszFile4)/sizeof(WCHAR)-1, file_name_buf);
    file_name = file_name_buf;
    bindf = 0;
    test_file_protocol_url(buf);
    bindf = BINDF_FROMURLMON;
    test_file_protocol_url(buf);

    buf[sizeof(wszFile4)/sizeof(WCHAR)] = '|';
    test_file_protocol_url(buf);

2750 2751 2752 2753 2754 2755 2756 2757
    DeleteFileW(wszIndexHtml);

    bindf = 0;
    test_file_protocol_fail();
    bindf = BINDF_FROMURLMON;
    test_file_protocol_fail();
}

2758
static BOOL http_protocol_start(LPCWSTR url, BOOL use_iuri)
2759
{
2760
    static BOOL got_user_agent = FALSE;
2761
    IUri *uri = NULL;
2762 2763
    HRESULT hres;

2764 2765 2766 2767 2768
    if(use_iuri && pCreateUri) {
        hres = pCreateUri(url, 0, 0, &uri);
        ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
    }

2769
    SET_EXPECT(GetBindInfo);
2770 2771
    if (!(bindf & BINDF_FROMURLMON))
        SET_EXPECT(ReportProgress_DIRECTBIND);
2772 2773
    if(!got_user_agent)
        SET_EXPECT(GetBindString_USER_AGENT);
2774 2775 2776 2777
    SET_EXPECT(GetBindString_ACCEPT_MIMES);
    SET_EXPECT(QueryService_HttpNegotiate);
    SET_EXPECT(BeginningTransaction);
    SET_EXPECT(GetRootSecurityId);
2778
    if(http_post_test) {
2779
        SET_EXPECT(GetBindString_POST_COOKIE);
2780 2781 2782
        if(http_post_test == TYMED_ISTREAM)
            SET_EXPECT(Stream_Seek);
    }
2783

2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
    if(uri) {
        IInternetProtocolEx *protocolex;

        hres = IInternetProtocol_QueryInterface(async_protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
        ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);

        hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, 0, 0);
        ok(hres == S_OK, "Start failed: %08x\n", hres);

        IInternetProtocolEx_Release(protocolex);
        IUri_Release(uri);
    }else {
        hres = IInternetProtocol_Start(async_protocol, url, &protocol_sink, &bind_info, 0, 0);
        ok(hres == S_OK, "Start failed: %08x\n", hres);
    }
2799 2800 2801 2802
    if(FAILED(hres))
        return FALSE;

    CHECK_CALLED(GetBindInfo);
2803 2804
    if (!(bindf & BINDF_FROMURLMON))
        CHECK_CALLED(ReportProgress_DIRECTBIND);
2805 2806 2807 2808 2809
    if (!got_user_agent)
    {
        CHECK_CALLED(GetBindString_USER_AGENT);
        got_user_agent = TRUE;
    }
2810 2811 2812
    CHECK_CALLED(GetBindString_ACCEPT_MIMES);
    CHECK_CALLED(QueryService_HttpNegotiate);
    CHECK_CALLED(BeginningTransaction);
2813 2814
    /* GetRootSecurityId called on WinXP but not on Win98 */
    CLEAR_CALLED(GetRootSecurityId);
2815
    if(http_post_test) {
2816
        CHECK_CALLED(GetBindString_POST_COOKIE);
2817 2818 2819
        if(http_post_test == TYMED_ISTREAM)
            CHECK_CALLED(Stream_Seek);
    }
2820 2821 2822 2823

    return TRUE;
}

2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
static void test_protocol_terminate(IInternetProtocol *protocol)
{
    BYTE buf[3600];
    DWORD cb;
    HRESULT hres;

    hres = IInternetProtocol_LockRequest(protocol, 0);
    ok(hres == S_OK, "LockRequest failed: %08x\n", hres);

    hres = IInternetProtocol_Read(protocol, buf, 1, &cb);
Jacek Caban's avatar
Jacek Caban committed
2834
    ok(hres == test_abort ? S_OK : S_FALSE, "Read failed: %08x\n", hres);
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852

    hres = IInternetProtocol_Terminate(protocol, 0);
    ok(hres == S_OK, "Terminate failed: %08x\n", hres);

    /* This wait is to give the internet handles being freed in Terminate
     * enough time to actually terminate in all cases. Internet handles
     * terminate asynchronously and native reuses the main InternetOpen
     * handle. The only case in which this seems to be necessary is on
     * wine with native wininet and urlmon, resulting in the next time
     * test_http_protocol_url being called the first data notification actually
     * being an extra last data notification from the previous connection
     * about once out of every ten times. */
    Sleep(100);

    hres = IInternetProtocol_UnlockRequest(protocol);
    ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
}

2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
static void test_http_info(IInternetProtocol *protocol)
{
    IWinInetHttpInfo *info;
    HRESULT hres;

    hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&info);
    ok(hres == S_OK, "Could not get IWinInterHttpInfo iface: %08x\n", hres);

    /* TODO */

    IWinInetHttpInfo_Release(info);
}

2866 2867
/* is_first refers to whether this is the first call to this function
 * _for this url_ */
2868
static void test_http_protocol_url(LPCWSTR url, int prot, DWORD flags, DWORD tymed)
2869 2870 2871 2872 2873 2874
{
    IInternetProtocolInfo *protocol_info;
    IClassFactory *factory;
    IUnknown *unk;
    HRESULT hres;

2875
    init_test(prot, flags);
2876
    http_url = url;
2877
    http_post_test = tymed;
2878

2879
    hres = CoGetClassObject(prot == HTTPS_TEST ? &CLSID_HttpSProtocol : &CLSID_HttpProtocol,
2880
            CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
2881
    ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
2882
    if(FAILED(hres))
2883 2884 2885 2886
        return;

    hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
    ok(hres == E_NOINTERFACE,
2887
        "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
2888
        hres);
2889 2890 2891 2892 2893 2894 2895 2896

    hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
    ok(hres == S_OK, "Could not get IClassFactory interface\n");
    IUnknown_Release(unk);
    if(FAILED(hres))
        return;

    hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
2897
                                        (void**)&async_protocol);
2898
    ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
2899
    if(SUCCEEDED(hres)) {
2900
        BYTE buf[3600];
2901
        DWORD cb;
2902
        ULONG ref;
2903

2904
        test_priority(async_protocol);
2905
        test_http_info(async_protocol);
2906

2907
        SET_EXPECT(ReportProgress_COOKIE_SENT);
2908 2909 2910 2911
        if(http_is_first) {
            SET_EXPECT(ReportProgress_FINDINGRESOURCE);
            SET_EXPECT(ReportProgress_CONNECTING);
        }
2912
        SET_EXPECT(ReportProgress_SENDINGREQUEST);
2913 2914
        if(test_redirect)
            SET_EXPECT(ReportProgress_REDIRECTING);
2915
        SET_EXPECT(ReportProgress_PROXYDETECTING);
2916
        if(prot == HTTP_TEST)
2917
            SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
2918 2919
        else
            SET_EXPECT(QueryService_HttpSecurity);
2920
        if(!(bindf & BINDF_FROMURLMON)) {
2921 2922 2923
            SET_EXPECT(OnResponse);
            SET_EXPECT(ReportProgress_RAWMIMETYPE);
            SET_EXPECT(ReportData);
2924 2925
        } else {
            SET_EXPECT(Switch);
2926
        }
2927

2928
        if(!http_protocol_start(url, (flags & TEST_USEIURI) != 0))
2929 2930
            return;

Jacek Caban's avatar
Jacek Caban committed
2931
        if(!direct_read && !test_abort)
2932
            SET_EXPECT(ReportResult);
Jacek Caban's avatar
Jacek Caban committed
2933
        expect_hrResult = test_abort ? E_ABORT : S_OK;
2934

2935
        if(direct_read) {
2936
            SET_EXPECT(Switch);
2937 2938
            while(wait_for_switch) {
                WaitForSingleObject(event_continue, INFINITE);
2939
                CHECK_CALLED(Switch); /* Set in ReportData */
2940 2941 2942 2943 2944 2945 2946
                call_continue(&continue_protdata);
                SetEvent(event_continue_done);
            }
        }else {
            hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
            ok((hres == E_PENDING && cb==0) ||
               (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
2947

2948
            WaitForSingleObject(event_complete, INFINITE);
2949
            if(bindf & BINDF_FROMURLMON)
2950
                CHECK_CALLED(Switch);
2951
            else
2952 2953 2954 2955 2956
                CHECK_CALLED(ReportData);
            if(prot == HTTPS_TEST)
                CLEAR_CALLED(QueryService_HttpSecurity);

            while(1) {
2957
                if(bindf & BINDF_FROMURLMON)
2958
                    SET_EXPECT(Switch);
2959
                else
2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
                    SET_EXPECT(ReportData);
                hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
                if(hres == E_PENDING) {
                    hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
                    ok((hres == E_PENDING && cb==0) ||
                       (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
                    WaitForSingleObject(event_complete, INFINITE);
                    if(bindf & BINDF_FROMURLMON)
                        CHECK_CALLED(Switch);
                    else
                        CHECK_CALLED(ReportData);
Jacek Caban's avatar
Jacek Caban committed
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983

                    if(test_abort) {
                        HRESULT hres;

                        SET_EXPECT(ReportResult);
                        hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
                        ok(hres == S_OK, "Abort failed: %08x\n", hres);
                        CHECK_CALLED(ReportResult);

                        hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
                        ok(hres == INET_E_RESULT_DISPATCHED, "Abort failed: %08x\n", hres);
                        break;
                    }
2984 2985 2986 2987 2988 2989 2990
                }else {
                    if(bindf & BINDF_FROMURLMON)
                        CHECK_NOT_CALLED(Switch);
                    else
                        CHECK_NOT_CALLED(ReportData);
                    if(cb == 0) break;
                }
2991
            }
Jacek Caban's avatar
Jacek Caban committed
2992 2993 2994 2995
            if(!test_abort) {
                ok(hres == S_FALSE, "Read failed: %08x\n", hres);
                CHECK_CALLED(ReportResult);
            }
2996
        }
2997
        if(prot == HTTPS_TEST)
2998
            CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
2999

3000 3001 3002
        if (prot == HTTP_TEST || prot == HTTPS_TEST)
            CLEAR_CALLED(ReportProgress_COOKIE_SENT);

Jacek Caban's avatar
Jacek Caban committed
3003 3004 3005
        hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
        ok(hres == INET_E_RESULT_DISPATCHED, "Abort failed: %08x\n", hres);

3006
        test_protocol_terminate(async_protocol);
Jacek Caban's avatar
Jacek Caban committed
3007 3008 3009 3010

        hres = IInternetProtocol_Abort(async_protocol, E_ABORT, 0);
        ok(hres == S_OK, "Abort failed: %08x\n", hres);

3011
        ref = IInternetProtocol_Release(async_protocol);
Jacek Caban's avatar
Jacek Caban committed
3012
        ok(!ref, "ref=%x\n", ref);
3013 3014 3015 3016 3017 3018 3019
    }

    IClassFactory_Release(factory);
}

static void test_http_protocol(void)
{
3020
    static const WCHAR posttest_url[] =
3021 3022
        {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
         't','e','s','t','s','/','p','o','s','t','.','p','h','p',0};
3023 3024
    static const WCHAR redirect_url[] =
        {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
3025
         't','e','s','t','s','/','r','e','d','i','r','e','c','t',0};
3026
    static const WCHAR winetest_url[] =
3027 3028
        {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
         't','e','s','t','s','/','d','a','t','a','.','p','h','p',0};
3029 3030 3031
    static const WCHAR empty_url[] =
        {'h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g','/',
         't','e','s','t','s','/','e','m','p','t','y','.','j','s',0};
3032

3033
    trace("Testing http protocol (not from urlmon)...\n");
3034
    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
3035
    test_http_protocol_url(winetest_url, HTTP_TEST, TEST_FIRST_HTTP, TYMED_NULL);
3036 3037

    trace("Testing http protocol (from urlmon)...\n");
3038
    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
3039
    test_http_protocol_url(winetest_url, HTTP_TEST, 0, TYMED_NULL);
3040

3041 3042
    trace("Testing http protocol (to file)...\n");
    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
3043
    test_http_protocol_url(winetest_url, HTTP_TEST, 0, TYMED_NULL);
3044

3045
    trace("Testing http protocol (post data)...\n");
3046
    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
3047 3048 3049 3050
    test_http_protocol_url(posttest_url, HTTP_TEST, TEST_FIRST_HTTP|TEST_POST, TYMED_HGLOBAL);

    trace("Testing http protocol (post data stream)...\n");
    test_http_protocol_url(posttest_url, HTTP_TEST, TEST_FIRST_HTTP|TEST_POST|TEST_ASYNCREQ, TYMED_ISTREAM);
3051 3052 3053

    trace("Testing http protocol (direct read)...\n");
    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
3054
    test_http_protocol_url(winetest_url, HTTP_TEST, TEST_DIRECT_READ|TEST_USEIURI, TYMED_NULL);
3055 3056

    trace("Testing http protocol (redirected)...\n");
3057
    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
3058
    test_http_protocol_url(redirect_url, HTTP_TEST, TEST_REDIRECT, TYMED_NULL);
Jacek Caban's avatar
Jacek Caban committed
3059

3060 3061 3062
    trace("Testing http protocol empty file...\n");
    test_http_protocol_url(empty_url, HTTP_TEST, TEST_EMPTY, TYMED_NULL);

Jacek Caban's avatar
Jacek Caban committed
3063
    trace("Testing http protocol abort...\n");
3064
    test_http_protocol_url(winetest_url, HTTP_TEST, TEST_ABORT, TYMED_NULL);
Jacek Caban's avatar
Jacek Caban committed
3065 3066 3067

    test_early_abort(&CLSID_HttpProtocol);
    test_early_abort(&CLSID_HttpSProtocol);
3068 3069
}

3070 3071 3072 3073 3074 3075 3076 3077
static void test_https_protocol(void)
{
    static const WCHAR codeweavers_url[] =
        {'h','t','t','p','s',':','/','/','w','w','w','.','c','o','d','e','w','e','a','v','e','r','s',
         '.','c','o','m','/','t','e','s','t','.','h','t','m','l',0};

    trace("Testing https protocol (from urlmon)...\n");
    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
3078
    test_http_protocol_url(codeweavers_url, HTTPS_TEST, TEST_FIRST_HTTP, TYMED_NULL);
3079 3080
}

3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098

static void test_ftp_protocol(void)
{
    IInternetProtocolInfo *protocol_info;
    IClassFactory *factory;
    IUnknown *unk;
    BYTE buf[4096];
    ULONG ref;
    DWORD cb;
    HRESULT hres;

    static const WCHAR ftp_urlW[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g',
    '/','p','u','b','/','o','t','h','e','r','/',
    'w','i','n','e','l','o','g','o','.','x','c','f','.','t','a','r','.','b','z','2',0};

    trace("Testing ftp protocol...\n");

    bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
3099
    state = STATE_STARTDOWNLOADING;
3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
    tested_protocol = FTP_TEST;
    first_data_notif = TRUE;
    expect_hrResult = E_PENDING;

    hres = CoGetClassObject(&CLSID_FtpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
    ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
    if(FAILED(hres))
        return;

    hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
    ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);

    hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
    ok(hres == S_OK, "Could not get IClassFactory interface\n");
    IUnknown_Release(unk);
    if(FAILED(hres))
        return;

    hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
                                        (void**)&async_protocol);
    IClassFactory_Release(factory);
    ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);

    test_priority(async_protocol);
3124
    test_http_info(async_protocol);
3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145

    SET_EXPECT(GetBindInfo);
    SET_EXPECT(ReportProgress_FINDINGRESOURCE);
    SET_EXPECT(ReportProgress_CONNECTING);
    SET_EXPECT(ReportProgress_SENDINGREQUEST);
    SET_EXPECT(Switch);

    hres = IInternetProtocol_Start(async_protocol, ftp_urlW, &protocol_sink, &bind_info, 0, 0);
    ok(hres == S_OK, "Start failed: %08x\n", hres);
    CHECK_CALLED(GetBindInfo);

    SET_EXPECT(ReportResult);

    hres = IInternetProtocol_Read(async_protocol, buf, 1, &cb);
    ok((hres == E_PENDING && cb==0) ||
       (hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);

    WaitForSingleObject(event_complete, INFINITE);

    while(1) {
        hres = IInternetProtocol_Read(async_protocol, buf, sizeof(buf), &cb);
3146
        if(hres == E_PENDING)
3147
            WaitForSingleObject(event_complete, INFINITE);
3148
        else
3149 3150 3151 3152 3153
            if(cb == 0) break;
    }

    ok(hres == S_FALSE, "Read failed: %08x\n", hres);
    CHECK_CALLED(ReportResult);
3154
    CHECK_CALLED(Switch);
3155 3156 3157

    test_protocol_terminate(async_protocol);

3158 3159 3160 3161 3162 3163 3164 3165
    if(pCreateUri) {
        IInternetProtocolEx *protocolex;

        hres = IInternetProtocol_QueryInterface(async_protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
        ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);
        IInternetProtocolEx_Release(protocolex);
    }

3166 3167
    ref = IInternetProtocol_Release(async_protocol);
    ok(!ref, "ref=%d\n", ref);
Jacek Caban's avatar
Jacek Caban committed
3168 3169

    test_early_abort(&CLSID_FtpProtocol);
3170 3171
}

3172 3173 3174 3175 3176 3177 3178 3179 3180 3181
static void test_gopher_protocol(void)
{
    IInternetProtocolInfo *protocol_info;
    IClassFactory *factory;
    IUnknown *unk;
    HRESULT hres;

    trace("Testing gopher protocol...\n");

    hres = CoGetClassObject(&CLSID_GopherProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
3182 3183 3184
    ok(hres == S_OK ||
       hres == REGDB_E_CLASSNOTREG, /* Gopher protocol has been removed as of Vista */
       "CoGetClassObject failed: %08x\n", hres);
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
    if(FAILED(hres))
        return;

    hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
    ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);

    hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
    ok(hres == S_OK, "Could not get IClassFactory interface\n");
    IUnknown_Release(unk);
    if(FAILED(hres))
        return;

    hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
                                        (void**)&async_protocol);
    IClassFactory_Release(factory);
    ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);

3202 3203
    test_priority(async_protocol);

3204
    IInternetProtocol_Release(async_protocol);
Jacek Caban's avatar
Jacek Caban committed
3205 3206

    test_early_abort(&CLSID_GopherProtocol);
3207 3208
}

3209 3210 3211 3212 3213 3214 3215 3216
static void test_mk_protocol(void)
{
    IInternetProtocolInfo *protocol_info;
    IInternetProtocol *protocol;
    IClassFactory *factory;
    IUnknown *unk;
    HRESULT hres;

3217 3218 3219 3220
    static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
                                       ':',':','/','t','e','s','t','.','h','t','m','l',0};
    static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};

3221
    trace("Testing mk protocol...\n");
3222
    init_test(MK_TEST, 0);
3223

3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
    hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
            &IID_IUnknown, (void**)&unk);
    ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);

    hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
    ok(hres == E_NOINTERFACE,
        "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
        hres);

    hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
    ok(hres == S_OK, "Could not get IClassFactory interface\n");
    IUnknown_Release(unk);
    if(FAILED(hres))
        return;

    hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
                                        (void**)&protocol);
    IClassFactory_Release(factory);
    ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);

3244 3245
    SET_EXPECT(GetBindInfo);
    hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
3246 3247 3248
    ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
       "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
    CLEAR_CALLED(GetBindInfo);
3249 3250 3251 3252 3253 3254 3255 3256 3257

    SET_EXPECT(GetBindInfo);
    SET_EXPECT(ReportProgress_DIRECTBIND);
    SET_EXPECT(ReportProgress_SENDINGREQUEST);
    SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
    SET_EXPECT(ReportResult);
    expect_hrResult = INET_E_RESOURCE_NOT_FOUND;

    hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
3258 3259 3260
    ok(hres == INET_E_RESOURCE_NOT_FOUND ||
       hres == INET_E_INVALID_URL, /* win2k3 */
       "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
3261

3262
    if (hres == INET_E_RESOURCE_NOT_FOUND) {
3263 3264 3265 3266 3267
        CHECK_CALLED(GetBindInfo);
        CLEAR_CALLED(ReportProgress_DIRECTBIND);
        CHECK_CALLED(ReportProgress_SENDINGREQUEST);
        CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
        CHECK_CALLED(ReportResult);
3268 3269 3270 3271 3272 3273
    }else {
        CLEAR_CALLED(GetBindInfo);
        CLEAR_CALLED(ReportProgress_DIRECTBIND);
        CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
        CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
        CLEAR_CALLED(ReportResult);
3274
    }
3275

3276 3277 3278
    IInternetProtocol_Release(protocol);
}

3279 3280 3281
static void test_CreateBinding(void)
{
    IInternetProtocol *protocol;
3282
    IInternetPriority *priority;
3283
    IInternetSession *session;
3284 3285
    IWinInetHttpInfo *http_info;
    IWinInetInfo *inet_info;
3286
    LONG p;
3287 3288 3289 3290 3291 3292 3293 3294 3295
    BYTE buf[1000];
    DWORD read;
    HRESULT hres;

    static const WCHAR test_url[] =
        {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
    static const WCHAR wsz_test[] = {'t','e','s','t',0};

    trace("Testing CreateBinding...\n");
3296
    init_test(BIND_TEST, TEST_BINDING);
3297

3298
    hres = pCoInternetGetSession(0, &session, 0);
3299 3300 3301 3302 3303 3304
    ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);

    hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
    ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);

    hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
3305
    binding_protocol = protocol;
3306 3307 3308 3309 3310 3311
    ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
    ok(protocol != NULL, "protocol == NULL\n");

    hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
    ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);

3312
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
3313 3314
    ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);

3315 3316 3317 3318 3319 3320 3321
    hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
    ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
    hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
    ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
    hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
    ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);

3322 3323 3324 3325 3326 3327 3328 3329
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
    ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);

    p = 0xdeadbeef;
    hres = IInternetPriority_GetPriority(priority, &p);
    ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
    ok(!p, "p=%d\n", p);

3330
    ex_priority = 100;
3331 3332 3333 3334 3335 3336 3337 3338
    hres = IInternetPriority_SetPriority(priority, 100);
    ok(hres == S_OK, "SetPriority failed: %08x\n", hres);

    p = 0xdeadbeef;
    hres = IInternetPriority_GetPriority(priority, &p);
    ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
    ok(p == 100, "p=%d\n", p);

3339 3340 3341
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
    ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);

3342 3343 3344
    SET_EXPECT(QueryService_InternetProtocol);
    SET_EXPECT(CreateInstance);
    SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
3345
    SET_EXPECT(SetPriority);
3346 3347 3348 3349 3350 3351 3352 3353 3354
    SET_EXPECT(Start);

    expect_hrResult = S_OK;
    hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
    ok(hres == S_OK, "Start failed: %08x\n", hres);

    CHECK_CALLED(QueryService_InternetProtocol);
    CHECK_CALLED(CreateInstance);
    CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
3355
    CHECK_CALLED(SetPriority);
3356 3357
    CHECK_CALLED(Start);

3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
    SET_EXPECT(QueryInterface_IWinInetInfo);
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
    ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
    CHECK_CALLED(QueryInterface_IWinInetInfo);

    SET_EXPECT(QueryInterface_IWinInetInfo);
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&inet_info);
    ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
    CHECK_CALLED(QueryInterface_IWinInetInfo);

    SET_EXPECT(QueryInterface_IWinInetHttpInfo);
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IWinInetHttpInfo, (void**)&http_info);
    ok(hres == E_NOINTERFACE, "Could not get IWinInetInfo protocol: %08x\n", hres);
    CHECK_CALLED(QueryInterface_IWinInetHttpInfo);

3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386
    SET_EXPECT(Read);
    read = 0xdeadbeef;
    hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
    ok(hres == S_OK, "Read failed: %08x\n", hres);
    ok(read == 100, "read = %d\n", read);
    CHECK_CALLED(Read);

    SET_EXPECT(Read);
    read = 0xdeadbeef;
    hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
    ok(hres == S_FALSE, "Read failed: %08x\n", hres);
    ok(!read, "read = %d\n", read);
    CHECK_CALLED(Read);

3387 3388 3389 3390 3391 3392 3393 3394
    p = 0xdeadbeef;
    hres = IInternetPriority_GetPriority(priority, &p);
    ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
    ok(p == 100, "p=%d\n", p);

    hres = IInternetPriority_SetPriority(priority, 101);
    ok(hres == S_OK, "SetPriority failed: %08x\n", hres);

3395 3396 3397 3398 3399
    SET_EXPECT(Terminate);
    hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
    ok(hres == S_OK, "Terminate failed: %08x\n", hres);
    CHECK_CALLED(Terminate);

3400
    SET_EXPECT(Continue);
3401
    hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
3402 3403 3404
    ok(hres == S_OK, "Switch failed: %08x\n", hres);
    CHECK_CALLED(Continue);

3405
    hres = IInternetProtocolSink_ReportProgress(binding_sink,
3406 3407 3408
            BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
    ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);

3409
    hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
3410 3411
    ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);

3412
    hres = IInternetProtocolSink_ReportData(binding_sink, 0, 0, 0);
3413 3414
    ok(hres == S_OK, "ReportData failed: %08x\n", hres);

3415
    IInternetProtocolSink_Release(binding_sink);
3416
    IInternetPriority_Release(priority);
3417 3418
    IInternetBindInfo_Release(prot_bind_info);
    IInternetProtocol_Release(protocol);
3419

3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431
    hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
    ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
    ok(protocol != NULL, "protocol == NULL\n");

    hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
    ok(hres == S_OK, "Abort failed: %08x\n", hres);

    hres = IInternetProtocol_Abort(protocol, E_FAIL, 0);
    ok(hres == S_OK, "Abort failed: %08x\n", hres);

    IInternetProtocol_Release(protocol);

3432 3433 3434
    hres = IInternetSession_UnregisterNameSpace(session, &ClassFactory, wsz_test);
    ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);

3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
    hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
    ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
    ok(protocol != NULL, "protocol == NULL\n");

    SET_EXPECT(QueryService_InternetProtocol);
    hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
    ok(hres == MK_E_SYNTAX, "Start failed: %08x, expected MK_E_SYNTAX\n", hres);
    CHECK_CALLED(QueryService_InternetProtocol);

    IInternetProtocol_Release(protocol);

3446 3447 3448
    IInternetSession_Release(session);
}

3449
static void test_binding(int prot, DWORD grf_pi, DWORD test_flags)
3450
{
3451
    IInternetProtocolEx *protocolex = NULL;
3452 3453
    IInternetProtocol *protocol;
    IInternetSession *session;
3454
    IUri *uri = NULL;
3455
    ULONG ref;
3456 3457
    HRESULT hres;

3458
    pi = grf_pi;
3459

3460 3461
    init_test(prot, test_flags|TEST_BINDING);

3462
    hres = pCoInternetGetSession(0, &session, 0);
3463 3464
    ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);

3465 3466 3467 3468
    if(test_flags & TEST_EMULATEPROT) {
        hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
        ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
    }
3469 3470

    hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
3471
    binding_protocol = protocol;
3472 3473 3474 3475 3476 3477
    ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
    ok(protocol != NULL, "protocol == NULL\n");

    hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
    ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);

3478 3479
    hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
    ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
3480

3481 3482 3483 3484 3485 3486 3487 3488
    if(test_flags & TEST_USEIURI) {
        hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
        ok(hres == S_OK, "Could not get IInternetProtocolEx iface: %08x\n", hres);

        hres = pCreateUri(binding_urls[prot], Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
        ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
    }

3489 3490 3491 3492 3493
    ex_priority = 0;
    SET_EXPECT(QueryService_InternetProtocol);
    SET_EXPECT(CreateInstance);
    SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
    SET_EXPECT(SetPriority);
3494 3495 3496 3497
    if(impl_protex)
        SET_EXPECT(StartEx);
    else
        SET_EXPECT(Start);
3498 3499

    expect_hrResult = S_OK;
3500 3501 3502 3503 3504 3505 3506 3507

    if(protocolex) {
        hres = IInternetProtocolEx_StartEx(protocolex, uri, &protocol_sink, &bind_info, pi, 0);
        ok(hres == S_OK, "StartEx failed: %08x\n", hres);
    }else {
        hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, pi, 0);
        ok(hres == S_OK, "Start failed: %08x\n", hres);
    }
3508 3509 3510 3511 3512

    CHECK_CALLED(QueryService_InternetProtocol);
    CHECK_CALLED(CreateInstance);
    CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
    CHECK_CALLED(SetPriority);
3513 3514 3515 3516 3517 3518 3519 3520 3521
    if(impl_protex)
        CHECK_CALLED(StartEx);
    else
        CHECK_CALLED(Start);

    if(protocolex)
        IInternetProtocolEx_Release(protocolex);
    if(uri)
        IUri_Release(uri);
3522

3523
    if(prot == HTTP_TEST || prot == HTTPS_TEST) {
3524
        while(prot_state < 4) {
3525
            WaitForSingleObject(event_complete, INFINITE);
3526 3527 3528 3529 3530 3531 3532 3533 3534
            if(mimefilter_test && filtered_protocol) {
                SET_EXPECT(Continue);
                IInternetProtocol_Continue(filtered_protocol, pdata);
                CHECK_CALLED(Continue);
            }else {
                SET_EXPECT(Continue);
                IInternetProtocol_Continue(protocol, pdata);
                CHECK_CALLED(Continue);
            }
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
            if(test_abort && prot_state == 2) {
                SET_EXPECT(Abort);
                hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
                ok(hres == S_OK, "Abort failed: %08x\n", hres);
                CHECK_CALLED(Abort);

                hres = IInternetProtocol_Abort(protocol, E_ABORT, 0);
                ok(hres == S_OK, "Abort failed: %08x\n", hres);
                SetEvent(event_complete2);
                break;
            }
3546 3547
            SetEvent(event_complete2);
        }
3548 3549
        if(direct_read)
            CHECK_CALLED(ReportData); /* Set in ReportResult */
3550
        WaitForSingleObject(event_complete, INFINITE);
3551
    }else {
3552 3553 3554 3555
        if(mimefilter_test)
            SET_EXPECT(MimeFilter_LockRequest);
        else
            SET_EXPECT(LockRequest);
3556 3557
        hres = IInternetProtocol_LockRequest(protocol, 0);
        ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
3558 3559 3560 3561
        if(mimefilter_test)
            CHECK_CALLED(MimeFilter_LockRequest);
        else
            CHECK_CALLED(LockRequest);
3562

3563 3564 3565 3566
        if(mimefilter_test)
            SET_EXPECT(MimeFilter_UnlockRequest);
        else
            SET_EXPECT(UnlockRequest);
3567 3568
        hres = IInternetProtocol_UnlockRequest(protocol);
        ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
3569 3570 3571 3572
        if(mimefilter_test)
            CHECK_CALLED(MimeFilter_UnlockRequest);
        else
            CHECK_CALLED(UnlockRequest);
3573 3574
    }

3575 3576 3577 3578
    if(mimefilter_test)
        SET_EXPECT(MimeFilter_Terminate);
    else
        SET_EXPECT(Terminate);
3579 3580
    hres = IInternetProtocol_Terminate(protocol, 0);
    ok(hres == S_OK, "Terminate failed: %08x\n", hres);
3581
    if(mimefilter_test)
3582
        CLEAR_CALLED(MimeFilter_Terminate);
3583 3584
    else
        CHECK_CALLED(Terminate);
3585

3586 3587
    if(filtered_protocol)
        IInternetProtocol_Release(filtered_protocol);
3588
    IInternetBindInfo_Release(prot_bind_info);
3589 3590 3591
    IInternetProtocolSink_Release(binding_sink);
    ref = IInternetProtocol_Release(protocol);
    ok(!ref, "ref=%u, expected 0\n", ref);
3592 3593 3594 3595 3596 3597 3598

    if(test_flags & TEST_EMULATEPROT) {
        hres = IInternetSession_UnregisterNameSpace(session, &ClassFactory, protocol_names[prot]);
        ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
    }

    IInternetSession_Release(session);
3599 3600
}

3601 3602
START_TEST(protocol)
{
3603 3604 3605 3606 3607
    HMODULE hurlmon;

    hurlmon = GetModuleHandle("urlmon.dll");
    pCoInternetGetSession = (void*) GetProcAddress(hurlmon, "CoInternetGetSession");
    pReleaseBindInfo = (void*) GetProcAddress(hurlmon, "ReleaseBindInfo");
3608
    pCreateUri = (void*) GetProcAddress(hurlmon, "CreateUri");
3609

3610 3611
    if(!GetProcAddress(hurlmon, "CompareSecurityIds")) {
        win_skip("Various needed functions not present, too old IE\n");
3612 3613 3614
        return;
    }

3615 3616 3617
    if(!pCreateUri)
        win_skip("CreateUri not supported\n");

3618 3619
    OleInitialize(NULL);

3620
    event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
3621
    event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
3622 3623
    event_continue = CreateEvent(NULL, FALSE, FALSE, NULL);
    event_continue_done = CreateEvent(NULL, FALSE, FALSE, NULL);
3624
    thread_id = GetCurrentThreadId();
3625

3626
    test_file_protocol();
3627
    test_http_protocol();
3628
    test_https_protocol();
3629
    test_ftp_protocol();
3630
    test_gopher_protocol();
3631
    test_mk_protocol();
3632
    test_CreateBinding();
3633 3634 3635 3636 3637 3638

    bindf &= ~BINDF_FROMURLMON;
    trace("Testing file binding (mime verification, emulate prot)...\n");
    test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
    trace("Testing http binding (mime verification, emulate prot)...\n");
    test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3639 3640
    trace("Testing its binding (mime verification, emulate prot)...\n");
    test_binding(ITS_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT);
3641 3642 3643 3644 3645 3646
    trace("Testing http binding (mime verification, emulate prot, short read, direct read)...\n");
    test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_SHORT_READ|TEST_DIRECT_READ);
    trace("Testing file binding (mime verification, emulate prot, mime filter)...\n");
    test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_FILTER);
    trace("Testing http binding (mime verification, emulate prot, mime filter)...\n");
    test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_FILTER);
3647 3648
    trace("Testing http binding (mime verification, emulate prot, mime filter, no mime)...\n");
    test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_FILTER|TEST_NOMIME);
3649 3650
    trace("Testing http binding (mime verification, emulate prot, direct read)...\n");
    test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_DIRECT_READ);
3651 3652
    trace("Testing http binding (mime verification, emulate prot, abort)...\n");
    test_binding(HTTP_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_ABORT);
3653 3654 3655 3656 3657 3658 3659 3660
    if(pCreateUri) {
        trace("Testing file binding (use IUri, mime verification, emulate prot)...\n");
        test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_USEIURI);
        trace("Testing file binding (use IUri, impl StartEx, mime verification, emulate prot)...\n");
        test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_USEIURI|TEST_IMPLPROTEX);
        trace("Testing file binding (impl StartEx, mime verification, emulate prot)...\n");
        test_binding(FILE_TEST, PI_MIMEVERIFICATION, TEST_EMULATEPROT|TEST_IMPLPROTEX);
    }
3661

3662
    CloseHandle(event_complete);
3663
    CloseHandle(event_complete2);
3664 3665
    CloseHandle(event_continue);
    CloseHandle(event_continue_done);
3666

3667 3668
    OleUninitialize();
}