persistentstorage/store/TSTOR/t_storstrm.cpp
changeset 0 08ec8eefde2f
child 55 44f437012c90
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 1998-2009 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 <s32mem.h>
       
    17 #include <s32file.h>
       
    18 #include <e32test.h>
       
    19 
       
    20 const TInt KTestCleanupStack=0x20;
       
    21 const TInt KTestExpandSize=0x20;
       
    22 const TInt KTestGranularity=0x02;
       
    23 
       
    24 // This is a path specification and should not be used as is
       
    25 _LIT(KFileLocationSpec, "Z:\\STOR-TST\\T_STRM.DAT");
       
    26 
       
    27 LOCAL_D RTest test(_L("t_storstrm"));
       
    28 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    29 LOCAL_D RFs TheFs;
       
    30 LOCAL_D CFileStore* TheStore;
       
    31 LOCAL_D CBufStore* TheBufStore;
       
    32 LOCAL_D RStoreWriteStream TheSink;
       
    33 LOCAL_D RStoreReadStream TheSource;
       
    34 
       
    35 class THexEncodeFilter : public TStreamFilter
       
    36 	{
       
    37 public:
       
    38 	void Set(MStreamBuf* aBuf,TInt aMode);
       
    39 private:
       
    40 	TInt Capacity(TInt aMaxLength);
       
    41 	TInt FilterL(TAny* aPtr,TInt aMaxLength,const TUint8*& aFrom,const TUint8* anEnd);
       
    42 	void DoSynchL();
       
    43 //
       
    44 	static TText8 HexDigit(TUint aNibble);
       
    45 	static TText8 HexMSN(TUint aByte);
       
    46 	static TText8 HexLSN(TUint aByte);
       
    47 private:
       
    48 	TChar iSpare;
       
    49 	};
       
    50 
       
    51 class RHexDumpStream : public RWriteStream
       
    52 	{
       
    53 public:
       
    54 	RHexDumpStream() {}
       
    55 	RHexDumpStream(RWriteStream& aStream);
       
    56 	void Open(RWriteStream& aStream);
       
    57 private:
       
    58 	THexEncodeFilter iFilter;
       
    59 	};
       
    60 class RHexLoadStream : public RReadStream
       
    61 	{
       
    62 public:
       
    63 	RHexLoadStream() {}
       
    64 	RHexLoadStream(RReadStream& aStream);
       
    65 	void Open(RReadStream& aStream);
       
    66 private:
       
    67 	THexEncodeFilter iFilter;
       
    68 	};
       
    69 
       
    70 /**
       
    71 @SYMTestCaseID          SYSLIB-STORE-CT-1205
       
    72 @SYMTestCaseDesc	    Streaming in and out tests
       
    73 @SYMTestPriority 	    High
       
    74 @SYMTestActions  	    Tests for copying and filtering
       
    75 @SYMTestExpectedResults Test must not fail
       
    76 @SYMREQ                 REQ0000
       
    77 */
       
    78 LOCAL_C void testFixL(CArrayFix< TBuf<16> >& aFix)
       
    79 	{
       
    80 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1205 Test streaming in and out "));
       
    81 	TBuf<16> aa(_L("aa"));
       
    82 	aFix.AppendL(aa);
       
    83 	TBuf<16> bb(_L("bbbb"));
       
    84 	aFix.AppendL(bb);
       
    85 	TStreamId id=TheSink.CreateL(*TheStore);
       
    86 	TheSink<<aFix;
       
    87 	TBuf<16> cc(_L("cccccc"));
       
    88 	aFix.AppendL(cc);
       
    89 	TheSink.Close();
       
    90 	id=TheSink.CreateL(*TheStore);
       
    91 	TheSink<<aFix;
       
    92 	TheSink.Close();
       
    93 	TheStore->SetRootL(id);
       
    94 	TheStore->CommitL();
       
    95 	aFix.Reset();
       
    96 	TheSource.OpenL(*TheStore,TheStore->Root());
       
    97 	TheSource>>aFix;
       
    98 	TheSource.Close();
       
    99 	test(aFix[0]==aa&&aFix[1]==bb&&aFix[2]==cc);
       
   100 	test.Printf(_L("%S %S %S\n"),&aa,&bb,&cc);
       
   101 	test.Printf(_L("%S %S %S\n"),&aFix[0],&aFix[1],&aFix[2]);
       
   102 //
       
   103 // Test copying and filtering.
       
   104 //
       
   105 	CBufSeg* bufp=CBufSeg::NewL(KTestExpandSize);
       
   106 	CleanupStack::PushL(bufp);
       
   107 //
       
   108 	TheSource.OpenL(*TheStore,id);
       
   109 	id=TheSink.CreateL(*TheStore);
       
   110 //
       
   111 	RBufWriteStream bout(*bufp);
       
   112 	RHexDumpStream hout(bout);
       
   113 	hout.WriteL(TheSource,10);
       
   114 	RHexLoadStream hin(TheSource);
       
   115 	hin.ReadL(bout,10);
       
   116 	bout.CommitL();
       
   117 //
       
   118 	RBufReadStream bin(*bufp);
       
   119 	bin.ReadL(TheSink);
       
   120 //
       
   121 	TheSink.Close();
       
   122 	TheSource.Close();
       
   123 //
       
   124 	CleanupStack::PopAndDestroy();
       
   125 	
       
   126 	}
       
   127 
       
   128 /**
       
   129 @SYMTestCaseID          PDS-STORE-CT-4018
       
   130 @SYMTestCaseDesc	    Testing RStoreWriteStream::Append API
       
   131 @SYMTestPriority 	    High
       
   132 @SYMTestActions  	    Open file for append, add data to Store
       
   133 @SYMTestExpectedResults Data is succesfully added to store
       
   134 @SYMDEF                 DEF135804
       
   135 */
       
   136 LOCAL_C void testAppendL()
       
   137 	{
       
   138 	test.Next(_L("PDS-STORE-CT-4018: Testing Append API"));
       
   139 	TheBufStore=CBufStore::NewLC(KTestExpandSize);
       
   140 	
       
   141 	//TStreamId id=TheSink.CreateL(*TheBufStore);
       
   142 	TStreamId id = TheBufStore->ExtendL();
       
   143 	TRAPD(r, TheSink.AppendL(*TheBufStore, id));
       
   144 	test(r == KErrNone);
       
   145 	TBuf<16> inbuf(_L("appendL"));
       
   146 	TheSink<<inbuf;
       
   147 	TheSink.CommitL();
       
   148 	TheSink.Close();
       
   149 	
       
   150 	
       
   151 	TBuf<16> outbuf;
       
   152 	TheSource.OpenL(*TheBufStore,id);
       
   153 	TheSource>>outbuf;
       
   154 	TheSource.Close();
       
   155 	test(inbuf==outbuf);
       
   156 	
       
   157 	test.Printf(_L("Write Buf: %S\n"),&inbuf);
       
   158 	test.Printf(_L("Read Buf: %S\n"),&outbuf);
       
   159 	
       
   160 	CleanupStack::PopAndDestroy(TheBufStore);
       
   161 	
       
   162 	}
       
   163 
       
   164 RHexDumpStream::RHexDumpStream(RWriteStream& aStream)
       
   165 	{
       
   166 	Open(aStream);
       
   167 	}
       
   168 
       
   169 void RHexDumpStream::Open(RWriteStream& aStream)
       
   170 	{
       
   171 	iFilter.Set(aStream.Sink(),THexEncodeFilter::EWrite);
       
   172 	RWriteStream::Attach(&iFilter);
       
   173 	}
       
   174 
       
   175 RHexLoadStream::RHexLoadStream(RReadStream& aStream)
       
   176 	{
       
   177 	Open(aStream);
       
   178 	}
       
   179 
       
   180 void RHexLoadStream::Open(RReadStream& aStream)
       
   181 	{
       
   182 	iFilter.Set(aStream.Source(),THexEncodeFilter::ERead);
       
   183 	RReadStream::Attach(&iFilter);
       
   184 	}
       
   185 
       
   186 void THexEncodeFilter::Set(MStreamBuf* aBuf,TInt aMode)
       
   187 	{
       
   188 	TStreamFilter::Set(aBuf,aMode);
       
   189 	iSpare=TChar(0);
       
   190 	}
       
   191 
       
   192 TInt THexEncodeFilter::Capacity(TInt aMaxLength)
       
   193 	{
       
   194 	__ASSERT_DEBUG(aMaxLength>0,User::Panic(_L("aargh!"),0));
       
   195 	return (iSpare.Eos()?aMaxLength+1:aMaxLength)>>1;
       
   196 	}
       
   197 
       
   198 TInt THexEncodeFilter::FilterL(TAny* aPtr,TInt aMaxLength,const TUint8*& aFrom,const TUint8* anEnd)
       
   199 	{
       
   200 	__ASSERT_DEBUG(aMaxLength>0,User::Panic(_L("aargh!"),0));
       
   201 	TPtr8 hex((TText8*)aPtr,aMaxLength);
       
   202 	const TUint8* bin=aFrom;
       
   203 	if (!iSpare.Eos())
       
   204 		{
       
   205 		hex.Append(iSpare);
       
   206 		aMaxLength--;
       
   207 		}
       
   208 	TInt n=Min(aMaxLength>>1,anEnd-bin);
       
   209 	for (const TUint8* end=bin+n;bin<end;bin++)
       
   210 		{
       
   211 		TUint8 byte=*bin;
       
   212 		hex.Append(HexMSN(byte));
       
   213 		hex.Append(HexLSN(byte));
       
   214 		}
       
   215 	if (aMaxLength>(n<<1)&&bin<anEnd)
       
   216 		{
       
   217 		TUint8 byte=*bin++;
       
   218 		hex.Append(HexMSN(byte));
       
   219 		iSpare=HexLSN(byte);
       
   220 		}
       
   221 	else
       
   222 		iSpare=TChar(0);
       
   223 	__ASSERT_DEBUG(hex.Length()==hex.MaxLength()||bin==anEnd,User::Panic(_L("aargh!"),0));
       
   224 	aFrom=bin;
       
   225 	return hex.Length();
       
   226 	}
       
   227 
       
   228 void THexEncodeFilter::DoSynchL()
       
   229 	{
       
   230 	if (IsCommitted())
       
   231 		return;
       
   232 //
       
   233 	if (!iSpare.Eos())
       
   234 		{
       
   235 		TText8 c=TText8(iSpare);
       
   236 		iSpare=TChar(0);
       
   237 		EmitL(&c,1);
       
   238 		}
       
   239 	TStreamFilter::DoSynchL();
       
   240 	}
       
   241 
       
   242 TText8 THexEncodeFilter::HexDigit(TUint aNibble)
       
   243 	{
       
   244 	return TText8(aNibble+(aNibble<10?'0':('a'-10)));
       
   245 	}
       
   246 
       
   247 TText8 THexEncodeFilter::HexMSN(TUint aByte)
       
   248 	{
       
   249 	return HexDigit(aByte>>4);
       
   250 	}
       
   251 
       
   252 TText8 THexEncodeFilter::HexLSN(TUint aByte)
       
   253 	{
       
   254 	return HexDigit(aByte&0x0f);
       
   255 	}
       
   256 
       
   257 LOCAL_C void doMainL()
       
   258 	{
       
   259 	test.Start(_L("Creating Store"));
       
   260 	TParsePtrC parse(KFileLocationSpec);
       
   261 	
       
   262 //	TheStore=CBufStore::NewLC(KTestExpandSize);
       
   263 	TheStore=CDirectFileStore::ReplaceLC(TheFs,parse.NameAndExt(),EFileRead|EFileWrite);
       
   264 	TheStore->SetTypeL(TUidType(KDirectFileStoreLayoutUid,TUid::Uid('H'|('E'<<8)|('X'<<16))));
       
   265 
       
   266 	test.Next(_L("Creating CArrayFixFlat"));
       
   267 	CArrayFixFlat< TBuf<16> >* pFixFlat=new(ELeave) CArrayFixFlat< TBuf<16> >(KTestGranularity);
       
   268 	CleanupStack::PushL(pFixFlat);
       
   269 	testFixL(*pFixFlat);
       
   270 	
       
   271 	testAppendL();
       
   272 	
       
   273 	CleanupStack::PopAndDestroy(2,TheStore);
       
   274 	}
       
   275 
       
   276 //
       
   277 // Prepare the test directory.
       
   278 //
       
   279 LOCAL_C void setupTestDirectory()
       
   280     {
       
   281 	TInt r=TheFs.Connect();
       
   282 	test(r==KErrNone);
       
   283 //
       
   284 	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
       
   285 	TParse parse;
       
   286 	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
       
   287 	
       
   288 	r=TheFs.MkDir(parse.DriveAndPath());
       
   289 	test(r==KErrNone||r==KErrAlreadyExists);
       
   290 	r=TheFs.SetSessionPath(parse.DriveAndPath());
       
   291 	test(r==KErrNone);
       
   292 	}
       
   293 
       
   294 //
       
   295 // Initialise the cleanup stack.
       
   296 //
       
   297 LOCAL_C void setupCleanup()
       
   298     {
       
   299 	TheTrapCleanup=CTrapCleanup::New();
       
   300 	test(TheTrapCleanup!=NULL);
       
   301 	TRAPD(r,\
       
   302 		{\
       
   303 		for (TInt i=KTestCleanupStack;i>0;i--)\
       
   304 			CleanupStack::PushL((TAny*)1);\
       
   305 		test(r==KErrNone);\
       
   306 		CleanupStack::Pop(KTestCleanupStack);\
       
   307 		});
       
   308 	test(r==KErrNone);
       
   309 	}
       
   310 
       
   311 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
       
   312 	{
       
   313 	RFs fsSession;
       
   314 	TInt err = fsSession.Connect();
       
   315 	if(err == KErrNone)
       
   316 		{
       
   317 		TEntry entry;
       
   318 		if(fsSession.Entry(aFullName, entry) == KErrNone)
       
   319 			{
       
   320 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
       
   321 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
       
   322 			if(err != KErrNone)
       
   323 				{
       
   324 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
       
   325 				}
       
   326 			err = fsSession.Delete(aFullName);
       
   327 			if(err != KErrNone)
       
   328 				{
       
   329 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
       
   330 				}
       
   331 			}
       
   332 		fsSession.Close();
       
   333 		}
       
   334 	else
       
   335 		{
       
   336 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
       
   337 		}
       
   338 	}
       
   339 
       
   340 //
       
   341 // Test the streaming framework.
       
   342 //
       
   343 GLDEF_C TInt E32Main()
       
   344     {
       
   345 	test.Title();
       
   346 	setupTestDirectory();
       
   347 	setupCleanup();
       
   348 	__UHEAP_MARK;
       
   349 //
       
   350 	TRAPD(r,doMainL());
       
   351 	test(r==KErrNone);
       
   352 
       
   353 	//deletion of data files must be before call to .End() - DEF047652
       
   354 	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
       
   355 	TParse parse;
       
   356 	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
       
   357 	::DeleteDataFile(parse.FullName());
       
   358 
       
   359 	test.End();
       
   360 //
       
   361 	__UHEAP_MARKEND;
       
   362 
       
   363 	delete TheTrapCleanup;
       
   364 	TheFs.Close();
       
   365 	test.Close();
       
   366 	return 0;
       
   367     }
       
   368