os/persistentdata/persistentstorage/store/TFILE/t_storfdir.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <s32file.h>
    17 #include <e32test.h>
    18 
    19 const TInt KTestCleanupStack=0x20;
    20 
    21 // This is a path specification and should not be used as is
    22 _LIT(KFileLocationSpec, "Z:\\STOR-TST\\T_FDIR.DAT");
    23 const TUint8* KTestData=_S8("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    24 const TInt KTestLength=36;
    25 const TInt KTestTotal=KTestLength*(KTestLength+1);
    26 const TPtrC8 KTestDes(KTestData,KTestLength);
    27 
    28 LOCAL_D CTrapCleanup* TheTrapCleanup;
    29 LOCAL_D RTest test(_L("t_storfdir"));
    30 LOCAL_D RFs TheFs;
    31 LOCAL_D TFileName TheTempFile;
    32 LOCAL_D TBuf8<KTestLength+1> TheBuf;
    33 
    34 /**
    35 @SYMTestCaseID          SYSLIB-STORE-CT-1147
    36 @SYMTestCaseDesc	    Writing to a store test
    37 @SYMTestPriority 	    High
    38 @SYMTestActions  	    Tests for writing to a store
    39 @SYMTestExpectedResults Test must not fail
    40 @SYMREQ                 REQ0000
    41 */
    42 LOCAL_C void testWriteL(CPersistentStore& aStore)
    43 	{
    44 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1147 Writing... "));
    45 	RStoreWriteStream out;
    46 	TStreamId id=out.CreateLC(aStore);
    47 	for (TInt i=0;i<=KTestLength;++i)
    48 		{
    49 		out.WriteL(KTestDes,i);
    50 		out.WriteL(&KTestData[i],KTestLength-i);
    51 		}
    52 	out.CommitL();
    53 	out.Close();
    54 	aStore.SetRootL(out.CreateL(aStore));
    55 	out<<KTestDes;
    56 	out<<id;
    57 	out.CommitL();
    58 	CleanupStack::PopAndDestroy();
    59 	}
    60 /**
    61 @SYMTestCaseID          SYSLIB-STORE-CT-1148
    62 @SYMTestCaseDesc	    Reading from a stream test
    63 @SYMTestPriority 	    High
    64 @SYMTestActions  	    Attempt for reading data from a stream
    65 @SYMTestExpectedResults Test must not fail
    66 @SYMREQ                 REQ0000
    67 */
    68 LOCAL_C void testReadL(RReadStream& aStream)
    69 	{
    70 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1148 "));
    71 	for (TInt i=KTestLength;i>=0;--i)
    72 		{
    73 		aStream.ReadL(TheBuf,i);
    74 		test(TheBuf.Length()==i);
    75 		TheBuf.SetMax();
    76 		aStream.ReadL(&TheBuf[i],KTestLength-i);
    77 		TheBuf.SetLength(KTestLength);
    78 		test(TheBuf==KTestDes);
    79 		}
    80 	}
    81 
    82 //
    83 // Test reading from a store
    84 //
    85 LOCAL_C void testReadL(const CPersistentStore& aStore)
    86 	{
    87 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1148 Reading... "));
    88 	RStoreReadStream in;
    89 	in.OpenLC(aStore,aStore.Root());
    90 	in>>TheBuf;
    91 	TStreamId id;
    92 	in>>id;
    93 	in.Close();
    94 	in.OpenL(aStore,id);
    95 	testReadL(in);
    96 	CleanupStack::PopAndDestroy();
    97 	}
    98 
    99 //
   100 // Test copying from one stream to another.
   101 //
   102 LOCAL_C void testCopyL(RWriteStream& aWriteStream,RReadStream& aReadStream)
   103 	{
   104 	test.Next(_L("Copying"));
   105 	for (TInt i=KTestLength;i>=0;--i)
   106 		{
   107 		aWriteStream.WriteL(aReadStream,i);
   108 		aReadStream.ReadL(aWriteStream,KTestLength-i);
   109 		}
   110 	}
   111 /**
   112 @SYMTestCaseID          SYSLIB-STORE-CT-1149
   113 @SYMTestCaseDesc	    Tests writing using a direct file store
   114 @SYMTestPriority 	    High
   115 @SYMTestActions  	    Tests for memory and end of file error while creating the store.
   116                         Tests for writing to replaced,temporary,opened,created file.
   117 						Tests for creating an already existing file.
   118 						Tests for panic while deleting a file.
   119 @SYMTestExpectedResults Test must not fail
   120 @SYMREQ                 REQ0000
   121 */
   122 LOCAL_C void testWriteL()
   123 	{
   124 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1149 Creating and failing to open 'ghost' file "));
   125 	
   126 	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   127 	TParse parse;
   128 	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   129 	
   130 	TheFs.Delete(parse.NameAndExt());
   131 	CFileStore* store=CDirectFileStore::CreateLC(TheFs,parse.NameAndExt(),EFileWrite);
   132 	CleanupStack::PopAndDestroy();
   133 	store=NULL;
   134 	TRAPD(r,store=CDirectFileStore::OpenL(TheFs,parse.NameAndExt(),EFileRead|EFileWrite));
   135 	test(store==NULL&&r==KErrEof);
   136 //
   137 	test.Next(_L("Writing to replaced file"));
   138 	store=CDirectFileStore::ReplaceLC(TheFs,parse.NameAndExt(),EFileWrite);
   139 	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KDirectFileStoreLayoutUid));
   140 	testWriteL(*store);
   141 	CleanupStack::PopAndDestroy();
   142 //
   143 	test.Next(_L("Writing to replaced file - 2"));
   144 	store=CDirectFileStore::ReplaceL(TheFs,parse.NameAndExt(),EFileWrite);
   145 	CleanupStack::PushL(store);
   146 	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KDirectFileStoreLayoutUid));
   147 	testWriteL(*store);
   148 	CleanupStack::PopAndDestroy();
   149 //
   150 	test.Next(_L("Writing to temp file"));
   151 	store=CDirectFileStore::TempLC(TheFs,parse.DriveAndPath(),TheTempFile,EFileWrite);
   152 	store->SetTypeL(TUidType(store->Layout(),KNullUid,KDirectFileStoreLayoutUid));
   153 	testWriteL(*store);
   154 	store->CommitL();
   155 	CleanupStack::PopAndDestroy();
   156 	(void)TheFs.Delete(TheTempFile);
   157 //	
   158 	test.Next(_L("Writing to temp file - 2"));
   159 	store=CDirectFileStore::TempL(TheFs,parse.DriveAndPath(),TheTempFile,EFileWrite);
   160 	CleanupStack::PushL(store);
   161 	store->SetTypeL(TUidType(store->Layout(),KNullUid,KDirectFileStoreLayoutUid));
   162 	testWriteL(*store);
   163 	store->CommitL();
   164 	CleanupStack::PopAndDestroy();
   165 //
   166 	test.Next(_L("Writing to opened file"));
   167 	store=CDirectFileStore::OpenLC(TheFs,parse.NameAndExt(),EFileRead|EFileWrite);
   168 	testWriteL(*store);
   169 	store->CommitL();
   170 	CleanupStack::PopAndDestroy();
   171 //
   172 	test.Next(_L("Failing to create existing file"));
   173 	store=NULL;
   174 	TRAP(r,store=CDirectFileStore::CreateL(TheFs,TheTempFile,EFileWrite));
   175 	test(store==NULL&&r==KErrAlreadyExists);
   176 	if (TheFs.Delete(parse.NameAndExt())!=KErrNone)
   177 		test.Panic(_L("Deleting file"));
   178 //
   179 	test.Next(_L("Writing to created file"));
   180 	RFile file;
   181 	test(file.Create(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
   182 	store=CDirectFileStore::NewLC(file);
   183 	CleanupStack::PopAndDestroy();
   184 	test(file.Open(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
   185 	store=CDirectFileStore::NewL(file);
   186 	CleanupStack::PushL(store);
   187 	store->SetTypeL(KDirectFileStoreLayoutUid);
   188 	testWriteL(*store);
   189 	store->CommitL();
   190 	CleanupStack::PopAndDestroy();
   191 	}
   192 /**
   193 @SYMTestCaseID          SYSLIB-STORE-CT-1150
   194 @SYMTestCaseDesc	    Tests reading from an opened file
   195 @SYMTestPriority 	    High
   196 @SYMTestActions  	    Tests for reading using a direct file store
   197 @SYMTestExpectedResults Test must not fail
   198 @SYMREQ                 REQ0000
   199 */
   200 LOCAL_C void testReadL()
   201 	{
   202 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1150 Reading from opened file "));
   203 	TParsePtrC parse(KFileLocationSpec);
   204 	
   205 	RFile file;
   206 	test(file.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   207 	CFileStore* store=CFileStore::FromL(file);
   208 	CleanupStack::PushL(store);
   209 	testReadL(*store);
   210 	store->CommitL();
   211 	TRAPD(r, store->RevertL());
   212 	test(r== KErrNotSupported);
   213 	CleanupStack::PopAndDestroy();
   214 //
   215 	test.Next(_L("Reading from temp file"));
   216 	test(file.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   217 	store=CDirectFileStore::FromLC(file);
   218 	testReadL(*store);
   219 	CleanupStack::PopAndDestroy();
   220 
   221 	test.Next(_L("Reading from temp file - 2"));
   222 	test(file.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   223 	store=CDirectFileStore::FromL(file);
   224 	CleanupStack::PushL(store);
   225 	testReadL(*store);
   226 	CleanupStack::PopAndDestroy();
   227 	}
   228 /**
   229 @SYMTestCaseID          SYSLIB-STORE-CT-1151
   230 @SYMTestCaseDesc	    Copying to a single file store test.
   231 @SYMTestPriority 	    High
   232 @SYMTestActions  	    Test for copying using different buffer sizes
   233 @SYMTestExpectedResults Test must not fail
   234 @SYMREQ                 REQ0000
   235 */
   236 LOCAL_C void testCopyL()
   237 	{
   238 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1151 Copying using small transfers "));
   239 	TParsePtrC parse(KFileLocationSpec);
   240 	
   241 	CFileStore* store=CFileStore::OpenLC(TheFs,parse.NameAndExt(),EFileRead|EFileWrite);
   242 	RStoreReadStream in;
   243 	in.OpenLC(*store,store->Root());
   244 	in>>TheBuf;
   245 	TStreamId copyId;
   246 	in>>copyId;
   247 	in.Close();
   248 	in.OpenL(*store,copyId);
   249 	RStoreWriteStream out;
   250 	TStreamId id=out.CreateLC(*store);
   251 	testCopyL(out,in);
   252 	out.CommitL();
   253 	out.Close();
   254 	in.Close();
   255 	in.OpenL(*store,id);
   256 	testReadL(in);
   257 	in.Close();
   258 //
   259 	test.Next(_L("Copying using a single big transfer"));
   260 	in.OpenL(*store,copyId);
   261 	id=out.CreateL(*store);
   262 	in.ReadL(out,KTestTotal);
   263 	out.CommitL();
   264 	out.Close();
   265 	in.Close();
   266 	in.OpenL(*store,id);
   267 	testReadL(in);
   268 	in.Close();
   269 	in.OpenL(*store,copyId);
   270 	id=out.CreateL(*store);
   271 	out.WriteL(in,KTestTotal);
   272 	out.CommitL();
   273 	out.Close();
   274 	in.Close();
   275 	in.OpenL(*store,id);
   276 	testReadL(in);
   277 //
   278 	CleanupStack::PopAndDestroy(3);
   279 	}
   280 
   281 //
   282 // Prepare the test directory.
   283 //
   284 LOCAL_C void setupTestDirectory()
   285     {
   286 	TInt r=TheFs.Connect();
   287 	test(r==KErrNone);
   288 //
   289 	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   290 	TParse parse;
   291 	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   292 	
   293 	r=TheFs.MkDir(parse.DriveAndPath());
   294 	test(r==KErrNone||r==KErrAlreadyExists);
   295 	r=TheFs.SetSessionPath(parse.DriveAndPath());
   296 	test(r==KErrNone);
   297 	}
   298 
   299 //
   300 // Initialise the cleanup stack.
   301 //
   302 LOCAL_C void setupCleanup()
   303     {
   304 	TheTrapCleanup=CTrapCleanup::New();
   305 	test(TheTrapCleanup!=NULL);
   306 	TRAPD(r,\
   307 		{\
   308 		for (TInt i=KTestCleanupStack;i>0;i--)\
   309 			CleanupStack::PushL((TAny*)0);\
   310 		CleanupStack::Pop(KTestCleanupStack);\
   311 		});
   312 	test(r==KErrNone);
   313 	}
   314 
   315 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   316 	{
   317 	RFs fsSession;
   318 	TInt err = fsSession.Connect();
   319 	if(err == KErrNone)
   320 		{
   321 		TEntry entry;
   322 		if(fsSession.Entry(aFullName, entry) == KErrNone)
   323 			{
   324 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   325 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   326 			if(err != KErrNone)
   327 				{
   328 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   329 				}
   330 			err = fsSession.Delete(aFullName);
   331 			if(err != KErrNone)
   332 				{
   333 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   334 				}
   335 			}
   336 		fsSession.Close();
   337 		}
   338 	else
   339 		{
   340 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   341 		}
   342 	}
   343 
   344 GLDEF_C TInt E32Main()
   345 //
   346 // Test direct file store.
   347 //
   348     {
   349 	test.Title();
   350 	setupTestDirectory();
   351 	setupCleanup();
   352 	__UHEAP_MARK;
   353 //
   354 	test.Start(_L("Test direct file store"));
   355 	TRAPD(r,testWriteL());
   356 	test(r==KErrNone);
   357 	TRAP(r,testReadL());
   358 	test(r==KErrNone);
   359 	TRAP(r,testCopyL());
   360 	test(r==KErrNone);
   361 	
   362 	//deletion of data files must be before call to .End() - DEF047652
   363 	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   364 	TParse parse;
   365 	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   366 	::DeleteDataFile(parse.FullName());
   367 
   368 	test.End();
   369 //
   370 	__UHEAP_MARKEND;
   371 
   372 	delete TheTrapCleanup;
   373 	if (TheFs.Delete(TheTempFile)!=KErrNone)
   374 		test.Panic(_L("Deleting temp file"));
   375 	TheFs.Close();
   376 	test.Close();
   377 	return 0;
   378     }
   379