os/kernelhwsrv/kerneltest/f32test/server/t_corruptlog.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 // f32test\server\t_corruptlog.cpp
    15 // Includes
    16 // 
    17 //
    18 
    19 //! @file
    20 //! @SYMTestCaseID FSBASE_corrupt_log
    21 //! @SYMPREQ 908
    22 //! @SYMTestSuites Base tests
    23 //! @SYMInitialAuthor Bob Jackman
    24 //! @SYMCreationDate 1/11/04
    25 //! @SYMTestCaseDesc Check trap action of CorruptFileNames.lst
    26 
    27 #include <e32test.h>
    28 RTest test(_L("t_corruptlog"));
    29 
    30 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
    31 #include <f32file.h>
    32 #include <f32dbg.h>
    33 
    34 // Error every time
    35 _LIT(KTestFile1,"z:\\system\\data\\BadFile1.txt");
    36 const TInt KTestFile1Error1=-6;
    37 const TInt KTestFile1Error2=KErrNone;
    38 //
    39 // Error once
    40 _LIT(KTestFile2,"z:\\system\\data\\BadFile2.txt");
    41 const TInt KTestFile2Error1=-20;
    42 const TInt KTestFile2Error2=-20;
    43    
    44 ////////////////////////////////////////////////////////////
    45 // Globals
    46 //
    47 static RFs TheFs;
    48 
    49 static TInt numberOfTraps=0;
    50 
    51 ////////////////////////////////////////////////////////////
    52 
    53 inline TInt controlIo(RFs &fs, TInt drv, TInt fn)
    54 	{
    55 	TPtr8 pDummy(NULL,0,0);
    56 	TInt r = fs.ControlIo(drv, fn, pDummy);
    57 	return r;
    58 	}
    59 
    60 inline TInt controlIo(RFs &fs, TInt drv, TInt fn, TInt &aNumRecs)
    61 	{
    62 	TPtr8 pNum((TUint8*)(&aNumRecs),sizeof(aNumRecs),sizeof(aNumRecs));
    63 	TInt r = fs.ControlIo(drv, fn, pNum);
    64 	return r;
    65 	}
    66 
    67 inline TInt controlIo(RFs &fs, TInt drv, TInt fn, TDes& aFileName)
    68 	{
    69 	TBuf8<KMaxFileName> fileName8;
    70 	TInt r = fs.ControlIo(drv, fn, fileName8);
    71 	if(r==KErrNone)
    72 		{
    73 		aFileName.Copy(fileName8);
    74 		}
    75 	return r;
    76 	}
    77 
    78 
    79 inline TInt controlIo(RFs &fs, TInt drv, TInt fn, TFsDebugCorruptLogRecordBuf &alogRec, TInt aRecNum)
    80 	{
    81     TInt r = fs.ControlIo(drv, fn, alogRec, *((TDes8*)aRecNum));
    82     return r;
    83 	}
    84 
    85 void PrintLogRecord(TFsDebugCorruptLogRecordBuf &alogRec, TInt aRecordNumber)
    86 	{
    87 	test.Printf(_L("#%d Process: %S tried to access %S errorCode=%d\n"),aRecordNumber,
    88 																		&(alogRec().iProcessName),
    89 																		&(alogRec().iFileName),
    90 																		alogRec().iError);
    91 	}
    92 
    93 TInt ResetCorruptLogRecords()
    94 	{
    95 	// Allows the test to be run again, by resetting the record to an unused state,
    96 	// and destroying the trap records
    97 	// Enables this test to be ran again.
    98 	TInt r=controlIo(TheFs, EDriveC, KControlIoCorruptLogRecordReset);
    99 	return r;
   100 	}	
   101 
   102 TInt GetNumberOfTraps()
   103 	{
   104 	// fetchs the number of corrupt file trap records that exist
   105 	// there is a separate record for every attempted access to a nominated file
   106 	// Note that C: is used in the IoControl call, which requires a valid drive, but has no other relevance
   107 	TInt numberOfRecords;
   108 	TInt r=controlIo(TheFs, EDriveC, KControlIoGetNumberOfCorruptLogRecords, numberOfRecords);
   109 	test(r==KErrNone);
   110 	return numberOfRecords;
   111 	}
   112 
   113 TInt GetTrapLogRecord(TFsDebugCorruptLogRecordBuf &alogRec, TInt aRecordNumber)
   114 	{
   115 	// fetchs a trap record
   116 	// Note that C: is used in the IoControl call, which requires a valid drive, but has no other relevance
   117 	TInt r=controlIo(TheFs, EDriveC, KControlIoGetCorruptLogRecord, alogRec, aRecordNumber);
   118 	test(r==KErrNone);
   119 	return r;
   120 	}
   121 
   122 TInt GetCorruptFileListFile(TDes& aFileName)
   123 	{
   124 	// Retrieves the name of the file containing the list of files, nominated as corrupt, used to generate
   125 	// the corrupt files list.
   126 	// Note that C: is used in the IoControl call, which requires a valid drive, but has no other relevance
   127 	TInt r=controlIo(TheFs, EDriveC, KControlIoGetCorruptListFile, aFileName);
   128 	test(r==KErrNone);
   129 	return r;
   130 	}
   131 
   132 void AccessFiles()
   133 	{
   134 	test.Next(_L("Access corrupt files"));
   135 	RFile f;
   136 	const TInt attribs=EFileShareExclusive|EFileStreamText|EFileRead;
   137 	// File1 
   138 	TInt r=f.Open(TheFs,KTestFile1,attribs);
   139 	test(r==KTestFile1Error1);
   140 	f.Close();
   141 	numberOfTraps+=(r==KErrNone?0:1);
   142 	// try again
   143 	r=f.Open(TheFs,KTestFile1,attribs);
   144 	test(r==KTestFile1Error2);
   145 	f.Close();
   146 	numberOfTraps+=(r==KErrNone?0:1);
   147 	// File2 
   148 	r=f.Open(TheFs,KTestFile2,attribs);
   149 	test(r==KTestFile2Error1);
   150 	f.Close();
   151 	numberOfTraps+=(r==KErrNone?0:1);
   152 	// try again
   153 	r=f.Open(TheFs,KTestFile2,attribs);
   154 	test(r==KTestFile2Error2);
   155 	f.Close();
   156 	numberOfTraps+=(r==KErrNone?0:1);
   157 	}
   158 
   159 void DoTests()
   160 	{
   161 	TFileName corruptFileNamesList;
   162     test.Next(_L("Get name of file with list of nominated files"));
   163 	TInt r=GetCorruptFileListFile(corruptFileNamesList);
   164     test(r==KErrNone);
   165  	test.Printf(_L("Using %S\n"),&corruptFileNamesList);
   166 
   167 	AccessFiles();
   168     test.Next(_L("Get Number of traps"));
   169 	TInt nRecs=GetNumberOfTraps();
   170     test.Next(_L("Test Number of traps"));
   171 	test(nRecs==numberOfTraps);
   172 
   173 	TFsDebugCorruptLogRecordBuf logRec;
   174 	for (TInt i=1;i<=nRecs;i++)
   175 		{ // fetch record #i
   176 		TInt r=GetTrapLogRecord(logRec,i);
   177 		test(r==KErrNone);
   178 		r=logRec().iProcessName.CompareF(_L("t_corruptlog.exe"));
   179 		test(r==KErrNone);
   180 		PrintLogRecord(logRec,i);
   181 		}
   182 	}
   183 
   184 #endif
   185 
   186 extern TInt E32Main()
   187 {
   188 
   189     CTrapCleanup* cleanup;
   190     cleanup=CTrapCleanup::New();
   191     __UHEAP_MARK;
   192     
   193     test.Title();
   194     test.Start(_L("Corrupt File trap log"));
   195 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
   196 	TInt r=TheFs.Connect();
   197     test(r==KErrNone);
   198 
   199     TheFs.ResourceCountMarkStart();
   200     
   201     DoTests();
   202     
   203     TheFs.ResourceCountMarkEnd();
   204 	ResetCorruptLogRecords();
   205     TheFs.Close();
   206 #endif
   207     
   208     test.End();
   209     test.Close();
   210     __UHEAP_MARKEND;
   211     delete cleanup;
   212 
   213 
   214 
   215     return KErrNone;
   216 }