1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/tsrc/T_RscPanic.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,293 @@
1.4 +// Copyright (c) 2006-2009 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 +// Tests RResourceFile/CResourceFile classes - panic/leave tests
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32test.h>
1.22 +#include <bautils.h>
1.23 +#include <barsc.h>
1.24 +#include <barsc2.h>
1.25 +#include <barsread2.h>
1.26 +
1.27 +LOCAL_D RTest test(_L("T_RSCPANIC"));
1.28 +LOCAL_D RFs TheFs;
1.29 +_LIT8(KRscFileHeaderData, "0123456789ABCDEF");
1.30 +_LIT(KPanicThread,"panicThread");
1.31 +
1.32 +LOCAL_C void DeleteDataFile(const TDesC& aFullName)
1.33 + {
1.34 + // make sure the file is read/write
1.35 + TInt err = TheFs.SetAtt(aFullName, 0, KEntryAttReadOnly);
1.36 + if(err != KErrNone)
1.37 + {
1.38 + RDebug::Print(_L("error changing attributes file = %d"),err);
1.39 + }
1.40 + // delete the file
1.41 + err = BaflUtils::DeleteFile(TheFs, aFullName);
1.42 + if(err != KErrNone)
1.43 + {
1.44 + RDebug::Print(_L("error deleting file = %d"),err);
1.45 + }
1.46 + }
1.47 +
1.48 +LOCAL_C TInt FileSizeL(const TDesC& aFileName)
1.49 + {
1.50 + RFile file;
1.51 + User::LeaveIfError(file.Open(TheFs, aFileName, EFileRead));
1.52 + CleanupClosePushL(file);
1.53 + TInt size = 0;
1.54 + User::LeaveIfError(file.Size(size));
1.55 + CleanupStack::PopAndDestroy(&file);
1.56 + return size;
1.57 + }
1.58 +
1.59 +LOCAL_C void CreateFileFromL(const TDesC& aDestFileName, const TDesC& aSrcFileName)
1.60 + {
1.61 + RFile destFile;
1.62 + RFile srcFile;
1.63 +
1.64 + CleanupClosePushL(destFile);
1.65 + CleanupClosePushL(srcFile);
1.66 +
1.67 + BaflUtils::DeleteFile(TheFs, aDestFileName);
1.68 + User::LeaveIfError(destFile.Create(TheFs, aDestFileName, EFileRead | EFileWrite));
1.69 +
1.70 + User::LeaveIfError(srcFile.Open(TheFs, aSrcFileName, EFileRead));
1.71 + TInt size = 0;
1.72 + User::LeaveIfError(srcFile.Size(size));
1.73 + HBufC8* buf = HBufC8::NewMaxLC(size);
1.74 + TPtr8 ptr = buf->Des();
1.75 + srcFile.Read(ptr);
1.76 +
1.77 + destFile.Write(KRscFileHeaderData);
1.78 + destFile.Write(ptr);
1.79 +
1.80 + CleanupStack::PopAndDestroy(buf);
1.81 + CleanupStack::PopAndDestroy(&srcFile);
1.82 + CleanupStack::PopAndDestroy(&destFile);
1.83 + }
1.84 +
1.85 +class TestRsc
1.86 + {
1.87 +public:
1.88 + void TestOpenReadL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize);
1.89 +//private:
1.90 + void TestReadL(CResourceFile* aRscFile);
1.91 + };
1.92 +
1.93 +void TestRsc::TestOpenReadL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize)
1.94 + {
1.95 + test.Next(aTitle);
1.96 + CResourceFile* rsc = CResourceFile::NewL(TheFs, aFileName, aFileOffset, aFileSize);
1.97 + CleanupStack::PushL(rsc);
1.98 + TestReadL(rsc);
1.99 + CleanupStack::PopAndDestroy(rsc);
1.100 + }
1.101 +
1.102 +void TestRsc::TestReadL(CResourceFile* aRscFile)
1.103 + {
1.104 + RResourceReader resourceReader;
1.105 + const TInt rsc_index[] = {2, 3, 4, 5, 6, 7, 8, 9};
1.106 + for(TInt i=0;i<TInt(sizeof(rsc_index)/sizeof(rsc_index[0]));i++)
1.107 + {
1.108 + const TInt max_size = 10;
1.109 + for(TInt j=0;j<max_size;j++)
1.110 + {
1.111 + resourceReader.OpenLC(aRscFile, rsc_index[i]);
1.112 + TUint8 temp[max_size + 1];
1.113 + resourceReader.ReadL(temp, j);
1.114 + CleanupStack::PopAndDestroy(1, &resourceReader);
1.115 + }
1.116 + }
1.117 + }
1.118 +
1.119 +/**
1.120 +@SYMTestCaseID SYSLIB-BAFL-CT-0482
1.121 +@SYMTestCaseDesc RResourceReader class test
1.122 +@SYMTestPriority High
1.123 +@SYMTestActions Tests for the opening of a file,should panic on invalid file
1.124 +@SYMTestExpectedResults Tests must not fail
1.125 +@SYMREQ REQ0000
1.126 +*/
1.127 +LOCAL_C TInt DoTestL(const TDesC& aFileName, TUint aFileOffset = 0, TUint aFileSize = 0)
1.128 + {
1.129 + test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0482 "));
1.130 + TestRsc t2;
1.131 + TBuf<128> buf;
1.132 + buf.Format(_L("Testing with invalid file,C class,file=%S"), &aFileName);
1.133 + TRAPD(err, t2.TestOpenReadL(buf, aFileName, aFileOffset, aFileSize));
1.134 + return err;
1.135 + }
1.136 +
1.137 +TInt ThreadFunc(TAny*)
1.138 + {
1.139 + CTrapCleanup* cleanup=CTrapCleanup::New();
1.140 + TestRsc panicObj;
1.141 + CResourceFile* rsc=NULL;
1.142 + TRAPD(err,panicObj.TestReadL(rsc))//calls RResourceReader::OpenLC, with NULL as argument, hence panics
1.143 + delete cleanup;
1.144 + return err;
1.145 + }
1.146 +
1.147 +/**
1.148 +@SYMTestCaseID SYSLIB-BAFL-UT-1790
1.149 +@SYMTestCaseDesc Testing panics on RResourceReader class(JustInTimeDebug is disabled)
1.150 +@SYMTestPriority Low
1.151 +@SYMTestActions Test that panics, when the condition inside __ASSERT is made false,by passing a NULL value to RResourceReader::OpenLC()
1.152 +@SYMTestExpectedResults Tests must panic
1.153 +@SYMREQ REQ0000
1.154 +*/
1.155 +void PanicTest()
1.156 + {
1.157 + test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-UT-1790 "));
1.158 + TRequestStatus threadStatus;
1.159 + RThread thread;
1.160 + TInt rc;
1.161 + TBool jit;
1.162 + jit = User::JustInTime();
1.163 + User::SetJustInTime(EFalse);
1.164 +
1.165 + rc = thread.Create(KPanicThread, ThreadFunc,
1.166 + KDefaultStackSize, KMinHeapSize, KMinHeapSize*4,NULL);
1.167 + test(KErrNone == rc);
1.168 +
1.169 + thread.Logon(threadStatus);
1.170 + thread.Resume();
1.171 + User::WaitForRequest(threadStatus);
1.172 + User::SetJustInTime(jit);
1.173 +
1.174 + test(thread.ExitType() == EExitPanic);
1.175 + thread.Close();
1.176 + }
1.177 +
1.178 +/**
1.179 +@SYMTestCaseID SYSLIB-BAFL-CT-0483
1.180 +@SYMTestCaseDesc Tests for the functionality of RResourceFile/CResourceFile
1.181 +@SYMTestPriority High
1.182 +@SYMTestActions Tests for creation,deleting,opening of a resource file,
1.183 + should panic on invalid file.
1.184 +@SYMTestExpectedResults Tests must not fail
1.185 +@SYMREQ REQ0000
1.186 +*/
1.187 +LOCAL_C void DoTestsL()
1.188 + {
1.189 + test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0483 "));
1.190 + CleanupClosePushL(TheFs);
1.191 + User::LeaveIfError(TheFs.Connect());
1.192 +
1.193 + const TPtrC inv_rsc_files[] =
1.194 + {
1.195 + _L("TRsc_Inv1.rsc") //Zero sized file
1.196 + , _L("TRsc_Inv2.rsc")
1.197 + , _L("TRsc_Inv3.rsc")
1.198 + , _L("TRsc_Inv4.rsc")
1.199 + , _L("TRsc_Inv5.rsc")
1.200 + , _L("TRsc_Inv6.rsc")
1.201 + , _L("TRsc_Inv7.rsc")
1.202 + , _L("TRsc_Inv8.rsc")
1.203 + , _L("TRsc_Inv9.rsc")
1.204 + , _L("TRscCalypso_Inv10.RSC")
1.205 + , _L("TRscCalypso_Inv11.RSC")
1.206 + , _L("TRscCalypso_Inv12.RSC")
1.207 + , _L("TRscComprU_Inv13.RSC")
1.208 + , _L("TRscComprU_Inv14.RSC")
1.209 + , _L("TRscComprU_Inv15.RSC")
1.210 + , _L("TRscCalypso_Inv16.RSC")
1.211 + , _L("TRscCalypso_Inv17.RSC")
1.212 + , _L("TRscNotExist.RSC") //This file doesn't exist
1.213 + };
1.214 +
1.215 + TInt i;
1.216 + TInt array_size = TInt(sizeof(inv_rsc_files)/sizeof(inv_rsc_files[0]));
1.217 +
1.218 + //Z drive
1.219 + _LIT(KZDir, "z:\\system\\data\\");
1.220 + for(i=0;i<array_size;i++)
1.221 + {
1.222 + TBuf<64> path;
1.223 + path += KZDir;
1.224 + path += inv_rsc_files[i];
1.225 + TInt err = DoTestL(path);
1.226 + test(err != KErrNone);
1.227 + }
1.228 +
1.229 + //C drive
1.230 + _LIT(KCDir, "c:\\");
1.231 + for(i=0;i<array_size;i++)
1.232 + {
1.233 + TBuf<64> src_path;
1.234 + src_path += KZDir;
1.235 + src_path += inv_rsc_files[i];
1.236 +
1.237 + TBuf<64> dest_path;
1.238 + dest_path += KCDir;
1.239 + dest_path += inv_rsc_files[i];
1.240 +
1.241 + //Copy the file to C drive except the last which doesn't exist.
1.242 + //Zero sized file doesn't exist on the assabet/lubbock platforms.
1.243 + TInt err = KErrNone;
1.244 + if(i > 0 && i < (array_size - 1))
1.245 + {
1.246 + err = BaflUtils::CopyFile(TheFs, src_path, dest_path);
1.247 + test(err == KErrNone || err == KErrAlreadyExists);
1.248 + }
1.249 +
1.250 + err = DoTestL(dest_path);
1.251 + DeleteDataFile(dest_path);
1.252 + test(err != KErrNone);
1.253 + }
1.254 +
1.255 + //C drive - new rsc file format
1.256 + for(i=1;i<(array_size-1);i++)
1.257 + {
1.258 + TBuf<64> dest_path;
1.259 + TBuf<64> src_path;
1.260 +
1.261 + dest_path += KCDir;
1.262 + dest_path += _L("N_");
1.263 + dest_path += inv_rsc_files[i];
1.264 +
1.265 + src_path += KZDir;
1.266 + src_path += inv_rsc_files[i];
1.267 +
1.268 + CreateFileFromL(dest_path, src_path);
1.269 +
1.270 + TInt err = DoTestL(dest_path, KRscFileHeaderData().Length(), FileSizeL(src_path));
1.271 + DeleteDataFile(dest_path);
1.272 + test(err != KErrNone);
1.273 + }
1.274 +
1.275 + //Tests that Raise panics
1.276 + PanicTest();
1.277 +
1.278 + CleanupStack::PopAndDestroy(1, &TheFs);
1.279 + }
1.280 +
1.281 +GLDEF_C TInt E32Main()
1.282 + {
1.283 + __UHEAP_MARK;
1.284 + CTrapCleanup* cleanup = CTrapCleanup::New();
1.285 + test.Title();
1.286 + test.Start(_L("Testing RResourceFile & CResourceFile panics/leaves"));
1.287 + TRAPD(err, DoTestsL());
1.288 + test.Printf(_L("Error code is %d\n"), err);
1.289 + test(err == KErrNone);
1.290 + test.Next(_L("/n"));
1.291 + test.End();
1.292 + test.Close();
1.293 + delete cleanup;
1.294 + __UHEAP_MARKEND;
1.295 + return 0;
1.296 + }