os/persistentdata/persistentstorage/store/TFILE/t_storfdir.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_storfdir.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,379 @@
     1.4 +// Copyright (c) 1998-2010 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 +const TInt KTestCleanupStack=0x20;
    1.23 +
    1.24 +// This is a path specification and should not be used as is
    1.25 +_LIT(KFileLocationSpec, "Z:\\STOR-TST\\T_FDIR.DAT");
    1.26 +const TUint8* KTestData=_S8("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    1.27 +const TInt KTestLength=36;
    1.28 +const TInt KTestTotal=KTestLength*(KTestLength+1);
    1.29 +const TPtrC8 KTestDes(KTestData,KTestLength);
    1.30 +
    1.31 +LOCAL_D CTrapCleanup* TheTrapCleanup;
    1.32 +LOCAL_D RTest test(_L("t_storfdir"));
    1.33 +LOCAL_D RFs TheFs;
    1.34 +LOCAL_D TFileName TheTempFile;
    1.35 +LOCAL_D TBuf8<KTestLength+1> TheBuf;
    1.36 +
    1.37 +/**
    1.38 +@SYMTestCaseID          SYSLIB-STORE-CT-1147
    1.39 +@SYMTestCaseDesc	    Writing to a store test
    1.40 +@SYMTestPriority 	    High
    1.41 +@SYMTestActions  	    Tests for writing to a store
    1.42 +@SYMTestExpectedResults Test must not fail
    1.43 +@SYMREQ                 REQ0000
    1.44 +*/
    1.45 +LOCAL_C void testWriteL(CPersistentStore& aStore)
    1.46 +	{
    1.47 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1147 Writing... "));
    1.48 +	RStoreWriteStream out;
    1.49 +	TStreamId id=out.CreateLC(aStore);
    1.50 +	for (TInt i=0;i<=KTestLength;++i)
    1.51 +		{
    1.52 +		out.WriteL(KTestDes,i);
    1.53 +		out.WriteL(&KTestData[i],KTestLength-i);
    1.54 +		}
    1.55 +	out.CommitL();
    1.56 +	out.Close();
    1.57 +	aStore.SetRootL(out.CreateL(aStore));
    1.58 +	out<<KTestDes;
    1.59 +	out<<id;
    1.60 +	out.CommitL();
    1.61 +	CleanupStack::PopAndDestroy();
    1.62 +	}
    1.63 +/**
    1.64 +@SYMTestCaseID          SYSLIB-STORE-CT-1148
    1.65 +@SYMTestCaseDesc	    Reading from a stream test
    1.66 +@SYMTestPriority 	    High
    1.67 +@SYMTestActions  	    Attempt for reading data from a stream
    1.68 +@SYMTestExpectedResults Test must not fail
    1.69 +@SYMREQ                 REQ0000
    1.70 +*/
    1.71 +LOCAL_C void testReadL(RReadStream& aStream)
    1.72 +	{
    1.73 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1148 "));
    1.74 +	for (TInt i=KTestLength;i>=0;--i)
    1.75 +		{
    1.76 +		aStream.ReadL(TheBuf,i);
    1.77 +		test(TheBuf.Length()==i);
    1.78 +		TheBuf.SetMax();
    1.79 +		aStream.ReadL(&TheBuf[i],KTestLength-i);
    1.80 +		TheBuf.SetLength(KTestLength);
    1.81 +		test(TheBuf==KTestDes);
    1.82 +		}
    1.83 +	}
    1.84 +
    1.85 +//
    1.86 +// Test reading from a store
    1.87 +//
    1.88 +LOCAL_C void testReadL(const CPersistentStore& aStore)
    1.89 +	{
    1.90 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1148 Reading... "));
    1.91 +	RStoreReadStream in;
    1.92 +	in.OpenLC(aStore,aStore.Root());
    1.93 +	in>>TheBuf;
    1.94 +	TStreamId id;
    1.95 +	in>>id;
    1.96 +	in.Close();
    1.97 +	in.OpenL(aStore,id);
    1.98 +	testReadL(in);
    1.99 +	CleanupStack::PopAndDestroy();
   1.100 +	}
   1.101 +
   1.102 +//
   1.103 +// Test copying from one stream to another.
   1.104 +//
   1.105 +LOCAL_C void testCopyL(RWriteStream& aWriteStream,RReadStream& aReadStream)
   1.106 +	{
   1.107 +	test.Next(_L("Copying"));
   1.108 +	for (TInt i=KTestLength;i>=0;--i)
   1.109 +		{
   1.110 +		aWriteStream.WriteL(aReadStream,i);
   1.111 +		aReadStream.ReadL(aWriteStream,KTestLength-i);
   1.112 +		}
   1.113 +	}
   1.114 +/**
   1.115 +@SYMTestCaseID          SYSLIB-STORE-CT-1149
   1.116 +@SYMTestCaseDesc	    Tests writing using a direct file store
   1.117 +@SYMTestPriority 	    High
   1.118 +@SYMTestActions  	    Tests for memory and end of file error while creating the store.
   1.119 +                        Tests for writing to replaced,temporary,opened,created file.
   1.120 +						Tests for creating an already existing file.
   1.121 +						Tests for panic while deleting a file.
   1.122 +@SYMTestExpectedResults Test must not fail
   1.123 +@SYMREQ                 REQ0000
   1.124 +*/
   1.125 +LOCAL_C void testWriteL()
   1.126 +	{
   1.127 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1149 Creating and failing to open 'ghost' file "));
   1.128 +	
   1.129 +	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   1.130 +	TParse parse;
   1.131 +	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   1.132 +	
   1.133 +	TheFs.Delete(parse.NameAndExt());
   1.134 +	CFileStore* store=CDirectFileStore::CreateLC(TheFs,parse.NameAndExt(),EFileWrite);
   1.135 +	CleanupStack::PopAndDestroy();
   1.136 +	store=NULL;
   1.137 +	TRAPD(r,store=CDirectFileStore::OpenL(TheFs,parse.NameAndExt(),EFileRead|EFileWrite));
   1.138 +	test(store==NULL&&r==KErrEof);
   1.139 +//
   1.140 +	test.Next(_L("Writing to replaced file"));
   1.141 +	store=CDirectFileStore::ReplaceLC(TheFs,parse.NameAndExt(),EFileWrite);
   1.142 +	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KDirectFileStoreLayoutUid));
   1.143 +	testWriteL(*store);
   1.144 +	CleanupStack::PopAndDestroy();
   1.145 +//
   1.146 +	test.Next(_L("Writing to replaced file - 2"));
   1.147 +	store=CDirectFileStore::ReplaceL(TheFs,parse.NameAndExt(),EFileWrite);
   1.148 +	CleanupStack::PushL(store);
   1.149 +	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KDirectFileStoreLayoutUid));
   1.150 +	testWriteL(*store);
   1.151 +	CleanupStack::PopAndDestroy();
   1.152 +//
   1.153 +	test.Next(_L("Writing to temp file"));
   1.154 +	store=CDirectFileStore::TempLC(TheFs,parse.DriveAndPath(),TheTempFile,EFileWrite);
   1.155 +	store->SetTypeL(TUidType(store->Layout(),KNullUid,KDirectFileStoreLayoutUid));
   1.156 +	testWriteL(*store);
   1.157 +	store->CommitL();
   1.158 +	CleanupStack::PopAndDestroy();
   1.159 +	(void)TheFs.Delete(TheTempFile);
   1.160 +//	
   1.161 +	test.Next(_L("Writing to temp file - 2"));
   1.162 +	store=CDirectFileStore::TempL(TheFs,parse.DriveAndPath(),TheTempFile,EFileWrite);
   1.163 +	CleanupStack::PushL(store);
   1.164 +	store->SetTypeL(TUidType(store->Layout(),KNullUid,KDirectFileStoreLayoutUid));
   1.165 +	testWriteL(*store);
   1.166 +	store->CommitL();
   1.167 +	CleanupStack::PopAndDestroy();
   1.168 +//
   1.169 +	test.Next(_L("Writing to opened file"));
   1.170 +	store=CDirectFileStore::OpenLC(TheFs,parse.NameAndExt(),EFileRead|EFileWrite);
   1.171 +	testWriteL(*store);
   1.172 +	store->CommitL();
   1.173 +	CleanupStack::PopAndDestroy();
   1.174 +//
   1.175 +	test.Next(_L("Failing to create existing file"));
   1.176 +	store=NULL;
   1.177 +	TRAP(r,store=CDirectFileStore::CreateL(TheFs,TheTempFile,EFileWrite));
   1.178 +	test(store==NULL&&r==KErrAlreadyExists);
   1.179 +	if (TheFs.Delete(parse.NameAndExt())!=KErrNone)
   1.180 +		test.Panic(_L("Deleting file"));
   1.181 +//
   1.182 +	test.Next(_L("Writing to created file"));
   1.183 +	RFile file;
   1.184 +	test(file.Create(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
   1.185 +	store=CDirectFileStore::NewLC(file);
   1.186 +	CleanupStack::PopAndDestroy();
   1.187 +	test(file.Open(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
   1.188 +	store=CDirectFileStore::NewL(file);
   1.189 +	CleanupStack::PushL(store);
   1.190 +	store->SetTypeL(KDirectFileStoreLayoutUid);
   1.191 +	testWriteL(*store);
   1.192 +	store->CommitL();
   1.193 +	CleanupStack::PopAndDestroy();
   1.194 +	}
   1.195 +/**
   1.196 +@SYMTestCaseID          SYSLIB-STORE-CT-1150
   1.197 +@SYMTestCaseDesc	    Tests reading from an opened file
   1.198 +@SYMTestPriority 	    High
   1.199 +@SYMTestActions  	    Tests for reading using a direct file store
   1.200 +@SYMTestExpectedResults Test must not fail
   1.201 +@SYMREQ                 REQ0000
   1.202 +*/
   1.203 +LOCAL_C void testReadL()
   1.204 +	{
   1.205 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1150 Reading from opened file "));
   1.206 +	TParsePtrC parse(KFileLocationSpec);
   1.207 +	
   1.208 +	RFile file;
   1.209 +	test(file.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.210 +	CFileStore* store=CFileStore::FromL(file);
   1.211 +	CleanupStack::PushL(store);
   1.212 +	testReadL(*store);
   1.213 +	store->CommitL();
   1.214 +	TRAPD(r, store->RevertL());
   1.215 +	test(r== KErrNotSupported);
   1.216 +	CleanupStack::PopAndDestroy();
   1.217 +//
   1.218 +	test.Next(_L("Reading from temp file"));
   1.219 +	test(file.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   1.220 +	store=CDirectFileStore::FromLC(file);
   1.221 +	testReadL(*store);
   1.222 +	CleanupStack::PopAndDestroy();
   1.223 +
   1.224 +	test.Next(_L("Reading from temp file - 2"));
   1.225 +	test(file.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   1.226 +	store=CDirectFileStore::FromL(file);
   1.227 +	CleanupStack::PushL(store);
   1.228 +	testReadL(*store);
   1.229 +	CleanupStack::PopAndDestroy();
   1.230 +	}
   1.231 +/**
   1.232 +@SYMTestCaseID          SYSLIB-STORE-CT-1151
   1.233 +@SYMTestCaseDesc	    Copying to a single file store test.
   1.234 +@SYMTestPriority 	    High
   1.235 +@SYMTestActions  	    Test for copying using different buffer sizes
   1.236 +@SYMTestExpectedResults Test must not fail
   1.237 +@SYMREQ                 REQ0000
   1.238 +*/
   1.239 +LOCAL_C void testCopyL()
   1.240 +	{
   1.241 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1151 Copying using small transfers "));
   1.242 +	TParsePtrC parse(KFileLocationSpec);
   1.243 +	
   1.244 +	CFileStore* store=CFileStore::OpenLC(TheFs,parse.NameAndExt(),EFileRead|EFileWrite);
   1.245 +	RStoreReadStream in;
   1.246 +	in.OpenLC(*store,store->Root());
   1.247 +	in>>TheBuf;
   1.248 +	TStreamId copyId;
   1.249 +	in>>copyId;
   1.250 +	in.Close();
   1.251 +	in.OpenL(*store,copyId);
   1.252 +	RStoreWriteStream out;
   1.253 +	TStreamId id=out.CreateLC(*store);
   1.254 +	testCopyL(out,in);
   1.255 +	out.CommitL();
   1.256 +	out.Close();
   1.257 +	in.Close();
   1.258 +	in.OpenL(*store,id);
   1.259 +	testReadL(in);
   1.260 +	in.Close();
   1.261 +//
   1.262 +	test.Next(_L("Copying using a single big transfer"));
   1.263 +	in.OpenL(*store,copyId);
   1.264 +	id=out.CreateL(*store);
   1.265 +	in.ReadL(out,KTestTotal);
   1.266 +	out.CommitL();
   1.267 +	out.Close();
   1.268 +	in.Close();
   1.269 +	in.OpenL(*store,id);
   1.270 +	testReadL(in);
   1.271 +	in.Close();
   1.272 +	in.OpenL(*store,copyId);
   1.273 +	id=out.CreateL(*store);
   1.274 +	out.WriteL(in,KTestTotal);
   1.275 +	out.CommitL();
   1.276 +	out.Close();
   1.277 +	in.Close();
   1.278 +	in.OpenL(*store,id);
   1.279 +	testReadL(in);
   1.280 +//
   1.281 +	CleanupStack::PopAndDestroy(3);
   1.282 +	}
   1.283 +
   1.284 +//
   1.285 +// Prepare the test directory.
   1.286 +//
   1.287 +LOCAL_C void setupTestDirectory()
   1.288 +    {
   1.289 +	TInt r=TheFs.Connect();
   1.290 +	test(r==KErrNone);
   1.291 +//
   1.292 +	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   1.293 +	TParse parse;
   1.294 +	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   1.295 +	
   1.296 +	r=TheFs.MkDir(parse.DriveAndPath());
   1.297 +	test(r==KErrNone||r==KErrAlreadyExists);
   1.298 +	r=TheFs.SetSessionPath(parse.DriveAndPath());
   1.299 +	test(r==KErrNone);
   1.300 +	}
   1.301 +
   1.302 +//
   1.303 +// Initialise the cleanup stack.
   1.304 +//
   1.305 +LOCAL_C void setupCleanup()
   1.306 +    {
   1.307 +	TheTrapCleanup=CTrapCleanup::New();
   1.308 +	test(TheTrapCleanup!=NULL);
   1.309 +	TRAPD(r,\
   1.310 +		{\
   1.311 +		for (TInt i=KTestCleanupStack;i>0;i--)\
   1.312 +			CleanupStack::PushL((TAny*)0);\
   1.313 +		CleanupStack::Pop(KTestCleanupStack);\
   1.314 +		});
   1.315 +	test(r==KErrNone);
   1.316 +	}
   1.317 +
   1.318 +LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   1.319 +	{
   1.320 +	RFs fsSession;
   1.321 +	TInt err = fsSession.Connect();
   1.322 +	if(err == KErrNone)
   1.323 +		{
   1.324 +		TEntry entry;
   1.325 +		if(fsSession.Entry(aFullName, entry) == KErrNone)
   1.326 +			{
   1.327 +			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   1.328 +			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   1.329 +			if(err != KErrNone)
   1.330 +				{
   1.331 +				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   1.332 +				}
   1.333 +			err = fsSession.Delete(aFullName);
   1.334 +			if(err != KErrNone)
   1.335 +				{
   1.336 +				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   1.337 +				}
   1.338 +			}
   1.339 +		fsSession.Close();
   1.340 +		}
   1.341 +	else
   1.342 +		{
   1.343 +		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   1.344 +		}
   1.345 +	}
   1.346 +
   1.347 +GLDEF_C TInt E32Main()
   1.348 +//
   1.349 +// Test direct file store.
   1.350 +//
   1.351 +    {
   1.352 +	test.Title();
   1.353 +	setupTestDirectory();
   1.354 +	setupCleanup();
   1.355 +	__UHEAP_MARK;
   1.356 +//
   1.357 +	test.Start(_L("Test direct file store"));
   1.358 +	TRAPD(r,testWriteL());
   1.359 +	test(r==KErrNone);
   1.360 +	TRAP(r,testReadL());
   1.361 +	test(r==KErrNone);
   1.362 +	TRAP(r,testCopyL());
   1.363 +	test(r==KErrNone);
   1.364 +	
   1.365 +	//deletion of data files must be before call to .End() - DEF047652
   1.366 +	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   1.367 +	TParse parse;
   1.368 +	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   1.369 +	::DeleteDataFile(parse.FullName());
   1.370 +
   1.371 +	test.End();
   1.372 +//
   1.373 +	__UHEAP_MARKEND;
   1.374 +
   1.375 +	delete TheTrapCleanup;
   1.376 +	if (TheFs.Delete(TheTempFile)!=KErrNone)
   1.377 +		test.Panic(_L("Deleting temp file"));
   1.378 +	TheFs.Close();
   1.379 +	test.Close();
   1.380 +	return 0;
   1.381 +    }
   1.382 +