lowlevellibsandfws/apputils/tsrc/T_RscPanic.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2006-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 // Tests RResourceFile/CResourceFile classes - panic/leave tests
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <bautils.h>
       
    20 #include <barsc.h>
       
    21 #include <barsc2.h>
       
    22 #include <barsread2.h>
       
    23 
       
    24 LOCAL_D RTest test(_L("T_RSCPANIC"));
       
    25 LOCAL_D RFs TheFs;
       
    26 _LIT8(KRscFileHeaderData, "0123456789ABCDEF");
       
    27 _LIT(KPanicThread,"panicThread");
       
    28 
       
    29 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
       
    30 	{
       
    31 	// make sure the file is read/write
       
    32 	TInt err = TheFs.SetAtt(aFullName, 0, KEntryAttReadOnly);
       
    33 	if(err != KErrNone)
       
    34 		{
       
    35 		RDebug::Print(_L("error changing attributes file = %d"),err);
       
    36 		}
       
    37 	// delete the file
       
    38 	err = BaflUtils::DeleteFile(TheFs, aFullName);
       
    39 	if(err != KErrNone)
       
    40 		{
       
    41 		RDebug::Print(_L("error deleting file = %d"),err);
       
    42 		}
       
    43 	}
       
    44 
       
    45 LOCAL_C TInt FileSizeL(const TDesC& aFileName)
       
    46 	{
       
    47 	RFile file;
       
    48 	User::LeaveIfError(file.Open(TheFs, aFileName, EFileRead));
       
    49 	CleanupClosePushL(file);
       
    50 	TInt size = 0;
       
    51 	User::LeaveIfError(file.Size(size));
       
    52 	CleanupStack::PopAndDestroy(&file);
       
    53 	return size;
       
    54 	}
       
    55 
       
    56 LOCAL_C void CreateFileFromL(const TDesC& aDestFileName, const TDesC& aSrcFileName)
       
    57 	{
       
    58 	RFile destFile;
       
    59 	RFile srcFile;
       
    60 
       
    61 	CleanupClosePushL(destFile);
       
    62 	CleanupClosePushL(srcFile);
       
    63 
       
    64 	BaflUtils::DeleteFile(TheFs, aDestFileName);
       
    65 	User::LeaveIfError(destFile.Create(TheFs, aDestFileName, EFileRead | EFileWrite));
       
    66 
       
    67 	User::LeaveIfError(srcFile.Open(TheFs, aSrcFileName, EFileRead));
       
    68 	TInt size = 0;
       
    69 	User::LeaveIfError(srcFile.Size(size));
       
    70 	HBufC8* buf = HBufC8::NewMaxLC(size);
       
    71 	TPtr8 ptr = buf->Des();
       
    72 	srcFile.Read(ptr);
       
    73 
       
    74 	destFile.Write(KRscFileHeaderData);
       
    75 	destFile.Write(ptr);
       
    76 
       
    77 	CleanupStack::PopAndDestroy(buf);
       
    78 	CleanupStack::PopAndDestroy(&srcFile);
       
    79 	CleanupStack::PopAndDestroy(&destFile);
       
    80 	}
       
    81 
       
    82 class TestRsc
       
    83 	{
       
    84 public:
       
    85 	void TestOpenReadL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize);
       
    86 //private:
       
    87 	void TestReadL(CResourceFile* aRscFile);
       
    88     };
       
    89 
       
    90 void TestRsc::TestOpenReadL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize)
       
    91 	{
       
    92 	test.Next(aTitle);
       
    93 	CResourceFile* rsc = CResourceFile::NewL(TheFs, aFileName, aFileOffset, aFileSize);
       
    94 	CleanupStack::PushL(rsc);
       
    95 	TestReadL(rsc);
       
    96 	CleanupStack::PopAndDestroy(rsc);
       
    97 	}
       
    98 
       
    99 void TestRsc::TestReadL(CResourceFile* aRscFile)
       
   100 	{
       
   101 	RResourceReader resourceReader;
       
   102 	const TInt rsc_index[] = {2, 3, 4, 5, 6, 7, 8, 9};
       
   103 	for(TInt i=0;i<TInt(sizeof(rsc_index)/sizeof(rsc_index[0]));i++)
       
   104 		{
       
   105 		const TInt max_size = 10;
       
   106 		for(TInt j=0;j<max_size;j++)
       
   107 			{
       
   108 			resourceReader.OpenLC(aRscFile, rsc_index[i]);
       
   109 			TUint8 temp[max_size + 1];
       
   110 			resourceReader.ReadL(temp, j);
       
   111 			CleanupStack::PopAndDestroy(1, &resourceReader);
       
   112 			}
       
   113 		}
       
   114     }
       
   115 
       
   116 /**
       
   117 @SYMTestCaseID          SYSLIB-BAFL-CT-0482
       
   118 @SYMTestCaseDesc        RResourceReader class test
       
   119 @SYMTestPriority        High
       
   120 @SYMTestActions         Tests for the opening of a file,should panic on invalid file
       
   121 @SYMTestExpectedResults Tests must not fail
       
   122 @SYMREQ                 REQ0000
       
   123 */
       
   124 LOCAL_C TInt DoTestL(const TDesC& aFileName, TUint aFileOffset = 0, TUint aFileSize = 0)
       
   125 	{
       
   126 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0482 "));
       
   127 	TestRsc t2;
       
   128 	TBuf<128> buf;
       
   129 	buf.Format(_L("Testing with invalid file,C class,file=%S"), &aFileName);
       
   130 	TRAPD(err, t2.TestOpenReadL(buf, aFileName, aFileOffset, aFileSize));
       
   131 	return err;
       
   132 	}
       
   133 
       
   134 TInt ThreadFunc(TAny*)
       
   135   	{
       
   136   	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   137   	TestRsc panicObj;
       
   138   	CResourceFile* rsc=NULL;
       
   139   	TRAPD(err,panicObj.TestReadL(rsc))//calls RResourceReader::OpenLC, with NULL as argument, hence panics
       
   140   	delete cleanup;
       
   141   	return err;
       
   142   	}
       
   143 
       
   144 /**
       
   145 @SYMTestCaseID          SYSLIB-BAFL-UT-1790
       
   146 @SYMTestCaseDesc        Testing panics on RResourceReader class(JustInTimeDebug is disabled)
       
   147 @SYMTestPriority        Low
       
   148 @SYMTestActions         Test that panics, when the condition inside __ASSERT is made false,by passing a NULL value to RResourceReader::OpenLC()
       
   149 @SYMTestExpectedResults Tests must panic
       
   150 @SYMREQ                 REQ0000
       
   151 */
       
   152 void PanicTest()
       
   153 	{
       
   154 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-UT-1790 "));
       
   155  	TRequestStatus threadStatus;
       
   156   	RThread thread;
       
   157   	TInt rc;
       
   158   	TBool jit;
       
   159   	jit = User::JustInTime();
       
   160   	User::SetJustInTime(EFalse);
       
   161 
       
   162   	rc = thread.Create(KPanicThread, ThreadFunc,
       
   163   		KDefaultStackSize, KMinHeapSize, KMinHeapSize*4,NULL);
       
   164   	test(KErrNone == rc);
       
   165 
       
   166   	thread.Logon(threadStatus);
       
   167   	thread.Resume();
       
   168   	User::WaitForRequest(threadStatus);
       
   169   	User::SetJustInTime(jit);
       
   170 
       
   171   	test(thread.ExitType() == EExitPanic);
       
   172   	thread.Close();
       
   173   	}
       
   174 
       
   175 /**
       
   176 @SYMTestCaseID          SYSLIB-BAFL-CT-0483
       
   177 @SYMTestCaseDesc        Tests for the functionality of RResourceFile/CResourceFile
       
   178 @SYMTestPriority        High
       
   179 @SYMTestActions         Tests for creation,deleting,opening of a resource file,
       
   180                         should panic on invalid file.
       
   181 @SYMTestExpectedResults Tests must not fail
       
   182 @SYMREQ                 REQ0000
       
   183 */
       
   184 LOCAL_C void DoTestsL()
       
   185     {
       
   186 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0483 "));
       
   187 	CleanupClosePushL(TheFs);
       
   188 	User::LeaveIfError(TheFs.Connect());
       
   189 
       
   190 	const TPtrC inv_rsc_files[] =
       
   191 		{
       
   192 		  _L("TRsc_Inv1.rsc")			//Zero sized file
       
   193 		, _L("TRsc_Inv2.rsc")
       
   194 		, _L("TRsc_Inv3.rsc")
       
   195 		, _L("TRsc_Inv4.rsc")
       
   196 		, _L("TRsc_Inv5.rsc")
       
   197 		, _L("TRsc_Inv6.rsc")
       
   198 		, _L("TRsc_Inv7.rsc")
       
   199 		, _L("TRsc_Inv8.rsc")
       
   200 		, _L("TRsc_Inv9.rsc")
       
   201 		, _L("TRscCalypso_Inv10.RSC")
       
   202 		, _L("TRscCalypso_Inv11.RSC")
       
   203 		, _L("TRscCalypso_Inv12.RSC")
       
   204 		, _L("TRscComprU_Inv13.RSC")
       
   205 		, _L("TRscComprU_Inv14.RSC")
       
   206 		, _L("TRscComprU_Inv15.RSC")
       
   207 		, _L("TRscCalypso_Inv16.RSC")
       
   208 		, _L("TRscCalypso_Inv17.RSC")
       
   209 		, _L("TRscNotExist.RSC")		//This file doesn't exist
       
   210 		};
       
   211 
       
   212 	TInt i;
       
   213 	TInt array_size = TInt(sizeof(inv_rsc_files)/sizeof(inv_rsc_files[0]));
       
   214 
       
   215 	//Z drive
       
   216 	_LIT(KZDir, "z:\\system\\data\\");
       
   217 	for(i=0;i<array_size;i++)
       
   218 		{
       
   219 		TBuf<64> path;
       
   220 		path += KZDir;
       
   221 		path += inv_rsc_files[i];
       
   222 		TInt err = DoTestL(path);
       
   223 		test(err != KErrNone);
       
   224 		}
       
   225 
       
   226 	//C drive
       
   227 	_LIT(KCDir, "c:\\");
       
   228 	for(i=0;i<array_size;i++)
       
   229 		{
       
   230 		TBuf<64> src_path;
       
   231 		src_path += KZDir;
       
   232 		src_path += inv_rsc_files[i];
       
   233 
       
   234 		TBuf<64> dest_path;
       
   235 		dest_path += KCDir;
       
   236 		dest_path += inv_rsc_files[i];
       
   237 
       
   238 		//Copy the file to C drive except the last which doesn't exist.
       
   239 		//Zero sized file doesn't exist on the assabet/lubbock platforms.
       
   240 		TInt err = KErrNone;
       
   241 		if(i > 0 && i < (array_size - 1))
       
   242 			{
       
   243 			err = BaflUtils::CopyFile(TheFs, src_path, dest_path);
       
   244 			test(err == KErrNone || err == KErrAlreadyExists);
       
   245 			}
       
   246 
       
   247 		err = DoTestL(dest_path);
       
   248 		DeleteDataFile(dest_path);
       
   249 		test(err != KErrNone);
       
   250 		}
       
   251 
       
   252 	//C drive - new rsc file format
       
   253 	for(i=1;i<(array_size-1);i++)
       
   254 		{
       
   255 		TBuf<64> dest_path;
       
   256 		TBuf<64> src_path;
       
   257 
       
   258 		dest_path += KCDir;
       
   259 		dest_path += _L("N_");
       
   260 		dest_path += inv_rsc_files[i];
       
   261 
       
   262 		src_path += KZDir;
       
   263 		src_path += inv_rsc_files[i];
       
   264 
       
   265 		CreateFileFromL(dest_path, src_path);
       
   266 
       
   267 		TInt err = DoTestL(dest_path, KRscFileHeaderData().Length(), FileSizeL(src_path));
       
   268 		DeleteDataFile(dest_path);
       
   269 		test(err != KErrNone);
       
   270 		}
       
   271 
       
   272 	//Tests that Raise panics
       
   273 	PanicTest();
       
   274 
       
   275 	CleanupStack::PopAndDestroy(1, &TheFs);
       
   276     }
       
   277 
       
   278 GLDEF_C TInt E32Main()
       
   279 	{
       
   280     __UHEAP_MARK;
       
   281     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   282 	test.Title();
       
   283 	test.Start(_L("Testing RResourceFile & CResourceFile panics/leaves"));
       
   284     TRAPD(err, DoTestsL());
       
   285     test.Printf(_L("Error code is %d\n"), err);
       
   286     test(err == KErrNone);
       
   287     test.Next(_L("/n"));
       
   288 	test.End();
       
   289     test.Close();
       
   290     delete cleanup;
       
   291     __UHEAP_MARKEND;
       
   292 	return 0;
       
   293     }