os/persistentdata/persistentstorage/store/TFILE/t_storoom.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/store/TFILE/t_storoom.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,389 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <s32file.h>
    1.20 +#include <e32test.h>
    1.21 +
    1.22 +#define UNUSED_VAR(a) a = a
    1.23 +
    1.24 +const TInt KTestCleanupStack=0x20;
    1.25 +const TPtrC KTestDir=_L("\\STOR-TST\\T_OOM\\");
    1.26 +
    1.27 +#ifdef _DEBUG
    1.28 +const TPtrC desOriginalReverted2(_S("original/reverted A"),19);
    1.29 +const TPtrC desOriginalReverted3(_S("original/reverted B"),19);
    1.30 +const TPtrC desNewOverwritten2(_S("new/overwritten X"),17);
    1.31 +const TPtrC desNewOverwritten3(_S("new/overwritten Y"),17);
    1.32 +const TPtrC alphabet(_S("abcdefghijklmnopqrstuvwxyz"),26);
    1.33 +LOCAL_D CFileStore* store;
    1.34 +RStoreWriteStream out;
    1.35 +RStoreReadStream in;
    1.36 +TInt KMemoryAllocsInTestFunction=1;
    1.37 +#endif
    1.38 +
    1.39 +LOCAL_D CTrapCleanup* TheTrapCleanup;
    1.40 +LOCAL_D RTest test(_L("t_storoom"));
    1.41 +LOCAL_D RFs TheFs;
    1.42 +
    1.43 +LOCAL_C void setupTestDirectory()
    1.44 +    {// Prepare the test directory.
    1.45 +	TInt r=TheFs.Connect();
    1.46 +	test(r==KErrNone);
    1.47 +//
    1.48 +	r=TheFs.MkDirAll(KTestDir);
    1.49 +	test(r==KErrNone||r==KErrAlreadyExists);
    1.50 +	r=TheFs.SetSessionPath(KTestDir);
    1.51 +	test(r==KErrNone);
    1.52 +	}
    1.53 +
    1.54 +LOCAL_C void setupCleanup()
    1.55 +    {// Initialise the cleanup stack
    1.56 +	TheTrapCleanup=CTrapCleanup::New();
    1.57 +	test(TheTrapCleanup!=NULL);
    1.58 +	TRAPD(r,\
    1.59 +		{\
    1.60 +		for (TInt i=KTestCleanupStack;i>0;i--)\
    1.61 +			CleanupStack::PushL((TAny*)0);\
    1.62 +		CleanupStack::Pop(KTestCleanupStack);\
    1.63 +		});
    1.64 +	test(r==KErrNone);
    1.65 +	}
    1.66 +
    1.67 +#ifdef _DEBUG
    1.68 +LOCAL_D void CreateStoreSetRootAndDestroyStoreL()
    1.69 +	{
    1.70 +	TheFs.Delete(_L("pfs"));
    1.71 +	store=CPermanentFileStore::CreateLC(TheFs,_L("pfs"),EFileWrite|EFileRead);
    1.72 +	store->SetTypeL(KPermanentFileStoreLayoutUid);
    1.73 +	TStreamId rootId = store->ExtendL();
    1.74 +	store->SetRootL(rootId);
    1.75 +	store->CommitL();
    1.76 +	CleanupStack::PopAndDestroy();
    1.77 +	}
    1.78 +
    1.79 +LOCAL_D void AlterStoreL()
    1.80 +	{
    1.81 +	RStoreWriteStream out2;
    1.82 +	RStoreWriteStream out3;
    1.83 +	RStoreWriteStream out4;
    1.84 +	RStoreReadStream in;
    1.85 +
    1.86 +	TStreamId id2 = out.CreateLC(*store);
    1.87 +	out.CommitL();
    1.88 +	CleanupStack::PopAndDestroy();
    1.89 +
    1.90 +	TStreamId id3 = out.CreateLC(*store);
    1.91 +	out.CommitL();
    1.92 +	CleanupStack::PopAndDestroy();
    1.93 +
    1.94 +	TStreamId id4 = out.CreateLC(*store);
    1.95 +	out << _L("mum");
    1.96 +	out.CommitL();
    1.97 +	CleanupStack::PopAndDestroy();
    1.98 +
    1.99 +	out.ReplaceLC(*store,store->Root());
   1.100 +	out << id2;
   1.101 +	out << id3;
   1.102 +	out << id4;
   1.103 +	out.CommitL();
   1.104 +	CleanupStack::PopAndDestroy();
   1.105 +
   1.106 +	in.OpenLC(*store,store->Root());// use the root for in and out streams
   1.107 +	out.ReplaceLC(*store,store->Root());
   1.108 +	out.WriteL(in);
   1.109 +	out.CommitL();
   1.110 +	CleanupStack::PopAndDestroy(2);
   1.111 +
   1.112 +	out.ReplaceLC(*store,store->Root());// swap the order
   1.113 +	in.OpenLC(*store,store->Root());
   1.114 +	out.WriteL(in);
   1.115 +	out << _L("fromage");
   1.116 +	out.CommitL();
   1.117 +	CleanupStack::PopAndDestroy(2);
   1.118 +
   1.119 +	store->CommitL();
   1.120 +
   1.121 +	in.OpenLC(*store,store->Root());
   1.122 +	TStreamId idX,idZ;
   1.123 +	in >> idX;
   1.124 +	in >> idX;
   1.125 +	in >> idZ;// id4 "mum"
   1.126 +	CleanupStack::PopAndDestroy();
   1.127 +	out.OpenLC(*store,idZ);
   1.128 +	in.OpenLC(*store,idZ);
   1.129 +	out2.OpenLC(*store,idZ);
   1.130 +	out3.OpenLC(*store,idZ);
   1.131 +	out4.OpenLC(*store,idZ);
   1.132 +	out4.WriteL(in);
   1.133 +	out.CommitL();
   1.134 +	CleanupStack::PopAndDestroy(5);
   1.135 +	}
   1.136 +/**
   1.137 +@SYMTestCaseID          SYSLIB-STORE-CT-1170
   1.138 +@SYMTestCaseDesc	    Allocation failure in store test
   1.139 +@SYMTestPriority 	    High
   1.140 +@SYMTestActions  	    Tests for any memory errors during allocation of store
   1.141 +@SYMTestExpectedResults Test must not fail
   1.142 +@SYMREQ                 REQ0000
   1.143 +*/
   1.144 +LOCAL_D void AllocFailInSampleStoreCodeL()
   1.145 +	{
   1.146 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1170 "));
   1.147 +	test.Console()->Printf(_L("AllocFailInSampleStoreCodeL()\n"));
   1.148 +	TRAPD(r,CreateStoreSetRootAndDestroyStoreL())
   1.149 +    UNUSED_VAR(r);
   1.150 +	const TInt KAllocFail=15;
   1.151 +	for (TInt ii=1;ii<=20;++ii)
   1.152 +		{
   1.153 +		store=CPermanentFileStore::OpenLC(TheFs,_L("pfs"),EFileWrite|EFileRead);
   1.154 +		__UHEAP_FAILNEXT(ii);
   1.155 +		TRAPD(r,AlterStoreL());
   1.156 +		if (ii<KAllocFail)
   1.157 +			test(r==KErrNoMemory);
   1.158 +		if (ii>=KAllocFail)
   1.159 +			test(r==KErrNone);
   1.160 +		__UHEAP_RESET;
   1.161 +		CleanupStack::PopAndDestroy();
   1.162 +		}
   1.163 +	TheFs.Delete(_L("pfs"));
   1.164 +	}
   1.165 +
   1.166 +LOCAL_D void InitialseStoreWithDataL()
   1.167 +	{
   1.168 +	TheFs.Delete(_L("pope"));
   1.169 +	store=CPermanentFileStore::CreateLC(TheFs,_L("pope"),EFileWrite|EFileRead);
   1.170 +	store->SetTypeL(KPermanentFileStoreLayoutUid);
   1.171 +	TStreamId rootId = store->ExtendL();
   1.172 +	store->SetRootL(rootId);
   1.173 +	store->CommitL();
   1.174 +	CleanupStack::PopAndDestroy();
   1.175 +
   1.176 +	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
   1.177 +	TStreamId id2 = out.CreateLC(*store);
   1.178 +	out << desOriginalReverted2;
   1.179 +	out.CommitL();
   1.180 +	CleanupStack::PopAndDestroy();
   1.181 +
   1.182 +	TStreamId id3 = out.CreateLC(*store);
   1.183 +	out << desOriginalReverted3;
   1.184 +	out.CommitL();
   1.185 +	CleanupStack::PopAndDestroy();
   1.186 +
   1.187 +	out.ReplaceLC(*store,store->Root());
   1.188 +	out << id2;
   1.189 +	out << id3;
   1.190 +	out.CommitL();
   1.191 +	CleanupStack::PopAndDestroy();// out
   1.192 +
   1.193 +	store->CommitL();
   1.194 +	CleanupStack::PopAndDestroy();// store
   1.195 +	}
   1.196 +
   1.197 +LOCAL_D void AlterStoreDuringOutOfMemoryL(TInt aFail)
   1.198 +	{
   1.199 +	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
   1.200 +	in.OpenLC(*store,store->Root());
   1.201 +	TStreamId id2;
   1.202 +	TStreamId id3;
   1.203 +	in >> id2;
   1.204 +	in >> id3;
   1.205 +	CleanupStack::PopAndDestroy();// in
   1.206 +
   1.207 +	out.ReplaceLC(*store,id2);
   1.208 +	out << desNewOverwritten2;
   1.209 +	out.CommitL();
   1.210 +	CleanupStack::PopAndDestroy();// out
   1.211 +
   1.212 +	store->CommitL();
   1.213 +	__UHEAP_FAILNEXT(aFail);// Out of memory
   1.214 +
   1.215 +	out.ReplaceLC(*store,id3);
   1.216 +	out << desNewOverwritten3;
   1.217 +	out.CommitL();
   1.218 +	CleanupStack::PopAndDestroy();// out
   1.219 +
   1.220 +	store->CommitL();
   1.221 +	CleanupStack::PopAndDestroy();// store
   1.222 +
   1.223 +	__UHEAP_RESET;
   1.224 +	}
   1.225 +
   1.226 +/**
   1.227 +@SYMTestCaseID          SYSLIB-STORE-CT-1346
   1.228 +@SYMTestCaseDesc	    Streaming of data test
   1.229 +@SYMTestPriority 	    High
   1.230 +@SYMTestActions  	    Tests for RStoreReadStream::>> operator
   1.231 +@SYMTestExpectedResults Test must not fail
   1.232 +@SYMREQ                 REQ0000
   1.233 +*/
   1.234 +LOCAL_D void TestStreamDataL(TInt aFail)
   1.235 +	{
   1.236 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1346 "));
   1.237 +	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
   1.238 +	in.OpenLC(*store,store->Root());
   1.239 +	TStreamId id2;
   1.240 +	TStreamId id3;
   1.241 +	in >> id2;
   1.242 +	in >> id3;
   1.243 +	CleanupStack::PopAndDestroy();// in
   1.244 +
   1.245 +	TBuf<32> buf;
   1.246 +
   1.247 +	in.OpenLC(*store,id2);
   1.248 +	in >> buf;
   1.249 +	test(buf==desNewOverwritten2);
   1.250 +
   1.251 +	CleanupStack::PopAndDestroy();// in
   1.252 +
   1.253 +	in.OpenLC(*store,id3);
   1.254 +	in >> buf;
   1.255 +	if (aFail > KMemoryAllocsInTestFunction)
   1.256 +		test(buf==desNewOverwritten3);
   1.257 +	else if (aFail<=KMemoryAllocsInTestFunction)
   1.258 +		test(buf==desOriginalReverted3);
   1.259 +
   1.260 +	CleanupStack::PopAndDestroy();// in
   1.261 +
   1.262 +	CleanupStack::PopAndDestroy();// store
   1.263 +	}
   1.264 +
   1.265 +LOCAL_D void ResetStreamDataL()
   1.266 +	{
   1.267 +	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
   1.268 +	in.OpenLC(*store,store->Root());
   1.269 +	TStreamId id2;
   1.270 +	TStreamId id3;
   1.271 +	in >> id2;
   1.272 +	in >> id3;
   1.273 +	CleanupStack::PopAndDestroy();// in
   1.274 +
   1.275 +	out.ReplaceLC(*store,id2);
   1.276 +	out << desOriginalReverted2;
   1.277 +	out.CommitL();
   1.278 +	CleanupStack::PopAndDestroy();// out
   1.279 +
   1.280 +	out.ReplaceLC(*store,id3);
   1.281 +	out << desOriginalReverted3;
   1.282 +	out.CommitL();
   1.283 +	CleanupStack::PopAndDestroy();// out
   1.284 +
   1.285 +	store->CommitL();
   1.286 +	CleanupStack::PopAndDestroy();// store
   1.287 +	}
   1.288 +/**
   1.289 +@SYMTestCaseID          SYSLIB-STORE-CT-1171
   1.290 +@SYMTestCaseDesc	    Out of memory errors test
   1.291 +@SYMTestPriority 	    High
   1.292 +@SYMTestActions  	    Tests for out of memory conditions before commiting to the store
   1.293 +@SYMTestExpectedResults Test must not fail
   1.294 +@SYMREQ                 REQ0000
   1.295 +*/
   1.296 +LOCAL_D void OutOfMemoryBeforeStoreCommitL()
   1.297 +	{
   1.298 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1171 "));
   1.299 +	test.Console()->Printf(_L("OutOfMemoryBeforeStoreCommitL()\n"));
   1.300 +	InitialseStoreWithDataL();
   1.301 +	for (TInt fail=1; fail<=5; ++ fail)
   1.302 +		{
   1.303 +		TRAPD(r,AlterStoreDuringOutOfMemoryL(fail));
   1.304 +		if (fail<=KMemoryAllocsInTestFunction)
   1.305 +			test(r==KErrNoMemory);// store saved when r!=KErrNone
   1.306 +		else
   1.307 +			test(r==KErrNone);
   1.308 +		TestStreamDataL(fail);
   1.309 +		ResetStreamDataL();
   1.310 +		}
   1.311 +	TheFs.Delete(_L("pope"));
   1.312 +	}
   1.313 +
   1.314 +
   1.315 +LOCAL_D void OpenCloseStoreL(TInt aFail)
   1.316 +	{
   1.317 +	__UHEAP_FAILNEXT(aFail);
   1.318 +	TheFs.Delete(_L("pope"));
   1.319 +	store=CPermanentFileStore::CreateLC(TheFs,_L("pope"),EFileWrite|EFileRead);
   1.320 +	store->SetTypeL(KPermanentFileStoreLayoutUid);
   1.321 +	TStreamId rootId = store->ExtendL();
   1.322 +	store->SetRootL(rootId);
   1.323 +	store->CommitL();
   1.324 +	CleanupStack::PopAndDestroy();
   1.325 +
   1.326 +	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
   1.327 +	TStreamId id2 = out.CreateLC(*store);
   1.328 +	out << desOriginalReverted2;
   1.329 +	out << id2;
   1.330 +	out.CommitL();
   1.331 +	CleanupStack::PopAndDestroy(2);
   1.332 +	}
   1.333 +/**
   1.334 +@SYMTestCaseID          SYSLIB-STORE-CT-1172
   1.335 +@SYMTestCaseDesc	    Out of memory test
   1.336 +@SYMTestPriority 	    High
   1.337 +@SYMTestActions  	    Test for memory errors during opening and closing of store operation.
   1.338 +@SYMTestExpectedResults Test must not fail
   1.339 +@SYMREQ                 REQ0000
   1.340 +*/
   1.341 +
   1.342 +LOCAL_D void OutOfMemoryWhenOpeningClosingStoreL()
   1.343 +	{
   1.344 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1172 "));
   1.345 +	test.Console()->Printf(_L("OutOfMemoryWhenOpeningClosingStoreL()\n"));
   1.346 +	const TInt KAllocs=12;
   1.347 +	for (TInt fail=1; fail<=20; ++ fail)
   1.348 +		{
   1.349 +		TRAPD(r,OpenCloseStoreL(fail))
   1.350 +		if (fail<KAllocs)
   1.351 +			test(r==KErrNoMemory);
   1.352 +		else
   1.353 +			test(r==KErrNone);
   1.354 +		}
   1.355 +	TheFs.Delete(_L("pope"));
   1.356 +	__UHEAP_RESET;
   1.357 +	}
   1.358 +#endif
   1.359 +
   1.360 +GLDEF_C TInt E32Main()
   1.361 +    {// Test permanent file store
   1.362 +	test.Title();
   1.363 +	setupTestDirectory();
   1.364 +	setupCleanup();
   1.365 +#ifdef _DEBUG
   1.366 +	__UHEAP_MARK;
   1.367 +//
   1.368 +	test.Start(_L("Begin tests"));
   1.369 +	TRAPD(r,AllocFailInSampleStoreCodeL());
   1.370 +	test(r==KErrNone);
   1.371 +	TRAP(r,OutOfMemoryBeforeStoreCommitL());
   1.372 +	test(r==KErrNone);
   1.373 +	TRAP(r,OutOfMemoryWhenOpeningClosingStoreL());
   1.374 +	test(r==KErrNone);
   1.375 +	test.End();
   1.376 +
   1.377 +	TheFs.Delete(_L("pope"));
   1.378 +	TheFs.Delete(_L("pfs"));
   1.379 +//
   1.380 +	__UHEAP_MARKEND;
   1.381 +#endif
   1.382 +
   1.383 +#ifndef _DEBUG
   1.384 +	test.Start(_L("The tests are not valid in release mode"));
   1.385 +	test.End();
   1.386 +#endif
   1.387 +	delete TheTrapCleanup;
   1.388 +	TheFs.Close();
   1.389 +	test.Close();
   1.390 +	return 0;
   1.391 +    }
   1.392 +