storage32.c 116 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit tests for OLE storage
 *
 * Copyright (c) 2004 Mike McCormack
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24
 */

#include <stdio.h>

#define COBJMACROS

25
#include <windows.h>
26
#include "wine/test.h"
27

28 29 30 31 32 33
#include "ole2.h"
#include "objidl.h"
#include "initguid.h"

DEFINE_GUID( test_stg_cls, 0x88888888, 0x0425, 0x0000, 0,0,0,0,0,0,0,0);

34 35
#define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)

36 37
static CHAR filenameA[MAX_PATH];
static WCHAR filename[MAX_PATH];
38

39
static const char file1_nameA[] = {'c','o','p','y','t','e','s','t','A',0};
40
static const WCHAR file1_name[] = {'c','o','p','y','t','e','s','t','A',0};
41
static const char file2_nameA[] = {'c','o','p','y','t','e','s','t','B',0};
42 43 44 45 46 47 48
static const WCHAR file2_name[] = {'c','o','p','y','t','e','s','t','B',0};
static const WCHAR stgA_name[] = {'S','t','o','r','a','g','e','A',0};
static const WCHAR stgB_name[] = {'S','t','o','r','a','g','e','B',0};
static const WCHAR strmA_name[] = {'S','t','r','e','a','m','A',0};
static const WCHAR strmB_name[] = {'S','t','r','e','a','m','B',0};
static const WCHAR strmC_name[] = {'S','t','r','e','a','m','C',0};

49 50 51 52 53 54 55 56 57
/* Win9x and WinMe don't have lstrcmpW */
static int strcmp_ww(LPCWSTR strw1, LPCWSTR strw2)
{
    CHAR stra1[512], stra2[512];
    WideCharToMultiByte(CP_ACP, 0, strw1, -1, stra1, sizeof(stra1), NULL, NULL);
    WideCharToMultiByte(CP_ACP, 0, strw2, -1, stra2, sizeof(stra2), NULL, NULL);
    return lstrcmpA(stra1, stra2);
}

58
static void test_hglobal_storage_stat(void)
59 60 61 62 63 64 65 66 67 68
{
    ILockBytes *ilb = NULL;
    IStorage *stg = NULL;
    HRESULT r;
    STATSTG stat;
    DWORD mode, refcount;

    r = CreateILockBytesOnHGlobal( NULL, TRUE, &ilb );
    ok( r == S_OK, "CreateILockBytesOnHGlobal failed\n");

69 70 71
    r = StgIsStorageILockBytes( ilb );
    ok( r == S_FALSE, "StgIsStorageILockBytes should have failed\n");

72 73
    mode = STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE;/*0x1012*/
    r = StgCreateDocfileOnILockBytes( ilb, mode, 0,  &stg );
74
    ok( r == S_OK, "StgCreateDocfileOnILockBytes failed\n");
75 76 77

    r = WriteClassStg( stg, &test_stg_cls );
    ok( r == S_OK, "WriteClassStg failed\n");
78 79 80

    r = StgIsStorageILockBytes( ilb );
    ok( r == S_OK, "StgIsStorageILockBytes failed\n");
81 82 83 84 85 86 87

    memset( &stat, 0, sizeof stat );
    r = IStorage_Stat( stg, &stat, 0 );

    ok( stat.pwcsName == NULL, "storage name not null\n");
    ok( stat.type == 1, "type is wrong\n");
    ok( stat.grfMode == 0x12, "grf mode is incorrect\n");
88
    ok( !memcmp(&stat.clsid, &test_stg_cls, sizeof test_stg_cls), "CLSID is wrong\n");
89 90 91 92 93 94 95

    refcount = IStorage_Release( stg );
    ok( refcount == 0, "IStorage refcount is wrong\n");
    refcount = ILockBytes_Release( ilb );
    ok( refcount == 0, "ILockBytes refcount is wrong\n");
}

96
static void test_create_storage_modes(void)
97 98 99 100
{
   IStorage *stg = NULL;
   HRESULT r;

101
   DeleteFileA(filenameA);
102 103 104 105 106 107 108 109 110 111 112 113

   /* test with some invalid parameters */
   r = StgCreateDocfile( NULL, 0, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, 0, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_CREATE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
114
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, NULL);
115
   ok(r==STG_E_INVALIDPOINTER, "StgCreateDocfile succeeded\n");
116
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, &stg);
117
   ok(r==STG_E_INVALIDPARAMETER, "StgCreateDocfile succeeded\n");
118 119
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
120 121 122 123
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_PRIORITY, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

   /* StgCreateDocfile seems to be very particular about the flags it accepts */
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | STGM_WRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 8, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x8000, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x80000, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED | 0x800000, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
140 141
   ok(stg == NULL, "stg was set\n");

142
   /* check what happens if the file already exists (which is how it's meant to be used) */
143 144 145 146
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
   ok(r==S_OK, "StgCreateDocfile failed\n");
   r = IStorage_Release(stg);
   ok(r == 0, "storage not released\n");
147
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
148
   ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n"); /* FAILIFTHERE is default */
149
   r = StgCreateDocfile( filename, STGM_READ, 0, &stg);
150
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n"); /* need at least readmode and sharemode */
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
   r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
   r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
   r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
   r = StgCreateDocfile( filename, STGM_SHARE_DENY_NONE | STGM_WRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
   r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_WRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile failed\n");
   r = StgCreateDocfile( filename, STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
167 168
   r = StgCreateDocfile( filename, STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READ, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile wrong error\n");
169
   ok(DeleteFileA(filenameA), "failed to delete file\n");
170

171
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
172 173 174
   ok(r==S_OK, "StgCreateDocfile failed\n");
   r = IStorage_Release(stg);
   ok(r == 0, "storage not released\n");
175
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED |STGM_FAILIFTHERE, 0, &stg);
176 177 178 179
   ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");
   r = StgCreateDocfile( filename, STGM_SHARE_EXCLUSIVE | STGM_WRITE, 0, &stg);
   ok(r==STG_E_FILEALREADYEXISTS, "StgCreateDocfile wrong error\n");

180 181
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_WRITE | STGM_READWRITE, 0, &stg);
   ok(r==STG_E_INVALIDFLAG, "StgCreateDocfile succeeded\n");
182
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
183 184 185
   ok(r==S_OK, "StgCreateDocfile failed\n");
   r = IStorage_Release(stg);
   ok(r == 0, "storage not released\n");
186
   ok(DeleteFileA(filenameA), "failed to delete file\n");
187

188 189 190 191
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
   ok(r==S_OK, "StgCreateDocfile failed\n");
   r = IStorage_Release(stg);
   ok(r == 0, "storage not released\n");
192
   ok(DeleteFileA(filenameA), "failed to delete file\n");
193

194 195 196 197 198 199 200
   /* test the way excel uses StgCreateDocFile */
   r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
   ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
   if(r == S_OK)
   {
      r = IStorage_Release(stg);
      ok(r == 0, "storage not released\n");
201
      ok(DeleteFileA(filenameA), "failed to delete file\n");
202 203
   }

204 205 206 207 208 209 210
   /* and the way windows media uses it ... */
   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_DENY_NONE | STGM_READWRITE | STGM_TRANSACTED, 0, &stg);
   ok(r==S_OK, "StgCreateDocfile the windows media way failed\n");
   if (r == S_OK)
   {
      r = IStorage_Release(stg);
      ok(r == 0, "storage not released\n");
211
      ok(DeleteFileA(filenameA), "failed to delete file\n");
212 213
   }

214 215 216 217 218 219 220
   /* looks like we need STGM_TRANSACTED or STGM_CREATE */
   r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, 0, &stg);
   ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
   if(r == S_OK)
   {
      r = IStorage_Release(stg);
      ok(r == 0, "storage not released\n");
221
      ok(DeleteFileA(filenameA), "failed to delete file\n");
222 223
   }

224 225 226 227 228 229
   r = StgCreateDocfile( filename, STGM_TRANSACTED|STGM_CREATE|STGM_SHARE_DENY_WRITE|STGM_WRITE, 0, &stg);
   ok(r==S_OK, "StgCreateDocfile the excel way failed\n");
   if(r == S_OK)
   {
      r = IStorage_Release(stg);
      ok(r == 0, "storage not released\n");
230
      ok(DeleteFileA(filenameA), "failed to delete file\n");
231 232 233 234 235 236 237 238
   }

   r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
   ok(r==S_OK, "StgCreateDocfile the powerpoint way failed\n");
   if(r == S_OK)
   {
      r = IStorage_Release(stg);
      ok(r == 0, "storage not released\n");
239
      ok(DeleteFileA(filenameA), "failed to delete file\n");
240 241
   }

242 243 244 245 246
   /* test the way msi uses StgCreateDocfile */
   r = StgCreateDocfile( filename, STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stg);
   ok(r==S_OK, "StgCreateDocFile failed\n");
   r = IStorage_Release(stg);
   ok(r == 0, "storage not released\n");
247
   ok(DeleteFileA(filenameA), "failed to delete file\n");
248 249
}

250 251 252
static void test_stgcreatestorageex(void)
{
   HRESULT (WINAPI *pStgCreateStorageEx)(const WCHAR* pwcsName, DWORD grfMode, DWORD stgfmt, DWORD grfAttrs, STGOPTIONS* pStgOptions, void* reserved, REFIID riid, void** ppObjectOpen);
253
   HMODULE hOle32 = GetModuleHandleA("ole32");
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
   IStorage *stg = NULL;
   STGOPTIONS stgoptions = {1, 0, 4096};
   HRESULT r;

   pStgCreateStorageEx = (void *) GetProcAddress(hOle32, "StgCreateStorageEx");
   if (!pStgCreateStorageEx)
   {
      win_skip("skipping test on NT4\n");
      return;
   }

   DeleteFileA(filenameA);

   /* Verify that StgCreateStorageEx can accept an options param */
   r = pStgCreateStorageEx( filename,
                           STGM_SHARE_EXCLUSIVE | STGM_READWRITE,
                           STGFMT_DOCFILE,
                           0,
                           &stgoptions,
                           NULL,
                           &IID_IStorage,
                           (void **) &stg);
   ok(r==S_OK || r==STG_E_UNIMPLEMENTEDFUNCTION, "StgCreateStorageEx with options failed\n");
   if (r==STG_E_UNIMPLEMENTEDFUNCTION)
   {
      /* We're on win98 which means all bets are off.  Let's get out of here. */
      win_skip("skipping test on win9x\n");
      return;
   }

   r = IStorage_Release(stg);
   ok(r == 0, "storage not released\n");
   ok(DeleteFileA(filenameA), "failed to delete file\n");

   /* Verify that StgCreateStorageEx can accept a NULL pStgOptions */
   r = pStgCreateStorageEx( filename,
                           STGM_SHARE_EXCLUSIVE | STGM_READWRITE,
                           STGFMT_STORAGE,
                           0,
                           NULL,
                           NULL,
                           &IID_IStorage,
                           (void **) &stg);
   ok(r==S_OK, "StgCreateStorageEx with NULL options failed\n");
   r = IStorage_Release(stg);
   ok(r == 0, "storage not released\n");
   ok(DeleteFileA(filenameA), "failed to delete file\n");
}

303
static void test_storage_stream(void)
304 305 306 307 308 309 310 311 312
{
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
    static const WCHAR longname[] = {
        'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
        'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0
    };
    IStorage *stg = NULL;
    HRESULT r;
    IStream *stm = NULL;
313
    IStream *stm2 = NULL;
314 315 316 317
    ULONG count = 0;
    LARGE_INTEGER pos;
    ULARGE_INTEGER p;
    unsigned char buffer[0x100];
318
    IUnknown *unk;
319

320
    DeleteFileA(filenameA);
321 322 323 324 325 326 327 328 329 330 331 332 333 334

    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* try create some invalid streams */
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 1, 0, &stm );
    ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 1, &stm );
    ok(r==STG_E_INVALIDPARAMETER, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, NULL );
    ok(r==STG_E_INVALIDPOINTER, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==STG_E_INVALIDNAME, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, longname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
335 336
    ok(r==STG_E_INVALIDNAME || broken(r==S_OK) /* nt4 */,
       "IStorage->CreateStream wrong error, got %d GetLastError()=%d\n", r, GetLastError());
337 338 339 340 341 342 343 344 345 346 347 348 349 350
    r = IStorage_CreateStream(stg, stmname, STGM_READWRITE, 0, 0, &stm );
    ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, stmname, STGM_READ, 0, 0, &stm );
    ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, stmname, STGM_WRITE, 0, 0, &stm );
    ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READWRITE, 0, 0, &stm );
    ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_DENY_NONE | STGM_READ, 0, 0, &stm );
    ok(r==STG_E_INVALIDFLAG, "IStorage->CreateStream wrong error\n");

    /* now really create a stream and delete it */
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");
351 352 353 354 355 356 357

    /* test for support interfaces */
    r = IStream_QueryInterface(stm, &IID_IPersist, (void**)&unk);
    ok(r==E_NOINTERFACE, "got 0x%08x\n", r);
    r = IStream_QueryInterface(stm, &IID_IPersistStream, (void**)&unk);
    ok(r==E_NOINTERFACE, "got 0x%08x\n", r);

358 359 360 361 362 363 364 365 366 367 368
    r = IStream_Release(stm);
    ok(r == 0, "wrong ref count\n");
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==STG_E_FILEALREADYEXISTS, "IStorage->CreateStream failed\n");
    r = IStorage_DestroyElement(stg,stmname);
    ok(r==S_OK, "IStorage->DestroyElement failed\n");

    /* create a stream and write to it */
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

369 370 371
    r = IStream_Clone(stm, &stm2);
    ok(r==S_OK, "failed to clone stream\n");

372 373 374 375 376 377 378 379 380 381 382 383 384
    r = IStream_Write(stm, NULL, 0, NULL );
    ok(r==STG_E_INVALIDPOINTER, "IStream->Write wrong error\n");
    r = IStream_Write(stm, "Hello\n", 0, NULL );
    ok(r==S_OK, "failed to write stream\n");
    r = IStream_Write(stm, "Hello\n", 0, &count );
    ok(r==S_OK, "failed to write stream\n");
    r = IStream_Write(stm, "Hello\n", 6, &count );
    ok(r==S_OK, "failed to write stream\n");
    r = IStream_Commit(stm, STGC_DEFAULT );
    ok(r==S_OK, "failed to commit stream\n");
    r = IStream_Commit(stm, STGC_DEFAULT );
    ok(r==S_OK, "failed to commit stream\n");

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    /* Read past the end of the stream. */
    pos.QuadPart = 3;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 3, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to read\n");
    ok(count == 3, "read bytes past end of stream\n");
    pos.QuadPart = 10;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 10, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to read\n");
    ok(count == 0, "read bytes past end of stream\n");
    pos.QuadPart = 10000;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 10000, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to read\n");
    ok(count == 0, "read bytes past end of stream\n");

    /* Convert to a big block stream, and read past the end. */
    p.QuadPart = 5000;
    r = IStream_SetSize(stm,p);
    ok(r==S_OK, "failed to set pos\n");
    pos.QuadPart = 4997;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 4997, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to read\n");
    ok(count == 3, "read bytes past end of stream\n");
    pos.QuadPart = 5001;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 5001, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to read\n");
    ok(count == 0, "read bytes past end of stream\n");
    pos.QuadPart = 10000;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 10000, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to read\n");
    ok(count == 0, "read bytes past end of stream\n");

434 435 436 437 438 439 440 441 442 443 444 445 446 447
    /* seek round a bit, reset the stream size */
    pos.QuadPart = 0;
    r = IStream_Seek(stm, pos, 3, &p );
    ok(r==STG_E_INVALIDFUNCTION, "IStream->Seek returned wrong error\n");
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, NULL);
    ok(r==S_OK, "failed to seek stream\n");
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    r = IStream_SetSize(stm,p);
    ok(r==S_OK, "failed to set pos\n");
    pos.QuadPart = 10;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 10, "at wrong place\n");
448 449 450 451 452 453 454 455 456 457
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to set pos\n");
    ok(count == 0, "read bytes from empty stream\n");
    pos.QuadPart = 10000;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 10000, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to set pos\n");
    ok(count == 0, "read bytes from empty stream\n");
458 459 460 461 462 463 464 465 466
    pos.QuadPart = 0;
    r = IStream_Seek(stm, pos, STREAM_SEEK_END, &p );
    ok(r==S_OK, "failed to seek stream\n");
    ok(p.QuadPart == 0, "at wrong place\n");
    r = IStream_Read(stm, buffer, sizeof buffer, &count );
    ok(r==S_OK, "failed to set pos\n");
    ok(count == 0, "read bytes from empty stream\n");

    /* wrap up */
467 468 469 470 471 472 473 474 475 476
    r = IStream_Release(stm2);
    ok(r == 0, "wrong ref count\n");

    /* create a stream and write to it */
    r = IStorage_CreateStream(stg, stmname, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm2 );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &p);
    ok(r==STG_E_REVERTED, "overwritten stream should return STG_E_REVERTED instead of 0x%08x\n", r);

477 478
    r = IStream_Release(stm2);
    ok(r == 0, "wrong ref count\n");
479 480
    r = IStream_Release(stm);
    ok(r == 0, "wrong ref count\n");
481

482 483
    r = IStorage_Release(stg);
    ok(r == 0, "wrong ref count\n");
484 485 486 487 488 489 490 491 492 493 494 495 496

    /* try create some invalid streams */
    stg = NULL;
    stm = NULL;
    r = StgOpenStorage(filename, NULL, STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
    ok(r == S_OK, "should succeed\n");
    if (stg)
    {
        r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stm);
        ok(r == STG_E_INVALIDFLAG, "IStorage->OpenStream should return STG_E_INVALIDFLAG instead of 0x%08x\n", r);
        IStorage_Release(stg);
    }

497
    r = DeleteFileA(filenameA);
498
    ok(r, "file should exist\n");
499 500
}

501
static BOOL touch_file(LPCSTR filename)
502 503 504
{
    HANDLE file;

505
    file = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
506 507 508 509 510 511 512
                CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (file==INVALID_HANDLE_VALUE)
        return FALSE;
    CloseHandle(file);
    return TRUE;
}

513
static BOOL is_zero_length(LPCSTR filename)
514 515 516 517
{
    HANDLE file;
    DWORD len;

518
    file = CreateFileA(filename, GENERIC_READ, 0, NULL,
519 520 521 522 523 524 525 526
                OPEN_EXISTING, 0, NULL);
    if (file==INVALID_HANDLE_VALUE)
        return FALSE;
    len = GetFileSize(file, NULL);
    CloseHandle(file);
    return len == 0;
}

527
static BOOL is_existing_file(LPCSTR filename)
528 529 530
{
    HANDLE file;

531
    file = CreateFileA(filename, GENERIC_READ, 0, NULL,
532 533 534 535 536 537 538
                       OPEN_EXISTING, 0, NULL);
    if (file==INVALID_HANDLE_VALUE)
        return FALSE;
    CloseHandle(file);
    return TRUE;
}

539
static void test_open_storage(void)
540 541 542 543
{
    static const WCHAR szNonExist[] = { 'n','o','n','e','x','i','s','t',0 };
    IStorage *stg = NULL, *stg2 = NULL;
    HRESULT r;
544
    DWORD stgm;
545

546
    /* try opening a zero length file - it should stay zero length */
547 548
    DeleteFileA(filenameA);
    touch_file(filenameA);
549 550 551 552 553 554 555
    stgm = STGM_NOSCRATCH | STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE;
    r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
    ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");

    stgm = STGM_SHARE_EXCLUSIVE | STGM_READWRITE;
    r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
    ok(r==STG_E_FILEALREADYEXISTS, "StgOpenStorage didn't fail\n");
556
    ok(is_zero_length(filenameA), "file length changed\n");
557

558
    DeleteFileA(filenameA);
559

560
    /* try opening a nonexistent file - it should not create it */
561 562
    stgm = STGM_DIRECT | STGM_SHARE_EXCLUSIVE | STGM_READWRITE;
    r = StgOpenStorage( filename, NULL, stgm, NULL, 0, &stg);
563
    ok(r!=S_OK, "StgOpenStorage failed: 0x%08x\n", r);
564
    if (r==S_OK) IStorage_Release(stg);
565 566
    ok(!is_existing_file(filenameA), "StgOpenStorage should not create a file\n");
    DeleteFileA(filenameA);
567

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");
    IStorage_Release(stg);

    r = StgOpenStorage( filename, NULL, 0, NULL, 0, &stg);
    ok(r==STG_E_INVALIDFLAG, "StgOpenStorage wrong error\n");
    r = StgOpenStorage( NULL, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
    ok(r==STG_E_INVALIDNAME, "StgOpenStorage wrong error\n");
    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, NULL);
    ok(r==STG_E_INVALIDPOINTER, "StgOpenStorage wrong error\n");
    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 1, &stg);
    ok(r==STG_E_INVALIDPARAMETER, "StgOpenStorage wrong error\n");
    r = StgOpenStorage( szNonExist, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
    ok(r==STG_E_FILENOTFOUND, "StgOpenStorage failed\n");
    r = StgOpenStorage( filename, NULL, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
    ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
    r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ, NULL, 0, &stg);
    ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
    r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_READ | STGM_READ, NULL, 0, &stg);
    ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
589 590
    r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
    ok(r==STG_E_INVALIDFLAG, "StgOpenStorage failed\n");
591 592

    /* open it for real */
593 594 595 596 597 598 599 600
    r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg); /* XLViewer 97/2000 */
    ok(r==S_OK, "StgOpenStorage failed\n");
    if(stg)
    {
        r = IStorage_Release(stg);
        ok(r == 0, "wrong ref count\n");
    }

601 602
    r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE | STGM_READ, NULL, 0, &stg);
    ok(r==S_OK, "StgOpenStorage failed\n");
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    if(stg)
    {
        r = IStorage_Release(stg);
        ok(r == 0, "wrong ref count\n");
    }

    /* test the way word opens its custom dictionary */
    r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_TRANSACTED |
                        STGM_SHARE_DENY_WRITE | STGM_READWRITE, NULL, 0, &stg);
    ok(r==S_OK, "StgOpenStorage failed\n");
    if(stg)
    {
        r = IStorage_Release(stg);
        ok(r == 0, "wrong ref count\n");
    }
618 619 620 621 622

    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
    ok(r==S_OK, "StgOpenStorage failed\n");
    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg2);
    ok(r==STG_E_SHAREVIOLATION, "StgOpenStorage failed\n");
623 624 625 626 627
    if(stg)
    {
        r = IStorage_Release(stg);
        ok(r == 0, "wrong ref count\n");
    }
628

629 630 631 632 633
    /* now try write to a storage file we opened read-only */ 
    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg);
    ok(r==S_OK, "StgOpenStorage failed\n");
    if(stg)
    { 
634
        static const WCHAR stmname[] =  { 'w','i','n','e','t','e','s','t',0};
635 636 637 638 639 640 641 642 643 644 645 646 647
        IStream *stm = NULL;
        IStorage *stg2 = NULL;

        r = IStorage_CreateStream( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE,
                                   0, 0, &stm );
        ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");
        r = IStorage_CreateStorage( stg, stmname, STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
        ok(r == STG_E_ACCESSDENIED, "CreateStream should fail\n");

        r = IStorage_Release(stg);
        ok(r == 0, "wrong ref count\n");
    }

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    /* open like visio 2003 */
    stg = NULL;
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_NONE, NULL, 0, &stg);
    ok(r == S_OK, "should succeed\n");
    if (stg)
        IStorage_Release(stg);

    /* test other sharing modes with STGM_PRIORITY */
    stg = NULL;
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
    ok(r == S_OK, "should succeed\n");
    if (stg)
        IStorage_Release(stg);

    stg = NULL;
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
    ok(r == S_OK, "should succeed\n");
    if (stg)
        IStorage_Release(stg);

    stg = NULL;
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_SHARE_DENY_READ, NULL, 0, &stg);
    ok(r == S_OK, "should succeed\n");
    if (stg)
        IStorage_Release(stg);

674 675 676 677
    /* open like Project 2003 */
    stg = NULL;
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg);
    ok(r == S_OK, "should succeed\n");
678 679 680 681
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stg2);
    ok(r == S_OK, "should succeed\n");
    if (stg2)
        IStorage_Release(stg2);
682 683 684
    if (stg)
        IStorage_Release(stg);

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
    stg = NULL;
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY | STGM_READWRITE, NULL, 0, &stg);
    ok(r == STG_E_INVALIDFLAG, "should fail\n");

    r = StgOpenStorage( filename, NULL, STGM_TRANSACTED | STGM_PRIORITY, NULL, 0, &stg);
    ok(r == STG_E_INVALIDFLAG, "should fail\n");

    r = StgOpenStorage( filename, NULL, STGM_SIMPLE | STGM_PRIORITY, NULL, 0, &stg);
    ok(r == STG_E_INVALIDFLAG, "should fail\n");

    r = StgOpenStorage( filename, NULL, STGM_DELETEONRELEASE | STGM_PRIORITY, NULL, 0, &stg);
    ok(r == STG_E_INVALIDFUNCTION, "should fail\n");

    r = StgOpenStorage( filename, NULL, STGM_NOSCRATCH | STGM_PRIORITY, NULL, 0, &stg);
    ok(r == STG_E_INVALIDFLAG, "should fail\n");

    r = StgOpenStorage( filename, NULL, STGM_NOSNAPSHOT | STGM_PRIORITY, NULL, 0, &stg);
    ok(r == STG_E_INVALIDFLAG, "should fail\n");

704
    r = DeleteFileA(filenameA);
705 706 707
    ok(r, "file didn't exist\n");
}

708
static void test_storage_suminfo(void)
709 710 711 712 713 714
{
    IStorage *stg = NULL;
    IPropertySetStorage *propset = NULL;
    IPropertyStorage *ps = NULL;
    HRESULT r;

715
    DeleteFileA(filenameA);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | 
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    r = IStorage_QueryInterface( stg, &IID_IPropertySetStorage, (LPVOID) &propset );
    ok(r == S_OK, "query interface failed\n");

    /* delete it */
    r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
    ok(r == STG_E_FILENOTFOUND, "deleted property set storage\n");

    r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
                                STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
    ok(r == STG_E_FILENOTFOUND, "opened property set storage\n");

    r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
                                STGM_READ | STGM_SHARE_EXCLUSIVE, &ps );
    ok(r == STG_E_INVALIDFLAG, "created property set storage\n");

    r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
                                STGM_READ, &ps );
    ok(r == STG_E_INVALIDFLAG, "created property set storage\n");

    r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0, 0, &ps );
    ok(r == STG_E_INVALIDFLAG, "created property set storage\n");

    r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
                                STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
    ok(r == STG_E_INVALIDFLAG, "created property set storage\n");

    r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
                                STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, &ps );
    ok(r == STG_E_INVALIDFLAG, "created property set storage\n");

752
    /* now try really creating a property set */
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
                                STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
    ok(r == S_OK, "failed to create property set storage\n");

    if( ps )
        IPropertyStorage_Release(ps);

    /* now try creating the same thing again */
    r = IPropertySetStorage_Create( propset, &FMTID_SummaryInformation, NULL, 0,
                                STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps );
    ok(r == S_OK, "failed to create property set storage\n");
    if( ps )
        IPropertyStorage_Release(ps);

    /* should be able to open it */
    r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
            STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
    ok(r == S_OK, "open failed\n");
    if(r == S_OK)
        IPropertyStorage_Release(ps);

    /* delete it */
    r = IPropertySetStorage_Delete( propset, &FMTID_SummaryInformation );
    ok(r == S_OK, "failed to delete property set storage\n");

    /* try opening with an invalid FMTID */
    r = IPropertySetStorage_Open( propset, NULL, 
            STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
    ok(r == E_INVALIDARG, "open succeeded\n");
    if(r == S_OK)
        IPropertyStorage_Release(ps);

    /* try a bad guid */
    r = IPropertySetStorage_Open( propset, &IID_IStorage, 
            STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
    ok(r == STG_E_FILENOTFOUND, "open succeeded\n");
    if(r == S_OK)
        IPropertyStorage_Release(ps);
    

    /* try some invalid flags */
    r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
            STGM_CREATE | STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
    ok(r == STG_E_INVALIDFLAG, "open succeeded\n");
    if(r == S_OK)
        IPropertyStorage_Release(ps);

    /* after deleting it, it should be gone */
    r = IPropertySetStorage_Open( propset, &FMTID_SummaryInformation, 
            STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &ps);
    ok(r == STG_E_FILENOTFOUND, "open failed\n");
    if(r == S_OK)
        IPropertyStorage_Release(ps);

    r = IPropertySetStorage_Release( propset );
    ok(r == 1, "ref count wrong\n");

    r = IStorage_Release(stg);
    ok(r == 0, "ref count wrong\n");

813
    DeleteFileA(filenameA);
814 815
}

816 817 818
static void test_storage_refcount(void)
{
    IStorage *stg = NULL;
819
    IStorage *stgprio = NULL;
820 821 822 823 824 825
    HRESULT r;
    IStream *stm = NULL;
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
    LARGE_INTEGER pos;
    ULARGE_INTEGER upos;
    STATSTG stat;
826
    char buffer[10];
827

828
    DeleteFileA(filenameA);
829 830 831 832 833 834

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | 
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

835 836 837 838 839 840
    r = WriteClassStg( stg, &test_stg_cls );
    ok( r == S_OK, "WriteClassStg failed\n");

    r = IStorage_Commit( stg, STGC_DEFAULT );
    ok( r == S_OK, "IStorage_Commit failed\n");

841 842 843 844 845 846 847 848 849 850 851 852 853 854
    /* now create a stream */
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStorage_Release( stg );
    ok (r == 0, "storage not released\n");

    pos.QuadPart = 0;
    r = IStream_Seek( stm, pos, 0, &upos );
    ok (r == STG_E_REVERTED, "seek should fail\n");

    r = IStream_Stat( stm, &stat, STATFLAG_DEFAULT );
    ok (r == STG_E_REVERTED, "stat should fail\n");

855
    r = IStream_Write( stm, "Test string", strlen("Test string"), NULL);
856
    ok (r == STG_E_REVERTED, "IStream_Write should return STG_E_REVERTED instead of 0x%08x\n", r);
857 858

    r = IStream_Read( stm, buffer, sizeof(buffer), NULL);
859
    ok (r == STG_E_REVERTED, "IStream_Read should return STG_E_REVERTED instead of 0x%08x\n", r);
860

861 862 863
    r = IStream_Release(stm);
    ok (r == 0, "stream not released\n");

864 865 866
    /* tests that STGM_PRIORITY doesn't prevent readwrite access from other
     * StgOpenStorage calls in transacted mode */
    r = StgOpenStorage( filename, NULL, STGM_PRIORITY, NULL, 0, &stgprio);
867
    ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);
868 869 870 871

    todo_wine {
    /* non-transacted mode read/write fails */
    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg);
872
    ok(r==STG_E_LOCKVIOLATION, "StgOpenStorage should return STG_E_LOCKVIOLATION instead of 0x%08x\n", r);
873 874 875 876
    }

    /* non-transacted mode read-only succeeds */
    r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_WRITE|STGM_READ, NULL, 0, &stg);
877
    ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);
878
    IStorage_Release(stg);
879

880
    r = StgOpenStorage( filename, NULL, STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE, NULL, 0, &stg);
881
    ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);
882 883
    if(stg)
    {
884 885 886 887 888
        static const WCHAR stgname[] = { ' ',' ',' ','2','9',0 };
        static const WCHAR stgname2[] = { 'C','V','_','i','e','w',0 };
        static const WCHAR stmname2[] = { 'V','a','r','2','D','a','t','a',0 };
        IStorage *stg2;
        IStorage *stg3;
889 890 891
        STATSTG statstg;

        r = IStorage_Stat( stg, &statstg, STATFLAG_NONAME );
892
        ok(r == S_OK, "Stat should have succeeded instead of returning 0x%08x\n", r);
893 894 895
        ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %d\n", statstg.type);
        ok(U(statstg.cbSize).LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %d\n", U(statstg.cbSize).LowPart);
        ok(U(statstg.cbSize).HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %d\n", U(statstg.cbSize).HighPart);
896
        ok(statstg.grfMode == (STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_READWRITE),
897 898
            "Statstg grfMode should have been 0x10022 instead of 0x%x\n", statstg.grfMode);
        ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %d\n", statstg.grfLocksSupported);
899
        ok(IsEqualCLSID(&statstg.clsid, &test_stg_cls), "Statstg clsid is not test_stg_cls\n");
900 901
        ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %d\n", statstg.grfStateBits);
        ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %d\n", statstg.reserved);
902 903

        r = IStorage_CreateStorage( stg, stgname, STGM_SHARE_EXCLUSIVE, 0, 0, &stg2 );
904
        ok(r == S_OK, "CreateStorage should have succeeded instead of returning 0x%08x\n", r);
905

906
        r = IStorage_Stat( stg2, &statstg, STATFLAG_DEFAULT );
907
        ok(r == S_OK, "Stat should have succeeded instead of returning 0x%08x\n", r);
908
        ok(!memcmp(statstg.pwcsName, stgname, sizeof(stgname)),
909
            "Statstg pwcsName should have been the name the storage was created with\n");
910 911 912
        ok(statstg.type == STGTY_STORAGE, "Statstg type should have been STGTY_STORAGE instead of %d\n", statstg.type);
        ok(U(statstg.cbSize).LowPart == 0, "Statstg cbSize.LowPart should have been 0 instead of %d\n", U(statstg.cbSize).LowPart);
        ok(U(statstg.cbSize).HighPart == 0, "Statstg cbSize.HighPart should have been 0 instead of %d\n", U(statstg.cbSize).HighPart);
913
        ok(statstg.grfMode == STGM_SHARE_EXCLUSIVE,
914 915
            "Statstg grfMode should have been STGM_SHARE_EXCLUSIVE instead of 0x%x\n", statstg.grfMode);
        ok(statstg.grfLocksSupported == 0, "Statstg grfLocksSupported should have been 0 instead of %d\n", statstg.grfLocksSupported);
916
        ok(IsEqualCLSID(&statstg.clsid, &CLSID_NULL), "Statstg clsid is not CLSID_NULL\n");
917 918
        ok(statstg.grfStateBits == 0, "Statstg grfStateBits should have been 0 instead of %d\n", statstg.grfStateBits);
        ok(statstg.reserved == 0, "Statstg reserved should have been 0 instead of %d\n", statstg.reserved);
919 920
        CoTaskMemFree(statstg.pwcsName);

921
        r = IStorage_CreateStorage( stg2, stgname2, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, 0, &stg3 );
922
        ok(r == STG_E_ACCESSDENIED, "CreateStorage should have returned STG_E_ACCESSDENIED instead of 0x%08x\n", r);
923 924

        r = IStorage_CreateStream( stg2, stmname2, STGM_CREATE|STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
925
        ok(r == STG_E_ACCESSDENIED, "CreateStream should have returned STG_E_ACCESSDENIED instead of 0x%08x\n", r);
926 927

        IStorage_Release(stg2);
928 929 930 931

        r = IStorage_Release(stg);
        ok(r == 0, "wrong ref count\n");
    }
932
    IStorage_Release(stgprio);
933

934
    DeleteFileA(filenameA);
935 936
}

937 938 939 940 941 942
static void test_writeclassstg(void)
{
    IStorage *stg = NULL;
    HRESULT r;
    CLSID temp_cls;

943
    DeleteFileA(filenameA);
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    r = ReadClassStg( NULL, NULL );
    ok(r == E_INVALIDARG, "ReadClassStg should return E_INVALIDARG instead of 0x%08X\n", r);

    r = ReadClassStg( stg, NULL );
    ok(r == E_INVALIDARG, "ReadClassStg should return E_INVALIDARG instead of 0x%08X\n", r);

    temp_cls.Data1 = 0xdeadbeef;
    r = ReadClassStg( stg, &temp_cls );
    ok(r == S_OK, "ReadClassStg failed with 0x%08X\n", r);

    ok(IsEqualCLSID(&temp_cls, &CLSID_NULL), "ReadClassStg returned wrong clsid\n");

    r = WriteClassStg( NULL, NULL );
    ok(r == E_INVALIDARG, "WriteClassStg should return E_INVALIDARG instead of 0x%08X\n", r);

    r = WriteClassStg( stg, NULL );
    ok(r == STG_E_INVALIDPOINTER, "WriteClassStg should return STG_E_INVALIDPOINTER instead of 0x%08X\n", r);

    r = WriteClassStg( stg, &test_stg_cls );
    ok( r == S_OK, "WriteClassStg failed with 0x%08X\n", r);

    r = ReadClassStg( stg, &temp_cls );
    ok( r == S_OK, "ReadClassStg failed with 0x%08X\n", r);
    ok(IsEqualCLSID(&temp_cls, &test_stg_cls), "ReadClassStg returned wrong clsid\n");

    r = IStorage_Release( stg );
    ok (r == 0, "storage not released\n");

978
    DeleteFileA(filenameA);
979 980
}

981 982 983 984 985 986
static void test_streamenum(void)
{
    IStorage *stg = NULL;
    HRESULT r;
    IStream *stm = NULL;
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
987 988
    static const WCHAR stmname2[] = { 'A','B','C','D','E','F','G','H','I',0 };
    static const WCHAR stmname3[] = { 'A','B','C','D','E','F','G','H','I','J',0 };
989 990 991 992
    STATSTG stat;
    IEnumSTATSTG *ee = NULL;
    ULONG count;

993
    DeleteFileA(filenameA);
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    r = WriteClassStg( stg, &test_stg_cls );
    ok( r == S_OK, "WriteClassStg failed\n");

    r = IStorage_Commit( stg, STGC_DEFAULT );
    ok( r == S_OK, "IStorage_Commit failed\n");

    /* now create a stream */
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Release(stm);

    /* first enum ... should be 1 stream */
    r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
    ok(r==S_OK, "IStorage->EnumElements failed\n");

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
    ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
    ok(count == 1, "count wrong\n");

1021 1022 1023
    if (r == S_OK)
        CoTaskMemFree(stat.pwcsName);

1024
    r = IEnumSTATSTG_Release(ee);
1025
    ok(r==S_OK, "EnumSTATSTG_Release failed with error 0x%08x\n", r);
1026 1027 1028 1029 1030 1031

    /* second enum... destroy the stream before reading */
    r = IStorage_EnumElements(stg, 0, NULL, 0, &ee);
    ok(r==S_OK, "IStorage->EnumElements failed\n");

    r = IStorage_DestroyElement(stg, stmname);
1032
    ok(r==S_OK, "IStorage->DestroyElement failed\n");
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
    ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
    ok(count == 0, "count wrong\n");

    /* reset and try again */
    r = IEnumSTATSTG_Reset(ee);
    ok(r==S_OK, "IEnumSTATSTG->Reset failed\n");

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
    ok(r==S_FALSE, "IEnumSTATSTG->Next failed\n");
    ok(count == 0, "count wrong\n");

1048 1049 1050 1051 1052 1053 1054 1055
    /* add a stream before reading */
    r = IEnumSTATSTG_Reset(ee);
    ok(r==S_OK, "IEnumSTATSTG->Reset failed\n");

    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Release(stm);
1056
    ok(r==S_OK, "Stream_Release failed with error 0x%08x\n", r);
1057 1058 1059

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
1060 1061
    ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
    ok(count == 1, "count wrong\n");
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

    if (r == S_OK)
    {
        ok(lstrcmpiW(stat.pwcsName, stmname) == 0, "expected CONTENTS, got %s\n", wine_dbgstr_w(stat.pwcsName));
        CoTaskMemFree(stat.pwcsName);
    }

    r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Release(stm);
1073
    ok(r==S_OK, "Stream_Release failed with error 0x%08x\n", r);
1074 1075 1076

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
1077 1078
    ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
    ok(count == 1, "count wrong\n");
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

    if (r == S_OK)
    {
        ok(lstrcmpiW(stat.pwcsName, stmname2) == 0, "expected ABCDEFGHI, got %s\n", wine_dbgstr_w(stat.pwcsName));
        CoTaskMemFree(stat.pwcsName);
    }

    /* delete previous and next stream after reading */
    r = IStorage_CreateStream(stg, stmname3, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Release(stm);
1091
    ok(r==S_OK, "Stream_Release failed with error 0x%08x\n", r);
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

    r = IEnumSTATSTG_Reset(ee);
    ok(r==S_OK, "IEnumSTATSTG->Reset failed\n");

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
    ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
    ok(count == 1, "count wrong\n");

    if (r == S_OK)
    {
        ok(lstrcmpiW(stat.pwcsName, stmname) == 0, "expected CONTENTS, got %s\n", wine_dbgstr_w(stat.pwcsName));
        CoTaskMemFree(stat.pwcsName);
    }

    r = IStorage_DestroyElement(stg, stmname);
    ok(r==S_OK, "IStorage->DestroyElement failed\n");

    r = IStorage_DestroyElement(stg, stmname2);
    ok(r==S_OK, "IStorage->DestroyElement failed\n");

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
    ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
    ok(count == 1, "count wrong\n");

    if (r == S_OK)
    {
1120
        ok(lstrcmpiW(stat.pwcsName, stmname3) == 0, "expected ABCDEFGHIJ, got %s\n", wine_dbgstr_w(stat.pwcsName));
1121 1122
        CoTaskMemFree(stat.pwcsName);
    }
1123 1124

    r = IStorage_Release( stg );
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    todo_wine ok (r == 0, "storage not released\n");

    /* enumerator is still valid and working after the storage is released */
    r = IEnumSTATSTG_Reset(ee);
    ok(r==S_OK, "IEnumSTATSTG->Reset failed\n");

    count = 0xf00;
    r = IEnumSTATSTG_Next(ee, 1, &stat, &count);
    ok(r==S_OK, "IEnumSTATSTG->Next failed\n");
    ok(count == 1, "count wrong\n");

    if (r == S_OK)
    {
        ok(lstrcmpiW(stat.pwcsName, stmname3) == 0, "expected ABCDEFGHIJ, got %s\n", wine_dbgstr_w(stat.pwcsName));
        CoTaskMemFree(stat.pwcsName);
    }

    /* the storage is left open until the enumerator is freed */
    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE |STGM_TRANSACTED, NULL, 0, &stg);
1145 1146 1147
    ok(r==STG_E_SHAREVIOLATION ||
       r==STG_E_LOCKVIOLATION, /* XP-SP2/W2K3-SP1 and below */
       "StgCreateDocfile failed, res=%x\n", r);
1148 1149 1150

    r = IEnumSTATSTG_Release(ee);
    ok (r == 0, "enum not released\n");
1151

1152
    DeleteFileA(filenameA);
1153 1154
}

1155 1156
static void test_transact(void)
{
1157
    IStorage *stg = NULL, *stg2 = NULL, *stg3 = NULL;
1158 1159 1160
    HRESULT r;
    IStream *stm = NULL;
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
1161
    static const WCHAR stmname2[] = { 'F','O','O',0 };
1162 1163
    static const WCHAR stgname[] = { 'P','E','R','M','S','T','G',0 };
    static const WCHAR stgname2[] = { 'T','E','M','P','S','T','G',0 };
1164

1165
    DeleteFileA(filenameA);
1166 1167 1168 1169 1170 1171

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | 
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

1172
    /* commit a new stream and storage */
1173 1174 1175 1176 1177 1178
    r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Write(stm, "this is stream 1\n", 16, NULL);
    ok(r==S_OK, "IStream->Write failed\n");

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    IStream_Release(stm);

    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        /* Create two substorages but only commit one */
        r = IStorage_CreateStorage(stg2, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
        ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

        if (r == S_OK)
            IStorage_Release(stg3);

        r = IStorage_Commit(stg, 0);
        ok(r==S_OK, "IStorage->Commit failed\n");
1195

1196 1197
        r = IStorage_CreateStorage(stg2, stgname2, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
        ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);
1198

1199 1200 1201 1202 1203 1204 1205
        if (r == S_OK)
            IStorage_Release(stg3);

        IStorage_Release(stg2);
    }

    /* now create a stream and storage, but don't commit them */
1206
    stm = NULL;
1207 1208 1209
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

1210 1211 1212
    r = IStream_Write(stm, "this is stream 2\n", 16, NULL);
    ok(r==S_OK, "IStream->Write failed\n");

1213
    /* IStream::Commit does nothing for OLE storage streams */
1214 1215 1216
    r = IStream_Commit(stm, STGC_ONLYIFCURRENT | STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE);
    ok(r==S_OK, "IStream->Commit failed\n");

1217 1218 1219 1220 1221 1222 1223
    r = IStorage_CreateStorage(stg, stgname2, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    if (r == S_OK)
        IStorage_Release(stg2);

    IStream_Release(stm);
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

    IStorage_Release(stg);

    stm = NULL;
    stg = NULL;
    r = StgOpenStorage( filename, NULL, STGM_SHARE_DENY_NONE | STGM_READ | STGM_TRANSACTED, NULL, 0, &stg);
    ok(r==S_OK, "StgOpenStorage failed\n");

    if (!stg)
        return;

    r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_DENY_NONE|STGM_READ, 0, &stm );
1236
    ok(r==STG_E_INVALIDFLAG, "IStorage->OpenStream failed %08x\n", r);
1237 1238

    r = IStorage_OpenStream(stg, stmname, NULL, STGM_DELETEONRELEASE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1239
    ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
1240 1241

    r = IStorage_OpenStream(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1242
    ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);
1243

1244 1245 1246
    r = IStorage_OpenStorage(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);

1247
    r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1248
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream should fail %08x\n", r);
1249 1250 1251 1252 1253 1254 1255
    if (r == S_OK)
        IStream_Release(stm);

    r = IStorage_OpenStorage(stg, stgname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStorage should fail %08x\n", r);
    if (r == S_OK)
        IStorage_Release(stg2);
1256

1257 1258 1259 1260
    r = IStorage_OpenStorage(stg, stmname2, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);

    r = IStorage_OpenStream(stg, stmname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    ok(r==S_OK, "IStorage->OpenStream should succeed %08x\n", r);
    if (r == S_OK)
        IStream_Release(stm);

    r = IStorage_OpenStorage(stg, stgname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==S_OK, "IStorage->OpenStorage should succeed %08x\n", r);
    if (r == S_OK)
    {
        r = IStorage_OpenStorage(stg2, stgname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg3 );
        ok(r==S_OK, "IStorage->OpenStorage should succeed %08x\n", r);
        if (r == S_OK)
            IStorage_Release(stg3);

        r = IStorage_OpenStorage(stg2, stgname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg3 );
1275
        ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStorage should fail %08x\n", r);
1276 1277 1278 1279 1280
        if (r == S_OK)
            IStorage_Release(stg3);

        IStorage_Release(stg2);
    }
1281

1282 1283
    IStorage_Release(stg);

1284
    r = DeleteFileA(filenameA);
1285 1286 1287
    ok( r == TRUE, "deleted file\n");
}

1288 1289 1290 1291 1292 1293 1294
static void test_substorage_share(void)
{
    IStorage *stg, *stg2, *stg3;
    IStream *stm, *stm2;
    HRESULT r;
    static const WCHAR stgname[] = { 'P','E','R','M','S','T','G',0 };
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
1295
    static const WCHAR othername[] = { 'N','E','W','N','A','M','E',0 };
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

    DeleteFileA(filenameA);

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* create a read/write storage and try to open it again */
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        r = IStorage_OpenStorage(stg, stgname, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
1311
        ok(r==STG_E_ACCESSDENIED, "IStorage->OpenStorage should fail %08x\n", r);
1312 1313 1314 1315 1316

        if (r == S_OK)
            IStorage_Release(stg3);

        r = IStorage_OpenStorage(stg, stgname, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
1317
        ok(r==STG_E_ACCESSDENIED, "IStorage->OpenStorage should fail %08x\n", r);
1318 1319 1320 1321

        if (r == S_OK)
            IStorage_Release(stg3);

1322 1323
        /* cannot rename the storage while it's open */
        r = IStorage_RenameElement(stg, stgname, othername);
1324
        ok(r==STG_E_ACCESSDENIED, "IStorage->RenameElement should fail %08x\n", r);
1325 1326
        if (SUCCEEDED(r)) IStorage_RenameElement(stg, othername, stgname);

1327 1328 1329 1330 1331
        /* destroying an object while it's open invalidates it */
        r = IStorage_DestroyElement(stg, stgname);
        ok(r==S_OK, "IStorage->DestroyElement failed, hr=%08x\n", r);

        r = IStorage_CreateStream(stg2, stmname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
1332
        ok(r==STG_E_REVERTED, "IStorage->CreateStream failed, hr=%08x\n", r);
1333 1334

        if (r == S_OK)
1335
            IStream_Release(stm);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

        IStorage_Release(stg2);
    }

    /* create a read/write stream and try to open it again */
    r = IStorage_CreateStream(stg, stmname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
    ok(r==S_OK, "IStorage->CreateStream failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        r = IStorage_OpenStream(stg, stmname, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stm2);
1347
        ok(r==STG_E_ACCESSDENIED, "IStorage->OpenStream should fail %08x\n", r);
1348 1349

        if (r == S_OK)
1350
            IStream_Release(stm2);
1351 1352

        r = IStorage_OpenStream(stg, stmname, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm2);
1353
        ok(r==STG_E_ACCESSDENIED, "IStorage->OpenStream should fail %08x\n", r);
1354 1355

        if (r == S_OK)
1356
            IStream_Release(stm2);
1357

1358 1359
        /* cannot rename the stream while it's open */
        r = IStorage_RenameElement(stg, stmname, othername);
1360
        ok(r==STG_E_ACCESSDENIED, "IStorage->RenameElement should fail %08x\n", r);
1361 1362
        if (SUCCEEDED(r)) IStorage_RenameElement(stg, othername, stmname);

1363 1364 1365 1366 1367
        /* destroying an object while it's open invalidates it */
        r = IStorage_DestroyElement(stg, stmname);
        ok(r==S_OK, "IStorage->DestroyElement failed, hr=%08x\n", r);

        r = IStream_Write(stm, "this shouldn't work\n", 20, NULL);
1368
        ok(r==STG_E_REVERTED, "IStream_Write should fail %08x\n", r);
1369

1370
        IStream_Release(stm);
1371 1372 1373 1374 1375 1376 1377 1378
    }

    IStorage_Release(stg);

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
static void test_revert(void)
{
    IStorage *stg = NULL, *stg2 = NULL, *stg3 = NULL;
    HRESULT r;
    IStream *stm = NULL, *stm2 = NULL;
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
    static const WCHAR stmname2[] = { 'F','O','O',0 };
    static const WCHAR stgname[] = { 'P','E','R','M','S','T','G',0 };
    static const WCHAR stgname2[] = { 'T','E','M','P','S','T','G',0 };
    STATSTG statstg;

    DeleteFileA(filenameA);

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* commit a new stream and storage */
    r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Write(stm, "this is stream 1\n", 16, NULL);
    ok(r==S_OK, "IStream->Write failed\n");

    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        /* Create two substorages but only commit one */
        r = IStorage_CreateStorage(stg2, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
        ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

        if (r == S_OK)
            IStorage_Release(stg3);

        r = IStorage_Commit(stg, 0);
        ok(r==S_OK, "IStorage->Commit failed\n");

        r = IStorage_CreateStorage(stg2, stgname2, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
        ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

        if (r == S_OK)
            IStorage_Release(stg3);
    }

    /* now create a stream and storage, then revert */
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm2 );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStream_Write(stm2, "this is stream 2\n", 16, NULL);
    ok(r==S_OK, "IStream->Write failed\n");

    r = IStorage_CreateStorage(stg, stgname2, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    r = IStorage_Revert(stg);
1437
    ok(r==S_OK, "Storage_Revert failed with error 0x%08x\n", r);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

    /* all open objects become invalid */
    r = IStream_Write(stm, "this shouldn't work\n", 20, NULL);
    ok(r==STG_E_REVERTED, "IStream_Write should fail %08x\n", r);

    r = IStream_Write(stm2, "this shouldn't work\n", 20, NULL);
    ok(r==STG_E_REVERTED, "IStream_Write should fail %08x\n", r);

    r = IStorage_Stat(stg2, &statstg, STATFLAG_NONAME);
    ok(r==STG_E_REVERTED, "IStorage_Stat should fail %08x\n", r);

    r = IStorage_Stat(stg3, &statstg, STATFLAG_NONAME);
    ok(r==STG_E_REVERTED, "IStorage_Stat should fail %08x\n", r);

    IStream_Release(stm);
    IStream_Release(stm2);
    IStorage_Release(stg2);
    IStorage_Release(stg3);

    r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_DENY_NONE|STGM_READ, 0, &stm );
    ok(r==STG_E_INVALIDFLAG, "IStorage->OpenStream failed %08x\n", r);

    r = IStorage_OpenStream(stg, stmname, NULL, STGM_DELETEONRELEASE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
    ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);

    r = IStorage_OpenStream(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
    ok(r==STG_E_INVALIDFUNCTION, "IStorage->OpenStream failed %08x\n", r);

    r = IStorage_OpenStorage(stg, stmname, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);

    r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream should fail %08x\n", r);
    if (r == S_OK)
        IStream_Release(stm);

    r = IStorage_OpenStorage(stg, stgname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStorage should fail %08x\n", r);
    if (r == S_OK)
        IStorage_Release(stg2);

    r = IStorage_OpenStorage(stg, stmname2, NULL, STGM_TRANSACTED|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream failed %08x\n", r);

    r = IStorage_OpenStream(stg, stmname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
    ok(r==S_OK, "IStorage->OpenStream should succeed %08x\n", r);
    if (r == S_OK)
        IStream_Release(stm);

    r = IStorage_OpenStorage(stg, stgname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg2 );
    ok(r==S_OK, "IStorage->OpenStorage should succeed %08x\n", r);
    if (r == S_OK)
    {
        r = IStorage_OpenStorage(stg2, stgname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg3 );
        ok(r==S_OK, "IStorage->OpenStorage should succeed %08x\n", r);
        if (r == S_OK)
            IStorage_Release(stg3);

        r = IStorage_OpenStorage(stg2, stgname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg3 );
1497
        ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStorage should fail %08x\n", r);
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
        if (r == S_OK)
            IStorage_Release(stg3);

        IStorage_Release(stg2);
    }

    IStorage_Release(stg);

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");

    /* Revert only invalidates objects in transacted mode */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
    ok(r==S_OK, "IStorage->CreateStream failed\n");

    r = IStorage_Revert(stg);
1518
    ok(r==S_OK, "IStorage->Revert failed %08x\n", r);
1519 1520 1521 1522 1523

    r = IStream_Write(stm, "this works\n", 11, NULL);
    ok(r==S_OK, "IStream_Write should succeed %08x\n", r);

    IStream_Release(stm);
1524
    IStorage_Release(stg);
1525 1526 1527 1528 1529

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

1530 1531
static void test_parent_free(void)
{
1532
    IStorage *stg = NULL, *stg2 = NULL, *stg3 = NULL;
1533 1534 1535 1536 1537
    HRESULT r;
    IStream *stm = NULL;
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
    static const WCHAR stgname[] = { 'P','E','R','M','S','T','G',0 };
    ULONG ref;
1538
    STATSTG statstg;
1539 1540 1541 1542 1543 1544 1545 1546

    DeleteFileA(filenameA);

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

1547
    /* create a new storage */
1548 1549 1550 1551 1552 1553 1554 1555 1556
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        /* now create a stream inside the new storage */
        r = IStorage_CreateStream(stg2, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
        ok(r==S_OK, "IStorage->CreateStream failed\n");

1557 1558 1559 1560 1561 1562 1563 1564
        if (r == S_OK)
        {
            /* create a storage inside the new storage */
            r = IStorage_CreateStorage(stg2, stgname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stg3 );
            ok(r==S_OK, "IStorage->CreateStorage failed\n");
        }

        /* free the parent */
1565 1566 1567
        ref = IStorage_Release(stg2);
        ok(ref == 0, "IStorage still has %u references\n", ref);

1568
        /* child objects are invalid */
1569 1570 1571
        if (r == S_OK)
        {
            r = IStream_Write(stm, "this should fail\n", 17, NULL);
1572
            ok(r==STG_E_REVERTED, "IStream->Write should fail, hr=%x\n", r);
1573 1574

            IStream_Release(stm);
1575 1576

            r = IStorage_Stat(stg3, &statstg, STATFLAG_NONAME);
1577
            ok(r==STG_E_REVERTED, "IStorage_Stat should fail %08x\n", r);
1578 1579

            r = IStorage_SetStateBits(stg3, 1, 1);
1580
            ok(r==STG_E_REVERTED, "IStorage_Stat should fail %08x\n", r);
1581 1582

            IStorage_Release(stg3);
1583 1584 1585 1586 1587 1588 1589 1590 1591
        }
    }

    IStorage_Release(stg);

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

1592 1593
static void test_nonroot_transacted(void)
{
1594
    IStorage *stg = NULL, *stg2 = NULL, *stg3 = NULL;
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    HRESULT r;
    IStream *stm = NULL;
    static const WCHAR stgname[] = { 'P','E','R','M','S','T','G',0 };
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
    static const WCHAR stmname2[] = { 'F','O','O',0 };

    DeleteFileA(filenameA);

    /* create a transacted file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* create a transacted substorage */
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        /* create and commit stmname */
        r = IStorage_CreateStream(stg2, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
        ok(r==S_OK, "IStorage->CreateStream failed\n");
        if (r == S_OK)
            IStream_Release(stm);

        IStorage_Commit(stg2, 0);

        /* create and revert stmname2 */
        r = IStorage_CreateStream(stg2, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
        ok(r==S_OK, "IStorage->CreateStream failed\n");
        if (r == S_OK)
            IStream_Release(stm);

        IStorage_Revert(stg2);

        /* check that Commit and Revert really worked */
        r = IStorage_OpenStream(stg2, stmname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
        ok(r==S_OK, "IStorage->OpenStream should succeed %08x\n", r);
        if (r == S_OK)
            IStream_Release(stm);

        r = IStorage_OpenStream(stg2, stmname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1637
        ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream should fail %08x\n", r);
1638 1639 1640 1641 1642 1643
        if (r == S_OK)
            IStream_Release(stm);

        IStorage_Release(stg2);
    }

1644 1645 1646 1647 1648 1649 1650 1651
    /* create a read-only transacted substorage */
    r = IStorage_OpenStorage(stg, stgname, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, NULL, 0, &stg2);
    ok(r==S_OK, "IStorage->OpenStorage failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        /* The storage can be modified. */
        r = IStorage_CreateStorage(stg2, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
1652
        ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);
1653
        if (r == S_OK)
1654
            IStorage_Release(stg3);
1655 1656 1657

        /* But changes cannot be committed. */
        r = IStorage_Commit(stg2, 0);
1658
        ok(r==STG_E_ACCESSDENIED, "IStorage->Commit should fail, hr=%08x\n", r);
1659 1660 1661 1662 1663

        IStorage_Release(stg2);
    }

    IStorage_Release(stg);
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698

    /* create a non-transacted file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* create a transacted substorage */
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    if (r == S_OK)
    {
        /* create and commit stmname */
        r = IStorage_CreateStream(stg2, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
        ok(r==S_OK, "IStorage->CreateStream failed\n");
        if (r == S_OK)
            IStream_Release(stm);

        IStorage_Commit(stg2, 0);

        /* create and revert stmname2 */
        r = IStorage_CreateStream(stg2, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm );
        ok(r==S_OK, "IStorage->CreateStream failed\n");
        if (r == S_OK)
            IStream_Release(stm);

        IStorage_Revert(stg2);

        /* check that Commit and Revert really worked */
        r = IStorage_OpenStream(stg2, stmname, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
        ok(r==S_OK, "IStorage->OpenStream should succeed %08x\n", r);
        if (r == S_OK)
            IStream_Release(stm);

        r = IStorage_OpenStream(stg2, stmname2, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &stm );
1699
        ok(r==STG_E_FILENOTFOUND, "IStorage->OpenStream should fail %08x\n", r);
1700 1701 1702 1703 1704 1705
        if (r == S_OK)
            IStream_Release(stm);

        IStorage_Release(stg2);
    }

1706
    IStorage_Release(stg);
1707 1708 1709 1710 1711

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
static void test_ReadClassStm(void)
{
    CLSID clsid;
    HRESULT hr;
    IStream *pStream;
    static const LARGE_INTEGER llZero;

    hr = ReadClassStm(NULL, &clsid);
    ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);

    hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    ok_ole_success(hr, "CreateStreamOnHGlobal");
    hr = WriteClassStm(pStream, &test_stg_cls);
    ok_ole_success(hr, "WriteClassStm");

    hr = ReadClassStm(pStream, NULL);
    ok(hr == E_INVALIDARG, "ReadClassStm should have returned E_INVALIDARG instead of 0x%08x\n", hr);

    /* test not rewound stream */
    hr = ReadClassStm(pStream, &clsid);
    ok(hr == STG_E_READFAULT, "ReadClassStm should have returned STG_E_READFAULT instead of 0x%08x\n", hr);
    ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid should have been zeroed\n");

    hr = IStream_Seek(pStream, llZero, STREAM_SEEK_SET, NULL);
    ok_ole_success(hr, "IStream_Seek");
    hr = ReadClassStm(pStream, &clsid);
    ok_ole_success(hr, "ReadClassStm");
    ok(IsEqualCLSID(&clsid, &test_stg_cls), "clsid should have been set to CLSID_WineTest\n");
1740 1741

    IStream_Release(pStream);
1742 1743
}

1744 1745 1746 1747
struct access_res
{
    BOOL gothandle;
    DWORD lasterr;
1748
    BOOL ignore;
1749 1750
};

1751
static const struct access_res create[16] =
1752
{
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
    { TRUE, ERROR_SUCCESS, TRUE },
    { TRUE, ERROR_SUCCESS, TRUE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { TRUE, ERROR_SUCCESS, TRUE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { TRUE, ERROR_SUCCESS, TRUE }
1769 1770
};

1771
static const struct access_res create_commit[16] =
1772
{
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
    { TRUE, ERROR_SUCCESS, TRUE },
    { TRUE, ERROR_SUCCESS, TRUE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { TRUE, ERROR_SUCCESS, TRUE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { FALSE, ERROR_SHARING_VIOLATION, FALSE },
    { TRUE, ERROR_SUCCESS, TRUE }
1789 1790
};

1791
static const struct access_res create_close[16] =
1792
{
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
    { TRUE, ERROR_SUCCESS, FALSE },
1808
    { TRUE, ERROR_SUCCESS }
1809 1810
};

1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
static const DWORD access_modes[4] = {
    0,
    GENERIC_READ,
    GENERIC_WRITE,
    GENERIC_READ | GENERIC_WRITE
};

static const DWORD share_modes[4] = {
    0,
    FILE_SHARE_READ,
    FILE_SHARE_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE
};

1825
static void _test_file_access(LPCSTR file, const struct access_res *ares, DWORD line)
1826 1827 1828
{
    int i, j, idx = 0;

1829
    for (i = 0; i < sizeof(access_modes)/sizeof(access_modes[0]); i++)
1830
    {
1831
        for (j = 0; j < sizeof(share_modes)/sizeof(share_modes[0]); j++)
1832
        {
1833 1834 1835
            DWORD lasterr;
            HANDLE hfile;

1836 1837 1838
            if (ares[idx].ignore)
                continue;

1839
            SetLastError(0xdeadbeef);
1840
            hfile = CreateFileA(file, access_modes[i], share_modes[j], NULL, OPEN_EXISTING,
1841 1842
                                FILE_ATTRIBUTE_NORMAL, 0);
            lasterr = GetLastError();
1843 1844 1845 1846 1847 1848

            ok((hfile != INVALID_HANDLE_VALUE) == ares[idx].gothandle,
               "(%d, handle, %d): Expected %d, got %d\n",
               line, idx, ares[idx].gothandle,
               (hfile != INVALID_HANDLE_VALUE));

1849 1850
            ok(lasterr == ares[idx].lasterr ||
               broken(lasterr == 0xdeadbeef) /* win9x */,
1851 1852
               "(%d, lasterr, %d): Expected %d, got %d\n",
               line, idx, ares[idx].lasterr, lasterr);
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863

            CloseHandle(hfile);
            idx++;
        }
    }
}

#define test_file_access(file, ares) _test_file_access(file, ares, __LINE__)

static void test_access(void)
{
1864 1865
    static const WCHAR fileW[] = {'w','i','n','e','t','e','s','t',0};
    static const char fileA[] = "winetest";
1866 1867 1868 1869 1870 1871 1872 1873
    IStorage *stg;
    HRESULT hr;

    /* STGM_TRANSACTED */
    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
                          STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, &stg);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1874
    test_file_access(fileA, create);
1875 1876 1877 1878

    hr = IStorage_Commit(stg, STGC_DEFAULT);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1879
    test_file_access(fileA, create_commit);
1880 1881 1882

    IStorage_Release(stg);

1883
    test_file_access(fileA, create_close);
1884

1885
    DeleteFileA(fileA);
1886 1887 1888 1889 1890 1891

    /* STGM_DIRECT */
    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
                          STGM_SHARE_EXCLUSIVE | STGM_DIRECT, 0, &stg);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1892
    test_file_access(fileA, create);
1893 1894 1895 1896

    hr = IStorage_Commit(stg, STGC_DEFAULT);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1897
    test_file_access(fileA, create_commit);
1898 1899 1900

    IStorage_Release(stg);

1901
    test_file_access(fileA, create_close);
1902

1903
    DeleteFileA(fileA);
1904

1905 1906 1907 1908 1909
    /* STGM_SHARE_DENY_NONE */
    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
                          STGM_SHARE_DENY_NONE | STGM_TRANSACTED, 0, &stg);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1910
    test_file_access(fileA, create);
1911 1912 1913 1914

    hr = IStorage_Commit(stg, STGC_DEFAULT);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1915
    test_file_access(fileA, create_commit);
1916 1917 1918

    IStorage_Release(stg);

1919
    test_file_access(fileA, create_close);
1920

1921
    DeleteFileA(fileA);
1922 1923 1924 1925 1926 1927

    /* STGM_SHARE_DENY_READ */
    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
                          STGM_SHARE_DENY_READ | STGM_TRANSACTED, 0, &stg);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1928
    test_file_access(fileA, create);
1929 1930 1931 1932

    hr = IStorage_Commit(stg, STGC_DEFAULT);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1933
    test_file_access(fileA, create_commit);
1934 1935 1936

    IStorage_Release(stg);

1937
    test_file_access(fileA, create_close);
1938

1939
    DeleteFileA(fileA);
1940 1941 1942 1943 1944 1945

    /* STGM_SHARE_DENY_WRITE */
    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE |
                          STGM_SHARE_DENY_WRITE | STGM_TRANSACTED, 0, &stg);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1946
    test_file_access(fileA, create);
1947 1948 1949 1950

    hr = IStorage_Commit(stg, STGC_DEFAULT);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

1951
    test_file_access(fileA, create_commit);
1952 1953 1954

    IStorage_Release(stg);

1955
    test_file_access(fileA, create_close);
1956

1957
    DeleteFileA(fileA);
1958 1959 1960 1961 1962 1963 1964

    /* STGM_DIRECT_SWMR | STGM_READ | STGM_SHARE_DENY_NONE - reader mode for direct SWMR mode */
    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE | STGM_SHARE_DENY_WRITE | STGM_TRANSACTED, 0, &stg);
    ok(hr == S_OK, "got %08x\n", hr);
    IStorage_Release(stg);

    hr = StgOpenStorage(fileW, NULL, STGM_DIRECT_SWMR | STGM_READ | STGM_SHARE_DENY_NONE, NULL, 0, &stg);
1965 1966 1967
    ok(hr == S_OK || broken(hr == STG_E_INVALIDFLAG), "got %08x\n", hr);
    if(hr != S_OK)
       return;
1968 1969 1970 1971 1972 1973 1974

    test_file_access(fileA, create);

    IStorage_Release(stg);
    test_file_access(fileA, create_close);

    DeleteFileA(fileA);
1975 1976
}

1977
static void test_readonly(void)
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
{
    IStorage *stg, *stg2, *stg3;
    IStream *stream;
    HRESULT hr;
    static const WCHAR fileW[] = {'w','i','n','e','t','e','s','t',0};
    static const WCHAR storageW[] = {'s','t','o','r','a','g','e',0};
    static const WCHAR streamW[] = {'s','t','r','e','a','m',0};

    hr = StgCreateDocfile( fileW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
    ok(hr == S_OK, "should succeed, res=%x\n", hr);
    if (SUCCEEDED(hr))
    {
        hr = IStorage_CreateStorage( stg, storageW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stg2 );
        ok(hr == S_OK, "should succeed, res=%x\n", hr);
        if (SUCCEEDED(hr))
        {
            hr = IStorage_CreateStream( stg2, streamW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stream );
            ok(hr == S_OK, "should succeed, res=%x\n", hr);
            if (SUCCEEDED(hr))
                IStream_Release(stream);
            IStorage_Release(stg2);
        }
        IStorage_Release(stg);
    }

    /* re-open read only */
    hr = StgOpenStorage( fileW, NULL, STGM_TRANSACTED | STGM_SHARE_DENY_NONE | STGM_READ, NULL, 0, &stg);
    ok(hr == S_OK, "should succeed, res=%x\n", hr);
    if (SUCCEEDED(hr))
    {
        hr = IStorage_OpenStorage( stg, storageW, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, NULL, 0, &stg2 );
        ok(hr == S_OK, "should succeed, res=%x\n", hr);
        if (SUCCEEDED(hr))
        {
            /* CreateStream on read-only storage, name exists */
            hr = IStorage_CreateStream( stg2, streamW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, 0, 0, &stream );
            ok(hr == STG_E_ACCESSDENIED, "should fail, res=%x\n", hr);
            if (SUCCEEDED(hr))
                IStream_Release(stream);

            /* CreateStream on read-only storage, name does not exist */
            hr = IStorage_CreateStream( stg2, storageW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, 0, 0, &stream );
            ok(hr == STG_E_ACCESSDENIED, "should fail, res=%x\n", hr);
            if (SUCCEEDED(hr))
                IStream_Release(stream);

            /* CreateStorage on read-only storage, name exists */
            hr = IStorage_CreateStorage( stg2, streamW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, 0, 0, &stg3 );
            ok(hr == STG_E_FILEALREADYEXISTS, "should fail, res=%x\n", hr);
            if (SUCCEEDED(hr))
2028
                IStorage_Release(stg3);
2029 2030 2031 2032 2033

            /* CreateStorage on read-only storage, name does not exist */
            hr = IStorage_CreateStorage( stg2, storageW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READ, 0, 0, &stg3 );
            ok(hr == STG_E_ACCESSDENIED, "should fail, res=%x\n", hr);
            if (SUCCEEDED(hr))
2034
                IStorage_Release(stg3);
2035 2036 2037

            /* DestroyElement on read-only storage, name exists */
            hr = IStorage_DestroyElement( stg2, streamW );
2038
            ok(hr == STG_E_ACCESSDENIED, "should fail, res=%x\n", hr);
2039 2040 2041

            /* DestroyElement on read-only storage, name does not exist */
            hr = IStorage_DestroyElement( stg2, storageW );
2042
            ok(hr == STG_E_ACCESSDENIED, "should fail, res=%x\n", hr);
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052

            IStorage_Release(stg2);
        }

        IStorage_Release(stg);
    }

    DeleteFileA("winetest");
}

2053 2054 2055 2056
static void test_simple(void)
{
    /* Tests for STGM_SIMPLE mode */

2057
    IStorage *stg, *stg2;
2058 2059
    HRESULT r;
    IStream *stm;
2060
    static const WCHAR stgname[] = { 'S','t','g',0 };
2061
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
2062
    static const WCHAR stmname2[] = { 'S','m','a','l','l',0 };
2063 2064 2065 2066
    LARGE_INTEGER pos;
    ULARGE_INTEGER upos;
    DWORD count;
    STATSTG stat;
2067

2068
    DeleteFileA(filenameA);
2069 2070 2071 2072

    r = StgCreateDocfile( filename, STGM_SIMPLE | STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
    ok(r == S_OK, "got %08x\n", r);

2073 2074 2075 2076
    r = IStorage_CreateStorage(stg, stgname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stg2);
    ok(r == STG_E_INVALIDFUNCTION, "got %08x\n", r);
    if (SUCCEEDED(r)) IStorage_Release(stg2);

2077 2078 2079 2080 2081
    r = IStorage_CreateStream(stg, stmname, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm);
    ok(r == STG_E_INVALIDFLAG, "got %08x\n", r);
    r = IStorage_CreateStream(stg, stmname, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm);
    ok(r == S_OK, "got %08x\n", r);

2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
    upos.QuadPart = 6000;
    r = IStream_SetSize(stm, upos);
    ok(r == S_OK, "got %08x\n", r);

    r = IStream_Write(stm, "foo", 3, &count);
    ok(r == S_OK, "got %08x\n", r);
    ok(count == 3, "got %d\n", count);

    pos.QuadPart = 0;
    r = IStream_Seek(stm, pos, STREAM_SEEK_CUR, &upos);
    ok(r == S_OK, "got %08x\n", r);
    ok(upos.QuadPart == 3, "got %d\n", upos.u.LowPart);

    r = IStream_Stat(stm, &stat, STATFLAG_NONAME);
2096 2097 2098 2099 2100
    ok(r == S_OK ||
       broken(r == STG_E_INVALIDFUNCTION), /* NT4 and below */
       "got %08x\n", r);
    if (r == S_OK)
        ok(stat.cbSize.QuadPart == 3, "got %d\n", stat.cbSize.u.LowPart);
2101 2102 2103 2104 2105 2106 2107

    pos.QuadPart = 1;
    r = IStream_Seek(stm, pos, STREAM_SEEK_SET, &upos);
    ok(r == S_OK, "got %08x\n", r);
    ok(upos.QuadPart == 1, "got %d\n", upos.u.LowPart);

    r = IStream_Stat(stm, &stat, STATFLAG_NONAME);
2108 2109 2110 2111 2112
    ok(r == S_OK ||
       broken(r == STG_E_INVALIDFUNCTION), /* NT4 and below */
       "got %08x\n", r);
    if (r == S_OK)
        ok(stat.cbSize.QuadPart == 1, "got %d\n", stat.cbSize.u.LowPart);
2113

2114 2115
    IStream_Release(stm);

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
    r = IStorage_CreateStream(stg, stmname2, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, 0, &stm);
    ok(r == S_OK, "got %08x\n", r);

    upos.QuadPart = 100;
    r = IStream_SetSize(stm, upos);
    ok(r == S_OK, "got %08x\n", r);

    r = IStream_Write(stm, "foo", 3, &count);
    ok(r == S_OK, "got %08x\n", r);
    ok(count == 3, "got %d\n", count);

    IStream_Release(stm);

2129 2130 2131
    IStorage_Commit(stg, STGC_DEFAULT);
    IStorage_Release(stg);

2132
    r = StgOpenStorage( filename, NULL, STGM_SIMPLE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, NULL, 0, &stg);
2133 2134 2135 2136 2137 2138
    if (r == STG_E_INVALIDFLAG)
    {
        win_skip("Flag combination is not supported on NT4 and below\n");
        DeleteFileA(filenameA);
        return;
    }
2139 2140
    ok(r == S_OK, "got %08x\n", r);

2141 2142 2143 2144
    r = IStorage_OpenStorage(stg, stgname, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, NULL, 0, &stg2);
    ok(r == STG_E_INVALIDFUNCTION, "got %08x\n", r);
    if (SUCCEEDED(r)) IStorage_Release(stg2);

2145 2146 2147 2148 2149 2150 2151 2152 2153
    r = IStorage_OpenStream(stg, stmname, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stm);
    ok(r == S_OK, "got %08x\n", r);

    r = IStream_Stat(stm, &stat, STATFLAG_NONAME);
    ok(r == S_OK, "got %08x\n", r);
    ok(stat.cbSize.QuadPart == 6000, "got %d\n", stat.cbSize.u.LowPart);

    IStream_Release(stm);

2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
    r = IStorage_OpenStream(stg, stmname2, NULL, STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stm);
    ok(r == S_OK, "got %08x\n", r);

    r = IStream_Stat(stm, &stat, STATFLAG_NONAME);
    ok(r == S_OK, "got %08x\n", r);
    ok(stat.cbSize.QuadPart == 4096, "got %d\n", stat.cbSize.u.LowPart);

    IStream_Release(stm);


2164 2165
    IStorage_Release(stg);

2166
    DeleteFileA(filenameA);
2167 2168
}

2169 2170 2171 2172 2173
static void test_fmtusertypestg(void)
{
    IStorage *stg;
    IEnumSTATSTG *stat;
    HRESULT hr;
2174
    static const char fileA[]  = {'f','m','t','t','e','s','t',0};
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
    static const WCHAR fileW[] = {'f','m','t','t','e','s','t',0};
    static WCHAR userTypeW[] = {'S','t','g','U','s','r','T','y','p','e',0};
    static WCHAR strmNameW[] = {1,'C','o','m','p','O','b','j',0};

    hr = StgCreateDocfile( fileW, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
    ok(hr == S_OK, "should succeed, res=%x\n", hr);

    if (SUCCEEDED(hr))
    {
        /* try to write the stream */
        hr = WriteFmtUserTypeStg(stg, 0, userTypeW);
        ok(hr == S_OK, "should succeed, res=%x\n", hr);

        /* check that the stream was created */
        hr = IStorage_EnumElements(stg, 0, NULL, 0, &stat);
        ok(hr == S_OK, "should succeed, res=%x\n", hr);
        if (SUCCEEDED(hr))
        {
            BOOL found = FALSE;
            STATSTG statstg;
            DWORD got;
            while ((hr = IEnumSTATSTG_Next(stat, 1, &statstg, &got)) == S_OK && got == 1)
            {
2198
                if (strcmp_ww(statstg.pwcsName, strmNameW) == 0)
2199 2200 2201
                    found = TRUE;
                else
                    ok(0, "found unexpected stream or storage\n");
2202
                CoTaskMemFree(statstg.pwcsName);
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
            }
            ok(found == TRUE, "expected storage to contain stream \\0001CompObj\n");
            IEnumSTATSTG_Release(stat);
        }

        /* re-write the stream */
        hr = WriteFmtUserTypeStg(stg, 0, userTypeW);
        ok(hr == S_OK, "should succeed, res=%x\n", hr);

        /* check that the stream is still there */
        hr = IStorage_EnumElements(stg, 0, NULL, 0, &stat);
        ok(hr == S_OK, "should succeed, res=%x\n", hr);
        if (SUCCEEDED(hr))
        {
            BOOL found = FALSE;
            STATSTG statstg;
            DWORD got;
            while ((hr = IEnumSTATSTG_Next(stat, 1, &statstg, &got)) == S_OK && got == 1)
            {
2222
                if (strcmp_ww(statstg.pwcsName, strmNameW) == 0)
2223 2224 2225
                    found = TRUE;
                else
                    ok(0, "found unexpected stream or storage\n");
2226
                CoTaskMemFree(statstg.pwcsName);
2227 2228 2229 2230 2231 2232
            }
            ok(found == TRUE, "expected storage to contain stream \\0001CompObj\n");
            IEnumSTATSTG_Release(stat);
        }

        IStorage_Release(stg);
2233
        DeleteFileA( fileA );
2234 2235 2236
    }
}

2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
static void test_references(void)
{
    IStorage *stg,*stg2;
    HRESULT hr;
    unsigned c1,c2;
    static const WCHAR StorName[] = { 'D','a','t','a','S','p','a','c','e','I','n','f','o',0 };

    DeleteFileA(filenameA);

    hr = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(hr==S_OK, "StgCreateDocfile failed\n");

    if (SUCCEEDED(hr))
    {
        IStorage_Release(stg);

        hr = StgOpenStorage( filename, NULL, STGM_TRANSACTED | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, NULL, 0, &stg);
        ok(hr==S_OK, "StgOpenStorage failed (result=%x)\n",hr);

        if (SUCCEEDED(hr))
        {
            hr = IStorage_CreateStorage(stg,StorName,STGM_READWRITE | STGM_SHARE_EXCLUSIVE,0,0,&stg2);
            ok(hr == S_OK, "IStorage_CreateStorage failed (result=%x)\n",hr);

            if (SUCCEEDED(hr))
            {
                c1 = IStorage_AddRef(stg);
                ok(c1 == 2, "creating internal storage added references to ancestor\n");
                c1 = IStorage_AddRef(stg);
                IStorage_Release(stg2);
                c2 = IStorage_AddRef(stg) - 1;
                ok(c1 == c2, "releasing internal storage removed references to ancestor\n");
            }
            c1 = IStorage_Release(stg);
            while ( c1 ) c1 = IStorage_Release(stg);
        }
    }
2274 2275

    DeleteFileA(filenameA);
2276 2277
}

2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
/* dest
 *  |-StorageA
 *  |  `StreamA: "StreamA"
 *  |-StorageB
 *  |  `StreamB: "StreamB"
 *  `StreamC: "StreamC"
 */
static HRESULT create_test_file(IStorage *dest)
{
    IStorage *stgA = NULL, *stgB = NULL;
    IStream *strmA = NULL, *strmB = NULL, *strmC = NULL;
    const ULONG strmA_name_size = lstrlenW(strmA_name) * sizeof(WCHAR);
    const ULONG strmB_name_size = lstrlenW(strmB_name) * sizeof(WCHAR);
    const ULONG strmC_name_size = lstrlenW(strmC_name) * sizeof(WCHAR);
    ULONG bytes;
    HRESULT hr;

    hr = IStorage_CreateStorage(dest, stgA_name, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stgA);
    ok(hr == S_OK, "IStorage_CreateStorage failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = IStorage_CreateStream(stgA, strmA_name, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &strmA);
    ok(hr == S_OK, "IStorage_CreateStream failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = IStream_Write(strmA, strmA_name, strmA_name_size, &bytes);
    ok(hr == S_OK && bytes == strmA_name_size, "IStream_Write failed: 0x%08x, %d of %d bytes written\n", hr, bytes, strmA_name_size);

    hr = IStorage_CreateStorage(dest, stgB_name, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stgB);
    ok(hr == S_OK, "IStorage_CreateStorage failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = IStorage_CreateStream(stgB, strmB_name, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &strmB);
    ok(hr == S_OK, "IStorage_CreateStream failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = IStream_Write(strmB, strmB_name, strmB_name_size, &bytes);
    ok(hr == S_OK && bytes == strmB_name_size, "IStream_Write failed: 0x%08x, %d of %d bytes written\n", hr, bytes, strmB_name_size);

    hr = IStorage_CreateStream(dest, strmC_name, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &strmC);
    ok(hr == S_OK, "IStorage_CreateStream failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = IStream_Write(strmC, strmC_name, strmC_name_size, &bytes);
    ok(hr == S_OK && bytes == strmC_name_size, "IStream_Write failed: 0x%08x, %d of %d bytes written\n", hr, bytes, strmC_name_size);

cleanup:
    if(strmC)
        IStream_Release(strmC);
    if(strmB)
        IStream_Release(strmB);
    if(stgB)
        IStorage_Release(stgB);
    if(strmA)
        IStream_Release(strmA);
    if(stgA)
        IStorage_Release(stgA);

    return hr;
}

static void test_copyto(void)
{
    IStorage *file1 = NULL, *file2 = NULL, *stg_tmp;
    IStream *strm_tmp;
    WCHAR buf[64];
    HRESULT hr;

    /* create & populate file1 */
    hr = StgCreateDocfile(file1_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file1);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = create_test_file(file1);
    if(FAILED(hr))
        goto cleanup;

    /* create file2 */
    hr = StgCreateDocfile(file2_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file2);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* copy file1 into file2 */
    hr = IStorage_CopyTo(file1, 0, NULL, NULL, NULL);
    ok(hr == STG_E_INVALIDPOINTER, "CopyTo should give STG_E_INVALIDPONITER, gave: 0x%08x\n", hr);

    hr = IStorage_CopyTo(file1, 0, NULL, NULL, file2);
    ok(hr == S_OK, "CopyTo failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* verify that all of file1 was copied */
    hr = IStorage_OpenStorage(file2, stgA_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == S_OK, "OpenStorage failed: 0x%08x\n", hr);

    if(SUCCEEDED(hr)){
        hr = IStorage_OpenStream(stg_tmp, strmA_name, NULL,
                STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
        ok(hr == S_OK, "OpenStream failed: 0x%08x\n", hr);

        if(SUCCEEDED(hr)){
            memset(buf, 0, sizeof(buf));
            hr = IStream_Read(strm_tmp, buf, sizeof(buf), NULL);
            ok(hr == S_OK, "Read failed: 0x%08x\n", hr);
            if(SUCCEEDED(hr))
2391
                ok(strcmp_ww(buf, strmA_name) == 0,
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
                        "Expected %s to be read, got %s\n", wine_dbgstr_w(strmA_name), wine_dbgstr_w(buf));

            IStream_Release(strm_tmp);
        }

        IStorage_Release(stg_tmp);
    }

    hr = IStorage_OpenStorage(file2, stgB_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == S_OK, "OpenStorage failed: 0x%08x\n", hr);

    if(SUCCEEDED(hr)){
        hr = IStorage_OpenStream(stg_tmp, strmB_name, NULL,
                STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
        ok(hr == S_OK, "OpenStream failed: 0x%08x\n", hr);

        if(SUCCEEDED(hr)){
            memset(buf, 0, sizeof(buf));
            hr = IStream_Read(strm_tmp, buf, sizeof(buf), NULL);
            ok(hr == S_OK, "Read failed: 0x%08x\n", hr);
            if(SUCCEEDED(hr))
2414
                ok(strcmp_ww(buf, strmB_name) == 0,
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
                        "Expected %s to be read, got %s\n", wine_dbgstr_w(strmB_name), wine_dbgstr_w(buf));

            IStream_Release(strm_tmp);
        }

        IStorage_Release(stg_tmp);
    }

    hr = IStorage_OpenStream(file2, strmC_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
    ok(hr == S_OK, "OpenStream failed: 0x%08x\n", hr);

    if(SUCCEEDED(hr)){
        memset(buf, 0, sizeof(buf));
        hr = IStream_Read(strm_tmp, buf, sizeof(buf), NULL);
        ok(hr == S_OK, "Read failed: 0x%08x\n", hr);
        if(SUCCEEDED(hr))
2432
            ok(strcmp_ww(buf, strmC_name) == 0,
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
                    "Expected %s to be read, got %s\n", wine_dbgstr_w(strmC_name), wine_dbgstr_w(buf));

        IStream_Release(strm_tmp);
    }

cleanup:
    if(file1)
        IStorage_Release(file1);
    if(file2)
        IStorage_Release(file2);

2444 2445
    DeleteFileA(file1_nameA);
    DeleteFileA(file2_nameA);
2446 2447
}

2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
static void test_copyto_snbexclusions(void)
{
    static const WCHAR *snb_exclude[] = {stgA_name, strmB_name, strmC_name, 0};

    IStorage *file1 = NULL, *file2 = NULL, *stg_tmp;
    IStream *strm_tmp;
    WCHAR buf[64];
    HRESULT hr;

    /* create & populate file1 */
    hr = StgCreateDocfile(file1_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file1);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = create_test_file(file1);
    if(FAILED(hr))
        goto cleanup;

    /* create file2 */
    hr = StgCreateDocfile(file2_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file2);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* copy file1 to file2 with name exclusions */
    hr = IStorage_CopyTo(file1, 0, NULL, (SNB)snb_exclude, file2);
    ok(hr == S_OK, "CopyTo failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* verify that file1 copied over, respecting exclusions */
    hr = IStorage_OpenStorage(file2, stgA_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStorage should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStorage_Release(stg_tmp);

    hr = IStorage_OpenStream(file2, strmA_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStream should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStream_Release(strm_tmp);

    hr = IStorage_OpenStorage(file2, stgB_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == S_OK, "OpenStorage failed: 0x%08x\n", hr);

    if(SUCCEEDED(hr)){
        hr = IStorage_OpenStream(stg_tmp, strmB_name, NULL,
                STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
        ok(hr == S_OK, "OpenStream failed: 0x%08x\n", hr);

        if(SUCCEEDED(hr)){
            memset(buf, 0, sizeof(buf));
            hr = IStream_Read(strm_tmp, buf, sizeof(buf), NULL);
            ok(hr == S_OK, "Read failed: 0x%08x\n", hr);
            if(SUCCEEDED(hr))
2506
                ok(strcmp_ww(buf, strmB_name) == 0,
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
                        "Expected %s to be read, got %s\n", wine_dbgstr_w(strmB_name), wine_dbgstr_w(buf));

            IStream_Release(strm_tmp);
        }

        IStorage_Release(stg_tmp);
    }

    hr = IStorage_OpenStream(file2, strmC_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStream should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStream_Release(strm_tmp);

cleanup:
    if(file1)
        IStorage_Release(file1);
    if(file2)
        IStorage_Release(file2);

2527 2528
    DeleteFileA(file1_nameA);
    DeleteFileA(file2_nameA);
2529 2530
}

2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
static void test_copyto_iidexclusions_storage(void)
{
    IStorage *file1 = NULL, *file2 = NULL, *stg_tmp;
    IStream *strm_tmp;
    WCHAR buf[64];
    HRESULT hr;

    /* create & populate file1 */
    hr = StgCreateDocfile(file1_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file1);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = create_test_file(file1);
    if(FAILED(hr))
        goto cleanup;

    /* create file2 */
    hr = StgCreateDocfile(file2_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file2);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* copy file1 to file2 with iid exclusions */
    hr = IStorage_CopyTo(file1, 1, &IID_IStorage, NULL, file2);
    ok(hr == S_OK, "CopyTo failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* verify that file1 copied over, respecting exclusions */
    hr = IStorage_OpenStorage(file2, stgA_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStorage should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStorage_Release(stg_tmp);

    hr = IStorage_OpenStream(file2, strmA_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStream should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStream_Release(strm_tmp);

    hr = IStorage_OpenStorage(file2, stgB_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStorage should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStorage_Release(stg_tmp);

    hr = IStorage_OpenStream(file2, strmB_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStream should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStream_Release(strm_tmp);

    hr = IStorage_OpenStream(file2, strmC_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
    ok(hr == S_OK, "OpenStream failed: 0x%08x\n", hr);

    if(SUCCEEDED(hr)){
        memset(buf, 0, sizeof(buf));
        hr = IStream_Read(strm_tmp, buf, sizeof(buf), NULL);
        ok(hr == S_OK, "Read failed: 0x%08x\n", hr);
        if(SUCCEEDED(hr))
2594
            ok(strcmp_ww(buf, strmC_name) == 0,
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
                    "Expected %s to be read, got %s\n", wine_dbgstr_w(strmC_name), wine_dbgstr_w(buf));

        IStream_Release(strm_tmp);
    }

cleanup:
    if(file1)
        IStorage_Release(file1);
    if(file2)
        IStorage_Release(file2);

2606 2607
    DeleteFileA(file1_nameA);
    DeleteFileA(file2_nameA);
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
}

static void test_copyto_iidexclusions_stream(void)
{
    IStorage *file1 = NULL, *file2 = NULL, *stg_tmp;
    IStream *strm_tmp;
    HRESULT hr;

    /* create & populate file1 */
    hr = StgCreateDocfile(file1_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file1);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    hr = create_test_file(file1);
    if(FAILED(hr))
        goto cleanup;

    /* create file2 */
    hr = StgCreateDocfile(file2_name, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &file2);
    ok(hr == S_OK, "StgCreateDocfile failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* copy file1 to file2 with iid exclusions */
    hr = IStorage_CopyTo(file1, 1, &IID_IStream, NULL, file2);
    ok(hr == S_OK, "CopyTo failed: 0x%08x\n", hr);
    if(FAILED(hr))
        goto cleanup;

    /* verify that file1 copied over, respecting exclusions */
    hr = IStorage_OpenStorage(file2, stgA_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == S_OK, "OpenStorage failed: 0x%08x\n", hr);

    if(SUCCEEDED(hr)){
        hr = IStorage_OpenStream(stg_tmp, strmA_name, NULL,
                STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
        ok(hr == STG_E_FILENOTFOUND, "OpenStream should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
        if(SUCCEEDED(hr))
            IStream_Release(strm_tmp);

        IStorage_Release(stg_tmp);
    }

    hr = IStorage_OpenStorage(file2, stgB_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg_tmp);
    ok(hr == S_OK, "OpenStorage failed: 0x%08x\n", hr);

    if(SUCCEEDED(hr)){
        hr = IStorage_OpenStream(stg_tmp, strmB_name, NULL,
                STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
        ok(hr == STG_E_FILENOTFOUND, "OpenStream should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
        if(SUCCEEDED(hr))
            IStream_Release(strm_tmp);

        IStorage_Release(stg_tmp);
    }

    hr = IStorage_OpenStream(file2, strmC_name, NULL,
            STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &strm_tmp);
    ok(hr == STG_E_FILENOTFOUND, "OpenStream should give STG_E_FILENOTFOUND, gave: 0x%08x\n", hr);
    if(SUCCEEDED(hr))
        IStream_Release(strm_tmp);

cleanup:
    if(file1)
        IStorage_Release(file1);
    if(file2)
        IStorage_Release(file2);

2679 2680
    DeleteFileA(file1_nameA);
    DeleteFileA(file2_nameA);
2681 2682
}

2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
static void test_rename(void)
{
    IStorage *stg, *stg2;
    IStream *stm;
    HRESULT r;
    static const WCHAR stgname[] = { 'P','E','R','M','S','T','G',0 };
    static const WCHAR stgname2[] = { 'S','T','G',0 };
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };
    static const WCHAR stmname2[] = { 'E','N','T','S',0 };

    DeleteFileA(filenameA);

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* create a substorage */
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* create a stream in the substorage */
    r = IStorage_CreateStream(stg2, stmname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
    ok(r==S_OK, "IStorage->CreateStream failed, hr=%08x\n", r);
    IStream_Release(stm);

    /* rename the stream */
    r = IStorage_RenameElement(stg2, stmname, stmname2);
    ok(r==S_OK, "IStorage->RenameElement failed, hr=%08x\n", r);

    /* cannot open stream with old name */
    r = IStorage_OpenStream(stg2, stmname, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
    ok(r==STG_E_FILENOTFOUND, "IStorage_OpenStream should fail, hr=%08x\n", r);
    if (SUCCEEDED(r)) IStream_Release(stm);

    /* can open stream with new name */
    r = IStorage_OpenStream(stg2, stmname2, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
    ok(r==S_OK, "IStorage_OpenStream failed, hr=%08x\n", r);
    if (SUCCEEDED(r)) IStream_Release(stm);

    IStorage_Release(stg2);

    /* rename the storage */
    IStorage_RenameElement(stg, stgname, stgname2);

    /* cannot open storage with old name */
    r = IStorage_OpenStorage(stg, stgname, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg2);
    ok(r==STG_E_FILENOTFOUND, "IStorage_OpenStream should fail, hr=%08x\n", r);
    if (SUCCEEDED(r)) IStorage_Release(stg2);

    /* can open storage with new name */
    r = IStorage_OpenStorage(stg, stgname2, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg2);
    ok(r==S_OK, "IStorage_OpenStream should fail, hr=%08x\n", r);
    if (SUCCEEDED(r))
    {
        /* opened storage still has the stream */
        r = IStorage_OpenStream(stg2, stmname2, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
        ok(r==S_OK, "IStorage_OpenStream failed, hr=%08x\n", r);
        if (SUCCEEDED(r)) IStream_Release(stm);

        IStorage_Release(stg2);
    }

    IStorage_Release(stg);

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

2752 2753 2754 2755 2756
static void test_toplevel_stat(void)
{
    IStorage *stg = NULL;
    HRESULT r;
    STATSTG stat;
2757 2758 2759 2760 2761
    char prev_dir[MAX_PATH];
    char temp[MAX_PATH];
    char full_path[MAX_PATH];
    LPSTR rel_pathA;
    WCHAR rel_path[MAX_PATH];
2762 2763 2764 2765 2766 2767 2768 2769

    DeleteFileA(filenameA);

    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    r = IStorage_Stat( stg, &stat, STATFLAG_DEFAULT );
2770
    ok(r==S_OK, "Storage_Stat failed with error 0x%08x\n", r);
2771
    ok(!strcmp_ww(stat.pwcsName, filename), "expected %s, got %s\n",
2772 2773 2774 2775 2776 2777 2778 2779 2780
        wine_dbgstr_w(filename), wine_dbgstr_w(stat.pwcsName));
    CoTaskMemFree(stat.pwcsName);

    IStorage_Release( stg );

    r = StgOpenStorage( filename, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg);
    ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);

    r = IStorage_Stat( stg, &stat, STATFLAG_DEFAULT );
2781
    ok(r==S_OK, "Storage_Stat failed with error 0x%08x\n", r);
2782
    ok(!strcmp_ww(stat.pwcsName, filename), "expected %s, got %s\n",
2783 2784 2785 2786 2787 2788 2789 2790
        wine_dbgstr_w(filename), wine_dbgstr_w(stat.pwcsName));
    CoTaskMemFree(stat.pwcsName);

    IStorage_Release( stg );

    DeleteFileA(filenameA);

    /* Stat always returns the full path, even for files opened with a relative path. */
2791
    GetCurrentDirectoryA(MAX_PATH, prev_dir);
2792

2793
    GetTempPathA(MAX_PATH, temp);
2794

2795
    SetCurrentDirectoryA(temp);
2796

2797 2798
    GetFullPathNameA(filenameA, MAX_PATH, full_path, &rel_pathA);
    MultiByteToWideChar(CP_ACP, 0, rel_pathA, -1, rel_path, MAX_PATH);
2799 2800 2801 2802 2803 2804

    r = StgCreateDocfile( rel_path, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE |STGM_TRANSACTED, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    r = IStorage_Stat( stg, &stat, STATFLAG_DEFAULT );
2805
    ok(r==S_OK, "Storage_Stat failed with error 0x%08x\n", r);
2806
    ok(!strcmp_ww(stat.pwcsName, filename), "expected %s, got %s\n",
2807 2808 2809 2810 2811 2812 2813 2814 2815
        wine_dbgstr_w(filename), wine_dbgstr_w(stat.pwcsName));
    CoTaskMemFree(stat.pwcsName);

    IStorage_Release( stg );

    r = StgOpenStorage( rel_path, NULL, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &stg);
    ok(r==S_OK, "StgOpenStorage failed with error 0x%08x\n", r);

    r = IStorage_Stat( stg, &stat, STATFLAG_DEFAULT );
2816
    ok(r==S_OK, "Storage_Stat failed with error 0x%08x\n", r);
2817
    ok(!strcmp_ww(stat.pwcsName, filename), "expected %s, got %s\n",
2818 2819 2820 2821 2822
        wine_dbgstr_w(filename), wine_dbgstr_w(stat.pwcsName));
    CoTaskMemFree(stat.pwcsName);

    IStorage_Release( stg );

2823
    SetCurrentDirectoryA(prev_dir);
2824 2825 2826 2827

    DeleteFileA(filenameA);
}

2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
static void test_substorage_enum(void)
{
    IStorage *stg, *stg2;
    IEnumSTATSTG *ee;
    HRESULT r;
    ULONG ref;
    static const WCHAR stgname[] = { 'P','E','R','M','S','T','G',0 };

    DeleteFileA(filenameA);

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* create a substorage */
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* create an enumelements */
    r = IStorage_EnumElements(stg2, 0, NULL, 0, &ee);
    ok(r==S_OK, "IStorage->EnumElements failed, hr=%08x\n", r);

    /* release the substorage */
    ref = IStorage_Release(stg2);
    todo_wine ok(ref==0, "storage not released\n");

    /* reopening fails, because the substorage is really still open */
    r = IStorage_OpenStorage(stg, stgname, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==STG_E_ACCESSDENIED, "IStorage->OpenStorage failed, hr=%08x\n", r);

    /* destroying the storage invalidates the enumerator */
    r = IStorage_DestroyElement(stg, stgname);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    r = IEnumSTATSTG_Reset(ee);
    ok(r==STG_E_REVERTED, "IEnumSTATSTG->Reset failed, hr=%08x\n", r);

    IEnumSTATSTG_Release(ee);

    IStorage_Release(stg);

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
static void test_copyto_locking(void)
{
    IStorage *stg, *stg2, *stg3, *stg4;
    IStream *stm;
    HRESULT r;
    static const WCHAR stgname[] = { 'S','T','G','1',0 };
    static const WCHAR stgname2[] = { 'S','T','G','2',0 };
    static const WCHAR stmname[] = { 'C','O','N','T','E','N','T','S',0 };

    DeleteFileA(filenameA);

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* create a substorage */
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* create another substorage */
    r = IStorage_CreateStorage(stg, stgname2, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg3);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* add a stream, and leave it open */
    r = IStorage_CreateStream(stg2, stmname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
    ok(r==S_OK, "IStorage->CreateStream failed, hr=%08x\n", r);

    /* Try to copy the storage while the stream is open */
    r = IStorage_CopyTo(stg2, 0, NULL, NULL, stg3);
2904
    ok(r==S_OK, "IStorage->CopyTo failed, hr=%08x\n", r);
2905 2906 2907 2908 2909 2910 2911 2912 2913

    IStream_Release(stm);

    /* create a substorage */
    r = IStorage_CreateStorage(stg2, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg4);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* Try to copy the storage while the substorage is open */
    r = IStorage_CopyTo(stg2, 0, NULL, NULL, stg3);
2914
    ok(r==S_OK, "IStorage->CopyTo failed, hr=%08x\n", r);
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924

    IStorage_Release(stg4);
    IStorage_Release(stg3);
    IStorage_Release(stg2);
    IStorage_Release(stg);

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975
static void test_copyto_recursive(void)
{
    IStorage *stg, *stg2, *stg3, *stg4;
    HRESULT r;
    static const WCHAR stgname[] = { 'S','T','G','1',0 };
    static const WCHAR stgname2[] = { 'S','T','G','2',0 };

    DeleteFileA(filenameA);

    /* create the file */
    r = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE |
                            STGM_READWRITE, 0, &stg);
    ok(r==S_OK, "StgCreateDocfile failed\n");

    /* create a substorage */
    r = IStorage_CreateStorage(stg, stgname, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stg2);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* copy the parent to the child */
    r = IStorage_CopyTo(stg, 0, NULL, NULL, stg2);
    ok(r==STG_E_ACCESSDENIED, "IStorage->CopyTo failed, hr=%08x\n", r);

    /* create a transacted substorage */
    r = IStorage_CreateStorage(stg, stgname2, STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, 0, &stg3);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* copy the parent to the transacted child */
    r = IStorage_CopyTo(stg, 0, NULL, NULL, stg2);
    ok(r==STG_E_ACCESSDENIED, "IStorage->CopyTo failed, hr=%08x\n", r);

    /* create a transacted subsubstorage */
    r = IStorage_CreateStorage(stg3, stgname2, STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 0, 0, &stg4);
    ok(r==S_OK, "IStorage->CreateStorage failed, hr=%08x\n", r);

    /* copy the parent to the transacted child of the transacted child */
    r = IStorage_CopyTo(stg, 0, NULL, NULL, stg4);
    ok(r==STG_E_ACCESSDENIED, "IStorage->CopyTo failed, hr=%08x\n", r);

    /* copy the parent but exclude storage objects */
    r = IStorage_CopyTo(stg, 1, &IID_IStorage, NULL, stg4);
    ok(r==S_OK, "IStorage->CopyTo failed, hr=%08x\n", r);

    IStorage_Release(stg4);
    IStorage_Release(stg3);
    IStorage_Release(stg2);
    IStorage_Release(stg);

    r = DeleteFileA(filenameA);
    ok( r == TRUE, "deleted file\n");
}

2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
static void test_hglobal_storage_creation(void)
{
    ILockBytes *ilb = NULL;
    IStorage *stg = NULL;
    HRESULT r;
    STATSTG stat;
    char junk[512];
    ULARGE_INTEGER offset;

    r = CreateILockBytesOnHGlobal(NULL, TRUE, &ilb);
    ok(r == S_OK, "CreateILockBytesOnHGlobal failed, hr=%x\n", r);

    offset.QuadPart = 0;
    memset(junk, 0xaa, 512);
    r = ILockBytes_WriteAt(ilb, offset, junk, 512, NULL);
    ok(r == S_OK, "ILockBytes_WriteAt failed, hr=%x\n", r);

    offset.QuadPart = 2000;
    r = ILockBytes_WriteAt(ilb, offset, junk, 512, NULL);
    ok(r == S_OK, "ILockBytes_WriteAt failed, hr=%x\n", r);

    r = StgCreateDocfileOnILockBytes(ilb, STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0,  &stg);
    ok(r == S_OK, "StgCreateDocfileOnILockBytes failed, hr=%x\n", r);

    IStorage_Release(stg);

    r = StgOpenStorageOnILockBytes(ilb, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE,
        NULL, 0, &stg);
3004
    ok(r == S_OK, "StgOpenStorageOnILockBytes failed, hr=%x\n", r);
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016

    if (SUCCEEDED(r))
    {
        r = IStorage_Stat(stg, &stat, STATFLAG_NONAME);
        ok(r == S_OK, "StgOpenStorageOnILockBytes failed, hr=%x\n", r);
        ok(IsEqualCLSID(&stat.clsid, &GUID_NULL), "unexpected CLSID value\n");

        IStorage_Release(stg);
    }

    r = ILockBytes_Stat(ilb, &stat, STATFLAG_NONAME);
    ok(r == S_OK, "ILockBytes_Stat failed, hr=%x\n", r);
3017
    ok(stat.cbSize.u.LowPart < 2512, "expected truncated size, got %d\n", stat.cbSize.u.LowPart);
3018 3019 3020 3021

    ILockBytes_Release(ilb);
}

3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
static void test_convert(void)
{
    static const WCHAR filename[] = {'s','t','o','r','a','g','e','.','s','t','g',0};
    IStorage *stg;
    HRESULT hr;

    hr = GetConvertStg(NULL);
    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);

    hr = StgCreateDocfile( filename, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, &stg);
    ok(hr == S_OK, "StgCreateDocfile failed\n");
    hr = GetConvertStg(stg);
    ok(hr == STG_E_FILENOTFOUND, "got 0x%08x\n", hr);
    hr = SetConvertStg(stg, TRUE);
3036 3037
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = SetConvertStg(stg, TRUE);
3038 3039 3040 3041 3042 3043 3044
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = GetConvertStg(stg);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = SetConvertStg(stg, FALSE);
    ok(hr == S_OK, "got 0x%08x\n", hr);
    hr = GetConvertStg(stg);
    ok(hr == S_FALSE, "got 0x%08x\n", hr);
3045

3046 3047 3048 3049 3050
    IStorage_Release(stg);

    DeleteFileW(filename);
}

3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073
static void test_direct_swmr(void)
{
    static const WCHAR fileW[] = {'w','i','n','e','t','e','s','t',0};
    IDirectWriterLock *dwlock;
    ULONG ref, ref2;
    IStorage *stg;
    HRESULT hr;

    /* it's possible to create in writer mode */
    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE | STGM_SHARE_DENY_WRITE | STGM_DIRECT_SWMR, 0, &stg);
todo_wine
    ok(hr == S_OK, "got %08x\n", hr);
if (hr == S_OK) {
    IStorage_Release(stg);
    DeleteFileW(fileW);
}

    hr = StgCreateDocfile(fileW, STGM_CREATE | STGM_READWRITE | STGM_SHARE_DENY_WRITE | STGM_TRANSACTED, 0, &stg);
    ok(hr == S_OK, "got %08x\n", hr);
    IStorage_Release(stg);

    /* reader mode */
    hr = StgOpenStorage(fileW, NULL, STGM_DIRECT_SWMR | STGM_READ | STGM_SHARE_DENY_NONE, NULL, 0, &stg);
3074 3075 3076 3077 3078 3079 3080
    ok(hr == S_OK || broken(hr == STG_E_INVALIDFLAG), "got %08x\n", hr);
    if(hr == S_OK)
    {
       hr = IStorage_QueryInterface(stg, &IID_IDirectWriterLock, (void**)&dwlock);
       ok(hr == E_NOINTERFACE, "got %08x\n", hr);
       IStorage_Release(stg);
    }
3081 3082 3083 3084

    /* writer mode */
    hr = StgOpenStorage(fileW, NULL, STGM_DIRECT_SWMR | STGM_READWRITE | STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
    ok(hr == S_OK, "got %08x\n", hr);
3085 3086 3087 3088
    if(hr == S_OK)
    {
        ref = IStorage_AddRef(stg);
        IStorage_Release(stg);
3089

3090 3091
        hr = IStorage_QueryInterface(stg, &IID_IDirectWriterLock, (void**)&dwlock);
        ok(hr == S_OK, "got %08x\n", hr);
3092

3093 3094 3095
        ref2 = IStorage_AddRef(stg);
        IStorage_Release(stg);
        ok(ref2 == ref + 1, "got %u\n", ref2);
3096

3097 3098 3099
        IDirectWriterLock_Release(dwlock);
        IStorage_Release(stg);
    }
3100 3101 3102 3103

    DeleteFileW(fileW);
}

3104 3105
START_TEST(storage32)
{
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116
    CHAR temp[MAX_PATH];

    GetTempPathA(MAX_PATH, temp);
    if(!GetTempFileNameA(temp, "stg", 0, filenameA))
    {
        win_skip("Could not create temp file, %u\n", GetLastError());
        return;
    }
    MultiByteToWideChar(CP_ACP, 0, filenameA, -1, filename, MAX_PATH);
    DeleteFileA(filenameA);

3117
    test_hglobal_storage_stat();
3118
    test_create_storage_modes();
3119
    test_stgcreatestorageex();
3120
    test_storage_stream();
3121
    test_open_storage();
3122
    test_storage_suminfo();
3123
    test_storage_refcount();
3124
    test_streamenum();
3125
    test_transact();
3126
    test_substorage_share();
3127
    test_revert();
3128
    test_parent_free();
3129
    test_nonroot_transacted();
3130
    test_ReadClassStm();
3131
    test_access();
3132
    test_writeclassstg();
3133
    test_readonly();
3134
    test_simple();
3135
    test_fmtusertypestg();
3136
    test_references();
3137
    test_copyto();
3138
    test_copyto_snbexclusions();
3139 3140
    test_copyto_iidexclusions_storage();
    test_copyto_iidexclusions_stream();
3141
    test_rename();
3142
    test_toplevel_stat();
3143
    test_substorage_enum();
3144
    test_copyto_locking();
3145
    test_copyto_recursive();
3146
    test_hglobal_storage_creation();
3147
    test_convert();
3148
    test_direct_swmr();
3149
}