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
25 #include "wine/test.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;
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");
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];
77 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
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;
169 unsigned char buffer[0x100];
171 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
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 */
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");
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");
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");
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 void test_open_storage(void)
261 static const WCHAR szPrefix[] = { 's','t','g',0 };
262 static const WCHAR szNonExist[] = { 'n','o','n','e','x','i','s','t',0 };
263 static const WCHAR szDot[] = { '.',0 };
264 WCHAR filename[MAX_PATH];
265 IStorage *stg = NULL, *stg2 = NULL;
268 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
271 DeleteFileW(filename);
273 /* create the file */
274 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
275 ok(r==S_OK, "StgCreateDocfile failed\n");
276 IStorage_Release(stg);
278 r = StgOpenStorage( filename, NULL, 0, NULL, 0, &stg);
279 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage wrong error\n");
280 r = StgOpenStorage( NULL, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
281 ok(r==STG_E_INVALIDNAME, "StgOpenStorage wrong error\n");
282 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, NULL);
283 ok(r==STG_E_INVALIDPOINTER, "StgOpenStorage wrong error\n");
284 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 1, &stg);
285 ok(r==STG_E_INVALIDPARAMETER, "StgOpenStorage wrong error\n");
286 r = StgOpenStorage( szNonExist, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
287 ok(r==STG_E_FILENOTFOUND, "StgOpenStorage failed\n");
288 r = StgOpenStorage( filename, NULL, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
289 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
290 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ, NULL, 0, &stg);
291 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
292 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_READ | STGM_READ, NULL, 0, &stg);
293 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
295 /* open it for real */
296 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READ, NULL, 0, &stg);
297 ok(r==S_OK, "StgOpenStorage failed\n");
298 r = IStorage_Release(stg);
299 ok(r == 0, "wrong ref count\n");
301 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
302 ok(r==S_OK, "StgOpenStorage failed\n");
303 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg2);
304 ok(r==STG_E_SHAREVIOLATION, "StgOpenStorage failed\n");
305 r = IStorage_Release(stg);
306 ok(r == 0, "wrong ref count\n");
308 r = DeleteFileW(filename);
309 ok(r, "file didn't exist\n");
312 void test_storage_suminfo(void)
314 static const WCHAR szDot[] = { '.',0 };
315 static const WCHAR szPrefix[] = { 's','t','g',0 };
316 WCHAR filename[MAX_PATH];
317 IStorage *stg = NULL;
318 IPropertySetStorage *propset = NULL;
319 IPropertyStorage *ps = NULL;
322 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
325 DeleteFileW(filename);
327 /* create the file */
328 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
329 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
330 ok(r==S_OK, "StgCreateDocfile failed\n");
332 r = IStorage_QueryInterface( stg, &IID_IPropertySetStorage, (LPVOID) &propset );
333 ok(r == S_OK, "query interface failed\n");
336 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
337 ok(r == STG_E_FILENOTFOUND, "deleted property set storage\n");
339 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
340 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
341 ok(r == STG_E_FILENOTFOUND, "opened property set storage\n");
343 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
344 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
345 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
347 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
349 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
351 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0, 0, &ps );
352 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
354 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
355 STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
356 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
358 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
359 STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
360 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
362 /* now try really creating a a property set */
363 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
364 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
365 ok(r == S_OK, "failed to create property set storage\n");
368 IPropertyStorage_Release(ps);
370 /* now try creating the same thing again */
371 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
372 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
373 ok(r == S_OK, "failed to create property set storage\n");
375 IPropertyStorage_Release(ps);
377 /* should be able to open it */
378 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
379 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
380 ok(r == S_OK, "open failed\n");
382 IPropertyStorage_Release(ps);
385 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
386 ok(r == S_OK, "failed to delete property set storage\n");
388 /* try opening with an invalid FMTID */
389 r = IPropertySetStorage_Open( propset, NULL,
390 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
391 ok(r == E_INVALIDARG, "open succeeded\n");
393 IPropertyStorage_Release(ps);
396 r = IPropertySetStorage_Open( propset, &IID_IStorage,
397 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
398 ok(r == STG_E_FILENOTFOUND, "open succeeded\n");
400 IPropertyStorage_Release(ps);
403 /* try some invalid flags */
404 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
405 STGM_CREATE | STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
406 ok(r == STG_E_INVALIDFLAG, "open succeeded\n");
408 IPropertyStorage_Release(ps);
410 /* after deleting it, it should be gone */
411 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
412 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
413 ok(r == STG_E_FILENOTFOUND, "open failed\n");
415 IPropertyStorage_Release(ps);
416 printf("r = %08lx\n",r);
418 r = IPropertySetStorage_Release( propset );
419 ok(r == 1, "ref count wrong\n");
421 r = IStorage_Release(stg);
422 ok(r == 0, "ref count wrong\n");
424 DeleteFileW(filename);
427 START_TEST(storage32)
429 test_hglobal_storage_stat();
430 test_create_storage_modes();
431 test_storage_stream();
433 test_storage_suminfo();