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