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 WIN32_NO_STATUS
23 #define COM_NO_WINDOWS_H
32 //#include "objbase.h"
34 #include <wine/test.h>
36 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
38 static char const * const *expected_method_list
;
40 #define CHECK_EXPECTED_METHOD(method_name) \
42 ok(*expected_method_list != NULL, "Extra method %s called\n", method_name); \
43 if (*expected_method_list) \
45 ok(!strcmp(*expected_method_list, method_name), "Expected %s to be called instead of %s\n", \
46 *expected_method_list, method_name); \
47 expected_method_list++; \
51 static void test_streamonhglobal(IStream
*pStream
)
53 const char data
[] = "Test String";
61 ull
.QuadPart
= sizeof(data
);
62 hr
= IStream_SetSize(pStream
, ull
);
63 ok_ole_success(hr
, "IStream_SetSize");
65 hr
= IStream_Write(pStream
, data
, sizeof(data
), NULL
);
66 ok_ole_success(hr
, "IStream_Write");
69 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, NULL
);
70 ok_ole_success(hr
, "IStream_Seek");
72 /* should return S_OK, not S_FALSE */
73 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
74 ok_ole_success(hr
, "IStream_Read");
75 ok(read
== sizeof(data
), "IStream_Read returned read %d\n", read
);
77 /* ignores HighPart */
80 hr
= IStream_SetSize(pStream
, ull
);
81 ok_ole_success(hr
, "IStream_SetSize");
83 /* IStream_Seek -- NULL position argument */
86 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, NULL
);
87 ok_ole_success(hr
, "IStream_Seek");
89 /* IStream_Seek -- valid position argument (seek from current position) */
90 ull
.u
.HighPart
= 0xCAFECAFE;
91 ull
.u
.LowPart
= 0xCAFECAFE;
94 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
95 ok_ole_success(hr
, "IStream_Seek");
96 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
97 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
99 /* IStream_Seek -- invalid seek argument */
100 ull
.u
.HighPart
= 0xCAFECAFE;
101 ull
.u
.LowPart
= 0xCAFECAFE;
104 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_END
+1, &ull
);
105 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
106 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
107 ok(ull
.u
.HighPart
== 0, "should not have changed HighPart, got %d\n", ull
.u
.HighPart
);
109 /* IStream_Seek -- valid position argument (seek to beginning) */
110 ull
.u
.HighPart
= 0xCAFECAFE;
111 ull
.u
.LowPart
= 0xCAFECAFE;
114 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
115 ok_ole_success(hr
, "IStream_Seek");
116 ok(ull
.u
.LowPart
== 0, "should have set LowPart to 0 instead of %d\n", ull
.u
.LowPart
);
117 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
119 /* IStream_Seek -- valid position argument (seek to end) */
120 ull
.u
.HighPart
= 0xCAFECAFE;
121 ull
.u
.LowPart
= 0xCAFECAFE;
124 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_END
, &ull
);
125 ok_ole_success(hr
, "IStream_Seek");
126 ok(ull
.u
.LowPart
== 0, "should have set LowPart to 0 instead of %d\n", ull
.u
.LowPart
);
127 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
129 /* IStream_Seek -- ignore HighPart in the move value (seek from current position) */
131 ll
.u
.LowPart
= sizeof(data
);
132 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
133 ok_ole_success(hr
, "IStream_Seek");
135 ull
.u
.HighPart
= 0xCAFECAFE;
136 ull
.u
.LowPart
= 0xCAFECAFE;
139 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
140 ok_ole_success(hr
, "IStream_Seek");
141 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
142 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
144 /* IStream_Seek -- ignore HighPart in the move value (seek to beginning) */
146 ll
.u
.LowPart
= sizeof(data
);
147 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
148 ok_ole_success(hr
, "IStream_Seek");
150 ull
.u
.HighPart
= 0xCAFECAFE;
151 ull
.u
.LowPart
= 0xCAFECAFE;
154 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
155 ok_ole_success(hr
, "IStream_Seek");
156 ok(ull
.u
.LowPart
== 0, "should have set LowPart to 0 instead of %d\n", ull
.u
.LowPart
);
157 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
159 /* IStream_Seek -- invalid LowPart value (seek before start of stream) */
161 ll
.u
.LowPart
= sizeof(data
);
162 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
163 ok_ole_success(hr
, "IStream_Seek");
165 ull
.u
.HighPart
= 0xCAFECAFE;
166 ull
.u
.LowPart
= 0xCAFECAFE;
168 ll
.u
.LowPart
= 0x80000000;
169 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
170 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
171 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
172 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
174 /* IStream_Seek -- valid LowPart value (seek to start of stream) */
176 ll
.u
.LowPart
= sizeof(data
);
177 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
178 ok_ole_success(hr
, "IStream_Seek");
180 ull
.u
.HighPart
= 0xCAFECAFE;
181 ull
.u
.LowPart
= 0xCAFECAFE;
183 ll
.u
.LowPart
= -(DWORD
)sizeof(data
);
184 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
185 ok_ole_success(hr
, "IStream_Seek");
186 ok(ull
.u
.LowPart
== 0, "LowPart set to %d\n", ull
.u
.LowPart
);
187 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
189 /* IStream_Seek -- invalid LowPart value (seek to start of stream-1) */
191 ll
.u
.LowPart
= sizeof(data
);
192 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
193 ok_ole_success(hr
, "IStream_Seek");
195 ull
.u
.HighPart
= 0xCAFECAFE;
196 ull
.u
.LowPart
= 0xCAFECAFE;
198 ll
.u
.LowPart
= -(DWORD
)sizeof(data
)-1;
199 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
200 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
201 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
202 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
204 /* IStream_Seek -- valid LowPart value (seek forward to 0x80000000) */
206 ll
.u
.LowPart
= sizeof(data
);
207 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
208 ok_ole_success(hr
, "IStream_Seek");
210 ull
.u
.HighPart
= 0xCAFECAFE;
211 ull
.u
.LowPart
= 0xCAFECAFE;
213 ll
.u
.LowPart
= 0x80000000 - sizeof(data
);
214 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
215 ok_ole_success(hr
, "IStream_Seek");
216 ok(ull
.u
.LowPart
== 0x80000000, "LowPart set to %d\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 -- invalid LowPart value (seek to beginning) */
221 ll
.u
.LowPart
= sizeof(data
);
222 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
223 ok_ole_success(hr
, "IStream_Seek");
225 ull
.u
.HighPart
= 0xCAFECAFE;
226 ull
.u
.LowPart
= 0xCAFECAFE;
228 ll
.u
.LowPart
= 0x80000000;
229 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
230 ok(hr
== STG_E_SEEKERROR
, "IStream_Seek should have returned STG_E_SEEKERROR instead of 0x%08x\n", hr
);
231 ok(ull
.u
.LowPart
== sizeof(data
), "LowPart set to %d\n", ull
.u
.LowPart
);
232 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
234 /* IStream_Seek -- valid LowPart value (seek to beginning) */
235 ull
.u
.HighPart
= 0xCAFECAFE;
236 ull
.u
.LowPart
= 0xCAFECAFE;
238 ll
.u
.LowPart
= 0x7FFFFFFF;
239 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
240 ok_ole_success(hr
, "IStream_Seek");
241 ok(ull
.u
.LowPart
== 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull
.u
.LowPart
);
242 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
244 /* IStream_Seek -- valid LowPart value (seek from current position) */
247 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
248 ok_ole_success(hr
, "IStream_Seek");
250 ull
.u
.HighPart
= 0xCAFECAFE;
251 ull
.u
.LowPart
= 0xCAFECAFE;
253 ll
.u
.LowPart
= 0x7FFFFFFF;
254 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
255 ok_ole_success(hr
, "IStream_Seek");
256 ok(ull
.u
.LowPart
== 0x7FFFFFFF, "should have set LowPart to 0x7FFFFFFF instead of %08x\n", ull
.u
.LowPart
);
257 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
259 /* IStream_Seek -- second seek allows you to go past 0x7FFFFFFF size */
260 ull
.u
.HighPart
= 0xCAFECAFE;
261 ull
.u
.LowPart
= 0xCAFECAFE;
264 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
265 ok_ole_success(hr
, "IStream_Seek");
266 ok(ull
.u
.LowPart
== 0x80000008, "should have set LowPart to 0x80000008 instead of %08x\n", ull
.u
.LowPart
);
267 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
269 /* IStream_Seek -- seek wraps position/size on integer overflow, but not on win8 */
270 ull
.u
.HighPart
= 0xCAFECAFE;
271 ull
.u
.LowPart
= 0xCAFECAFE;
273 ll
.u
.LowPart
= 0x7FFFFFFF;
274 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, &ull
);
275 ok(hr
== S_OK
|| hr
== STG_E_SEEKERROR
/* win8 */, "IStream_Seek\n");
277 ok(ull
.u
.LowPart
== 0x00000007, "should have set LowPart to 0x00000007 instead of %08x\n", ull
.u
.LowPart
);
279 ok(ull
.u
.LowPart
== 0x80000008, "should have set LowPart to 0x80000008 instead of %08x\n", ull
.u
.LowPart
);
280 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
282 hr
= IStream_Commit(pStream
, STGC_DEFAULT
);
283 ok_ole_success(hr
, "IStream_Commit");
285 hr
= IStream_Revert(pStream
);
286 ok_ole_success(hr
, "IStream_Revert");
288 hr
= IStream_LockRegion(pStream
, ull
, ull
, LOCK_WRITE
);
289 ok(hr
== STG_E_INVALIDFUNCTION
, "IStream_LockRegion should have returned STG_E_INVALIDFUNCTION instead of 0x%08x\n", hr
);
291 hr
= IStream_Stat(pStream
, &statstg
, STATFLAG_DEFAULT
);
292 ok_ole_success(hr
, "IStream_Stat");
293 ok(statstg
.type
== STGTY_STREAM
, "statstg.type should have been STGTY_STREAM instead of %d\n", statstg
.type
);
295 /* test OOM condition */
298 hr
= IStream_SetSize(pStream
, ull
);
299 ok(hr
== E_OUTOFMEMORY
|| broken(hr
== S_OK
), /* win9x */
300 "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr
);
303 static HRESULT WINAPI
TestStream_QueryInterface(IStream
*iface
, REFIID riid
, void **ppv
)
305 if (IsEqualIID(riid
, &IID_IUnknown
) ||
306 IsEqualIID(riid
, &IID_ISequentialStream
) ||
307 IsEqualIID(riid
, &IID_IStream
))
310 IStream_AddRef(iface
);
314 return E_NOINTERFACE
;
317 static ULONG WINAPI
TestStream_AddRef(IStream
*iface
)
322 static ULONG WINAPI
TestStream_Release(IStream
*iface
)
327 static HRESULT WINAPI
TestStream_Read(IStream
*iface
, void *pv
, ULONG cb
, ULONG
*pcbRead
)
329 CHECK_EXPECTED_METHOD("TestStream_Read");
333 static HRESULT WINAPI
TestStream_Write(IStream
*iface
, const void *pv
, ULONG cb
, ULONG
*pcbWritten
)
335 CHECK_EXPECTED_METHOD("TestStream_Write");
340 static HRESULT WINAPI
TestStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
342 CHECK_EXPECTED_METHOD("TestStream_Seek");
346 static HRESULT WINAPI
TestStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
348 CHECK_EXPECTED_METHOD("TestStream_SetSize");
352 static HRESULT WINAPI
TestStream_CopyTo(IStream
*iface
, IStream
*pStream
, ULARGE_INTEGER cb
, ULARGE_INTEGER
*pcbRead
, ULARGE_INTEGER
*pcbWritten
)
354 CHECK_EXPECTED_METHOD("TestStream_CopyTo");
358 static HRESULT WINAPI
TestStream_Commit(IStream
*iface
, DWORD grfCommitFlags
)
360 CHECK_EXPECTED_METHOD("TestStream_Commit");
364 static HRESULT WINAPI
TestStream_Revert(IStream
*iface
)
366 CHECK_EXPECTED_METHOD("TestStream_Revert");
370 static HRESULT WINAPI
TestStream_LockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
372 CHECK_EXPECTED_METHOD("TestStream_LockRegion");
376 static HRESULT WINAPI
TestStream_UnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
378 CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
382 static HRESULT WINAPI
TestStream_Stat(IStream
*iface
, STATSTG
*pstatstg
, DWORD grfStatFlag
)
384 CHECK_EXPECTED_METHOD("TestStream_Stat");
388 static HRESULT WINAPI
TestStream_Clone(IStream
*iface
, IStream
**pStream
)
390 CHECK_EXPECTED_METHOD("TestStream_Clone");
394 static /*const*/ IStreamVtbl StreamVtbl
=
396 TestStream_QueryInterface
,
406 TestStream_LockRegion
,
407 TestStream_UnlockRegion
,
412 static IStream Test_Stream
= { &StreamVtbl
};
414 static void test_copyto(void)
416 IStream
*pStream
, *pStream2
;
417 HRESULT hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
418 static const char szHello
[] = "Hello";
420 static const char *methods_copyto
[] =
426 ULARGE_INTEGER ullRead
;
427 ULARGE_INTEGER ullWritten
;
428 ULARGE_INTEGER libNewPosition
;
429 static const LARGE_INTEGER llZero
;
432 ok_ole_success(hr
, "CreateStreamOnHGlobal");
434 expected_method_list
= methods_copyto
;
436 hr
= IStream_Write(pStream
, szHello
, sizeof(szHello
), &written
);
437 ok_ole_success(hr
, "IStream_Write");
438 ok(written
== sizeof(szHello
), "only %d bytes written\n", written
);
440 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
441 ok_ole_success(hr
, "IStream_Seek");
443 cb
.QuadPart
= sizeof(szHello
);
444 hr
= IStream_CopyTo(pStream
, &Test_Stream
, cb
, &ullRead
, &ullWritten
);
445 ok(ullWritten
.QuadPart
== 5, "ullWritten was %d instead\n", (ULONG
)ullWritten
.QuadPart
);
446 ok(ullRead
.QuadPart
== sizeof(szHello
), "only %d bytes read\n", (ULONG
)ullRead
.QuadPart
);
447 ok_ole_success(hr
, "IStream_CopyTo");
449 ok(!*expected_method_list
, "Method sequence starting from %s not called\n", *expected_method_list
);
451 hr
= IStream_Clone(pStream
, &pStream2
);
452 ok_ole_success(hr
, "IStream_Clone");
454 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_CUR
, &libNewPosition
);
455 ok_ole_success(hr
, "IStream_Seek");
456 ok(libNewPosition
.QuadPart
== sizeof(szHello
), "libNewPosition wasn't set correctly for the cloned stream\n");
458 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_SET
, NULL
);
459 ok_ole_success(hr
, "IStream_Seek");
461 hr
= IStream_Read(pStream2
, buffer
, sizeof(buffer
), NULL
);
462 ok_ole_success(hr
, "IStream_Read");
463 ok(!strcmp(buffer
, szHello
), "read data \"%s\" didn't match originally written data\n", buffer
);
465 IStream_Release(pStream2
);
466 IStream_Release(pStream
);
469 static void test_freed_hglobal(void)
471 static const char teststring
[] = "this is a test string";
476 char buffer
[sizeof(teststring
) + 8];
480 hglobal
= GlobalAlloc(GMEM_DDESHARE
|GMEM_NODISCARD
|GMEM_MOVEABLE
, strlen(teststring
) + 1);
481 ok(hglobal
!= NULL
, "GlobalAlloc failed with error %d\n", GetLastError());
482 p
= GlobalLock(hglobal
);
483 strcpy(p
, teststring
);
484 GlobalUnlock(hglobal
);
486 hr
= CreateStreamOnHGlobal(hglobal
, FALSE
, &pStream
);
487 ok_ole_success(hr
, "CreateStreamOnHGlobal");
489 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
490 ok_ole_success(hr
, "IStream_Read");
491 ok(!strcmp(buffer
, teststring
), "buffer data %s differs\n", buffer
);
492 ok(read
== sizeof(teststring
) ||
493 broken(read
== ((sizeof(teststring
) + 3) & ~3)), /* win9x rounds the size */
494 "read should be sizeof(teststring) instead of %d\n", read
);
498 memset(buffer
, 0, sizeof(buffer
));
500 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
501 ok_ole_success(hr
, "IStream_Read");
502 ok(buffer
[0] == 0, "buffer data should be untouched\n");
503 ok(read
== 0, "read should be 0 instead of %d\n", read
);
505 ull
.QuadPart
= sizeof(buffer
);
506 hr
= IStream_SetSize(pStream
, ull
);
507 ok(hr
== E_OUTOFMEMORY
, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
509 hr
= IStream_Write(pStream
, buffer
, sizeof(buffer
), &written
);
510 ok(hr
== E_OUTOFMEMORY
, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
511 ok(written
== 0, "written should be 0 instead of %d\n", written
);
513 IStream_Release(pStream
);
516 START_TEST(hglobalstream
)
521 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
522 ok_ole_success(hr
, "CreateStreamOnHGlobal");
524 test_streamonhglobal(pStream
);
525 IStream_Release(pStream
);
527 test_freed_hglobal();