mshtml: Add support for IHTMLStyle4 interface.
[wine] / dlls / mshtml / tests / protocol.c
1 /*
2  * Copyright 2005 Jacek Caban
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
21 #include <wine/test.h>
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "ole2.h"
27 #include "urlmon.h"
28 #include "shlwapi.h"
29
30 #include "initguid.h"
31
32 #define DEFINE_EXPECT(func) \
33     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
34
35 #define SET_EXPECT(func) \
36     expect_ ## func = TRUE
37
38 #define CHECK_EXPECT(func) \
39     do { \
40         ok(expect_ ##func, "unexpected call " #func "\n"); \
41         expect_ ## func = FALSE; \
42         called_ ## func = TRUE; \
43     }while(0)
44
45 #define CHECK_EXPECT2(func) \
46     do { \
47         ok(expect_ ##func, "unexpected call " #func  "\n"); \
48         called_ ## func = TRUE; \
49     }while(0)
50
51 #define CHECK_CALLED(func) \
52     do { \
53         ok(called_ ## func, "expected " #func "\n"); \
54         expect_ ## func = called_ ## func = FALSE; \
55     }while(0)
56
57 DEFINE_GUID(CLSID_ResProtocol, 0x3050F3BC, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
58 DEFINE_GUID(CLSID_AboutProtocol, 0x3050F406, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
59
60 DEFINE_EXPECT(GetBindInfo);
61 DEFINE_EXPECT(ReportProgress);
62 DEFINE_EXPECT(ReportData);
63 DEFINE_EXPECT(ReportResult);
64
65 static HRESULT expect_hrResult;
66 static BOOL expect_hr_win32err = FALSE;
67 static DWORD bindf;
68
69 static const WCHAR about_blank_url[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
70 static const WCHAR about_test_url[] = {'a','b','o','u','t',':','t','e','s','t',0};
71 static const WCHAR about_res_url[] = {'r','e','s',':','b','l','a','n','k',0};
72
73 static const char *debugstr_w(LPCWSTR str)
74 {
75     static char buf[1024];
76     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
77     return buf;
78 }
79
80 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
81 {
82     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
83         *ppv = iface;
84         return S_OK;
85     }
86     return E_NOINTERFACE;
87 }
88
89 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
90 {
91     return 2;
92 }
93
94 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
95 {
96     return 1;
97 }
98
99 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
100 {
101     ok(0, "unexpected call\n");
102     return E_NOTIMPL;
103 }
104
105 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
106         LPCWSTR szStatusText)
107 {
108     static const WCHAR text_html[] = {'t','e','x','t','/','h','t','m','l',0};
109
110     CHECK_EXPECT(ReportProgress);
111
112     ok(ulStatusCode == BINDSTATUS_MIMETYPEAVAILABLE
113             || ulStatusCode == BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE,
114             "ulStatusCode=%d\n", ulStatusCode);
115     ok(!lstrcmpW(szStatusText, text_html), "szStatusText != text/html\n");
116
117     return S_OK;
118 }
119
120 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF, ULONG ulProgress,
121         ULONG ulProgressMax)
122 {
123     CHECK_EXPECT(ReportData);
124
125     ok(ulProgress == ulProgressMax, "ulProgress != ulProgressMax\n");
126     ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE),
127             "grcf = %08x\n", grfBSCF);
128
129     return S_OK;
130 }
131
132 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult, DWORD dwError,
133         LPCWSTR szResult)
134 {
135     CHECK_EXPECT(ReportResult);
136
137     if(expect_hr_win32err)
138         ok((hrResult&0xffff0000) == ((FACILITY_WIN32 << 16)|0x80000000) || expect_hrResult,
139                 "expected win32 err or %08x got: %08x\n", expect_hrResult, hrResult);
140     else
141         ok(hrResult == expect_hrResult || ((expect_hrResult == E_INVALIDARG ||
142            expect_hrResult == HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND)) &&
143            hrResult == MK_E_SYNTAX), "expected: %08x got: %08x\n", expect_hrResult, hrResult);
144     ok(dwError == 0, "dwError = %d\n", dwError);
145     ok(!szResult, "szResult != NULL\n");
146
147     return S_OK;
148 }
149
150 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
151     ProtocolSink_QueryInterface,
152     ProtocolSink_AddRef,
153     ProtocolSink_Release,
154     ProtocolSink_Switch,
155     ProtocolSink_ReportProgress,
156     ProtocolSink_ReportData,
157     ProtocolSink_ReportResult
158 };
159
160 static IInternetProtocolSink protocol_sink = {
161     &protocol_sink_vtbl
162 };
163
164 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
165 {
166     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
167         *ppv = iface;
168         return S_OK;
169     }
170     return E_NOINTERFACE;
171 }
172
173 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
174 {
175     return 2;
176 }
177
178 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
179 {
180     return 1;
181 }
182
183 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
184 {
185     CHECK_EXPECT(GetBindInfo);
186
187     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
188     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
189     ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
190
191     *grfBINDF = bindf;
192     return S_OK;
193 }
194
195 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType, LPOLESTR *ppwzStr,
196         ULONG cEl, ULONG *pcElFetched)
197 {
198     ok(0, "unexpected call\n");
199     return E_NOTIMPL;
200 }
201
202 static IInternetBindInfoVtbl bind_info_vtbl = {
203     BindInfo_QueryInterface,
204     BindInfo_AddRef,
205     BindInfo_Release,
206     BindInfo_GetBindInfo,
207     BindInfo_GetBindString
208 };
209
210 static IInternetBindInfo bind_info = {
211     &bind_info_vtbl
212 };
213
214 static void test_protocol_fail(IInternetProtocol *protocol, LPCWSTR url, HRESULT expected_hres,
215         BOOL expect_win32err)
216 {
217     HRESULT hres;
218
219     SET_EXPECT(GetBindInfo);
220     SET_EXPECT(ReportResult);
221
222     expect_hrResult = expected_hres;
223     expect_hr_win32err = expect_win32err;
224     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
225     if(expect_win32err)
226         ok((hres&0xffff0000) == ((FACILITY_WIN32 << 16)|0x80000000) || hres == expect_hrResult,
227                 "expected win32 err or %08x got: %08x\n", expected_hres, hres);
228     else
229         ok(hres == expected_hres || ((expected_hres == E_INVALIDARG ||
230            expected_hres == HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND)) && hres == MK_E_SYNTAX),
231            "expected: %08x got: %08x\n", expected_hres, hres);
232
233     CHECK_CALLED(GetBindInfo);
234     CHECK_CALLED(ReportResult);
235 }
236
237 static void protocol_start(IInternetProtocol *protocol, LPCWSTR url)
238 {
239     HRESULT hres;
240
241     SET_EXPECT(GetBindInfo);
242     SET_EXPECT(ReportResult);
243     SET_EXPECT(ReportProgress);
244     SET_EXPECT(ReportData);
245     expect_hrResult = S_OK;
246     expect_hr_win32err = FALSE;
247
248     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
249     ok(hres == S_OK, "Start failed: %08x\n", hres);
250
251     CHECK_CALLED(GetBindInfo);
252     CHECK_CALLED(ReportProgress);
253     CHECK_CALLED(ReportData);
254     CHECK_CALLED(ReportResult);
255 }
256
257 static void res_sec_url_cmp(LPCWSTR url, DWORD size, LPCWSTR file)
258 {
259     WCHAR buf[MAX_PATH];
260     DWORD len;
261
262     static const WCHAR fileW[] = {'f','i','l','e',':','/','/'};
263
264     if(size < sizeof(fileW)/sizeof(WCHAR) || memcmp(url, fileW, sizeof(fileW))) {
265         ok(0, "wrong URL protocol\n");
266         return;
267     }
268
269     SetLastError(0xdeadbeef);
270     len = SearchPathW(NULL, file, NULL, sizeof(buf)/sizeof(WCHAR), buf, NULL);
271     if(!len) {
272         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
273             win_skip("SearchPathW is not implemented\n");
274         else
275             ok(0, "SearchPath failed: %u\n", GetLastError());
276         return;
277     }
278
279     len += sizeof(fileW)/sizeof(WCHAR)+1;
280     ok(len == size, "wrong size %u, expected %u\n", size, len);
281     ok(!lstrcmpW(url + sizeof(fileW)/sizeof(WCHAR), buf), "wrong file part %s\n", debugstr_w(url));
282 }
283
284 static void test_res_protocol(void)
285 {
286     IInternetProtocolInfo *protocol_info;
287     IUnknown *unk;
288     IClassFactory *factory;
289     HRESULT hres;
290
291     static const WCHAR blank_url[] =
292         {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l','/','b','l','a','n','k','.','h','t','m',0};
293     static const WCHAR test_part_url[] = {'r','e','s',':','/','/','C','S','S','/','t','e','s','t',0};
294     static const WCHAR wrong_url1[] =
295         {'m','s','h','t','m','l','.','d','l','l','/','b','l','a','n','k','.','m','t','h',0};
296     static const WCHAR wrong_url2[] =
297         {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l',0};
298     static const WCHAR wrong_url3[] =
299         {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l','/','x','x','.','h','t','m',0};
300     static const WCHAR wrong_url4[] =
301         {'r','e','s',':','/','/','x','x','.','d','l','l','/','b','l','a','n','k','.','h','t','m',0};
302     static const WCHAR wrong_url5[] =
303         {'r','e','s',':','/','/','s','h','t','m','l','.','d','l','l','/','b','l','a','n','k','.','h','t','m',0};
304     static const WCHAR mshtml_dllW[] = {'m','s','h','t','m','l','.','d','l','l',0};
305
306     hres = CoGetClassObject(&CLSID_ResProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
307     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
308     if(FAILED(hres))
309         return;
310
311     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
312     ok(hres == S_OK, "Could not get IInternetProtocolInfo interface: %08x\n", hres);
313     if(SUCCEEDED(hres)) {
314         WCHAR buf[128];
315         DWORD size;
316         int i;
317
318         for(i = PARSE_CANONICALIZE; i <= PARSE_UNESCAPE; i++) {
319             if(i != PARSE_SECURITY_URL && i != PARSE_DOMAIN) {
320                 hres = IInternetProtocolInfo_ParseUrl(protocol_info, blank_url, i, 0, buf,
321                         sizeof(buf)/sizeof(buf[0]), &size, 0);
322                 ok(hres == INET_E_DEFAULT_ACTION,
323                         "[%d] failed: %08x, expected INET_E_DEFAULT_ACTION\n", i, hres);
324             }
325         }
326
327         hres = IInternetProtocolInfo_ParseUrl(protocol_info, blank_url, PARSE_SECURITY_URL, 0, buf,
328                 sizeof(buf)/sizeof(buf[0]), &size, 0);
329         ok(hres == S_OK, "ParseUrl failed: %08x\n", hres);
330         res_sec_url_cmp(buf, size, mshtml_dllW);
331
332         size = 0;
333         hres = IInternetProtocolInfo_ParseUrl(protocol_info, blank_url, PARSE_SECURITY_URL, 0, buf,
334                 3, &size, 0);
335         ok(hres == S_FALSE, "ParseUrl failed: %08x, expected S_FALSE\n", hres);
336         ok(size, "size=0\n");
337
338         hres = IInternetProtocolInfo_ParseUrl(protocol_info, wrong_url1, PARSE_SECURITY_URL, 0, buf,
339                 sizeof(buf)/sizeof(buf[0]), &size, 0);
340         ok(hres == MK_E_SYNTAX || hres == E_INVALIDARG,
341            "ParseUrl failed: %08x, expected MK_E_SYNTAX\n", hres);
342
343         hres = IInternetProtocolInfo_ParseUrl(protocol_info, wrong_url5, PARSE_SECURITY_URL, 0, buf,
344                 sizeof(buf)/sizeof(buf[0]), &size, 0);
345         ok(hres == MK_E_SYNTAX, "ParseUrl failed: %08x, expected MK_E_SYNTAX\n", hres);
346
347         size = 0xdeadbeef;
348         buf[0] = '?';
349         hres = IInternetProtocolInfo_ParseUrl(protocol_info, blank_url, PARSE_DOMAIN, 0, buf,
350                 sizeof(buf)/sizeof(buf[0]), &size, 0);
351         ok(hres == S_OK || hres == E_FAIL, "ParseUrl failed: %08x\n", hres);
352         ok(buf[0] == '?', "buf changed\n");
353         ok(size == sizeof(blank_url)/sizeof(WCHAR), "size=%d\n", size);
354
355         size = 0xdeadbeef;
356         hres = IInternetProtocolInfo_ParseUrl(protocol_info, wrong_url1, PARSE_DOMAIN, 0, buf,
357                 sizeof(buf)/sizeof(buf[0]), &size, 0);
358         ok(hres == S_OK || hres == E_FAIL, "ParseUrl failed: %08x\n", hres);
359         ok(buf[0] == '?', "buf changed\n");
360         ok(size == sizeof(wrong_url1)/sizeof(WCHAR), "size=%d\n", size);
361
362         if (0)
363         {
364         /* Crashes on win9x */
365         size = 0xdeadbeef;
366         buf[0] = '?';
367         hres = IInternetProtocolInfo_ParseUrl(protocol_info, NULL, PARSE_DOMAIN, 0, buf,
368                 sizeof(buf)/sizeof(buf[0]), &size, 0);
369         ok(hres == E_FAIL, "ParseUrl failed: %08x\n", hres);
370         ok(buf[0] == '?', "buf changed\n");
371         ok(size == 1, "size=%u, expected 1\n", size);
372
373         buf[0] = '?';
374         hres = IInternetProtocolInfo_ParseUrl(protocol_info, blank_url, PARSE_DOMAIN, 0, buf,
375                 sizeof(buf)/sizeof(buf[0]), NULL, 0);
376         ok(hres == E_POINTER, "ParseUrl failed: %08x\n", hres);
377         ok(buf[0] == '?', "buf changed\n");
378
379         buf[0] = '?';
380         hres = IInternetProtocolInfo_ParseUrl(protocol_info, NULL, PARSE_DOMAIN, 0, buf,
381                 sizeof(buf)/sizeof(buf[0]), NULL, 0);
382         ok(hres == E_POINTER, "ParseUrl failed: %08x\n", hres);
383         ok(buf[0] == '?', "buf changed\n");
384         }
385
386         buf[0] = '?';
387         hres = IInternetProtocolInfo_ParseUrl(protocol_info, blank_url, PARSE_UNESCAPE+1, 0, buf,
388                 sizeof(buf)/sizeof(buf[0]), &size, 0);
389         ok(hres == INET_E_DEFAULT_ACTION,
390                 "ParseUrl failed: %08x, expected INET_E_DEFAULT_ACTION\n", hres);
391         ok(buf[0] == '?', "buf changed\n");
392
393         size = 0xdeadbeef;
394         hres = IInternetProtocolInfo_CombineUrl(protocol_info, blank_url, test_part_url,
395                 0, buf, sizeof(buf)/sizeof(buf[0]), &size, 0);
396         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER, "CombineUrl failed: %08x\n", hres);
397         ok(size == 0xdeadbeef, "size=%d\n", size);
398
399         size = 0xdeadbeef;
400         hres = IInternetProtocolInfo_CombineUrl(protocol_info, blank_url, test_part_url,
401                 URL_FILE_USE_PATHURL, buf, sizeof(buf)/sizeof(buf[0]), &size, 0);
402         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER, "CombineUrl failed: %08x\n", hres);
403         ok(size == 0xdeadbeef, "size=%d\n", size);
404
405         size = 0xdeadbeef;
406         hres = IInternetProtocolInfo_CombineUrl(protocol_info, NULL, NULL,
407                 URL_FILE_USE_PATHURL, NULL, 0xdeadbeef, NULL, 0);
408         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER, "CombineUrl failed: %08x\n", hres);
409         ok(size == 0xdeadbeef, "size=%d\n", size);
410
411         hres = IInternetProtocolInfo_CompareUrl(protocol_info, blank_url, blank_url, 0);
412         ok(hres == E_NOTIMPL, "CompareUrl failed: %08x\n", hres);
413
414         hres = IInternetProtocolInfo_CompareUrl(protocol_info, NULL, NULL, 0xdeadbeef);
415         ok(hres == E_NOTIMPL, "CompareUrl failed: %08x\n", hres);
416
417         for(i=0; i<30; i++) {
418             if(i == QUERY_USES_NETWORK || i == QUERY_IS_SECURE || i == QUERY_IS_SAFE)
419                 continue;
420
421             hres = IInternetProtocolInfo_QueryInfo(protocol_info, blank_url, i, 0,
422                                                    buf, sizeof(buf), &size, 0);
423             ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER,
424                "QueryInfo(%d) returned: %08x, expected INET_E_USE_DEFAULT_PROTOCOLHANDLER\n", i, hres);
425         }
426
427         size = 0xdeadbeef;
428         memset(buf, '?', sizeof(buf));
429         hres = IInternetProtocolInfo_QueryInfo(protocol_info, blank_url, QUERY_USES_NETWORK, 0,
430                                                buf, sizeof(buf), &size, 0);
431         ok(hres == S_OK, "QueryInfo(QUERY_USES_NETWORK) failed: %08x\n", hres);
432         ok(size == sizeof(DWORD), "size=%d\n", size);
433         ok(!*(DWORD*)buf, "buf=%d\n", *(DWORD*)buf);
434
435         memset(buf, '?', sizeof(buf));
436         hres = IInternetProtocolInfo_QueryInfo(protocol_info, blank_url, QUERY_USES_NETWORK, 0,
437                                                buf, sizeof(buf), NULL, 0);
438         ok(hres == S_OK, "QueryInfo(QUERY_USES_NETWORK) failed: %08x\n", hres);
439         ok(!*(DWORD*)buf, "buf=%d\n", *(DWORD*)buf);
440
441         hres = IInternetProtocolInfo_QueryInfo(protocol_info, blank_url, QUERY_USES_NETWORK, 0,
442                                                buf, 3, &size, 0);
443         ok(hres == E_FAIL, "QueryInfo(QUERY_USES_NETWORK) failed: %08x, expected E_FAIL\n", hres);
444
445         size = 0xdeadbeef;
446         memset(buf, '?', sizeof(buf));
447         hres = IInternetProtocolInfo_QueryInfo(protocol_info, NULL, QUERY_USES_NETWORK, 0,
448                                                buf, sizeof(buf), &size, 0);
449         ok(hres == S_OK, "QueryInfo(QUERY_USES_NETWORK) failed: %08x, expected E_FAIL\n", hres);
450         ok(hres == S_OK, "QueryInfo(QUERY_USES_NETWORK) failed: %08x\n", hres);
451         ok(size == sizeof(DWORD), "size=%d\n", size);
452         ok(!*(DWORD*)buf, "buf=%d\n", *(DWORD*)buf);
453
454         hres = IInternetProtocolInfo_QueryInfo(protocol_info, blank_url, QUERY_USES_NETWORK, 0,
455                                                NULL, sizeof(buf), &size, 0);
456         ok(hres == E_FAIL, "QueryInfo(QUERY_USES_NETWORK) failed: %08x, expected E_FAIL\n", hres);
457
458         hres = IInternetProtocolInfo_QueryInfo(protocol_info, blank_url, 60, 0,
459                                                NULL, sizeof(buf), &size, 0);
460         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER,
461            "QueryInfo failed: %08x, expected INET_E_USE_DEFAULT_PROTOCOLHANDLER\n", hres);
462
463         IInternetProtocolInfo_Release(protocol_info);
464     }
465
466     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
467     ok(hres == S_OK, "Could not get IClassFactory interface\n");
468     if(SUCCEEDED(hres)) {
469         IInternetProtocol *protocol;
470         BYTE buf[512];
471         ULONG cb;
472         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
473         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
474
475         if(SUCCEEDED(hres)) {
476             IInternetPriority *priority;
477
478             hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
479             ok(hres == E_NOINTERFACE,
480                "QueryInterface(IInternetPriority) returned %08x, expected E_NOINTEFACE\n", hres);
481
482             test_protocol_fail(protocol, wrong_url1, E_INVALIDARG, FALSE);
483             test_protocol_fail(protocol, wrong_url2,
484                                HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND), FALSE);
485             test_protocol_fail(protocol, wrong_url3, E_FAIL, TRUE);
486             test_protocol_fail(protocol, wrong_url4, E_FAIL, TRUE);
487
488             cb = 0xdeadbeef;
489             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
490             ok(hres == E_FAIL, "Read returned %08x expected E_FAIL\n", hres);
491             ok(cb == 0xdeadbeef, "cb=%u expected 0xdeadbeef\n", cb);
492     
493             protocol_start(protocol, blank_url);
494             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
495             ok(hres == S_OK, "Read failed: %08x\n", hres);
496             ok(cb == 2, "cb=%u expected 2\n", cb);
497             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
498             ok(hres == S_OK, "Read failed: %08x\n", hres);
499             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
500             ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
501             ok(cb == 0, "cb=%u expected 0\n", cb);
502             hres = IInternetProtocol_UnlockRequest(protocol);
503             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
504
505             protocol_start(protocol, blank_url);
506             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
507             ok(hres == S_OK, "Read failed: %08x\n", hres);
508             hres = IInternetProtocol_LockRequest(protocol, 0);
509             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
510             hres = IInternetProtocol_UnlockRequest(protocol);
511             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
512             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
513             ok(hres == S_OK, "Read failed: %08x\n", hres);
514
515             protocol_start(protocol, blank_url);
516             hres = IInternetProtocol_LockRequest(protocol, 0);
517             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
518             hres = IInternetProtocol_Terminate(protocol, 0);
519             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
520             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
521             ok(hres == S_OK, "Read failed: %08x\n\n", hres);
522             hres = IInternetProtocol_UnlockRequest(protocol);
523             ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
524             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
525             ok(hres == S_OK, "Read failed: %08x\n", hres);
526             hres = IInternetProtocol_Terminate(protocol, 0);
527             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
528             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
529             ok(hres == S_OK, "Read failed: %08x\n", hres);
530             ok(cb == 2, "cb=%u expected 2\n", cb);
531
532             protocol_start(protocol, blank_url);
533             hres = IInternetProtocol_LockRequest(protocol, 0);
534             ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
535             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
536             ok(hres == S_OK, "Read failed: %08x\n", hres);
537             protocol_start(protocol, blank_url);
538             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
539             ok(hres == S_OK, "Read failed: %08x\n", hres);
540             hres = IInternetProtocol_Terminate(protocol, 0);
541             ok(hres == S_OK, "Terminate failed: %08x\n", hres);
542
543             IInternetProtocol_Release(protocol);
544         }
545
546         IClassFactory_Release(factory);
547     }
548
549     IUnknown_Release(unk);
550 }
551
552 static void do_test_about_protocol(IClassFactory *factory, DWORD bf)
553 {
554     IInternetProtocol *protocol;
555     IInternetPriority *priority;
556     BYTE buf[512];
557     ULONG cb;
558     HRESULT hres;
559
560     static const WCHAR blank_html[] = {0xfeff,'<','H','T','M','L','>','<','/','H','T','M','L','>',0};
561     static const WCHAR test_html[] =
562         {0xfeff,'<','H','T','M','L','>','t','e','s','t','<','/','H','T','M','L','>',0};
563
564     bindf = bf;
565
566     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
567     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
568     if(FAILED(hres))
569         return;
570
571     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
572     ok(hres == E_NOINTERFACE,
573        "QueryInterface(IInternetPriority) returned %08x, expected E_NOINTEFACE\n", hres);
574
575     protocol_start(protocol, about_blank_url);
576     hres = IInternetProtocol_LockRequest(protocol, 0);
577     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
578     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
579     ok(hres == S_OK, "Read failed: %08x\n", hres);
580     ok(cb == sizeof(blank_html), "cb=%d\n", cb);
581     ok(!memcmp(buf, blank_html, cb), "Readed wrong data\n");
582     hres = IInternetProtocol_UnlockRequest(protocol);
583     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
584
585     protocol_start(protocol, about_test_url);
586     hres = IInternetProtocol_LockRequest(protocol, 0);
587     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
588     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
589     ok(hres == S_OK, "Read failed: %08x\n", hres);
590     ok(cb == sizeof(test_html), "cb=%d\n", cb);
591     ok(!memcmp(buf, test_html, cb), "Readed wrong data\n");
592     hres = IInternetProtocol_UnlockRequest(protocol);
593     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
594
595     protocol_start(protocol, about_res_url);
596     hres = IInternetProtocol_LockRequest(protocol, 0);
597     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
598     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
599     ok(hres == S_OK, "Read failed: %08x\n", hres);
600     ok(cb == sizeof(blank_html), "cb=%d\n", cb);
601     ok(!memcmp(buf, blank_html, cb), "Readed wrong data\n");
602     hres = IInternetProtocol_UnlockRequest(protocol);
603     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
604
605     IInternetProtocol_Release(protocol);
606 }
607
608 static void test_about_protocol(void)
609 {
610     IInternetProtocolInfo *protocol_info;
611     IUnknown *unk;
612     IClassFactory *factory;
613     HRESULT hres;
614
615     hres = CoGetClassObject(&CLSID_AboutProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
616     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
617     if(FAILED(hres))
618         return;
619
620     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
621     ok(hres == S_OK, "Could not get IInternetProtocolInfo interface: %08x\n", hres);
622     if(SUCCEEDED(hres)) {
623         WCHAR buf[128];
624         DWORD size;
625         int i;
626
627         for(i = PARSE_CANONICALIZE; i <= PARSE_UNESCAPE; i++) {
628             if(i != PARSE_SECURITY_URL && i != PARSE_DOMAIN) {
629                 hres = IInternetProtocolInfo_ParseUrl(protocol_info, about_blank_url, i, 0, buf,
630                         sizeof(buf)/sizeof(buf[0]), &size, 0);
631                 ok(hres == INET_E_DEFAULT_ACTION,
632                         "[%d] failed: %08x, expected INET_E_DEFAULT_ACTION\n", i, hres);
633             }
634         }
635
636         hres = IInternetProtocolInfo_ParseUrl(protocol_info, about_blank_url, PARSE_SECURITY_URL, 0, buf,
637                 sizeof(buf)/sizeof(buf[0]), &size, 0);
638         ok(hres == S_OK, "ParseUrl failed: %08x\n", hres);
639         ok(!lstrcmpW(about_blank_url, buf), "buf != blank_url\n");
640
641         hres = IInternetProtocolInfo_ParseUrl(protocol_info, about_blank_url, PARSE_SECURITY_URL, 0, buf,
642                 3, &size, 0);
643         ok(hres == S_FALSE, "ParseUrl failed: %08x, expected S_FALSE\n", hres);
644
645         hres = IInternetProtocolInfo_ParseUrl(protocol_info, about_test_url, PARSE_SECURITY_URL, 0, buf,
646                 sizeof(buf)/sizeof(buf[0]), &size, 0);
647         ok(hres == S_OK, "ParseUrl failed: %08x\n", hres);
648         ok(!lstrcmpW(about_test_url, buf), "buf != test_url\n");
649
650         size = 0xdeadbeef;
651         buf[0] = '?';
652         hres = IInternetProtocolInfo_ParseUrl(protocol_info, about_blank_url, PARSE_DOMAIN, 0, buf,
653                 sizeof(buf)/sizeof(buf[0]), &size, 0);
654         ok(hres == S_OK || hres == E_FAIL, "ParseUrl failed: %08x\n", hres);
655         ok(buf[0] == '?', "buf changed\n");
656         ok(size == sizeof(about_blank_url)/sizeof(WCHAR), "size=%d\n", size);
657
658         if (0)
659         {
660         /* Crashes on win9x */
661         size = 0xdeadbeef;
662         buf[0] = '?';
663         hres = IInternetProtocolInfo_ParseUrl(protocol_info, NULL, PARSE_DOMAIN, 0, buf,
664                 sizeof(buf)/sizeof(buf[0]), &size, 0);
665         ok(hres == E_FAIL, "ParseUrl failed: %08x\n", hres);
666         ok(buf[0] == '?', "buf changed\n");
667         ok(size == 1, "size=%u, expected 1\n", size);
668
669         buf[0] = '?';
670         hres = IInternetProtocolInfo_ParseUrl(protocol_info, about_blank_url, PARSE_DOMAIN, 0, buf,
671                 sizeof(buf)/sizeof(buf[0]), NULL, 0);
672         ok(hres == E_POINTER, "ParseUrl failed: %08x\n", hres);
673         ok(buf[0] == '?', "buf changed\n");
674
675         buf[0] = '?';
676         hres = IInternetProtocolInfo_ParseUrl(protocol_info, NULL, PARSE_DOMAIN, 0, buf,
677                 sizeof(buf)/sizeof(buf[0]), NULL, 0);
678         ok(hres == E_POINTER, "ParseUrl failed: %08x\n", hres);
679         ok(buf[0] == '?', "buf changed\n");
680         }
681
682         hres = IInternetProtocolInfo_ParseUrl(protocol_info, about_blank_url, PARSE_UNESCAPE+1, 0, buf,
683                 sizeof(buf)/sizeof(buf[0]), &size, 0);
684         ok(hres == INET_E_DEFAULT_ACTION,
685                 "ParseUrl failed: %08x, expected INET_E_DEFAULT_ACTION\n", hres);
686
687         size = 0xdeadbeef;
688         hres = IInternetProtocolInfo_CombineUrl(protocol_info, about_blank_url, about_test_url,
689                 0, buf, sizeof(buf)/sizeof(buf[0]), &size, 0);
690         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER, "CombineUrl failed: %08x\n", hres);
691         ok(size == 0xdeadbeef, "size=%d\n", size);
692
693         size = 0xdeadbeef;
694         hres = IInternetProtocolInfo_CombineUrl(protocol_info, about_blank_url, about_test_url,
695                 URL_FILE_USE_PATHURL, buf, sizeof(buf)/sizeof(buf[0]), &size, 0);
696         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER, "CombineUrl failed: %08x\n", hres);
697         ok(size == 0xdeadbeef, "size=%d\n", size);
698
699         size = 0xdeadbeef;
700         hres = IInternetProtocolInfo_CombineUrl(protocol_info, NULL, NULL,
701                 URL_FILE_USE_PATHURL, buf, sizeof(buf)/sizeof(buf[0]), &size, 0);
702         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER, "CombineUrl failed: %08x\n", hres);
703         ok(size == 0xdeadbeef, "size=%d\n", size);
704
705         hres = IInternetProtocolInfo_CompareUrl(protocol_info, about_blank_url, about_blank_url, 0);
706         ok(hres == E_NOTIMPL, "CompareUrl failed: %08x\n", hres);
707
708         hres = IInternetProtocolInfo_CompareUrl(protocol_info, NULL, NULL, 0xdeadbeef);
709         ok(hres == E_NOTIMPL, "CompareUrl failed: %08x\n", hres);
710
711         for(i=0; i<30; i++) {
712             switch(i) {
713             case QUERY_CAN_NAVIGATE:
714             case QUERY_USES_NETWORK:
715             case QUERY_IS_CACHED:
716             case QUERY_IS_INSTALLEDENTRY:
717             case QUERY_IS_CACHED_OR_MAPPED:
718             case QUERY_IS_SECURE:
719             case QUERY_IS_SAFE:
720             case QUERY_USES_HISTORYFOLDER:
721                 break;
722             default:
723                 hres = IInternetProtocolInfo_QueryInfo(protocol_info, about_blank_url, i, 0,
724                                                        buf, sizeof(buf), &size, 0);
725                 ok(hres == E_FAIL, "QueryInfo(%d) returned: %08x, expected E_FAIL\n", i, hres);
726             }
727         }
728
729         hres = IInternetProtocolInfo_QueryInfo(protocol_info, about_blank_url, QUERY_CAN_NAVIGATE, 0,
730                                                buf, sizeof(buf), &size, 0);
731         ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER ||
732            hres == E_FAIL, /* win2k */
733            "QueryInfo returned: %08x, expected INET_E_USE_DEFAULT_PROTOCOLHANDLER or E_FAIL\n", hres);
734
735         size = 0xdeadbeef;
736         memset(buf, '?', sizeof(buf));
737         hres = IInternetProtocolInfo_QueryInfo(protocol_info, about_blank_url, QUERY_USES_NETWORK, 0,
738                                                buf, sizeof(buf), &size, 0);
739         ok(hres == S_OK, "QueryInfo(QUERY_USES_NETWORK) failed: %08x\n", hres);
740         ok(size == sizeof(DWORD), "size=%d\n", size);
741         ok(!*(DWORD*)buf, "buf=%d\n", *(DWORD*)buf);
742
743         memset(buf, '?', sizeof(buf));
744         hres = IInternetProtocolInfo_QueryInfo(protocol_info, about_blank_url, QUERY_USES_NETWORK, 0,
745                                                buf, sizeof(buf), NULL, 0);
746         ok(hres == S_OK, "QueryInfo(QUERY_USES_NETWORK) failed: %08x\n", hres);
747         ok(!*(DWORD*)buf, "buf=%d\n", *(DWORD*)buf);
748
749         hres = IInternetProtocolInfo_QueryInfo(protocol_info, about_blank_url, QUERY_USES_NETWORK, 0,
750                                                buf, 3, &size, 0);
751         ok(hres == E_FAIL, "QueryInfo(QUERY_USES_NETWORK) failed: %08x, expected E_FAIL\n", hres);
752
753         hres = IInternetProtocolInfo_QueryInfo(protocol_info, about_blank_url, QUERY_USES_NETWORK, 0,
754                                                NULL, sizeof(buf), &size, 0);
755         ok(hres == E_FAIL, "QueryInfo(QUERY_USES_NETWORK) failed: %08x, expected E_FAIL\n", hres);
756
757         hres = IInternetProtocolInfo_QueryInfo(protocol_info, about_blank_url, 60, 0,
758                                                NULL, sizeof(buf), &size, 0);
759         ok(hres == E_FAIL, "QueryInfo failed: %08x, expected E_FAIL\n", hres);
760
761         IInternetProtocolInfo_Release(protocol_info);
762     }
763
764     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
765     ok(hres == S_OK, "Could not get IClassFactory interface\n");
766     if(SUCCEEDED(hres)) {
767         do_test_about_protocol(factory, 0);
768         do_test_about_protocol(factory,
769                 BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA | BINDF_FROMURLMON | BINDF_NEEDFILE);
770
771         IClassFactory_Release(factory);
772     }
773
774     IUnknown_Release(unk);
775 }
776
777 START_TEST(protocol)
778 {
779     OleInitialize(NULL);
780
781     test_res_protocol();
782     test_about_protocol();
783
784     OleUninitialize();
785 }