Move protocol's CLSIDs to urlmon.idl.
[wine] / dlls / urlmon / tests / protocol.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
29 #include "initguid.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     ok(expect_ ##func, "unexpected call\n"); \
39     expect_ ## func = FALSE; \
40     called_ ## func = TRUE
41
42 #define CHECK_EXPECT2(func) \
43     ok(expect_ ##func, "unexpected call\n"); \
44     called_ ## func = TRUE
45
46 #define CHECK_CALLED(func) \
47     ok(called_ ## func, "expected " #func "\n"); \
48     expect_ ## func = called_ ## func = FALSE
49
50 DEFINE_EXPECT(GetBindInfo);
51 DEFINE_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
52 DEFINE_EXPECT(ReportProgress_DIRECTBIND);
53 DEFINE_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
54 DEFINE_EXPECT(ReportData);
55 DEFINE_EXPECT(ReportResult);
56
57 static HRESULT expect_hrResult;
58 static LPCWSTR file_name;
59
60 static HRESULT WINAPI ProtocolSink_QueryInterface(IInternetProtocolSink *iface, REFIID riid, void **ppv)
61 {
62     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
63         *ppv = iface;
64         return S_OK;
65     }
66     return E_NOINTERFACE;
67 }
68
69 static ULONG WINAPI ProtocolSink_AddRef(IInternetProtocolSink *iface)
70 {
71     return 2;
72 }
73
74 static ULONG WINAPI ProtocolSink_Release(IInternetProtocolSink *iface)
75 {
76     return 1;
77 }
78
79 static HRESULT WINAPI ProtocolSink_Switch(IInternetProtocolSink *iface, PROTOCOLDATA *pProtocolData)
80 {
81     ok(0, "unexpected call\n");
82     return E_NOTIMPL;
83 }
84
85 static HRESULT WINAPI ProtocolSink_ReportProgress(IInternetProtocolSink *iface, ULONG ulStatusCode,
86         LPCWSTR szStatusText)
87 {
88     static const WCHAR text_html[] = {'t','e','x','t','/','h','t','m','l',0};
89
90     switch(ulStatusCode) {
91         case BINDSTATUS_MIMETYPEAVAILABLE:
92             CHECK_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
93             if(szStatusText)
94                 ok(!lstrcmpW(szStatusText, text_html), "szStatusText != text/html\n");
95         case BINDSTATUS_DIRECTBIND:
96             CHECK_EXPECT2(ReportProgress_DIRECTBIND);
97             if(szStatusText)
98                 ok(!lstrcmpW(szStatusText, text_html), "szStatusText != text/html\n");
99             break;
100         case BINDSTATUS_CACHEFILENAMEAVAILABLE:
101             CHECK_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
102             if(szStatusText)
103                 ok(!lstrcmpW(szStatusText, file_name), "szStatusText != file_name\n");
104             break;
105     };
106
107     return S_OK;
108 }
109
110 static HRESULT WINAPI ProtocolSink_ReportData(IInternetProtocolSink *iface, DWORD grfBSCF,
111         ULONG ulProgress, ULONG ulProgressMax)
112 {
113     CHECK_EXPECT(ReportData);
114
115     ok(ulProgress == ulProgressMax, "ulProgress != ulProgressMax\n");
116     ok(ulProgressMax == 13, "ulProgressMax=%ld, expected 13\n", ulProgressMax);
117     ok(grfBSCF == (BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION),
118             "grcf = %08lx\n", grfBSCF);
119
120     return S_OK;
121 }
122
123 static HRESULT WINAPI ProtocolSink_ReportResult(IInternetProtocolSink *iface, HRESULT hrResult,
124         DWORD dwError, LPCWSTR szResult)
125 {
126     CHECK_EXPECT(ReportResult);
127
128     ok(hrResult == expect_hrResult, "hrResult = %08lx, expected: %08lx\n",
129             hrResult, expect_hrResult);
130     if(SUCCEEDED(hrResult))
131         ok(dwError == ERROR_SUCCESS, "dwError = %ld, expected ERROR_SUCCESS\n", dwError);
132     else
133         ok(dwError != ERROR_SUCCESS, "dwError == ERROR_SUCCESS\n");
134     ok(!szResult, "szResult != NULL\n");
135
136     return S_OK;
137 }
138
139 static IInternetProtocolSinkVtbl protocol_sink_vtbl = {
140     ProtocolSink_QueryInterface,
141     ProtocolSink_AddRef,
142     ProtocolSink_Release,
143     ProtocolSink_Switch,
144     ProtocolSink_ReportProgress,
145     ProtocolSink_ReportData,
146     ProtocolSink_ReportResult
147 };
148
149 static IInternetProtocolSink protocol_sink = { &protocol_sink_vtbl };
150
151 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface, REFIID riid, void **ppv)
152 {
153     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetBindInfo, riid)) {
154         *ppv = iface;
155         return S_OK;
156     }
157     return E_NOINTERFACE;
158 }
159
160 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
161 {
162     return 2;
163 }
164
165 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
166 {
167     return 1;
168 }
169
170 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
171 {
172     CHECK_EXPECT(GetBindInfo);
173
174     ok(grfBINDF != NULL, "grfBINDF == NULL\n");
175     if(grfBINDF)
176         ok(!*grfBINDF, "*grfBINDF != 0\n");
177     ok(pbindinfo != NULL, "pbindinfo == NULL\n");
178     ok(pbindinfo->cbSize == sizeof(BINDINFO), "wrong size of pbindinfo: %ld\n", pbindinfo->cbSize);
179
180     return S_OK;
181 }
182
183 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface, ULONG ulStringType,
184         LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
185 {
186     ok(0, "unexpected call\n");
187     return E_NOTIMPL;
188 }
189
190 static IInternetBindInfoVtbl bind_info_vtbl = {
191     BindInfo_QueryInterface,
192     BindInfo_AddRef,
193     BindInfo_Release,
194     BindInfo_GetBindInfo,
195     BindInfo_GetBindString
196 };
197
198 static IInternetBindInfo bind_info = { &bind_info_vtbl };
199
200 static void file_protocol_start(IInternetProtocol *protocol, LPCWSTR url, BOOL is_first)
201 {
202     HRESULT hres;
203
204     SET_EXPECT(GetBindInfo);
205     SET_EXPECT(ReportProgress_DIRECTBIND);
206     if(is_first) {
207         SET_EXPECT(ReportProgress_CACHEFILENAMEAVAILABLE);
208         SET_EXPECT(ReportProgress_MIMETYPEAVAILABLE);
209         SET_EXPECT(ReportResult);
210     }
211     SET_EXPECT(ReportData);
212     expect_hrResult = S_OK;
213
214     hres = IInternetProtocol_Start(protocol, url, &protocol_sink, &bind_info, 0, 0);
215     ok(hres == S_OK, "Start failed: %08lx\n", hres);
216
217     CHECK_CALLED(GetBindInfo);
218     todo_wine { CHECK_CALLED(ReportProgress_DIRECTBIND); }
219     if(is_first) {
220         CHECK_CALLED(ReportProgress_CACHEFILENAMEAVAILABLE);
221         todo_wine { CHECK_CALLED(ReportProgress_MIMETYPEAVAILABLE); }
222         CHECK_CALLED(ReportResult);
223     }
224     CHECK_CALLED(ReportData);
225 }
226
227 static void test_file_protocol_url(LPCWSTR url)
228 {
229     IInternetProtocolInfo *protocol_info;
230     IUnknown *unk;
231     IClassFactory *factory;
232     HRESULT hres;
233
234     hres = CoGetClassObject(&CLSID_FileProtocol, CLSCTX_INPROC_SERVER, NULL,
235             &IID_IUnknown, (void**)&unk);
236     ok(hres == S_OK, "CoGetClassObject failed: %08lx\n", hres);
237     if(!SUCCEEDED(hres))
238         return;
239
240     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&protocol_info);
241     ok(hres == E_NOINTERFACE,
242             "Could not get IInternetProtocolInfo interface: %08lx, expected E_NOINTERFACE\n", hres);
243
244     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)&factory);
245     ok(hres == S_OK, "Could not get IClassFactory interface\n");
246     if(SUCCEEDED(hres)) {
247         IInternetProtocol *protocol;
248         BYTE buf[512];
249         ULONG cb;
250         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
251         ok(hres == S_OK, "Could not get IInternetProtocol: %08lx\n", hres);
252
253         if(SUCCEEDED(hres)) {
254             file_protocol_start(protocol, url, TRUE);
255             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
256             ok(hres == S_OK, "Read failed: %08lx\n", hres);
257             ok(cb == 2, "cb=%lu expected 2\n", cb);
258             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
259             ok(hres == S_FALSE, "Read failed: %08lx\n", hres);
260             hres = IInternetProtocol_Read(protocol, buf, sizeof(buf), &cb);
261             ok(hres == S_FALSE, "Read failed: %08lx expected S_FALSE\n", hres);
262             ok(cb == 0, "cb=%lu expected 0\n", cb);
263             hres = IInternetProtocol_UnlockRequest(protocol);
264             ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
265
266             file_protocol_start(protocol, url, FALSE);
267             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
268             ok(hres == S_FALSE, "Read failed: %08lx\n", hres);
269             hres = IInternetProtocol_LockRequest(protocol, 0);
270             ok(hres == S_OK, "LockRequest failed: %08lx\n", hres);
271             hres = IInternetProtocol_UnlockRequest(protocol);
272             ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
273
274             IInternetProtocol_Release(protocol);
275         }
276
277         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
278         ok(hres == S_OK, "Could not get IInternetProtocol: %08lx\n", hres);
279
280         if(SUCCEEDED(hres)) {
281             file_protocol_start(protocol, url, TRUE);
282             hres = IInternetProtocol_LockRequest(protocol, 0);
283             ok(hres == S_OK, "LockRequest failed: %08lx\n", hres);
284             hres = IInternetProtocol_Terminate(protocol, 0);
285             ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
286             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
287             ok(hres == S_OK, "Read failed: %08lx\n\n", hres);
288             hres = IInternetProtocol_UnlockRequest(protocol);
289             ok(hres == S_OK, "UnlockRequest failed: %08lx\n", hres);
290             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
291             ok(hres == S_OK, "Read failed: %08lx\n", hres);
292             hres = IInternetProtocol_Terminate(protocol, 0);
293             ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
294
295             IInternetProtocol_Release(protocol);
296         }
297
298         hres = IClassFactory_CreateInstance(factory, NULL, &IID_IInternetProtocol, (void**)&protocol);
299         ok(hres == S_OK, "Could not get IInternetProtocol: %08lx\n", hres);
300
301         if(SUCCEEDED(hres)) {
302             file_protocol_start(protocol, url, TRUE);
303             hres = IInternetProtocol_Terminate(protocol, 0);
304             ok(hres == S_OK, "Terminate failed: %08lx\n", hres);
305             hres = IInternetProtocol_Read(protocol, buf, 2, &cb);
306             ok(hres == S_OK, "Read failed: %08lx\n", hres);
307             ok(cb == 2, "cb=%lu expected 2\n", cb);
308
309             IInternetProtocol_Release(protocol);
310         }
311
312         IClassFactory_Release(factory);
313     }
314
315     IUnknown_Release(unk);
316 }
317
318 static void test_file_protocol(void) {
319     IInternetProtocol *protocol;
320     WCHAR buf[MAX_PATH];
321     ULONG len;
322     HANDLE file;
323     HRESULT hres;
324
325     static const WCHAR index_url[] =
326         {'f','i','l','e',':','i','n','d','e','x','.','h','t','m','l',0};
327     static const WCHAR index_url2[] =
328         {'f','i','l','e',':','/','/','i','n','d','e','x','.','h','t','m','l',0};
329     static const WCHAR wszFile[] = {'f','i','l','e',':',0};
330     static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
331     static const char html_doc[] = "<HTML></HTML>";
332
333     file = CreateFileW(wszIndexHtml, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
334             FILE_ATTRIBUTE_NORMAL, NULL);
335     ok(file != NULL, "CreateFile failed\n");
336     if(!file)
337         return;
338     WriteFile(file, html_doc, sizeof(html_doc)-1, NULL, NULL);
339     CloseHandle(file);
340
341     file_name = wszIndexHtml;
342     test_file_protocol_url(index_url);
343
344     memcpy(buf, wszFile, sizeof(wszFile));
345     len = sizeof(wszFile)/sizeof(WCHAR)-1;
346     len += GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR)-len, buf+len);
347     buf[len++] = '\\';
348     memcpy(buf+len, wszIndexHtml, sizeof(wszIndexHtml));
349
350     file_name = buf + sizeof(wszFile)/sizeof(WCHAR)-1;
351     test_file_protocol_url(buf);
352
353     DeleteFileW(wszIndexHtml);
354
355     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
356             &IID_IInternetProtocol, (void**)&protocol);
357     ok(hres == S_OK, "CoCreateInstance failed: %08lx\n", hres);
358     if(FAILED(hres))
359         return;
360
361     SET_EXPECT(GetBindInfo);
362     expect_hrResult = MK_E_SYNTAX;
363     hres = IInternetProtocol_Start(protocol, wszIndexHtml, &protocol_sink, &bind_info, 0, 0);
364     ok(hres == MK_E_SYNTAX, "Start failed: %08lx, expected MK_E_SYNTAX\n", hres);
365     CHECK_CALLED(GetBindInfo);
366
367     SET_EXPECT(GetBindInfo);
368     SET_EXPECT(ReportProgress_DIRECTBIND);
369     SET_EXPECT(ReportResult);
370     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
371     hres = IInternetProtocol_Start(protocol, index_url, &protocol_sink, &bind_info, 0, 0);
372     ok(hres == INET_E_RESOURCE_NOT_FOUND,
373             "Start failed: %08lx expected INET_E_RESOURCE_NOT_FOUND\n", hres);
374     CHECK_CALLED(GetBindInfo);
375     todo_wine { CHECK_CALLED(ReportProgress_DIRECTBIND); }
376     CHECK_CALLED(ReportResult);
377
378     IInternetProtocol_Release(protocol);
379
380     hres = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
381             &IID_IInternetProtocol, (void**)&protocol);
382     ok(hres == S_OK, "CoCreateInstance failed: %08lx\n", hres);
383     if(FAILED(hres))
384         return;
385
386     SET_EXPECT(GetBindInfo);
387     SET_EXPECT(ReportProgress_DIRECTBIND);
388     SET_EXPECT(ReportResult);
389     expect_hrResult = INET_E_RESOURCE_NOT_FOUND;
390     hres = IInternetProtocol_Start(protocol, index_url2, &protocol_sink, &bind_info, 0, 0);
391     ok(hres == INET_E_RESOURCE_NOT_FOUND,
392             "Start failed: %08lx, expected INET_E_RESOURCE_NOT_FOUND\n", hres);
393     CHECK_CALLED(GetBindInfo);
394     todo_wine { CHECK_CALLED(ReportProgress_DIRECTBIND); }
395     CHECK_CALLED(ReportResult);
396
397     IInternetProtocol_Release(protocol);
398 }
399
400 START_TEST(protocol)
401 {
402     OleInitialize(NULL);
403
404     test_file_protocol();
405
406     OleUninitialize();
407 }