Reverse the order for deleting the items in resetcontent to correctly
[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 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     todo_wine {
59     ok( stat.grfMode == 0x12, "grf mode is incorrect\n");
60     }
61     ok( !memcmp(&stat.clsid, &test_stg_cls, sizeof test_stg_cls), "CLSID is wrong\n");
62
63     refcount = IStorage_Release( stg );
64     ok( refcount == 0, "IStorage refcount is wrong\n");
65     refcount = ILockBytes_Release( ilb );
66     ok( refcount == 0, "ILockBytes refcount is wrong\n");
67 }
68
69 void test_create_storage_modes(void)
70 {
71    static const WCHAR szPrefix[] = { 's','t','g',0 };
72    static const WCHAR szDot[] = { '.',0 };
73    WCHAR filename[MAX_PATH];
74    IStorage *stg = NULL;
75    HRESULT r;
76
77    if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
78       return;
79
80    DeleteFileW(filename);
81
82    /* test with some invalid parameters */
83    r = StgCreateDocfile( NULL, 0, 0, &stg);
84    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
85    r = StgCreateDocfile( filename, 0, 0, &stg);
86    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
87    r = StgCreateDocfile( filename, STGM_CREATE, 0, &stg);
88    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
89    r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE, 0, &stg);
90    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
91    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &stg);
92    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
93    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
94                                    STGM_READWRITE, 0, NULL);
95    ok(r==STG_E_INVALIDPOINTER, "StgCreateDocfile succeeded\n");
96    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
97                                    STGM_READWRITE, 1, &stg);
98    ok(r==STG_E_INVALIDPARAMETER, "StgCreateDocfile succeeded\n");
99    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stg);
100    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
101    r = StgCreateDocfile( filename, STGM_PRIORITY, 0, &stg);
102    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
103    ok(stg == NULL, "stg was set\n");
104
105    /* check what happens if the file already exists */
106    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
107    ok(r==S_OK, "StgCreateDocfile failed\n");
108    r = IStorage_Release(stg);
109    ok(r == 0, "storage not released\n");
110    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |
111                                    STGM_TRANSACTED, 0, &stg);
112    ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
113    r = StgCreateDocfile( filename, STGM_READ, 0, &stg);
114    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
115    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE, 0, &stg);
116    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
117    r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE, 0, &stg);
118    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
119    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE, 0, &stg);
120    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
121    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
122    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
123    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, &stg);
124    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
125    r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_WRITE, 0, &stg);
126    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
127    r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_WRITE, 0, &stg);
128    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
129    r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
130    ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
131    ok(DeleteFileW(filename), "failed to delete file\n");
132
133    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |
134                                    STGM_TRANSACTED, 0, &stg);
135    ok(r==S_OK, "StgCreateDocfile failed\n");
136    r = IStorage_Release(stg);
137    ok(r == 0, "storage not released\n");
138    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |
139                                    STGM_TRANSACTED |STGM_FAILIFTHERE, 0, &stg);
140    ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
141    r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_WRITE, 0, &stg);
142    ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
143
144    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
145                                    STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
146    ok(r==S_OK, "StgCreateDocfile failed\n");
147    r = IStorage_Release(stg);
148    ok(r == 0, "storage not released\n");
149
150    ok(DeleteFileW(filename), "failed to delete file\n");
151 }
152
153 void test_storage_stream(void)
154 {
155     static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
156     static const WCHAR szPrefix[] = { 's','t','g',0 };
157     static const WCHAR szDot[] = { '.',0 };
158     static const WCHAR longname[] = {
159         'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
160         'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0
161     };
162     WCHAR filename[MAX_PATH];
163     IStorage *stg = NULL;
164     HRESULT r;
165     IStream *stm = NULL;
166     ULONG count = 0;
167     LARGE_INTEGER pos;
168     ULARGE_INTEGER p;
169     unsigned char buffer[0x100];
170
171     if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
172         return;
173
174     DeleteFileW(filename);
175
176     r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
177     ok(r==S_OK, "StgCreateDocfile failed\n");
178
179     /* try create some invalid streams */
180     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, 0, &stm );
181     ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
182     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 1, &stm );
183     ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
184     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, NULL );
185     ok(r==STG_E_INVALIDPOINTER, "IStorage->CreateStream wrong error\n");
186     r = IStorage_CreateStream(stg, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
187     ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
188     r = IStorage_CreateStream(stg, longname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
189     ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
190     r = IStorage_CreateStream(stg, stmname, STGM_READWRITE, 0, 0, &stm );
191     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
192     r = IStorage_CreateStream(stg, stmname, STGM_READ, 0, 0, &stm );
193     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
194     r = IStorage_CreateStream(stg, stmname, STGM_WRITE, 0, 0, &stm );
195     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
196     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, 0, &stm );
197     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
198     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READ, 0, 0, &stm );
199     ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
200
201     /* now really create a stream and delete it */
202     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
203     ok(r==S_OK, "IStorage->CreateStream failed\n");
204     r = IStream_Release(stm);
205     ok(r == 0, "wrong ref count\n");
206     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
207     ok(r==STG_E_FILEALREADYEXISTS, "IStorage->CreateStream failed\n");
208     r = IStorage_DestroyElement(stg,stmname);
209     ok(r==S_OK, "IStorage->DestroyElement failed\n");
210
211     /* create a stream and write to it */
212     r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
213     ok(r==S_OK, "IStorage->CreateStream failed\n");
214
215     r = IStream_Write(stm, NULL, 0, NULL );
216     ok(r==STG_E_INVALIDPOINTER, "IStream->Write wrong error\n");
217     r = IStream_Write(stm, "Hello\n", 0, NULL );
218     ok(r==S_OK, "failed to write stream\n");
219     r = IStream_Write(stm, "Hello\n", 0, &count );
220     ok(r==S_OK, "failed to write stream\n");
221     r = IStream_Write(stm, "Hello\n", 6, &count );
222     ok(r==S_OK, "failed to write stream\n");
223     r = IStream_Commit(stm, STGC_DEFAULT );
224     ok(r==S_OK, "failed to commit stream\n");
225     r = IStream_Commit(stm, STGC_DEFAULT );
226     ok(r==S_OK, "failed to commit stream\n");
227
228     /* seek round a bit, reset the stream size */
229     pos.QuadPart = 0;
230     r = IStream_Seek(stm, pos, 3, &p );
231     ok(r==STG_E_INVALIDFUNCTION, "IStream->Seek returned wrong error\n");
232     r = IStream_Seek(stm, pos, STREAM_SEEK_SET, NULL);
233     ok(r==S_OK, "failed to seek stream\n");
234     r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
235     ok(r==S_OK, "failed to seek stream\n");
236     r = IStream_SetSize(stm,p);
237     ok(r==S_OK, "failed to set pos\n");
238     pos.QuadPart = 10;
239     r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
240     ok(r==S_OK, "failed to seek stream\n");
241     ok(p.QuadPart == 10, "at wrong place\n");
242     pos.QuadPart = 0;
243     r = IStream_Seek(stm, pos, STREAM_SEEK_END, &p );
244     ok(r==S_OK, "failed to seek stream\n");
245     ok(p.QuadPart == 0, "at wrong place\n");
246     r = IStream_Read(stm, buffer, sizeof buffer, &count );
247     ok(r==S_OK, "failed to set pos\n");
248     ok(count == 0, "read bytes from empty stream\n");
249
250     /* wrap up */
251     r = IStream_Release(stm);
252     ok(r == 0, "wrong ref count\n");
253     r = IStorage_Release(stg);
254     ok(r == 0, "wrong ref count\n");
255     r = DeleteFileW(filename);
256     ok(r == TRUE, "file should exist\n");
257 }
258
259 void test_open_storage(void)
260 {
261     static const WCHAR szPrefix[] = { 's','t','g',0 };
262     static const WCHAR szNonExist[] = { 'n','o','n','e','x','i','s','t',0 };
263     static const WCHAR szDot[] = { '.',0 };
264     WCHAR filename[MAX_PATH];
265     IStorage *stg = NULL, *stg2 = NULL;
266     HRESULT r;
267
268     if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
269         return;
270
271     DeleteFileW(filename);
272
273     /* create the file */
274     r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
275     ok(r==S_OK, "StgCreateDocfile failed\n");
276     IStorage_Release(stg);
277
278     r = StgOpenStorage( filename, NULL, 0, NULL, 0, &stg);
279     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage wrong error\n");
280     r = StgOpenStorage( NULL, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
281     ok(r==STG_E_INVALIDNAME, "StgOpenStorage wrong error\n");
282     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, NULL);
283     ok(r==STG_E_INVALIDPOINTER, "StgOpenStorage wrong error\n");
284     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 1, &stg);
285     ok(r==STG_E_INVALIDPARAMETER, "StgOpenStorage wrong error\n");
286     r = StgOpenStorage( szNonExist, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
287     ok(r==STG_E_FILENOTFOUND, "StgOpenStorage failed\n");
288     r = StgOpenStorage( filename, NULL, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
289     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
290     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ, NULL, 0, &stg);
291     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
292     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_READ | STGM_READ, NULL, 0, &stg);
293     ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
294
295     /* open it for real */
296     r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READ, NULL, 0, &stg);
297     ok(r==S_OK, "StgOpenStorage failed\n");
298     r = IStorage_Release(stg);
299     ok(r == 0, "wrong ref count\n");
300
301     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
302     ok(r==S_OK, "StgOpenStorage failed\n");
303     r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg2);
304     ok(r==STG_E_SHAREVIOLATION, "StgOpenStorage failed\n");
305     r = IStorage_Release(stg);
306     ok(r == 0, "wrong ref count\n");
307
308     r = DeleteFileW(filename);
309     ok(r, "file didn't exist\n");
310 }
311
312 START_TEST(storage32)
313 {
314     test_hglobal_storage_stat();
315     test_create_storage_modes();
316     test_storage_stream();
317     test_open_storage();
318 }