push 6c2bed22d4a356b01aae243fbf554b5dba1af534
[wine/hacks.git] / dlls / windowscodecs / tests / stream.c
blob6eff70a606fbff37fc97ea1bd782ef7e2d401c39
1 /*
2 * Copyright 2009 Tony Wasserka
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "wine/test.h"
21 #define COBJMACROS
22 #include "wincodec.h"
24 static void test_StreamOnMemory(void)
26 IWICImagingFactory *pFactory;
27 IWICStream *pStream, *pBufStream;
28 const BYTE CmpMem[] = {
29 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
31 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
32 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
34 BYTE Memory[64], MemBuf[64];
35 LARGE_INTEGER LargeNull, LargeInt;
36 ULARGE_INTEGER uLargeNull, uNewPos;
37 ULONG uBytesRead, uBytesWritten;
38 HRESULT hr;
39 STATSTG Stats;
41 LargeNull.QuadPart = 0;
42 uLargeNull.QuadPart = 0;
44 memcpy(Memory, CmpMem, sizeof(CmpMem));
46 CoInitialize(NULL);
48 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&pFactory);
49 if(FAILED(hr)) {
50 skip("CoCreateInstance returned with %#x, expected %#x\n", hr, S_OK);
51 return;
54 hr = IWICImagingFactory_CreateStream(pFactory, &pStream);
55 ok(hr == S_OK, "CreateStream returned with %#x, expected %#x\n", hr, S_OK);
56 if(FAILED(hr)) {
57 skip("Failed to create stream\n");
58 return;
61 /* InitializeFromMemory */
62 hr = IWICStream_InitializeFromMemory(pStream, NULL, sizeof(Memory)); /* memory = NULL */
63 ok(hr == E_INVALIDARG, "InitializeFromMemory returned with %#x, expected %#x\n", hr, E_INVALIDARG);
65 hr = IWICStream_InitializeFromMemory(pStream, Memory, 0); /* size = 0 */
66 ok(hr == S_OK, "InitializeFromMemory returned with %#x, expected %#x\n", hr, S_OK);
68 hr = IWICStream_InitializeFromMemory(pStream, Memory, sizeof(Memory)); /* stream already initialized */
69 ok(hr == WINCODEC_ERR_WRONGSTATE, "InitializeFromMemory returned with %#x, expected %#x\n", hr, WINCODEC_ERR_WRONGSTATE);
71 /* recreate stream */
72 IWICStream_Release(pStream);
73 hr = IWICImagingFactory_CreateStream(pFactory, &pStream);
74 ok(hr == S_OK, "CreateStream failed with %#x\n", hr);
76 hr = IWICStream_InitializeFromMemory(pStream, Memory, sizeof(Memory));
77 ok(hr == S_OK, "InitializeFromMemory returned with %#x, expected %#x\n", hr, S_OK);
80 /* Seek */
81 hr = IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, &uNewPos);
82 ok(hr == S_OK, "Seek returned with %#x, expected %#x\n", hr, S_OK);
83 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == 0, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, 0);
85 hr = IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
86 ok(hr == S_OK, "Seek returned with %#x, expected %#x\n", hr, S_OK);
88 LargeInt.u.HighPart = 1;
89 LargeInt.u.LowPart = 0;
90 hr = IWICStream_Seek(pStream, LargeInt, STREAM_SEEK_SET, &uNewPos);
91 ok(hr == HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW), "Seek returned with %#x, expected %#x\n", hr, HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW));
92 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == 0, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, 0);
94 LargeInt.QuadPart = sizeof(Memory) + 10;
95 hr = IWICStream_Seek(pStream, LargeInt, STREAM_SEEK_SET, &uNewPos);
96 ok(hr == E_INVALIDARG, "Seek returned with %#x, expected %#x\n", hr, E_INVALIDARG);
97 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == 0, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, 0);
99 LargeInt.QuadPart = 1;
100 hr = IWICStream_Seek(pStream, LargeInt, STREAM_SEEK_END, &uNewPos);
101 ok(hr == E_INVALIDARG, "Seek returned with %#x, expected %#x\n", hr, E_INVALIDARG);
102 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == 0, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, 0);
104 LargeInt.QuadPart = -1;
105 hr = IWICStream_Seek(pStream, LargeInt, STREAM_SEEK_END, &uNewPos);
106 ok(hr == S_OK, "Seek returned with %#x, expected %#x\n", hr, S_OK);
107 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == sizeof(Memory) - 1, "bSeek cursor moved to position (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart);
109 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, &uNewPos); /* reset seek pointer */
110 LargeInt.QuadPart = -(LONGLONG)sizeof(Memory) - 5;
111 hr = IWICStream_Seek(pStream, LargeInt, STREAM_SEEK_END, &uNewPos);
112 ok(hr == HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW),
113 "Seek returned with %#x, expected %#x\n", hr, HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW));
114 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == 0, "bSeek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, 0); /* remains unchanged */
115 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
118 /* Read */
119 hr = IWICStream_Read(pStream, MemBuf, 12, &uBytesRead);
120 ok(hr == S_OK, "Read returned with %#x, expected %#x\n", hr, S_OK);
121 if(SUCCEEDED(hr)) {
122 ok(uBytesRead == 12, "Read %u bytes, expected %u\n", uBytesRead, 3);
123 ok(memcmp(MemBuf, CmpMem, 12) == 0, "Read returned invalid data!\n");
125 /* check whether the seek pointer has moved correctly */
126 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_CUR, &uNewPos);
127 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == uBytesRead, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, uBytesRead);
130 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
132 hr = IWICStream_Read(pStream, Memory, 10, &uBytesRead); /* source = dest */
133 ok(hr == S_OK, "Read returned with %#x, expected %#x\n", hr, S_OK);
134 if(SUCCEEDED(hr)) {
135 ok(uBytesRead == 10, "Read %u bytes, expected %u\n", uBytesRead, 10);
136 ok(memcmp(Memory, CmpMem, uBytesRead) == 0, "Read returned invalid data!\n");
139 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
141 hr = IWICStream_Read(pStream, Memory, sizeof(Memory) + 10, &uBytesRead); /* request too many bytes */
142 ok(hr == S_OK, "Read returned with %#x, expected %#x\n", hr, S_OK);
143 if(SUCCEEDED(hr)) {
144 ok(uBytesRead == sizeof(Memory), "Read %u bytes\n", uBytesRead);
145 ok(memcmp(Memory, CmpMem, uBytesRead) == 0, "Read returned invalid data!\n");
148 hr = IWICStream_Read(pStream, NULL, 1, &uBytesRead); /* destination buffer = NULL */
149 ok(hr == E_INVALIDARG, "Read returned with %#x, expected %#x\n", hr, E_INVALIDARG);
151 hr = IWICStream_Read(pStream, MemBuf, 0, &uBytesRead); /* read 0 bytes */
152 ok(hr == S_OK, "Read returned with %#x, expected %#x\n", hr, S_OK);
154 hr = IWICStream_Read(pStream, NULL, 0, &uBytesRead);
155 ok(hr == E_INVALIDARG, "Read returned with %#x, expected %#x\n", hr, E_INVALIDARG);
157 hr = IWICStream_Read(pStream, NULL, 0, NULL);
158 ok(hr == E_INVALIDARG, "Read returned with %#x, expected %#x\n", hr, E_INVALIDARG);
160 hr = IWICStream_Read(pStream, MemBuf, 1, NULL);
161 ok(hr == S_OK, "Read returned with %#x, expected %#x\n", hr, S_OK);
163 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
164 ZeroMemory(MemBuf, sizeof(MemBuf));
165 hr = IWICStream_Read(pStream, MemBuf, sizeof(Memory) + 10, &uBytesRead);
166 ok(hr == S_OK, "Read returned with %#x, expected %#x\n", hr, S_OK);
167 if(SUCCEEDED(hr)) {
168 ok(uBytesRead == sizeof(Memory), "Read %u bytes\n", uBytesRead);
169 ok(memcmp(Memory, CmpMem, 64) == 0, "Read returned invalid data!\n");
171 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
174 /* Write */
175 MemBuf[0] = CmpMem[0] + 1;
176 MemBuf[1] = CmpMem[1] + 1;
177 MemBuf[2] = CmpMem[2] + 1;
178 hr = IWICStream_Write(pStream, MemBuf, 3, &uBytesWritten);
179 ok(hr == S_OK, "Write returned with %#x, expected %#x\n", hr, S_OK);
180 if(SUCCEEDED(hr)) {
181 ok(uBytesWritten == 3, "Wrote %u bytes, expected %u\n", uBytesWritten, 3);
182 ok(memcmp(MemBuf, Memory, 3) == 0, "Wrote returned invalid data!\n"); /* make sure we're writing directly */
184 /* check whether the seek pointer has moved correctly */
185 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_CUR, &uNewPos);
186 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == uBytesWritten, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, uBytesWritten);
188 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
190 hr = IWICStream_Write(pStream, MemBuf, 0, &uBytesWritten);
191 ok(hr == S_OK, "Read returned with %#x, expected %#x\n", hr, S_OK);
193 hr = IWICStream_Write(pStream, NULL, 3, &uBytesWritten);
194 ok(hr == E_INVALIDARG, "Write returned with %#x, expected %#x\n", hr, E_INVALIDARG);
195 ok(uBytesWritten == 0, "Wrote %u bytes, expected %u\n", uBytesWritten, 0);
196 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_CUR, &uNewPos);
197 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == 0, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, 0);
199 hr = IWICStream_Write(pStream, NULL, 0, &uBytesWritten);
200 ok(hr == E_INVALIDARG, "Write returned with %#x, expected %#x\n", hr, E_INVALIDARG);
201 ok(uBytesWritten == 0, "Wrote %u bytes, expected %u\n", uBytesWritten, 0);
202 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_CUR, &uNewPos);
203 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == 0, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, 0);
205 hr = IWICStream_Write(pStream, CmpMem, sizeof(Memory) + 10, &uBytesWritten);
206 ok(hr == STG_E_MEDIUMFULL, "Write returned with %#x, expected %#x\n", hr, STG_E_MEDIUMFULL);
207 ok(uBytesWritten == 0, "Wrote %u bytes, expected %u\n", uBytesWritten, 0);
208 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_CUR, &uNewPos);
209 ok(uNewPos.u.HighPart == 0 && uNewPos.u.LowPart == uBytesWritten, "Seek cursor moved to position (%u;%u), expected (%u;%u)\n", uNewPos.u.HighPart, uNewPos.u.LowPart, 0, uBytesWritten);
210 IWICStream_Seek(pStream, LargeNull, STREAM_SEEK_SET, NULL);
213 /* SetSize */
214 uNewPos.u.HighPart = 0;
215 uNewPos.u.LowPart = sizeof(Memory) + 10;
216 hr = IWICStream_SetSize(pStream, uNewPos);
217 ok(hr == E_NOTIMPL, "SetSize returned %#x, expected %#x\n", hr, E_NOTIMPL);
219 uNewPos.u.HighPart = 0;
220 uNewPos.u.LowPart = sizeof(Memory);
221 hr = IWICStream_SetSize(pStream, uNewPos);
222 ok(hr == E_NOTIMPL, "SetSize returned %#x, expected %#x\n", hr, E_NOTIMPL);
224 uNewPos.u.HighPart = 0;
225 uNewPos.u.LowPart = sizeof(Memory) - 10;
226 hr = IWICStream_SetSize(pStream, uNewPos);
227 ok(hr == E_NOTIMPL, "SetSize returned %#x, expected %#x\n", hr, E_NOTIMPL);
229 uNewPos.u.HighPart = 0;
230 uNewPos.u.LowPart = 0;
231 hr = IWICStream_SetSize(pStream, uNewPos);
232 ok(hr == E_NOTIMPL, "SetSize returned %#x, expected %#x\n", hr, E_NOTIMPL);
234 uNewPos.QuadPart = -10;
235 hr = IWICStream_SetSize(pStream, uNewPos);
236 ok(hr == E_NOTIMPL, "SetSize returned %#x, expected %#x\n", hr, E_NOTIMPL);
239 /* CopyTo */
240 uNewPos.u.HighPart = 0;
241 uNewPos.u.LowPart = 5;
242 hr = IWICStream_CopyTo(pStream, NULL, uNewPos, NULL, NULL);
243 ok(hr == E_NOTIMPL, "CopyTo returned %#x, expected %#x\n", hr, E_NOTIMPL);
245 hr = IWICImagingFactory_CreateStream(pFactory, &pBufStream);
246 ok(hr == S_OK, "CreateStream failed with %#x\n", hr);
248 hr = IWICStream_InitializeFromMemory(pBufStream, Memory, sizeof(Memory));
249 ok(hr == S_OK, "InitializeFromMemory returned with %#x, expected %#x\n", hr, S_OK);
251 hr = IWICStream_CopyTo(pStream, (IStream*)pBufStream, uNewPos, NULL, NULL);
252 ok(hr == E_NOTIMPL, "CopyTo returned %#x, expected %#x\n", hr, E_NOTIMPL);
253 IWICStream_Release(pBufStream);
256 /* Commit */
257 hr = IWICStream_Commit(pStream, STGC_DEFAULT);
258 ok(hr == E_NOTIMPL, "Commit returned %#x, expected %#x\n", hr, E_NOTIMPL);
260 hr = IWICStream_Commit(pStream, STGC_OVERWRITE);
261 ok(hr == E_NOTIMPL, "Commit returned %#x, expected %#x\n", hr, E_NOTIMPL);
263 hr = IWICStream_Commit(pStream, STGC_ONLYIFCURRENT);
264 ok(hr == E_NOTIMPL, "Commit returned %#x, expected %#x\n", hr, E_NOTIMPL);
266 hr = IWICStream_Commit(pStream, STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE);
267 ok(hr == E_NOTIMPL, "Commit returned %#x, expected %#x\n", hr, E_NOTIMPL);
269 hr = IWICStream_Commit(pStream, STGC_CONSOLIDATE);
270 ok(hr == E_NOTIMPL, "Commit returned %#x, expected %#x\n", hr, E_NOTIMPL);
273 /* Revert */
274 IWICStream_Write(pStream, &MemBuf[5], 6, NULL);
275 hr = IWICStream_Revert(pStream);
276 ok(hr == E_NOTIMPL, "Revert returned %#x, expected %#x\n", hr, E_NOTIMPL);
277 memcpy(Memory, CmpMem, sizeof(Memory));
280 /* LockRegion/UnlockRegion */
281 hr = IWICStream_LockRegion(pStream, uLargeNull, uLargeNull, 0);
282 ok(hr == E_NOTIMPL, "LockRegion returned %#x, expected %#x\n", hr, E_NOTIMPL);
284 hr = IWICStream_UnlockRegion(pStream, uLargeNull, uLargeNull, 0);
285 ok(hr == E_NOTIMPL, "UnlockRegion returned %#x, expected %#x\n", hr, E_NOTIMPL);
288 /* Stat */
289 hr = IWICStream_Stat(pStream, NULL, 0);
290 ok(hr == E_INVALIDARG, "Stat returned %#x, expected %#x\n", hr, E_INVALIDARG);
292 hr = IWICStream_Stat(pStream, &Stats, 0);
293 ok(hr == S_OK, "Stat returned %#x, expected %#x\n", hr, S_OK);
294 ok(Stats.pwcsName == NULL, "Stat returned name %p, expected %p\n", Stats.pwcsName, NULL);
295 ok(Stats.type == STGTY_STREAM, "Stat returned type %d, expected %d\n", Stats.type, STGTY_STREAM);
296 ok(Stats.cbSize.u.HighPart == 0 && Stats.cbSize.u.LowPart == sizeof(Memory), "Stat returned size (%u;%u)\n", Stats.cbSize.u.HighPart, Stats.cbSize.u.LowPart);
297 ok(Stats.mtime.dwHighDateTime == 0 && Stats.mtime.dwLowDateTime == 0, "Stat returned mtime (%u;%u), expected (%u;%u)\n", Stats.mtime.dwHighDateTime, Stats.mtime.dwLowDateTime, 0, 0);
298 ok(Stats.ctime.dwHighDateTime == 0 && Stats.ctime.dwLowDateTime == 0, "Stat returned ctime (%u;%u), expected (%u;%u)\n", Stats.ctime.dwHighDateTime, Stats.ctime.dwLowDateTime, 0, 0);
299 ok(Stats.atime.dwHighDateTime == 0 && Stats.atime.dwLowDateTime == 0, "Stat returned atime (%u;%u), expected (%u;%u)\n", Stats.atime.dwHighDateTime, Stats.atime.dwLowDateTime, 0, 0);
300 ok(Stats.grfMode == 0, "Stat returned access mode %d, expected %d\n", Stats.grfMode, 0);
301 ok(Stats.grfLocksSupported == 0, "Stat returned supported locks %#x, expected %#x\n", Stats.grfLocksSupported, 0);
302 ok(Stats.grfStateBits == 0, "Stat returned state bits %#x, expected %#x\n", Stats.grfStateBits, 0);
305 /* Clone */
306 hr = IWICStream_Clone(pStream, (IStream**)&pBufStream);
307 ok(hr == E_NOTIMPL, "UnlockRegion returned %#x, expected %#x\n", hr, E_NOTIMPL);
310 IWICStream_Release(pStream);
311 IWICStream_Release(pFactory);
312 CoUninitialize();
315 START_TEST(stream)
317 test_StreamOnMemory();