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
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) \
37 ok(*expected_method_list != NULL, "Extra method %s called\n", method_name); \
38 if (*expected_method_list) \
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++; \
46 static void test_streamonhglobal(void)
48 const char data
[] = "Test String";
57 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
58 ok(hr
== S_OK
, "Failed to create a stream, hr %#x.\n", hr
);
60 ull
.QuadPart
= sizeof(data
);
61 hr
= IStream_SetSize(pStream
, ull
);
62 ok_ole_success(hr
, "IStream_SetSize");
64 hr
= IStream_Write(pStream
, data
, sizeof(data
), NULL
);
65 ok_ole_success(hr
, "IStream_Write");
68 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, NULL
);
69 ok_ole_success(hr
, "IStream_Seek");
71 /* should return S_OK, not S_FALSE */
72 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
73 ok_ole_success(hr
, "IStream_Read");
74 ok(read
== sizeof(data
), "IStream_Read returned read %d\n", read
);
76 /* ignores HighPart */
79 hr
= IStream_SetSize(pStream
, ull
);
80 ok_ole_success(hr
, "IStream_SetSize");
82 /* IStream_Seek -- NULL position argument */
85 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, NULL
);
86 ok_ole_success(hr
, "IStream_Seek");
88 /* IStream_Seek -- valid position argument (seek from current position) */
89 ull
.u
.HighPart
= 0xCAFECAFE;
90 ull
.u
.LowPart
= 0xCAFECAFE;
93 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
94 ok_ole_success(hr
, "IStream_Seek");
95 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
96 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
98 /* IStream_Seek -- invalid seek argument */
99 ull
.u
.HighPart
= 0xCAFECAFE;
100 ull
.u
.LowPart
= 0xCAFECAFE;
103 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_END
+1, &ull
);
104 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
105 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
106 ok(ull
.u
.HighPart
== 0, "should not have changed HighPart, got %d\n", ull
.u
.HighPart
);
108 /* IStream_Seek -- valid position argument (seek to beginning) */
109 ull
.u
.HighPart
= 0xCAFECAFE;
110 ull
.u
.LowPart
= 0xCAFECAFE;
113 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
114 ok_ole_success(hr
, "IStream_Seek");
115 ok(ull
.u
.LowPart
== 0, "should have set LowPart to 0 instead of %d\n", ull
.u
.LowPart
);
116 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
118 /* IStream_Seek -- valid position argument (seek to end) */
119 ull
.u
.HighPart
= 0xCAFECAFE;
120 ull
.u
.LowPart
= 0xCAFECAFE;
123 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_END
, &ull
);
124 ok_ole_success(hr
, "IStream_Seek");
125 ok(ull
.u
.LowPart
== 0, "should have set LowPart to 0 instead of %d\n", ull
.u
.LowPart
);
126 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
128 /* IStream_Seek -- ignore HighPart in the move value (seek from current position) */
130 ll
.u
.LowPart
= sizeof(data
);
131 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
132 ok_ole_success(hr
, "IStream_Seek");
134 ull
.u
.HighPart
= 0xCAFECAFE;
135 ull
.u
.LowPart
= 0xCAFECAFE;
138 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
139 ok_ole_success(hr
, "IStream_Seek");
140 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
141 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
143 /* IStream_Seek -- ignore HighPart in the move value (seek to beginning) */
145 ll
.u
.LowPart
= sizeof(data
);
146 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
147 ok_ole_success(hr
, "IStream_Seek");
149 ull
.u
.HighPart
= 0xCAFECAFE;
150 ull
.u
.LowPart
= 0xCAFECAFE;
153 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
154 ok_ole_success(hr
, "IStream_Seek");
155 ok(ull
.u
.LowPart
== 0, "should have set LowPart to 0 instead of %d\n", ull
.u
.LowPart
);
156 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
158 /* IStream_Seek -- invalid LowPart value (seek before start of stream) */
160 ll
.u
.LowPart
= sizeof(data
);
161 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
162 ok_ole_success(hr
, "IStream_Seek");
164 ull
.u
.HighPart
= 0xCAFECAFE;
165 ull
.u
.LowPart
= 0xCAFECAFE;
167 ll
.u
.LowPart
= 0x80000000;
168 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
169 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
170 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
171 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
173 /* IStream_Seek -- valid LowPart value (seek to start of stream) */
175 ll
.u
.LowPart
= sizeof(data
);
176 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
177 ok_ole_success(hr
, "IStream_Seek");
179 ull
.u
.HighPart
= 0xCAFECAFE;
180 ull
.u
.LowPart
= 0xCAFECAFE;
182 ll
.u
.LowPart
= -(DWORD
)sizeof(data
);
183 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
184 ok_ole_success(hr
, "IStream_Seek");
185 ok(ull
.u
.LowPart
== 0, "LowPart set to %d\n", ull
.u
.LowPart
);
186 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
188 /* IStream_Seek -- invalid LowPart value (seek to start of stream-1) */
190 ll
.u
.LowPart
= sizeof(data
);
191 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
192 ok_ole_success(hr
, "IStream_Seek");
194 ull
.u
.HighPart
= 0xCAFECAFE;
195 ull
.u
.LowPart
= 0xCAFECAFE;
197 ll
.u
.LowPart
= -(DWORD
)sizeof(data
)-1;
198 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
199 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
200 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
201 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
203 /* IStream_Seek -- valid LowPart value (seek forward to 0x80000000) */
205 ll
.u
.LowPart
= sizeof(data
);
206 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
207 ok_ole_success(hr
, "IStream_Seek");
209 ull
.u
.HighPart
= 0xCAFECAFE;
210 ull
.u
.LowPart
= 0xCAFECAFE;
212 ll
.u
.LowPart
= 0x80000000 - sizeof(data
);
213 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
214 ok_ole_success(hr
, "IStream_Seek");
215 ok(ull
.u
.LowPart
== 0x80000000, "LowPart set to %d\n", ull
.u
.LowPart
);
216 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
218 /* IStream_Seek -- invalid LowPart value (seek to beginning) */
220 ll
.u
.LowPart
= sizeof(data
);
221 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
222 ok_ole_success(hr
, "IStream_Seek");
224 ull
.u
.HighPart
= 0xCAFECAFE;
225 ull
.u
.LowPart
= 0xCAFECAFE;
227 ll
.u
.LowPart
= 0x80000000;
228 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
229 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
230 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
231 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
233 /* IStream_Seek -- valid LowPart value (seek to beginning) */
234 ull
.u
.HighPart
= 0xCAFECAFE;
235 ull
.u
.LowPart
= 0xCAFECAFE;
237 ll
.u
.LowPart
= 0x7FFFFFFF;
238 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
239 ok_ole_success(hr
, "IStream_Seek");
240 ok(ull
.u
.LowPart
== 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull
.u
.LowPart
);
241 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
243 /* IStream_Seek -- valid LowPart value (seek from current position) */
246 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
247 ok_ole_success(hr
, "IStream_Seek");
249 ull
.u
.HighPart
= 0xCAFECAFE;
250 ull
.u
.LowPart
= 0xCAFECAFE;
252 ll
.u
.LowPart
= 0x7FFFFFFF;
253 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
254 ok_ole_success(hr
, "IStream_Seek");
255 ok(ull
.u
.LowPart
== 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull
.u
.LowPart
);
256 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
258 /* IStream_Seek -- second seek allows you to go past 0x7FFFFFFF size */
259 ull
.u
.HighPart
= 0xCAFECAFE;
260 ull
.u
.LowPart
= 0xCAFECAFE;
263 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
264 ok_ole_success(hr
, "IStream_Seek");
265 ok(ull
.u
.LowPart
== 0x80000008, "should have set LowPart to 0x80000008 instead of %08x\n", ull
.u
.LowPart
);
266 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
268 /* IStream_Seek -- seek wraps position/size on integer overflow, but not on win8 */
269 ull
.u
.HighPart
= 0xCAFECAFE;
270 ull
.u
.LowPart
= 0xCAFECAFE;
272 ll
.u
.LowPart
= 0x7FFFFFFF;
273 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
274 ok(hr
== S_OK
|| hr
== STG_E_SEEKERROR
/* win8 */, "IStream_Seek\n");
276 ok(ull
.u
.LowPart
== 0x00000007, "should have set LowPart to 0x00000007 instead of %08x\n", ull
.u
.LowPart
);
278 ok(ull
.u
.LowPart
== 0x80000008, "should have set LowPart to 0x80000008 instead of %08x\n", ull
.u
.LowPart
);
279 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
281 hr
= IStream_Commit(pStream
, STGC_DEFAULT
);
282 ok_ole_success(hr
, "IStream_Commit");
284 hr
= IStream_Revert(pStream
);
285 ok_ole_success(hr
, "IStream_Revert");
287 hr
= IStream_LockRegion(pStream
, ull
, ull
, LOCK_WRITE
);
288 ok(hr
== STG_E_INVALIDFUNCTION
, "IStream_LockRegion should have returned STG_E_INVALIDFUNCTION instead of 0x%08x\n", hr
);
290 hr
= IStream_Stat(pStream
, &statstg
, STATFLAG_DEFAULT
);
291 ok_ole_success(hr
, "IStream_Stat");
292 ok(statstg
.type
== STGTY_STREAM
, "statstg.type should have been STGTY_STREAM instead of %d\n", statstg
.type
);
294 /* test OOM condition */
297 hr
= IStream_SetSize(pStream
, ull
);
298 ok(hr
== E_OUTOFMEMORY
|| broken(hr
== S_OK
), /* win9x */
299 "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr
);
301 IStream_Release(pStream
);
304 static HRESULT WINAPI
TestStream_QueryInterface(IStream
*iface
, REFIID riid
, void **ppv
)
306 if (IsEqualIID(riid
, &IID_IUnknown
) ||
307 IsEqualIID(riid
, &IID_ISequentialStream
) ||
308 IsEqualIID(riid
, &IID_IStream
))
311 IStream_AddRef(iface
);
315 return E_NOINTERFACE
;
318 static ULONG WINAPI
TestStream_AddRef(IStream
*iface
)
323 static ULONG WINAPI
TestStream_Release(IStream
*iface
)
328 static HRESULT WINAPI
TestStream_Read(IStream
*iface
, void *pv
, ULONG cb
, ULONG
*pcbRead
)
330 CHECK_EXPECTED_METHOD("TestStream_Read");
334 static HRESULT WINAPI
TestStream_Write(IStream
*iface
, const void *pv
, ULONG cb
, ULONG
*pcbWritten
)
336 CHECK_EXPECTED_METHOD("TestStream_Write");
341 static HRESULT WINAPI
TestStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
343 CHECK_EXPECTED_METHOD("TestStream_Seek");
347 static HRESULT WINAPI
TestStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
349 CHECK_EXPECTED_METHOD("TestStream_SetSize");
353 static HRESULT WINAPI
TestStream_CopyTo(IStream
*iface
, IStream
*pStream
, ULARGE_INTEGER cb
, ULARGE_INTEGER
*pcbRead
, ULARGE_INTEGER
*pcbWritten
)
355 CHECK_EXPECTED_METHOD("TestStream_CopyTo");
359 static HRESULT WINAPI
TestStream_Commit(IStream
*iface
, DWORD grfCommitFlags
)
361 CHECK_EXPECTED_METHOD("TestStream_Commit");
365 static HRESULT WINAPI
TestStream_Revert(IStream
*iface
)
367 CHECK_EXPECTED_METHOD("TestStream_Revert");
371 static HRESULT WINAPI
TestStream_LockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
373 CHECK_EXPECTED_METHOD("TestStream_LockRegion");
377 static HRESULT WINAPI
TestStream_UnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
379 CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
383 static HRESULT WINAPI
TestStream_Stat(IStream
*iface
, STATSTG
*pstatstg
, DWORD grfStatFlag
)
385 CHECK_EXPECTED_METHOD("TestStream_Stat");
389 static HRESULT WINAPI
TestStream_Clone(IStream
*iface
, IStream
**pStream
)
391 CHECK_EXPECTED_METHOD("TestStream_Clone");
395 static /*const*/ IStreamVtbl StreamVtbl
=
397 TestStream_QueryInterface
,
407 TestStream_LockRegion
,
408 TestStream_UnlockRegion
,
413 static IStream Test_Stream
= { &StreamVtbl
};
415 static void test_copyto(void)
417 IStream
*pStream
, *pStream2
;
418 HRESULT hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
419 static const char szHello
[] = "Hello";
421 static const char *methods_copyto
[] =
427 ULARGE_INTEGER ullRead
;
428 ULARGE_INTEGER ullWritten
;
429 ULARGE_INTEGER libNewPosition
;
430 static const LARGE_INTEGER llZero
;
433 ok_ole_success(hr
, "CreateStreamOnHGlobal");
435 expected_method_list
= methods_copyto
;
437 hr
= IStream_Write(pStream
, szHello
, sizeof(szHello
), &written
);
438 ok_ole_success(hr
, "IStream_Write");
439 ok(written
== sizeof(szHello
), "only %d bytes written\n", written
);
441 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
442 ok_ole_success(hr
, "IStream_Seek");
444 cb
.QuadPart
= sizeof(szHello
);
445 hr
= IStream_CopyTo(pStream
, &Test_Stream
, cb
, &ullRead
, &ullWritten
);
446 ok(ullWritten
.QuadPart
== 5, "ullWritten was %d instead\n", (ULONG
)ullWritten
.QuadPart
);
447 ok(ullRead
.QuadPart
== sizeof(szHello
), "only %d bytes read\n", (ULONG
)ullRead
.QuadPart
);
448 ok_ole_success(hr
, "IStream_CopyTo");
450 ok(!*expected_method_list
, "Method sequence starting from %s not called\n", *expected_method_list
);
452 hr
= IStream_Clone(pStream
, &pStream2
);
453 ok_ole_success(hr
, "IStream_Clone");
455 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_CUR
, &libNewPosition
);
456 ok_ole_success(hr
, "IStream_Seek");
457 ok(libNewPosition
.QuadPart
== sizeof(szHello
), "libNewPosition wasn't set correctly for the cloned stream\n");
459 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_SET
, NULL
);
460 ok_ole_success(hr
, "IStream_Seek");
462 hr
= IStream_Read(pStream2
, buffer
, sizeof(buffer
), NULL
);
463 ok_ole_success(hr
, "IStream_Read");
464 ok(!strcmp(buffer
, szHello
), "read data \"%s\" didn't match originally written data\n", buffer
);
466 IStream_Release(pStream2
);
467 IStream_Release(pStream
);
470 static void test_freed_hglobal(void)
472 static const char teststring
[] = "this is a test string";
477 char buffer
[sizeof(teststring
) + 8];
481 hglobal
= GlobalAlloc(GMEM_DDESHARE
|GMEM_NODISCARD
|GMEM_MOVEABLE
, strlen(teststring
) + 1);
482 ok(hglobal
!= NULL
, "GlobalAlloc failed with error %d\n", GetLastError());
483 p
= GlobalLock(hglobal
);
484 strcpy(p
, teststring
);
485 GlobalUnlock(hglobal
);
487 hr
= CreateStreamOnHGlobal(hglobal
, FALSE
, &pStream
);
488 ok_ole_success(hr
, "CreateStreamOnHGlobal");
490 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
491 ok_ole_success(hr
, "IStream_Read");
492 ok(!strcmp(buffer
, teststring
), "buffer data %s differs\n", buffer
);
493 ok(read
== sizeof(teststring
) ||
494 broken(read
== ((sizeof(teststring
) + 3) & ~3)), /* win9x rounds the size */
495 "read should be sizeof(teststring) instead of %d\n", read
);
499 memset(buffer
, 0, sizeof(buffer
));
501 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
502 ok_ole_success(hr
, "IStream_Read");
503 ok(buffer
[0] == 0, "buffer data should be untouched\n");
504 ok(read
== 0, "read should be 0 instead of %d\n", read
);
506 ull
.QuadPart
= sizeof(buffer
);
507 hr
= IStream_SetSize(pStream
, ull
);
508 ok(hr
== E_OUTOFMEMORY
, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
510 hr
= IStream_Write(pStream
, buffer
, sizeof(buffer
), &written
);
511 ok(hr
== E_OUTOFMEMORY
, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
512 ok(written
== 0, "written should be 0 instead of %d\n", written
);
514 IStream_Release(pStream
);
517 START_TEST(hglobalstream
)
519 test_streamonhglobal();
521 test_freed_hglobal();