Test and fix a few problems with OLE storage streams.
[wine/dcerpc.git] / dlls / ole32 / tests / storage32.c
blob4565470f5a670e9587e6f8aa8f0d2aa14e9d4538
1 /*
2 * Unit tests for OLE storage
4 * Copyright (c) 2004 Mike McCormack
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdio.h>
23 #define COBJMACROS
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "ole2.h"
30 #include "objidl.h"
31 #include "initguid.h"
33 DEFINE_GUID( test_stg_cls, 0x88888888, 0x0425, 0x0000, 0,0,0,0,0,0,0,0);
35 void test_hglobal_storage_stat(void)
37 ILockBytes *ilb = NULL;
38 IStorage *stg = NULL;
39 HRESULT r;
40 STATSTG stat;
41 DWORD mode, refcount;
43 r = CreateILockBytesOnHGlobal( NULL, TRUE, &ilb );
44 ok( r == S_OK, "CreateILockBytesOnHGlobal failed\n");
46 mode = STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE;/*0x1012*/
47 r = StgCreateDocfileOnILockBytes( ilb, mode, 0, &stg );
48 ok( r == S_OK, "CreateILockBytesOnHGlobal failed\n");
50 r = WriteClassStg( stg, &test_stg_cls );
51 ok( r == S_OK, "WriteClassStg failed\n");
53 memset( &stat, 0, sizeof stat );
54 r = IStorage_Stat( stg, &stat, 0 );
56 ok( stat.pwcsName == NULL, "storage name not null\n");
57 ok( stat.type == 1, "type is wrong\n");
58 todo_wine {
59 ok( stat.grfMode == 0x12, "grf mode is incorrect\n");
61 ok( !memcmp(&stat.clsid, &test_stg_cls, sizeof test_stg_cls), "CLSID is wrong\n");
63 refcount = IStorage_Release( stg );
64 ok( refcount == 0, "IStorage refcount is wrong\n");
65 refcount = ILockBytes_Release( ilb );
66 ok( refcount == 0, "ILockBytes refcount is wrong\n");
69 void test_create_storage_modes(void)
71 static const WCHAR szPrefix[] = { 's','t','g',0 };
72 static const WCHAR szDot[] = { '.',0 };
73 WCHAR filename[MAX_PATH];
74 IStorage *stg = NULL;
75 HRESULT r;
77 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
78 return;
80 DeleteFileW(filename);
82 /* test with some invalid parameters */
83 r = StgCreateDocfile( NULL, 0, 0, &stg);
84 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
85 r = StgCreateDocfile( filename, 0, 0, &stg);
86 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
87 r = StgCreateDocfile( filename, STGM_CREATE, 0, &stg);
88 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
89 r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE, 0, &stg);
90 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
91 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &stg);
92 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
93 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
94 STGM_READWRITE, 0, NULL);
95 ok(r==STG_E_INVALIDPOINTER, "StgCreateDocfile succeeded\n");
96 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
97 STGM_READWRITE, 1, &stg);
98 ok(r==STG_E_INVALIDPARAMETER, "StgCreateDocfile succeeded\n");
99 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stg);
100 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
101 r = StgCreateDocfile( filename, STGM_PRIORITY, 0, &stg);
102 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
103 ok(stg == NULL, "stg was set\n");
105 /* check what happens if the file already exists */
106 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
107 ok(r==S_OK, "StgCreateDocfile failed\n");
108 r = IStorage_Release(stg);
109 ok(r == 0, "storage not released\n");
110 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |
111 STGM_TRANSACTED, 0, &stg);
112 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
113 r = StgCreateDocfile( filename, STGM_READ, 0, &stg);
114 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
115 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE, 0, &stg);
116 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
117 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE, 0, &stg);
118 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
119 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE, 0, &stg);
120 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
121 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
122 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
123 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, &stg);
124 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
125 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_WRITE, 0, &stg);
126 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
127 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_WRITE, 0, &stg);
128 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
129 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
130 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
131 ok(DeleteFileW(filename), "failed to delete file\n");
133 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |
134 STGM_TRANSACTED, 0, &stg);
135 ok(r==S_OK, "StgCreateDocfile failed\n");
136 r = IStorage_Release(stg);
137 ok(r == 0, "storage not released\n");
138 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |
139 STGM_TRANSACTED |STGM_FAILIFTHERE, 0, &stg);
140 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
141 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_WRITE, 0, &stg);
142 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
144 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
145 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
146 ok(r==S_OK, "StgCreateDocfile failed\n");
147 r = IStorage_Release(stg);
148 ok(r == 0, "storage not released\n");
150 ok(DeleteFileW(filename), "failed to delete file\n");
153 void test_storage_stream(void)
155 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
156 static const WCHAR szPrefix[] = { 's','t','g',0 };
157 static const WCHAR szDot[] = { '.',0 };
158 static const WCHAR longname[] = {
159 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
160 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0
162 WCHAR filename[MAX_PATH];
163 IStorage *stg = NULL;
164 HRESULT r;
165 IStream *stm = NULL;
166 ULONG count = 0;
167 LARGE_INTEGER pos;
168 ULARGE_INTEGER p;
169 unsigned char buffer[0x100];
171 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
172 return;
174 DeleteFileW(filename);
176 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
177 ok(r==S_OK, "StgCreateDocfile failed\n");
179 /* try create some invalid streams */
180 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, 0, &stm );
181 ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
182 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 1, &stm );
183 ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
184 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, NULL );
185 ok(r==STG_E_INVALIDPOINTER, "IStorage->CreateStream wrong error\n");
186 r = IStorage_CreateStream(stg, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
187 ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
188 r = IStorage_CreateStream(stg, longname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
189 ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
190 r = IStorage_CreateStream(stg, stmname, STGM_READWRITE, 0, 0, &stm );
191 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
192 r = IStorage_CreateStream(stg, stmname, STGM_READ, 0, 0, &stm );
193 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
194 r = IStorage_CreateStream(stg, stmname, STGM_WRITE, 0, 0, &stm );
195 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
196 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, 0, &stm );
197 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
198 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READ, 0, 0, &stm );
199 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
201 /* now really create a stream and delete it */
202 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
203 ok(r==S_OK, "IStorage->CreateStream failed\n");
204 r = IStream_Release(stm);
205 ok(r == 0, "wrong ref count\n");
206 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
207 ok(r==STG_E_FILEALREADYEXISTS, "IStorage->CreateStream failed\n");
208 r = IStorage_DestroyElement(stg,stmname);
209 ok(r==S_OK, "IStorage->DestroyElement failed\n");
211 /* create a stream and write to it */
212 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
213 ok(r==S_OK, "IStorage->CreateStream failed\n");
215 r = IStream_Write(stm, NULL, 0, NULL );
216 ok(r==STG_E_INVALIDPOINTER, "IStream->Write wrong error\n");
217 r = IStream_Write(stm, "Hello\n", 0, NULL );
218 ok(r==S_OK, "failed to write stream\n");
219 r = IStream_Write(stm, "Hello\n", 0, &count );
220 ok(r==S_OK, "failed to write stream\n");
221 r = IStream_Write(stm, "Hello\n", 6, &count );
222 ok(r==S_OK, "failed to write stream\n");
223 r = IStream_Commit(stm, STGC_DEFAULT );
224 ok(r==S_OK, "failed to commit stream\n");
225 r = IStream_Commit(stm, STGC_DEFAULT );
226 ok(r==S_OK, "failed to commit stream\n");
228 /* seek round a bit, reset the stream size */
229 pos.QuadPart = 0;
230 r = IStream_Seek(stm, pos, 3, &p );
231 ok(r==STG_E_INVALIDFUNCTION, "IStream->Seek returned wrong error\n");
232 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, NULL);
233 ok(r==S_OK, "failed to seek stream\n");
234 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
235 ok(r==S_OK, "failed to seek stream\n");
236 r = IStream_SetSize(stm,p);
237 ok(r==S_OK, "failed to set pos\n");
238 pos.QuadPart = 10;
239 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
240 ok(r==S_OK, "failed to seek stream\n");
241 ok(p.QuadPart == 10, "at wrong place\n");
242 pos.QuadPart = 0;
243 r = IStream_Seek(stm, pos, STREAM_SEEK_END, &p );
244 ok(r==S_OK, "failed to seek stream\n");
245 ok(p.QuadPart == 0, "at wrong place\n");
246 r = IStream_Read(stm, buffer, sizeof buffer, &count );
247 ok(r==S_OK, "failed to set pos\n");
248 ok(count == 0, "read bytes from empty stream\n");
250 /* wrap up */
251 r = IStream_Release(stm);
252 ok(r == 0, "wrong ref count\n");
253 r = IStorage_Release(stg);
254 ok(r == 0, "wrong ref count\n");
255 r = DeleteFileW(filename);
256 ok(r == TRUE, "file should exist\n");
259 START_TEST(storage32)
261 test_hglobal_storage_stat();
262 test_create_storage_modes();
263 test_storage_stream();