Merge trunk head (r43756)
[reactos.git] / rostests / winetests / urlmon / protocol.c
1 /*
2 * Copyright 2005-2007 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * 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 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "ole2.h"
29 #include "urlmon.h"
30 #include "wininet.h"
31
32 #include "initguid.h"
33
34 #define DEFINE_EXPECT(func) \
35 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
36
37 #define SET_EXPECT(func) \
38 expect_ ## func = TRUE
39
40 #define CHECK_EXPECT2(func) \
41 do { \
42 ok(expect_ ##func, "unexpected call " #func "\n"); \
43 called_ ## func = TRUE; \
44 }while(0)
45
46 #define CHECK_EXPECT(func) \
47 do { \
48 CHECK_EXPECT2(func); \
49 expect_ ## func = FALSE; \
50 }while(0)
51
52 #define CHECK_CALLED(func) \
53 do { \
54 ok(called_ ## func, "expected " #func "\n"); \
55 expect_ ## func = called_ ## func = FALSE; \
56 }while(0)
57
58 #define CHECK_NOT_CALLED(func) \
59 do { \
60 ok(!called_ ## func, "unexpected " #func "\n"); \
61 expect_ ## func = called_ ## func = FALSE; \
62 }while(0)
63
64 #define CLEAR_CALLED(func) \
65 expect_ ## func = called_ ## func = FALSE
66
67 DEFINE_EXPECT(GetBindInfo);
68 DEFINE_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
69 DEFINE_EXPECT(ReportProgress_DIRECTBIND);
70 DEFINE_EXPECT(ReportProgress_RAWMIMETYPE);
71 DEFINE_EXPECT(ReportProgress_FINDINGRESOURCE);
72 DEFINE_EXPECT(ReportProgress_CONNECTING);
73 DEFINE_EXPECT(ReportProgress_SENDINGREQUEST);
74 DEFINE_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
75 DEFINE_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
76 DEFINE_EXPECT(ReportProgress_PROTOCOLCLASSID);
77 DEFINE_EXPECT(ReportProgress_COOKIE_SENT);
78 DEFINE_EXPECT(ReportProgress_REDIRECTING);
79 DEFINE_EXPECT(ReportData);
80 DEFINE_EXPECT(ReportResult);
81 DEFINE_EXPECT(GetBindString_ACCEPT_MIMES);
82 DEFINE_EXPECT(GetBindString_USER_AGENT);
83 DEFINE_EXPECT(GetBindString_POST_COOKIE);
84 DEFINE_EXPECT(QueryService_HttpNegotiate);
85 DEFINE_EXPECT(QueryService_InternetProtocol);
86 DEFINE_EXPECT(BeginningTransaction);
87 DEFINE_EXPECT(GetRootSecurityId);
88 DEFINE_EXPECT(OnResponse);
89 DEFINE_EXPECT(Switch);
90 DEFINE_EXPECT(Continue);
91 DEFINE_EXPECT(CreateInstance);
92 DEFINE_EXPECT(Start);
93 DEFINE_EXPECT(Terminate);
94 DEFINE_EXPECT(Read);
95 DEFINE_EXPECT(SetPriority);
96 DEFINE_EXPECT(LockRequest);
97 DEFINE_EXPECT(UnlockRequest);
98
99 static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
100 static const WCHAR index_url[] =
101 {'f','i','l','e',':','i','n','d','e','x','.','h','t','m','l',0};
102
103 static const WCHAR acc_mimeW[] = {'*','/','*',0};
104 static const WCHAR user_agentW[] = {'W','i','n','e',0};
105 static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
106 static const WCHAR hostW[] = {'w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
107 static const WCHAR winehq_ipW[] = {'2','0','9','.','4','6','.','2','5','.','1','3','4',0};
108 static const WCHAR emptyW[] = {0};
109
110 static HRESULT expect_hrResult;
111 static LPCWSTR file_name, http_url, expect_wsz;
112 static IInternetProtocol *http_protocol = NULL;
113 static BOOL first_data_notif = FALSE, http_is_first = FALSE,
114 http_post_test = FALSE;
115 static int state = 0, prot_state;
116 static DWORD bindf = 0, ex_priority = 0;
117 static IInternetProtocol *binding_protocol;
118 static IInternetBindInfo *prot_bind_info;
119 static IInternetProtocolSink *binding_sink;
120 static void *expect_pv;
121 static HANDLE event_complete, event_complete2;
122 static BOOL binding_test;
123 static PROTOCOLDATA protocoldata, *pdata;
124 static DWORD prot_read;
125
126 static enum {
127 FILE_TEST,
128 HTTP_TEST,
129 MK_TEST,
130 BIND_TEST
131 } tested_protocol;
132
133 static const WCHAR protocol_names[][10] = {
134 {'f','i','l','e',0},
135 {'h','t','t','p',0},
136 {'m','k',0},
137 {'t','e','s','t',0}
138 };
139
140 static const WCHAR binding_urls[][30] = {
141 {'f','i','l','e',':','t','e','s','t','.','h','t','m','l',0},
142 {'h','t','t','p',':','/','/','t','e','s','t','/','t','e','s','t','.','h','t','m','l',0},
143 {'m','k',':','t','e','s','t',0},
144 {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0}
145 };
146
147 static const char *debugstr_w(LPCWSTR str)
148 {
149 static char buf[512];
150 if(!str)
151 return "(null)";
152 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
153 return buf;
154 }
155
156 static const char *debugstr_guid(REFIID riid)
157 {
158 static char buf[50];
159
160 sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
161 riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
162 riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
163 riid->Data4[5], riid->Data4[6], riid->Data4[7]);
164
165 return buf;
166 }
167
168 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface, REFIID riid, void **ppv)
169 {
170 if(IsEqualGUID(&IID_IUnknown, riid)
171 || IsEqualGUID(&IID_IHttpNegotiate, riid)
172 || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
173 *ppv = iface;
174 return S_OK;
175 }
176
177 ok(0, "unexpected call\n");
178 return E_NOINTERFACE;
179 }
180
181 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
182 {
183 return 2;
184 }
185
186 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
187 {
188 return 1;
189 }
190
191 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface, LPCWSTR szURL,
192 LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
193 {
194 LPWSTR addl_headers;
195
196 static const WCHAR wszHeaders[] =
197 {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ','a','p','p','l','i','c','a','t',
198 'i','o','n','/','x','-','w','w','w','-','f','o','r','m','-','u','r','l','e','n','c','o',
199 'd','e','d','\r','\n',0};
200
201 CHECK_EXPECT(BeginningTransaction);
202
203 if(binding_test)
204 ok(!lstrcmpW(szURL, binding_urls[tested_protocol]), "szURL != http_url\n");
205 else
206 ok(!lstrcmpW(szURL, http_url), "szURL != http_url\n");
207 ok(!dwReserved, "dwReserved=%d, expected 0\n", dwReserved);
208 ok(pszAdditionalHeaders != NULL, "pszAdditionalHeaders == NULL\n");
209 if(pszAdditionalHeaders)
210 {
211 ok(*pszAdditionalHeaders == NULL, "*pszAdditionalHeaders != NULL\n");
212 if (http_post_test)
213 {
214 addl_headers = CoTaskMemAlloc(sizeof(wszHeaders));
215 if (!addl_headers)
216 {
217 http_post_test = FALSE;
218 skip("Out of memory\n");
219 return E_OUTOFMEMORY;
220 }
221 lstrcpyW(addl_headers, wszHeaders);
222 *pszAdditionalHeaders = addl_headers;
223 }
224 }
225
226 return S_OK;
227 }
228
229 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
230 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
231 {
232 CHECK_EXPECT(OnResponse);
233
234 ok(dwResponseCode == 200, "dwResponseCode=%d, expected 200\n", dwResponseCode);
235 ok(szResponseHeaders != NULL, "szResponseHeaders == NULL\n");
236 ok(szRequestHeaders == NULL, "szRequestHeaders != NULL\n");
237 ok(pszAdditionalRequestHeaders == NULL, "pszAdditionalHeaders != NULL\n");
238
239 return S_OK;
240 }
241
242 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
243 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
244 {
245 static const BYTE sec_id[] = {'h','t','t','p',':','t','e','s','t',1,0,0,0};
246
247 CHECK_EXPECT(GetRootSecurityId);
248
249 ok(!dwReserved, "dwReserved=%ld, expected 0\n", dwReserved);
250 ok(pbSecurityId != NULL, "pbSecurityId == NULL\n");
251 ok(pcbSecurityId != NULL, "pcbSecurityId == NULL\n");
252
253 if(pcbSecurityId) {
254 ok(*pcbSecurityId == 512, "*pcbSecurityId=%d, expected 512\n", *pcbSecurityId);
255 *pcbSecurityId = sizeof(sec_id);
256 }
257
258 if(pbSecurityId)
259 memcpy(pbSecurityId, sec_id, sizeof(sec_id));
260
261 return E_FAIL;
262 }
263
264 static IHttpNegotiate2Vtbl HttpNegotiateVtbl = {
265 HttpNegotiate_QueryInterface,
266 HttpNegotiate_AddRef,
267 HttpNegotiate_Release,
268 HttpNegotiate_BeginningTransaction,
269 HttpNegotiate_OnResponse,
270 HttpNegotiate_GetRootSecurityId
271 };
272
273 static IHttpNegotiate2 http_negotiate = { &HttpNegotiateVtbl };
274
275 static HRESULT QueryInterface(REFIID,void**);
276
277 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
278 {
279 return QueryInterface(riid, ppv);
280 }
281
282 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
283 {
284 return 2;
285 }
286
287 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
288 {
289 return 1;
290 }
291
292 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
293 REFIID riid, void **ppv)
294 {
295 if(IsEqualGUID(&IID_IHttpNegotiate, guidService) || IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
296 CHECK_EXPECT2(QueryService_HttpNegotiate);
297 return IHttpNegotiate2_QueryInterface(&http_negotiate, riid, ppv);
298 }
299
300 if(IsEqualGUID(&IID_IInternetProtocol, guidService)) {
301 ok(IsEqualGUID(&IID_IInternetProtocol, riid), "unexpected riid\n");
302 CHECK_EXPECT(QueryService_InternetProtocol);
303 return E_NOINTERFACE;
304 }
305
306 ok(0, "unexpected service %s\n", debugstr_guid(guidService));
307 return E_FAIL;
308 }
309
310 static const IServiceProviderVtbl ServiceProviderVtbl = {
311 ServiceProvider_QueryInterface,
312 ServiceProvider_AddRef,
313 ServiceProvider_Release,
314 ServiceProvider_QueryService
315 };
316
317 static IServiceProvider service_provider = { &ServiceProviderVtbl };
318
319 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
320 {
321 return QueryInterface(riid, ppv);
322 }
323
324 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
325 {
326 return 2;
327 }
328
329 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
330 {
331 return 1;
332 }
333
334 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
335 {
336 HRESULT hres;
337
338 CHECK_EXPECT(Switch);
339 ok(pProtocolData != NULL, "pProtocolData == NULL\n");
340
341 pdata = pProtocolData;
342
343 if(binding_test) {
344 SetEvent(event_complete);
345 WaitForSingleObject(event_complete2, INFINITE);
346 return S_OK;
347 }
348
349 if (!state) {
350 if (http_is_first) {
351 CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
352 CHECK_CALLED(ReportProgress_CONNECTING);
353 } else todo_wine {
354 CHECK_NOT_CALLED(ReportProgress_FINDINGRESOURCE);
355 CHECK_NOT_CALLED(ReportProgress_CONNECTING);
356 }
357 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
358 SET_EXPECT(OnResponse);
359 SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
360 if(bindf & BINDF_NEEDFILE)
361 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
362 }
363
364 SET_EXPECT(ReportData);
365 hres = IInternetProtocol_Continue(http_protocol, pProtocolData);
366 ok(hres == S_OK, "Continue failed: %08x\n", hres);
367 CHECK_CALLED(ReportData);
368
369 if (!state) {
370 state = 1;
371 CHECK_CALLED(OnResponse);
372 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
373 if(bindf & BINDF_NEEDFILE)
374 CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
375 }
376
377 SetEvent(event_complete);
378
379 return S_OK;
380 }
381
382 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
383 LPCWSTR szStatusText)
384 {
385 static const WCHAR null_guid[] = {'{','0','0','0','0','0','0','0','0','-','0','0','0','0','-',
386 '0','0','0','0','-','0','0','0','0','-','0','0','0','0','0','0','0','0','0','0','0','0','}',0};
387 static const WCHAR text_plain[] = {'t','e','x','t','/','p','l','a','i','n',0};
388 static const WCHAR post_host[] =
389 {'c','r','o','s','s','o','v','e','r','.','c','o','d','e',
390 'w','e','a','v','e','r','s','.','c','o','m',0};
391 static const WCHAR wszCrossoverIP[] =
392 {'2','0','9','.','4','6','.','2','5','.','1','3','2',0};
393 /* I'm not sure if it's a good idea to hardcode here the IP address... */
394
395 switch(ulStatusCode) {
396 case BINDSTATUS_MIMETYPEAVAILABLE:
397 CHECK_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
398 ok(szStatusText != NULL, "szStatusText == NULL\n");
399 if(szStatusText) {
400 if(tested_protocol == BIND_TEST)
401 ok(szStatusText == expect_wsz, "unexpected szStatusText\n");
402 else if (http_post_test)
403 ok(lstrlenW(text_plain) <= lstrlenW(szStatusText) &&
404 !memcmp(szStatusText, text_plain, lstrlenW(text_plain)*sizeof(WCHAR)),
405 "szStatusText != text/plain\n");
406 else
407 ok(lstrlenW(text_htmlW) <= lstrlenW(szStatusText) &&
408 !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
409 "szStatusText != text/html\n");
410 }
411 break;
412 case BINDSTATUS_DIRECTBIND:
413 CHECK_EXPECT2(ReportProgress_DIRECTBIND);
414 ok(szStatusText == NULL, "szStatusText != NULL\n");
415 break;
416 case BINDSTATUS_RAWMIMETYPE:
417 CHECK_EXPECT2(ReportProgress_RAWMIMETYPE);
418 ok(szStatusText != NULL, "szStatusText == NULL\n");
419 if(szStatusText)
420 ok(lstrlenW(szStatusText) < lstrlenW(text_htmlW) ||
421 !memcmp(szStatusText, text_htmlW, lstrlenW(text_htmlW)*sizeof(WCHAR)),
422 "szStatusText != text/html\n");
423 break;
424 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
425 CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
426 ok(szStatusText != NULL, "szStatusText == NULL\n");
427 if(szStatusText) {
428 if(binding_test)
429 ok(!lstrcmpW(szStatusText, expect_wsz), "unexpected szStatusText\n");
430 else if(tested_protocol == FILE_TEST)
431 ok(!lstrcmpW(szStatusText, file_name), "szStatusText = \"%s\"\n", debugstr_w(szStatusText));
432 else
433 ok(szStatusText != NULL, "szStatusText == NULL\n");
434 }
435 break;
436 case BINDSTATUS_FINDINGRESOURCE:
437 CHECK_EXPECT(ReportProgress_FINDINGRESOURCE);
438 ok(szStatusText != NULL, "szStatusText == NULL\n");
439 if(szStatusText)
440 {
441 if (!http_post_test)
442 ok(!lstrcmpW(szStatusText, hostW),
443 "szStatustext != \"www.winehq.org\"\n");
444 else
445 ok(!lstrcmpW(szStatusText, post_host),
446 "szStatustext != \"crossover.codeweavers.com\"\n");
447 }
448 break;
449 case BINDSTATUS_CONNECTING:
450 CHECK_EXPECT(ReportProgress_CONNECTING);
451 ok(szStatusText != NULL, "szStatusText == NULL\n");
452 if(szStatusText)
453 ok(!lstrcmpW(szStatusText, http_post_test ?
454 wszCrossoverIP : winehq_ipW),
455 "Unexpected szStatusText\n");
456 break;
457 case BINDSTATUS_SENDINGREQUEST:
458 CHECK_EXPECT(ReportProgress_SENDINGREQUEST);
459 if(tested_protocol == FILE_TEST) {
460 ok(szStatusText != NULL, "szStatusText == NULL\n");
461 if(szStatusText)
462 ok(!*szStatusText, "wrong szStatusText\n");
463 }
464 break;
465 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
466 CHECK_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
467 ok(szStatusText != NULL, "szStatusText == NULL\n");
468 if(szStatusText)
469 ok(!lstrcmpW(szStatusText, text_htmlW), "szStatusText != text/html\n");
470 break;
471 case BINDSTATUS_PROTOCOLCLASSID:
472 CHECK_EXPECT(ReportProgress_PROTOCOLCLASSID);
473 ok(szStatusText != NULL, "szStatusText == NULL\n");
474 ok(!lstrcmpW(szStatusText, null_guid), "unexpected szStatusText\n");
475 break;
476 case BINDSTATUS_COOKIE_SENT:
477 CHECK_EXPECT(ReportProgress_COOKIE_SENT);
478 ok(szStatusText == NULL, "szStatusText != NULL\n");
479 break;
480 case BINDSTATUS_REDIRECTING:
481 CHECK_EXPECT(ReportProgress_REDIRECTING);
482 ok(szStatusText == NULL, "szStatusText != NULL\n");
483 break;
484 default:
485 ok(0, "Unexpected status %d\n", ulStatusCode);
486 };
487
488 return S_OK;
489 }
490
491 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
492 ULONG ulProgress, ULONG ulProgressMax)
493 {
494 if(tested_protocol == FILE_TEST) {
495 CHECK_EXPECT2(ReportData);
496
497 ok(ulProgress == ulProgressMax, "ulProgress (%d) != ulProgressMax (%d)\n",
498 ulProgress, ulProgressMax);
499 ok(ulProgressMax == 13, "ulProgressMax=%d, expected 13\n", ulProgressMax);
500 ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION),
501 "grcfBSCF = %08x\n", grfBSCF);
502 }else if(!binding_test && tested_protocol == HTTP_TEST) {
503 if(!(grfBSCF & BSCF_LASTDATANOTIFICATION))
504 CHECK_EXPECT(ReportData);
505 else if (http_post_test)
506 ok(ulProgress == 13, "Read %u bytes instead of 13\n", ulProgress);
507
508 ok(ulProgress, "ulProgress == 0\n");
509
510 if(first_data_notif) {
511 ok(grfBSCF == BSCF_FIRSTDATANOTIFICATION, "grcfBSCF = %08x\n", grfBSCF);
512 first_data_notif = FALSE;
513 } else {
514 ok(grfBSCF == BSCF_INTERMEDIATEDATANOTIFICATION
515 || grfBSCF == (BSCF_LASTDATANOTIFICATION|BSCF_INTERMEDIATEDATANOTIFICATION),
516 "grcfBSCF = %08x\n", grfBSCF);
517 }
518
519 if(!(bindf & BINDF_FROMURLMON) &&
520 !(grfBSCF & BSCF_LASTDATANOTIFICATION)) {
521 if(!state) {
522 state = 1;
523 if(http_is_first) {
524 CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
525 CHECK_CALLED(ReportProgress_CONNECTING);
526 } else todo_wine {
527 CHECK_NOT_CALLED(ReportProgress_FINDINGRESOURCE);
528 CHECK_NOT_CALLED(ReportProgress_CONNECTING);
529 }
530 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
531 CHECK_CALLED(OnResponse);
532 CHECK_CALLED(ReportProgress_RAWMIMETYPE);
533 }
534 SetEvent(event_complete);
535 }
536 }else {
537 BYTE buf[1000];
538 ULONG read;
539 HRESULT hres;
540
541 CHECK_EXPECT(ReportData);
542
543 if(tested_protocol == BIND_TEST)
544 return S_OK;
545
546 do {
547 SET_EXPECT(Read);
548 hres = IInternetProtocol_Read(binding_protocol, expect_pv=buf, sizeof(buf), &read);
549 CHECK_CALLED(Read);
550 }while(hres == S_OK);
551 }
552
553 return S_OK;
554 }
555
556 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
557 DWORD dwError, LPCWSTR szResult)
558 {
559 CHECK_EXPECT(ReportResult);
560
561 ok(hrResult == expect_hrResult, "hrResult = %08x, expected: %08x\n",
562 hrResult, expect_hrResult);
563 if(SUCCEEDED(hrResult))
564 ok(dwError == ERROR_SUCCESS, "dwError = %d, expected ERROR_SUCCESS\n", dwError);
565 else
566 ok(dwError != ERROR_SUCCESS, "dwError == ERROR_SUCCESS\n");
567 ok(!szResult, "szResult != NULL\n");
568
569 return S_OK;
570 }
571
572 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
573 ProtocolSink_QueryInterface,
574 ProtocolSink_AddRef,
575 ProtocolSink_Release,
576 ProtocolSink_Switch,
577 ProtocolSink_ReportProgress,
578 ProtocolSink_ReportData,
579 ProtocolSink_ReportResult
580 };
581
582 static IInternetProtocolSink protocol_sink = { &protocol_sink_vtbl };
583
584 static HRESULT QueryInterface(REFIID riid, void **ppv)
585 {
586 *ppv = NULL;
587
588 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid))
589 *ppv = &protocol_sink;
590 if(IsEqualGUID(&IID_IServiceProvider, riid))
591 *ppv = &service_provider;
592
593 if(*ppv)
594 return S_OK;
595
596 return E_NOINTERFACE;
597 }
598
599 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
600 {
601 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
602 *ppv = iface;
603 return S_OK;
604 }
605 return E_NOINTERFACE;
606 }
607
608 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
609 {
610 return 2;
611 }
612
613 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
614 {
615 return 1;
616 }
617
618 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
619 {
620 DWORD cbSize;
621
622 static const CHAR szPostData[] = "mode=Test";
623
624 CHECK_EXPECT(GetBindInfo);
625
626 ok(grfBINDF != NULL, "grfBINDF == NULL\n");
627 ok(pbindinfo != NULL, "pbindinfo == NULL\n");
628 ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
629
630 *grfBINDF = bindf;
631 if(binding_test)
632 *grfBINDF |= BINDF_FROMURLMON;
633 cbSize = pbindinfo->cbSize;
634 memset(pbindinfo, 0, cbSize);
635 pbindinfo->cbSize = cbSize;
636
637 if (http_post_test)
638 {
639 /* Must be GMEM_FIXED, GMEM_MOVABLE does not work properly
640 * with urlmon on native (Win98 and WinXP) */
641 U(pbindinfo->stgmedData).hGlobal = GlobalAlloc(GPTR, sizeof(szPostData));
642 if (!U(pbindinfo->stgmedData).hGlobal)
643 {
644 http_post_test = FALSE;
645 skip("Out of memory\n");
646 return E_OUTOFMEMORY;
647 }
648 lstrcpy((LPSTR)U(pbindinfo->stgmedData).hGlobal, szPostData);
649 pbindinfo->cbstgmedData = sizeof(szPostData)-1;
650 pbindinfo->dwBindVerb = BINDVERB_POST;
651 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
652 }
653
654 return S_OK;
655 }
656
657 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType,
658 LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
659 {
660 ok(ppwzStr != NULL, "ppwzStr == NULL\n");
661 ok(pcElFetched != NULL, "pcElFetched == NULL\n");
662
663 switch(ulStringType) {
664 case BINDSTRING_ACCEPT_MIMES:
665 CHECK_EXPECT(GetBindString_ACCEPT_MIMES);
666 ok(cEl == 256, "cEl=%d, expected 256\n", cEl);
667 if(pcElFetched) {
668 ok(*pcElFetched == 256, "*pcElFetched=%d, expected 256\n", *pcElFetched);
669 *pcElFetched = 1;
670 }
671 if(ppwzStr) {
672 *ppwzStr = CoTaskMemAlloc(sizeof(acc_mimeW));
673 memcpy(*ppwzStr, acc_mimeW, sizeof(acc_mimeW));
674 }
675 return S_OK;
676 case BINDSTRING_USER_AGENT:
677 CHECK_EXPECT(GetBindString_USER_AGENT);
678 ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
679 if(pcElFetched) {
680 ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
681 *pcElFetched = 1;
682 }
683 if(ppwzStr) {
684 *ppwzStr = CoTaskMemAlloc(sizeof(user_agentW));
685 memcpy(*ppwzStr, user_agentW, sizeof(user_agentW));
686 }
687 return S_OK;
688 case BINDSTRING_POST_COOKIE:
689 CHECK_EXPECT(GetBindString_POST_COOKIE);
690 ok(cEl == 1, "cEl=%d, expected 1\n", cEl);
691 if(pcElFetched)
692 ok(*pcElFetched == 0, "*pcElFetch=%d, expectd 0\n", *pcElFetched);
693 return S_OK;
694 default:
695 ok(0, "unexpected call\n");
696 }
697
698 return E_NOTIMPL;
699 }
700
701 static IInternetBindInfoVtbl bind_info_vtbl = {
702 BindInfo_QueryInterface,
703 BindInfo_AddRef,
704 BindInfo_Release,
705 BindInfo_GetBindInfo,
706 BindInfo_GetBindString
707 };
708
709 static IInternetBindInfo bind_info = { &bind_info_vtbl };
710
711 static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
712 REFIID riid, void **ppv)
713 {
714 ok(0, "unexpected call\n");
715 return E_NOINTERFACE;
716 }
717
718 static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
719 {
720 return 2;
721 }
722
723 static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
724 {
725 return 1;
726 }
727
728 static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
729 {
730 CHECK_EXPECT(SetPriority);
731 ok(nPriority == ex_priority, "nPriority=%d\n", nPriority);
732 return S_OK;
733 }
734
735 static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
736 {
737 ok(0, "unexpected call\n");
738 return E_NOTIMPL;
739 }
740
741
742 static const IInternetPriorityVtbl InternetPriorityVtbl = {
743 InternetPriority_QueryInterface,
744 InternetPriority_AddRef,
745 InternetPriority_Release,
746 InternetPriority_SetPriority,
747 InternetPriority_GetPriority
748 };
749
750 static IInternetPriority InternetPriority = { &InternetPriorityVtbl };
751
752 static HRESULT WINAPI Protocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
753 {
754 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocol, riid)) {
755 *ppv = iface;
756 return S_OK;
757 }
758
759 if(IsEqualGUID(&IID_IInternetPriority, riid)) {
760 *ppv = &InternetPriority;
761 return S_OK;
762 }
763
764 ok(0, "unexpected riid %s\n", debugstr_guid(riid));
765 *ppv = NULL;
766 return E_NOINTERFACE;
767 }
768
769 static ULONG WINAPI Protocol_AddRef(IInternetProtocol *iface)
770 {
771 return 2;
772 }
773
774 static ULONG WINAPI Protocol_Release(IInternetProtocol *iface)
775 {
776 return 1;
777 }
778
779 static DWORD WINAPI thread_proc(PVOID arg)
780 {
781 HRESULT hres;
782
783 memset(&protocoldata, -1, sizeof(protocoldata));
784
785 prot_state = 0;
786
787 SET_EXPECT(ReportProgress_FINDINGRESOURCE);
788 hres = IInternetProtocolSink_ReportProgress(binding_sink,
789 BINDSTATUS_FINDINGRESOURCE, hostW);
790 CHECK_CALLED(ReportProgress_FINDINGRESOURCE);
791 ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
792
793 SET_EXPECT(ReportProgress_CONNECTING);
794 hres = IInternetProtocolSink_ReportProgress(binding_sink,
795 BINDSTATUS_CONNECTING, winehq_ipW);
796 CHECK_CALLED(ReportProgress_CONNECTING);
797 ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
798
799 SET_EXPECT(ReportProgress_SENDINGREQUEST);
800 hres = IInternetProtocolSink_ReportProgress(binding_sink,
801 BINDSTATUS_SENDINGREQUEST, NULL);
802 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
803 ok(hres == S_OK, "ReportProgress failed: %08x\n", hres);
804
805 prot_state = 1;
806 SET_EXPECT(Switch);
807 hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
808 CHECK_CALLED(Switch);
809 ok(hres == S_OK, "Switch failed: %08x\n", hres);
810
811 prot_state = 2;
812 SET_EXPECT(Switch);
813 hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
814 CHECK_CALLED(Switch);
815 ok(hres == S_OK, "Switch failed: %08x\n", hres);
816
817 prot_state = 2;
818 SET_EXPECT(Switch);
819 hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
820 CHECK_CALLED(Switch);
821 ok(hres == S_OK, "Switch failed: %08x\n", hres);
822
823 prot_state = 3;
824 SET_EXPECT(Switch);
825 hres = IInternetProtocolSink_Switch(binding_sink, &protocoldata);
826 CHECK_CALLED(Switch);
827 ok(hres == S_OK, "Switch failed: %08x\n", hres);
828
829 SetEvent(event_complete);
830
831 return 0;
832 }
833
834 static HRESULT WINAPI Protocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
835 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
836 DWORD grfPI, DWORD dwReserved)
837 {
838 BINDINFO bindinfo, exp_bindinfo;
839 DWORD cbindf = 0;
840 HRESULT hres;
841
842 CHECK_EXPECT(Start);
843
844 ok(pOIProtSink != NULL, "pOIProtSink == NULL\n");
845 ok(pOIBindInfo != NULL, "pOIBindInfo == NULL\n");
846 ok(pOIProtSink != &protocol_sink, "unexpected pOIProtSink\n");
847 ok(pOIBindInfo != &bind_info, "unexpected pOIBindInfo\n");
848 ok(!grfPI, "grfPI = %x\n", grfPI);
849 ok(!dwReserved, "dwReserved = %d\n", dwReserved);
850
851 memset(&bindinfo, 0, sizeof(bindinfo));
852 bindinfo.cbSize = sizeof(bindinfo);
853 memcpy(&exp_bindinfo, &bindinfo, sizeof(bindinfo));
854 SET_EXPECT(GetBindInfo);
855 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &cbindf, &bindinfo);
856 ok(hres == S_OK, "GetBindInfo failed: %08x\n", hres);
857 CHECK_CALLED(GetBindInfo);
858 ok(cbindf == (bindf|BINDF_FROMURLMON), "bindf = %x, expected %x\n",
859 cbindf, (bindf|BINDF_FROMURLMON));
860 ok(!memcmp(&exp_bindinfo, &bindinfo, sizeof(bindinfo)), "unexpected bindinfo\n");
861 ReleaseBindInfo(&bindinfo);
862
863 SET_EXPECT(ReportProgress_SENDINGREQUEST);
864 hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, emptyW);
865 ok(hres == S_OK, "ReportProgress(BINDSTATUS_SENDINGREQUEST) failed: %08x\n", hres);
866 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
867
868 if(tested_protocol == HTTP_TEST) {
869 IServiceProvider *service_provider;
870 IHttpNegotiate *http_negotiate;
871 IHttpNegotiate2 *http_negotiate2;
872 LPWSTR ua = (LPWSTR)0xdeadbeef, accept_mimes[256];
873 LPWSTR additional_headers = NULL;
874 BYTE sec_id[100];
875 DWORD fetched = 0, size = 100;
876
877 SET_EXPECT(GetBindString_USER_AGENT);
878 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT,
879 &ua, 1, &fetched);
880 CHECK_CALLED(GetBindString_USER_AGENT);
881 ok(hres == S_OK, "GetBindString(BINDSTRING_USER_AGETNT) failed: %08x\n", hres);
882 ok(fetched == 1, "fetched = %d, expected 254\n", fetched);
883 ok(ua != NULL, "ua = %p\n", ua);
884 ok(!lstrcmpW(ua, user_agentW), "unexpected user agent %s\n", debugstr_w(ua));
885 CoTaskMemFree(ua);
886
887 fetched = 256;
888 SET_EXPECT(GetBindString_ACCEPT_MIMES);
889 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
890 accept_mimes, 256, &fetched);
891 CHECK_CALLED(GetBindString_ACCEPT_MIMES);
892
893 ok(hres == S_OK,
894 "GetBindString(BINDSTRING_ACCEPT_MIMES) failed: %08x\n", hres);
895 ok(fetched == 1, "fetched = %d, expected 1\n", fetched);
896 ok(!lstrcmpW(acc_mimeW, accept_mimes[0]), "unexpected mimes %s\n", debugstr_w(accept_mimes[0]));
897
898 hres = IInternetBindInfo_QueryInterface(pOIBindInfo, &IID_IServiceProvider,
899 (void**)&service_provider);
900 ok(hres == S_OK, "QueryInterface failed: %08x\n", hres);
901
902 SET_EXPECT(QueryService_HttpNegotiate);
903 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
904 &IID_IHttpNegotiate, (void**)&http_negotiate);
905 CHECK_CALLED(QueryService_HttpNegotiate);
906 ok(hres == S_OK, "QueryService failed: %08x\n", hres);
907
908 SET_EXPECT(BeginningTransaction);
909 hres = IHttpNegotiate_BeginningTransaction(http_negotiate, binding_urls[tested_protocol],
910 NULL, 0, &additional_headers);
911 CHECK_CALLED(BeginningTransaction);
912 IHttpNegotiate_Release(http_negotiate);
913 ok(hres == S_OK, "BeginningTransction failed: %08x\n", hres);
914 ok(additional_headers == NULL, "additional_headers=%p\n", additional_headers);
915
916 SET_EXPECT(QueryService_HttpNegotiate);
917 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
918 &IID_IHttpNegotiate2, (void**)&http_negotiate2);
919 CHECK_CALLED(QueryService_HttpNegotiate);
920 ok(hres == S_OK, "QueryService failed: %08x\n", hres);
921
922 size = 512;
923 SET_EXPECT(GetRootSecurityId);
924 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, sec_id, &size, 0);
925 CHECK_CALLED(GetRootSecurityId);
926 IHttpNegotiate2_Release(http_negotiate2);
927 ok(hres == E_FAIL, "GetRootSecurityId failed: %08x, expected E_FAIL\n", hres);
928 ok(size == 13, "size=%d\n", size);
929
930 IServiceProvider_Release(service_provider);
931
932 CreateThread(NULL, 0, thread_proc, NULL, 0, NULL);
933
934 return S_OK;
935 }
936
937 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
938 hres = IInternetProtocolSink_ReportProgress(pOIProtSink,
939 BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
940 ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
941 CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
942
943 SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
944 hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
945 expect_wsz = text_htmlW);
946 ok(hres == S_OK,
947 "ReportProgress(BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE) failed: %08x\n", hres);
948 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
949
950 SET_EXPECT(ReportData);
951 hres = IInternetProtocolSink_ReportData(pOIProtSink,
952 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION, 13, 13);
953 ok(hres == S_OK, "ReportData failed: %08x\n", hres);
954 CHECK_CALLED(ReportData);
955
956 if(tested_protocol == BIND_TEST) {
957 hres = IInternetProtocol_Terminate(binding_protocol, 0);
958 ok(hres == E_FAIL, "Termiante failed: %08x\n", hres);
959 }
960
961 SET_EXPECT(ReportResult);
962 hres = IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
963 ok(hres == S_OK, "ReportResult failed: %08x\n", hres);
964 CHECK_CALLED(ReportResult);
965
966 return S_OK;
967 }
968
969 static HRESULT WINAPI Protocol_Continue(IInternetProtocol *iface,
970 PROTOCOLDATA *pProtocolData)
971 {
972 DWORD bscf = 0;
973 HRESULT hres;
974
975 CHECK_EXPECT(Continue);
976
977 ok(pProtocolData != NULL, "pProtocolData == NULL\n");
978 if(!pProtocolData || tested_protocol == BIND_TEST)
979 return S_OK;
980
981 switch(prot_state) {
982 case 1: {
983 IServiceProvider *service_provider;
984 IHttpNegotiate *http_negotiate;
985 static WCHAR header[] = {'?',0};
986
987 hres = IInternetProtocolSink_QueryInterface(binding_sink, &IID_IServiceProvider,
988 (void**)&service_provider);
989 ok(hres == S_OK, "Could not get IServiceProvicder\n");
990
991 SET_EXPECT(QueryService_HttpNegotiate);
992 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
993 &IID_IHttpNegotiate, (void**)&http_negotiate);
994 IServiceProvider_Release(service_provider);
995 CHECK_CALLED(QueryService_HttpNegotiate);
996 ok(hres == S_OK, "Could not get IHttpNegotiate\n");
997
998 SET_EXPECT(OnResponse);
999 hres = IHttpNegotiate_OnResponse(http_negotiate, 200, header, NULL, NULL);
1000 IHttpNegotiate_Release(http_negotiate);
1001 CHECK_CALLED(OnResponse);
1002 IHttpNegotiate_Release(http_negotiate);
1003 ok(hres == S_OK, "OnResponse failed: %08x\n", hres);
1004
1005 SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1006 hres = IInternetProtocolSink_ReportProgress(binding_sink,
1007 BINDSTATUS_MIMETYPEAVAILABLE, text_htmlW);
1008 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1009 ok(hres == S_OK,
1010 "ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE) failed: %08x\n", hres);
1011
1012 bscf |= BSCF_FIRSTDATANOTIFICATION;
1013 break;
1014 }
1015 case 2:
1016 case 3:
1017 bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
1018 break;
1019 }
1020
1021 SET_EXPECT(ReportData);
1022 hres = IInternetProtocolSink_ReportData(binding_sink, bscf, 100, 400);
1023 CHECK_CALLED(ReportData);
1024 ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1025
1026 if(prot_state == 3)
1027 prot_state = 4;
1028
1029 return S_OK;
1030 }
1031
1032 static HRESULT WINAPI Protocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
1033 DWORD dwOptions)
1034 {
1035 ok(0, "unexpected call\n");
1036 return E_NOTIMPL;
1037 }
1038
1039 static HRESULT WINAPI Protocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
1040 {
1041 CHECK_EXPECT(Terminate);
1042 ok(!dwOptions, "dwOptions=%d\n", dwOptions);
1043 return S_OK;
1044 }
1045
1046 static HRESULT WINAPI Protocol_Suspend(IInternetProtocol *iface)
1047 {
1048 ok(0, "unexpected call\n");
1049 return E_NOTIMPL;
1050 }
1051
1052 static HRESULT WINAPI Protocol_Resume(IInternetProtocol *iface)
1053 {
1054 ok(0, "unexpected call\n");
1055 return E_NOTIMPL;
1056 }
1057
1058 static HRESULT WINAPI Protocol_Read(IInternetProtocol *iface, void *pv,
1059 ULONG cb, ULONG *pcbRead)
1060 {
1061 static BOOL b = TRUE;
1062
1063 CHECK_EXPECT(Read);
1064
1065 ok(pv == expect_pv, "pv != expect_pv\n");
1066 ok(cb == 1000, "cb=%d\n", cb);
1067 ok(pcbRead != NULL, "pcbRead == NULL\n");
1068 ok(!*pcbRead, "*pcbRead = %d\n", *pcbRead);
1069
1070 if(prot_state == 3) {
1071 HRESULT hres;
1072
1073 SET_EXPECT(ReportResult);
1074 hres = IInternetProtocolSink_ReportResult(binding_sink, S_OK, ERROR_SUCCESS, NULL);
1075 CHECK_CALLED(ReportResult);
1076
1077 return S_FALSE;
1078 }
1079
1080 if((b = !b))
1081 return tested_protocol == HTTP_TEST ? E_PENDING : S_FALSE;
1082
1083 memset(pv, 'x', 100);
1084 prot_read += *pcbRead = 100;
1085 return S_OK;
1086 }
1087
1088 static HRESULT WINAPI Protocol_Seek(IInternetProtocol *iface,
1089 LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
1090 {
1091 ok(0, "unexpected call\n");
1092 return E_NOTIMPL;
1093 }
1094
1095 static HRESULT WINAPI Protocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
1096 {
1097 CHECK_EXPECT(LockRequest);
1098 ok(dwOptions == 0, "dwOptions=%x\n", dwOptions);
1099 return S_OK;
1100 }
1101
1102 static HRESULT WINAPI Protocol_UnlockRequest(IInternetProtocol *iface)
1103 {
1104 CHECK_EXPECT(UnlockRequest);
1105 return S_OK;
1106 }
1107
1108 static const IInternetProtocolVtbl ProtocolVtbl = {
1109 Protocol_QueryInterface,
1110 Protocol_AddRef,
1111 Protocol_Release,
1112 Protocol_Start,
1113 Protocol_Continue,
1114 Protocol_Abort,
1115 Protocol_Terminate,
1116 Protocol_Suspend,
1117 Protocol_Resume,
1118 Protocol_Read,
1119 Protocol_Seek,
1120 Protocol_LockRequest,
1121 Protocol_UnlockRequest
1122 };
1123
1124 static IInternetProtocol Protocol = { &ProtocolVtbl };
1125
1126 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
1127 {
1128 ok(0, "unexpected call\n");
1129 return E_NOINTERFACE;
1130 }
1131
1132 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
1133 {
1134 return 2;
1135 }
1136
1137 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
1138 {
1139 return 1;
1140 }
1141
1142 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
1143 REFIID riid, void **ppv)
1144 {
1145 CHECK_EXPECT(CreateInstance);
1146
1147 ok(pOuter == (IUnknown*)prot_bind_info, "pOuter != protocol_unk\n");
1148 ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", debugstr_guid(riid));
1149 ok(ppv != NULL, "ppv == NULL\n");
1150
1151 *ppv = &Protocol;
1152 return S_OK;
1153 }
1154
1155 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
1156 {
1157 ok(0, "unexpected call\n");
1158 return S_OK;
1159 }
1160
1161 static const IClassFactoryVtbl ClassFactoryVtbl = {
1162 ClassFactory_QueryInterface,
1163 ClassFactory_AddRef,
1164 ClassFactory_Release,
1165 ClassFactory_CreateInstance,
1166 ClassFactory_LockServer
1167 };
1168
1169 static IClassFactory ClassFactory = { &ClassFactoryVtbl };
1170
1171 static void test_priority(IInternetProtocol *protocol)
1172 {
1173 IInternetPriority *priority;
1174 LONG pr;
1175 HRESULT hres;
1176
1177 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority,
1178 (void**)&priority);
1179 ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
1180 if(FAILED(hres))
1181 return;
1182
1183 hres = IInternetPriority_GetPriority(priority, &pr);
1184 ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1185 ok(pr == 0, "pr=%d, expected 0\n", pr);
1186
1187 hres = IInternetPriority_SetPriority(priority, 1);
1188 ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
1189
1190 hres = IInternetPriority_GetPriority(priority, &pr);
1191 ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1192 ok(pr == 1, "pr=%d, expected 1\n", pr);
1193
1194 IInternetPriority_Release(priority);
1195 }
1196
1197 static void file_protocol_start(IInternetProtocol *protocol, LPCWSTR url, BOOL is_first)
1198 {
1199 HRESULT hres;
1200
1201 SET_EXPECT(GetBindInfo);
1202 if(!(bindf & BINDF_FROMURLMON))
1203 SET_EXPECT(ReportProgress_DIRECTBIND);
1204 if(is_first) {
1205 SET_EXPECT(ReportProgress_SENDINGREQUEST);
1206 SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
1207 if(bindf & BINDF_FROMURLMON)
1208 SET_EXPECT(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1209 else
1210 SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1211 }
1212 SET_EXPECT(ReportData);
1213 if(is_first)
1214 SET_EXPECT(ReportResult);
1215
1216 expect_hrResult = S_OK;
1217
1218 hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
1219 ok(hres == S_OK, "Start failed: %08x\n", hres);
1220
1221 CHECK_CALLED(GetBindInfo);
1222 if(!(bindf & BINDF_FROMURLMON))
1223 CHECK_CALLED(ReportProgress_DIRECTBIND);
1224 if(is_first) {
1225 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1226 CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
1227 if(bindf & BINDF_FROMURLMON)
1228 CHECK_CALLED(ReportProgress_VERIFIEDMIMETYPEAVAILABLE);
1229 else
1230 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1231 }
1232 CHECK_CALLED(ReportData);
1233 if(is_first)
1234 CHECK_CALLED(ReportResult);
1235 }
1236
1237 static void test_file_protocol_url(LPCWSTR url)
1238 {
1239 IInternetProtocolInfo *protocol_info;
1240 IUnknown *unk;
1241 IClassFactory *factory;
1242 HRESULT hres;
1243
1244 hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
1245 &IID_IUnknown, (void**)&unk);
1246 ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1247 if(!SUCCEEDED(hres))
1248 return;
1249
1250 hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1251 ok(hres == E_NOINTERFACE,
1252 "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n", hres);
1253
1254 hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1255 ok(hres == S_OK, "Could not get IClassFactory interface\n");
1256 if(SUCCEEDED(hres)) {
1257 IInternetProtocol *protocol;
1258 BYTE buf[512];
1259 ULONG cb;
1260 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1261 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1262
1263 if(SUCCEEDED(hres)) {
1264 file_protocol_start(protocol, url, TRUE);
1265 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1266 ok(hres == S_OK, "Read failed: %08x\n", hres);
1267 ok(cb == 2, "cb=%u expected 2\n", cb);
1268 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1269 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1270 hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
1271 ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
1272 ok(cb == 0, "cb=%u expected 0\n", cb);
1273 hres = IInternetProtocol_UnlockRequest(protocol);
1274 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1275
1276 file_protocol_start(protocol, url, FALSE);
1277 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1278 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1279 hres = IInternetProtocol_LockRequest(protocol, 0);
1280 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1281 hres = IInternetProtocol_UnlockRequest(protocol);
1282 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1283
1284 IInternetProtocol_Release(protocol);
1285 }
1286
1287 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1288 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1289
1290 if(SUCCEEDED(hres)) {
1291 file_protocol_start(protocol, url, TRUE);
1292 hres = IInternetProtocol_LockRequest(protocol, 0);
1293 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1294 hres = IInternetProtocol_Terminate(protocol, 0);
1295 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1296 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1297 ok(hres == S_OK, "Read failed: %08x\n\n", hres);
1298 hres = IInternetProtocol_UnlockRequest(protocol);
1299 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1300 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1301 ok(hres == S_OK, "Read failed: %08x\n", hres);
1302 hres = IInternetProtocol_Terminate(protocol, 0);
1303 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1304
1305 IInternetProtocol_Release(protocol);
1306 }
1307
1308 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
1309 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1310
1311 if(SUCCEEDED(hres)) {
1312 file_protocol_start(protocol, url, TRUE);
1313 hres = IInternetProtocol_Terminate(protocol, 0);
1314 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1315 hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
1316 ok(hres == S_OK, "Read failed: %08x\n", hres);
1317 ok(cb == 2, "cb=%u expected 2\n", cb);
1318
1319 IInternetProtocol_Release(protocol);
1320 }
1321
1322 IClassFactory_Release(factory);
1323 }
1324
1325 IUnknown_Release(unk);
1326 }
1327
1328 static void test_file_protocol_fail(void)
1329 {
1330 IInternetProtocol *protocol;
1331 HRESULT hres;
1332
1333 static const WCHAR index_url2[] =
1334 {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
1335
1336 hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1337 &IID_IInternetProtocol, (void**)&protocol);
1338 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
1339 if(FAILED(hres))
1340 return;
1341
1342 SET_EXPECT(GetBindInfo);
1343 expect_hrResult = MK_E_SYNTAX;
1344 hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
1345 ok(hres == MK_E_SYNTAX ||
1346 hres == E_INVALIDARG,
1347 "Start failed: %08x, expected MK_E_SYNTAX or E_INVALIDARG\n", hres);
1348 CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
1349
1350 SET_EXPECT(GetBindInfo);
1351 if(!(bindf & BINDF_FROMURLMON))
1352 SET_EXPECT(ReportProgress_DIRECTBIND);
1353 SET_EXPECT(ReportProgress_SENDINGREQUEST);
1354 SET_EXPECT(ReportResult);
1355 expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
1356 hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
1357 ok(hres == INET_E_RESOURCE_NOT_FOUND,
1358 "Start failed: %08x expected INET_E_RESOURCE_NOT_FOUND\n", hres);
1359 CHECK_CALLED(GetBindInfo);
1360 if(!(bindf & BINDF_FROMURLMON))
1361 CHECK_CALLED(ReportProgress_DIRECTBIND);
1362 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1363 CHECK_CALLED(ReportResult);
1364
1365 IInternetProtocol_Release(protocol);
1366
1367 hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1368 &IID_IInternetProtocol, (void**)&protocol);
1369 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
1370 if(FAILED(hres))
1371 return;
1372
1373 SET_EXPECT(GetBindInfo);
1374 if(!(bindf & BINDF_FROMURLMON))
1375 SET_EXPECT(ReportProgress_DIRECTBIND);
1376 SET_EXPECT(ReportProgress_SENDINGREQUEST);
1377 SET_EXPECT(ReportResult);
1378 expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
1379
1380 hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
1381 ok(hres == INET_E_RESOURCE_NOT_FOUND,
1382 "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
1383 CHECK_CALLED(GetBindInfo);
1384 if(!(bindf & BINDF_FROMURLMON))
1385 CHECK_CALLED(ReportProgress_DIRECTBIND);
1386 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1387 CHECK_CALLED(ReportResult);
1388
1389 SET_EXPECT(GetBindInfo);
1390 hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
1391 ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1392 CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
1393
1394 SET_EXPECT(GetBindInfo);
1395 hres = IInternetProtocol_Start(protocol, emptyW, &protocol_sink, &bind_info, 0, 0);
1396 ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1397 CLEAR_CALLED(GetBindInfo); /* GetBindInfo not called in IE7 */
1398
1399 IInternetProtocol_Release(protocol);
1400 }
1401
1402 static void test_file_protocol(void) {
1403 WCHAR buf[INTERNET_MAX_URL_LENGTH], file_name_buf[MAX_PATH];
1404 DWORD size;
1405 ULONG len;
1406 HANDLE file;
1407
1408 static const WCHAR wszFile[] = {'f','i','l','e',':',0};
1409 static const WCHAR wszFile2[] = {'f','i','l','e',':','/','/',0};
1410 static const WCHAR wszFile3[] = {'f','i','l','e',':','/','/','/',0};
1411 static const char html_doc[] = "<HTML></HTML>";
1412
1413 trace("Testing file protocol...\n");
1414 tested_protocol = FILE_TEST;
1415
1416 file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1417 FILE_ATTRIBUTE_NORMAL, NULL);
1418 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1419 if(file == INVALID_HANDLE_VALUE)
1420 return;
1421 WriteFile(file, html_doc, sizeof(html_doc)-1, &size, NULL);
1422 CloseHandle(file);
1423
1424 file_name = wszIndexHtml;
1425 bindf = 0;
1426 test_file_protocol_url(index_url);
1427 bindf = BINDF_FROMURLMON;
1428 test_file_protocol_url(index_url);
1429 bindf = BINDF_FROMURLMON | BINDF_NEEDFILE;
1430 test_file_protocol_url(index_url);
1431
1432 memcpy(buf, wszFile, sizeof(wszFile));
1433 len = sizeof(wszFile)/sizeof(WCHAR)-1;
1434 len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
1435 buf[len++] = '\\';
1436 memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
1437
1438 file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
1439 bindf = 0;
1440 test_file_protocol_url(buf);
1441 bindf = BINDF_FROMURLMON;
1442 test_file_protocol_url(buf);
1443
1444 memcpy(buf, wszFile2, sizeof(wszFile2));
1445 len = GetCurrentDirectoryW(sizeof(file_name_buf)/sizeof(WCHAR), file_name_buf);
1446 file_name_buf[len++] = '\\';
1447 memcpy(file_name_buf+len, wszIndexHtml, sizeof(wszIndexHtml));
1448 lstrcpyW(buf+sizeof(wszFile2)/sizeof(WCHAR)-1, file_name_buf);
1449 file_name = file_name_buf;
1450 bindf = 0;
1451 test_file_protocol_url(buf);
1452 bindf = BINDF_FROMURLMON;
1453 test_file_protocol_url(buf);
1454
1455 buf[sizeof(wszFile2)/sizeof(WCHAR)] = '|';
1456 test_file_protocol_url(buf);
1457
1458 memcpy(buf, wszFile3, sizeof(wszFile3));
1459 len = sizeof(wszFile3)/sizeof(WCHAR)-1;
1460 len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
1461 buf[len++] = '\\';
1462 memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
1463
1464 file_name = buf + sizeof(wszFile3)/sizeof(WCHAR)-1;
1465 bindf = 0;
1466 test_file_protocol_url(buf);
1467 bindf = BINDF_FROMURLMON;
1468 test_file_protocol_url(buf);
1469
1470 DeleteFileW(wszIndexHtml);
1471
1472 bindf = 0;
1473 test_file_protocol_fail();
1474 bindf = BINDF_FROMURLMON;
1475 test_file_protocol_fail();
1476 }
1477
1478 static BOOL http_protocol_start(LPCWSTR url, BOOL is_first)
1479 {
1480 static BOOL got_user_agent = FALSE;
1481 HRESULT hres;
1482
1483 first_data_notif = TRUE;
1484 state = 0;
1485
1486 SET_EXPECT(GetBindInfo);
1487 if (!(bindf & BINDF_FROMURLMON))
1488 SET_EXPECT(ReportProgress_DIRECTBIND);
1489 SET_EXPECT(GetBindString_USER_AGENT);
1490 SET_EXPECT(GetBindString_ACCEPT_MIMES);
1491 SET_EXPECT(QueryService_HttpNegotiate);
1492 SET_EXPECT(BeginningTransaction);
1493 SET_EXPECT(GetRootSecurityId);
1494 if (http_post_test)
1495 SET_EXPECT(GetBindString_POST_COOKIE);
1496
1497 hres = IInternetProtocol_Start(http_protocol, url, &protocol_sink, &bind_info, 0, 0);
1498 ok(hres == S_OK, "Start failed: %08x\n", hres);
1499 if(FAILED(hres))
1500 return FALSE;
1501
1502 CHECK_CALLED(GetBindInfo);
1503 if (!(bindf & BINDF_FROMURLMON))
1504 CHECK_CALLED(ReportProgress_DIRECTBIND);
1505 if (!got_user_agent)
1506 {
1507 CHECK_CALLED(GetBindString_USER_AGENT);
1508 got_user_agent = TRUE;
1509 }
1510 else todo_wine
1511 {
1512 /* user agent only retrieved once, even with different URLs */
1513 CHECK_NOT_CALLED(GetBindString_USER_AGENT);
1514 }
1515 CHECK_CALLED(GetBindString_ACCEPT_MIMES);
1516 CHECK_CALLED(QueryService_HttpNegotiate);
1517 CHECK_CALLED(BeginningTransaction);
1518 /* GetRootSecurityId called on WinXP but not on Win98 */
1519 CLEAR_CALLED(GetRootSecurityId);
1520 if (http_post_test)
1521 CHECK_CALLED(GetBindString_POST_COOKIE);
1522
1523 return TRUE;
1524 }
1525
1526 /* is_first refers to whether this is the first call to this function
1527 * _for this url_ */
1528 static void test_http_protocol_url(LPCWSTR url, BOOL is_first)
1529 {
1530 IInternetProtocolInfo *protocol_info;
1531 IClassFactory *factory;
1532 IUnknown *unk;
1533 HRESULT hres;
1534
1535 http_url = url;
1536 http_is_first = is_first;
1537
1538 hres = CoGetClassObject(&CLSID_HttpProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1539 ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1540 if(!SUCCEEDED(hres))
1541 return;
1542
1543 hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1544 ok(hres == E_NOINTERFACE,
1545 "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
1546 hres);
1547
1548 hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1549 ok(hres == S_OK, "Could not get IClassFactory interface\n");
1550 IUnknown_Release(unk);
1551 if(FAILED(hres))
1552 return;
1553
1554 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1555 (void**)&http_protocol);
1556 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1557 if(SUCCEEDED(hres)) {
1558 BYTE buf[3600];
1559 DWORD cb;
1560 int *called = (bindf & BINDF_FROMURLMON) ? &called_Switch : &called_ReportData;
1561
1562 test_priority(http_protocol);
1563
1564 SET_EXPECT(ReportProgress_FINDINGRESOURCE);
1565 SET_EXPECT(ReportProgress_CONNECTING);
1566 SET_EXPECT(ReportProgress_SENDINGREQUEST);
1567 if(!(bindf & BINDF_FROMURLMON)) {
1568 SET_EXPECT(OnResponse);
1569 SET_EXPECT(ReportProgress_RAWMIMETYPE);
1570 SET_EXPECT(ReportData);
1571 } else {
1572 SET_EXPECT(Switch);
1573 }
1574
1575 if(!http_protocol_start(url, is_first))
1576 return;
1577
1578 SET_EXPECT(ReportResult);
1579 expect_hrResult = S_OK;
1580
1581 hres = IInternetProtocol_Read(http_protocol, buf, 1, &cb);
1582 ok((!*called && hres == E_PENDING && cb==0) ||
1583 (*called && hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1584
1585 WaitForSingleObject(event_complete, INFINITE);
1586 if(bindf & BINDF_FROMURLMON)
1587 CHECK_CALLED(Switch);
1588 else
1589 CHECK_CALLED(ReportData);
1590
1591 while(1) {
1592 if(bindf & BINDF_FROMURLMON)
1593 SET_EXPECT(Switch);
1594 else
1595 SET_EXPECT(ReportData);
1596 hres = IInternetProtocol_Read(http_protocol, buf, sizeof(buf), &cb);
1597 if(hres == E_PENDING) {
1598 hres = IInternetProtocol_Read(http_protocol, buf, 1, &cb);
1599 ok((!*called && hres == E_PENDING && cb==0) ||
1600 (*called && hres == S_OK && cb==1), "Read failed: %08x (%d bytes)\n", hres, cb);
1601 WaitForSingleObject(event_complete, INFINITE);
1602 if(bindf & BINDF_FROMURLMON)
1603 CHECK_CALLED(Switch);
1604 else
1605 CHECK_CALLED(ReportData);
1606 } else {
1607 if(bindf & BINDF_FROMURLMON)
1608 CHECK_NOT_CALLED(Switch);
1609 else
1610 CHECK_NOT_CALLED(ReportData);
1611 if(cb == 0) break;
1612 }
1613 }
1614 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1615 CHECK_CALLED(ReportResult);
1616
1617 hres = IInternetProtocol_LockRequest(http_protocol, 0);
1618 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1619
1620 hres = IInternetProtocol_Read(http_protocol, buf, 1, &cb);
1621 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1622
1623 hres = IInternetProtocol_Terminate(http_protocol, 0);
1624 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1625
1626 /* This wait is to give the internet handles being freed in Terminate
1627 * enough time to actually terminate in all cases. Internet handles
1628 * terminate asynchronously and native reuses the main InternetOpen
1629 * handle. The only case in which this seems to be necessary is on
1630 * wine with native wininet and urlmon, resulting in the next time
1631 * test_http_protocol_url being called the first data notification actually
1632 * being an extra last data notification from the previous connection
1633 * about once out of every ten times. */
1634 Sleep(100);
1635
1636 hres = IInternetProtocol_UnlockRequest(http_protocol);
1637 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1638
1639 IInternetProtocol_Release(http_protocol);
1640 }
1641
1642 IClassFactory_Release(factory);
1643 }
1644
1645 static void test_http_protocol(void)
1646 {
1647 static const WCHAR winehq_url[] =
1648 {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.',
1649 'o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
1650 static const WCHAR posttest_url[] =
1651 {'h','t','t','p',':','/','/','c','r','o','s','s','o','v','e','r','.',
1652 'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m','/',
1653 'p','o','s','t','t','e','s','t','.','p','h','p',0};
1654
1655 trace("Testing http protocol (not from urlmon)...\n");
1656 tested_protocol = HTTP_TEST;
1657 bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
1658 test_http_protocol_url(winehq_url, TRUE);
1659
1660 trace("Testing http protocol (from urlmon)...\n");
1661 bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON;
1662 test_http_protocol_url(winehq_url, FALSE);
1663
1664 trace("Testing http protocol (to file)...\n");
1665 bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE;
1666 test_http_protocol_url(winehq_url, FALSE);
1667
1668 trace("Testing http protocol (post data)...\n");
1669 http_post_test = TRUE;
1670 /* Without this flag we get a ReportProgress_CACHEFILENAMEAVAILABLE
1671 * notification with BINDVERB_POST */
1672 bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NOWRITECACHE;
1673 test_http_protocol_url(posttest_url, TRUE);
1674 http_post_test = FALSE;
1675 }
1676
1677 static void test_mk_protocol(void)
1678 {
1679 IInternetProtocolInfo *protocol_info;
1680 IInternetProtocol *protocol;
1681 IClassFactory *factory;
1682 IUnknown *unk;
1683 HRESULT hres;
1684
1685 static const WCHAR wrong_url1[] = {'t','e','s','t',':','@','M','S','I','T','S','t','o','r','e',
1686 ':',':','/','t','e','s','t','.','h','t','m','l',0};
1687 static const WCHAR wrong_url2[] = {'m','k',':','/','t','e','s','t','.','h','t','m','l',0};
1688
1689 trace("Testing mk protocol...\n");
1690 tested_protocol = MK_TEST;
1691
1692 hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL,
1693 &IID_IUnknown, (void**)&unk);
1694 ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
1695
1696 hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
1697 ok(hres == E_NOINTERFACE,
1698 "Could not get IInternetProtocolInfo interface: %08x, expected E_NOINTERFACE\n",
1699 hres);
1700
1701 hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
1702 ok(hres == S_OK, "Could not get IClassFactory interface\n");
1703 IUnknown_Release(unk);
1704 if(FAILED(hres))
1705 return;
1706
1707 hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol,
1708 (void**)&protocol);
1709 IClassFactory_Release(factory);
1710 ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
1711
1712 SET_EXPECT(GetBindInfo);
1713 hres = IInternetProtocol_Start(protocol, wrong_url1, &protocol_sink, &bind_info, 0, 0);
1714 ok(hres == MK_E_SYNTAX || hres == INET_E_INVALID_URL,
1715 "Start failed: %08x, expected MK_E_SYNTAX or INET_E_INVALID_URL\n", hres);
1716 CLEAR_CALLED(GetBindInfo);
1717
1718 SET_EXPECT(GetBindInfo);
1719 SET_EXPECT(ReportProgress_DIRECTBIND);
1720 SET_EXPECT(ReportProgress_SENDINGREQUEST);
1721 SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
1722 SET_EXPECT(ReportResult);
1723 expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
1724
1725 hres = IInternetProtocol_Start(protocol, wrong_url2, &protocol_sink, &bind_info, 0, 0);
1726 ok(hres == INET_E_RESOURCE_NOT_FOUND ||
1727 hres == INET_E_INVALID_URL, /* win2k3 */
1728 "Start failed: %08x, expected INET_E_RESOURCE_NOT_FOUND or INET_E_INVALID_URL\n", hres);
1729
1730 if (hres == INET_E_RESOURCE_NOT_FOUND) {
1731 CHECK_CALLED(GetBindInfo);
1732 CLEAR_CALLED(ReportProgress_DIRECTBIND);
1733 CHECK_CALLED(ReportProgress_SENDINGREQUEST);
1734 CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1735 CHECK_CALLED(ReportResult);
1736 }else {
1737 CLEAR_CALLED(GetBindInfo);
1738 CLEAR_CALLED(ReportProgress_DIRECTBIND);
1739 CLEAR_CALLED(ReportProgress_SENDINGREQUEST);
1740 CLEAR_CALLED(ReportProgress_MIMETYPEAVAILABLE);
1741 CLEAR_CALLED(ReportResult);
1742 }
1743
1744 IInternetProtocol_Release(protocol);
1745 }
1746
1747 static void test_CreateBinding(void)
1748 {
1749 IInternetProtocolSink *sink;
1750 IInternetProtocol *protocol;
1751 IInternetPriority *priority;
1752 IInternetSession *session;
1753 LONG p;
1754 BYTE buf[1000];
1755 DWORD read;
1756 HRESULT hres;
1757
1758 static const WCHAR test_url[] =
1759 {'t','e','s','t',':','/','/','f','i','l','e','.','h','t','m','l',0};
1760 static const WCHAR wsz_test[] = {'t','e','s','t',0};
1761
1762 trace("Testing CreateBinding...\n");
1763 tested_protocol = BIND_TEST;
1764 binding_test = TRUE;
1765
1766 hres = CoInternetGetSession(0, &session, 0);
1767 ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
1768
1769 hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, wsz_test, 0, NULL, 0);
1770 ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
1771
1772 hres = IInternetSession_CreateBinding(session, NULL, test_url, NULL, NULL, &protocol, 0);
1773 binding_protocol = protocol;
1774 ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
1775 ok(protocol != NULL, "protocol == NULL\n");
1776
1777 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
1778 ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
1779
1780 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&sink);
1781 ok(hres == S_OK, "Could not get IInternetProtocolSink: %08x\n", hres);
1782
1783 hres = IInternetProtocol_Start(protocol, test_url, NULL, &bind_info, 0, 0);
1784 ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1785 hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, NULL, 0, 0);
1786 ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1787 hres = IInternetProtocol_Start(protocol, NULL, &protocol_sink, &bind_info, 0, 0);
1788 ok(hres == E_INVALIDARG, "Start failed: %08x, expected E_INVALIDARG\n", hres);
1789
1790 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
1791 ok(hres == S_OK, "QueryInterface(IID_IInternetPriority) failed: %08x\n", hres);
1792
1793 p = 0xdeadbeef;
1794 hres = IInternetPriority_GetPriority(priority, &p);
1795 ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1796 ok(!p, "p=%d\n", p);
1797
1798 ex_priority = 100;
1799 hres = IInternetPriority_SetPriority(priority, 100);
1800 ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
1801
1802 p = 0xdeadbeef;
1803 hres = IInternetPriority_GetPriority(priority, &p);
1804 ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1805 ok(p == 100, "p=%d\n", p);
1806
1807 SET_EXPECT(QueryService_InternetProtocol);
1808 SET_EXPECT(CreateInstance);
1809 SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
1810 SET_EXPECT(SetPriority);
1811 SET_EXPECT(Start);
1812
1813 expect_hrResult = S_OK;
1814 hres = IInternetProtocol_Start(protocol, test_url, &protocol_sink, &bind_info, 0, 0);
1815 ok(hres == S_OK, "Start failed: %08x\n", hres);
1816
1817 CHECK_CALLED(QueryService_InternetProtocol);
1818 CHECK_CALLED(CreateInstance);
1819 CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
1820 CHECK_CALLED(SetPriority);
1821 CHECK_CALLED(Start);
1822
1823 SET_EXPECT(Read);
1824 read = 0xdeadbeef;
1825 hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
1826 ok(hres == S_OK, "Read failed: %08x\n", hres);
1827 ok(read == 100, "read = %d\n", read);
1828 CHECK_CALLED(Read);
1829
1830 SET_EXPECT(Read);
1831 read = 0xdeadbeef;
1832 hres = IInternetProtocol_Read(protocol, expect_pv = buf, sizeof(buf), &read);
1833 ok(hres == S_FALSE, "Read failed: %08x\n", hres);
1834 ok(!read, "read = %d\n", read);
1835 CHECK_CALLED(Read);
1836
1837 p = 0xdeadbeef;
1838 hres = IInternetPriority_GetPriority(priority, &p);
1839 ok(hres == S_OK, "GetPriority failed: %08x\n", hres);
1840 ok(p == 100, "p=%d\n", p);
1841
1842 hres = IInternetPriority_SetPriority(priority, 101);
1843 ok(hres == S_OK, "SetPriority failed: %08x\n", hres);
1844
1845 SET_EXPECT(Terminate);
1846 hres = IInternetProtocol_Terminate(protocol, 0xdeadbeef);
1847 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1848 CHECK_CALLED(Terminate);
1849
1850 SET_EXPECT(Continue);
1851 hres = IInternetProtocolSink_Switch(sink, &protocoldata);
1852 ok(hres == S_OK, "Switch failed: %08x\n", hres);
1853 CHECK_CALLED(Continue);
1854
1855 hres = IInternetProtocolSink_ReportProgress(sink,
1856 BINDSTATUS_CACHEFILENAMEAVAILABLE, expect_wsz = emptyW);
1857 ok(hres == S_OK, "ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE) failed: %08x\n", hres);
1858
1859 hres = IInternetProtocolSink_ReportResult(sink, S_OK, ERROR_SUCCESS, NULL);
1860 ok(hres == E_FAIL, "ReportResult failed: %08x, expected E_FAIL\n", hres);
1861
1862 hres = IInternetProtocolSink_ReportData(sink, 0, 0, 0);
1863 ok(hres == S_OK, "ReportData failed: %08x\n", hres);
1864
1865 IInternetProtocolSink_Release(sink);
1866 IInternetPriority_Release(priority);
1867 IInternetBindInfo_Release(prot_bind_info);
1868 IInternetProtocol_Release(protocol);
1869 IInternetSession_Release(session);
1870 }
1871
1872 static void test_binding(int prot)
1873 {
1874 IInternetProtocol *protocol;
1875 IInternetSession *session;
1876 ULONG ref;
1877 HRESULT hres;
1878
1879 trace("Testing %s binding...\n", debugstr_w(protocol_names[prot]));
1880
1881 tested_protocol = prot;
1882 binding_test = TRUE;
1883 first_data_notif = TRUE;
1884 prot_read = 0;
1885
1886 hres = CoInternetGetSession(0, &session, 0);
1887 ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
1888
1889 hres = IInternetSession_RegisterNameSpace(session, &ClassFactory, &IID_NULL, protocol_names[prot], 0, NULL, 0);
1890 ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
1891
1892 hres = IInternetSession_CreateBinding(session, NULL, binding_urls[prot], NULL, NULL, &protocol, 0);
1893 binding_protocol = protocol;
1894 IInternetSession_Release(session);
1895 ok(hres == S_OK, "CreateBinding failed: %08x\n", hres);
1896 ok(protocol != NULL, "protocol == NULL\n");
1897
1898 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetBindInfo, (void**)&prot_bind_info);
1899 ok(hres == S_OK, "QueryInterface(IID_IInternetBindInfo) failed: %08x\n", hres);
1900
1901 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolSink, (void**)&binding_sink);
1902 ok(hres == S_OK, "QueryInterface(IID_IInternetProtocolSink) failed: %08x\n", hres);
1903
1904 ex_priority = 0;
1905 SET_EXPECT(QueryService_InternetProtocol);
1906 SET_EXPECT(CreateInstance);
1907 SET_EXPECT(ReportProgress_PROTOCOLCLASSID);
1908 SET_EXPECT(SetPriority);
1909 SET_EXPECT(Start);
1910
1911 expect_hrResult = S_OK;
1912 hres = IInternetProtocol_Start(protocol, binding_urls[prot], &protocol_sink, &bind_info, 0, 0);
1913 ok(hres == S_OK, "Start failed: %08x\n", hres);
1914
1915 CHECK_CALLED(QueryService_InternetProtocol);
1916 CHECK_CALLED(CreateInstance);
1917 CHECK_CALLED(ReportProgress_PROTOCOLCLASSID);
1918 CHECK_CALLED(SetPriority);
1919 CHECK_CALLED(Start);
1920
1921 if(prot == HTTP_TEST) {
1922 while(prot_state < 4) {
1923 WaitForSingleObject(event_complete, INFINITE);
1924 SET_EXPECT(Continue);
1925 IInternetProtocol_Continue(protocol, pdata);
1926 CHECK_CALLED(Continue);
1927 SetEvent(event_complete2);
1928 }
1929
1930 WaitForSingleObject(event_complete, INFINITE);
1931 }else {
1932 SET_EXPECT(LockRequest);
1933 hres = IInternetProtocol_LockRequest(protocol, 0);
1934 ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
1935 CHECK_CALLED(LockRequest);
1936
1937 SET_EXPECT(UnlockRequest);
1938 hres = IInternetProtocol_UnlockRequest(protocol);
1939 ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
1940 CHECK_CALLED(UnlockRequest);
1941 }
1942
1943 SET_EXPECT(Terminate);
1944 hres = IInternetProtocol_Terminate(protocol, 0);
1945 ok(hres == S_OK, "Terminate failed: %08x\n", hres);
1946 CHECK_CALLED(Terminate);
1947
1948 IInternetBindInfo_Release(prot_bind_info);
1949 IInternetProtocolSink_Release(binding_sink);
1950 ref = IInternetProtocol_Release(protocol);
1951 ok(!ref, "ref=%u, expected 0\n", ref);
1952 }
1953
1954 START_TEST(protocol)
1955 {
1956 OleInitialize(NULL);
1957
1958 event_complete = CreateEvent(NULL, FALSE, FALSE, NULL);
1959 event_complete2 = CreateEvent(NULL, FALSE, FALSE, NULL);
1960
1961 test_file_protocol();
1962 test_http_protocol();
1963 test_mk_protocol();
1964 test_CreateBinding();
1965 test_binding(FILE_TEST);
1966 test_binding(HTTP_TEST);
1967
1968 CloseHandle(event_complete);
1969 CloseHandle(event_complete2);
1970
1971 OleUninitialize();
1972 }