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 */
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_ole_success(hr
, "IStream_Seek");
276 ok(ull
.u
.LowPart
== 0x00000007, "should have set LowPart to 0x00000007 instead of %08x\n", ull
.u
.LowPart
);
277 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
279 hr
= IStream_Commit(pStream
, STGC_DEFAULT
);
280 ok_ole_success(hr
, "IStream_Commit");
282 hr
= IStream_Revert(pStream
);
283 ok_ole_success(hr
, "IStream_Revert");
285 hr
= IStream_LockRegion(pStream
, ull
, ull
, LOCK_WRITE
);
286 ok(hr
== STG_E_INVALIDFUNCTION
, "IStream_LockRegion should have returned STG_E_INVALIDFUNCTION instead of 0x%08x\n", hr
);
288 hr
= IStream_Stat(pStream
, &statstg
, STATFLAG_DEFAULT
);
289 ok_ole_success(hr
, "IStream_Stat");
290 ok(statstg
.type
== STGTY_STREAM
, "statstg.type should have been STGTY_STREAM instead of %d\n", statstg
.type
);
292 /* test OOM condition */
295 hr
= IStream_SetSize(pStream
, ull
);
296 ok(hr
== E_OUTOFMEMORY
|| broken(hr
== S_OK
), /* win9x */
297 "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr
);
300 static HRESULT WINAPI
TestStream_QueryInterface(IStream
*iface
, REFIID riid
, void **ppv
)
302 if (IsEqualIID(riid
, &IID_IUnknown
) ||
303 IsEqualIID(riid
, &IID_ISequentialStream
) ||
304 IsEqualIID(riid
, &IID_IStream
))
307 IStream_AddRef(iface
);
311 return E_NOINTERFACE
;
314 static ULONG WINAPI
TestStream_AddRef(IStream
*iface
)
319 static ULONG WINAPI
TestStream_Release(IStream
*iface
)
324 static HRESULT WINAPI
TestStream_Read(IStream
*iface
, void *pv
, ULONG cb
, ULONG
*pcbRead
)
326 CHECK_EXPECTED_METHOD("TestStream_Read");
330 static HRESULT WINAPI
TestStream_Write(IStream
*iface
, const void *pv
, ULONG cb
, ULONG
*pcbWritten
)
332 CHECK_EXPECTED_METHOD("TestStream_Write");
337 static HRESULT WINAPI
TestStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
339 CHECK_EXPECTED_METHOD("TestStream_Seek");
343 static HRESULT WINAPI
TestStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
345 CHECK_EXPECTED_METHOD("TestStream_SetSize");
349 static HRESULT WINAPI
TestStream_CopyTo(IStream
*iface
, IStream
*pStream
, ULARGE_INTEGER cb
, ULARGE_INTEGER
*pcbRead
, ULARGE_INTEGER
*pcbWritten
)
351 CHECK_EXPECTED_METHOD("TestStream_CopyTo");
355 static HRESULT WINAPI
TestStream_Commit(IStream
*iface
, DWORD grfCommitFlags
)
357 CHECK_EXPECTED_METHOD("TestStream_Commit");
361 static HRESULT WINAPI
TestStream_Revert(IStream
*iface
)
363 CHECK_EXPECTED_METHOD("TestStream_Revert");
367 static HRESULT WINAPI
TestStream_LockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
369 CHECK_EXPECTED_METHOD("TestStream_LockRegion");
373 static HRESULT WINAPI
TestStream_UnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
375 CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
379 static HRESULT WINAPI
TestStream_Stat(IStream
*iface
, STATSTG
*pstatstg
, DWORD grfStatFlag
)
381 CHECK_EXPECTED_METHOD("TestStream_Stat");
385 static HRESULT WINAPI
TestStream_Clone(IStream
*iface
, IStream
**pStream
)
387 CHECK_EXPECTED_METHOD("TestStream_Clone");
391 static /*const*/ IStreamVtbl StreamVtbl
=
393 TestStream_QueryInterface
,
403 TestStream_LockRegion
,
404 TestStream_UnlockRegion
,
409 static IStream Test_Stream
= { &StreamVtbl
};
411 static void test_copyto(void)
413 IStream
*pStream
, *pStream2
;
414 HRESULT hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
415 static const char szHello
[] = "Hello";
417 static const char *methods_copyto
[] =
423 ULARGE_INTEGER ullRead
;
424 ULARGE_INTEGER ullWritten
;
425 ULARGE_INTEGER libNewPosition
;
426 static const LARGE_INTEGER llZero
;
429 ok_ole_success(hr
, "CreateStreamOnHGlobal");
431 expected_method_list
= methods_copyto
;
433 hr
= IStream_Write(pStream
, szHello
, sizeof(szHello
), &written
);
434 ok_ole_success(hr
, "IStream_Write");
435 ok(written
== sizeof(szHello
), "only %d bytes written\n", written
);
437 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
438 ok_ole_success(hr
, "IStream_Seek");
440 cb
.QuadPart
= sizeof(szHello
);
441 hr
= IStream_CopyTo(pStream
, &Test_Stream
, cb
, &ullRead
, &ullWritten
);
442 ok(ullWritten
.QuadPart
== 5, "ullWritten was %d instead\n", (ULONG
)ullWritten
.QuadPart
);
443 ok(ullRead
.QuadPart
== sizeof(szHello
), "only %d bytes read\n", (ULONG
)ullRead
.QuadPart
);
444 ok_ole_success(hr
, "IStream_CopyTo");
446 ok(!*expected_method_list
, "Method sequence starting from %s not called\n", *expected_method_list
);
448 hr
= IStream_Clone(pStream
, &pStream2
);
449 ok_ole_success(hr
, "IStream_Clone");
451 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_CUR
, &libNewPosition
);
452 ok_ole_success(hr
, "IStream_Seek");
453 ok(libNewPosition
.QuadPart
== sizeof(szHello
), "libNewPosition wasn't set correctly for the cloned stream\n");
455 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_SET
, NULL
);
456 ok_ole_success(hr
, "IStream_Seek");
458 hr
= IStream_Read(pStream2
, buffer
, sizeof(buffer
), NULL
);
459 ok_ole_success(hr
, "IStream_Read");
460 ok(!strcmp(buffer
, szHello
), "read data \"%s\" didn't match originally written data\n", buffer
);
462 IStream_Release(pStream2
);
463 IStream_Release(pStream
);
466 static void test_freed_hglobal(void)
468 static const char teststring
[] = "this is a test string";
473 char buffer
[sizeof(teststring
) + 8];
477 hglobal
= GlobalAlloc(GMEM_DDESHARE
|GMEM_NODISCARD
|GMEM_MOVEABLE
, strlen(teststring
) + 1);
478 ok(hglobal
!= NULL
, "GlobalAlloc failed with error %d\n", GetLastError());
479 p
= GlobalLock(hglobal
);
480 strcpy(p
, teststring
);
481 GlobalUnlock(hglobal
);
483 hr
= CreateStreamOnHGlobal(hglobal
, FALSE
, &pStream
);
484 ok_ole_success(hr
, "CreateStreamOnHGlobal");
486 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
487 ok_ole_success(hr
, "IStream_Read");
488 ok(!strcmp(buffer
, teststring
), "buffer data %s differs\n", buffer
);
489 ok(read
== sizeof(teststring
) ||
490 broken(read
== ((sizeof(teststring
) + 3) & ~3)), /* win9x rounds the size */
491 "read should be sizeof(teststring) instead of %d\n", read
);
495 memset(buffer
, 0, sizeof(buffer
));
497 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
498 ok_ole_success(hr
, "IStream_Read");
499 ok(buffer
[0] == 0, "buffer data should be untouched\n");
500 ok(read
== 0, "read should be 0 instead of %d\n", read
);
502 ull
.QuadPart
= sizeof(buffer
);
503 hr
= IStream_SetSize(pStream
, ull
);
504 ok(hr
== E_OUTOFMEMORY
, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
506 hr
= IStream_Write(pStream
, buffer
, sizeof(buffer
), &written
);
507 ok(hr
== E_OUTOFMEMORY
, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
508 ok(written
== 0, "written should be 0 instead of %d\n", written
);
510 IStream_Release(pStream
);
513 START_TEST(hglobalstream
)
518 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
519 ok_ole_success(hr
, "CreateStreamOnHGlobal");
521 test_streamonhglobal(pStream
);
522 IStream_Release(pStream
);
524 test_freed_hglobal();