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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/test.h"
32 DEFINE_GUID( test_stg_cls, 0x88888888, 0x0425, 0x0000, 0,0,0,0,0,0,0,0);
34 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
36 static void test_hglobal_storage_stat(void)
38 ILockBytes *ilb = NULL;
44 r = CreateILockBytesOnHGlobal( NULL, TRUE, &ilb );
45 ok( r == S_OK, "CreateILockBytesOnHGlobal failed\n");
47 mode = STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE;/*0x1012*/
48 r = StgCreateDocfileOnILockBytes( ilb, mode, 0, &stg );
49 ok( r == S_OK, "StgCreateDocfileOnILockBytes failed\n");
51 r = WriteClassStg( stg, &test_stg_cls );
52 ok( r == S_OK, "WriteClassStg failed\n");
54 memset( &stat, 0, sizeof stat );
55 r = IStorage_Stat( stg, &stat, 0 );
57 ok( stat.pwcsName == NULL, "storage name not null\n");
58 ok( stat.type == 1, "type is wrong\n");
59 ok( stat.grfMode == 0x12, "grf mode is incorrect\n");
60 ok( !memcmp(&stat.clsid, &test_stg_cls, sizeof test_stg_cls), "CLSID is wrong\n");
62 refcount = IStorage_Release( stg );
63 ok( refcount == 0, "IStorage refcount is wrong\n");
64 refcount = ILockBytes_Release( ilb );
65 ok( refcount == 0, "ILockBytes refcount is wrong\n");
68 static void test_create_storage_modes(void)
70 static const WCHAR szPrefix[] = { 's','t','g',0 };
71 static const WCHAR szDot[] = { '.',0 };
72 WCHAR filename[MAX_PATH];
76 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
79 DeleteFileW(filename);
81 /* test with some invalid parameters */
82 r = StgCreateDocfile( NULL, 0, 0, &stg);
83 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
84 r = StgCreateDocfile( filename, 0, 0, &stg);
85 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
86 r = StgCreateDocfile( filename, STGM_CREATE, 0, &stg);
87 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
88 r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE, 0, &stg);
89 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
90 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &stg);
91 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
92 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, NULL);
93 ok(r==STG_E_INVALIDPOINTER, "StgCreateDocfile succeeded\n");
94 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, &stg);
95 ok(r==STG_E_INVALIDPARAMETER, "StgCreateDocfile succeeded\n");
96 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
97 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
98 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stg);
99 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
100 r = StgCreateDocfile( filename, STGM_PRIORITY, 0, &stg);
101 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
103 /* StgCreateDocfile seems to be very particular about the flags it accepts */
104 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | STGM_WRITE, 0, &stg);
105 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
106 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 8, 0, &stg);
107 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
108 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80, 0, &stg);
109 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
110 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800, 0, &stg);
111 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
112 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x8000, 0, &stg);
113 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
114 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80000, 0, &stg);
115 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
116 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800000, 0, &stg);
117 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
118 ok(stg == NULL, "stg was set\n");
120 /* check what happens if the file already exists (which is how it's meant to be used) */
121 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
122 ok(r==S_OK, "StgCreateDocfile failed\n");
123 r = IStorage_Release(stg);
124 ok(r == 0, "storage not released\n");
125 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
126 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n"); /* FAILIFTHERE is default */
127 r = StgCreateDocfile( filename, STGM_READ, 0, &stg);
128 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n"); /* need at least readmode and sharemode */
129 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE, 0, &stg);
130 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
131 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE, 0, &stg);
132 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
133 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE, 0, &stg);
134 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
135 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
136 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
137 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, &stg);
138 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
139 r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_WRITE, 0, &stg);
140 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
141 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_WRITE, 0, &stg);
142 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
143 r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
144 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
145 r = StgCreateDocfile( filename, STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
146 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
147 ok(DeleteFileW(filename), "failed to delete file\n");
149 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
150 ok(r==S_OK, "StgCreateDocfile failed\n");
151 r = IStorage_Release(stg);
152 ok(r == 0, "storage not released\n");
153 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED |STGM_FAILIFTHERE, 0, &stg);
154 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
155 r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_WRITE, 0, &stg);
156 ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
158 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
159 ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
160 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
161 ok(r==S_OK, "StgCreateDocfile failed\n");
162 r = IStorage_Release(stg);
163 ok(r == 0, "storage not released\n");
164 ok(DeleteFileW(filename), "failed to delete file\n");
166 r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
167 ok(r==S_OK, "StgCreateDocfile failed\n");
168 r = IStorage_Release(stg);
169 ok(r == 0, "storage not released\n");
170 ok(DeleteFileW(filename), "failed to delete file\n");
172 /* test the way excel uses StgCreateDocFile */
173 r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
174 ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
177 r = IStorage_Release(stg);
178 ok(r == 0, "storage not released\n");
179 ok(DeleteFileW(filename), "failed to delete file\n");
182 /* and the way windows media uses it ... */
183 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_NONE | STGM_READWRITE | STGM_TRANSACTED, 0, &stg);
184 ok(r==S_OK, "StgCreateDocfile the windows media way failed\n");
187 r = IStorage_Release(stg);
188 ok(r == 0, "storage not released\n");
189 ok(DeleteFileW(filename), "failed to delete file\n");
192 /* looks like we need STGM_TRANSACTED or STGM_CREATE */
193 r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
194 ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
197 r = IStorage_Release(stg);
198 ok(r == 0, "storage not released\n");
199 ok(DeleteFileW(filename), "failed to delete file\n");
202 r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_WRITE, 0, &stg);
203 ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
206 r = IStorage_Release(stg);
207 ok(r == 0, "storage not released\n");
208 ok(DeleteFileW(filename), "failed to delete file\n");
211 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
212 ok(r==S_OK, "StgCreateDocfile the powerpoint way failed\n");
215 r = IStorage_Release(stg);
216 ok(r == 0, "storage not released\n");
217 ok(DeleteFileW(filename), "failed to delete file\n");
220 /* test the way msi uses StgCreateDocfile */
221 r = StgCreateDocfile( filename, STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stg);
222 ok(r==S_OK, "StgCreateDocFile failed\n");
223 r = IStorage_Release(stg);
224 ok(r == 0, "storage not released\n");
225 ok(DeleteFileW(filename), "failed to delete file\n");
228 static void test_storage_stream(void)
230 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
231 static const WCHAR szPrefix[] = { 's','t','g',0 };
232 static const WCHAR szDot[] = { '.',0 };
233 static const WCHAR longname[] = {
234 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
235 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0
237 WCHAR filename[MAX_PATH];
238 IStorage *stg = NULL;
241 IStream *stm2 = NULL;
245 unsigned char buffer[0x100];
247 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
250 DeleteFileW(filename);
252 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
253 ok(r==S_OK, "StgCreateDocfile failed\n");
255 /* try create some invalid streams */
256 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, 0, &stm );
257 ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
258 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 1, &stm );
259 ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
260 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, NULL );
261 ok(r==STG_E_INVALIDPOINTER, "IStorage->CreateStream wrong error\n");
262 r = IStorage_CreateStream(stg, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
263 ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
264 r = IStorage_CreateStream(stg, longname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
265 ok(r==STG_E_INVALIDNAME || broken(r==S_OK) /* nt4 */,
266 "IStorage->CreateStream wrong error, got %d GetLastError()=%d\n", r, GetLastError());
267 r = IStorage_CreateStream(stg, stmname, STGM_READWRITE, 0, 0, &stm );
268 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
269 r = IStorage_CreateStream(stg, stmname, STGM_READ, 0, 0, &stm );
270 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
271 r = IStorage_CreateStream(stg, stmname, STGM_WRITE, 0, 0, &stm );
272 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
273 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, 0, &stm );
274 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
275 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READ, 0, 0, &stm );
276 ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
278 /* now really create a stream and delete it */
279 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
280 ok(r==S_OK, "IStorage->CreateStream failed\n");
281 r = IStream_Release(stm);
282 ok(r == 0, "wrong ref count\n");
283 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
284 ok(r==STG_E_FILEALREADYEXISTS, "IStorage->CreateStream failed\n");
285 r = IStorage_DestroyElement(stg,stmname);
286 ok(r==S_OK, "IStorage->DestroyElement failed\n");
288 /* create a stream and write to it */
289 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
290 ok(r==S_OK, "IStorage->CreateStream failed\n");
292 r = IStream_Clone(stm, &stm2);
293 ok(r==S_OK, "failed to clone stream\n");
295 r = IStream_Write(stm, NULL, 0, NULL );
296 ok(r==STG_E_INVALIDPOINTER, "IStream->Write wrong error\n");
297 r = IStream_Write(stm, "Hello\n", 0, NULL );
298 ok(r==S_OK, "failed to write stream\n");
299 r = IStream_Write(stm, "Hello\n", 0, &count );
300 ok(r==S_OK, "failed to write stream\n");
301 r = IStream_Write(stm, "Hello\n", 6, &count );
302 ok(r==S_OK, "failed to write stream\n");
303 r = IStream_Commit(stm, STGC_DEFAULT );
304 ok(r==S_OK, "failed to commit stream\n");
305 r = IStream_Commit(stm, STGC_DEFAULT );
306 ok(r==S_OK, "failed to commit stream\n");
308 /* seek round a bit, reset the stream size */
310 r = IStream_Seek(stm, pos, 3, &p );
311 ok(r==STG_E_INVALIDFUNCTION, "IStream->Seek returned wrong error\n");
312 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, NULL);
313 ok(r==S_OK, "failed to seek stream\n");
314 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
315 ok(r==S_OK, "failed to seek stream\n");
316 r = IStream_SetSize(stm,p);
317 ok(r==S_OK, "failed to set pos\n");
319 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
320 ok(r==S_OK, "failed to seek stream\n");
321 ok(p.QuadPart == 10, "at wrong place\n");
323 r = IStream_Seek(stm, pos, STREAM_SEEK_END, &p );
324 ok(r==S_OK, "failed to seek stream\n");
325 ok(p.QuadPart == 0, "at wrong place\n");
326 r = IStream_Read(stm, buffer, sizeof buffer, &count );
327 ok(r==S_OK, "failed to set pos\n");
328 ok(count == 0, "read bytes from empty stream\n");
331 r = IStream_Release(stm2);
332 ok(r == 0, "wrong ref count\n");
334 /* create a stream and write to it */
335 r = IStorage_CreateStream(stg, stmname, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm2 );
336 ok(r==S_OK, "IStorage->CreateStream failed\n");
338 r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p);
339 ok(r==STG_E_REVERTED, "overwritten stream should return STG_E_REVERTED instead of 0x%08x\n", r);
341 r = IStream_Release(stm2);
342 ok(r == 0, "wrong ref count\n");
343 r = IStream_Release(stm);
344 ok(r == 0, "wrong ref count\n");
346 r = IStorage_Release(stg);
347 ok(r == 0, "wrong ref count\n");
348 r = DeleteFileW(filename);
349 ok(r, "file should exist\n");
352 static BOOL touch_file(LPCWSTR filename)
356 file = CreateFileW(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
357 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
358 if (file==INVALID_HANDLE_VALUE)
364 static BOOL is_zero_length(LPCWSTR filename)
369 file = CreateFileW(filename, GENERIC_READ, 0, NULL,
370 OPEN_EXISTING, 0, NULL);
371 if (file==INVALID_HANDLE_VALUE)
373 len = GetFileSize(file, NULL);
378 static BOOL is_existing_file(LPCWSTR filename)
382 file = CreateFileW(filename, GENERIC_READ, 0, NULL,
383 OPEN_EXISTING, 0, NULL);
384 if (file==INVALID_HANDLE_VALUE)
390 static void test_open_storage(void)
392 static const WCHAR szPrefix[] = { 's','t','g',0 };
393 static const WCHAR szNonExist[] = { 'n','o','n','e','x','i','s','t',0 };
394 static const WCHAR szDot[] = { '.',0 };
395 WCHAR filename[MAX_PATH];
396 IStorage *stg = NULL, *stg2 = NULL;
400 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
403 /* try opening a zero length file - it should stay zero length */
404 DeleteFileW(filename);
405 touch_file(filename);
406 stgm = STGM_NOSCRATCH | STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE;
407 r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
408 ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");
410 stgm = STGM_SHARE_EXCLUSIVE | STGM_READWRITE;
411 r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
412 ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");
413 ok(is_zero_length(filename), "file length changed\n");
415 DeleteFileW(filename);
417 /* try opening a nonexistent file - it should not create it */
418 stgm = STGM_DIRECT | STGM_SHARE_EXCLUSIVE | STGM_READWRITE;
419 r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
420 ok(r!=S_OK, "StgOpenStorage failed: 0x%08x\n", r);
421 if (r==S_OK) IStorage_Release(stg);
422 ok(!is_existing_file(filename), "StgOpenStorage should not create a file\n");
423 DeleteFileW(filename);
425 /* create the file */
426 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
427 ok(r==S_OK, "StgCreateDocfile failed\n");
428 IStorage_Release(stg);
430 r = StgOpenStorage( filename, NULL, 0, NULL, 0, &stg);
431 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage wrong error\n");
432 r = StgOpenStorage( NULL, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
433 ok(r==STG_E_INVALIDNAME, "StgOpenStorage wrong error\n");
434 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, NULL);
435 ok(r==STG_E_INVALIDPOINTER, "StgOpenStorage wrong error\n");
436 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 1, &stg);
437 ok(r==STG_E_INVALIDPARAMETER, "StgOpenStorage wrong error\n");
438 r = StgOpenStorage( szNonExist, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
439 ok(r==STG_E_FILENOTFOUND, "StgOpenStorage failed\n");
440 r = StgOpenStorage( filename, NULL, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
441 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
442 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ, NULL, 0, &stg);
443 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
444 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_READ | STGM_READ, NULL, 0, &stg);
445 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
446 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
447 ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
449 /* open it for real */
450 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg); /* XLViewer 97/2000 */
451 ok(r==S_OK, "StgOpenStorage failed\n");
454 r = IStorage_Release(stg);
455 ok(r == 0, "wrong ref count\n");
458 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READ, NULL, 0, &stg);
459 ok(r==S_OK, "StgOpenStorage failed\n");
462 r = IStorage_Release(stg);
463 ok(r == 0, "wrong ref count\n");
466 /* test the way word opens its custom dictionary */
467 r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_TRANSACTED |
468 STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
469 ok(r==S_OK, "StgOpenStorage failed\n");
472 r = IStorage_Release(stg);
473 ok(r == 0, "wrong ref count\n");
476 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
477 ok(r==S_OK, "StgOpenStorage failed\n");
478 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg2);
479 ok(r==STG_E_SHAREVIOLATION, "StgOpenStorage failed\n");
482 r = IStorage_Release(stg);
483 ok(r == 0, "wrong ref count\n");
486 /* now try write to a storage file we opened read-only */
487 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
488 ok(r==S_OK, "StgOpenStorage failed\n");
491 static const WCHAR stmname[] = { 'w','i','n','e','t','e','s','t',0};
493 IStorage *stg2 = NULL;
495 r = IStorage_CreateStream( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE,
497 ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");
498 r = IStorage_CreateStorage( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
499 ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");
501 r = IStorage_Release(stg);
502 ok(r == 0, "wrong ref count\n");
505 /* open like visio 2003 */
507 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_NONE, NULL, 0, &stg);
508 ok(r == S_OK, "should succeed\n");
510 IStorage_Release(stg);
512 /* test other sharing modes with STGM_PRIORITY */
514 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
515 ok(r == S_OK, "should succeed\n");
517 IStorage_Release(stg);
520 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
521 ok(r == S_OK, "should succeed\n");
523 IStorage_Release(stg);
526 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_READ, NULL, 0, &stg);
527 ok(r == S_OK, "should succeed\n");
529 IStorage_Release(stg);
531 /* open like Project 2003 */
533 r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg);
534 ok(r == S_OK, "should succeed\n");
535 r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg2);
536 ok(r == S_OK, "should succeed\n");
538 IStorage_Release(stg2);
540 IStorage_Release(stg);
543 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_READWRITE, NULL, 0, &stg);
544 ok(r == STG_E_INVALIDFLAG, "should fail\n");
546 r = StgOpenStorage( filename, NULL, STGM_TRANSACTED | STGM_PRIORITY, NULL, 0, &stg);
547 ok(r == STG_E_INVALIDFLAG, "should fail\n");
549 r = StgOpenStorage( filename, NULL, STGM_SIMPLE | STGM_PRIORITY, NULL, 0, &stg);
550 ok(r == STG_E_INVALIDFLAG, "should fail\n");
552 r = StgOpenStorage( filename, NULL, STGM_DELETEONRELEASE | STGM_PRIORITY, NULL, 0, &stg);
553 ok(r == STG_E_INVALIDFUNCTION, "should fail\n");
555 r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_PRIORITY, NULL, 0, &stg);
556 ok(r == STG_E_INVALIDFLAG, "should fail\n");
558 r = StgOpenStorage( filename, NULL, STGM_NOSNAPSHOT | STGM_PRIORITY, NULL, 0, &stg);
559 ok(r == STG_E_INVALIDFLAG, "should fail\n");
561 r = DeleteFileW(filename);
562 ok(r, "file didn't exist\n");
565 static void test_storage_suminfo(void)
567 static const WCHAR szDot[] = { '.',0 };
568 static const WCHAR szPrefix[] = { 's','t','g',0 };
569 WCHAR filename[MAX_PATH];
570 IStorage *stg = NULL;
571 IPropertySetStorage *propset = NULL;
572 IPropertyStorage *ps = NULL;
575 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
578 DeleteFileW(filename);
580 /* create the file */
581 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
582 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
583 ok(r==S_OK, "StgCreateDocfile failed\n");
585 r = IStorage_QueryInterface( stg, &IID_IPropertySetStorage, (LPVOID) &propset );
586 ok(r == S_OK, "query interface failed\n");
589 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
590 ok(r == STG_E_FILENOTFOUND, "deleted property set storage\n");
592 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
593 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
594 ok(r == STG_E_FILENOTFOUND, "opened property set storage\n");
596 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
597 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
598 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
600 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
602 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
604 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0, 0, &ps );
605 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
607 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
608 STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
609 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
611 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
612 STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
613 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
615 /* now try really creating a property set */
616 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
617 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
618 ok(r == S_OK, "failed to create property set storage\n");
621 IPropertyStorage_Release(ps);
623 /* now try creating the same thing again */
624 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
625 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
626 ok(r == S_OK, "failed to create property set storage\n");
628 IPropertyStorage_Release(ps);
630 /* should be able to open it */
631 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
632 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
633 ok(r == S_OK, "open failed\n");
635 IPropertyStorage_Release(ps);
638 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
639 ok(r == S_OK, "failed to delete property set storage\n");
641 /* try opening with an invalid FMTID */
642 r = IPropertySetStorage_Open( propset, NULL,
643 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
644 ok(r == E_INVALIDARG, "open succeeded\n");
646 IPropertyStorage_Release(ps);
649 r = IPropertySetStorage_Open( propset, &IID_IStorage,
650 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
651 ok(r == STG_E_FILENOTFOUND, "open succeeded\n");
653 IPropertyStorage_Release(ps);
656 /* try some invalid flags */
657 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
658 STGM_CREATE | STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
659 ok(r == STG_E_INVALIDFLAG, "open succeeded\n");
661 IPropertyStorage_Release(ps);
663 /* after deleting it, it should be gone */
664 r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation,
665 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
666 ok(r == STG_E_FILENOTFOUND, "open failed\n");
668 IPropertyStorage_Release(ps);
670 r = IPropertySetStorage_Release( propset );
671 ok(r == 1, "ref count wrong\n");
673 r = IStorage_Release(stg);
674 ok(r == 0, "ref count wrong\n");
676 DeleteFileW(filename);
679 static void test_storage_refcount(void)
681 static const WCHAR szPrefix[] = { 's','t','g',0 };
682 static const WCHAR szDot[] = { '.',0 };
683 WCHAR filename[MAX_PATH];
684 IStorage *stg = NULL;
685 IStorage *stgprio = NULL;
688 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
694 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
697 DeleteFileW(filename);
699 /* create the file */
700 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
701 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
702 ok(r==S_OK, "StgCreateDocfile failed\n");
704 r = WriteClassStg( stg, &test_stg_cls );
705 ok( r == S_OK, "WriteClassStg failed\n");
707 r = IStorage_Commit( stg, STGC_DEFAULT );
708 ok( r == S_OK, "IStorage_Commit failed\n");
710 /* now create a stream */
711 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
712 ok(r==S_OK, "IStorage->CreateStream failed\n");
714 r = IStorage_Release( stg );
715 ok (r == 0, "storage not released\n");
718 r = IStream_Seek( stm, pos, 0, &upos );
719 ok (r == STG_E_REVERTED, "seek should fail\n");
721 r = IStream_Stat( stm, &stat, STATFLAG_DEFAULT );
722 ok (r == STG_E_REVERTED, "stat should fail\n");
724 r = IStream_Write( stm, "Test string", strlen("Test string"), NULL);
725 ok (r == STG_E_REVERTED, "IStream_Write should return STG_E_REVERTED instead of 0x%08x\n", r);
727 r = IStream_Read( stm, buffer, sizeof(buffer), NULL);
728 ok (r == STG_E_REVERTED, "IStream_Read should return STG_E_REVERTED instead of 0x%08x\n", r);
730 r = IStream_Release(stm);
731 ok (r == 0, "stream not released\n");
733 /* tests that STGM_PRIORITY doesn't prevent readwrite access from other
734 * StgOpenStorage calls in transacted mode */
735 r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stgprio);
736 ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);
739 /* non-transacted mode read/write fails */
740 r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg);
741 ok(r==STG_E_LOCKVIOLATION, "StgOpenStorage should return STG_E_LOCKVIOLATION instead of 0x%08x\n", r);
744 /* non-transacted mode read-only succeeds */
745 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE|STGM_READ, NULL, 0, &stg);
746 ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);
747 IStorage_Release(stg);
749 r = StgOpenStorage( filename, NULL, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, NULL, 0, &stg);
750 ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);
753 static const WCHAR stgname[] = { ' ',' ',' ','2','9',0 };
754 static const WCHAR stgname2[] = { 'C','V','_','i','e','w',0 };
755 static const WCHAR stmname2[] = { 'V','a','r','2','D','a','t','a',0 };
760 r = IStorage_Stat( stg, &statstg, STATFLAG_NONAME );
761 ok(r == S_OK, "Stat should have succeded instead of returning 0x%08x\n", r);
762 ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %d\n", statstg.type);
763 ok(U(statstg.cbSize).LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %d\n", U(statstg.cbSize).LowPart);
764 ok(U(statstg.cbSize).HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %d\n", U(statstg.cbSize).HighPart);
765 ok(statstg.grfMode == (STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE),
766 "Statstg grfMode should have been 0x10022 instead of 0x%x\n", statstg.grfMode);
767 ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %d\n", statstg.grfLocksSupported);
768 ok(IsEqualCLSID(&statstg.clsid, &test_stg_cls), "Statstg clsid is not test_stg_cls\n");
769 ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %d\n", statstg.grfStateBits);
770 ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %d\n", statstg.reserved);
772 r = IStorage_CreateStorage( stg, stgname, STGM_SHARE_EXCLUSIVE, 0, 0, &stg2 );
773 ok(r == S_OK, "CreateStorage should have succeeded instead of returning 0x%08x\n", r);
775 r = IStorage_Stat( stg2, &statstg, STATFLAG_DEFAULT );
776 ok(r == S_OK, "Stat should have succeded instead of returning 0x%08x\n", r);
777 ok(!lstrcmpW(statstg.pwcsName, stgname),
778 "Statstg pwcsName should have been the name the storage was created with\n");
779 ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %d\n", statstg.type);
780 ok(U(statstg.cbSize).LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %d\n", U(statstg.cbSize).LowPart);
781 ok(U(statstg.cbSize).HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %d\n", U(statstg.cbSize).HighPart);
782 ok(statstg.grfMode == STGM_SHARE_EXCLUSIVE,
783 "Statstg grfMode should have been STGM_SHARE_EXCLUSIVE instead of 0x%x\n", statstg.grfMode);
784 ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %d\n", statstg.grfLocksSupported);
785 ok(IsEqualCLSID(&statstg.clsid, &CLSID_NULL), "Statstg clsid is not CLSID_NULL\n");
786 ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %d\n", statstg.grfStateBits);
787 ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %d\n", statstg.reserved);
788 CoTaskMemFree(statstg.pwcsName);
790 r = IStorage_CreateStorage( stg2, stgname2, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, 0, &stg3 );
791 ok(r == STG_E_ACCESSDENIED, "CreateStorage should have returned STG_E_ACCESSDENIED instead of 0x%08x\n", r);
793 r = IStorage_CreateStream( stg2, stmname2, STGM_CREATE|STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
794 ok(r == STG_E_ACCESSDENIED, "CreateStream should have returned STG_E_ACCESSDENIED instead of 0x%08x\n", r);
796 IStorage_Release(stg2);
798 r = IStorage_Release(stg);
799 ok(r == 0, "wrong ref count\n");
801 IStorage_Release(stgprio);
803 DeleteFileW(filename);
806 static void test_writeclassstg(void)
808 static const WCHAR szPrefix[] = { 's','t','g',0 };
809 static const WCHAR szDot[] = { '.',0 };
810 WCHAR filename[MAX_PATH];
811 IStorage *stg = NULL;
815 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
818 DeleteFileW(filename);
820 /* create the file */
821 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
822 STGM_READWRITE, 0, &stg);
823 ok(r==S_OK, "StgCreateDocfile failed\n");
825 r = ReadClassStg( NULL, NULL );
826 ok(r == E_INVALIDARG, "ReadClassStg should return E_INVALIDARG instead of 0x%08X\n", r);
828 r = ReadClassStg( stg, NULL );
829 ok(r == E_INVALIDARG, "ReadClassStg should return E_INVALIDARG instead of 0x%08X\n", r);
831 temp_cls.Data1 = 0xdeadbeef;
832 r = ReadClassStg( stg, &temp_cls );
833 ok(r == S_OK, "ReadClassStg failed with 0x%08X\n", r);
835 ok(IsEqualCLSID(&temp_cls, &CLSID_NULL), "ReadClassStg returned wrong clsid\n");
837 r = WriteClassStg( NULL, NULL );
838 ok(r == E_INVALIDARG, "WriteClassStg should return E_INVALIDARG instead of 0x%08X\n", r);
840 r = WriteClassStg( stg, NULL );
841 ok(r == STG_E_INVALIDPOINTER, "WriteClassStg should return STG_E_INVALIDPOINTER instead of 0x%08X\n", r);
843 r = WriteClassStg( stg, &test_stg_cls );
844 ok( r == S_OK, "WriteClassStg failed with 0x%08X\n", r);
846 r = ReadClassStg( stg, &temp_cls );
847 ok( r == S_OK, "ReadClassStg failed with 0x%08X\n", r);
848 ok(IsEqualCLSID(&temp_cls, &test_stg_cls), "ReadClassStg returned wrong clsid\n");
850 r = IStorage_Release( stg );
851 ok (r == 0, "storage not released\n");
853 DeleteFileW(filename);
856 static void test_streamenum(void)
858 static const WCHAR szPrefix[] = { 's','t','g',0 };
859 static const WCHAR szDot[] = { '.',0 };
860 WCHAR filename[MAX_PATH];
861 IStorage *stg = NULL;
864 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
866 IEnumSTATSTG *ee = NULL;
869 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
872 DeleteFileW(filename);
874 /* create the file */
875 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
876 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
877 ok(r==S_OK, "StgCreateDocfile failed\n");
879 r = WriteClassStg( stg, &test_stg_cls );
880 ok( r == S_OK, "WriteClassStg failed\n");
882 r = IStorage_Commit( stg, STGC_DEFAULT );
883 ok( r == S_OK, "IStorage_Commit failed\n");
885 /* now create a stream */
886 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
887 ok(r==S_OK, "IStorage->CreateStream failed\n");
889 r = IStream_Release(stm);
891 /* first enum ... should be 1 stream */
892 r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
893 ok(r==S_OK, "IStorage->EnumElements failed\n");
896 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
897 ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
898 ok(count == 1, "count wrong\n");
900 r = IEnumSTATSTG_Release(ee);
902 /* second enum... destroy the stream before reading */
903 r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
904 ok(r==S_OK, "IStorage->EnumElements failed\n");
906 r = IStorage_DestroyElement(stg, stmname);
907 ok(r==S_OK, "IStorage->EnumElements failed\n");
911 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
912 ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
913 ok(count == 0, "count wrong\n");
916 /* reset and try again */
917 r = IEnumSTATSTG_Reset(ee);
918 ok(r==S_OK, "IEnumSTATSTG->Reset failed\n");
921 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
922 ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
923 ok(count == 0, "count wrong\n");
925 r = IEnumSTATSTG_Release(ee);
926 ok (r == 0, "enum not released\n");
928 r = IStorage_Release( stg );
929 ok (r == 0, "storage not released\n");
931 DeleteFileW(filename);
934 static void test_transact(void)
936 static const WCHAR szPrefix[] = { 's','t','g',0 };
937 static const WCHAR szDot[] = { '.',0 };
938 WCHAR filename[MAX_PATH];
939 IStorage *stg = NULL, *stg2 = NULL;
942 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
943 static const WCHAR stmname2[] = { 'F','O','O',0 };
945 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
948 DeleteFileW(filename);
950 /* create the file */
951 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
952 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
953 ok(r==S_OK, "StgCreateDocfile failed\n");
955 /* now create a stream, but don't commit it */
956 r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
957 ok(r==S_OK, "IStorage->CreateStream failed\n");
959 r = IStream_Write(stm, "this is stream 1\n", 16, NULL);
960 ok(r==S_OK, "IStream->Write failed\n");
962 r = IStream_Release(stm);
964 r = IStorage_Commit(stg, 0);
965 ok(r==S_OK, "IStorage->Commit failed\n");
967 /* now create a stream, but don't commit it */
969 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
970 ok(r==S_OK, "IStorage->CreateStream failed\n");
972 r = IStream_Write(stm, "this is stream 2\n", 16, NULL);
973 ok(r==S_OK, "IStream->Write failed\n");
975 r = IStream_Commit(stm, STGC_ONLYIFCURRENT | STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE);
976 ok(r==S_OK, "IStream->Commit failed\n");
978 r = IStream_Release(stm);
980 IStorage_Release(stg);
984 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg);
985 ok(r==S_OK, "StgOpenStorage failed\n");
990 r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_DENY_NONE|STGM_READ, 0, &stm );
991 ok(r==STG_E_INVALIDFLAG, "IStorage->OpenStream failed %08x\n", r);
993 r = IStorage_OpenStream(stg, stmname, NULL, STGM_DELETEONRELEASE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
994 ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
996 r = IStorage_OpenStream(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
997 ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
999 r = IStorage_OpenStorage(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
1000 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);
1003 r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1004 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream should fail %08x\n", r);
1007 r = IStream_Release(stm);
1009 r = IStorage_OpenStorage(stg, stmname2, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
1010 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);
1012 r = IStorage_OpenStream(stg, stmname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1013 ok(r==S_OK, "IStorage->OpenStream should fail %08x\n", r);
1015 r = IStream_Release(stm);
1017 IStorage_Release(stg);
1019 r = DeleteFileW(filename);
1020 ok( r == TRUE, "deleted file\n");
1023 static void test_ReadClassStm(void)
1028 static const LARGE_INTEGER llZero;
1030 hr = ReadClassStm(NULL, &clsid);
1031 ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1033 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1034 ok_ole_success(hr, "CreateStreamOnHGlobal");
1035 hr = WriteClassStm(pStream, &test_stg_cls);
1036 ok_ole_success(hr, "WriteClassStm");
1038 hr = ReadClassStm(pStream, NULL);
1039 ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1041 /* test not rewound stream */
1042 hr = ReadClassStm(pStream, &clsid);
1043 ok(hr == STG_E_READFAULT, "ReadClassStm should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1044 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid should have been zeroed\n");
1046 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1047 ok_ole_success(hr, "IStream_Seek");
1048 hr = ReadClassStm(pStream, &clsid);
1049 ok_ole_success(hr, "ReadClassStm");
1050 ok(IsEqualCLSID(&clsid, &test_stg_cls), "clsid should have been set to CLSID_WineTest\n");
1060 static const struct access_res create[16] =
1062 { TRUE, ERROR_SUCCESS, TRUE },
1063 { TRUE, ERROR_SUCCESS, TRUE },
1064 { TRUE, ERROR_SUCCESS, FALSE },
1065 { TRUE, ERROR_SUCCESS, FALSE },
1066 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1067 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1068 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1069 { TRUE, ERROR_SUCCESS, FALSE },
1070 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1071 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1072 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1073 { TRUE, ERROR_SUCCESS, TRUE },
1074 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1075 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1076 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1077 { TRUE, ERROR_SUCCESS, TRUE }
1080 static const struct access_res create_commit[16] =
1082 { TRUE, ERROR_SUCCESS, TRUE },
1083 { TRUE, ERROR_SUCCESS, TRUE },
1084 { TRUE, ERROR_SUCCESS, FALSE },
1085 { TRUE, ERROR_SUCCESS, FALSE },
1086 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1087 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1088 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1089 { TRUE, ERROR_SUCCESS, FALSE },
1090 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1091 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1092 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1093 { TRUE, ERROR_SUCCESS, TRUE },
1094 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1095 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1096 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1097 { TRUE, ERROR_SUCCESS, TRUE }
1100 static const struct access_res create_close[16] =
1102 { TRUE, ERROR_SUCCESS, FALSE },
1103 { TRUE, ERROR_SUCCESS, FALSE },
1104 { TRUE, ERROR_SUCCESS, FALSE },
1105 { TRUE, ERROR_SUCCESS, FALSE },
1106 { TRUE, ERROR_SUCCESS, FALSE },
1107 { TRUE, ERROR_SUCCESS, FALSE },
1108 { TRUE, ERROR_SUCCESS, FALSE },
1109 { TRUE, ERROR_SUCCESS, FALSE },
1110 { TRUE, ERROR_SUCCESS, FALSE },
1111 { TRUE, ERROR_SUCCESS, FALSE },
1112 { TRUE, ERROR_SUCCESS, FALSE },
1113 { TRUE, ERROR_SUCCESS, FALSE },
1114 { TRUE, ERROR_SUCCESS, FALSE },
1115 { TRUE, ERROR_SUCCESS, FALSE },
1116 { TRUE, ERROR_SUCCESS, FALSE },
1117 { TRUE, ERROR_SUCCESS }
1120 static void _test_file_access(LPCSTR file, const struct access_res *ares, DWORD line)
1122 DWORD access = 0, share = 0;
1127 for (i = 0; i < 4; i++)
1129 if (i == 0) access = 0;
1130 if (i == 1) access = GENERIC_READ;
1131 if (i == 2) access = GENERIC_WRITE;
1132 if (i == 3) access = GENERIC_READ | GENERIC_WRITE;
1134 for (j = 0; j < 4; j++)
1136 if (ares[idx].ignore)
1139 if (j == 0) share = 0;
1140 if (j == 1) share = FILE_SHARE_READ;
1141 if (j == 2) share = FILE_SHARE_WRITE;
1142 if (j == 3) share = FILE_SHARE_READ | FILE_SHARE_WRITE;
1144 SetLastError(0xdeadbeef);
1145 hfile = CreateFileA(file, access, share, NULL, OPEN_EXISTING,
1146 FILE_ATTRIBUTE_NORMAL, 0);
1147 lasterr = GetLastError();
1149 ok((hfile != INVALID_HANDLE_VALUE) == ares[idx].gothandle,
1150 "(%d, handle, %d): Expected %d, got %d\n",
1151 line, idx, ares[idx].gothandle,
1152 (hfile != INVALID_HANDLE_VALUE));
1154 ok(lasterr == ares[idx].lasterr ||
1155 broken(lasterr == 0xdeadbeef) /* win9x */,
1156 "(%d, lasterr, %d): Expected %d, got %d\n",
1157 line, idx, ares[idx].lasterr, lasterr);
1165 #define test_file_access(file, ares) _test_file_access(file, ares, __LINE__)
1167 static void test_access(void)
1172 static const WCHAR fileW[] = {'w','i','n','e','t','e','s','t',0};
1174 /* STGM_TRANSACTED */
1176 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1177 STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, &stg);
1178 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1180 test_file_access("winetest", create);
1182 hr = IStorage_Commit(stg, STGC_DEFAULT);
1183 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1185 test_file_access("winetest", create_commit);
1187 IStorage_Release(stg);
1189 test_file_access("winetest", create_close);
1191 DeleteFileA("winetest");
1195 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1196 STGM_SHARE_EXCLUSIVE | STGM_DIRECT, 0, &stg);
1197 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1199 test_file_access("winetest", create);
1201 hr = IStorage_Commit(stg, STGC_DEFAULT);
1202 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1204 test_file_access("winetest", create_commit);
1206 IStorage_Release(stg);
1208 test_file_access("winetest", create_close);
1210 DeleteFileA("winetest");
1212 /* STGM_SHARE_DENY_NONE */
1214 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1215 STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
1216 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1218 test_file_access("winetest", create);
1220 hr = IStorage_Commit(stg, STGC_DEFAULT);
1221 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1223 test_file_access("winetest", create_commit);
1225 IStorage_Release(stg);
1227 test_file_access("winetest", create_close);
1229 DeleteFileA("winetest");
1231 /* STGM_SHARE_DENY_READ */
1233 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1234 STGM_SHARE_DENY_READ | STGM_TRANSACTED, 0, &stg);
1235 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1237 test_file_access("winetest", create);
1239 hr = IStorage_Commit(stg, STGC_DEFAULT);
1240 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1242 test_file_access("winetest", create_commit);
1244 IStorage_Release(stg);
1246 test_file_access("winetest", create_close);
1248 DeleteFileA("winetest");
1250 /* STGM_SHARE_DENY_WRITE */
1252 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1253 STGM_SHARE_DENY_WRITE | STGM_TRANSACTED, 0, &stg);
1254 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1256 test_file_access("winetest", create);
1258 hr = IStorage_Commit(stg, STGC_DEFAULT);
1259 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1261 test_file_access("winetest", create_commit);
1263 IStorage_Release(stg);
1265 test_file_access("winetest", create_close);
1267 DeleteFileA("winetest");
1270 START_TEST(storage32)
1272 test_hglobal_storage_stat();
1273 test_create_storage_modes();
1274 test_storage_stream();
1275 test_open_storage();
1276 test_storage_suminfo();
1277 test_storage_refcount();
1280 test_ReadClassStm();
1282 test_writeclassstg();