sbsv2/raptor/test/smoke_suite/test_resources/mmp/mmp3/t_oom.cpp
changeset 13 c327db0664bb
equal deleted inserted replaced
12:5e7562f67577 13:c327db0664bb
       
     1 /*
       
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32file.h>
       
    20 #include <e32test.h>
       
    21 
       
    22 #define UNUSED_VAR(a) a = a
       
    23 
       
    24 const TInt KTestCleanupStack=0x20;
       
    25 const TPtrC KTestDir=_L("\\STOR-TST\\T_OOM\\");
       
    26 
       
    27 #ifdef _DEBUG
       
    28 const TPtrC desOriginalReverted2(_S("original/reverted A"),19);
       
    29 const TPtrC desOriginalReverted3(_S("original/reverted B"),19);
       
    30 const TPtrC desNewOverwritten2(_S("new/overwritten X"),17);
       
    31 const TPtrC desNewOverwritten3(_S("new/overwritten Y"),17);
       
    32 const TPtrC alphabet(_S("abcdefghijklmnopqrstuvwxyz"),26);
       
    33 LOCAL_D CFileStore* store;
       
    34 RStoreWriteStream out;
       
    35 RStoreReadStream in;
       
    36 TInt KMemoryAllocsInTestFunction=1;
       
    37 #endif
       
    38 
       
    39 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    40 LOCAL_D RTest test(_L("T_OOM"));
       
    41 LOCAL_D RFs TheFs;
       
    42 
       
    43 LOCAL_C void setupTestDirectory()
       
    44     {// Prepare the test directory.
       
    45 	TInt r=TheFs.Connect();
       
    46 	test(r==KErrNone);
       
    47 //
       
    48 	r=TheFs.MkDirAll(KTestDir);
       
    49 	test(r==KErrNone||r==KErrAlreadyExists);
       
    50 	r=TheFs.SetSessionPath(KTestDir);
       
    51 	test(r==KErrNone);
       
    52 	}
       
    53 
       
    54 LOCAL_C void setupCleanup()
       
    55     {// Initialise the cleanup stack
       
    56 	TheTrapCleanup=CTrapCleanup::New();
       
    57 	test(TheTrapCleanup!=NULL);
       
    58 	TRAPD(r,\
       
    59 		{\
       
    60 		for (TInt i=KTestCleanupStack;i>0;i--)\
       
    61 			CleanupStack::PushL((TAny*)0);\
       
    62 		CleanupStack::Pop(KTestCleanupStack);\
       
    63 		});
       
    64 	test(r==KErrNone);
       
    65 	}
       
    66 
       
    67 #ifdef _DEBUG
       
    68 LOCAL_D void CreateStoreSetRootAndDestroyStoreL()
       
    69 	{
       
    70 	TheFs.Delete(_L("pfs"));
       
    71 	store=CPermanentFileStore::CreateLC(TheFs,_L("pfs"),EFileWrite|EFileRead);
       
    72 	store->SetTypeL(KPermanentFileStoreLayoutUid);
       
    73 	TStreamId rootId = store->ExtendL();
       
    74 	store->SetRootL(rootId);
       
    75 	store->CommitL();
       
    76 	CleanupStack::PopAndDestroy();
       
    77 	}
       
    78 
       
    79 LOCAL_D void AlterStoreL()
       
    80 	{
       
    81 	RStoreWriteStream out2;
       
    82 	RStoreWriteStream out3;
       
    83 	RStoreWriteStream out4;
       
    84 	RStoreReadStream in;
       
    85 
       
    86 	TStreamId id2 = out.CreateLC(*store);
       
    87 	out.CommitL();
       
    88 	CleanupStack::PopAndDestroy();
       
    89 
       
    90 	TStreamId id3 = out.CreateLC(*store);
       
    91 	out.CommitL();
       
    92 	CleanupStack::PopAndDestroy();
       
    93 
       
    94 	TStreamId id4 = out.CreateLC(*store);
       
    95 	out << _L("mum");
       
    96 	out.CommitL();
       
    97 	CleanupStack::PopAndDestroy();
       
    98 
       
    99 	out.ReplaceLC(*store,store->Root());
       
   100 	out << id2;
       
   101 	out << id3;
       
   102 	out << id4;
       
   103 	out.CommitL();
       
   104 	CleanupStack::PopAndDestroy();
       
   105 
       
   106 	in.OpenLC(*store,store->Root());// use the root for in and out streams
       
   107 	out.ReplaceLC(*store,store->Root());
       
   108 	out.WriteL(in);
       
   109 	out.CommitL();
       
   110 	CleanupStack::PopAndDestroy(2);
       
   111 
       
   112 	out.ReplaceLC(*store,store->Root());// swap the order
       
   113 	in.OpenLC(*store,store->Root());
       
   114 	out.WriteL(in);
       
   115 	out << _L("fromage");
       
   116 	out.CommitL();
       
   117 	CleanupStack::PopAndDestroy(2);
       
   118 
       
   119 	store->CommitL();
       
   120 
       
   121 	in.OpenLC(*store,store->Root());
       
   122 	TStreamId idX,idZ;
       
   123 	in >> idX;
       
   124 	in >> idX;
       
   125 	in >> idZ;// id4 "mum"
       
   126 	CleanupStack::PopAndDestroy();
       
   127 	out.OpenLC(*store,idZ);
       
   128 	in.OpenLC(*store,idZ);
       
   129 	out2.OpenLC(*store,idZ);
       
   130 	out3.OpenLC(*store,idZ);
       
   131 	out4.OpenLC(*store,idZ);
       
   132 	out4.WriteL(in);
       
   133 	out.CommitL();
       
   134 	CleanupStack::PopAndDestroy(5);
       
   135 	}
       
   136 /**
       
   137 @SYMTestCaseID          SYSLIB-STORE-CT-1170
       
   138 @SYMTestCaseDesc	    Allocation failure in store test
       
   139 @SYMTestPriority 	    High
       
   140 @SYMTestActions  	    Tests for any memory errors during allocation of store
       
   141 @SYMTestExpectedResults Test must not fail
       
   142 @SYMREQ                 REQ0000
       
   143 */
       
   144 LOCAL_D void AllocFailInSampleStoreCodeL()
       
   145 	{
       
   146 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1170 "));
       
   147 	test.Console()->Printf(_L("AllocFailInSampleStoreCodeL()\n"));
       
   148 	TRAPD(r,CreateStoreSetRootAndDestroyStoreL())
       
   149     UNUSED_VAR(r);
       
   150 	const TInt KAllocFail=15;
       
   151 	for (TInt ii=1;ii<=20;++ii)
       
   152 		{
       
   153 		store=CPermanentFileStore::OpenLC(TheFs,_L("pfs"),EFileWrite|EFileRead);
       
   154 		__UHEAP_FAILNEXT(ii);
       
   155 		TRAPD(r,AlterStoreL());
       
   156 		if (ii<KAllocFail)
       
   157 			test(r==KErrNoMemory);
       
   158 		if (ii>=KAllocFail)
       
   159 			test(r==KErrNone);
       
   160 		__UHEAP_RESET;
       
   161 		CleanupStack::PopAndDestroy();
       
   162 		}
       
   163 	TheFs.Delete(_L("pfs"));
       
   164 	}
       
   165 
       
   166 LOCAL_D void InitialseStoreWithDataL()
       
   167 	{
       
   168 	TheFs.Delete(_L("pope"));
       
   169 	store=CPermanentFileStore::CreateLC(TheFs,_L("pope"),EFileWrite|EFileRead);
       
   170 	store->SetTypeL(KPermanentFileStoreLayoutUid);
       
   171 	TStreamId rootId = store->ExtendL();
       
   172 	store->SetRootL(rootId);
       
   173 	store->CommitL();
       
   174 	CleanupStack::PopAndDestroy();
       
   175 
       
   176 	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
       
   177 	TStreamId id2 = out.CreateLC(*store);
       
   178 	out << desOriginalReverted2;
       
   179 	out.CommitL();
       
   180 	CleanupStack::PopAndDestroy();
       
   181 
       
   182 	TStreamId id3 = out.CreateLC(*store);
       
   183 	out << desOriginalReverted3;
       
   184 	out.CommitL();
       
   185 	CleanupStack::PopAndDestroy();
       
   186 
       
   187 	out.ReplaceLC(*store,store->Root());
       
   188 	out << id2;
       
   189 	out << id3;
       
   190 	out.CommitL();
       
   191 	CleanupStack::PopAndDestroy();// out
       
   192 
       
   193 	store->CommitL();
       
   194 	CleanupStack::PopAndDestroy();// store
       
   195 	}
       
   196 
       
   197 LOCAL_D void AlterStoreDuringOutOfMemoryL(TInt aFail)
       
   198 	{
       
   199 	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
       
   200 	in.OpenLC(*store,store->Root());
       
   201 	TStreamId id2;
       
   202 	TStreamId id3;
       
   203 	in >> id2;
       
   204 	in >> id3;
       
   205 	CleanupStack::PopAndDestroy();// in
       
   206 
       
   207 	out.ReplaceLC(*store,id2);
       
   208 	out << desNewOverwritten2;
       
   209 	out.CommitL();
       
   210 	CleanupStack::PopAndDestroy();// out
       
   211 
       
   212 	store->CommitL();
       
   213 	__UHEAP_FAILNEXT(aFail);// Out of memory
       
   214 
       
   215 	out.ReplaceLC(*store,id3);
       
   216 	out << desNewOverwritten3;
       
   217 	out.CommitL();
       
   218 	CleanupStack::PopAndDestroy();// out
       
   219 
       
   220 	store->CommitL();
       
   221 	CleanupStack::PopAndDestroy();// store
       
   222 
       
   223 	__UHEAP_RESET;
       
   224 	}
       
   225 
       
   226 /**
       
   227 @SYMTestCaseID          SYSLIB-STORE-CT-1346
       
   228 @SYMTestCaseDesc	    Streaming of data test
       
   229 @SYMTestPriority 	    High
       
   230 @SYMTestActions  	    Tests for RStoreReadStream::>> operator
       
   231 @SYMTestExpectedResults Test must not fail
       
   232 @SYMREQ                 REQ0000
       
   233 */
       
   234 LOCAL_D void TestStreamDataL(TInt aFail)
       
   235 	{
       
   236 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1346 "));
       
   237 	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
       
   238 	in.OpenLC(*store,store->Root());
       
   239 	TStreamId id2;
       
   240 	TStreamId id3;
       
   241 	in >> id2;
       
   242 	in >> id3;
       
   243 	CleanupStack::PopAndDestroy();// in
       
   244 
       
   245 	TBuf<32> buf;
       
   246 
       
   247 	in.OpenLC(*store,id2);
       
   248 	in >> buf;
       
   249 	test(buf==desNewOverwritten2);
       
   250 
       
   251 	CleanupStack::PopAndDestroy();// in
       
   252 
       
   253 	in.OpenLC(*store,id3);
       
   254 	in >> buf;
       
   255 	if (aFail > KMemoryAllocsInTestFunction)
       
   256 		test(buf==desNewOverwritten3);
       
   257 	else if (aFail<=KMemoryAllocsInTestFunction)
       
   258 		test(buf==desOriginalReverted3);
       
   259 
       
   260 	CleanupStack::PopAndDestroy();// in
       
   261 
       
   262 	CleanupStack::PopAndDestroy();// store
       
   263 	}
       
   264 
       
   265 LOCAL_D void ResetStreamDataL()
       
   266 	{
       
   267 	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
       
   268 	in.OpenLC(*store,store->Root());
       
   269 	TStreamId id2;
       
   270 	TStreamId id3;
       
   271 	in >> id2;
       
   272 	in >> id3;
       
   273 	CleanupStack::PopAndDestroy();// in
       
   274 
       
   275 	out.ReplaceLC(*store,id2);
       
   276 	out << desOriginalReverted2;
       
   277 	out.CommitL();
       
   278 	CleanupStack::PopAndDestroy();// out
       
   279 
       
   280 	out.ReplaceLC(*store,id3);
       
   281 	out << desOriginalReverted3;
       
   282 	out.CommitL();
       
   283 	CleanupStack::PopAndDestroy();// out
       
   284 
       
   285 	store->CommitL();
       
   286 	CleanupStack::PopAndDestroy();// store
       
   287 	}
       
   288 /**
       
   289 @SYMTestCaseID          SYSLIB-STORE-CT-1171
       
   290 @SYMTestCaseDesc	    Out of memory errors test
       
   291 @SYMTestPriority 	    High
       
   292 @SYMTestActions  	    Tests for out of memory conditions before commiting to the store
       
   293 @SYMTestExpectedResults Test must not fail
       
   294 @SYMREQ                 REQ0000
       
   295 */
       
   296 LOCAL_D void OutOfMemoryBeforeStoreCommitL()
       
   297 	{
       
   298 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1171 "));
       
   299 	test.Console()->Printf(_L("OutOfMemoryBeforeStoreCommitL()\n"));
       
   300 	InitialseStoreWithDataL();
       
   301 	for (TInt fail=1; fail<=5; ++ fail)
       
   302 		{
       
   303 		TRAPD(r,AlterStoreDuringOutOfMemoryL(fail));
       
   304 		if (fail<=KMemoryAllocsInTestFunction)
       
   305 			test(r==KErrNoMemory);// store saved when r!=KErrNone
       
   306 		else
       
   307 			test(r==KErrNone);
       
   308 		TestStreamDataL(fail);
       
   309 		ResetStreamDataL();
       
   310 		}
       
   311 	TheFs.Delete(_L("pope"));
       
   312 	}
       
   313 
       
   314 
       
   315 LOCAL_D void OpenCloseStoreL(TInt aFail)
       
   316 	{
       
   317 	__UHEAP_FAILNEXT(aFail);
       
   318 	TheFs.Delete(_L("pope"));
       
   319 	store=CPermanentFileStore::CreateLC(TheFs,_L("pope"),EFileWrite|EFileRead);
       
   320 	store->SetTypeL(KPermanentFileStoreLayoutUid);
       
   321 	TStreamId rootId = store->ExtendL();
       
   322 	store->SetRootL(rootId);
       
   323 	store->CommitL();
       
   324 	CleanupStack::PopAndDestroy();
       
   325 
       
   326 	store=CPermanentFileStore::OpenLC(TheFs,_L("pope"),EFileWrite|EFileRead);
       
   327 	TStreamId id2 = out.CreateLC(*store);
       
   328 	out << desOriginalReverted2;
       
   329 	out << id2;
       
   330 	out.CommitL();
       
   331 	CleanupStack::PopAndDestroy(2);
       
   332 	}
       
   333 /**
       
   334 @SYMTestCaseID          SYSLIB-STORE-CT-1172
       
   335 @SYMTestCaseDesc	    Out of memory test
       
   336 @SYMTestPriority 	    High
       
   337 @SYMTestActions  	    Test for memory errors during opening and closing of store operation.
       
   338 @SYMTestExpectedResults Test must not fail
       
   339 @SYMREQ                 REQ0000
       
   340 */
       
   341 
       
   342 LOCAL_D void OutOfMemoryWhenOpeningClosingStoreL()
       
   343 	{
       
   344 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1172 "));
       
   345 	test.Console()->Printf(_L("OutOfMemoryWhenOpeningClosingStoreL()\n"));
       
   346 	const TInt KAllocs=12;
       
   347 	for (TInt fail=1; fail<=20; ++ fail)
       
   348 		{
       
   349 		TRAPD(r,OpenCloseStoreL(fail))
       
   350 		if (fail<KAllocs)
       
   351 			test(r==KErrNoMemory);
       
   352 		else
       
   353 			test(r==KErrNone);
       
   354 		}
       
   355 	TheFs.Delete(_L("pope"));
       
   356 	__UHEAP_RESET;
       
   357 	}
       
   358 #endif
       
   359 
       
   360 GLDEF_C TInt E32Main()
       
   361     {// Test permanent file store
       
   362 	test.Title();
       
   363 	setupTestDirectory();
       
   364 	setupCleanup();
       
   365 #ifdef _DEBUG
       
   366 	__UHEAP_MARK;
       
   367 //
       
   368 	test.Start(_L("Begin tests"));
       
   369 	TRAPD(r,AllocFailInSampleStoreCodeL());
       
   370 	test(r==KErrNone);
       
   371 	TRAP(r,OutOfMemoryBeforeStoreCommitL());
       
   372 	test(r==KErrNone);
       
   373 	TRAP(r,OutOfMemoryWhenOpeningClosingStoreL());
       
   374 	test(r==KErrNone);
       
   375 	test.End();
       
   376 
       
   377 	TheFs.Delete(_L("pope"));
       
   378 	TheFs.Delete(_L("pfs"));
       
   379 //
       
   380 	__UHEAP_MARKEND;
       
   381 #endif
       
   382 
       
   383 #ifndef _DEBUG
       
   384 	test.Start(_L("The tests are not valid in release mode"));
       
   385 	test.End();
       
   386 #endif
       
   387 	delete TheTrapCleanup;
       
   388 	TheFs.Close();
       
   389 	test.Close();
       
   390 	return 0;
       
   391     }
       
   392