ole32: Add a test for IMoniker_IsDirty.
[wine] / dlls / ole32 / tests / storage32.c
1 /*
2  * Unit tests for OLE storage
3  *
4  * Copyright (c) 2004 Mike McCormack
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22
23 #define COBJMACROS
24
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "ole2.h"
30 #include "objidl.h"
31 #include "initguid.h"
32
33 DEFINE_GUID( test_stg_cls, 0x88888888, 0x0425, 0x0000, 0,0,0,0,0,0,0,0);
34
35 static void test_hglobal_storage_stat(void)
36 {
37     ILockBytes *ilb = NULL;
38     IStorage *stg = NULL;
39     HRESULT r;
40     STATSTG stat;
41     DWORD mode, refcount;
42
43     r = CreateILockBytesOnHGlobal( NULL, TRUE, &ilb );
44     ok( r == S_OK, "CreateILockBytesOnHGlobal failed\n");
45
46     mode = STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE;/*0x1012*/
47     r = StgCreateDocfileOnILockBytes( ilb, mode, 0,  &stg );
48     ok( r == S_OK, "CreateILockBytesOnHGlobal failed\n");
49
50     r = WriteClassStg( stg, &test_stg_cls );
51     ok( r == S_OK, "WriteClassStg failed\n");
52
53     memset( &stat, 0, sizeof stat );
54     r = IStorage_Stat( stg, &stat, 0 );
55
56     ok( stat.pwcsName == NULL, "storage name not null\n");
57     ok( stat.type == 1, "type is wrong\n");
58     ok( stat.grfMode == 0x12, "grf mode is incorrect\n");
59     ok( !memcmp(&stat.clsid, &test_stg_cls, sizeof test_stg_cls), "CLSID is wrong\n");
60
61     refcount = IStorage_Release( stg );
62     ok( refcount == 0, "IStorage refcount is wrong\n");
63     refcount = ILockBytes_Release( ilb );
64     ok( refcount == 0, "ILockBytes refcount is wrong\n");
65 }
66
67 static void test_create_storage_modes(void)
68 {
69    static const WCHAR szPrefix[] = { 's','t','g',0 };
70    static const WCHAR szDot[] = { '.',0 };
71    WCHAR filename[MAX_PATH];
72    IStorage *stg = NULL;
73    HRESULT r;
74
75    if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
76       return;
77
78    DeleteFileW(filename);
79
80    /* test with some invalid parameters */
81    r = StgCreateDocfile( NULL, 0, 0, &stg);
82    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
83    r = StgCreateDocfile( filename, 0, 0, &stg);
84    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
85    r = StgCreateDocfile( filename, STGM_CREATE, 0, &stg);
86    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
87    r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE, 0, &stg);
88    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
89    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &stg);
90    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
91    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, NULL);
92    ok(r==STG_E_INVALIDPOINTER, "StgCreateDocfile succeeded\n");
93    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, &stg);
94    ok(r==STG_E_INVALIDPARAMETER, "StgCreateDocfile succeeded\n");
95    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
96    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
97    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stg);
98    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
99    r = StgCreateDocfile( filename, STGM_PRIORITY, 0, &stg);
100    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
101
102    /* StgCreateDocfile seems to be very particular about the flags it accepts */
103    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | STGM_WRITE, 0, &stg);
104    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
105    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 8, 0, &stg);
106    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
107    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80, 0, &stg);
108    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
109    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800, 0, &stg);
110    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
111    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x8000, 0, &stg);
112    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
113    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80000, 0, &stg);
114    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
115    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800000, 0, &stg);
116    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
117    ok(stg == NULL, "stg was set\n");
118
119    /* check what happens if the file already exists (which is how it's meant to be used) */
120    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
121    ok(r==S_OK, "StgCreateDocfile failed\n");
122    r = IStorage_Release(stg);
123    ok(r == 0, "storage not released\n");
124    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
125    ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n"); /* FAILIFTHERE is default */
126    r = StgCreateDocfile( filename, STGM_READ, 0, &stg);
127    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n"); /* need at least readmode and sharemode */
128    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE, 0, &stg);
129    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
130    r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE, 0, &stg);
131    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
132    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE, 0, &stg);
133    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
134    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
135    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
136    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, &stg);
137    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
138    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_WRITE, 0, &stg);
139    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
140    r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_WRITE, 0, &stg);
141    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
142    r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
143    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
144    r = StgCreateDocfile( filename, STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
145    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
146    ok(DeleteFileW(filename), "failed to delete file\n");
147
148    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
149    ok(r==S_OK, "StgCreateDocfile failed\n");
150    r = IStorage_Release(stg);
151    ok(r == 0, "storage not released\n");
152    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED |STGM_FAILIFTHERE, 0, &stg);
153    ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
154    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_WRITE, 0, &stg);
155    ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
156
157    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
158    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
159    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
160    ok(r==S_OK, "StgCreateDocfile failed\n");
161    r = IStorage_Release(stg);
162    ok(r == 0, "storage not released\n");
163    ok(DeleteFileW(filename), "failed to delete file\n");
164
165    /* test the way excel uses StgCreateDocFile */
166    r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
167    ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
168    if(r == S_OK)
169    {
170       r = IStorage_Release(stg);
171       ok(r == 0, "storage not released\n");
172       ok(DeleteFileW(filename), "failed to delete file\n");
173    }
174
175    /* and the way windows media uses it ... */
176    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_NONE | STGM_READWRITE | STGM_TRANSACTED, 0, &stg);
177    ok(r==S_OK, "StgCreateDocfile the windows media way failed\n");
178    if (r == S_OK)
179    {
180       r = IStorage_Release(stg);
181       ok(r == 0, "storage not released\n");
182       ok(DeleteFileW(filename), "failed to delete file\n");
183    }
184
185    /* looks like we need STGM_TRANSACTED or STGM_CREATE */
186    r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
187    ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
188    if(r == S_OK)
189    {
190       r = IStorage_Release(stg);
191       ok(r == 0, "storage not released\n");
192       ok(DeleteFileW(filename), "failed to delete file\n");
193    }
194
195    r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_WRITE, 0, &stg);
196    ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
197    if(r == S_OK)
198    {
199       r = IStorage_Release(stg);
200       ok(r == 0, "storage not released\n");
201       ok(DeleteFileW(filename), "failed to delete file\n");
202    }
203
204    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
205    ok(r==S_OK, "StgCreateDocfile the powerpoint way failed\n");
206    if(r == S_OK)
207    {
208       r = IStorage_Release(stg);
209       ok(r == 0, "storage not released\n");
210       ok(DeleteFileW(filename), "failed to delete file\n");
211    }
212
213    /* test the way msi uses StgCreateDocfile */
214    r = StgCreateDocfile( filename, STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stg);
215    ok(r==S_OK, "StgCreateDocFile failed\n");
216    r = IStorage_Release(stg);
217    ok(r == 0, "storage not released\n");
218    ok(DeleteFileW(filename), "failed to delete file\n");
219 }
220
221 static void test_storage_stream(void)
222 {
223     static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
224     static const WCHAR szPrefix[] = { 's','t','g',0 };
225     static const WCHAR szDot[] = { '.',0 };
226     static const WCHAR longname[] = {
227         'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
228         'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0
229     };
230     WCHAR filename[MAX_PATH];
231     IStorage *stg = NULL;
232     HRESULT r;
233     IStream *stm = NULL;
234     ULONG count = 0;
235     LARGE_INTEGER pos;
236     ULARGE_INTEGER p;
237     unsigned char buffer[0x100];
238
239     if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
240         return;
241
242     DeleteFileW(filename);
243
244     r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
245     ok(r==S_OK, "StgCreateDocfile failed\n");
246
247     /* try create some invalid streams */
248     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, 0, &stm );
249     ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
250     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 1, &stm );
251     ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
252     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, NULL );
253     ok(r==STG_E_INVALIDPOINTER, "IStorage->CreateStream wrong error\n");
254     r = IStorage_CreateStream(stg, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
255     ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
256     r = IStorage_CreateStream(stg, longname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
257     ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error, got %ld GetLastError()=%ld\n", r, GetLastError());
258     r = IStorage_CreateStream(stg, stmname, STGM_READWRITE, 0, 0, &stm );
259     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
260     r = IStorage_CreateStream(stg, stmname, STGM_READ, 0, 0, &stm );
261     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
262     r = IStorage_CreateStream(stg, stmname, STGM_WRITE, 0, 0, &stm );
263     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
264     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, 0, &stm );
265     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
266     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READ, 0, 0, &stm );
267     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
268
269     /* now really create a stream and delete it */
270     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
271     ok(r==S_OK, "IStorage->CreateStream failed\n");
272     r = IStream_Release(stm);
273     ok(r == 0, "wrong ref count\n");
274     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
275     ok(r==STG_E_FILEALREADYEXISTS, "IStorage->CreateStream failed\n");
276     r = IStorage_DestroyElement(stg,stmname);
277     ok(r==S_OK, "IStorage->DestroyElement failed\n");
278
279     /* create a stream and write to it */
280     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
281     ok(r==S_OK, "IStorage->CreateStream failed\n");
282
283     r = IStream_Write(stm, NULL, 0, NULL );
284     ok(r==STG_E_INVALIDPOINTER, "IStream->Write wrong error\n");
285     r = IStream_Write(stm, "Hello\n", 0, NULL );
286     ok(r==S_OK, "failed to write stream\n");
287     r = IStream_Write(stm, "Hello\n", 0, &count );
288     ok(r==S_OK, "failed to write stream\n");
289     r = IStream_Write(stm, "Hello\n", 6, &count );
290     ok(r==S_OK, "failed to write stream\n");
291     r = IStream_Commit(stm, STGC_DEFAULT );
292     ok(r==S_OK, "failed to commit stream\n");
293     r = IStream_Commit(stm, STGC_DEFAULT );
294     ok(r==S_OK, "failed to commit stream\n");
295
296     /* seek round a bit, reset the stream size */
297     pos.QuadPart = 0;
298     r = IStream_Seek(stm, pos, 3, &p );
299     ok(r==STG_E_INVALIDFUNCTION, "IStream->Seek returned wrong error\n");
300     r = IStream_Seek(stm, pos, STREAM_SEEK_SET, NULL);
301     ok(r==S_OK, "failed to seek stream\n");
302     r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
303     ok(r==S_OK, "failed to seek stream\n");
304     r = IStream_SetSize(stm,p);
305     ok(r==S_OK, "failed to set pos\n");
306     pos.QuadPart = 10;
307     r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
308     ok(r==S_OK, "failed to seek stream\n");
309     ok(p.QuadPart == 10, "at wrong place\n");
310     pos.QuadPart = 0;
311     r = IStream_Seek(stm, pos, STREAM_SEEK_END, &p );
312     ok(r==S_OK, "failed to seek stream\n");
313     ok(p.QuadPart == 0, "at wrong place\n");
314     r = IStream_Read(stm, buffer, sizeof buffer, &count );
315     ok(r==S_OK, "failed to set pos\n");
316     ok(count == 0, "read bytes from empty stream\n");
317
318     /* wrap up */
319     r = IStream_Release(stm);
320     ok(r == 0, "wrong ref count\n");
321     r = IStorage_Release(stg);
322     ok(r == 0, "wrong ref count\n");
323     r = DeleteFileW(filename);
324     ok(r, "file should exist\n");
325 }
326
327 static BOOL touch_file(LPCWSTR filename)
328 {
329     HANDLE file;
330
331     file = CreateFileW(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, 
332                 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
333     if (file==INVALID_HANDLE_VALUE)
334         return FALSE;
335     CloseHandle(file);
336     return TRUE;
337 }
338
339 static BOOL is_zero_length(LPCWSTR filename)
340 {
341     HANDLE file;
342     DWORD len;
343
344     file = CreateFileW(filename, GENERIC_READ, 0, NULL, 
345                 OPEN_EXISTING, 0, NULL);
346     if (file==INVALID_HANDLE_VALUE)
347         return FALSE;
348     len = GetFileSize(file, NULL);
349     CloseHandle(file);
350     return len == 0;
351 }
352
353 static void test_open_storage(void)
354 {
355     static const WCHAR szPrefix[] = { 's','t','g',0 };
356     static const WCHAR szNonExist[] = { 'n','o','n','e','x','i','s','t',0 };
357     static const WCHAR szDot[] = { '.',0 };
358     WCHAR filename[MAX_PATH];
359     IStorage *stg = NULL, *stg2 = NULL;
360     HRESULT r;
361     DWORD stgm;
362
363     if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
364         return;
365
366     /* try opening a zero length file - it should stay zero length */
367     DeleteFileW(filename);
368     touch_file(filename);
369     stgm = STGM_NOSCRATCH | STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE;
370     r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
371     ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");
372
373     stgm = STGM_SHARE_EXCLUSIVE | STGM_READWRITE;
374     r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
375     ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");
376     ok(is_zero_length(filename), "file length changed\n");
377
378     DeleteFileW(filename);
379
380     /* create the file */
381     r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
382     ok(r==S_OK, "StgCreateDocfile failed\n");
383     IStorage_Release(stg);
384
385     r = StgOpenStorage( filename, NULL, 0, NULL, 0, &stg);
386     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage wrong error\n");
387     r = StgOpenStorage( NULL, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
388     ok(r==STG_E_INVALIDNAME, "StgOpenStorage wrong error\n");
389     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, NULL);
390     ok(r==STG_E_INVALIDPOINTER, "StgOpenStorage wrong error\n");
391     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 1, &stg);
392     ok(r==STG_E_INVALIDPARAMETER, "StgOpenStorage wrong error\n");
393     r = StgOpenStorage( szNonExist, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
394     ok(r==STG_E_FILENOTFOUND, "StgOpenStorage failed\n");
395     r = StgOpenStorage( filename, NULL, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
396     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
397     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ, NULL, 0, &stg);
398     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
399     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_READ | STGM_READ, NULL, 0, &stg);
400     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
401     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
402     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
403
404     /* open it for real */
405     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg); /* XLViewer 97/2000 */
406     ok(r==S_OK, "StgOpenStorage failed\n");
407     if(stg)
408     {
409         r = IStorage_Release(stg);
410         ok(r == 0, "wrong ref count\n");
411     }
412
413     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READ, NULL, 0, &stg);
414     ok(r==S_OK, "StgOpenStorage failed\n");
415     if(stg)
416     {
417         r = IStorage_Release(stg);
418         ok(r == 0, "wrong ref count\n");
419     }
420
421     /* test the way word opens its custom dictionary */
422     r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_TRANSACTED |
423                         STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
424     ok(r==S_OK, "StgOpenStorage failed\n");
425     if(stg)
426     {
427         r = IStorage_Release(stg);
428         ok(r == 0, "wrong ref count\n");
429     }
430
431     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
432     ok(r==S_OK, "StgOpenStorage failed\n");
433     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg2);
434     ok(r==STG_E_SHAREVIOLATION, "StgOpenStorage failed\n");
435     if(stg)
436     {
437         r = IStorage_Release(stg);
438         ok(r == 0, "wrong ref count\n");
439     }
440
441     /* now try write to a storage file we opened read-only */ 
442     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
443     ok(r==S_OK, "StgOpenStorage failed\n");
444     if(stg)
445     { 
446         static const WCHAR stmname[] =  { 'w','i','n','e','t','e','s','t',0};
447         IStream *stm = NULL;
448         IStorage *stg2 = NULL;
449
450         r = IStorage_CreateStream( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE,
451                                    0, 0, &stm );
452         ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");
453         r = IStorage_CreateStorage( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
454         ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");
455
456         r = IStorage_Release(stg);
457         ok(r == 0, "wrong ref count\n");
458     }
459
460     /* open like visio 2003 */
461     stg = NULL;
462     r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_NONE, NULL, 0, &stg);
463     ok(r == S_OK, "should succeed\n");
464     if (stg)
465         IStorage_Release(stg);
466
467     /* test other sharing modes with STGM_PRIORITY */
468     stg = NULL;
469     r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
470     ok(r == S_OK, "should succeed\n");
471     if (stg)
472         IStorage_Release(stg);
473
474     stg = NULL;
475     r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
476     ok(r == S_OK, "should succeed\n");
477     if (stg)
478         IStorage_Release(stg);
479
480     stg = NULL;
481     r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_READ, NULL, 0, &stg);
482     ok(r == S_OK, "should succeed\n");
483     if (stg)
484         IStorage_Release(stg);
485
486     /* open like Project 2003 */
487     stg = NULL;
488     r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg);
489     ok(r == S_OK, "should succeed\n");
490     r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg2);
491     ok(r == S_OK, "should succeed\n");
492     if (stg2)
493         IStorage_Release(stg2);
494     if (stg)
495         IStorage_Release(stg);
496
497     stg = NULL;
498     r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_READWRITE, NULL, 0, &stg);
499     ok(r == STG_E_INVALIDFLAG, "should fail\n");
500
501     r = StgOpenStorage( filename, NULL, STGM_TRANSACTED | STGM_PRIORITY, NULL, 0, &stg);
502     ok(r == STG_E_INVALIDFLAG, "should fail\n");
503
504     r = StgOpenStorage( filename, NULL, STGM_SIMPLE | STGM_PRIORITY, NULL, 0, &stg);
505     ok(r == STG_E_INVALIDFLAG, "should fail\n");
506
507     r = StgOpenStorage( filename, NULL, STGM_DELETEONRELEASE | STGM_PRIORITY, NULL, 0, &stg);
508     ok(r == STG_E_INVALIDFUNCTION, "should fail\n");
509
510     r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_PRIORITY, NULL, 0, &stg);
511     ok(r == STG_E_INVALIDFLAG, "should fail\n");
512
513     r = StgOpenStorage( filename, NULL, STGM_NOSNAPSHOT | STGM_PRIORITY, NULL, 0, &stg);
514     ok(r == STG_E_INVALIDFLAG, "should fail\n");
515
516     r = DeleteFileW(filename);
517     ok(r, "file didn't exist\n");
518 }
519
520 static void test_storage_suminfo(void)
521 {
522     static const WCHAR szDot[] = { '.',0 };
523     static const WCHAR szPrefix[] = { 's','t','g',0 };
524     WCHAR filename[MAX_PATH];
525     IStorage *stg = NULL;
526     IPropertySetStorage *propset = NULL;
527     IPropertyStorage *ps = NULL;
528     HRESULT r;
529
530     if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
531         return;
532
533     DeleteFileW(filename);
534
535     /* create the file */
536     r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | 
537                             STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
538     ok(r==S_OK, "StgCreateDocfile failed\n");
539
540     r = IStorage_QueryInterface( stg, &IID_IPropertySetStorage, (LPVOID) &propset );
541     ok(r == S_OK, "query interface failed\n");
542
543     /* delete it */
544     r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
545     ok(r == STG_E_FILENOTFOUND, "deleted property set storage\n");
546
547     r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
548                                 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
549     ok(r == STG_E_FILENOTFOUND, "opened property set storage\n");
550
551     r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
552                                 STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
553     ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
554
555     r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
556                                 STGM_READ, &ps );
557     ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
558
559     r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0, 0, &ps );
560     ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
561
562     r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
563                                 STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
564     ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
565
566     r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
567                                 STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
568     ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
569
570     /* now try really creating a a property set */
571     r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
572                                 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
573     ok(r == S_OK, "failed to create property set storage\n");
574
575     if( ps )
576         IPropertyStorage_Release(ps);
577
578     /* now try creating the same thing again */
579     r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
580                                 STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
581     ok(r == S_OK, "failed to create property set storage\n");
582     if( ps )
583         IPropertyStorage_Release(ps);
584
585     /* should be able to open it */
586     r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
587             STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
588     ok(r == S_OK, "open failed\n");
589     if(r == S_OK)
590         IPropertyStorage_Release(ps);
591
592     /* delete it */
593     r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
594     ok(r == S_OK, "failed to delete property set storage\n");
595
596     /* try opening with an invalid FMTID */
597     r = IPropertySetStorage_Open( propset, NULL, 
598             STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
599     ok(r == E_INVALIDARG, "open succeeded\n");
600     if(r == S_OK)
601         IPropertyStorage_Release(ps);
602
603     /* try a bad guid */
604     r = IPropertySetStorage_Open( propset, &IID_IStorage, 
605             STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
606     ok(r == STG_E_FILENOTFOUND, "open succeeded\n");
607     if(r == S_OK)
608         IPropertyStorage_Release(ps);
609     
610
611     /* try some invalid flags */
612     r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
613             STGM_CREATE | STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
614     ok(r == STG_E_INVALIDFLAG, "open succeeded\n");
615     if(r == S_OK)
616         IPropertyStorage_Release(ps);
617
618     /* after deleting it, it should be gone */
619     r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
620             STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
621     ok(r == STG_E_FILENOTFOUND, "open failed\n");
622     if(r == S_OK)
623         IPropertyStorage_Release(ps);
624
625     r = IPropertySetStorage_Release( propset );
626     ok(r == 1, "ref count wrong\n");
627
628     r = IStorage_Release(stg);
629     ok(r == 0, "ref count wrong\n");
630
631     DeleteFileW(filename);
632 }
633
634 static void test_storage_refcount(void)
635 {
636     static const WCHAR szPrefix[] = { 's','t','g',0 };
637     static const WCHAR szDot[] = { '.',0 };
638     WCHAR filename[MAX_PATH];
639     IStorage *stg = NULL;
640     IStorage *stgprio = NULL;
641     HRESULT r;
642     IStream *stm = NULL;
643     static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
644     LARGE_INTEGER pos;
645     ULARGE_INTEGER upos;
646     STATSTG stat;
647     char buffer[10];
648
649     if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
650         return;
651
652     DeleteFileW(filename);
653
654     /* create the file */
655     r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | 
656                             STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
657     ok(r==S_OK, "StgCreateDocfile failed\n");
658
659     r = WriteClassStg( stg, &test_stg_cls );
660     ok( r == S_OK, "WriteClassStg failed\n");
661
662     r = IStorage_Commit( stg, STGC_DEFAULT );
663     ok( r == S_OK, "IStorage_Commit failed\n");
664
665     /* now create a stream */
666     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
667     ok(r==S_OK, "IStorage->CreateStream failed\n");
668
669     r = IStorage_Release( stg );
670     ok (r == 0, "storage not released\n");
671
672     pos.QuadPart = 0;
673     r = IStream_Seek( stm, pos, 0, &upos );
674     ok (r == STG_E_REVERTED, "seek should fail\n");
675
676     r = IStream_Stat( stm, &stat, STATFLAG_DEFAULT );
677     ok (r == STG_E_REVERTED, "stat should fail\n");
678
679     r = IStream_Write( stm, "Test string", strlen("Test string"), NULL);
680     ok (r == STG_E_REVERTED, "IStream_Write should return STG_E_REVERTED instead of 0x%08lx\n", r);
681
682     r = IStream_Read( stm, buffer, sizeof(buffer), NULL);
683     ok (r == STG_E_REVERTED, "IStream_Read should return STG_E_REVERTED instead of 0x%08lx\n", r);
684
685     r = IStream_Release(stm);
686     ok (r == 0, "stream not released\n");
687
688     /* tests that STGM_PRIORITY doesn't prevent readwrite access from other
689      * StgOpenStorage calls in transacted mode */
690     r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stgprio);
691     ok(r==S_OK, "StgOpenStorage failed with error 0x%08lx\n", r);
692
693     todo_wine {
694     /* non-transacted mode read/write fails */
695     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg);
696     ok(r==STG_E_LOCKVIOLATION, "StgOpenStorage should return STG_E_LOCKVIOLATION instead of 0x%08lx\n", r);
697     }
698
699     /* non-transacted mode read-only succeeds */
700     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE|STGM_READ, NULL, 0, &stg);
701     ok(r==S_OK, "StgOpenStorage failed with error 0x%08lx\n", r);
702     IStorage_Release(stg);
703
704     r = StgOpenStorage( filename, NULL, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, NULL, 0, &stg);
705     ok(r==S_OK, "StgOpenStorage failed with error 0x%08lx\n", r);
706     if(stg)
707     {
708         static const WCHAR stgname[] = { ' ',' ',' ','2','9',0 };
709         static const WCHAR stgname2[] = { 'C','V','_','i','e','w',0 };
710         static const WCHAR stmname2[] = { 'V','a','r','2','D','a','t','a',0 };
711         IStorage *stg2;
712         IStorage *stg3;
713         STATSTG statstg;
714
715         r = IStorage_Stat( stg, &statstg, STATFLAG_NONAME );
716         ok(r == S_OK, "Stat should have succeded instead of returning 0x%08lx\n", r);
717         ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %ld\n", statstg.type);
718         ok(statstg.cbSize.LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %ld\n", statstg.cbSize.LowPart);
719         ok(statstg.cbSize.HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %ld\n", statstg.cbSize.HighPart);
720         ok(statstg.grfMode == (STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE),
721             "Statstg grfMode should have been 0x10022 instead of 0x%lx\n", statstg.grfMode);
722         ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %ld\n", statstg.grfLocksSupported);
723         ok(IsEqualCLSID(&statstg.clsid, &test_stg_cls), "Statstg clsid is not test_stg_cls\n");
724         ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %ld\n", statstg.grfStateBits);
725         ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %ld\n", statstg.reserved);
726
727         r = IStorage_CreateStorage( stg, stgname, STGM_SHARE_EXCLUSIVE, 0, 0, &stg2 );
728         ok(r == S_OK, "CreateStorage should have succeeded instead of returning 0x%08lx\n", r);
729
730         r = IStorage_Stat( stg2, &statstg, STATFLAG_DEFAULT );
731         ok(r == S_OK, "Stat should have succeded instead of returning 0x%08lx\n", r);
732         ok(!lstrcmpW(statstg.pwcsName, stgname),
733             "Statstg pwcsName should have been the name the storage was created with\n");
734         ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %ld\n", statstg.type);
735         ok(statstg.cbSize.LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %ld\n", statstg.cbSize.LowPart);
736         ok(statstg.cbSize.HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %ld\n", statstg.cbSize.HighPart);
737         ok(statstg.grfMode == STGM_SHARE_EXCLUSIVE,
738             "Statstg grfMode should have been STGM_SHARE_EXCLUSIVE instead of 0x%lx\n", statstg.grfMode);
739         ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %ld\n", statstg.grfLocksSupported);
740         ok(IsEqualCLSID(&statstg.clsid, &CLSID_NULL), "Statstg clsid is not CLSID_NULL\n");
741         ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %ld\n", statstg.grfStateBits);
742         ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %ld\n", statstg.reserved);
743         CoTaskMemFree(statstg.pwcsName);
744
745         r = IStorage_CreateStorage( stg2, stgname2, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, 0, &stg3 );
746         ok(r == STG_E_ACCESSDENIED, "CreateStorage should have returned STG_E_ACCESSDENIED instead of 0x%08lx\n", r);
747
748         r = IStorage_CreateStream( stg2, stmname2, STGM_CREATE|STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
749         ok(r == STG_E_ACCESSDENIED, "CreateStream should have returned STG_E_ACCESSDENIED instead of 0x%08lx\n", r);
750
751         IStorage_Release(stg2);
752
753         r = IStorage_Release(stg);
754         ok(r == 0, "wrong ref count\n");
755     }
756     IStorage_Release(stgprio);
757
758     DeleteFileW(filename);
759 }
760
761 START_TEST(storage32)
762 {
763     test_hglobal_storage_stat();
764     test_create_storage_modes();
765     test_storage_stream();
766     test_open_storage();
767     test_storage_suminfo();
768     test_storage_refcount();
769 }