crypt32: Get rid of attribute certs from signed info, they're not supported anyway.
[wine] / dlls / itss / tests / protocol.c
1 /*
2  * Copyright 2006 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
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_ITSProtocol,0x9d148291,0xb9c8,0x11d0,0xa4,0xcc,0x00,0x00,0xf8,0x01,0x49,0xf6);
58
59 DEFINE_EXPECT(GetBindInfo);
60 DEFINE_EXPECT(ReportProgress_BEGINDOWNLOADDATA);
61 DEFINE_EXPECT(ReportProgress_SENDINGREQUEST);
62 DEFINE_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
63 DEFINE_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
64 DEFINE_EXPECT(ReportProgress_DIRECTBIND);
65 DEFINE_EXPECT(ReportData);
66 DEFINE_EXPECT(ReportResult);
67
68 static HRESULT expect_hrResult;
69 static IInternetProtocol *read_protocol = NULL;
70
71 static const WCHAR blank_url1[] = {'i','t','s',':',
72     't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
73 static const WCHAR blank_url2[] = {'m','S','-','i','T','s',':',
74     't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
75 static const WCHAR blank_url3[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
76     't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
77 static const WCHAR blank_url4[] = {'i','t','s',':',
78     't','e','s','t','.','c','h','m',':',':','b','l','a','n','k','.','h','t','m','l',0};
79 static const WCHAR blank_url5[] = {'i','t','s',':',
80     't','e','s','t','.','c','h','m',':',':','\\','b','l','a','n','k','.','h','t','m','l',0};
81 static const WCHAR blank_url6[] = {'i','t','s',':',
82     't','e','s','t','.','c','h','m',':',':','/','%','6','2','l','a','n','k','.','h','t','m','l',0};
83 static const WCHAR blank_url7[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
84     't','e','s','t','.','c','h','m',':',':','\\','b','l','a','n','k','.','h','t','m','l',0};
85 static const WCHAR blank_url8[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
86     't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l','/',0};
87
88 static enum {
89     ITS_PROTOCOL,
90     MK_PROTOCOL
91 } test_protocol;
92
93 static const WCHAR cache_file1[] =
94     {'t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
95 static const WCHAR cache_file2[] =
96     {'t','e','s','t','.','c','h','m',':',':','\\','b','l','a','n','k','.','h','t','m','l',0};
97 static const WCHAR cache_file3[] =
98     {'t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l','/',0};
99 static const WCHAR *cache_file = cache_file1;
100
101 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
102 {
103     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
104         *ppv = iface;
105         return S_OK;
106     }
107     return E_NOINTERFACE;
108 }
109
110 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
111 {
112     return 2;
113 }
114
115 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
116 {
117     return 1;
118 }
119
120 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
121 {
122     ok(0, "unexpected call\n");
123     return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
127         LPCWSTR szStatusText)
128 {
129     static const WCHAR blank_html[] = {'b','l','a','n','k','.','h','t','m','l',0};
130     static const WCHAR text_html[] = {'t','e','x','t','/','h','t','m','l',0};
131
132     switch(ulStatusCode) {
133     case BINDSTATUS_BEGINDOWNLOADDATA:
134         CHECK_EXPECT(ReportProgress_BEGINDOWNLOADDATA);
135         ok(!szStatusText, "szStatusText != NULL\n");
136         break;
137     case BINDSTATUS_SENDINGREQUEST:
138         CHECK_EXPECT(ReportProgress_SENDINGREQUEST);
139         if(test_protocol == ITS_PROTOCOL)
140             ok(!lstrcmpW(szStatusText, blank_html), "unexpected szStatusText\n");
141         else
142             ok(szStatusText == NULL, "szStatusText != NULL\n");
143         break;
144     case BINDSTATUS_MIMETYPEAVAILABLE:
145         CHECK_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
146         ok(!lstrcmpW(szStatusText, text_html), "unexpected szStatusText\n");
147         break;
148     case BINDSTATUS_CACHEFILENAMEAVAILABLE:
149         CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
150         ok(!lstrcmpW(szStatusText, cache_file), "unexpected szStatusText\n");
151         break;
152     case BINDSTATUS_DIRECTBIND:
153         CHECK_EXPECT(ReportProgress_DIRECTBIND);
154         ok(!szStatusText, "szStatusText != NULL\n");
155         break;
156     default:
157         ok(0, "unexpected ulStatusCode %d\n", ulStatusCode);
158         break;
159     }
160
161     return S_OK;
162 }
163
164 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF, ULONG ulProgress,
165         ULONG ulProgressMax)
166 {
167     CHECK_EXPECT(ReportData);
168
169     ok(ulProgress == ulProgressMax, "ulProgress != ulProgressMax\n");
170     if(test_protocol == ITS_PROTOCOL)
171         ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE), "grcf = %08x\n", grfBSCF);
172     else
173         ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION), "grcf = %08x\n", grfBSCF);
174
175     if(read_protocol) {
176         BYTE buf[100];
177         DWORD cb = 0xdeadbeef;
178         HRESULT hres;
179
180         hres = IInternetProtocol_Read(read_protocol, buf, sizeof(buf), &cb);
181         ok(hres == S_OK, "Read failed: %08x\n", hres);
182         ok(cb == 13, "cb=%u expected 13\n", cb);
183         ok(!memcmp(buf, "<html></html>", 13), "unexpected data\n");
184     }
185
186     return S_OK;
187 }
188
189 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
190         DWORD dwError, LPCWSTR szResult)
191 {
192     CHECK_EXPECT(ReportResult);
193
194     ok(hrResult == expect_hrResult, "expected: %08x got: %08x\n", expect_hrResult, hrResult);
195     ok(dwError == 0, "dwError = %d\n", dwError);
196     ok(!szResult, "szResult != NULL\n");
197
198     return S_OK;
199 }
200
201 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
202     ProtocolSink_QueryInterface,
203     ProtocolSink_AddRef,
204     ProtocolSink_Release,
205     ProtocolSink_Switch,
206     ProtocolSink_ReportProgress,
207     ProtocolSink_ReportData,
208     ProtocolSink_ReportResult
209 };
210
211 static IInternetProtocolSink protocol_sink = {
212     &protocol_sink_vtbl
213 };
214
215 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
216 {
217     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
218         *ppv = iface;
219         return S_OK;
220     }
221     return E_NOINTERFACE;
222 }
223
224 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
225 {
226     return 2;
227 }
228
229 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
230 {
231     return 1;
232 }
233
234 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
235 {
236     CHECK_EXPECT(GetBindInfo);
237
238     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
239     if(grfBINDF)
240         ok(!*grfBINDF, "*grfBINDF != 0\n");
241     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
242     ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %d\n", pbindinfo->cbSize);
243
244     return S_OK;
245 }
246
247 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType, LPOLESTR *ppwzStr,
248         ULONG cEl, ULONG *pcElFetched)
249 {
250     ok(0, "unexpected call\n");
251     return E_NOTIMPL;
252 }
253
254 static IInternetBindInfoVtbl bind_info_vtbl = {
255     BindInfo_QueryInterface,
256     BindInfo_AddRef,
257     BindInfo_Release,
258     BindInfo_GetBindInfo,
259     BindInfo_GetBindString
260 };
261
262 static IInternetBindInfo bind_info = {
263     &bind_info_vtbl
264 };
265
266 static void test_protocol_fail(IInternetProtocol *protocol, LPCWSTR url, HRESULT expected_hres)
267 {
268     HRESULT hres;
269
270     SET_EXPECT(GetBindInfo);
271     SET_EXPECT(ReportResult);
272
273     expect_hrResult = expected_hres;
274     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
275     ok(hres == expected_hres, "expected: %08x got: %08x\n", expected_hres, hres);
276
277     CHECK_CALLED(GetBindInfo);
278     CHECK_CALLED(ReportResult);
279 }
280
281 static void protocol_start(IInternetProtocol *protocol, LPCWSTR url, BOOL expect_mime)
282 {
283     HRESULT hres;
284
285     SET_EXPECT(GetBindInfo);
286     if(test_protocol == MK_PROTOCOL)
287         SET_EXPECT(ReportProgress_DIRECTBIND);
288     SET_EXPECT(ReportProgress_SENDINGREQUEST);
289     if(expect_mime)
290         SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
291     if(test_protocol == MK_PROTOCOL)
292         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
293     SET_EXPECT(ReportData);
294     if(test_protocol == ITS_PROTOCOL)
295         SET_EXPECT(ReportProgress_BEGINDOWNLOADDATA);
296     SET_EXPECT(ReportResult);
297     expect_hrResult = S_OK;
298
299     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
300     ok(hres == S_OK, "Start failed: %08x\n", hres);
301
302     CHECK_CALLED(GetBindInfo);
303     if(test_protocol == MK_PROTOCOL)
304         CHECK_CALLED(ReportProgress_DIRECTBIND);
305     CHECK_CALLED(ReportProgress_SENDINGREQUEST);
306     if(expect_mime)
307         CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE);
308     if(test_protocol == MK_PROTOCOL)
309         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAIABLE);
310     CHECK_CALLED(ReportData);
311     if(test_protocol == ITS_PROTOCOL)
312         CHECK_CALLED(ReportProgress_BEGINDOWNLOADDATA);
313     CHECK_CALLED(ReportResult);
314 }
315
316 static void test_protocol_url(IClassFactory *factory, LPCWSTR url, BOOL expect_mime)
317 {
318     IInternetProtocol *protocol;
319     BYTE buf[512];
320     ULONG cb, ref;
321     HRESULT hres;
322
323     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
324     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
325     if(FAILED(hres))
326         return;
327
328     protocol_start(protocol, url, expect_mime);
329     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
330     ok(hres == S_OK, "Read failed: %08x\n", hres);
331     ok(cb == 13, "cb=%u expected 13\n", cb);
332     ok(!memcmp(buf, "<html></html>", 13), "unexpected data\n");
333     ref = IInternetProtocol_Release(protocol);
334     ok(!ref, "protocol ref=%d\n", ref);
335
336     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
337     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
338     if(FAILED(hres))
339         return;
340
341     cb = 0xdeadbeef;
342     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
343     ok(hres == (test_protocol == ITS_PROTOCOL ? INET_E_DATA_NOT_AVAILABLE : E_FAIL),
344        "Read returned %08x\n", hres);
345     ok(cb == 0xdeadbeef, "cb=%u expected 0xdeadbeef\n", cb);
346
347     protocol_start(protocol, url, expect_mime);
348     hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
349     ok(hres == S_OK, "Read failed: %08x\n", hres);
350     ok(cb == 2, "cb=%u expected 2\n", cb);
351     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
352     ok(hres == S_OK, "Read failed: %08x\n", hres);
353     ok(cb == 11, "cb=%u, expected 11\n", cb);
354     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
355     ok(hres == S_FALSE, "Read failed: %08x expected S_FALSE\n", hres);
356     ok(cb == 0, "cb=%u expected 0\n", cb);
357     hres = IInternetProtocol_UnlockRequest(protocol);
358     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
359     ref = IInternetProtocol_Release(protocol);
360     ok(!ref, "protocol ref=%d\n", ref);
361
362     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
363     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
364     if(FAILED(hres))
365         return;
366
367     protocol_start(protocol, url, expect_mime);
368     hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
369     ok(hres == S_OK, "Read failed: %08x\n", hres);
370     hres = IInternetProtocol_LockRequest(protocol, 0);
371     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
372     hres = IInternetProtocol_UnlockRequest(protocol);
373     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
374     hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
375     ok(hres == S_OK, "Read failed: %08x\n", hres);
376     ok(cb == 11, "cb=%u, expected 11\n", cb);
377     ref = IInternetProtocol_Release(protocol);
378     ok(!ref, "protocol ref=%d\n", ref);
379
380     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
381     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
382     if(FAILED(hres))
383         return;
384
385     protocol_start(protocol, url, expect_mime);
386     hres = IInternetProtocol_LockRequest(protocol, 0);
387     ok(hres == S_OK, "LockRequest failed: %08x\n", hres);
388     hres = IInternetProtocol_Terminate(protocol, 0);
389     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
390     hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
391     ok(hres == S_OK, "Read failed: %08x\n", hres);
392     ok(cb == 2, "cb=%u, expected 2\n", cb);
393     hres = IInternetProtocol_UnlockRequest(protocol);
394     ok(hres == S_OK, "UnlockRequest failed: %08x\n", hres);
395     hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
396     ok(hres == S_OK, "Read failed: %08x\n", hres);
397     ok(cb == 2, "cb=%u, expected 2\n", cb);
398     hres = IInternetProtocol_Terminate(protocol, 0);
399     ok(hres == S_OK, "Terminate failed: %08x\n", hres);
400     hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
401     ok(hres == S_OK, "Read failed: %08x\n", hres);
402     ok(cb == 2, "cb=%u expected 2\n", cb);
403     ref = IInternetProtocol_Release(protocol);
404     ok(!ref, "protocol ref=%d\n", ref);
405
406     hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&read_protocol);
407     ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
408     if(FAILED(hres))
409         return;
410
411     protocol_start(read_protocol, url, expect_mime);
412     ref = IInternetProtocol_Release(read_protocol);
413     ok(!ref, "protocol ref=%d\n", ref);
414     read_protocol = NULL;
415 }
416
417 static const WCHAR rel_url1[] =
418     {'t','e','s','t','.','h','t','m','l',0};
419 static const WCHAR rel_url2[] =
420     {'t','e','s','t','.','c','h','m',':',':','/','t','e','s','t','.','h','t','m','l',0};
421 static const WCHAR rel_url3[] =
422     {'/','t','e','s','t','.','h','t','m','l',0};
423 static const WCHAR rel_url4[] =
424     {'t','e',':','t','.','h','t','m','l',0};
425 static const WCHAR rel_url5[] =
426     {'d','i','r','/','t','e','s','t','.','h','t','m','l',0};
427
428 static const WCHAR base_url1[] = {'i','t','s',':',
429     't','e','s','t',':','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
430 static const WCHAR base_url2[] = {'i','t','s',':','t','e','s','t','.','c','h','m',
431     ':',':','/','d','i','r','/','b','l','a','n','k','.','h','t','m','l',0};
432 static const WCHAR base_url3[] = {'m','s','-','i','t','s',':','t','e','s','t','.','c','h','m',
433     ':',':','/','d','i','r','/','b','l','a','n','k','.','h','t','m','l',0};
434 static const WCHAR base_url4[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
435     't','e','s','t','.','c','h','m',':',':','/','d','i','r','/',
436     'b','l','a','n','k','.','h','t','m','l',0};
437 static const WCHAR base_url5[] = {'x','x','x',':','t','e','s','t','.','c','h','m',
438     ':',':','/','d','i','r','/','b','l','a','n','k','.','h','t','m','l',0};
439
440 static const WCHAR combined_url1[] = {'i','t','s',':',
441     't','e','s','t','.','c','h','m',':',':','/','t','e','s','t','.','h','t','m','l',0};
442 static const WCHAR combined_url2[] = {'i','t','s',':',
443     't','e','s','t','.','c','h','m',':',':','/','d','i','r','/','t','e','s','t','.','h','t','m','l',0};
444 static const WCHAR combined_url3[] = {'i','t','s',':',
445     't','e','s','t',':','.','c','h','m',':',':','/','t','e','s','t','.','h','t','m','l',0};
446 static const WCHAR combined_url4[] = {'i','t','s',':','t','e','s','t','.','c','h','m',
447     ':',':','b','l','a','n','k','.','h','t','m','l','t','e','s','t','.','h','t','m','l',0};
448 static const WCHAR combined_url5[] = {'m','s','-','i','t','s',':',
449     't','e','s','t','.','c','h','m',':',':','/','d','i','r','/','t','e','s','t','.','h','t','m','l',0};
450 static const WCHAR combined_url6[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':',
451     't','e','s','t','.','c','h','m',':',':','/','d','i','r','/','t','e','s','t','.','h','t','m','l',0};
452
453 static const struct {
454     LPCWSTR base_url;
455     LPCWSTR rel_url;
456     DWORD flags;
457     HRESULT hres;
458     LPCWSTR combined_url;
459 } combine_tests[] = {
460     {blank_url1, blank_url1, 0, STG_E_INVALIDNAME, NULL},
461     {blank_url2, blank_url2, 0, STG_E_INVALIDNAME, NULL},
462     {blank_url1, rel_url1, 0, S_OK, combined_url1},
463     {blank_url1, rel_url2, 0, STG_E_INVALIDNAME, NULL},
464     {blank_url1, rel_url3, 0, S_OK, combined_url1},
465     {blank_url1, rel_url4, 0, STG_E_INVALIDNAME, NULL},
466     {blank_url1, rel_url3, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO, S_OK, combined_url1},
467     {blank_url1, rel_url5, 0, S_OK, combined_url2},
468     {rel_url1, rel_url2, 0, 0x80041001, NULL},
469     {base_url1, rel_url1, 0, S_OK, combined_url3},
470     {base_url2, rel_url1, 0, S_OK, combined_url2},
471     {blank_url4, rel_url1, 0, S_OK, combined_url4},
472     {base_url3, rel_url1, 0, S_OK, combined_url5},
473     {base_url4, rel_url1, 0, S_OK, combined_url6},
474     {base_url5, rel_url1, 0, INET_E_USE_DEFAULT_PROTOCOLHANDLER, NULL},
475     {base_url2, rel_url3, 0, S_OK, combined_url1},
476 };
477
478 static void test_its_protocol_info(IInternetProtocol *protocol)
479 {
480     IInternetProtocolInfo *info;
481     WCHAR buf[1024];
482     DWORD size, i;
483     HRESULT hres;
484
485     hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolInfo, (void**)&info);
486     ok(hres == S_OK, "Could not get IInternetProtocolInfo interface: %08x\n", hres);
487     if(FAILED(hres))
488         return;
489
490     for(i = PARSE_CANONICALIZE; i <= PARSE_UNESCAPE; i++) {
491         if(i != PARSE_CANONICALIZE && i != PARSE_SECURITY_URL) {
492             hres = IInternetProtocolInfo_ParseUrl(info, blank_url1, i, 0, buf,
493                     sizeof(buf)/sizeof(buf[0]), &size, 0);
494             ok(hres == INET_E_DEFAULT_ACTION,
495                "[%d] failed: %08x, expected INET_E_DEFAULT_ACTION\n", i, hres);
496         }
497     }
498
499     for(i=0; i < sizeof(combine_tests)/sizeof(combine_tests[0]); i++) {
500         size = 0xdeadbeef;
501         memset(buf, 0xfe, sizeof(buf));
502         hres = IInternetProtocolInfo_CombineUrl(info, combine_tests[i].base_url,
503                 combine_tests[i].rel_url, combine_tests[i].flags, buf,
504                 sizeof(buf)/sizeof(WCHAR), &size, 0);
505         ok(hres == combine_tests[i].hres, "[%d] CombineUrl returned %08x, expected %08x\n",
506            i, hres, combine_tests[i].hres);
507         ok(size == (combine_tests[i].combined_url ? lstrlenW(combine_tests[i].combined_url)+1
508            : 0xdeadbeef), "[%d] unexpected size=%d\n", i, size);
509         if(combine_tests[i].combined_url)
510             ok(!lstrcmpW(combine_tests[i].combined_url, buf), "[%d] unexpected result\n", i);
511         else
512             ok(buf[0] == 0xfefe, "buf changed\n");
513     }
514
515     size = 0xdeadbeef;
516     memset(buf, 0xfe, sizeof(buf));
517     hres = IInternetProtocolInfo_CombineUrl(info, blank_url1, rel_url1, 0, buf,
518             1, &size, 0);
519     ok(hres == E_OUTOFMEMORY, "CombineUrl failed: %08x\n", hres);
520     ok(size == sizeof(combined_url1)/sizeof(WCHAR), "size=%d\n", size);
521     ok(buf[0] == 0xfefe, "buf changed\n");
522
523     IInternetProtocolInfo_Release(info);
524 }
525
526 static void test_its_protocol(void)
527 {
528     IInternetProtocolInfo *info;
529     IClassFactory *factory;
530     IUnknown *unk;
531     ULONG ref;
532     HRESULT hres;
533
534     static const WCHAR wrong_url1[] =
535         {'i','t','s',':','t','e','s','t','.','c','h','m',':',':','/','b','l','a','n','.','h','t','m','l',0};
536     static const WCHAR wrong_url2[] =
537         {'i','t','s',':','t','e','s','.','c','h','m',':',':','b','/','l','a','n','k','.','h','t','m','l',0};
538     static const WCHAR wrong_url3[] =
539         {'i','t','s',':','t','e','s','t','.','c','h','m','/','b','l','a','n','k','.','h','t','m','l',0};
540     static const WCHAR wrong_url4[] = {'m','k',':','@','M','S','I','T','S','t','o','r',':',
541          't','e','s','t','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
542     static const WCHAR wrong_url5[] = {'f','i','l','e',':',
543         't','e','s','.','c','h','m',':',':','/','b','l','a','n','k','.','h','t','m','l',0};
544
545     test_protocol = ITS_PROTOCOL;
546
547     hres = CoGetClassObject(&CLSID_ITSProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
548     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
549     if(!SUCCEEDED(hres))
550         return;
551
552     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&info);
553     ok(hres == E_NOINTERFACE, "Could not get IInternetProtocolInfo: %08x\n", hres);
554
555     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
556     ok(hres == S_OK, "Could not get IClassFactory interface\n");
557     if(SUCCEEDED(hres)) {
558         IInternetProtocol *protocol;
559
560         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
561         ok(hres == S_OK, "Could not get IInternetProtocol: %08x\n", hres);
562         if(SUCCEEDED(hres)) {
563             test_its_protocol_info(protocol);
564
565             test_protocol_fail(protocol, wrong_url1, STG_E_FILENOTFOUND);
566             test_protocol_fail(protocol, wrong_url2, STG_E_FILENOTFOUND);
567             test_protocol_fail(protocol, wrong_url3, STG_E_FILENOTFOUND);
568
569             hres = IInternetProtocol_Start(protocol, wrong_url4, &protocol_sink, &bind_info, 0, 0);
570             ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER,
571                "Start failed: %08x, expected INET_E_USE_DEFAULT_PROTOCOLHANDLER\n", hres);
572
573             hres = IInternetProtocol_Start(protocol, wrong_url5, &protocol_sink, &bind_info, 0, 0);
574             ok(hres == INET_E_USE_DEFAULT_PROTOCOLHANDLER,
575                "Start failed: %08x, expected INET_E_USE_DEFAULT_PROTOCOLHANDLER\n", hres);
576
577             ref = IInternetProtocol_Release(protocol);
578             ok(!ref, "protocol ref=%d\n", ref);
579
580             test_protocol_url(factory, blank_url1, TRUE);
581             test_protocol_url(factory, blank_url2, TRUE);
582             test_protocol_url(factory, blank_url3, TRUE);
583             test_protocol_url(factory, blank_url4, TRUE);
584             test_protocol_url(factory, blank_url5, TRUE);
585             test_protocol_url(factory, blank_url6, TRUE);
586             test_protocol_url(factory, blank_url8, TRUE);
587         }
588
589         IClassFactory_Release(factory);
590     }
591
592     IUnknown_Release(unk);
593 }
594
595 static void test_mk_protocol(void)
596 {
597     IClassFactory *cf;
598     HRESULT hres;
599
600     test_protocol = MK_PROTOCOL;
601
602     hres = CoGetClassObject(&CLSID_MkProtocol, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory,
603                             (void**)&cf);
604     ok(hres == S_OK, "CoGetClassObject failed: %08x\n", hres);
605     if(!SUCCEEDED(hres))
606         return;
607
608     cache_file = cache_file1;
609     test_protocol_url(cf, blank_url3, TRUE);
610     cache_file = cache_file2;
611     test_protocol_url(cf, blank_url7, TRUE);
612     cache_file = cache_file3;
613     test_protocol_url(cf, blank_url8, FALSE);
614
615     IClassFactory_Release(cf);
616 }
617
618 static BOOL create_chm(void)
619 {
620     HANDLE file;
621     HRSRC src;
622     DWORD size;
623
624     file = CreateFileA("test.chm", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
625             FILE_ATTRIBUTE_NORMAL, NULL);
626     ok(file != INVALID_HANDLE_VALUE, "Could not create test.chm file\n");
627     if(file == INVALID_HANDLE_VALUE)
628         return FALSE;
629
630     src = FindResourceA(NULL, MAKEINTRESOURCEA(60), MAKEINTRESOURCEA(60));
631
632     WriteFile(file, LoadResource(NULL, src), SizeofResource(NULL, src), &size, NULL);
633     CloseHandle(file);
634
635     return TRUE;
636 }
637
638 static void delete_chm(void)
639 {
640     BOOL ret;
641
642     ret = DeleteFileA("test.chm");
643     ok(ret, "DeleteFileA failed: %d\n", GetLastError());
644 }
645
646 START_TEST(protocol)
647 {
648     OleInitialize(NULL);
649
650     if(!create_chm())
651         return;
652
653     test_its_protocol();
654     test_mk_protocol();
655
656     delete_chm();
657     OleUninitialize();
658 }