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");
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 static 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
| STGM_READWRITE
, 0, NULL
);
94 ok(r
==STG_E_INVALIDPOINTER
, "StgCreateDocfile succeeded\n");
95 r
= StgCreateDocfile( filename
, STGM_CREATE
| STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 1, &stg
);
96 ok(r
==STG_E_INVALIDPARAMETER
, "StgCreateDocfile succeeded\n");
97 r
= StgCreateDocfile( filename
, STGM_CREATE
| STGM_SHARE_DENY_WRITE
| STGM_READWRITE
, 0, &stg
);
98 ok(r
==STG_E_INVALIDFLAG
, "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");
104 /* StgCreateDocfile seems to be very particular about the flags it accepts */
105 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
| STGM_WRITE
, 0, &stg
);
106 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
107 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
| 8, 0, &stg
);
108 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
109 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
| 0x80, 0, &stg
);
110 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
111 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
| 0x800, 0, &stg
);
112 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
113 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
| 0x8000, 0, &stg
);
114 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
115 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
| 0x80000, 0, &stg
);
116 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
117 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
| 0x800000, 0, &stg
);
118 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
119 ok(stg
== NULL
, "stg was set\n");
121 /* check what happens if the file already exists */
122 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, &stg
);
123 ok(r
==S_OK
, "StgCreateDocfile failed\n");
124 r
= IStorage_Release(stg
);
125 ok(r
== 0, "storage not released\n");
126 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
, 0, &stg
);
127 ok(r
==STG_E_FILEALREADYEXISTS
, "StgCreateDocfile wrong error\n");
128 r
= StgCreateDocfile( filename
, STGM_READ
, 0, &stg
);
129 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
130 r
= StgCreateDocfile( filename
, STGM_SHARE_EXCLUSIVE
, 0, &stg
);
131 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
132 r
= StgCreateDocfile( filename
, STGM_SHARE_DENY_WRITE
, 0, &stg
);
133 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile succeeded\n");
134 r
= StgCreateDocfile( filename
, STGM_SHARE_DENY_NONE
, 0, &stg
);
135 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile failed\n");
136 r
= StgCreateDocfile( filename
, STGM_SHARE_DENY_NONE
| STGM_TRANSACTED
, 0, &stg
);
137 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile failed\n");
138 r
= StgCreateDocfile( filename
, STGM_SHARE_DENY_NONE
| STGM_READWRITE
, 0, &stg
);
139 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile failed\n");
140 r
= StgCreateDocfile( filename
, STGM_SHARE_DENY_NONE
| STGM_WRITE
, 0, &stg
);
141 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile failed\n");
142 r
= StgCreateDocfile( filename
, STGM_SHARE_DENY_WRITE
| STGM_WRITE
, 0, &stg
);
143 ok(r
==STG_E_INVALIDFLAG
, "StgCreateDocfile failed\n");
144 r
= StgCreateDocfile( filename
, 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");
164 ok(DeleteFileW(filename
), "failed to delete file\n");
166 /* test the way excel uses StgCreateDocFile */
167 r
= StgCreateDocfile( filename
, STGM_TRANSACTED
|STGM_CREATE
|STGM_SHARE_DENY_WRITE
|STGM_READWRITE
, 0, &stg
);
168 ok(r
==S_OK
, "StgCreateDocfile the excel way failed\n");
171 r
= IStorage_Release(stg
);
172 ok(r
== 0, "storage not released\n");
173 ok(DeleteFileW(filename
), "failed to delete file\n");
176 /* looks like we need STGM_TRANSACTED or STGM_CREATE */
177 r
= StgCreateDocfile( filename
, STGM_TRANSACTED
|STGM_SHARE_DENY_WRITE
|STGM_READWRITE
, 0, &stg
);
178 ok(r
==S_OK
, "StgCreateDocfile the excel way failed\n");
181 r
= IStorage_Release(stg
);
182 ok(r
== 0, "storage not released\n");
183 ok(DeleteFileW(filename
), "failed to delete file\n");
186 r
= StgCreateDocfile( filename
, STGM_TRANSACTED
|STGM_CREATE
|STGM_SHARE_DENY_WRITE
|STGM_WRITE
, 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_CREATE
| STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, &stg
);
196 ok(r
==S_OK
, "StgCreateDocfile the powerpoint 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 /* test the way msi uses StgCreateDocfile */
205 r
= StgCreateDocfile( filename
, STGM_DIRECT
| STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stg
);
206 ok(r
==S_OK
, "StgCreateDocFile failed\n");
207 r
= IStorage_Release(stg
);
208 ok(r
== 0, "storage not released\n");
209 ok(DeleteFileW(filename
), "failed to delete file\n");
212 static void test_storage_stream(void)
214 static const WCHAR stmname
[] = { 'C','O','N','T','E','N','T','S',0 };
215 static const WCHAR szPrefix
[] = { 's','t','g',0 };
216 static const WCHAR szDot
[] = { '.',0 };
217 static const WCHAR longname
[] = {
218 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
219 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0
221 WCHAR filename
[MAX_PATH
];
222 IStorage
*stg
= NULL
;
228 unsigned char buffer
[0x100];
230 if(!GetTempFileNameW(szDot
, szPrefix
, 0, filename
))
233 DeleteFileW(filename
);
235 r
= StgCreateDocfile( filename
, STGM_CREATE
| STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
, 0, &stg
);
236 ok(r
==S_OK
, "StgCreateDocfile failed\n");
238 /* try create some invalid streams */
239 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 1, 0, &stm
);
240 ok(r
==STG_E_INVALIDPARAMETER
, "IStorage->CreateStream wrong error\n");
241 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 1, &stm
);
242 ok(r
==STG_E_INVALIDPARAMETER
, "IStorage->CreateStream wrong error\n");
243 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 0, NULL
);
244 ok(r
==STG_E_INVALIDPOINTER
, "IStorage->CreateStream wrong error\n");
245 r
= IStorage_CreateStream(stg
, NULL
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 0, &stm
);
246 ok(r
==STG_E_INVALIDNAME
, "IStorage->CreateStream wrong error\n");
247 r
= IStorage_CreateStream(stg
, longname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 0, &stm
);
248 ok(r
==STG_E_INVALIDNAME
, "IStorage->CreateStream wrong error, got %ld GetLastError()=%ld\n", r
, GetLastError());
249 r
= IStorage_CreateStream(stg
, stmname
, STGM_READWRITE
, 0, 0, &stm
);
250 ok(r
==STG_E_INVALIDFLAG
, "IStorage->CreateStream wrong error\n");
251 r
= IStorage_CreateStream(stg
, stmname
, STGM_READ
, 0, 0, &stm
);
252 ok(r
==STG_E_INVALIDFLAG
, "IStorage->CreateStream wrong error\n");
253 r
= IStorage_CreateStream(stg
, stmname
, STGM_WRITE
, 0, 0, &stm
);
254 ok(r
==STG_E_INVALIDFLAG
, "IStorage->CreateStream wrong error\n");
255 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_DENY_NONE
| STGM_READWRITE
, 0, 0, &stm
);
256 ok(r
==STG_E_INVALIDFLAG
, "IStorage->CreateStream wrong error\n");
257 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_DENY_NONE
| STGM_READ
, 0, 0, &stm
);
258 ok(r
==STG_E_INVALIDFLAG
, "IStorage->CreateStream wrong error\n");
260 /* now really create a stream and delete it */
261 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 0, &stm
);
262 ok(r
==S_OK
, "IStorage->CreateStream failed\n");
263 r
= IStream_Release(stm
);
264 ok(r
== 0, "wrong ref count\n");
265 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 0, &stm
);
266 ok(r
==STG_E_FILEALREADYEXISTS
, "IStorage->CreateStream failed\n");
267 r
= IStorage_DestroyElement(stg
,stmname
);
268 ok(r
==S_OK
, "IStorage->DestroyElement failed\n");
270 /* create a stream and write to it */
271 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 0, &stm
);
272 ok(r
==S_OK
, "IStorage->CreateStream failed\n");
274 r
= IStream_Write(stm
, NULL
, 0, NULL
);
275 ok(r
==STG_E_INVALIDPOINTER
, "IStream->Write wrong error\n");
276 r
= IStream_Write(stm
, "Hello\n", 0, NULL
);
277 ok(r
==S_OK
, "failed to write stream\n");
278 r
= IStream_Write(stm
, "Hello\n", 0, &count
);
279 ok(r
==S_OK
, "failed to write stream\n");
280 r
= IStream_Write(stm
, "Hello\n", 6, &count
);
281 ok(r
==S_OK
, "failed to write stream\n");
282 r
= IStream_Commit(stm
, STGC_DEFAULT
);
283 ok(r
==S_OK
, "failed to commit stream\n");
284 r
= IStream_Commit(stm
, STGC_DEFAULT
);
285 ok(r
==S_OK
, "failed to commit stream\n");
287 /* seek round a bit, reset the stream size */
289 r
= IStream_Seek(stm
, pos
, 3, &p
);
290 ok(r
==STG_E_INVALIDFUNCTION
, "IStream->Seek returned wrong error\n");
291 r
= IStream_Seek(stm
, pos
, STREAM_SEEK_SET
, NULL
);
292 ok(r
==S_OK
, "failed to seek stream\n");
293 r
= IStream_Seek(stm
, pos
, STREAM_SEEK_SET
, &p
);
294 ok(r
==S_OK
, "failed to seek stream\n");
295 r
= IStream_SetSize(stm
,p
);
296 ok(r
==S_OK
, "failed to set pos\n");
298 r
= IStream_Seek(stm
, pos
, STREAM_SEEK_SET
, &p
);
299 ok(r
==S_OK
, "failed to seek stream\n");
300 ok(p
.QuadPart
== 10, "at wrong place\n");
302 r
= IStream_Seek(stm
, pos
, STREAM_SEEK_END
, &p
);
303 ok(r
==S_OK
, "failed to seek stream\n");
304 ok(p
.QuadPart
== 0, "at wrong place\n");
305 r
= IStream_Read(stm
, buffer
, sizeof buffer
, &count
);
306 ok(r
==S_OK
, "failed to set pos\n");
307 ok(count
== 0, "read bytes from empty stream\n");
310 r
= IStream_Release(stm
);
311 ok(r
== 0, "wrong ref count\n");
312 r
= IStorage_Release(stg
);
313 ok(r
== 0, "wrong ref count\n");
314 r
= DeleteFileW(filename
);
315 ok(r
, "file should exist\n");
318 static BOOL
touch_file(LPCWSTR filename
)
322 file
= CreateFileW(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
323 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
324 if (file
==INVALID_HANDLE_VALUE
)
330 static BOOL
is_zero_length(LPCWSTR filename
)
335 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
336 OPEN_EXISTING
, 0, NULL
);
337 if (file
==INVALID_HANDLE_VALUE
)
339 len
= GetFileSize(file
, NULL
);
344 static void test_open_storage(void)
346 static const WCHAR szPrefix
[] = { 's','t','g',0 };
347 static const WCHAR szNonExist
[] = { 'n','o','n','e','x','i','s','t',0 };
348 static const WCHAR szDot
[] = { '.',0 };
349 WCHAR filename
[MAX_PATH
];
350 IStorage
*stg
= NULL
, *stg2
= NULL
;
354 if(!GetTempFileNameW(szDot
, szPrefix
, 0, filename
))
357 /* try opening a zero length file - it should stay zero length */
358 DeleteFileW(filename
);
359 touch_file(filename
);
360 stgm
= STGM_NOSCRATCH
| STGM_TRANSACTED
| STGM_SHARE_DENY_WRITE
| STGM_READWRITE
;
361 r
= StgOpenStorage( filename
, NULL
, stgm
, NULL
, 0, &stg
);
362 ok(r
==STG_E_FILEALREADYEXISTS
, "StgOpenStorage didn't fail\n");
364 stgm
= STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
;
365 r
= StgOpenStorage( filename
, NULL
, stgm
, NULL
, 0, &stg
);
366 ok(r
==STG_E_FILEALREADYEXISTS
, "StgOpenStorage didn't fail\n");
367 ok(is_zero_length(filename
), "file length changed\n");
369 DeleteFileW(filename
);
371 /* create the file */
372 r
= StgCreateDocfile( filename
, STGM_CREATE
| STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
|STGM_TRANSACTED
, 0, &stg
);
373 ok(r
==S_OK
, "StgCreateDocfile failed\n");
374 IStorage_Release(stg
);
376 r
= StgOpenStorage( filename
, NULL
, 0, NULL
, 0, &stg
);
377 ok(r
==STG_E_INVALIDFLAG
, "StgOpenStorage wrong error\n");
378 r
= StgOpenStorage( NULL
, NULL
, STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
379 ok(r
==STG_E_INVALIDNAME
, "StgOpenStorage wrong error\n");
380 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_EXCLUSIVE
| STGM_READ
, NULL
, 0, NULL
);
381 ok(r
==STG_E_INVALIDPOINTER
, "StgOpenStorage wrong error\n");
382 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_EXCLUSIVE
| STGM_READ
, NULL
, 1, &stg
);
383 ok(r
==STG_E_INVALIDPARAMETER
, "StgOpenStorage wrong error\n");
384 r
= StgOpenStorage( szNonExist
, NULL
, STGM_SHARE_EXCLUSIVE
| STGM_READ
, NULL
, 0, &stg
);
385 ok(r
==STG_E_FILENOTFOUND
, "StgOpenStorage failed\n");
386 r
= StgOpenStorage( filename
, NULL
, STGM_CREATE
| STGM_SHARE_EXCLUSIVE
| STGM_READ
, NULL
, 0, &stg
);
387 ok(r
==STG_E_INVALIDFLAG
, "StgOpenStorage failed\n");
388 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_DENY_NONE
| STGM_READ
, NULL
, 0, &stg
);
389 ok(r
==STG_E_INVALIDFLAG
, "StgOpenStorage failed\n");
390 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_DENY_READ
| STGM_READ
, NULL
, 0, &stg
);
391 ok(r
==STG_E_INVALIDFLAG
, "StgOpenStorage failed\n");
392 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_DENY_WRITE
| STGM_READWRITE
, NULL
, 0, &stg
);
393 ok(r
==STG_E_INVALIDFLAG
, "StgOpenStorage failed\n");
395 /* open it for real */
396 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_DENY_NONE
| STGM_READ
| STGM_TRANSACTED
, NULL
, 0, &stg
); /* XLViewer 97/2000 */
397 ok(r
==S_OK
, "StgOpenStorage failed\n");
400 r
= IStorage_Release(stg
);
401 ok(r
== 0, "wrong ref count\n");
404 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_DENY_WRITE
| STGM_READ
, NULL
, 0, &stg
);
405 ok(r
==S_OK
, "StgOpenStorage failed\n");
408 r
= IStorage_Release(stg
);
409 ok(r
== 0, "wrong ref count\n");
412 /* test the way word opens its custom dictionary */
413 r
= StgOpenStorage( filename
, NULL
, STGM_NOSCRATCH
| STGM_TRANSACTED
|
414 STGM_SHARE_DENY_WRITE
| STGM_READWRITE
, NULL
, 0, &stg
);
415 ok(r
==S_OK
, "StgOpenStorage failed\n");
418 r
= IStorage_Release(stg
);
419 ok(r
== 0, "wrong ref count\n");
422 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_EXCLUSIVE
| STGM_READ
, NULL
, 0, &stg
);
423 ok(r
==S_OK
, "StgOpenStorage failed\n");
424 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_EXCLUSIVE
| STGM_READ
, NULL
, 0, &stg2
);
425 ok(r
==STG_E_SHAREVIOLATION
, "StgOpenStorage failed\n");
428 r
= IStorage_Release(stg
);
429 ok(r
== 0, "wrong ref count\n");
432 /* now try write to a storage file we opened read-only */
433 r
= StgOpenStorage( filename
, NULL
, STGM_SHARE_EXCLUSIVE
| STGM_READ
, NULL
, 0, &stg
);
434 ok(r
==S_OK
, "StgOpenStorage failed\n");
437 static const WCHAR stmname
[] = { 'w','i','n','e','t','e','s','t',0};
439 IStorage
*stg2
= NULL
;
441 r
= IStorage_CreateStream( stg
, stmname
, STGM_WRITE
| STGM_SHARE_EXCLUSIVE
,
443 ok(r
== STG_E_ACCESSDENIED
, "CreateStream should fail\n");
444 r
= IStorage_CreateStorage( stg
, stmname
, STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stg2
);
445 ok(r
== STG_E_ACCESSDENIED
, "CreateStream should fail\n");
447 r
= IStorage_Release(stg
);
448 ok(r
== 0, "wrong ref count\n");
451 /* open like visio 2003 */
453 r
= StgOpenStorage( filename
, NULL
, STGM_PRIORITY
| STGM_SHARE_DENY_NONE
, NULL
, 0, &stg
);
454 ok(r
== S_OK
, "should succeed\n");
456 IStorage_Release(stg
);
458 /* test other sharing modes with STGM_PRIORITY */
460 r
= StgOpenStorage( filename
, NULL
, STGM_PRIORITY
| STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
461 ok(r
== S_OK
, "should succeed\n");
463 IStorage_Release(stg
);
466 r
= StgOpenStorage( filename
, NULL
, STGM_PRIORITY
| STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
467 ok(r
== S_OK
, "should succeed\n");
469 IStorage_Release(stg
);
472 r
= StgOpenStorage( filename
, NULL
, STGM_PRIORITY
| STGM_SHARE_DENY_READ
, NULL
, 0, &stg
);
473 ok(r
== S_OK
, "should succeed\n");
475 IStorage_Release(stg
);
477 /* open like Project 2003 */
479 r
= StgOpenStorage( filename
, NULL
, STGM_PRIORITY
, NULL
, 0, &stg
);
480 ok(r
== S_OK
, "should succeed\n");
481 r
= StgOpenStorage( filename
, NULL
, STGM_PRIORITY
, NULL
, 0, &stg2
);
482 ok(r
== S_OK
, "should succeed\n");
484 IStorage_Release(stg2
);
486 IStorage_Release(stg
);
489 r
= StgOpenStorage( filename
, NULL
, STGM_PRIORITY
| STGM_READWRITE
, NULL
, 0, &stg
);
490 ok(r
== STG_E_INVALIDFLAG
, "should fail\n");
492 r
= StgOpenStorage( filename
, NULL
, STGM_TRANSACTED
| STGM_PRIORITY
, NULL
, 0, &stg
);
493 ok(r
== STG_E_INVALIDFLAG
, "should fail\n");
495 r
= StgOpenStorage( filename
, NULL
, STGM_SIMPLE
| STGM_PRIORITY
, NULL
, 0, &stg
);
496 ok(r
== STG_E_INVALIDFLAG
, "should fail\n");
498 r
= StgOpenStorage( filename
, NULL
, STGM_DELETEONRELEASE
| STGM_PRIORITY
, NULL
, 0, &stg
);
499 ok(r
== STG_E_INVALIDFUNCTION
, "should fail\n");
501 r
= StgOpenStorage( filename
, NULL
, STGM_NOSCRATCH
| STGM_PRIORITY
, NULL
, 0, &stg
);
502 ok(r
== STG_E_INVALIDFLAG
, "should fail\n");
504 r
= StgOpenStorage( filename
, NULL
, STGM_NOSNAPSHOT
| STGM_PRIORITY
, NULL
, 0, &stg
);
505 ok(r
== STG_E_INVALIDFLAG
, "should fail\n");
507 r
= DeleteFileW(filename
);
508 ok(r
, "file didn't exist\n");
511 static void test_storage_suminfo(void)
513 static const WCHAR szDot
[] = { '.',0 };
514 static const WCHAR szPrefix
[] = { 's','t','g',0 };
515 WCHAR filename
[MAX_PATH
];
516 IStorage
*stg
= NULL
;
517 IPropertySetStorage
*propset
= NULL
;
518 IPropertyStorage
*ps
= NULL
;
521 if(!GetTempFileNameW(szDot
, szPrefix
, 0, filename
))
524 DeleteFileW(filename
);
526 /* create the file */
527 r
= StgCreateDocfile( filename
, STGM_CREATE
| STGM_SHARE_EXCLUSIVE
|
528 STGM_READWRITE
|STGM_TRANSACTED
, 0, &stg
);
529 ok(r
==S_OK
, "StgCreateDocfile failed\n");
531 r
= IStorage_QueryInterface( stg
, &IID_IPropertySetStorage
, (LPVOID
) &propset
);
532 ok(r
== S_OK
, "query interface failed\n");
535 r
= IPropertySetStorage_Delete( propset
, &FMTID_SummaryInformation
);
536 ok(r
== STG_E_FILENOTFOUND
, "deleted property set storage\n");
538 r
= IPropertySetStorage_Open( propset
, &FMTID_SummaryInformation
,
539 STGM_READ
| STGM_SHARE_EXCLUSIVE
, &ps
);
540 ok(r
== STG_E_FILENOTFOUND
, "opened property set storage\n");
542 r
= IPropertySetStorage_Create( propset
, &FMTID_SummaryInformation
, NULL
, 0,
543 STGM_READ
| STGM_SHARE_EXCLUSIVE
, &ps
);
544 ok(r
== STG_E_INVALIDFLAG
, "created property set storage\n");
546 r
= IPropertySetStorage_Create( propset
, &FMTID_SummaryInformation
, NULL
, 0,
548 ok(r
== STG_E_INVALIDFLAG
, "created property set storage\n");
550 r
= IPropertySetStorage_Create( propset
, &FMTID_SummaryInformation
, NULL
, 0, 0, &ps
);
551 ok(r
== STG_E_INVALIDFLAG
, "created property set storage\n");
553 r
= IPropertySetStorage_Create( propset
, &FMTID_SummaryInformation
, NULL
, 0,
554 STGM_WRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
555 ok(r
== STG_E_INVALIDFLAG
, "created property set storage\n");
557 r
= IPropertySetStorage_Create( propset
, &FMTID_SummaryInformation
, NULL
, 0,
558 STGM_CREATE
|STGM_WRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
559 ok(r
== STG_E_INVALIDFLAG
, "created property set storage\n");
561 /* now try really creating a a property set */
562 r
= IPropertySetStorage_Create( propset
, &FMTID_SummaryInformation
, NULL
, 0,
563 STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
564 ok(r
== S_OK
, "failed to create property set storage\n");
567 IPropertyStorage_Release(ps
);
569 /* now try creating the same thing again */
570 r
= IPropertySetStorage_Create( propset
, &FMTID_SummaryInformation
, NULL
, 0,
571 STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
572 ok(r
== S_OK
, "failed to create property set storage\n");
574 IPropertyStorage_Release(ps
);
576 /* should be able to open it */
577 r
= IPropertySetStorage_Open( propset
, &FMTID_SummaryInformation
,
578 STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
579 ok(r
== S_OK
, "open failed\n");
581 IPropertyStorage_Release(ps
);
584 r
= IPropertySetStorage_Delete( propset
, &FMTID_SummaryInformation
);
585 ok(r
== S_OK
, "failed to delete property set storage\n");
587 /* try opening with an invalid FMTID */
588 r
= IPropertySetStorage_Open( propset
, NULL
,
589 STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
590 ok(r
== E_INVALIDARG
, "open succeeded\n");
592 IPropertyStorage_Release(ps
);
595 r
= IPropertySetStorage_Open( propset
, &IID_IStorage
,
596 STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
597 ok(r
== STG_E_FILENOTFOUND
, "open succeeded\n");
599 IPropertyStorage_Release(ps
);
602 /* try some invalid flags */
603 r
= IPropertySetStorage_Open( propset
, &FMTID_SummaryInformation
,
604 STGM_CREATE
| STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
605 ok(r
== STG_E_INVALIDFLAG
, "open succeeded\n");
607 IPropertyStorage_Release(ps
);
609 /* after deleting it, it should be gone */
610 r
= IPropertySetStorage_Open( propset
, &FMTID_SummaryInformation
,
611 STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, &ps
);
612 ok(r
== STG_E_FILENOTFOUND
, "open failed\n");
614 IPropertyStorage_Release(ps
);
616 r
= IPropertySetStorage_Release( propset
);
617 ok(r
== 1, "ref count wrong\n");
619 r
= IStorage_Release(stg
);
620 ok(r
== 0, "ref count wrong\n");
622 DeleteFileW(filename
);
625 static void test_storage_refcount(void)
627 static const WCHAR szPrefix
[] = { 's','t','g',0 };
628 static const WCHAR szDot
[] = { '.',0 };
629 WCHAR filename
[MAX_PATH
];
630 IStorage
*stg
= NULL
;
633 static const WCHAR stmname
[] = { 'C','O','N','T','E','N','T','S',0 };
639 if(!GetTempFileNameW(szDot
, szPrefix
, 0, filename
))
642 DeleteFileW(filename
);
644 /* create the file */
645 r
= StgCreateDocfile( filename
, STGM_CREATE
| STGM_SHARE_EXCLUSIVE
|
646 STGM_READWRITE
|STGM_TRANSACTED
, 0, &stg
);
647 ok(r
==S_OK
, "StgCreateDocfile failed\n");
649 /* now create a stream */
650 r
= IStorage_CreateStream(stg
, stmname
, STGM_SHARE_EXCLUSIVE
| STGM_READWRITE
, 0, 0, &stm
);
651 ok(r
==S_OK
, "IStorage->CreateStream failed\n");
653 r
= IStorage_Release( stg
);
654 ok (r
== 0, "storage not released\n");
657 r
= IStream_Seek( stm
, pos
, 0, &upos
);
658 ok (r
== STG_E_REVERTED
, "seek should fail\n");
660 r
= IStream_Stat( stm
, &stat
, STATFLAG_DEFAULT
);
661 ok (r
== STG_E_REVERTED
, "stat should fail\n");
663 r
= IStream_Write( stm
, "Test string", strlen("Test string"), NULL
);
664 ok (r
== STG_E_REVERTED
, "IStream_Write should return STG_E_REVERTED instead of 0x%08lx\n", r
);
666 r
= IStream_Read( stm
, buffer
, sizeof(buffer
), NULL
);
667 ok (r
== STG_E_REVERTED
, "IStream_Read should return STG_E_REVERTED instead of 0x%08lx\n", r
);
669 r
= IStream_Release(stm
);
670 ok (r
== 0, "stream not released\n");
672 /* test for grfMode open issue */
674 r
= StgOpenStorage( filename
, NULL
, STGM_TRANSACTED
|STGM_SHARE_DENY_WRITE
|STGM_READWRITE
, NULL
, 0, &stg
);
675 ok(r
==S_OK
, "StgOpenStorage failed\n");
678 static const WCHAR stgname
[] = { ' ',' ',' ','2','9',0 };
679 static const WCHAR stgname2
[] = { 'C','V','_','i','e','w',0 };
680 static const WCHAR stmname2
[] = { 'V','a','r','2','D','a','t','a',0 };
684 r
= IStorage_CreateStorage( stg
, stgname
, STGM_SHARE_EXCLUSIVE
, 0, 0, &stg2
);
685 ok(r
== S_OK
, "CreateStorage should have succeeded instead of returning 0x%08lx\n", r
);
688 r
= IStorage_CreateStorage( stg2
, stgname2
, STGM_SHARE_EXCLUSIVE
|STGM_READWRITE
, 0, 0, &stg3
);
689 ok(r
== STG_E_ACCESSDENIED
, "CreateStorage should have returned STG_E_ACCESSDENIED instead of 0x%08lx\n", r
);
691 r
= IStorage_CreateStream( stg2
, stmname2
, STGM_CREATE
|STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
692 ok(r
== STG_E_ACCESSDENIED
, "CreateStream should have returned STG_E_ACCESSDENIED instead of 0x%08lx\n", r
);
694 IStorage_Release(stg2
);
696 r
= IStorage_Release(stg
);
697 ok(r
== 0, "wrong ref count\n");
701 DeleteFileW(filename
);
704 START_TEST(storage32
)
706 test_hglobal_storage_stat();
707 test_create_storage_modes();
708 test_storage_stream();
710 test_storage_suminfo();
711 test_storage_refcount();