ole32/tests: Do not call Release if CoCreateInstance failed.
[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, "IStream_SetSize with large size should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr);
96 }
97
98 static HRESULT WINAPI TestStream_QueryInterface(IStream *iface, REFIID riid, void **ppv)
99 {
100     if (IsEqualIID(riid, &IID_IUnknown) ||
101         IsEqualIID(riid, &IID_ISequentialStream) ||
102         IsEqualIID(riid, &IID_IStream))
103     {
104         *ppv = iface;
105         IUnknown_AddRef(iface);
106         return S_OK;
107     }
108     *ppv = NULL;
109     return E_NOINTERFACE;
110 }
111
112 static ULONG WINAPI TestStream_AddRef(IStream *iface)
113 {
114     return 2;
115 }
116
117 static ULONG WINAPI TestStream_Release(IStream *iface)
118 {
119     return 1;
120 }
121
122 static HRESULT WINAPI TestStream_Read(IStream *iface, void *pv, ULONG cb, ULONG *pcbRead)
123 {
124     CHECK_EXPECTED_METHOD("TestStream_Read");
125     return E_NOTIMPL;
126 }
127
128 static HRESULT WINAPI TestStream_Write(IStream *iface, const void *pv, ULONG cb, ULONG *pcbWritten)
129 {
130     CHECK_EXPECTED_METHOD("TestStream_Write");
131     *pcbWritten = 5;
132     return S_OK;
133 }
134
135 static HRESULT WINAPI TestStream_Seek(IStream *iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
136 {
137     CHECK_EXPECTED_METHOD("TestStream_Seek");
138     return E_NOTIMPL;
139 }
140
141 static HRESULT WINAPI TestStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
142 {
143     CHECK_EXPECTED_METHOD("TestStream_SetSize");
144     return E_NOTIMPL;
145 }
146
147 static HRESULT WINAPI TestStream_CopyTo(IStream *iface, IStream *pStream, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
148 {
149     CHECK_EXPECTED_METHOD("TestStream_CopyTo");
150     return E_NOTIMPL;
151 }
152
153 static HRESULT WINAPI TestStream_Commit(IStream *iface, DWORD grfCommitFlags)
154 {
155     CHECK_EXPECTED_METHOD("TestStream_Commit");
156     return E_NOTIMPL;
157 }
158
159 static HRESULT WINAPI TestStream_Revert(IStream *iface)
160 {
161     CHECK_EXPECTED_METHOD("TestStream_Revert");
162     return E_NOTIMPL;
163 }
164
165 static HRESULT WINAPI TestStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
166 {
167     CHECK_EXPECTED_METHOD("TestStream_LockRegion");
168     return E_NOTIMPL;
169 }
170
171 static HRESULT WINAPI TestStream_UnlockRegion(IStream *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
172 {
173     CHECK_EXPECTED_METHOD("TestStream_UnlockRegion");
174     return E_NOTIMPL;
175 }
176
177 static HRESULT WINAPI TestStream_Stat(IStream *iface, STATSTG *pstatstg, DWORD grfStatFlag)
178 {
179     CHECK_EXPECTED_METHOD("TestStream_Stat");
180     return E_NOTIMPL;
181 }
182
183 static HRESULT WINAPI TestStream_Clone(IStream *iface, IStream **pStream)
184 {
185     CHECK_EXPECTED_METHOD("TestStream_Clone");
186     return E_NOTIMPL;
187 }
188
189 static /*const*/ IStreamVtbl StreamVtbl =
190 {
191     TestStream_QueryInterface,
192     TestStream_AddRef,
193     TestStream_Release,
194     TestStream_Read,
195     TestStream_Write,
196     TestStream_Seek,
197     TestStream_SetSize,
198     TestStream_CopyTo,
199     TestStream_Commit,
200     TestStream_Revert,
201     TestStream_LockRegion,
202     TestStream_UnlockRegion,
203     TestStream_Stat,
204     TestStream_Clone
205 };
206
207 static IStream Test_Stream = { &StreamVtbl };
208
209 static void test_copyto(void)
210 {
211     IStream *pStream, *pStream2;
212     HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
213     static const char szHello[] = "Hello";
214     ULARGE_INTEGER cb;
215     static const char *methods_copyto[] =
216     {
217         "TestStream_Write",
218         NULL
219     };
220     ULONG written;
221     ULARGE_INTEGER ullRead;
222     ULARGE_INTEGER ullWritten;
223     ULARGE_INTEGER libNewPosition;
224     static const LARGE_INTEGER llZero;
225     char buffer[15];
226
227     expected_method_list = methods_copyto;
228
229     hr = IStream_Write(pStream, szHello, sizeof(szHello), &written);
230     ok_ole_success(hr, "IStream_Write");
231     ok(written == sizeof(szHello), "only %d bytes written\n", written);
232
233     hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
234     ok_ole_success(hr, "IStream_Seek");
235
236     cb.QuadPart = sizeof(szHello);
237     hr = IStream_CopyTo(pStream, &Test_Stream, cb, &ullRead, &ullWritten);
238     ok(ullWritten.QuadPart == 5, "ullWritten was %d instead\n", (ULONG)ullWritten.QuadPart);
239     ok(ullRead.QuadPart == sizeof(szHello), "only %d bytes read\n", (ULONG)ullRead.QuadPart);
240     ok_ole_success(hr, "IStream_CopyTo");
241
242     ok(!*expected_method_list, "Method sequence starting from %s not called\n", *expected_method_list);
243
244     hr = IStream_Clone(pStream, &pStream2);
245     ok_ole_success(hr, "IStream_Clone");
246
247     hr = IStream_Seek(pStream2, llZero, STREAM_SEEK_CUR, &libNewPosition);
248     ok_ole_success(hr, "IStream_Seek");
249     ok(libNewPosition.QuadPart == sizeof(szHello), "libNewPosition wasn't set correctly for the cloned stream\n");
250
251     hr = IStream_Seek(pStream2, llZero, STREAM_SEEK_SET, NULL);
252     ok_ole_success(hr, "IStream_Seek");
253
254     hr = IStream_Read(pStream2, buffer, sizeof(buffer), NULL);
255     ok_ole_success(hr, "IStream_Read");
256     ok(!strcmp(buffer, szHello), "read data \"%s\" didn't match originally written data\n", buffer);
257
258     IStream_Release(pStream2);
259     IStream_Release(pStream);
260 }
261
262 static void test_freed_hglobal(void)
263 {
264     HRESULT hr;
265     IStream *pStream;
266     HGLOBAL hglobal;
267     char *p;
268     char buffer[10];
269     ULARGE_INTEGER ull;
270     ULONG read, written;
271
272     hglobal = GlobalAlloc(GMEM_DDESHARE|GMEM_NODISCARD|GMEM_MOVEABLE, strlen("Rob") + 1);
273     ok(hglobal != NULL, "GlobalAlloc failed with error %d\n", GetLastError());
274     p = GlobalLock(hglobal);
275     strcpy(p, "Rob");
276     GlobalUnlock(hglobal);
277
278     hr = CreateStreamOnHGlobal(hglobal, FALSE, &pStream);
279     ok_ole_success(hr, "CreateStreamOnHGlobal");
280
281     hr = IStream_Read(pStream, buffer, sizeof(buffer), &read);
282     ok_ole_success(hr, "IStream_Read");
283     ok(!strcmp(buffer, "Rob"), "buffer data %s differs\n", buffer);
284     ok(read == strlen("Rob") + 1, "read should be 4 instead of %d\n", read);
285
286     GlobalFree(hglobal);
287
288     memset(buffer, 0, sizeof(buffer));
289     read = -1;
290     hr = IStream_Read(pStream, buffer, sizeof(buffer), &read);
291     ok_ole_success(hr, "IStream_Read");
292     ok(buffer[0] == 0, "buffer data should be untouched\n");
293     ok(read == 0, "read should be 0 instead of %d\n", read);
294
295     ull.QuadPart = sizeof(buffer);
296     hr = IStream_SetSize(pStream, ull);
297     ok(hr == E_OUTOFMEMORY, "IStream_SetSize with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr);
298
299     hr = IStream_Write(pStream, buffer, sizeof(buffer), &written);
300     ok(hr == E_OUTOFMEMORY, "IStream_Write with invalid HGLOBAL should return E_OUTOFMEMORY instead of 0x%08x\n", hr);
301     ok(written == 0, "written should be 0 instead of %d\n", written);
302
303     IStream_Release(pStream);
304 }
305
306 START_TEST(hglobalstream)
307 {
308     HRESULT hr;
309     IStream *pStream;
310
311     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
312     ok_ole_success(hr, "CreateStreamOnHGlobal");
313
314     test_streamonhglobal(pStream);
315     IStream_Release(pStream);
316     test_copyto();
317     test_freed_hglobal();
318 }