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 static 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");
58 ok( stat.grfMode == 0x12, "grf mode is incorrect\n");
59 ok( !memcmp(&stat.clsid, &test_stg_cls, sizeof test_stg_cls), "CLSID is wrong\n");
61 refcount = IStorage_Release( stg );
62 ok( refcount == 0, "IStorage refcount is wrong\n");
63 refcount = ILockBytes_Release( ilb );
64 ok( refcount == 0, "ILockBytes refcount is wrong\n");
67 static void test_create_storage_modes(void)
69 static const WCHAR szPrefix[] = { 's','t','g',0 };
70 static const WCHAR szDot[] = { '.',0 };
71 WCHAR filename[MAX_PATH];
75 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
78 DeleteFileW(filename);
80 /* test with some invalid parameters */
81 r = StgCreateDocfile( NULL, 0, 0, &stg);
82 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
83 r = StgCreateDocfile( filename, 0, 0, &stg);
84 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
85 r = StgCreateDocfile( filename, STGM_CREATE, 0, &stg);
86 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
87 r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE, 0, &stg);
88 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
89 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &stg);
90 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
91 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, NULL);
92 ok(r==STG_E_INVALIDPOINTER, "StgCreateDocfile succeeded\n");
93 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, &stg);
94 ok(r==STG_E_INVALIDPARAMETER, "StgCreateDocfile succeeded\n");
95 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
96 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
97 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stg);
98 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
99 r = StgCreateDocfile( filename, STGM_PRIORITY, 0, &stg);
100 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
102 /* StgCreateDocfile seems to be very particular about the flags it accepts */
103 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | STGM_WRITE, 0, &stg);
104 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
105 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 8, 0, &stg);
106 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
107 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80, 0, &stg);
108 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
109 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800, 0, &stg);
110 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
111 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x8000, 0, &stg);
112 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
113 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80000, 0, &stg);
114 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
115 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800000, 0, &stg);
116 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
117 ok(stg == NULL, "stg was set\n");
119 /* check what happens if the file already exists (which is how it's meant to be used) */
120 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
121 ok(r==S_OK, "StgCreateDocfile failed\n");
122 r = IStorage_Release(stg);
123 ok(r == 0, "storage not released\n");
124 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
125 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n"); /* FAILIFTHERE is default */
126 r = StgCreateDocfile( filename, STGM_READ, 0, &stg);
127 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n"); /* need at least readmode and sharemode */
128 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE, 0, &stg);
129 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
130 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE, 0, &stg);
131 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
132 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE, 0, &stg);
133 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
134 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
135 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
136 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, &stg);
137 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
138 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_WRITE, 0, &stg);
139 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
140 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_WRITE, 0, &stg);
141 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
142 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
143 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
144 r = StgCreateDocfile( filename, STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
145 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
146 ok(DeleteFileW(filename), "failed to delete file\n");
148 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
149 ok(r==S_OK, "StgCreateDocfile failed\n");
150 r = IStorage_Release(stg);
151 ok(r == 0, "storage not released\n");
152 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED |STGM_FAILIFTHERE, 0, &stg);
153 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
154 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_WRITE, 0, &stg);
155 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
157 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
158 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
159 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
160 ok(r==S_OK, "StgCreateDocfile failed\n");
161 r = IStorage_Release(stg);
162 ok(r == 0, "storage not released\n");
163 ok(DeleteFileW(filename), "failed to delete file\n");
165 /* test the way excel uses StgCreateDocFile */
166 r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
167 ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
170 r = IStorage_Release(stg);
171 ok(r == 0, "storage not released\n");
172 ok(DeleteFileW(filename), "failed to delete file\n");
175 /* and the way windows media uses it ... */
176 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_NONE | STGM_READWRITE | STGM_TRANSACTED, 0, &stg);
177 ok(r==S_OK, "StgCreateDocfile the windows media way failed\n");
180 r = IStorage_Release(stg);
181 ok(r == 0, "storage not released\n");
182 ok(DeleteFileW(filename), "failed to delete file\n");
185 /* looks like we need STGM_TRANSACTED or STGM_CREATE */
186 r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
187 ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
190 r = IStorage_Release(stg);
191 ok(r == 0, "storage not released\n");
192 ok(DeleteFileW(filename), "failed to delete file\n");
195 r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_WRITE, 0, &stg);
196 ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
199 r = IStorage_Release(stg);
200 ok(r == 0, "storage not released\n");
201 ok(DeleteFileW(filename), "failed to delete file\n");
204 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
205 ok(r==S_OK, "StgCreateDocfile the powerpoint way failed\n");
208 r = IStorage_Release(stg);
209 ok(r == 0, "storage not released\n");
210 ok(DeleteFileW(filename), "failed to delete file\n");
213 /* test the way msi uses StgCreateDocfile */
214 r = StgCreateDocfile( filename, STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stg);
215 ok(r==S_OK, "StgCreateDocFile failed\n");
216 r = IStorage_Release(stg);
217 ok(r == 0, "storage not released\n");
218 ok(DeleteFileW(filename), "failed to delete file\n");
221 static void test_storage_stream(void)
223 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
224 static const WCHAR szPrefix[] = { 's','t','g',0 };
225 static const WCHAR szDot[] = { '.',0 };
226 static const WCHAR longname[] = {
227 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
228 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0
230 WCHAR filename[MAX_PATH];
231 IStorage *stg = NULL;
237 unsigned char buffer[0x100];
239 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
242 DeleteFileW(filename);
244 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
245 ok(r==S_OK, "StgCreateDocfile failed\n");
247 /* try create some invalid streams */
248 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, 0, &stm );
249 ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
250 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 1, &stm );
251 ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
252 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, NULL );
253 ok(r==STG_E_INVALIDPOINTER, "IStorage->CreateStream wrong error\n");
254 r = IStorage_CreateStream(stg, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
255 ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
256 r = IStorage_CreateStream(stg, longname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
257 ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error, got %ld GetLastError()=%ld\n", r, GetLastError());
258 r = IStorage_CreateStream(stg, stmname, STGM_READWRITE, 0, 0, &stm );
259 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
260 r = IStorage_CreateStream(stg, stmname, STGM_READ, 0, 0, &stm );
261 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
262 r = IStorage_CreateStream(stg, stmname, STGM_WRITE, 0, 0, &stm );
263 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
264 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, 0, &stm );
265 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
266 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READ, 0, 0, &stm );
267 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
269 /* now really create a stream and delete it */
270 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
271 ok(r==S_OK, "IStorage->CreateStream failed\n");
272 r = IStream_Release(stm);
273 ok(r == 0, "wrong ref count\n");
274 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
275 ok(r==STG_E_FILEALREADYEXISTS, "IStorage->CreateStream failed\n");
276 r = IStorage_DestroyElement(stg,stmname);
277 ok(r==S_OK, "IStorage->DestroyElement failed\n");
279 /* create a stream and write to it */
280 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
281 ok(r==S_OK, "IStorage->CreateStream failed\n");
283 r = IStream_Write(stm, NULL, 0, NULL );
284 ok(r==STG_E_INVALIDPOINTER, "IStream->Write wrong error\n");
285 r = IStream_Write(stm, "Hello\n", 0, NULL );
286 ok(r==S_OK, "failed to write stream\n");
287 r = IStream_Write(stm, "Hello\n", 0, &count );
288 ok(r==S_OK, "failed to write stream\n");
289 r = IStream_Write(stm, "Hello\n", 6, &count );
290 ok(r==S_OK, "failed to write stream\n");
291 r = IStream_Commit(stm, STGC_DEFAULT );
292 ok(r==S_OK, "failed to commit stream\n");
293 r = IStream_Commit(stm, STGC_DEFAULT );
294 ok(r==S_OK, "failed to commit stream\n");
296 /* seek round a bit, reset the stream size */
298 r = IStream_Seek(stm, pos, 3, &p );
299 ok(r==STG_E_INVALIDFUNCTION, "IStream->Seek returned wrong error\n");
300 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, NULL);
301 ok(r==S_OK, "failed to seek stream\n");
302 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
303 ok(r==S_OK, "failed to seek stream\n");
304 r = IStream_SetSize(stm,p);
305 ok(r==S_OK, "failed to set pos\n");
307 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
308 ok(r==S_OK, "failed to seek stream\n");
309 ok(p.QuadPart == 10, "at wrong place\n");
311 r = IStream_Seek(stm, pos, STREAM_SEEK_END, &p );
312 ok(r==S_OK, "failed to seek stream\n");
313 ok(p.QuadPart == 0, "at wrong place\n");
314 r = IStream_Read(stm, buffer, sizeof buffer, &count );
315 ok(r==S_OK, "failed to set pos\n");
316 ok(count == 0, "read bytes from empty stream\n");
319 r = IStream_Release(stm);
320 ok(r == 0, "wrong ref count\n");
321 r = IStorage_Release(stg);
322 ok(r == 0, "wrong ref count\n");
323 r = DeleteFileW(filename);
324 ok(r, "file should exist\n");
327 static BOOL touch_file(LPCWSTR filename)
331 file = CreateFileW(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
332 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
333 if (file==INVALID_HANDLE_VALUE)
339 static BOOL is_zero_length(LPCWSTR filename)
344 file = CreateFileW(filename, GENERIC_READ, 0, NULL,
345 OPEN_EXISTING, 0, NULL);
346 if (file==INVALID_HANDLE_VALUE)
348 len = GetFileSize(file, NULL);
353 static void test_open_storage(void)
355 static const WCHAR szPrefix[] = { 's','t','g',0 };
356 static const WCHAR szNonExist[] = { 'n','o','n','e','x','i','s','t',0 };
357 static const WCHAR szDot[] = { '.',0 };
358 WCHAR filename[MAX_PATH];
359 IStorage *stg = NULL, *stg2 = NULL;
363 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
366 /* try opening a zero length file - it should stay zero length */
367 DeleteFileW(filename);
368 touch_file(filename);
369 stgm = STGM_NOSCRATCH | STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE;
370 r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
371 ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");
373 stgm = STGM_SHARE_EXCLUSIVE | STGM_READWRITE;
374 r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
375 ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");
376 ok(is_zero_length(filename), "file length changed\n");
378 DeleteFileW(filename);
380 /* create the file */
381 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
382 ok(r==S_OK, "StgCreateDocfile failed\n");
383 IStorage_Release(stg);
385 r = StgOpenStorage( filename, NULL, 0, NULL, 0, &stg);
386 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage wrong error\n");
387 r = StgOpenStorage( NULL, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
388 ok(r==STG_E_INVALIDNAME, "StgOpenStorage wrong error\n");
389 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, NULL);
390 ok(r==STG_E_INVALIDPOINTER, "StgOpenStorage wrong error\n");
391 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 1, &stg);
392 ok(r==STG_E_INVALIDPARAMETER, "StgOpenStorage wrong error\n");
393 r = StgOpenStorage( szNonExist, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
394 ok(r==STG_E_FILENOTFOUND, "StgOpenStorage failed\n");
395 r = StgOpenStorage( filename, NULL, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
396 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
397 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ, NULL, 0, &stg);
398 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
399 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_READ | STGM_READ, NULL, 0, &stg);
400 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
401 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
402 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
404 /* open it for real */
405 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg); /* XLViewer 97/2000 */
406 ok(r==S_OK, "StgOpenStorage failed\n");
409 r = IStorage_Release(stg);
410 ok(r == 0, "wrong ref count\n");
413 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READ, NULL, 0, &stg);
414 ok(r==S_OK, "StgOpenStorage failed\n");
417 r = IStorage_Release(stg);
418 ok(r == 0, "wrong ref count\n");
421 /* test the way word opens its custom dictionary */
422 r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_TRANSACTED |
423 STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
424 ok(r==S_OK, "StgOpenStorage failed\n");
427 r = IStorage_Release(stg);
428 ok(r == 0, "wrong ref count\n");
431 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
432 ok(r==S_OK, "StgOpenStorage failed\n");
433 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg2);
434 ok(r==STG_E_SHAREVIOLATION, "StgOpenStorage failed\n");
437 r = IStorage_Release(stg);
438 ok(r == 0, "wrong ref count\n");
441 /* now try write to a storage file we opened read-only */
442 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
443 ok(r==S_OK, "StgOpenStorage failed\n");
446 static const WCHAR stmname[] = { 'w','i','n','e','t','e','s','t',0};
448 IStorage *stg2 = NULL;
450 r = IStorage_CreateStream( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE,
452 ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");
453 r = IStorage_CreateStorage( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
454 ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");
456 r = IStorage_Release(stg);
457 ok(r == 0, "wrong ref count\n");
460 /* open like visio 2003 */
462 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_NONE, NULL, 0, &stg);
463 ok(r == S_OK, "should succeed\n");
465 IStorage_Release(stg);
467 /* test other sharing modes with STGM_PRIORITY */
469 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
470 ok(r == S_OK, "should succeed\n");
472 IStorage_Release(stg);
475 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
476 ok(r == S_OK, "should succeed\n");
478 IStorage_Release(stg);
481 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_READ, NULL, 0, &stg);
482 ok(r == S_OK, "should succeed\n");
484 IStorage_Release(stg);
486 /* open like Project 2003 */
488 r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg);
489 ok(r == S_OK, "should succeed\n");
490 r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg2);
491 ok(r == S_OK, "should succeed\n");
493 IStorage_Release(stg2);
495 IStorage_Release(stg);
498 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_READWRITE, NULL, 0, &stg);
499 ok(r == STG_E_INVALIDFLAG, "should fail\n");
501 r = StgOpenStorage( filename, NULL, STGM_TRANSACTED | STGM_PRIORITY, NULL, 0, &stg);
502 ok(r == STG_E_INVALIDFLAG, "should fail\n");
504 r = StgOpenStorage( filename, NULL, STGM_SIMPLE | STGM_PRIORITY, NULL, 0, &stg);
505 ok(r == STG_E_INVALIDFLAG, "should fail\n");
507 r = StgOpenStorage( filename, NULL, STGM_DELETEONRELEASE | STGM_PRIORITY, NULL, 0, &stg);
508 ok(r == STG_E_INVALIDFUNCTION, "should fail\n");
510 r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_PRIORITY, NULL, 0, &stg);
511 ok(r == STG_E_INVALIDFLAG, "should fail\n");
513 r = StgOpenStorage( filename, NULL, STGM_NOSNAPSHOT | STGM_PRIORITY, NULL, 0, &stg);
514 ok(r == STG_E_INVALIDFLAG, "should fail\n");
516 r = DeleteFileW(filename);
517 ok(r, "file didn't exist\n");
520 static void test_storage_suminfo(void)
522 static const WCHAR szDot[] = { '.',0 };
523 static const WCHAR szPrefix[] = { 's','t','g',0 };
524 WCHAR filename[MAX_PATH];
525 IStorage *stg = NULL;
526 IPropertySetStorage *propset = NULL;
527 IPropertyStorage *ps = NULL;
530 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
533 DeleteFileW(filename);
535 /* create the file */
536 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
537 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
538 ok(r==S_OK, "StgCreateDocfile failed\n");
540 r = IStorage_QueryInterface( stg, &IID_IPropertySetStorage, (LPVOID) &propset );
541 ok(r == S_OK, "query interface failed\n");
544 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
545 ok(r == STG_E_FILENOTFOUND, "deleted property set storage\n");
547 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
548 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
549 ok(r == STG_E_FILENOTFOUND, "opened property set storage\n");
551 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
552 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
553 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
555 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
557 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
559 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0, 0, &ps );
560 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
562 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
563 STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
564 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
566 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
567 STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
568 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
570 /* now try really creating a a property set */
571 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
572 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
573 ok(r == S_OK, "failed to create property set storage\n");
576 IPropertyStorage_Release(ps);
578 /* now try creating the same thing again */
579 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
580 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
581 ok(r == S_OK, "failed to create property set storage\n");
583 IPropertyStorage_Release(ps);
585 /* should be able to open it */
586 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
587 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
588 ok(r == S_OK, "open failed\n");
590 IPropertyStorage_Release(ps);
593 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
594 ok(r == S_OK, "failed to delete property set storage\n");
596 /* try opening with an invalid FMTID */
597 r = IPropertySetStorage_Open( propset, NULL,
598 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
599 ok(r == E_INVALIDARG, "open succeeded\n");
601 IPropertyStorage_Release(ps);
604 r = IPropertySetStorage_Open( propset, &IID_IStorage,
605 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
606 ok(r == STG_E_FILENOTFOUND, "open succeeded\n");
608 IPropertyStorage_Release(ps);
611 /* try some invalid flags */
612 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
613 STGM_CREATE | STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
614 ok(r == STG_E_INVALIDFLAG, "open succeeded\n");
616 IPropertyStorage_Release(ps);
618 /* after deleting it, it should be gone */
619 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
620 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
621 ok(r == STG_E_FILENOTFOUND, "open failed\n");
623 IPropertyStorage_Release(ps);
625 r = IPropertySetStorage_Release( propset );
626 ok(r == 1, "ref count wrong\n");
628 r = IStorage_Release(stg);
629 ok(r == 0, "ref count wrong\n");
631 DeleteFileW(filename);
634 static void test_storage_refcount(void)
636 static const WCHAR szPrefix[] = { 's','t','g',0 };
637 static const WCHAR szDot[] = { '.',0 };
638 WCHAR filename[MAX_PATH];
639 IStorage *stg = NULL;
640 IStorage *stgprio = NULL;
643 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
649 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
652 DeleteFileW(filename);
654 /* create the file */
655 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
656 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
657 ok(r==S_OK, "StgCreateDocfile failed\n");
659 r = WriteClassStg( stg, &test_stg_cls );
660 ok( r == S_OK, "WriteClassStg failed\n");
662 r = IStorage_Commit( stg, STGC_DEFAULT );
663 ok( r == S_OK, "IStorage_Commit failed\n");
665 /* now create a stream */
666 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
667 ok(r==S_OK, "IStorage->CreateStream failed\n");
669 r = IStorage_Release( stg );
670 ok (r == 0, "storage not released\n");
673 r = IStream_Seek( stm, pos, 0, &upos );
674 ok (r == STG_E_REVERTED, "seek should fail\n");
676 r = IStream_Stat( stm, &stat, STATFLAG_DEFAULT );
677 ok (r == STG_E_REVERTED, "stat should fail\n");
679 r = IStream_Write( stm, "Test string", strlen("Test string"), NULL);
680 ok (r == STG_E_REVERTED, "IStream_Write should return STG_E_REVERTED instead of 0x%08lx\n", r);
682 r = IStream_Read( stm, buffer, sizeof(buffer), NULL);
683 ok (r == STG_E_REVERTED, "IStream_Read should return STG_E_REVERTED instead of 0x%08lx\n", r);
685 r = IStream_Release(stm);
686 ok (r == 0, "stream not released\n");
688 /* tests that STGM_PRIORITY doesn't prevent readwrite access from other
689 * StgOpenStorage calls in transacted mode */
690 r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stgprio);
691 ok(r==S_OK, "StgOpenStorage failed with error 0x%08lx\n", r);
694 /* non-transacted mode read/write fails */
695 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg);
696 ok(r==STG_E_LOCKVIOLATION, "StgOpenStorage should return STG_E_LOCKVIOLATION instead of 0x%08lx\n", r);
699 /* non-transacted mode read-only succeeds */
700 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE|STGM_READ, NULL, 0, &stg);
701 ok(r==S_OK, "StgOpenStorage failed with error 0x%08lx\n", r);
702 IStorage_Release(stg);
704 r = StgOpenStorage( filename, NULL, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, NULL, 0, &stg);
705 ok(r==S_OK, "StgOpenStorage failed with error 0x%08lx\n", r);
708 static const WCHAR stgname[] = { ' ',' ',' ','2','9',0 };
709 static const WCHAR stgname2[] = { 'C','V','_','i','e','w',0 };
710 static const WCHAR stmname2[] = { 'V','a','r','2','D','a','t','a',0 };
715 r = IStorage_Stat( stg, &statstg, STATFLAG_NONAME );
716 ok(r == S_OK, "Stat should have succeded instead of returning 0x%08lx\n", r);
717 ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %ld\n", statstg.type);
718 ok(statstg.cbSize.LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %ld\n", statstg.cbSize.LowPart);
719 ok(statstg.cbSize.HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %ld\n", statstg.cbSize.HighPart);
720 ok(statstg.grfMode == (STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE),
721 "Statstg grfMode should have been 0x10022 instead of 0x%lx\n", statstg.grfMode);
722 ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %ld\n", statstg.grfLocksSupported);
723 ok(IsEqualCLSID(&statstg.clsid, &test_stg_cls), "Statstg clsid is not test_stg_cls\n");
724 ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %ld\n", statstg.grfStateBits);
725 ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %ld\n", statstg.reserved);
727 r = IStorage_CreateStorage( stg, stgname, STGM_SHARE_EXCLUSIVE, 0, 0, &stg2 );
728 ok(r == S_OK, "CreateStorage should have succeeded instead of returning 0x%08lx\n", r);
730 r = IStorage_Stat( stg2, &statstg, STATFLAG_DEFAULT );
731 ok(r == S_OK, "Stat should have succeded instead of returning 0x%08lx\n", r);
732 ok(!lstrcmpW(statstg.pwcsName, stgname),
733 "Statstg pwcsName should have been the name the storage was created with\n");
734 ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %ld\n", statstg.type);
735 ok(statstg.cbSize.LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %ld\n", statstg.cbSize.LowPart);
736 ok(statstg.cbSize.HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %ld\n", statstg.cbSize.HighPart);
737 ok(statstg.grfMode == STGM_SHARE_EXCLUSIVE,
738 "Statstg grfMode should have been STGM_SHARE_EXCLUSIVE instead of 0x%lx\n", statstg.grfMode);
739 ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %ld\n", statstg.grfLocksSupported);
740 ok(IsEqualCLSID(&statstg.clsid, &CLSID_NULL), "Statstg clsid is not CLSID_NULL\n");
741 ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %ld\n", statstg.grfStateBits);
742 ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %ld\n", statstg.reserved);
743 CoTaskMemFree(statstg.pwcsName);
745 r = IStorage_CreateStorage( stg2, stgname2, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, 0, &stg3 );
746 ok(r == STG_E_ACCESSDENIED, "CreateStorage should have returned STG_E_ACCESSDENIED instead of 0x%08lx\n", r);
748 r = IStorage_CreateStream( stg2, stmname2, STGM_CREATE|STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
749 ok(r == STG_E_ACCESSDENIED, "CreateStream should have returned STG_E_ACCESSDENIED instead of 0x%08lx\n", r);
751 IStorage_Release(stg2);
753 r = IStorage_Release(stg);
754 ok(r == 0, "wrong ref count\n");
756 IStorage_Release(stgprio);
758 DeleteFileW(filename);
761 START_TEST(storage32)
763 test_hglobal_storage_stat();
764 test_create_storage_modes();
765 test_storage_stream();
767 test_storage_suminfo();
768 test_storage_refcount();