[URLMON_WINETEST]
[reactos.git] / rostests / winetests / urlmon / stream.c
1 /*
2 * Copyright 2007 Robert Shearman 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 #define CONST_VTABLE
21
22 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stddef.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "ole2.h"
29 #include "urlmon.h"
30 #include "wininet.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 CHECK_NOT_CALLED(func) \
58 do { \
59 ok(!called_ ## func, "unexpected " #func "\n"); \
60 expect_ ## func = called_ ## func = FALSE; \
61 }while(0)
62
63 #define CLEAR_CALLED(func) \
64 expect_ ## func = called_ ## func = FALSE
65
66 DEFINE_EXPECT(QueryInterface_IServiceProvider);
67 DEFINE_EXPECT(OnStartBinding);
68 DEFINE_EXPECT(OnProgress_FINDINGRESOURCE);
69 DEFINE_EXPECT(OnProgress_CONNECTING);
70 DEFINE_EXPECT(OnProgress_SENDINGREQUEST);
71 DEFINE_EXPECT(OnProgress_MIMETYPEAVAILABLE);
72 DEFINE_EXPECT(OnProgress_BEGINDOWNLOADDATA);
73 DEFINE_EXPECT(OnProgress_DOWNLOADINGDATA);
74 DEFINE_EXPECT(OnProgress_ENDDOWNLOADDATA);
75 DEFINE_EXPECT(OnStopBinding);
76 DEFINE_EXPECT(OnDataAvailable);
77 DEFINE_EXPECT(GetBindInfo);
78
79 static const CHAR wszIndexHtmlA[] = "index.html";
80 static const WCHAR wszIndexHtml[] = {'i','n','d','e','x','.','h','t','m','l',0};
81 static WCHAR INDEX_HTML[MAX_PATH];
82 static const char szHtmlDoc[] = "<HTML></HTML>";
83
84 static HRESULT WINAPI statusclb_QueryInterface(IBindStatusCallback *iface, REFIID riid, void **ppv)
85 {
86 if (IsEqualGUID(&IID_IBindStatusCallback, riid) ||
87 IsEqualGUID(&IID_IUnknown, riid))
88 {
89 *ppv = iface;
90 return S_OK;
91 }
92 else if (IsEqualGUID(&IID_IServiceProvider, riid))
93 {
94 CHECK_EXPECT(QueryInterface_IServiceProvider);
95 }
96
97 return E_NOINTERFACE;
98 }
99
100 static ULONG WINAPI statusclb_AddRef(IBindStatusCallback *iface)
101 {
102 return 2;
103 }
104
105 static ULONG WINAPI statusclb_Release(IBindStatusCallback *iface)
106 {
107 return 1;
108 }
109
110 static HRESULT WINAPI statusclb_OnStartBinding(IBindStatusCallback *iface, DWORD dwReserved,
111 IBinding *pib)
112 {
113 HRESULT hres;
114 IMoniker *mon;
115
116 CHECK_EXPECT(OnStartBinding);
117
118 ok(pib != NULL, "pib should not be NULL\n");
119
120 hres = IBinding_QueryInterface(pib, &IID_IMoniker, (void**)&mon);
121 ok(hres == E_NOINTERFACE, "IBinding should not have IMoniker interface\n");
122 if(SUCCEEDED(hres))
123 IMoniker_Release(mon);
124
125 return S_OK;
126 }
127
128 static HRESULT WINAPI statusclb_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
129 {
130 ok(0, "unexpected call\n");
131 return E_NOTIMPL;
132 }
133
134 static HRESULT WINAPI statusclb_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
135 {
136 ok(0, "unexpected call\n");
137 return E_NOTIMPL;
138 }
139
140 static HRESULT WINAPI statusclb_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
141 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
142 {
143 switch(ulStatusCode) {
144 case BINDSTATUS_FINDINGRESOURCE:
145 CHECK_EXPECT(OnProgress_FINDINGRESOURCE);
146 break;
147 case BINDSTATUS_CONNECTING:
148 CHECK_EXPECT(OnProgress_CONNECTING);
149 break;
150 case BINDSTATUS_SENDINGREQUEST:
151 CHECK_EXPECT(OnProgress_SENDINGREQUEST);
152 break;
153 case BINDSTATUS_MIMETYPEAVAILABLE:
154 CHECK_EXPECT(OnProgress_MIMETYPEAVAILABLE);
155 break;
156 case BINDSTATUS_BEGINDOWNLOADDATA:
157 CHECK_EXPECT(OnProgress_BEGINDOWNLOADDATA);
158 ok(szStatusText != NULL, "szStatusText == NULL\n");
159 break;
160 case BINDSTATUS_DOWNLOADINGDATA:
161 CHECK_EXPECT2(OnProgress_DOWNLOADINGDATA);
162 break;
163 case BINDSTATUS_ENDDOWNLOADDATA:
164 CHECK_EXPECT(OnProgress_ENDDOWNLOADDATA);
165 ok(szStatusText != NULL, "szStatusText == NULL\n");
166 break;
167 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
168 ok(szStatusText != NULL, "szStatusText == NULL\n");
169 break;
170 default:
171 todo_wine { ok(0, "unexpected code %d\n", ulStatusCode); }
172 };
173 return S_OK;
174 }
175
176 static HRESULT WINAPI statusclb_OnStopBinding(IBindStatusCallback *iface, HRESULT hresult, LPCWSTR szError)
177 {
178 CHECK_EXPECT(OnStopBinding);
179
180 /* ignore DNS failure */
181 if (hresult != HRESULT_FROM_WIN32(ERROR_INTERNET_NAME_NOT_RESOLVED))
182 {
183 ok(SUCCEEDED(hresult), "Download failed: %08x\n", hresult);
184 ok(szError == NULL, "szError should be NULL\n");
185 }
186
187 return S_OK;
188 }
189
190 static HRESULT WINAPI statusclb_GetBindInfo(IBindStatusCallback *iface, DWORD *grfBINDF, BINDINFO *pbindinfo)
191 {
192 DWORD cbSize;
193
194 CHECK_EXPECT(GetBindInfo);
195
196 *grfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
197 cbSize = pbindinfo->cbSize;
198 memset(pbindinfo, 0, cbSize);
199 pbindinfo->cbSize = cbSize;
200
201 return S_OK;
202 }
203
204 static HRESULT WINAPI statusclb_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
205 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
206 {
207 HRESULT hres;
208 DWORD readed;
209 BYTE buf[512];
210
211 CHECK_EXPECT2(OnDataAvailable);
212
213 if (0)
214 {
215 /* FIXME: Uncomment after removing BindToStorage hack. */
216 ok(pformatetc != NULL, "pformatetx == NULL\n");
217 if(pformatetc) {
218 ok(pformatetc->cfFormat == 0xc02d, "clipformat=%x\n", pformatetc->cfFormat);
219 ok(pformatetc->ptd == NULL, "ptd = %p\n", pformatetc->ptd);
220 ok(pformatetc->dwAspect == 1, "dwAspect=%u\n", pformatetc->dwAspect);
221 ok(pformatetc->lindex == -1, "lindex=%d\n", pformatetc->lindex);
222 ok(pformatetc->tymed == TYMED_ISTREAM, "tymed=%u\n", pformatetc->tymed);
223 }
224
225 ok(pstgmed != NULL, "stgmeg == NULL\n");
226 if(pstgmed) {
227 ok(pstgmed->tymed == TYMED_ISTREAM, "tymed=%u\n", pstgmed->tymed);
228 ok(U(*pstgmed).pstm != NULL, "pstm == NULL\n");
229 ok(pstgmed->pUnkForRelease != NULL, "pUnkForRelease == NULL\n");
230 }
231 }
232
233 if(U(*pstgmed).pstm) {
234 do hres = IStream_Read(U(*pstgmed).pstm, buf, 512, &readed);
235 while(hres == S_OK);
236 ok(hres == S_FALSE || hres == E_PENDING, "IStream_Read returned %08x\n", hres);
237 }
238
239 return S_OK;
240 }
241
242 static HRESULT WINAPI statusclb_OnObjectAvailable(IBindStatusCallback *iface, REFIID riid, IUnknown *punk)
243 {
244 ok(0, "unexpected call\n");
245 return E_NOTIMPL;
246 }
247
248 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
249 statusclb_QueryInterface,
250 statusclb_AddRef,
251 statusclb_Release,
252 statusclb_OnStartBinding,
253 statusclb_GetPriority,
254 statusclb_OnLowResource,
255 statusclb_OnProgress,
256 statusclb_OnStopBinding,
257 statusclb_GetBindInfo,
258 statusclb_OnDataAvailable,
259 statusclb_OnObjectAvailable
260 };
261
262 static IBindStatusCallback BindStatusCallback = { &BindStatusCallbackVtbl };
263
264 static void set_file_url(char *path)
265 {
266 char INDEX_HTML_A[MAX_PATH];
267
268 lstrcpyA(INDEX_HTML_A, "file:///");
269 lstrcatA(INDEX_HTML_A, path);
270 MultiByteToWideChar(CP_ACP, 0, INDEX_HTML_A, -1, INDEX_HTML, MAX_PATH);
271 }
272
273 static void create_file(void)
274 {
275 HANDLE file;
276 DWORD size;
277 CHAR path[MAX_PATH];
278
279 file = CreateFileA(wszIndexHtmlA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
280 FILE_ATTRIBUTE_NORMAL, NULL);
281 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
282 if(file == INVALID_HANDLE_VALUE)
283 return;
284
285 WriteFile(file, szHtmlDoc, sizeof(szHtmlDoc)-1, &size, NULL);
286 CloseHandle(file);
287
288 GetCurrentDirectoryA(MAX_PATH, path);
289 lstrcatA(path, "\\");
290 lstrcatA(path, wszIndexHtmlA);
291 set_file_url(path);
292 }
293
294 static void test_URLOpenBlockingStreamW(void)
295 {
296 HRESULT hr;
297 IStream *pStream = NULL;
298 char buffer[256];
299
300 hr = URLOpenBlockingStreamW(NULL, NULL, &pStream, 0, &BindStatusCallback);
301 ok(hr == E_INVALIDARG, "URLOpenBlockingStreamW should have failed with E_INVALIDARG instead of 0x%08x\n", hr);
302 if (0) /* crashes on Win2k */
303 {
304 hr = URLOpenBlockingStreamW(NULL, INDEX_HTML, NULL, 0, &BindStatusCallback);
305 ok(hr == E_INVALIDARG, "URLOpenBlockingStreamW should have failed with E_INVALIDARG instead of 0x%08x\n", hr);
306 }
307
308 SET_EXPECT(GetBindInfo);
309 SET_EXPECT(QueryInterface_IServiceProvider);
310 SET_EXPECT(OnStartBinding);
311 SET_EXPECT(OnProgress_SENDINGREQUEST);
312 SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
313 SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
314 SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
315 SET_EXPECT(OnStopBinding);
316
317 hr = URLOpenBlockingStreamW(NULL, INDEX_HTML, &pStream, 0, &BindStatusCallback);
318 ok(hr == S_OK, "URLOpenBlockingStreamW failed with error 0x%08x\n", hr);
319
320 CHECK_CALLED(GetBindInfo);
321 todo_wine CHECK_CALLED(QueryInterface_IServiceProvider);
322 CHECK_CALLED(OnStartBinding);
323 CHECK_CALLED(OnProgress_SENDINGREQUEST);
324 CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
325 CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
326 CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
327 CHECK_CALLED(OnStopBinding);
328
329 ok(pStream != NULL, "pStream is NULL\n");
330 if(pStream)
331 {
332 hr = IStream_Read(pStream, buffer, sizeof(buffer), NULL);
333 ok(hr == S_OK, "IStream_Read failed with error 0x%08x\n", hr);
334 ok(!memcmp(buffer, szHtmlDoc, sizeof(szHtmlDoc)-1), "read data differs from file\n");
335
336 IStream_Release(pStream);
337 }
338 }
339
340 static void test_URLOpenStreamW(void)
341 {
342 HRESULT hr;
343
344 hr = URLOpenStreamW(NULL, NULL, 0, &BindStatusCallback);
345 ok(hr == E_INVALIDARG, "URLOpenStreamW should have failed with E_INVALIDARG instead of 0x%08x\n", hr);
346
347 SET_EXPECT(GetBindInfo);
348 SET_EXPECT(QueryInterface_IServiceProvider);
349 SET_EXPECT(OnStartBinding);
350 SET_EXPECT(OnProgress_SENDINGREQUEST);
351 SET_EXPECT(OnProgress_MIMETYPEAVAILABLE);
352 SET_EXPECT(OnProgress_BEGINDOWNLOADDATA);
353 SET_EXPECT(OnProgress_ENDDOWNLOADDATA);
354 SET_EXPECT(OnDataAvailable);
355 SET_EXPECT(OnStopBinding);
356
357 hr = URLOpenStreamW(NULL, INDEX_HTML, 0, &BindStatusCallback);
358 ok(hr == S_OK, "URLOpenStreamW failed with error 0x%08x\n", hr);
359
360 CHECK_CALLED(GetBindInfo);
361 todo_wine CHECK_CALLED(QueryInterface_IServiceProvider);
362 CHECK_CALLED(OnStartBinding);
363 CHECK_CALLED(OnProgress_SENDINGREQUEST);
364 CHECK_CALLED(OnProgress_MIMETYPEAVAILABLE);
365 CHECK_CALLED(OnProgress_BEGINDOWNLOADDATA);
366 CHECK_CALLED(OnProgress_ENDDOWNLOADDATA);
367 CHECK_CALLED(OnDataAvailable);
368 CHECK_CALLED(OnStopBinding);
369 }
370
371 START_TEST(stream)
372 {
373 create_file();
374 test_URLOpenBlockingStreamW();
375 test_URLOpenStreamW();
376 DeleteFileA(wszIndexHtmlA);
377 }