push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / ole32 / tests / storage32.c
blobf65af733d286bcfff3c92d2e79e4a9d72fa2cbf9
1 /*
2 * Unit tests for OLE storage
4 * Copyright (c) 2004 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
23 #define COBJMACROS
25 #include <windows.h>
26 #include "wine/test.h"
28 #include "ole2.h"
29 #include "objidl.h"
30 #include "initguid.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;
39 IStorage *stg = NULL;
40 HRESULT r;
41 STATSTG stat;
42 DWORD mode, refcount;
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];
73 IStorage *stg = NULL;
74 HRESULT r;
76 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
77 return;
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");
175 if(r == S_OK)
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");
185 if (r == S_OK)
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");
195 if(r == S_OK)
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");
204 if(r == S_OK)
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");
213 if(r == S_OK)
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;
239 HRESULT r;
240 IStream *stm = NULL;
241 IStream *stm2 = NULL;
242 ULONG count = 0;
243 LARGE_INTEGER pos;
244 ULARGE_INTEGER p;
245 unsigned char buffer[0x100];
247 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
248 return;
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 */
309 pos.QuadPart = 0;
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");
318 pos.QuadPart = 10;
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");
322 pos.QuadPart = 0;
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");
330 /* wrap up */
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)
354 HANDLE file;
356 file = CreateFileW(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
357 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
358 if (file==INVALID_HANDLE_VALUE)
359 return FALSE;
360 CloseHandle(file);
361 return TRUE;
364 static BOOL is_zero_length(LPCWSTR filename)
366 HANDLE file;
367 DWORD len;
369 file = CreateFileW(filename, GENERIC_READ, 0, NULL,
370 OPEN_EXISTING, 0, NULL);
371 if (file==INVALID_HANDLE_VALUE)
372 return FALSE;
373 len = GetFileSize(file, NULL);
374 CloseHandle(file);
375 return len == 0;
378 static BOOL is_existing_file(LPCWSTR filename)
380 HANDLE file;
382 file = CreateFileW(filename, GENERIC_READ, 0, NULL,
383 OPEN_EXISTING, 0, NULL);
384 if (file==INVALID_HANDLE_VALUE)
385 return FALSE;
386 CloseHandle(file);
387 return TRUE;
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;
397 HRESULT r;
398 DWORD stgm;
400 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
401 return;
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");
452 if(stg)
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");
460 if(stg)
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");
470 if(stg)
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");
480 if(stg)
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");
489 if(stg)
491 static const WCHAR stmname[] = { 'w','i','n','e','t','e','s','t',0};
492 IStream *stm = NULL;
493 IStorage *stg2 = NULL;
495 r = IStorage_CreateStream( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE,
496 0, 0, &stm );
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 */
506 stg = NULL;
507 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_NONE, NULL, 0, &stg);
508 ok(r == S_OK, "should succeed\n");
509 if (stg)
510 IStorage_Release(stg);
512 /* test other sharing modes with STGM_PRIORITY */
513 stg = NULL;
514 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
515 ok(r == S_OK, "should succeed\n");
516 if (stg)
517 IStorage_Release(stg);
519 stg = NULL;
520 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
521 ok(r == S_OK, "should succeed\n");
522 if (stg)
523 IStorage_Release(stg);
525 stg = NULL;
526 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_READ, NULL, 0, &stg);
527 ok(r == S_OK, "should succeed\n");
528 if (stg)
529 IStorage_Release(stg);
531 /* open like Project 2003 */
532 stg = NULL;
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");
537 if (stg2)
538 IStorage_Release(stg2);
539 if (stg)
540 IStorage_Release(stg);
542 stg = NULL;
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;
573 HRESULT r;
575 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
576 return;
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");
588 /* delete it */
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,
601 STGM_READ, &ps );
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");
620 if( ps )
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");
627 if( ps )
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");
634 if(r == S_OK)
635 IPropertyStorage_Release(ps);
637 /* delete it */
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");
645 if(r == S_OK)
646 IPropertyStorage_Release(ps);
648 /* try a bad guid */
649 r = IPropertySetStorage_Open( propset, &IID_IStorage,
650 STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
651 ok(r == STG_E_FILENOTFOUND, "open succeeded\n");
652 if(r == S_OK)
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");
660 if(r == S_OK)
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");
667 if(r == S_OK)
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;
686 HRESULT r;
687 IStream *stm = NULL;
688 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
689 LARGE_INTEGER pos;
690 ULARGE_INTEGER upos;
691 STATSTG stat;
692 char buffer[10];
694 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
695 return;
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");
717 pos.QuadPart = 0;
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);
738 todo_wine {
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);
751 if(stg)
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 };
756 IStorage *stg2;
757 IStorage *stg3;
758 STATSTG statstg;
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_streamenum(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;
812 HRESULT r;
813 IStream *stm = NULL;
814 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
815 STATSTG stat;
816 IEnumSTATSTG *ee = NULL;
817 ULONG count;
819 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
820 return;
822 DeleteFileW(filename);
824 /* create the file */
825 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
826 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
827 ok(r==S_OK, "StgCreateDocfile failed\n");
829 r = WriteClassStg( stg, &test_stg_cls );
830 ok( r == S_OK, "WriteClassStg failed\n");
832 r = IStorage_Commit( stg, STGC_DEFAULT );
833 ok( r == S_OK, "IStorage_Commit failed\n");
835 /* now create a stream */
836 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
837 ok(r==S_OK, "IStorage->CreateStream failed\n");
839 r = IStream_Release(stm);
841 /* first enum ... should be 1 stream */
842 r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
843 ok(r==S_OK, "IStorage->EnumElements failed\n");
845 count = 0xf00;
846 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
847 ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
848 ok(count == 1, "count wrong\n");
850 r = IEnumSTATSTG_Release(ee);
852 /* second enum... destroy the stream before reading */
853 r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
854 ok(r==S_OK, "IStorage->EnumElements failed\n");
856 r = IStorage_DestroyElement(stg, stmname);
857 ok(r==S_OK, "IStorage->EnumElements failed\n");
859 todo_wine {
860 count = 0xf00;
861 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
862 ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
863 ok(count == 0, "count wrong\n");
866 /* reset and try again */
867 r = IEnumSTATSTG_Reset(ee);
868 ok(r==S_OK, "IEnumSTATSTG->Reset failed\n");
870 count = 0xf00;
871 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
872 ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
873 ok(count == 0, "count wrong\n");
875 r = IEnumSTATSTG_Release(ee);
876 ok (r == 0, "enum not released\n");
878 r = IStorage_Release( stg );
879 ok (r == 0, "storage not released\n");
881 DeleteFileW(filename);
884 static void test_transact(void)
886 static const WCHAR szPrefix[] = { 's','t','g',0 };
887 static const WCHAR szDot[] = { '.',0 };
888 WCHAR filename[MAX_PATH];
889 IStorage *stg = NULL, *stg2 = NULL;
890 HRESULT r;
891 IStream *stm = NULL;
892 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
893 static const WCHAR stmname2[] = { 'F','O','O',0 };
895 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
896 return;
898 DeleteFileW(filename);
900 /* create the file */
901 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
902 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
903 ok(r==S_OK, "StgCreateDocfile failed\n");
905 /* now create a stream, but don't commit it */
906 r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
907 ok(r==S_OK, "IStorage->CreateStream failed\n");
909 r = IStream_Write(stm, "this is stream 1\n", 16, NULL);
910 ok(r==S_OK, "IStream->Write failed\n");
912 r = IStream_Release(stm);
914 r = IStorage_Commit(stg, 0);
915 ok(r==S_OK, "IStorage->Commit failed\n");
917 /* now create a stream, but don't commit it */
918 stm = NULL;
919 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
920 ok(r==S_OK, "IStorage->CreateStream failed\n");
922 r = IStream_Write(stm, "this is stream 2\n", 16, NULL);
923 ok(r==S_OK, "IStream->Write failed\n");
925 r = IStream_Commit(stm, STGC_ONLYIFCURRENT | STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE);
926 ok(r==S_OK, "IStream->Commit failed\n");
928 r = IStream_Release(stm);
930 IStorage_Release(stg);
932 stm = NULL;
933 stg = NULL;
934 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg);
935 ok(r==S_OK, "StgOpenStorage failed\n");
937 if (!stg)
938 return;
940 r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_DENY_NONE|STGM_READ, 0, &stm );
941 ok(r==STG_E_INVALIDFLAG, "IStorage->OpenStream failed %08x\n", r);
943 r = IStorage_OpenStream(stg, stmname, NULL, STGM_DELETEONRELEASE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
944 ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
946 r = IStorage_OpenStream(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
947 ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
949 r = IStorage_OpenStorage(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
950 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);
952 todo_wine {
953 r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
954 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream should fail %08x\n", r);
956 if (stm)
957 r = IStream_Release(stm);
959 r = IStorage_OpenStorage(stg, stmname2, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
960 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);
962 r = IStorage_OpenStream(stg, stmname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
963 ok(r==S_OK, "IStorage->OpenStream should fail %08x\n", r);
964 if (stm)
965 r = IStream_Release(stm);
967 IStorage_Release(stg);
969 r = DeleteFileW(filename);
970 ok( r == TRUE, "deleted file\n");
973 static void test_ReadClassStm(void)
975 CLSID clsid;
976 HRESULT hr;
977 IStream *pStream;
978 static const LARGE_INTEGER llZero;
980 hr = ReadClassStm(NULL, &clsid);
981 ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);
983 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
984 ok_ole_success(hr, "CreateStreamOnHGlobal");
985 hr = WriteClassStm(pStream, &test_stg_cls);
986 ok_ole_success(hr, "WriteClassStm");
988 hr = ReadClassStm(pStream, NULL);
989 ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);
991 /* test not rewound stream */
992 hr = ReadClassStm(pStream, &clsid);
993 ok(hr == STG_E_READFAULT, "ReadClassStm should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
994 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid should have been zeroed\n");
996 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
997 ok_ole_success(hr, "IStream_Seek");
998 hr = ReadClassStm(pStream, &clsid);
999 ok_ole_success(hr, "ReadClassStm");
1000 ok(IsEqualCLSID(&clsid, &test_stg_cls), "clsid should have been set to CLSID_WineTest\n");
1003 struct access_res
1005 BOOL gothandle;
1006 DWORD lasterr;
1007 BOOL ignore;
1010 static const struct access_res create[16] =
1012 { TRUE, ERROR_SUCCESS, TRUE },
1013 { TRUE, ERROR_SUCCESS, TRUE },
1014 { TRUE, ERROR_SUCCESS, FALSE },
1015 { TRUE, ERROR_SUCCESS, FALSE },
1016 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1017 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1018 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1019 { TRUE, ERROR_SUCCESS, FALSE },
1020 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1021 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1022 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1023 { TRUE, ERROR_SUCCESS, TRUE },
1024 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1025 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1026 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1027 { TRUE, ERROR_SUCCESS, TRUE }
1030 static const struct access_res create_commit[16] =
1032 { TRUE, ERROR_SUCCESS, TRUE },
1033 { TRUE, ERROR_SUCCESS, TRUE },
1034 { TRUE, ERROR_SUCCESS, FALSE },
1035 { TRUE, ERROR_SUCCESS, FALSE },
1036 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1037 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1038 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1039 { TRUE, ERROR_SUCCESS, FALSE },
1040 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1041 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1042 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1043 { TRUE, ERROR_SUCCESS, TRUE },
1044 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1045 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1046 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1047 { TRUE, ERROR_SUCCESS, TRUE }
1050 static const struct access_res create_close[16] =
1052 { TRUE, ERROR_SUCCESS, FALSE },
1053 { TRUE, ERROR_SUCCESS, FALSE },
1054 { TRUE, ERROR_SUCCESS, FALSE },
1055 { TRUE, ERROR_SUCCESS, FALSE },
1056 { TRUE, ERROR_SUCCESS, FALSE },
1057 { TRUE, ERROR_SUCCESS, FALSE },
1058 { TRUE, ERROR_SUCCESS, FALSE },
1059 { TRUE, ERROR_SUCCESS, FALSE },
1060 { TRUE, ERROR_SUCCESS, FALSE },
1061 { TRUE, ERROR_SUCCESS, FALSE },
1062 { TRUE, ERROR_SUCCESS, FALSE },
1063 { TRUE, ERROR_SUCCESS, FALSE },
1064 { TRUE, ERROR_SUCCESS, FALSE },
1065 { TRUE, ERROR_SUCCESS, FALSE },
1066 { TRUE, ERROR_SUCCESS, FALSE },
1067 { TRUE, ERROR_SUCCESS }
1070 static void _test_file_access(LPCSTR file, const struct access_res *ares, DWORD line)
1072 DWORD access = 0, share = 0;
1073 DWORD lasterr;
1074 HANDLE hfile;
1075 int i, j, idx = 0;
1077 for (i = 0; i < 4; i++)
1079 if (i == 0) access = 0;
1080 if (i == 1) access = GENERIC_READ;
1081 if (i == 2) access = GENERIC_WRITE;
1082 if (i == 3) access = GENERIC_READ | GENERIC_WRITE;
1084 for (j = 0; j < 4; j++)
1086 if (ares[idx].ignore)
1087 continue;
1089 if (j == 0) share = 0;
1090 if (j == 1) share = FILE_SHARE_READ;
1091 if (j == 2) share = FILE_SHARE_WRITE;
1092 if (j == 3) share = FILE_SHARE_READ | FILE_SHARE_WRITE;
1094 SetLastError(0xdeadbeef);
1095 hfile = CreateFileA(file, access, share, NULL, OPEN_EXISTING,
1096 FILE_ATTRIBUTE_NORMAL, 0);
1097 lasterr = GetLastError();
1099 ok((hfile != INVALID_HANDLE_VALUE) == ares[idx].gothandle,
1100 "(%d, handle, %d): Expected %d, got %d\n",
1101 line, idx, ares[idx].gothandle,
1102 (hfile != INVALID_HANDLE_VALUE));
1104 ok(lasterr == ares[idx].lasterr ||
1105 broken(lasterr == 0xdeadbeef) /* win9x */,
1106 "(%d, lasterr, %d): Expected %d, got %d\n",
1107 line, idx, ares[idx].lasterr, lasterr);
1109 CloseHandle(hfile);
1110 idx++;
1115 #define test_file_access(file, ares) _test_file_access(file, ares, __LINE__)
1117 static void test_access(void)
1119 IStorage *stg;
1120 HRESULT hr;
1122 static const WCHAR fileW[] = {'w','i','n','e','t','e','s','t',0};
1124 /* STGM_TRANSACTED */
1126 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1127 STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, &stg);
1128 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1130 test_file_access("winetest", create);
1132 hr = IStorage_Commit(stg, STGC_DEFAULT);
1133 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1135 test_file_access("winetest", create_commit);
1137 IStorage_Release(stg);
1139 test_file_access("winetest", create_close);
1141 DeleteFileA("winetest");
1143 /* STGM_DIRECT */
1145 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1146 STGM_SHARE_EXCLUSIVE | STGM_DIRECT, 0, &stg);
1147 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1149 test_file_access("winetest", create);
1151 hr = IStorage_Commit(stg, STGC_DEFAULT);
1152 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1154 test_file_access("winetest", create_commit);
1156 IStorage_Release(stg);
1158 test_file_access("winetest", create_close);
1160 DeleteFileA("winetest");
1162 /* STGM_SHARE_DENY_NONE */
1164 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1165 STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
1166 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1168 test_file_access("winetest", create);
1170 hr = IStorage_Commit(stg, STGC_DEFAULT);
1171 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1173 test_file_access("winetest", create_commit);
1175 IStorage_Release(stg);
1177 test_file_access("winetest", create_close);
1179 DeleteFileA("winetest");
1181 /* STGM_SHARE_DENY_READ */
1183 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1184 STGM_SHARE_DENY_READ | STGM_TRANSACTED, 0, &stg);
1185 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1187 test_file_access("winetest", create);
1189 hr = IStorage_Commit(stg, STGC_DEFAULT);
1190 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1192 test_file_access("winetest", create_commit);
1194 IStorage_Release(stg);
1196 test_file_access("winetest", create_close);
1198 DeleteFileA("winetest");
1200 /* STGM_SHARE_DENY_WRITE */
1202 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1203 STGM_SHARE_DENY_WRITE | STGM_TRANSACTED, 0, &stg);
1204 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1206 test_file_access("winetest", create);
1208 hr = IStorage_Commit(stg, STGC_DEFAULT);
1209 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1211 test_file_access("winetest", create_commit);
1213 IStorage_Release(stg);
1215 test_file_access("winetest", create_close);
1217 DeleteFileA("winetest");
1220 START_TEST(storage32)
1222 test_hglobal_storage_stat();
1223 test_create_storage_modes();
1224 test_storage_stream();
1225 test_open_storage();
1226 test_storage_suminfo();
1227 test_storage_refcount();
1228 test_streamenum();
1229 test_transact();
1230 test_ReadClassStm();
1231 test_access();