ole32: Fix test failure on win2000+.
[wine] / dlls / ole32 / tests / hglobalstream.c
1 /*
2  * Stream on HGLOBAL Tests
3  *
4  * Copyright 2006 Robert Shearman (for CodeWeavers)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define COBJMACROS
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28
29 #include "wine/test.h"
30
31 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
32
33 static char const * const *expected_method_list;
34
35 #define CHECK_EXPECTED_METHOD(method_name) \
36 do { \
37     ok(*expected_method_list != NULL, "Extra method %s called\n", method_name); \
38         if (*expected_method_list) \
39         { \
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++; \
43         } \
44 } while(0)
45
46 static void test_streamonhglobal(IStream *pStream)
47 {
48     const char data[] = "Test String";
49     ULARGE_INTEGER ull;
50     LARGE_INTEGER ll;
51     char buffer[128];
52     ULONG read;
53     STATSTG statstg;
54     HRESULT hr;
55
56     ull.QuadPart = sizeof(data);
57     hr = IStream_SetSize(pStream, ull);
58     ok_ole_success(hr, "IStream_SetSize");
59
60     hr = IStream_Write(pStream, data, sizeof(data), NULL);
61     ok_ole_success(hr, "IStream_Write");
62
63     ll.QuadPart = 0;
64     hr = IStream_Seek(pStream, ll, STREAM_SEEK_SET, NULL);
65     ok_ole_success(hr, "IStream_Seek");
66
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);
71
72     /* ignores HighPart */
73     ull.u.HighPart = -1;
74     ull.u.LowPart = 0;
75     hr = IStream_SetSize(pStream, ull);
76     ok_ole_success(hr, "IStream_SetSize");
77
78     hr = IStream_Commit(pStream, STGC_DEFAULT);
79     ok_ole_success(hr, "IStream_Commit");
80
81     hr = IStream_Revert(pStream);
82     ok_ole_success(hr, "IStream_Revert");
83
84     hr = IStream_LockRegion(pStream, ull, ull, LOCK_WRITE);
85     ok(hr == STG_E_INVALIDFUNCTION, "IStream_LockRegion should have returned STG_E_INVALIDFUNCTION instead of 0x%08x\n", hr);
86
87     hr = IStream_Stat(pStream, &statstg, STATFLAG_DEFAULT);
88     ok_ole_success(hr, "IStream_Stat");
89     ok(statstg.type == STGTY_STREAM, "statstg.type should have been STGTY_STREAM instead of %d\n", statstg.type);
90
91     /* test OOM condition */
92     ull.u.HighPart = -1;
93     ull.u.LowPart = -1;
94     hr = IStream_SetSize(pStream, ull);
95     ok(hr == E_OUTOFMEMORY || broken(hr == S_OK), /* win9x */
96        "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr);
97 }
98
99 static HRESULT WINAPI TestStream_QueryInterface(IStream *iface, REFIID riid, void **ppv)
100 {
101     if (IsEqualIID(riid, &IID_IUnknown) ||
102         IsEqualIID(riid, &IID_ISequentialStream) ||
103         IsEqualIID(riid, &IID_IStream))
104     {
105         *ppv = iface;
106         IUnknown_AddRef(iface);
107         return S_OK;
108     }
109     *ppv = NULL;
110     return E_NOINTERFACE;
111 }
112
113 static ULONG WINAPI TestStream_AddRef(IStream *iface)
114 {
115     return 2;
116 }
117
118 static ULONG WINAPI TestStream_Release(IStream *iface)
119 {
120     return 1;
121 }
122
123 static HRESULT WINAPI TestStream_Read(IStream *iface, void *pv, ULONG cb, ULONG *pcbRead)
124 {
125     CHECK_EXPECTED_METHOD("TestStream_Read");
126     return E_NOTIMPL;
127 }
128
129 static HRESULT WINAPI TestStream_Write(IStream *iface, const void *pv, ULONG cb, ULONG *pcbWritten)
130 {
131     CHECK_EXPECTED_METHOD("TestStream_Write");
132     *pcbWritten = 5;
133     return S_OK;
134 }
135
136 static HRESULT WINAPI TestStream_Seek(IStream *iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
137 {
138     CHECK_EXPECTED_METHOD("TestStream_Seek");
139     return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI TestStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
143 {
144     CHECK_EXPECTED_METHOD("TestStream_SetSize");
145     return E_NOTIMPL;
146 }
147
148 static HRESULT WINAPI TestStream_CopyTo(IStream *iface, IStream *pStream, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
149 {
150     CHECK_EXPECTED_METHOD("TestStream_CopyTo");
151     return E_NOTIMPL;
152 }
153
154 static HRESULT WINAPI TestStream_Commit(IStream *iface, DWORD grfCommitFlags)
155 {
156     CHECK_EXPECTED_METHOD("TestStream_Commit");
157     return E_NOTIMPL;
158 }
159
160 static HRESULT WINAPI TestStream_Revert(IStream *iface)
161 {
162     CHECK_EXPECTED_METHOD("TestStream_Revert");
163     return E_NOTIMPL;
164 }
165
166 static HRESULT WINAPI TestStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
167 {
168     CHECK_EXPECTED_METHOD("TestStream_LockRegion");
169     return E_NOTIMPL;
170 }
171
172 static HRESULT WINAPI TestStream_UnlockRegion(IStream *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
173 {
174     CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
175     return E_NOTIMPL;
176 }
177
178 static HRESULT WINAPI TestStream_Stat(IStream *iface, STATSTG *pstatstg, DWORD grfStatFlag)
179 {
180     CHECK_EXPECTED_METHOD("TestStream_Stat");
181     return E_NOTIMPL;
182 }
183
184 static HRESULT WINAPI TestStream_Clone(IStream *iface, IStream **pStream)
185 {
186     CHECK_EXPECTED_METHOD("TestStream_Clone");
187     return E_NOTIMPL;
188 }
189
190 static /*const*/ IStreamVtbl StreamVtbl =
191 {
192     TestStream_QueryInterface,
193     TestStream_AddRef,
194     TestStream_Release,
195     TestStream_Read,
196     TestStream_Write,
197     TestStream_Seek,
198     TestStream_SetSize,
199     TestStream_CopyTo,
200     TestStream_Commit,
201     TestStream_Revert,
202     TestStream_LockRegion,
203     TestStream_UnlockRegion,
204     TestStream_Stat,
205     TestStream_Clone
206 };
207
208 static IStream Test_Stream = { &StreamVtbl };
209
210 static void test_copyto(void)
211 {
212     IStream *pStream, *pStream2;
213     HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
214     static const char szHello[] = "Hello";
215     ULARGE_INTEGER cb;
216     static const char *methods_copyto[] =
217     {
218         "TestStream_Write",
219         NULL
220     };
221     ULONG written;
222     ULARGE_INTEGER ullRead;
223     ULARGE_INTEGER ullWritten;
224     ULARGE_INTEGER libNewPosition;
225     static const LARGE_INTEGER llZero;
226     char buffer[15];
227
228     expected_method_list = methods_copyto;
229
230     hr = IStream_Write(pStream, szHello, sizeof(szHello), &written);
231     ok_ole_success(hr, "IStream_Write");
232     ok(written == sizeof(szHello), "only %d bytes written\n", written);
233
234     hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
235     ok_ole_success(hr, "IStream_Seek");
236
237     cb.QuadPart = sizeof(szHello);
238     hr = IStream_CopyTo(pStream, &Test_Stream, cb, &ullRead, &ullWritten);
239     ok(ullWritten.QuadPart == 5, "ullWritten was %d instead\n", (ULONG)ullWritten.QuadPart);
240     ok(ullRead.QuadPart == sizeof(szHello), "only %d bytes read\n", (ULONG)ullRead.QuadPart);
241     ok_ole_success(hr, "IStream_CopyTo");
242
243     ok(!*expected_method_list, "Method sequence starting from %s not called\n", *expected_method_list);
244
245     hr = IStream_Clone(pStream, &pStream2);
246     ok_ole_success(hr, "IStream_Clone");
247
248     hr = IStream_Seek(pStream2, llZero, STREAM_SEEK_CUR, &libNewPosition);
249     ok_ole_success(hr, "IStream_Seek");
250     ok(libNewPosition.QuadPart == sizeof(szHello), "libNewPosition wasn't set correctly for the cloned stream\n");
251
252     hr = IStream_Seek(pStream2, llZero, STREAM_SEEK_SET, NULL);
253     ok_ole_success(hr, "IStream_Seek");
254
255     hr = IStream_Read(pStream2, buffer, sizeof(buffer), NULL);
256     ok_ole_success(hr, "IStream_Read");
257     ok(!strcmp(buffer, szHello), "read data \"%s\" didn't match originally written data\n", buffer);
258
259     IStream_Release(pStream2);
260     IStream_Release(pStream);
261 }
262
263 static void test_freed_hglobal(void)
264 {
265     static const char teststring[] = "this is a test string";
266     HRESULT hr;
267     IStream *pStream;
268     HGLOBAL hglobal;
269     char *p;
270     char buffer[sizeof(teststring) + 8];
271     ULARGE_INTEGER ull;
272     ULONG read, written;
273
274     hglobal = GlobalAlloc(GMEM_DDESHARE|GMEM_NODISCARD|GMEM_MOVEABLE, strlen(teststring) + 1);
275     ok(hglobal != NULL, "GlobalAlloc failed with error %d\n", GetLastError());
276     p = GlobalLock(hglobal);
277     strcpy(p, teststring);
278     GlobalUnlock(hglobal);
279
280     hr = CreateStreamOnHGlobal(hglobal, FALSE, &pStream);
281     ok_ole_success(hr, "CreateStreamOnHGlobal");
282
283     hr = IStream_Read(pStream, buffer, sizeof(buffer), &read);
284     ok_ole_success(hr, "IStream_Read");
285     ok(!strcmp(buffer, teststring), "buffer data %s differs\n", buffer);
286     ok(read == sizeof(teststring) ||
287        broken(read == ((sizeof(teststring) + 3) & ~3)), /* win9x rounds the size */
288        "read should be sizeof(teststring) instead of %d\n", read);
289
290     GlobalFree(hglobal);
291
292     memset(buffer, 0, sizeof(buffer));
293     read = -1;
294     hr = IStream_Read(pStream, buffer, sizeof(buffer), &read);
295     ok_ole_success(hr, "IStream_Read");
296     ok(buffer[0] == 0, "buffer data should be untouched\n");
297     ok(read == 0, "read should be 0 instead of %d\n", read);
298
299     ull.QuadPart = sizeof(buffer);
300     hr = IStream_SetSize(pStream, ull);
301     ok(hr == E_OUTOFMEMORY, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr);
302
303     hr = IStream_Write(pStream, buffer, sizeof(buffer), &written);
304     ok(hr == E_OUTOFMEMORY, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr);
305     ok(written == 0, "written should be 0 instead of %d\n", written);
306
307     IStream_Release(pStream);
308 }
309
310 START_TEST(hglobalstream)
311 {
312     HRESULT hr;
313     IStream *pStream;
314
315     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
316     ok_ole_success(hr, "CreateStreamOnHGlobal");
317
318     test_streamonhglobal(pStream);
319     IStream_Release(pStream);
320     test_copyto();
321     test_freed_hglobal();
322 }