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