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 /* ignores HighPart */
81 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_SET
, &ull
);
83 ok_ole_success(hr
, "IStream_Seek");
84 ok(ull
.u
.LowPart
== 0, "should have set LowPart to 0 instead of %d\n", ull
.u
.LowPart
);
86 ok(ull
.u
.HighPart
== 0, "should have set HighPart to 0 instead of %d\n", ull
.u
.HighPart
);
88 /* ignores HighPart */
91 hr
= IStream_Seek(pStream
, ll
, STREAM_SEEK_CUR
, NULL
);
93 ok_ole_success(hr
, "IStream_Seek");
95 hr
= IStream_Commit(pStream
, STGC_DEFAULT
);
96 ok_ole_success(hr
, "IStream_Commit");
98 hr
= IStream_Revert(pStream
);
99 ok_ole_success(hr
, "IStream_Revert");
101 hr
= IStream_LockRegion(pStream
, ull
, ull
, LOCK_WRITE
);
102 ok(hr
== STG_E_INVALIDFUNCTION
, "IStream_LockRegion should have returned STG_E_INVALIDFUNCTION instead of 0x%08x\n", hr
);
104 hr
= IStream_Stat(pStream
, &statstg
, STATFLAG_DEFAULT
);
105 ok_ole_success(hr
, "IStream_Stat");
106 ok(statstg
.type
== STGTY_STREAM
, "statstg.type should have been STGTY_STREAM instead of %d\n", statstg
.type
);
108 /* test OOM condition */
111 hr
= IStream_SetSize(pStream
, ull
);
112 ok(hr
== E_OUTOFMEMORY
|| broken(hr
== S_OK
), /* win9x */
113 "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr
);
116 static HRESULT WINAPI
TestStream_QueryInterface(IStream
*iface
, REFIID riid
, void **ppv
)
118 if (IsEqualIID(riid
, &IID_IUnknown
) ||
119 IsEqualIID(riid
, &IID_ISequentialStream
) ||
120 IsEqualIID(riid
, &IID_IStream
))
123 IUnknown_AddRef(iface
);
127 return E_NOINTERFACE
;
130 static ULONG WINAPI
TestStream_AddRef(IStream
*iface
)
135 static ULONG WINAPI
TestStream_Release(IStream
*iface
)
140 static HRESULT WINAPI
TestStream_Read(IStream
*iface
, void *pv
, ULONG cb
, ULONG
*pcbRead
)
142 CHECK_EXPECTED_METHOD("TestStream_Read");
146 static HRESULT WINAPI
TestStream_Write(IStream
*iface
, const void *pv
, ULONG cb
, ULONG
*pcbWritten
)
148 CHECK_EXPECTED_METHOD("TestStream_Write");
153 static HRESULT WINAPI
TestStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
155 CHECK_EXPECTED_METHOD("TestStream_Seek");
159 static HRESULT WINAPI
TestStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
161 CHECK_EXPECTED_METHOD("TestStream_SetSize");
165 static HRESULT WINAPI
TestStream_CopyTo(IStream
*iface
, IStream
*pStream
, ULARGE_INTEGER cb
, ULARGE_INTEGER
*pcbRead
, ULARGE_INTEGER
*pcbWritten
)
167 CHECK_EXPECTED_METHOD("TestStream_CopyTo");
171 static HRESULT WINAPI
TestStream_Commit(IStream
*iface
, DWORD grfCommitFlags
)
173 CHECK_EXPECTED_METHOD("TestStream_Commit");
177 static HRESULT WINAPI
TestStream_Revert(IStream
*iface
)
179 CHECK_EXPECTED_METHOD("TestStream_Revert");
183 static HRESULT WINAPI
TestStream_LockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
185 CHECK_EXPECTED_METHOD("TestStream_LockRegion");
189 static HRESULT WINAPI
TestStream_UnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
191 CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
195 static HRESULT WINAPI
TestStream_Stat(IStream
*iface
, STATSTG
*pstatstg
, DWORD grfStatFlag
)
197 CHECK_EXPECTED_METHOD("TestStream_Stat");
201 static HRESULT WINAPI
TestStream_Clone(IStream
*iface
, IStream
**pStream
)
203 CHECK_EXPECTED_METHOD("TestStream_Clone");
207 static /*const*/ IStreamVtbl StreamVtbl
=
209 TestStream_QueryInterface
,
219 TestStream_LockRegion
,
220 TestStream_UnlockRegion
,
225 static IStream Test_Stream
= { &StreamVtbl
};
227 static void test_copyto(void)
229 IStream
*pStream
, *pStream2
;
230 HRESULT hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
231 static const char szHello
[] = "Hello";
233 static const char *methods_copyto
[] =
239 ULARGE_INTEGER ullRead
;
240 ULARGE_INTEGER ullWritten
;
241 ULARGE_INTEGER libNewPosition
;
242 static const LARGE_INTEGER llZero
;
245 expected_method_list
= methods_copyto
;
247 hr
= IStream_Write(pStream
, szHello
, sizeof(szHello
), &written
);
248 ok_ole_success(hr
, "IStream_Write");
249 ok(written
== sizeof(szHello
), "only %d bytes written\n", written
);
251 hr
= IStream_Seek(pStream
, llZero
, STREAM_SEEK_SET
, NULL
);
252 ok_ole_success(hr
, "IStream_Seek");
254 cb
.QuadPart
= sizeof(szHello
);
255 hr
= IStream_CopyTo(pStream
, &Test_Stream
, cb
, &ullRead
, &ullWritten
);
256 ok(ullWritten
.QuadPart
== 5, "ullWritten was %d instead\n", (ULONG
)ullWritten
.QuadPart
);
257 ok(ullRead
.QuadPart
== sizeof(szHello
), "only %d bytes read\n", (ULONG
)ullRead
.QuadPart
);
258 ok_ole_success(hr
, "IStream_CopyTo");
260 ok(!*expected_method_list
, "Method sequence starting from %s not called\n", *expected_method_list
);
262 hr
= IStream_Clone(pStream
, &pStream2
);
263 ok_ole_success(hr
, "IStream_Clone");
265 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_CUR
, &libNewPosition
);
266 ok_ole_success(hr
, "IStream_Seek");
267 ok(libNewPosition
.QuadPart
== sizeof(szHello
), "libNewPosition wasn't set correctly for the cloned stream\n");
269 hr
= IStream_Seek(pStream2
, llZero
, STREAM_SEEK_SET
, NULL
);
270 ok_ole_success(hr
, "IStream_Seek");
272 hr
= IStream_Read(pStream2
, buffer
, sizeof(buffer
), NULL
);
273 ok_ole_success(hr
, "IStream_Read");
274 ok(!strcmp(buffer
, szHello
), "read data \"%s\" didn't match originally written data\n", buffer
);
276 IStream_Release(pStream2
);
277 IStream_Release(pStream
);
280 static void test_freed_hglobal(void)
282 static const char teststring
[] = "this is a test string";
287 char buffer
[sizeof(teststring
) + 8];
291 hglobal
= GlobalAlloc(GMEM_DDESHARE
|GMEM_NODISCARD
|GMEM_MOVEABLE
, strlen(teststring
) + 1);
292 ok(hglobal
!= NULL
, "GlobalAlloc failed with error %d\n", GetLastError());
293 p
= GlobalLock(hglobal
);
294 strcpy(p
, teststring
);
295 GlobalUnlock(hglobal
);
297 hr
= CreateStreamOnHGlobal(hglobal
, FALSE
, &pStream
);
298 ok_ole_success(hr
, "CreateStreamOnHGlobal");
300 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
301 ok_ole_success(hr
, "IStream_Read");
302 ok(!strcmp(buffer
, teststring
), "buffer data %s differs\n", buffer
);
303 ok(read
== sizeof(teststring
) ||
304 broken(read
== ((sizeof(teststring
) + 3) & ~3)), /* win9x rounds the size */
305 "read should be sizeof(teststring) instead of %d\n", read
);
309 memset(buffer
, 0, sizeof(buffer
));
311 hr
= IStream_Read(pStream
, buffer
, sizeof(buffer
), &read
);
312 ok_ole_success(hr
, "IStream_Read");
313 ok(buffer
[0] == 0, "buffer data should be untouched\n");
314 ok(read
== 0, "read should be 0 instead of %d\n", read
);
316 ull
.QuadPart
= sizeof(buffer
);
317 hr
= IStream_SetSize(pStream
, ull
);
318 ok(hr
== E_OUTOFMEMORY
, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
320 hr
= IStream_Write(pStream
, buffer
, sizeof(buffer
), &written
);
321 ok(hr
== E_OUTOFMEMORY
, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr
);
322 ok(written
== 0, "written should be 0 instead of %d\n", written
);
324 IStream_Release(pStream
);
327 START_TEST(hglobalstream
)
332 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &pStream
);
333 ok_ole_success(hr
, "CreateStreamOnHGlobal");
335 test_streamonhglobal(pStream
);
336 IStream_Release(pStream
);
338 test_freed_hglobal();