ole32: Use the cached information in BlockChainStream_GetCount.
[wine/multimedia.git] / dlls / ole32 / tests / hglobalstream.c
blob5632b24ae4407de0d0f75d32a5bbb44b939f19dd
1 /*
2 * Stream on HGLOBAL Tests
4 * Copyright 2006 Robert Shearman (for CodeWeavers)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
29 #include "wine/test.h"
31 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
33 static char const * const *expected_method_list;
35 #define CHECK_EXPECTED_METHOD(method_name) \
36 do { \
37 ok(*expected_method_list != NULL, "Extra method %s called\n", method_name); \
38 if (*expected_method_list) \
39 { \
40 ok(!strcmp(*expected_method_list, method_name), "Expected %s to be called instead of %s\n", \
41 *expected_method_list, method_name); \
42 expected_method_list++; \
43 } \
44 } while(0)
46 static void test_streamonhglobal(IStream *pStream)
48 const char data[] = "Test String";
49 ULARGE_INTEGER ull;
50 LARGE_INTEGER ll;
51 char buffer[128];
52 ULONG read;
53 STATSTG statstg;
54 HRESULT hr;
56 ull.QuadPart = sizeof(data);
57 hr = IStream_SetSize(pStream, ull);
58 ok_ole_success(hr, "IStream_SetSize");
60 hr = IStream_Write(pStream, data, sizeof(data), NULL);
61 ok_ole_success(hr, "IStream_Write");
63 ll.QuadPart = 0;
64 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, NULL);
65 ok_ole_success(hr, "IStream_Seek");
67 /* should return S_OK, not S_FALSE */
68 hr = IStream_Read(pStream, buffer, sizeof(buffer), &read);
69 ok_ole_success(hr, "IStream_Read");
70 ok(read == sizeof(data), "IStream_Read returned read %d\n", read);
72 /* ignores HighPart */
73 ull.u.HighPart = -1;
74 ull.u.LowPart = 0;
75 hr = IStream_SetSize(pStream, ull);
76 ok_ole_success(hr, "IStream_SetSize");
78 /* IStream_Seek -- NULL position argument */
79 ll.u.HighPart = 0;
80 ll.u.LowPart = 0;
81 hr = IStream_Seek(pStream, ll, STREAM_SEEK_CUR, NULL);
82 ok_ole_success(hr, "IStream_Seek");
84 /* IStream_Seek -- valid position argument (seek from current position) */
85 ull.u.HighPart = 0xCAFECAFE;
86 ull.u.LowPart = 0xCAFECAFE;
87 ll.u.HighPart = 0;
88 ll.u.LowPart = 0;
89 hr = IStream_Seek(pStream, ll, STREAM_SEEK_CUR, &ull);
90 ok_ole_success(hr, "IStream_Seek");
91 ok(ull.u.LowPart == sizeof(data), "LowPart set to %d\n", ull.u.LowPart);
92 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
94 /* IStream_Seek -- invalid seek argument */
95 ull.u.HighPart = 0xCAFECAFE;
96 ull.u.LowPart = 0xCAFECAFE;
97 ll.u.HighPart = 0;
98 ll.u.LowPart = 123;
99 hr = IStream_Seek(pStream, ll, STREAM_SEEK_END+1, &ull);
100 ok(hr == STG_E_SEEKERROR, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr);
101 ok(ull.u.LowPart == sizeof(data), "LowPart set to %d\n", ull.u.LowPart);
102 ok(ull.u.HighPart == 0, "should not have changed HighPart, got %d\n", ull.u.HighPart);
104 /* IStream_Seek -- valid position argument (seek to beginning) */
105 ull.u.HighPart = 0xCAFECAFE;
106 ull.u.LowPart = 0xCAFECAFE;
107 ll.u.HighPart = 0;
108 ll.u.LowPart = 0;
109 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
110 ok_ole_success(hr, "IStream_Seek");
111 ok(ull.u.LowPart == 0, "should have set LowPart to 0 instead of %d\n", ull.u.LowPart);
112 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
114 /* IStream_Seek -- valid position argument (seek to end) */
115 ull.u.HighPart = 0xCAFECAFE;
116 ull.u.LowPart = 0xCAFECAFE;
117 ll.u.HighPart = 0;
118 ll.u.LowPart = 0;
119 hr = IStream_Seek(pStream, ll, STREAM_SEEK_END, &ull);
120 ok_ole_success(hr, "IStream_Seek");
121 ok(ull.u.LowPart == 0, "should have set LowPart to 0 instead of %d\n", ull.u.LowPart);
122 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
124 /* IStream_Seek -- ignore HighPart in the move value (seek from current position) */
125 ll.u.HighPart = 0;
126 ll.u.LowPart = sizeof(data);
127 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
128 ok_ole_success(hr, "IStream_Seek");
130 ull.u.HighPart = 0xCAFECAFE;
131 ull.u.LowPart = 0xCAFECAFE;
132 ll.u.HighPart = -1;
133 ll.u.LowPart = 0;
134 hr = IStream_Seek(pStream, ll, STREAM_SEEK_CUR, &ull);
135 ok_ole_success(hr, "IStream_Seek");
136 ok(ull.u.LowPart == sizeof(data), "LowPart set to %d\n", ull.u.LowPart);
137 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
139 /* IStream_Seek -- ignore HighPart in the move value (seek to beginning) */
140 ll.u.HighPart = 0;
141 ll.u.LowPart = sizeof(data);
142 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
143 ok_ole_success(hr, "IStream_Seek");
145 ull.u.HighPart = 0xCAFECAFE;
146 ull.u.LowPart = 0xCAFECAFE;
147 ll.u.HighPart = -1;
148 ll.u.LowPart = 0;
149 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
150 ok_ole_success(hr, "IStream_Seek");
151 ok(ull.u.LowPart == 0, "should have set LowPart to 0 instead of %d\n", ull.u.LowPart);
152 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
154 /* IStream_Seek -- invalid LowPart value (seek from current position) */
155 ll.u.HighPart = 0;
156 ll.u.LowPart = sizeof(data);
157 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
158 ok_ole_success(hr, "IStream_Seek");
160 ull.u.HighPart = 0xCAFECAFE;
161 ull.u.LowPart = 0xCAFECAFE;
162 ll.u.HighPart = 0;
163 ll.u.LowPart = 0x80000000;
164 hr = IStream_Seek(pStream, ll, STREAM_SEEK_CUR, &ull);
165 ok(hr == STG_E_SEEKERROR, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr);
166 ok(ull.u.LowPart == sizeof(data), "LowPart set to %d\n", ull.u.LowPart);
167 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
169 /* IStream_Seek -- invalid LowPart value (seek to beginning) */
170 ll.u.HighPart = 0;
171 ll.u.LowPart = sizeof(data);
172 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
173 ok_ole_success(hr, "IStream_Seek");
175 ull.u.HighPart = 0xCAFECAFE;
176 ull.u.LowPart = 0xCAFECAFE;
177 ll.u.HighPart = 0;
178 ll.u.LowPart = 0x80000000;
179 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
180 ok(hr == STG_E_SEEKERROR, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr);
181 ok(ull.u.LowPart == sizeof(data), "LowPart set to %d\n", ull.u.LowPart);
182 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
184 /* IStream_Seek -- valid LowPart value (seek to beginning) */
185 ull.u.HighPart = 0xCAFECAFE;
186 ull.u.LowPart = 0xCAFECAFE;
187 ll.u.HighPart = 0;
188 ll.u.LowPart = 0x7FFFFFFF;
189 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
190 ok_ole_success(hr, "IStream_Seek");
191 ok(ull.u.LowPart == 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull.u.LowPart);
192 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
194 /* IStream_Seek -- valid LowPart value (seek from current position) */
195 ll.u.HighPart = 0;
196 ll.u.LowPart = 0;
197 hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, &ull);
198 ok_ole_success(hr, "IStream_Seek");
200 ull.u.HighPart = 0xCAFECAFE;
201 ull.u.LowPart = 0xCAFECAFE;
202 ll.u.HighPart = 0;
203 ll.u.LowPart = 0x7FFFFFFF;
204 hr = IStream_Seek(pStream, ll, STREAM_SEEK_CUR, &ull);
205 ok_ole_success(hr, "IStream_Seek");
206 ok(ull.u.LowPart == 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull.u.LowPart);
207 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
209 /* IStream_Seek -- second seek allows you to go past 0x7FFFFFFF size */
210 ull.u.HighPart = 0xCAFECAFE;
211 ull.u.LowPart = 0xCAFECAFE;
212 ll.u.HighPart = 0;
213 ll.u.LowPart = 9;
214 hr = IStream_Seek(pStream, ll, STREAM_SEEK_CUR, &ull);
215 ok_ole_success(hr, "IStream_Seek");
216 ok(ull.u.LowPart == 0x80000008, "should have set LowPart to 0x80000008 instead of %08x\n", ull.u.LowPart);
217 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
219 /* IStream_Seek -- seek wraps position/size on integer overflow */
220 ull.u.HighPart = 0xCAFECAFE;
221 ull.u.LowPart = 0xCAFECAFE;
222 ll.u.HighPart = 0;
223 ll.u.LowPart = 0x7FFFFFFF;
224 hr = IStream_Seek(pStream, ll, STREAM_SEEK_CUR, &ull);
225 ok_ole_success(hr, "IStream_Seek");
226 ok(ull.u.LowPart == 0x00000007, "should have set LowPart to 0x00000007 instead of %08x\n", ull.u.LowPart);
227 ok(ull.u.HighPart == 0, "should have set HighPart to 0 instead of %d\n", ull.u.HighPart);
229 hr = IStream_Commit(pStream, STGC_DEFAULT);
230 ok_ole_success(hr, "IStream_Commit");
232 hr = IStream_Revert(pStream);
233 ok_ole_success(hr, "IStream_Revert");
235 hr = IStream_LockRegion(pStream, ull, ull, LOCK_WRITE);
236 ok(hr == STG_E_INVALIDFUNCTION, "IStream_LockRegion should have returned STG_E_INVALIDFUNCTION instead of 0x%08x\n", hr);
238 hr = IStream_Stat(pStream, &statstg, STATFLAG_DEFAULT);
239 ok_ole_success(hr, "IStream_Stat");
240 ok(statstg.type == STGTY_STREAM, "statstg.type should have been STGTY_STREAM instead of %d\n", statstg.type);
242 /* test OOM condition */
243 ull.u.HighPart = -1;
244 ull.u.LowPart = -1;
245 hr = IStream_SetSize(pStream, ull);
246 ok(hr == E_OUTOFMEMORY || broken(hr == S_OK), /* win9x */
247 "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr);
250 static HRESULT WINAPI TestStream_QueryInterface(IStream *iface, REFIID riid, void **ppv)
252 if (IsEqualIID(riid, &IID_IUnknown) ||
253 IsEqualIID(riid, &IID_ISequentialStream) ||
254 IsEqualIID(riid, &IID_IStream))
256 *ppv = iface;
257 IUnknown_AddRef(iface);
258 return S_OK;
260 *ppv = NULL;
261 return E_NOINTERFACE;
264 static ULONG WINAPI TestStream_AddRef(IStream *iface)
266 return 2;
269 static ULONG WINAPI TestStream_Release(IStream *iface)
271 return 1;
274 static HRESULT WINAPI TestStream_Read(IStream *iface, void *pv, ULONG cb, ULONG *pcbRead)
276 CHECK_EXPECTED_METHOD("TestStream_Read");
277 return E_NOTIMPL;
280 static HRESULT WINAPI TestStream_Write(IStream *iface, const void *pv, ULONG cb, ULONG *pcbWritten)
282 CHECK_EXPECTED_METHOD("TestStream_Write");
283 *pcbWritten = 5;
284 return S_OK;
287 static HRESULT WINAPI TestStream_Seek(IStream *iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
289 CHECK_EXPECTED_METHOD("TestStream_Seek");
290 return E_NOTIMPL;
293 static HRESULT WINAPI TestStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
295 CHECK_EXPECTED_METHOD("TestStream_SetSize");
296 return E_NOTIMPL;
299 static HRESULT WINAPI TestStream_CopyTo(IStream *iface, IStream *pStream, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
301 CHECK_EXPECTED_METHOD("TestStream_CopyTo");
302 return E_NOTIMPL;
305 static HRESULT WINAPI TestStream_Commit(IStream *iface, DWORD grfCommitFlags)
307 CHECK_EXPECTED_METHOD("TestStream_Commit");
308 return E_NOTIMPL;
311 static HRESULT WINAPI TestStream_Revert(IStream *iface)
313 CHECK_EXPECTED_METHOD("TestStream_Revert");
314 return E_NOTIMPL;
317 static HRESULT WINAPI TestStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
319 CHECK_EXPECTED_METHOD("TestStream_LockRegion");
320 return E_NOTIMPL;
323 static HRESULT WINAPI TestStream_UnlockRegion(IStream *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
325 CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
326 return E_NOTIMPL;
329 static HRESULT WINAPI TestStream_Stat(IStream *iface, STATSTG *pstatstg, DWORD grfStatFlag)
331 CHECK_EXPECTED_METHOD("TestStream_Stat");
332 return E_NOTIMPL;
335 static HRESULT WINAPI TestStream_Clone(IStream *iface, IStream **pStream)
337 CHECK_EXPECTED_METHOD("TestStream_Clone");
338 return E_NOTIMPL;
341 static /*const*/ IStreamVtbl StreamVtbl =
343 TestStream_QueryInterface,
344 TestStream_AddRef,
345 TestStream_Release,
346 TestStream_Read,
347 TestStream_Write,
348 TestStream_Seek,
349 TestStream_SetSize,
350 TestStream_CopyTo,
351 TestStream_Commit,
352 TestStream_Revert,
353 TestStream_LockRegion,
354 TestStream_UnlockRegion,
355 TestStream_Stat,
356 TestStream_Clone
359 static IStream Test_Stream = { &StreamVtbl };
361 static void test_copyto(void)
363 IStream *pStream, *pStream2;
364 HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
365 static const char szHello[] = "Hello";
366 ULARGE_INTEGER cb;
367 static const char *methods_copyto[] =
369 "TestStream_Write",
370 NULL
372 ULONG written;
373 ULARGE_INTEGER ullRead;
374 ULARGE_INTEGER ullWritten;
375 ULARGE_INTEGER libNewPosition;
376 static const LARGE_INTEGER llZero;
377 char buffer[15];
379 expected_method_list = methods_copyto;
381 hr = IStream_Write(pStream, szHello, sizeof(szHello), &written);
382 ok_ole_success(hr, "IStream_Write");
383 ok(written == sizeof(szHello), "only %d bytes written\n", written);
385 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
386 ok_ole_success(hr, "IStream_Seek");
388 cb.QuadPart = sizeof(szHello);
389 hr = IStream_CopyTo(pStream, &Test_Stream, cb, &ullRead, &ullWritten);
390 ok(ullWritten.QuadPart == 5, "ullWritten was %d instead\n", (ULONG)ullWritten.QuadPart);
391 ok(ullRead.QuadPart == sizeof(szHello), "only %d bytes read\n", (ULONG)ullRead.QuadPart);
392 ok_ole_success(hr, "IStream_CopyTo");
394 ok(!*expected_method_list, "Method sequence starting from %s not called\n", *expected_method_list);
396 hr = IStream_Clone(pStream, &pStream2);
397 ok_ole_success(hr, "IStream_Clone");
399 hr = IStream_Seek(pStream2, llZero, STREAM_SEEK_CUR, &libNewPosition);
400 ok_ole_success(hr, "IStream_Seek");
401 ok(libNewPosition.QuadPart == sizeof(szHello), "libNewPosition wasn't set correctly for the cloned stream\n");
403 hr = IStream_Seek(pStream2, llZero, STREAM_SEEK_SET, NULL);
404 ok_ole_success(hr, "IStream_Seek");
406 hr = IStream_Read(pStream2, buffer, sizeof(buffer), NULL);
407 ok_ole_success(hr, "IStream_Read");
408 ok(!strcmp(buffer, szHello), "read data \"%s\" didn't match originally written data\n", buffer);
410 IStream_Release(pStream2);
411 IStream_Release(pStream);
414 static void test_freed_hglobal(void)
416 static const char teststring[] = "this is a test string";
417 HRESULT hr;
418 IStream *pStream;
419 HGLOBAL hglobal;
420 char *p;
421 char buffer[sizeof(teststring) + 8];
422 ULARGE_INTEGER ull;
423 ULONG read, written;
425 hglobal = GlobalAlloc(GMEM_DDESHARE|GMEM_NODISCARD|GMEM_MOVEABLE, strlen(teststring) + 1);
426 ok(hglobal != NULL, "GlobalAlloc failed with error %d\n", GetLastError());
427 p = GlobalLock(hglobal);
428 strcpy(p, teststring);
429 GlobalUnlock(hglobal);
431 hr = CreateStreamOnHGlobal(hglobal, FALSE, &pStream);
432 ok_ole_success(hr, "CreateStreamOnHGlobal");
434 hr = IStream_Read(pStream, buffer, sizeof(buffer), &read);
435 ok_ole_success(hr, "IStream_Read");
436 ok(!strcmp(buffer, teststring), "buffer data %s differs\n", buffer);
437 ok(read == sizeof(teststring) ||
438 broken(read == ((sizeof(teststring) + 3) & ~3)), /* win9x rounds the size */
439 "read should be sizeof(teststring) instead of %d\n", read);
441 GlobalFree(hglobal);
443 memset(buffer, 0, sizeof(buffer));
444 read = -1;
445 hr = IStream_Read(pStream, buffer, sizeof(buffer), &read);
446 ok_ole_success(hr, "IStream_Read");
447 ok(buffer[0] == 0, "buffer data should be untouched\n");
448 ok(read == 0, "read should be 0 instead of %d\n", read);
450 ull.QuadPart = sizeof(buffer);
451 hr = IStream_SetSize(pStream, ull);
452 ok(hr == E_OUTOFMEMORY, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr);
454 hr = IStream_Write(pStream, buffer, sizeof(buffer), &written);
455 ok(hr == E_OUTOFMEMORY, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr);
456 ok(written == 0, "written should be 0 instead of %d\n", written);
458 IStream_Release(pStream);
461 START_TEST(hglobalstream)
463 HRESULT hr;
464 IStream *pStream;
466 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
467 ok_ole_success(hr, "CreateStreamOnHGlobal");
469 test_streamonhglobal(pStream);
470 IStream_Release(pStream);
471 test_copyto();
472 test_freed_hglobal();