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(IStream
*pStream
)
48 const char data
[] = "Test String";
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");
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 */
75 hr
= IStream_SetSize(pStream
, ull
);
76 ok_ole_success(hr
, "IStream_SetSize");
78 /* IStream_Seek -- NULL position argument */
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;
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;
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;
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;
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) */
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;
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) */
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;
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 before start of stream) */
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;
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 -- valid LowPart value (seek to start of stream) */
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;
178 ll
.u
.LowPart
= -(DWORD
)sizeof(data
);
179 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
180 ok_ole_success(hr
, "IStream_Seek");
181 ok(ull
.u
.LowPart
== 0, "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 -- invalid LowPart value (seek to start of stream-1) */
186 ll
.u
.LowPart
= sizeof(data
);
187 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
188 ok_ole_success(hr
, "IStream_Seek");
190 ull
.u
.HighPart
= 0xCAFECAFE;
191 ull
.u
.LowPart
= 0xCAFECAFE;
193 ll
.u
.LowPart
= -(DWORD
)sizeof(data
)-1;
194 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
195 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
196 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
197 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
199 /* IStream_Seek -- valid LowPart value (seek forward to 0x80000000) */
201 ll
.u
.LowPart
= sizeof(data
);
202 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
203 ok_ole_success(hr
, "IStream_Seek");
205 ull
.u
.HighPart
= 0xCAFECAFE;
206 ull
.u
.LowPart
= 0xCAFECAFE;
208 ll
.u
.LowPart
= 0x80000000 - sizeof(data
);
209 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
210 ok_ole_success(hr
, "IStream_Seek");
211 ok(ull
.u
.LowPart
== 0x80000000, "LowPart set to %d\n", ull
.u
.LowPart
);
212 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
214 /* IStream_Seek -- invalid LowPart value (seek to beginning) */
216 ll
.u
.LowPart
= sizeof(data
);
217 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
218 ok_ole_success(hr
, "IStream_Seek");
220 ull
.u
.HighPart
= 0xCAFECAFE;
221 ull
.u
.LowPart
= 0xCAFECAFE;
223 ll
.u
.LowPart
= 0x80000000;
224 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
225 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
226 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
227 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
229 /* IStream_Seek -- valid LowPart value (seek to beginning) */
230 ull
.u
.HighPart
= 0xCAFECAFE;
231 ull
.u
.LowPart
= 0xCAFECAFE;
233 ll
.u
.LowPart
= 0x7FFFFFFF;
234 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
235 ok_ole_success(hr
, "IStream_Seek");
236 ok(ull
.u
.LowPart
== 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull
.u
.LowPart
);
237 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
239 /* IStream_Seek -- valid LowPart value (seek from current position) */
242 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
243 ok_ole_success(hr
, "IStream_Seek");
245 ull
.u
.HighPart
= 0xCAFECAFE;
246 ull
.u
.LowPart
= 0xCAFECAFE;
248 ll
.u
.LowPart
= 0x7FFFFFFF;
249 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
250 ok_ole_success(hr
, "IStream_Seek");
251 ok(ull
.u
.LowPart
== 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull
.u
.LowPart
);
252 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
254 /* IStream_Seek -- second seek allows you to go past 0x7FFFFFFF size */
255 ull
.u
.HighPart
= 0xCAFECAFE;
256 ull
.u
.LowPart
= 0xCAFECAFE;
259 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
260 ok_ole_success(hr
, "IStream_Seek");
261 ok(ull
.u
.LowPart
== 0x80000008, "should have set LowPart to 0x80000008 instead of %08x\n", ull
.u
.LowPart
);
262 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
264 /* IStream_Seek -- seek wraps position/size on integer overflow, but not on win8 */
265 ull
.u
.HighPart
= 0xCAFECAFE;
266 ull
.u
.LowPart
= 0xCAFECAFE;
268 ll
.u
.LowPart
= 0x7FFFFFFF;
269 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
270 ok(hr
== S_OK
|| hr
== STG_E_SEEKERROR
/* win8 */, "IStream_Seek\n");
272 ok(ull
.u
.LowPart
== 0x00000007, "should have set LowPart to 0x00000007 instead of %08x\n", ull
.u
.LowPart
);
274 ok(ull
.u
.LowPart
== 0x80000008, "should have set LowPart to 0x80000008 instead of %08x\n", ull
.u
.LowPart
);
275 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
277 hr
= IStream_Commit(pStream
, STGC_DEFAULT
);
278 ok_ole_success(hr
, "IStream_Commit");
280 hr
= IStream_Revert(pStream
);
281 ok_ole_success(hr
, "IStream_Revert");
283 hr
= IStream_LockRegion(pStream
, ull
, ull
, LOCK_WRITE
);
284 ok(hr
== STG_E_INVALIDFUNCTION
, "IStream_LockRegion should have returned STG_E_INVALIDFUNCTION instead of 0x%08x\n", hr
);
286 hr
= IStream_Stat(pStream
, &statstg
, STATFLAG_DEFAULT
);
287 ok_ole_success(hr
, "IStream_Stat");
288 ok(statstg
.type
== STGTY_STREAM
, "statstg.type should have been STGTY_STREAM instead of %d\n", statstg
.type
);
290 /* test OOM condition */
293 hr
= IStream_SetSize(pStream
, ull
);
294 ok(hr
== E_OUTOFMEMORY
|| broken(hr
== S_OK
), /* win9x */
295 "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr
);
298 static HRESULT WINAPI
TestStream_QueryInterface(IStream
*iface
, REFIID riid
, void **ppv
)
300 if (IsEqualIID(riid
, &IID_IUnknown
) ||
301 IsEqualIID(riid
, &IID_ISequentialStream
) ||
302 IsEqualIID(riid
, &IID_IStream
))
305 IStream_AddRef(iface
);
309 return E_NOINTERFACE
;
312 static ULONG WINAPI
TestStream_AddRef(IStream
*iface
)
317 static ULONG WINAPI
TestStream_Release(IStream
*iface
)
322 static HRESULT WINAPI
TestStream_Read(IStream
*iface
, void *pv
, ULONG cb
, ULONG
*pcbRead
)
324 CHECK_EXPECTED_METHOD("TestStream_Read");
328 static HRESULT WINAPI
TestStream_Write(IStream
*iface
, const void *pv
, ULONG cb
, ULONG
*pcbWritten
)
330 CHECK_EXPECTED_METHOD("TestStream_Write");
335 static HRESULT WINAPI
TestStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
337 CHECK_EXPECTED_METHOD("TestStream_Seek");
341 static HRESULT WINAPI
TestStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
343 CHECK_EXPECTED_METHOD("TestStream_SetSize");
347 static HRESULT WINAPI
TestStream_CopyTo(IStream
*iface
, IStream
*pStream
, ULARGE_INTEGER cb
, ULARGE_INTEGER
*pcbRead
, ULARGE_INTEGER
*pcbWritten
)
349 CHECK_EXPECTED_METHOD("TestStream_CopyTo");
353 static HRESULT WINAPI
TestStream_Commit(IStream
*iface
, DWORD grfCommitFlags
)
355 CHECK_EXPECTED_METHOD("TestStream_Commit");
359 static HRESULT WINAPI
TestStream_Revert(IStream
*iface
)
361 CHECK_EXPECTED_METHOD("TestStream_Revert");
365 static HRESULT WINAPI
TestStream_LockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
367 CHECK_EXPECTED_METHOD("TestStream_LockRegion");
371 static HRESULT WINAPI
TestStream_UnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
373 CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
377 static HRESULT WINAPI
TestStream_Stat(IStream
*iface
, STATSTG
*pstatstg
, DWORD grfStatFlag
)
379 CHECK_EXPECTED_METHOD("TestStream_Stat");
383 static HRESULT WINAPI
TestStream_Clone(IStream
*iface
, IStream
**pStream
)
385 CHECK_EXPECTED_METHOD("TestStream_Clone");
389 static /*const*/ IStreamVtbl StreamVtbl
=
391 TestStream_QueryInterface
,
401 TestStream_LockRegion
,
402 TestStream_UnlockRegion
,
407 static IStream Test_Stream
= { &StreamVtbl
};
409 static void test_copyto(void)
411 IStream
*pStream
, *pStream2
;
412 HRESULT hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
413 static const char szHello
[] = "Hello";
415 static const char *methods_copyto
[] =
421 ULARGE_INTEGER ullRead
;
422 ULARGE_INTEGER ullWritten
;
423 ULARGE_INTEGER libNewPosition
;
424 static const LARGE_INTEGER llZero
;
427 ok_ole_success(hr
, "CreateStreamOnHGlobal");
429 expected_method_list
= methods_copyto
;
431 hr
= IStream_Write(pStream
, szHello
, sizeof(szHello
), &written
);
432 ok_ole_success(hr
, "IStream_Write");
433 ok(written
== sizeof(szHello
), "only %d bytes written\n", written
);
435 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
436 ok_ole_success(hr
, "IStream_Seek");
438 cb
.QuadPart
= sizeof(szHello
);
439 hr
= IStream_CopyTo(pStream
, &Test_Stream
, cb
, &ullRead
, &ullWritten
);
440 ok(ullWritten
.QuadPart
== 5, "ullWritten was %d instead\n", (ULONG
)ullWritten
.QuadPart
);
441 ok(ullRead
.QuadPart
== sizeof(szHello
), "only %d bytes read\n", (ULONG
)ullRead
.QuadPart
);
442 ok_ole_success(hr
, "IStream_CopyTo");
444 ok(!*expected_method_list
, "Method sequence starting from %s not called\n", *expected_method_list
);
446 hr
= IStream_Clone(pStream
, &pStream2
);
447 ok_ole_success(hr
, "IStream_Clone");
449 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_CUR
, &libNewPosition
);
450 ok_ole_success(hr
, "IStream_Seek");
451 ok(libNewPosition
.QuadPart
== sizeof(szHello
), "libNewPosition wasn't set correctly for the cloned stream\n");
453 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_SET
, NULL
);
454 ok_ole_success(hr
, "IStream_Seek");
456 hr
= IStream_Read(pStream2
, buffer
, sizeof(buffer
), NULL
);
457 ok_ole_success(hr
, "IStream_Read");
458 ok(!strcmp(buffer
, szHello
), "read data \"%s\" didn't match originally written data\n", buffer
);
460 IStream_Release(pStream2
);
461 IStream_Release(pStream
);
464 static void test_freed_hglobal(void)
466 static const char teststring
[] = "this is a test string";
471 char buffer
[sizeof(teststring
) + 8];
475 hglobal
= GlobalAlloc(GMEM_DDESHARE
|GMEM_NODISCARD
|GMEM_MOVEABLE
, strlen(teststring
) + 1);
476 ok(hglobal
!= NULL
, "GlobalAlloc failed with error %d\n", GetLastError());
477 p
= GlobalLock(hglobal
);
478 strcpy(p
, teststring
);
479 GlobalUnlock(hglobal
);
481 hr
= CreateStreamOnHGlobal(hglobal
, FALSE
, &pStream
);
482 ok_ole_success(hr
, "CreateStreamOnHGlobal");
484 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
485 ok_ole_success(hr
, "IStream_Read");
486 ok(!strcmp(buffer
, teststring
), "buffer data %s differs\n", buffer
);
487 ok(read
== sizeof(teststring
) ||
488 broken(read
== ((sizeof(teststring
) + 3) & ~3)), /* win9x rounds the size */
489 "read should be sizeof(teststring) instead of %d\n", read
);
493 memset(buffer
, 0, sizeof(buffer
));
495 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
496 ok_ole_success(hr
, "IStream_Read");
497 ok(buffer
[0] == 0, "buffer data should be untouched\n");
498 ok(read
== 0, "read should be 0 instead of %d\n", read
);
500 ull
.QuadPart
= sizeof(buffer
);
501 hr
= IStream_SetSize(pStream
, ull
);
502 ok(hr
== E_OUTOFMEMORY
, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
504 hr
= IStream_Write(pStream
, buffer
, sizeof(buffer
), &written
);
505 ok(hr
== E_OUTOFMEMORY
, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
506 ok(written
== 0, "written should be 0 instead of %d\n", written
);
508 IStream_Release(pStream
);
511 START_TEST(hglobalstream
)
516 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
517 ok_ole_success(hr
, "CreateStreamOnHGlobal");
519 test_streamonhglobal(pStream
);
520 IStream_Release(pStream
);
522 test_freed_hglobal();