da2d0aa5a6e73329b621f60d14f441461654abf1
[reactos.git] / rostests / winetests / ole32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdio.h>
22
23 #define COBJMACROS
24
25 #include <windows.h>
26 #include "wine/test.h"
27
28 #include "ole2.h"
29 #include "objidl.h"
30 #include "initguid.h"
31
32 DEFINE_GUID( test_stg_cls, 0x88888888, 0x0425, 0x0000, 0,0,0,0,0,0,0,0);
33
34 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
35
36 static void test_hglobal_storage_stat(void)
37 {
38 ILockBytes *ilb = NULL;
39 IStorage *stg = NULL;
40 HRESULT r;
41 STATSTG stat;
42 DWORD mode, refcount;
43
44 r = CreateILockBytesOnHGlobal( NULL, TRUE, &ilb );
45 ok( r == S_OK, "CreateILockBytesOnHGlobal failed\n");
46
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");
50
51 r = WriteClassStg( stg, &test_stg_cls );
52 ok( r == S_OK, "WriteClassStg failed\n");
53
54 memset( &stat, 0, sizeof stat );
55 r = IStorage_Stat( stg, &stat, 0 );
56
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");
61
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");
66 }
67
68 static void test_create_storage_modes(void)
69 {
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;
75
76 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
77 return;
78
79 DeleteFileW(filename);
80
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");
102
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");
119
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");
148
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");
157
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");
165
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");
171
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)
176 {
177 r = IStorage_Release(stg);
178 ok(r == 0, "storage not released\n");
179 ok(DeleteFileW(filename), "failed to delete file\n");
180 }
181
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)
186 {
187 r = IStorage_Release(stg);
188 ok(r == 0, "storage not released\n");
189 ok(DeleteFileW(filename), "failed to delete file\n");
190 }
191
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)
196 {
197 r = IStorage_Release(stg);
198 ok(r == 0, "storage not released\n");
199 ok(DeleteFileW(filename), "failed to delete file\n");
200 }
201
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)
205 {
206 r = IStorage_Release(stg);
207 ok(r == 0, "storage not released\n");
208 ok(DeleteFileW(filename), "failed to delete file\n");
209 }
210
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)
214 {
215 r = IStorage_Release(stg);
216 ok(r == 0, "storage not released\n");
217 ok(DeleteFileW(filename), "failed to delete file\n");
218 }
219
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");
226 }
227
228 static void test_storage_stream(void)
229 {
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
236 };
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];
246
247 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
248 return;
249
250 DeleteFileW(filename);
251
252 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
253 ok(r==S_OK, "StgCreateDocfile failed\n");
254
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");
277
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");
287
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");
291
292 r = IStream_Clone(stm, &stm2);
293 ok(r==S_OK, "failed to clone stream\n");
294
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");
307
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");
329
330 /* wrap up */
331 r = IStream_Release(stm2);
332 ok(r == 0, "wrong ref count\n");
333
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");
337
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);
340
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");
345
346 r = IStorage_Release(stg);
347 ok(r == 0, "wrong ref count\n");
348 r = DeleteFileW(filename);
349 ok(r, "file should exist\n");
350 }
351
352 static BOOL touch_file(LPCWSTR filename)
353 {
354 HANDLE file;
355
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;
362 }
363
364 static BOOL is_zero_length(LPCWSTR filename)
365 {
366 HANDLE file;
367 DWORD len;
368
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;
376 }
377
378 static BOOL is_existing_file(LPCWSTR filename)
379 {
380 HANDLE file;
381
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;
388 }
389
390 static void test_open_storage(void)
391 {
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;
399
400 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
401 return;
402
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");
409
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");
414
415 DeleteFileW(filename);
416
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);
424
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);
429
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");
448
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)
453 {
454 r = IStorage_Release(stg);
455 ok(r == 0, "wrong ref count\n");
456 }
457
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)
461 {
462 r = IStorage_Release(stg);
463 ok(r == 0, "wrong ref count\n");
464 }
465
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)
471 {
472 r = IStorage_Release(stg);
473 ok(r == 0, "wrong ref count\n");
474 }
475
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)
481 {
482 r = IStorage_Release(stg);
483 ok(r == 0, "wrong ref count\n");
484 }
485
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)
490 {
491 static const WCHAR stmname[] = { 'w','i','n','e','t','e','s','t',0};
492 IStream *stm = NULL;
493 IStorage *stg2 = NULL;
494
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");
500
501 r = IStorage_Release(stg);
502 ok(r == 0, "wrong ref count\n");
503 }
504
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);
511
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);
518
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);
524
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);
530
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);
541
542 stg = NULL;
543 r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_READWRITE, NULL, 0, &stg);
544 ok(r == STG_E_INVALIDFLAG, "should fail\n");
545
546 r = StgOpenStorage( filename, NULL, STGM_TRANSACTED | STGM_PRIORITY, NULL, 0, &stg);
547 ok(r == STG_E_INVALIDFLAG, "should fail\n");
548
549 r = StgOpenStorage( filename, NULL, STGM_SIMPLE | STGM_PRIORITY, NULL, 0, &stg);
550 ok(r == STG_E_INVALIDFLAG, "should fail\n");
551
552 r = StgOpenStorage( filename, NULL, STGM_DELETEONRELEASE | STGM_PRIORITY, NULL, 0, &stg);
553 ok(r == STG_E_INVALIDFUNCTION, "should fail\n");
554
555 r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_PRIORITY, NULL, 0, &stg);
556 ok(r == STG_E_INVALIDFLAG, "should fail\n");
557
558 r = StgOpenStorage( filename, NULL, STGM_NOSNAPSHOT | STGM_PRIORITY, NULL, 0, &stg);
559 ok(r == STG_E_INVALIDFLAG, "should fail\n");
560
561 r = DeleteFileW(filename);
562 ok(r, "file didn't exist\n");
563 }
564
565 static void test_storage_suminfo(void)
566 {
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;
574
575 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
576 return;
577
578 DeleteFileW(filename);
579
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");
584
585 r = IStorage_QueryInterface( stg, &IID_IPropertySetStorage, (LPVOID) &propset );
586 ok(r == S_OK, "query interface failed\n");
587
588 /* delete it */
589 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
590 ok(r == STG_E_FILENOTFOUND, "deleted property set storage\n");
591
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");
595
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");
599
600 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
601 STGM_READ, &ps );
602 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
603
604 r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0, 0, &ps );
605 ok(r == STG_E_INVALIDFLAG, "created property set storage\n");
606
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");
610
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");
614
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");
619
620 if( ps )
621 IPropertyStorage_Release(ps);
622
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);
629
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);
636
637 /* delete it */
638 r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
639 ok(r == S_OK, "failed to delete property set storage\n");
640
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);
647
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);
654
655
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);
662
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);
669
670 r = IPropertySetStorage_Release( propset );
671 ok(r == 1, "ref count wrong\n");
672
673 r = IStorage_Release(stg);
674 ok(r == 0, "ref count wrong\n");
675
676 DeleteFileW(filename);
677 }
678
679 static void test_storage_refcount(void)
680 {
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];
693
694 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
695 return;
696
697 DeleteFileW(filename);
698
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");
703
704 r = WriteClassStg( stg, &test_stg_cls );
705 ok( r == S_OK, "WriteClassStg failed\n");
706
707 r = IStorage_Commit( stg, STGC_DEFAULT );
708 ok( r == S_OK, "IStorage_Commit failed\n");
709
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");
713
714 r = IStorage_Release( stg );
715 ok (r == 0, "storage not released\n");
716
717 pos.QuadPart = 0;
718 r = IStream_Seek( stm, pos, 0, &upos );
719 ok (r == STG_E_REVERTED, "seek should fail\n");
720
721 r = IStream_Stat( stm, &stat, STATFLAG_DEFAULT );
722 ok (r == STG_E_REVERTED, "stat should fail\n");
723
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);
726
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);
729
730 r = IStream_Release(stm);
731 ok (r == 0, "stream not released\n");
732
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);
737
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);
742 }
743
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);
748
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)
752 {
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;
759
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);
771
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);
774
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);
789
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);
792
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);
795
796 IStorage_Release(stg2);
797
798 r = IStorage_Release(stg);
799 ok(r == 0, "wrong ref count\n");
800 }
801 IStorage_Release(stgprio);
802
803 DeleteFileW(filename);
804 }
805
806 static void test_writeclassstg(void)
807 {
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 CLSID temp_cls;
814
815 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
816 return;
817
818 DeleteFileW(filename);
819
820 /* create the file */
821 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
822 STGM_READWRITE, 0, &stg);
823 ok(r==S_OK, "StgCreateDocfile failed\n");
824
825 r = ReadClassStg( NULL, NULL );
826 ok(r == E_INVALIDARG, "ReadClassStg should return E_INVALIDARG instead of 0x%08X\n", r);
827
828 r = ReadClassStg( stg, NULL );
829 ok(r == E_INVALIDARG, "ReadClassStg should return E_INVALIDARG instead of 0x%08X\n", r);
830
831 temp_cls.Data1 = 0xdeadbeef;
832 r = ReadClassStg( stg, &temp_cls );
833 ok(r == S_OK, "ReadClassStg failed with 0x%08X\n", r);
834
835 ok(IsEqualCLSID(&temp_cls, &CLSID_NULL), "ReadClassStg returned wrong clsid\n");
836
837 r = WriteClassStg( NULL, NULL );
838 ok(r == E_INVALIDARG, "WriteClassStg should return E_INVALIDARG instead of 0x%08X\n", r);
839
840 r = WriteClassStg( stg, NULL );
841 ok(r == STG_E_INVALIDPOINTER, "WriteClassStg should return STG_E_INVALIDPOINTER instead of 0x%08X\n", r);
842
843 r = WriteClassStg( stg, &test_stg_cls );
844 ok( r == S_OK, "WriteClassStg failed with 0x%08X\n", r);
845
846 r = ReadClassStg( stg, &temp_cls );
847 ok( r == S_OK, "ReadClassStg failed with 0x%08X\n", r);
848 ok(IsEqualCLSID(&temp_cls, &test_stg_cls), "ReadClassStg returned wrong clsid\n");
849
850 r = IStorage_Release( stg );
851 ok (r == 0, "storage not released\n");
852
853 DeleteFileW(filename);
854 }
855
856 static void test_streamenum(void)
857 {
858 static const WCHAR szPrefix[] = { 's','t','g',0 };
859 static const WCHAR szDot[] = { '.',0 };
860 WCHAR filename[MAX_PATH];
861 IStorage *stg = NULL;
862 HRESULT r;
863 IStream *stm = NULL;
864 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
865 STATSTG stat;
866 IEnumSTATSTG *ee = NULL;
867 ULONG count;
868
869 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
870 return;
871
872 DeleteFileW(filename);
873
874 /* create the file */
875 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
876 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
877 ok(r==S_OK, "StgCreateDocfile failed\n");
878
879 r = WriteClassStg( stg, &test_stg_cls );
880 ok( r == S_OK, "WriteClassStg failed\n");
881
882 r = IStorage_Commit( stg, STGC_DEFAULT );
883 ok( r == S_OK, "IStorage_Commit failed\n");
884
885 /* now create a stream */
886 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
887 ok(r==S_OK, "IStorage->CreateStream failed\n");
888
889 r = IStream_Release(stm);
890
891 /* first enum ... should be 1 stream */
892 r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
893 ok(r==S_OK, "IStorage->EnumElements failed\n");
894
895 count = 0xf00;
896 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
897 ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
898 ok(count == 1, "count wrong\n");
899
900 r = IEnumSTATSTG_Release(ee);
901
902 /* second enum... destroy the stream before reading */
903 r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
904 ok(r==S_OK, "IStorage->EnumElements failed\n");
905
906 r = IStorage_DestroyElement(stg, stmname);
907 ok(r==S_OK, "IStorage->EnumElements failed\n");
908
909 todo_wine {
910 count = 0xf00;
911 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
912 ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
913 ok(count == 0, "count wrong\n");
914 }
915
916 /* reset and try again */
917 r = IEnumSTATSTG_Reset(ee);
918 ok(r==S_OK, "IEnumSTATSTG->Reset failed\n");
919
920 count = 0xf00;
921 r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
922 ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
923 ok(count == 0, "count wrong\n");
924
925 r = IEnumSTATSTG_Release(ee);
926 ok (r == 0, "enum not released\n");
927
928 r = IStorage_Release( stg );
929 ok (r == 0, "storage not released\n");
930
931 DeleteFileW(filename);
932 }
933
934 static void test_transact(void)
935 {
936 static const WCHAR szPrefix[] = { 's','t','g',0 };
937 static const WCHAR szDot[] = { '.',0 };
938 WCHAR filename[MAX_PATH];
939 IStorage *stg = NULL, *stg2 = NULL;
940 HRESULT r;
941 IStream *stm = NULL;
942 static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
943 static const WCHAR stmname2[] = { 'F','O','O',0 };
944
945 if(!GetTempFileNameW(szDot, szPrefix, 0, filename))
946 return;
947
948 DeleteFileW(filename);
949
950 /* create the file */
951 r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
952 STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
953 ok(r==S_OK, "StgCreateDocfile failed\n");
954
955 /* now create a stream, but don't commit it */
956 r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
957 ok(r==S_OK, "IStorage->CreateStream failed\n");
958
959 r = IStream_Write(stm, "this is stream 1\n", 16, NULL);
960 ok(r==S_OK, "IStream->Write failed\n");
961
962 r = IStream_Release(stm);
963
964 r = IStorage_Commit(stg, 0);
965 ok(r==S_OK, "IStorage->Commit failed\n");
966
967 /* now create a stream, but don't commit it */
968 stm = NULL;
969 r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
970 ok(r==S_OK, "IStorage->CreateStream failed\n");
971
972 r = IStream_Write(stm, "this is stream 2\n", 16, NULL);
973 ok(r==S_OK, "IStream->Write failed\n");
974
975 r = IStream_Commit(stm, STGC_ONLYIFCURRENT | STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE);
976 ok(r==S_OK, "IStream->Commit failed\n");
977
978 r = IStream_Release(stm);
979
980 IStorage_Release(stg);
981
982 stm = NULL;
983 stg = NULL;
984 r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg);
985 ok(r==S_OK, "StgOpenStorage failed\n");
986
987 if (!stg)
988 return;
989
990 r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_DENY_NONE|STGM_READ, 0, &stm );
991 ok(r==STG_E_INVALIDFLAG, "IStorage->OpenStream failed %08x\n", r);
992
993 r = IStorage_OpenStream(stg, stmname, NULL, STGM_DELETEONRELEASE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
994 ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
995
996 r = IStorage_OpenStream(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
997 ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
998
999 r = IStorage_OpenStorage(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
1000 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);
1001
1002 todo_wine {
1003 r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1004 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream should fail %08x\n", r);
1005 }
1006 if (stm)
1007 r = IStream_Release(stm);
1008
1009 r = IStorage_OpenStorage(stg, stmname2, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
1010 ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);
1011
1012 r = IStorage_OpenStream(stg, stmname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1013 ok(r==S_OK, "IStorage->OpenStream should fail %08x\n", r);
1014 if (stm)
1015 r = IStream_Release(stm);
1016
1017 IStorage_Release(stg);
1018
1019 r = DeleteFileW(filename);
1020 ok( r == TRUE, "deleted file\n");
1021 }
1022
1023 static void test_ReadClassStm(void)
1024 {
1025 CLSID clsid;
1026 HRESULT hr;
1027 IStream *pStream;
1028 static const LARGE_INTEGER llZero;
1029
1030 hr = ReadClassStm(NULL, &clsid);
1031 ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1032
1033 hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
1034 ok_ole_success(hr, "CreateStreamOnHGlobal");
1035 hr = WriteClassStm(pStream, &test_stg_cls);
1036 ok_ole_success(hr, "WriteClassStm");
1037
1038 hr = ReadClassStm(pStream, NULL);
1039 ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);
1040
1041 /* test not rewound stream */
1042 hr = ReadClassStm(pStream, &clsid);
1043 ok(hr == STG_E_READFAULT, "ReadClassStm should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
1044 ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid should have been zeroed\n");
1045
1046 hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
1047 ok_ole_success(hr, "IStream_Seek");
1048 hr = ReadClassStm(pStream, &clsid);
1049 ok_ole_success(hr, "ReadClassStm");
1050 ok(IsEqualCLSID(&clsid, &test_stg_cls), "clsid should have been set to CLSID_WineTest\n");
1051 }
1052
1053 struct access_res
1054 {
1055 BOOL gothandle;
1056 DWORD lasterr;
1057 BOOL ignore;
1058 };
1059
1060 static const struct access_res create[16] =
1061 {
1062 { TRUE, ERROR_SUCCESS, TRUE },
1063 { TRUE, ERROR_SUCCESS, TRUE },
1064 { TRUE, ERROR_SUCCESS, FALSE },
1065 { TRUE, ERROR_SUCCESS, FALSE },
1066 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1067 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1068 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1069 { TRUE, ERROR_SUCCESS, FALSE },
1070 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1071 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1072 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1073 { TRUE, ERROR_SUCCESS, TRUE },
1074 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1075 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1076 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1077 { TRUE, ERROR_SUCCESS, TRUE }
1078 };
1079
1080 static const struct access_res create_commit[16] =
1081 {
1082 { TRUE, ERROR_SUCCESS, TRUE },
1083 { TRUE, ERROR_SUCCESS, TRUE },
1084 { TRUE, ERROR_SUCCESS, FALSE },
1085 { TRUE, ERROR_SUCCESS, FALSE },
1086 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1087 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1088 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1089 { TRUE, ERROR_SUCCESS, FALSE },
1090 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1091 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1092 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1093 { TRUE, ERROR_SUCCESS, TRUE },
1094 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1095 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1096 { FALSE, ERROR_SHARING_VIOLATION, FALSE },
1097 { TRUE, ERROR_SUCCESS, TRUE }
1098 };
1099
1100 static const struct access_res create_close[16] =
1101 {
1102 { TRUE, ERROR_SUCCESS, FALSE },
1103 { TRUE, ERROR_SUCCESS, FALSE },
1104 { TRUE, ERROR_SUCCESS, FALSE },
1105 { TRUE, ERROR_SUCCESS, FALSE },
1106 { TRUE, ERROR_SUCCESS, FALSE },
1107 { TRUE, ERROR_SUCCESS, FALSE },
1108 { TRUE, ERROR_SUCCESS, FALSE },
1109 { TRUE, ERROR_SUCCESS, FALSE },
1110 { TRUE, ERROR_SUCCESS, FALSE },
1111 { TRUE, ERROR_SUCCESS, FALSE },
1112 { TRUE, ERROR_SUCCESS, FALSE },
1113 { TRUE, ERROR_SUCCESS, FALSE },
1114 { TRUE, ERROR_SUCCESS, FALSE },
1115 { TRUE, ERROR_SUCCESS, FALSE },
1116 { TRUE, ERROR_SUCCESS, FALSE },
1117 { TRUE, ERROR_SUCCESS }
1118 };
1119
1120 static void _test_file_access(LPCSTR file, const struct access_res *ares, DWORD line)
1121 {
1122 DWORD access = 0, share = 0;
1123 DWORD lasterr;
1124 HANDLE hfile;
1125 int i, j, idx = 0;
1126
1127 for (i = 0; i < 4; i++)
1128 {
1129 if (i == 0) access = 0;
1130 if (i == 1) access = GENERIC_READ;
1131 if (i == 2) access = GENERIC_WRITE;
1132 if (i == 3) access = GENERIC_READ | GENERIC_WRITE;
1133
1134 for (j = 0; j < 4; j++)
1135 {
1136 if (ares[idx].ignore)
1137 continue;
1138
1139 if (j == 0) share = 0;
1140 if (j == 1) share = FILE_SHARE_READ;
1141 if (j == 2) share = FILE_SHARE_WRITE;
1142 if (j == 3) share = FILE_SHARE_READ | FILE_SHARE_WRITE;
1143
1144 SetLastError(0xdeadbeef);
1145 hfile = CreateFileA(file, access, share, NULL, OPEN_EXISTING,
1146 FILE_ATTRIBUTE_NORMAL, 0);
1147 lasterr = GetLastError();
1148
1149 ok((hfile != INVALID_HANDLE_VALUE) == ares[idx].gothandle,
1150 "(%d, handle, %d): Expected %d, got %d\n",
1151 line, idx, ares[idx].gothandle,
1152 (hfile != INVALID_HANDLE_VALUE));
1153
1154 ok(lasterr == ares[idx].lasterr ||
1155 broken(lasterr == 0xdeadbeef) /* win9x */,
1156 "(%d, lasterr, %d): Expected %d, got %d\n",
1157 line, idx, ares[idx].lasterr, lasterr);
1158
1159 CloseHandle(hfile);
1160 idx++;
1161 }
1162 }
1163 }
1164
1165 #define test_file_access(file, ares) _test_file_access(file, ares, __LINE__)
1166
1167 static void test_access(void)
1168 {
1169 IStorage *stg;
1170 HRESULT hr;
1171
1172 static const WCHAR fileW[] = {'w','i','n','e','t','e','s','t',0};
1173
1174 /* STGM_TRANSACTED */
1175
1176 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1177 STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, &stg);
1178 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1179
1180 test_file_access("winetest", create);
1181
1182 hr = IStorage_Commit(stg, STGC_DEFAULT);
1183 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1184
1185 test_file_access("winetest", create_commit);
1186
1187 IStorage_Release(stg);
1188
1189 test_file_access("winetest", create_close);
1190
1191 DeleteFileA("winetest");
1192
1193 /* STGM_DIRECT */
1194
1195 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1196 STGM_SHARE_EXCLUSIVE | STGM_DIRECT, 0, &stg);
1197 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1198
1199 test_file_access("winetest", create);
1200
1201 hr = IStorage_Commit(stg, STGC_DEFAULT);
1202 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1203
1204 test_file_access("winetest", create_commit);
1205
1206 IStorage_Release(stg);
1207
1208 test_file_access("winetest", create_close);
1209
1210 DeleteFileA("winetest");
1211
1212 /* STGM_SHARE_DENY_NONE */
1213
1214 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1215 STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
1216 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1217
1218 test_file_access("winetest", create);
1219
1220 hr = IStorage_Commit(stg, STGC_DEFAULT);
1221 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1222
1223 test_file_access("winetest", create_commit);
1224
1225 IStorage_Release(stg);
1226
1227 test_file_access("winetest", create_close);
1228
1229 DeleteFileA("winetest");
1230
1231 /* STGM_SHARE_DENY_READ */
1232
1233 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1234 STGM_SHARE_DENY_READ | STGM_TRANSACTED, 0, &stg);
1235 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1236
1237 test_file_access("winetest", create);
1238
1239 hr = IStorage_Commit(stg, STGC_DEFAULT);
1240 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1241
1242 test_file_access("winetest", create_commit);
1243
1244 IStorage_Release(stg);
1245
1246 test_file_access("winetest", create_close);
1247
1248 DeleteFileA("winetest");
1249
1250 /* STGM_SHARE_DENY_WRITE */
1251
1252 hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
1253 STGM_SHARE_DENY_WRITE | STGM_TRANSACTED, 0, &stg);
1254 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1255
1256 test_file_access("winetest", create);
1257
1258 hr = IStorage_Commit(stg, STGC_DEFAULT);
1259 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1260
1261 test_file_access("winetest", create_commit);
1262
1263 IStorage_Release(stg);
1264
1265 test_file_access("winetest", create_close);
1266
1267 DeleteFileA("winetest");
1268 }
1269
1270 START_TEST(storage32)
1271 {
1272 test_hglobal_storage_stat();
1273 test_create_storage_modes();
1274 test_storage_stream();
1275 test_open_storage();
1276 test_storage_suminfo();
1277 test_storage_refcount();
1278 test_streamenum();
1279 test_transact();
1280 test_ReadClassStm();
1281 test_access();
1282 test_writeclassstg();
1283 }