kerneltest/f32test/rofs/src/t_rofsfile.cpp
changeset 43 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 43:96e5fb8b040d
       
     1 // Copyright (c) 2002-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 the License "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 // Tests reading from files
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <e32std_private.h>
       
    20 #include <e32test.h>
       
    21 #include <f32file.h>
       
    22 #include "utl.h"
       
    23 #include "randgen.h"
       
    24 
       
    25 GLREF_D RFs TheFs;
       
    26 
       
    27 RTest	test( _L("T_ROFSFILE") );
       
    28 
       
    29 
       
    30 _LIT( KSpotFile1, "root.txt" );
       
    31 _LIT( KSpotFile2, "Dir1\\level1.txt" );
       
    32 _LIT( KSpotFile3, "Dir1\\Dir2\\level2.txt" );
       
    33 _LIT( KSpotFile4, "Dir1\\Dir2\\Dir3\\level3.txt" );
       
    34 _LIT( KSpotFile5, "Dir1\\Dir2\\Dir3\\Dir4\\level4.txt" );
       
    35 _LIT( KSpotFile6, "Dir1\\Dir2\\Dir3\\Dir4\\Dir5\\level5.txt" );
       
    36 _LIT( KSpotFile7, "Dir1\\Dir2\\Dir3\\Dir4\\Dir5\\Dir6\\level6.txt" );
       
    37 _LIT( KSpotFile8, "Dir1\\Dir2\\Dir3\\Dir4\\Dir5\\Dir6\\Dir7\\level7.txt" );
       
    38 _LIT( KSpotFile9, "DeepDir1\\DeepDir2\\DeepDir3\\DeepDir4\\DeepDir5\\DeepDir6\\DeepDir7\\DeepDir8\\DeepDir9\\DeepDir10\\DeepDir11\\DeepDir12\\DeepDir13\\DeepDir14\\file.txt" );
       
    39 _LIT( KSpotFile10, "Parent\\parfile.txt" );
       
    40 _LIT( KSpotFile11, "Parent\\SubDir1A\\subfileA.txt" );
       
    41 _LIT( KSpotFile12, "Mixed\\par1.txt" );
       
    42 _LIT( KSpotFile13, "Mixed\\SubDir1\\sub1.txt" );
       
    43 _LIT( KSpotFile14, "Mixed\\SubDir2\\sub2.txt" );
       
    44 _LIT( KSpotFile15, "Dir1\\level1_ext.txt" );
       
    45 
       
    46 
       
    47 _LIT( KSameDirFile1, "ReadTest\\A\\file1" );
       
    48 _LIT( KSameDirFile2, "ReadTest\\A\\file2" );
       
    49 _LIT( KSameDirFile3, "ReadTest\\A\\file3" );
       
    50 _LIT( KSameDirFile4, "ReadTest\\A\\file4" );
       
    51 const TInt KSameDirFileLength = 256;
       
    52 const TUint KSameDirFile1Seed = 0xEF1113BC;
       
    53 const TUint KSameDirFile2Seed = 0x04082195;
       
    54 const TUint KSameDirFile3Seed = 0xEC36D359;
       
    55 const TUint KSameDirFile4Seed = 0x07D4DAC2;
       
    56 
       
    57 _LIT( KSameNameFile1, "ReadTest\\B\\B1\\testfile" );
       
    58 _LIT( KSameNameFile2, "ReadTest\\B\\B2\\testfile" );
       
    59 _LIT( KSameNameFile3, "ReadTest\\B\\B3\\testfile" );
       
    60 _LIT( KSameNameFile4, "ReadTest\\B\\B4\\testfile" );
       
    61 const TInt KSameNameFileLength = 256;	// we will read this much of the file
       
    62 const TUint KSameNameFile1Seed = 0x8DA9AA5A;
       
    63 const TUint KSameNameFile2Seed = 0x735AA240;
       
    64 const TUint KSameNameFile3Seed = 0x42D4BF02;
       
    65 const TUint KSameNameFile4Seed = 0x47C728FB;
       
    66 
       
    67 _LIT( KRandomReadFile, "ReadTest\\C\\seektest" );
       
    68 const TInt KRandomReadFileSize = 17466;
       
    69 const TUint KRandomReadFileSeed = 0x8DA9AA5A;
       
    70 
       
    71 _LIT( KEofFile1, "ReadTest\\D\\eoftest1" );
       
    72 _LIT( KEofFile2, "ReadTest\\D\\eoftest2" );
       
    73 _LIT( KEofFile3, "ReadTest\\D\\eoftest3" );
       
    74 _LIT( KEofFile4, "ReadTest\\D\\eoftest4" );
       
    75 _LIT( KEofFile5, "ReadTest\\D\\eoftest5" );
       
    76 _LIT( KEofFile6, "ReadTest\\D\\eoftest6" );
       
    77 
       
    78 
       
    79 _LIT( KStreamFile1, "ReadTest\\E\\stream1" );
       
    80 _LIT( KStreamFile2, "ReadTest\\E\\stream2" );
       
    81 _LIT( KStreamFile3, "ReadTest\\E\\stream3" );
       
    82 _LIT( KStreamFile4, "ReadTest\\E\\stream4" );
       
    83 
       
    84 _LIT( KTestFileExt, "ext.txt" );
       
    85 
       
    86 const TUint KStreamReadSeed = 0x8DA9AA5A;
       
    87 
       
    88 _LIT( KDriveBase, " :\\" );
       
    89 
       
    90 
       
    91 const TInt KHello8FileSize = 5;
       
    92 _LIT8( KHello8FileContent, "hello" );
       
    93 
       
    94 
       
    95 // A large buffer for file reads
       
    96 LOCAL_D TBuf8<18000> gFileBuffer;
       
    97 
       
    98 
       
    99 LOCAL_C void SpotCheckFilesL(TInt aDriveToTest, TBool aExtension)
       
   100 //
       
   101 // Tests a few files for size & content. These are all the hello8.txt test file
       
   102 //
       
   103 	{
       
   104 	const TDesC* const fileArray[15] =
       
   105 		{
       
   106 		&KSpotFile1, &KSpotFile2, &KSpotFile3, &KSpotFile4, &KSpotFile5,
       
   107 		&KSpotFile6, &KSpotFile7, &KSpotFile8, &KSpotFile9, &KSpotFile10,
       
   108 		&KSpotFile11, &KSpotFile12, &KSpotFile13, &KSpotFile14, &KSpotFile15
       
   109 		};
       
   110 
       
   111 	test.Next( _L("Spot-check some files") );
       
   112 	TFileName name(KDriveBase);
       
   113 	name[0] = TText('A' + aDriveToTest);
       
   114 
       
   115 	TInt index =0;
       
   116 	if(aExtension) index =1;
       
   117 	for( TInt i = index; i < 14; i++ )
       
   118 		{
       
   119 		name.SetLength( 3 );	// trim back to drive specifier
       
   120 		if(aExtension &&(i==1))
       
   121 			name.Append( *fileArray[i+13] );
       
   122 		else
       
   123 			name.Append( *fileArray[i] );
       
   124 
       
   125 		test.Printf( _L("Opening file %S"), &name );
       
   126 		RFile file;
       
   127 		TInt r = file.Open( TheFs, name, EFileRead );
       
   128 		TEST_FOR_ERROR( r );
       
   129 
       
   130 		TInt fileSize;
       
   131 		r = file.Size( fileSize );
       
   132 		TEST_FOR_ERROR( r );
       
   133 		TEST_FOR_MATCH( fileSize, KHello8FileSize );
       
   134 
       
   135 		TBuf8<5> buf;
       
   136 		r = file.Read( buf );
       
   137 		TEST_FOR_ERROR( r );
       
   138 		TEST_FOR_MATCH( buf.Length(), KHello8FileSize );
       
   139 		test(buf == KHello8FileContent);
       
   140 		file.Close();
       
   141 		}
       
   142 	}
       
   143 
       
   144 
       
   145 LOCAL_C void FillRandomBuffer( TDes8& aDes, TRandomGenerator& aRand, TInt aLength )
       
   146 	{
       
   147 	aDes.SetLength( aLength );
       
   148 	TUint* ptr = (TUint*)aDes.Ptr();
       
   149 	while( aLength >= 4 )
       
   150 		{
       
   151 		TUint v = aRand.Next();
       
   152 		*ptr++ = v;
       
   153 		aLength -= 4;
       
   154 		}
       
   155 	if( aLength )
       
   156 		{
       
   157 		TUint v = aRand.Next();
       
   158 		TUint8* p8 = (TUint8*)ptr;
       
   159 		while( aLength )
       
   160 			{
       
   161 			*p8++ = (TUint8)(v & 0xFF);
       
   162 			v >>= 8;
       
   163 			--aLength;
       
   164 			}
       
   165 		}
       
   166 	}
       
   167 
       
   168 LOCAL_C TBool ValidateRandomBuffer( const TDes8& aDes, TUint aSeed )
       
   169 	{
       
   170 	const TUint* ptr = (const TUint*)aDes.Ptr();
       
   171 	TInt length = aDes.Length();
       
   172 	
       
   173 	TRandomGenerator rand;
       
   174 	rand.SetSeed( aSeed );
       
   175 	while( length >= 4 )
       
   176 		{
       
   177 		TUint v = rand.Next();
       
   178 		if( *ptr++ != v )
       
   179 			{
       
   180 			return EFalse;
       
   181 			}
       
   182 		length -= 4;
       
   183 		}
       
   184 	if( length )
       
   185 		{
       
   186 		TUint v = rand.Next();
       
   187 		TUint8* p8 = (TUint8*)ptr;
       
   188 		while( length )
       
   189 			{
       
   190 			if( *p8++ != (TUint8)(v & 0xFF) )
       
   191 				{
       
   192 				return EFalse;
       
   193 				}
       
   194 			v >>= 8;
       
   195 			--length;
       
   196 			}
       
   197 		}
       
   198 	return ETrue;
       
   199 	}
       
   200 
       
   201 
       
   202 
       
   203 LOCAL_C void TestFilesInSameDirectoryL(TInt aDriveToTest)
       
   204 	//
       
   205 	// Tests opening different files in the same directory
       
   206 	//
       
   207 	{
       
   208 	test.Next( _L("Testing files in same directory") );
       
   209 	
       
   210 	struct TTheFiles
       
   211 		{
       
   212 		RFile	iFile;
       
   213 		TRandomGenerator	iRand;
       
   214 		};
       
   215 	TTheFiles files[4];
       
   216 
       
   217 	TFileName name(KDriveBase);
       
   218 	name[0] = TText('A' + aDriveToTest);
       
   219 	
       
   220 	name.Append( KSameDirFile1 );
       
   221 	TInt r = files[0].iFile.Open( TheFs, name, EFileRead );
       
   222 	TEST_FOR_ERROR( r );
       
   223 
       
   224 	name.SetLength( 3 );
       
   225 	name.Append( KSameDirFile2 );
       
   226 	r = files[1].iFile.Open( TheFs, name, EFileRead );
       
   227 	TEST_FOR_ERROR( r );
       
   228 
       
   229 	name.SetLength( 3 );
       
   230 	name.Append( KSameDirFile3 );
       
   231 	r = files[2].iFile.Open( TheFs, name, EFileRead );
       
   232 	TEST_FOR_ERROR( r );
       
   233 
       
   234 	name.SetLength( 3 );
       
   235 	name.Append( KSameDirFile4 );
       
   236 	r = files[3].iFile.Open( TheFs, name, EFileRead );
       
   237 	TEST_FOR_ERROR( r );
       
   238 
       
   239 	const TInt KReadLen = KSameDirFileLength / 16;
       
   240 	TBuf8<KReadLen> readBuf;
       
   241 	TBuf8<KReadLen> randomBuf;
       
   242 
       
   243 	files[0].iRand.SetSeed( KSameDirFile1Seed );
       
   244 	files[1].iRand.SetSeed( KSameDirFile2Seed );
       
   245 	files[2].iRand.SetSeed( KSameDirFile3Seed );
       
   246 	files[3].iRand.SetSeed( KSameDirFile4Seed );
       
   247 
       
   248 	for( TInt fragment = 0; fragment < (KSameDirFileLength / KReadLen); ++fragment )
       
   249 		{
       
   250 		// read next fragment from each file and compare
       
   251 		for( TInt j = 0; j < 4; ++j )
       
   252 			{
       
   253 			FillRandomBuffer( randomBuf, files[j].iRand, KReadLen );
       
   254 			r = files[j].iFile.Read( readBuf );
       
   255 			TEST_FOR_ERROR( r );
       
   256 			TEST_FOR_MATCH( readBuf.Length(), KReadLen );
       
   257 			test( readBuf == randomBuf );
       
   258 			}
       
   259 		}
       
   260 	files[0].iFile.Close();
       
   261 	files[1].iFile.Close();
       
   262 	files[2].iFile.Close();
       
   263 	files[3].iFile.Close();
       
   264 	}
       
   265 
       
   266 
       
   267 LOCAL_C void TestFilesSameNameDifferentDirectoryL(TInt aDriveToTest)
       
   268 	//
       
   269 	// Tests opening files with the same name but in different directories
       
   270 	//
       
   271 	{
       
   272 	test.Next( _L("Testing files with same name in different directories") );
       
   273 	
       
   274 	struct TTheFiles
       
   275 		{
       
   276 		RFile	iFile;
       
   277 		TRandomGenerator	iRand;
       
   278 		};
       
   279 	TTheFiles files[4];
       
   280 
       
   281 	TFileName name(KDriveBase);
       
   282 	name[0] = TText('A' + aDriveToTest);
       
   283 	
       
   284 	name.Append( KSameNameFile1 );
       
   285 	TInt r = files[0].iFile.Open( TheFs, name, EFileRead );
       
   286 	TEST_FOR_ERROR( r );
       
   287 
       
   288 	name.SetLength( 3 );
       
   289 	name.Append( KSameNameFile2 );
       
   290 	r = files[1].iFile.Open( TheFs, name, EFileRead );
       
   291 	TEST_FOR_ERROR( r );
       
   292 
       
   293 	name.SetLength( 3 );
       
   294 	name.Append( KSameNameFile3 );
       
   295 	r = files[2].iFile.Open( TheFs, name, EFileRead );
       
   296 	TEST_FOR_ERROR( r );
       
   297 
       
   298 	name.SetLength( 3 );
       
   299 	name.Append( KSameNameFile4 );
       
   300 	r = files[3].iFile.Open( TheFs, name, EFileRead );
       
   301 	TEST_FOR_ERROR( r );
       
   302 
       
   303 	const TInt KReadLen = KSameNameFileLength / 16;
       
   304 	TBuf8<KReadLen> readBuf;
       
   305 	TBuf8<KReadLen> randomBuf;
       
   306 
       
   307 	files[0].iRand.SetSeed( KSameNameFile1Seed );
       
   308 	files[1].iRand.SetSeed( KSameNameFile2Seed );
       
   309 	files[2].iRand.SetSeed( KSameNameFile3Seed );
       
   310 	files[3].iRand.SetSeed( KSameNameFile4Seed );
       
   311 
       
   312 	for( TInt fragment = 0; fragment < (KSameDirFileLength / KReadLen); ++fragment )
       
   313 		{
       
   314 		// read next fragment from each file and compare
       
   315 		for( TInt j = 0; j < 4; ++j )
       
   316 			{
       
   317 			FillRandomBuffer( randomBuf, files[j].iRand, KReadLen );
       
   318 			r = files[j].iFile.Read( readBuf );
       
   319 			TEST_FOR_ERROR( r );
       
   320 			TEST_FOR_MATCH( readBuf.Length(), KReadLen );
       
   321 			test( readBuf == randomBuf );
       
   322 			}
       
   323 		}
       
   324 	files[0].iFile.Close();
       
   325 	files[1].iFile.Close();
       
   326 	files[2].iFile.Close();
       
   327 	files[3].iFile.Close();
       
   328 	}
       
   329 
       
   330 
       
   331 
       
   332 
       
   333 LOCAL_C void TestRandomSeekL(TInt aDriveToTest)
       
   334 	//
       
   335 	// Tests reading from random positions in a file
       
   336 	//
       
   337 	{
       
   338 	test.Next( _L("Testing random read position") );
       
   339 	
       
   340 	TFileName name(KDriveBase);
       
   341 	name[0] = TText('A' + aDriveToTest);
       
   342 	
       
   343 	name.Append( KRandomReadFile );
       
   344 	RFile file;
       
   345 	TInt r = file.Open( TheFs, name, EFileRead );
       
   346 	TEST_FOR_ERROR( r );
       
   347 
       
   348 	// Check file size
       
   349 	TInt fileSize;
       
   350 	r = file.Size( fileSize ), 
       
   351 	TEST_FOR_ERROR( r );
       
   352 	TEST_FOR_MATCH( fileSize, KRandomReadFileSize );
       
   353 
       
   354 	// Fill buffer with expected file contents
       
   355 	TRandomGenerator rand;
       
   356 	rand.SetSeed( KRandomReadFileSeed );
       
   357 	FillRandomBuffer( gFileBuffer, rand, KRandomReadFileSize );
       
   358 
       
   359 
       
   360 	// Read from random positions within the file
       
   361 	rand.SetSeed( 0xA6E320F1 );
       
   362 	const TInt KMaxRead = 256;
       
   363 	TBuf8<KMaxRead> buf;
       
   364 	const TInt maxRead = Min( fileSize, buf.MaxLength() );
       
   365 	for( TInt readCount = 0; readCount < 100; ++readCount )
       
   366 		{
       
   367 		TInt readLen = rand.Next() % (maxRead+1);
       
   368 		if( readLen > buf.MaxLength() )
       
   369 			{
       
   370 			readLen = buf.MaxLength();
       
   371 			}
       
   372 		TInt readPos = rand.Next() % (fileSize - readLen);
       
   373 
       
   374 		_LIT( KMessage, "Reading %d bytes from 0x%x" );
       
   375 		test.Printf( KMessage, readLen, readPos );
       
   376 		
       
   377 		const TUint8* pExpected = gFileBuffer.Ptr() + readPos;
       
   378 		r = file.Seek( ESeekStart, readPos );
       
   379 		TEST_FOR_ERROR( r );
       
   380 
       
   381 		r = file.Read( buf, readLen );
       
   382 		TEST_FOR_ERROR( r );
       
   383 		TEST_FOR_MATCH( buf.Length(), readLen );
       
   384 
       
   385 		const TUint8* pRead = buf.Ptr();
       
   386 		test( 0 == Mem::Compare( pExpected, readLen, pRead, readLen ) );
       
   387 		}
       
   388 	}
       
   389 
       
   390 
       
   391 LOCAL_C void TestEofReadL(TInt aDriveToTest)
       
   392 	//
       
   393 	// Test reading off the end of a file
       
   394 	//
       
   395 	{
       
   396 	test.Next( _L("Testing reading past EOF") );
       
   397 
       
   398 	
       
   399 	const TDesC* const fileArray[6] =
       
   400 		{
       
   401 		&KEofFile1, &KEofFile2, &KEofFile3, &KEofFile4, &KEofFile5, &KEofFile6
       
   402 		};
       
   403 
       
   404 	TFileName name(KDriveBase);
       
   405 	name[0] = TText('A' + aDriveToTest);
       
   406 
       
   407 	TRandomGenerator rand;
       
   408 	rand.SetSeed( 0x1001A005 );
       
   409 	for( TInt i = 0; i < 6; i++ )
       
   410 		{
       
   411 		name.SetLength( 3 );	// trim back to drive specifier
       
   412 		name.Append( *fileArray[i] );
       
   413 		test.Printf( _L("Opening file %S"), &name );
       
   414 		RFile file;
       
   415 		TInt r = file.Open( TheFs, name, EFileRead );
       
   416 		TEST_FOR_ERROR( r );
       
   417 		
       
   418 		TInt fileSize;
       
   419 		r = file.Size( fileSize ), 
       
   420 		TEST_FOR_ERROR( r );
       
   421 		test( fileSize > 0 );
       
   422 		
       
   423 		TInt readPos = fileSize - ( rand.Next() % 12);
       
   424 		if( readPos < 0 )
       
   425 			{
       
   426 			readPos = 0;
       
   427 			}
       
   428 		r = file.Seek( ESeekStart, readPos );
       
   429 		TEST_FOR_ERROR( r );
       
   430 		
       
   431 		TBuf8<32> buf;
       
   432 		r = file.Read( buf );
       
   433 		TEST_FOR_ERROR( r );
       
   434 		TEST_FOR_MATCH( buf.Length(), fileSize - readPos );
       
   435 		file.Close();
       
   436 		}
       
   437 	
       
   438 	}
       
   439 
       
   440 
       
   441 
       
   442 LOCAL_C void TestStreamReadL(TInt aDriveToTest)
       
   443 	//
       
   444 	// Reads the whole file into a buffer in a single read
       
   445 	// and validates the data
       
   446 	//
       
   447 	{
       
   448 	test.Next( _L("Testing whole file read") );
       
   449 
       
   450 	const TDesC* const fileArray[4] =
       
   451 		{
       
   452 		&KStreamFile1, &KStreamFile2, &KStreamFile3, &KStreamFile4
       
   453 		};
       
   454 	
       
   455 	TFileName name(KDriveBase);
       
   456 	name[0] = TText('A' + aDriveToTest);
       
   457 
       
   458 	for( TInt i = 0; i < 4; ++i )
       
   459 		{
       
   460 		name.SetLength(3);
       
   461 		name.Append( *fileArray[i] );
       
   462 
       
   463 		test.Printf( _L("... file %S"), &name );
       
   464 		
       
   465 		RFile file;
       
   466 		TInt r = file.Open( TheFs, name, EFileRead );
       
   467 		TEST_FOR_ERROR( r );
       
   468 
       
   469 		TInt fileSize;
       
   470 		r = file.Size( fileSize ), 
       
   471 		TEST_FOR_ERROR( r );
       
   472 		test( fileSize > 0 );
       
   473 
       
   474 		r = file.Read( gFileBuffer );
       
   475 		TEST_FOR_ERROR( r );
       
   476 		TEST_FOR_MATCH( fileSize, gFileBuffer.Length() );
       
   477 
       
   478 		test( ValidateRandomBuffer( gFileBuffer, KStreamReadSeed ) );
       
   479 
       
   480 		file.Close();
       
   481 		}
       
   482 	}
       
   483 
       
   484 //************************
       
   485 // Entry point
       
   486 
       
   487 void DoTestL(TInt aDriveToTest)
       
   488 	{
       
   489 	test.Title();
       
   490 	test.Start( _L("Testing ROFS file reading") );
       
   491 	
       
   492 	test.Printf( _L("Looking for ROFS extension\n"));
       
   493 	TBool extension = EFalse;
       
   494 	TFileName name(KDriveBase);
       
   495 	name[0] = TText('A' + aDriveToTest);
       
   496 
       
   497 	name.SetLength( 3 );	// trim back to drive specifier
       
   498 	name.Append( KTestFileExt );
       
   499 
       
   500 	RFile file;
       
   501 	test.Printf( _L("Opening file %S"), &name );
       
   502 	TInt r = file.Open( TheFs, name, EFileRead );
       
   503 	if(r==KErrNone)
       
   504 		{
       
   505 		extension=ETrue;
       
   506 		file.Close();
       
   507 		test.Printf( _L("ROFS extension found\n"));
       
   508 		}
       
   509 	else if(r==KErrNotFound)
       
   510 		{
       
   511 		test.Printf( _L("Not found, ROFS extension not present\n"));
       
   512 		}
       
   513 
       
   514 	SpotCheckFilesL(aDriveToTest, extension);
       
   515 	TestFilesInSameDirectoryL(aDriveToTest);
       
   516 	TestFilesSameNameDifferentDirectoryL(aDriveToTest);
       
   517 	TestRandomSeekL(aDriveToTest);
       
   518 	TestEofReadL(aDriveToTest);
       
   519 	TestStreamReadL(aDriveToTest);
       
   520 
       
   521 	test.End();
       
   522 	}