os/persistentdata/persistentstorage/store/TFILE/t_storfstrm.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_storfstrm.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,486 @@
     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 <s32mem.h>
    1.21 +#include <e32test.h>
    1.22 +
    1.23 +const TInt KTestCleanupStack=0x20;
    1.24 +
    1.25 +// This is a path specification and should not be used as is
    1.26 +_LIT(KFileLocationSpec, "Z:\\STOR-TST\\T_FSTRM.DAT");
    1.27 +const TUint8* KTestData=_S8("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    1.28 +const TInt KTestLength=36;
    1.29 +const TInt KTestTotal=KTestLength*(KTestLength+1);
    1.30 +const TPtrC8 KTestDes(KTestData,KTestLength);
    1.31 +
    1.32 +LOCAL_D CTrapCleanup* TheTrapCleanup;
    1.33 +LOCAL_D RTest test(_L("t_storfstrm"));
    1.34 +LOCAL_D RFs TheFs;
    1.35 +LOCAL_D TFileName TheTempFile;
    1.36 +/**
    1.37 +@SYMTestCaseID          SYSLIB-STORE-CT-1162
    1.38 +@SYMTestCaseDesc	    Tests for writing to a stream.
    1.39 +@SYMTestPriority 	    High
    1.40 +@SYMTestActions  	    Attempt for writing to a stream
    1.41 +@SYMTestExpectedResults Test must not fail
    1.42 +@SYMREQ                 REQ0000
    1.43 +*/
    1.44 +LOCAL_C void testWriteL(RWriteStream& aStream)
    1.45 +	{
    1.46 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1162 Writing... "));
    1.47 +	for (TInt i=0;i<=KTestLength;++i)
    1.48 +		{
    1.49 +		aStream.WriteL(KTestDes,i);
    1.50 +		aStream.WriteL(&KTestData[i],KTestLength-i);
    1.51 +		}
    1.52 +	}
    1.53 +/**
    1.54 +@SYMTestCaseID          SYSLIB-STORE-CT-1163
    1.55 +@SYMTestCaseDesc	    Tests for reading from a stream.
    1.56 +@SYMTestPriority 	    High
    1.57 +@SYMTestActions  	    Attempt for reading from a stream
    1.58 +@SYMTestExpectedResults Test must not fail
    1.59 +@SYMREQ                 REQ0000
    1.60 +*/
    1.61 +LOCAL_C void testReadL(RReadStream& aStream)
    1.62 +	{
    1.63 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1163 Reading... "));
    1.64 +	for (TInt i=KTestLength;i>=0;--i)
    1.65 +		{
    1.66 +		TBuf8<KTestLength+1> buf;
    1.67 +		aStream.ReadL(buf,i);
    1.68 +		test(buf.Length()==i);
    1.69 +		buf.SetMax();
    1.70 +		aStream.ReadL(&buf[i],KTestLength-i);
    1.71 +		buf.SetLength(KTestLength);
    1.72 +		test(buf==KTestDes);
    1.73 +		}
    1.74 +	}
    1.75 +/**
    1.76 +@SYMTestCaseID          SYSLIB-STORE-CT-1164
    1.77 +@SYMTestCaseDesc	    Tests for skipping data on a stream.
    1.78 +@SYMTestPriority 	    High
    1.79 +@SYMTestActions  	    Tests for skipping data while reading from a stream
    1.80 +@SYMTestExpectedResults Test must not fail
    1.81 +@SYMREQ                 REQ0000
    1.82 +*/
    1.83 +LOCAL_C void testSkipL(RReadStream& aStream)
    1.84 +	{
    1.85 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1164 Skipping... "));
    1.86 +	for (TInt i=0;i<=KTestLength;++i)
    1.87 +		{
    1.88 +		aStream.ReadL(i);
    1.89 +		aStream.ReadL(KTestLength-i);
    1.90 +		}
    1.91 +	}
    1.92 +/**
    1.93 +@SYMTestCaseID          SYSLIB-STORE-CT-1165
    1.94 +@SYMTestCaseDesc	    Tests a stream is at end-of-file.
    1.95 +@SYMTestPriority 	    High
    1.96 +@SYMTestActions  	    Tests for end of file while reading from a stream
    1.97 +@SYMTestExpectedResults Test must not fail
    1.98 +@SYMREQ                 REQ0000
    1.99 +*/
   1.100 +LOCAL_C void testEofL(RReadStream& aStream)
   1.101 +	{
   1.102 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1165 At end "));
   1.103 +	TUint8 b;
   1.104 +	test(aStream.Source()->ReadL(&b,1)==0);
   1.105 +	}
   1.106 +
   1.107 +//
   1.108 +// Test copying from one stream to another.
   1.109 +//
   1.110 +LOCAL_C void testCopyL(RWriteStream& aWriteStream,RReadStream& aReadStream)
   1.111 +	{
   1.112 +	test.Next(_L("Copying"));
   1.113 +	for (TInt i=KTestLength;i>=0;--i)
   1.114 +		{
   1.115 +		aWriteStream.WriteL(aReadStream,i);
   1.116 +		aReadStream.ReadL(aWriteStream,KTestLength-i);
   1.117 +		}
   1.118 +	}
   1.119 +/**
   1.120 +@SYMTestCaseID          SYSLIB-STORE-CT-1166
   1.121 +@SYMTestCaseDesc	    Tests for writing to a file stream.
   1.122 +						Tests RFileWriteStream::WriteL() function
   1.123 +@SYMTestPriority 	    High
   1.124 +@SYMTestActions  	    Tests for writing to replaced,temporary,opened,created file.
   1.125 +						Tests for creating an already existing file.
   1.126 +						Tests for panic while deleting a file.
   1.127 +@SYMTestExpectedResults Test must not fail
   1.128 +@SYMREQ                 REQ0000
   1.129 +*/
   1.130 +LOCAL_C void testWriteL()
   1.131 +	{
   1.132 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1166 Writing to attached file "));
   1.133 +
   1.134 +	
   1.135 +	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   1.136 +	TParse parse;
   1.137 +	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   1.138 +	RFile file;
   1.139 +	if (file.Replace(TheFs,parse.NameAndExt(),EFileWrite)!=KErrNone)
   1.140 +		test.Panic(_L("Replacing file"));
   1.141 +	RFile f=file;
   1.142 +	RFileWriteStream out(f);
   1.143 +	testWriteL(out);
   1.144 +	out.CommitL();
   1.145 +	out.Attach(file);
   1.146 +	testWriteL(out);
   1.147 +	out.Close();
   1.148 +//
   1.149 +	test.Next(_L("Writing to replaced file"));
   1.150 +	test(out.Replace(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
   1.151 +	testWriteL(out);
   1.152 +	out.Close();
   1.153 +//
   1.154 +	test.Next(_L("Writing to temp file"));
   1.155 +	test(out.Temp(TheFs,parse.DriveAndPath(),TheTempFile,EFileWrite)==KErrNone);
   1.156 +	testWriteL(out);
   1.157 +	out.CommitL();
   1.158 +	out.Close();
   1.159 +//
   1.160 +	test.Next(_L("Writing to opened file"));
   1.161 +	test(out.Open(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
   1.162 +	testWriteL(out);
   1.163 +//
   1.164 +	test.Next(_L("Failing to create existing file"));
   1.165 +	test(out.Create(TheFs,TheTempFile,EFileWrite)==KErrAlreadyExists);
   1.166 +	testWriteL(out);
   1.167 +	out.Close();
   1.168 +	if (TheFs.Delete(parse.NameAndExt())!=KErrNone)
   1.169 +		test.Panic(_L("Deleting file"));
   1.170 +//
   1.171 +	test.Next(_L("Writing to created file"));
   1.172 +	test(out.Create(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
   1.173 +	testWriteL(out);
   1.174 +	out.CommitL();
   1.175 +	out.Close();
   1.176 +	}
   1.177 +/**
   1.178 +@SYMTestCaseID          SYSLIB-STORE-CT-1167
   1.179 +@SYMTestCaseDesc	    Tests reading from a file stream.
   1.180 +@SYMTestPriority 	    High
   1.181 +@SYMTestActions  	    Attempt for reading from an attached file
   1.182 +						Attempt for reading from opened file
   1.183 +						Attempt for reading from temp file.Test for end of file error
   1.184 +@SYMTestExpectedResults Test must not fail
   1.185 +@SYMREQ                 REQ0000
   1.186 +*/
   1.187 +LOCAL_C void testReadL()
   1.188 +	{
   1.189 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1167 Reading from attached file "));
   1.190 +
   1.191 +	TParsePtrC parse(KFileLocationSpec);
   1.192 +	
   1.193 +	RFile file;
   1.194 +	if (file.Open(TheFs,parse.NameAndExt(),EFileRead)!=KErrNone)
   1.195 +		test.Panic(_L("Opening file"));
   1.196 +	RFile f=file;
   1.197 +	RFileReadStream in(f);
   1.198 +	testReadL(in);
   1.199 +	testEofL(in);
   1.200 +	in.Attach(file);
   1.201 +	testReadL(in);
   1.202 +	testEofL(in);
   1.203 +	in.Close();
   1.204 +//
   1.205 +	test.Next(_L("Reading from opened file"));
   1.206 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.207 +	testReadL(in);
   1.208 +	testEofL(in);
   1.209 +	in.Close();
   1.210 +//
   1.211 +	test.Next(_L("Reading from temp file"));
   1.212 +	test(in.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   1.213 +	testReadL(in);
   1.214 +	testEofL(in);
   1.215 +	in.Close();
   1.216 +	}
   1.217 +/**
   1.218 +@SYMTestCaseID          SYSLIB-STORE-CT-1168
   1.219 +@SYMTestCaseDesc	    Skipping on a file stream test
   1.220 +@SYMTestPriority 	    High
   1.221 +@SYMTestActions  	    Attempt for skipping data while reading from a stream
   1.222 +@SYMTestExpectedResults Test must not fail
   1.223 +@SYMREQ                 REQ0000
   1.224 +*/
   1.225 +LOCAL_C void testSkipL()
   1.226 +	{
   1.227 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1168 Skipping using small transfers "));
   1.228 +
   1.229 +	TParsePtrC parse(KFileLocationSpec);
   1.230 +	
   1.231 +	RFileReadStream in;
   1.232 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.233 +	testSkipL(in);
   1.234 +	testEofL(in);
   1.235 +	in.Close();
   1.236 +//
   1.237 +	test.Next(_L("Skipping using a single big transfer"));
   1.238 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.239 +	in.ReadL(KTestTotal);
   1.240 +	testEofL(in);
   1.241 +	in.Close();
   1.242 +	}
   1.243 +/**
   1.244 +@SYMTestCaseID          SYSLIB-STORE-CT-1169
   1.245 +@SYMTestCaseDesc	    Copying from one file stream to another test
   1.246 +@SYMTestPriority 	    High
   1.247 +@SYMTestActions  	    Attempt for copying using small transfers.
   1.248 +                        Attempt for copying until end of file.
   1.249 +						Attempt for end of file error
   1.250 +@SYMTestExpectedResults Test must not fail
   1.251 +@SYMREQ                 REQ0000
   1.252 +*/
   1.253 +LOCAL_C void testCopyL()
   1.254 +	{
   1.255 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1169 Copying using small transfers "));
   1.256 +
   1.257 +	TParsePtrC parse(KFileLocationSpec);
   1.258 +	
   1.259 +	RFileReadStream in;
   1.260 +	RFileWriteStream out;
   1.261 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.262 +	test(out.Replace(TheFs,TheTempFile,EFileWrite)==KErrNone);
   1.263 +	testCopyL(out,in);
   1.264 +	testEofL(in);
   1.265 +	out.CommitL();
   1.266 +	out.Close();
   1.267 +	in.Close();
   1.268 +	test(in.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   1.269 +	testReadL(in);
   1.270 +	testEofL(in);
   1.271 +	in.Close();
   1.272 +//
   1.273 +	test.Next(_L("Copying using a single big transfer"));
   1.274 +	test(out.Replace(TheFs,TheTempFile,EFileWrite)==KErrNone);
   1.275 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.276 +	in.ReadL(out,KTestTotal);
   1.277 +	testEofL(in);
   1.278 +	in.Close();
   1.279 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.280 +	out.WriteL(in,KTestTotal);
   1.281 +	testEofL(in);
   1.282 +	in.Close();
   1.283 +	out.CommitL();
   1.284 +	out.Close();
   1.285 +	test(in.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   1.286 +	testReadL(in);
   1.287 +	testReadL(in);
   1.288 +	testEofL(in);
   1.289 +	in.Close();
   1.290 +//
   1.291 +	test.Next(_L("Copying until end of file"));
   1.292 +	test(out.Replace(TheFs,TheTempFile,EFileWrite)==KErrNone);
   1.293 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.294 +	in.ReadL(out);
   1.295 +	testEofL(in);
   1.296 +	in.Close();
   1.297 +	test(in.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
   1.298 +	out.WriteL(in);
   1.299 +	testEofL(in);
   1.300 +	in.Close();
   1.301 +	out.CommitL();
   1.302 +	out.Close();
   1.303 +	test(in.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
   1.304 +	testReadL(in);
   1.305 +	testReadL(in);
   1.306 +	testEofL(in);
   1.307 +	in.Close();
   1.308 +	}
   1.309 +
   1.310 +//
   1.311 +// Prepare the test directory.
   1.312 +//
   1.313 +LOCAL_C void setupTestDirectory()
   1.314 +    {
   1.315 +	TInt r=TheFs.Connect();
   1.316 +	test(r==KErrNone);
   1.317 +//
   1.318 +	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   1.319 +	TParse parse;
   1.320 +	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   1.321 +	
   1.322 +	r=TheFs.MkDir(parse.DriveAndPath());
   1.323 +	test(r==KErrNone||r==KErrAlreadyExists);
   1.324 +	r=TheFs.SetSessionPath(parse.DriveAndPath());
   1.325 +	test(r==KErrNone);
   1.326 +	}
   1.327 +
   1.328 +//
   1.329 +// Initialise the cleanup stack.
   1.330 +//
   1.331 +LOCAL_C void setupCleanup()
   1.332 +    {
   1.333 +	TheTrapCleanup=CTrapCleanup::New();
   1.334 +	test(TheTrapCleanup!=NULL);
   1.335 +	TRAPD(r,\
   1.336 +		{\
   1.337 +		for (TInt i=KTestCleanupStack;i>0;i--)\
   1.338 +			CleanupStack::PushL((TAny*)1);\
   1.339 +		test(r==KErrNone);\
   1.340 +		CleanupStack::Pop(KTestCleanupStack);\
   1.341 +		});
   1.342 +	test(r==KErrNone);
   1.343 +	}
   1.344 +
   1.345 +LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   1.346 +	{
   1.347 +	RFs fsSession;
   1.348 +	TInt err = fsSession.Connect();
   1.349 +	if(err == KErrNone)
   1.350 +		{
   1.351 +		TEntry entry;
   1.352 +		if(fsSession.Entry(aFullName, entry) == KErrNone)
   1.353 +			{
   1.354 +			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   1.355 +			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   1.356 +			if(err != KErrNone)
   1.357 +				{
   1.358 +				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   1.359 +				}
   1.360 +			err = fsSession.Delete(aFullName);
   1.361 +			if(err != KErrNone)
   1.362 +				{
   1.363 +				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   1.364 +				}
   1.365 +			}
   1.366 +		fsSession.Close();
   1.367 +		}
   1.368 +	else
   1.369 +		{
   1.370 +		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   1.371 +		}
   1.372 +	}
   1.373 +
   1.374 +class RTestReadStream : public RReadStream
   1.375 +	{
   1.376 +public:
   1.377 +	RTestReadStream(MStreamBuf* aSource) :
   1.378 +		RReadStream(aSource)
   1.379 +		{
   1.380 +		}
   1.381 +	void Attach(MStreamBuf* aSource)
   1.382 +		{
   1.383 +		RReadStream::Attach(aSource);
   1.384 +		}
   1.385 +	void Detach()
   1.386 +		{
   1.387 +		RReadStream::Detach();
   1.388 +		}
   1.389 +	};
   1.390 +
   1.391 +class RTestWriteStream : public RWriteStream
   1.392 +	{
   1.393 +public:
   1.394 +	RTestWriteStream(MStreamBuf* aSink) :
   1.395 +		RWriteStream(aSink)
   1.396 +		{
   1.397 +		}
   1.398 +	void Attach(MStreamBuf* aSink)
   1.399 +		{
   1.400 +		RWriteStream::Attach(aSink);
   1.401 +		}
   1.402 +	void Detach()
   1.403 +		{
   1.404 +		RWriteStream::Detach();
   1.405 +		}
   1.406 +	};
   1.407 +
   1.408 +/**
   1.409 +@SYMTestCaseID          PDS-STORE-CT-4064
   1.410 +@SYMTestCaseDesc        RReadStream, RWriteStream, Pop() and Detach() test.
   1.411 +@SYMTestActions			The test calls Pop() and Detach() methods of RReadStream and RWriteStream classes.         
   1.412 +@SYMTestPriority        High
   1.413 +@SYMTestExpectedResults Test must not fail
   1.414 +*/
   1.415 +void StreamDetachTestL()
   1.416 +	{
   1.417 +	test.Next(_L("@SYMTestCaseID:PDS-STORE-CT-4064: RReadStream, RWriteStream, Pop() and Detach() test"));
   1.418 +	
   1.419 +	TBuf8<100> buf;
   1.420 +	TDesBuf desBuf;
   1.421 +	desBuf.Set(buf);
   1.422 +	MStreamBuf* mbuf = &desBuf; 
   1.423 +	
   1.424 +	_LIT8(KStr, "1234567890");
   1.425 +	
   1.426 +	RTestWriteStream wstrm(mbuf);
   1.427 +	wstrm.PushL();
   1.428 +	wstrm.Detach();
   1.429 +	wstrm.Attach(mbuf);
   1.430 +	TRAPD(err, wstrm.WriteL(KStr));
   1.431 +	test(err == KErrNone);
   1.432 +	TRAP(err, wstrm.CommitL());
   1.433 +	test(err == KErrNone);
   1.434 +	wstrm.Pop();
   1.435 +	wstrm.Close();
   1.436 +
   1.437 +	RTestReadStream rstrm(mbuf);
   1.438 +	rstrm.PushL();
   1.439 +	rstrm.Detach();
   1.440 +	rstrm.Attach(mbuf);
   1.441 +	TBuf8<100> buf2;
   1.442 +	TRAP(err, rstrm.ReadL(buf2, KStr().Length()));
   1.443 +	test(err == KErrNone);
   1.444 +	rstrm.Pop();
   1.445 +	rstrm.Close();
   1.446 +	
   1.447 +	test(KStr() == buf2);
   1.448 +	}
   1.449 +
   1.450 +//
   1.451 +// Test file-based streams.
   1.452 +//
   1.453 +GLDEF_C TInt E32Main()
   1.454 +    {
   1.455 +	test.Title();
   1.456 +	setupTestDirectory();
   1.457 +	setupCleanup();
   1.458 +	__UHEAP_MARK;
   1.459 +//
   1.460 +	test.Start(_L("Test file-based streams"));
   1.461 +	TRAPD(r,testWriteL());
   1.462 +	test(r==KErrNone);
   1.463 +	TRAP(r,testReadL());
   1.464 +	test(r==KErrNone);
   1.465 +	TRAP(r,testSkipL());
   1.466 +	test(r==KErrNone);
   1.467 +	TRAP(r,testCopyL());
   1.468 +	test(r==KErrNone);
   1.469 +	TRAP(r, StreamDetachTestL());
   1.470 +	test(r==KErrNone);
   1.471 +
   1.472 +	//deletion of data files must be before call to .End() - DEF047652
   1.473 +	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
   1.474 +	TParse parse;
   1.475 +	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
   1.476 +	::DeleteDataFile(parse.FullName());
   1.477 +
   1.478 +	test.End();
   1.479 +//
   1.480 +	__UHEAP_MARKEND;
   1.481 +
   1.482 +	delete TheTrapCleanup;
   1.483 +	if (TheFs.Delete(TheTempFile)!=KErrNone)
   1.484 +		test.Panic(_L("Deleting temp file"));
   1.485 +	TheFs.Close();
   1.486 +	test.Close();
   1.487 +	return 0;
   1.488 +    }
   1.489 +