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