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