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