kerneltest/f32test/demandpaging/t_reaper.cpp
changeset 0 a41df078684a
child 33 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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 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\demandpaging\t_reaper.cpp
       
    15 //
       
    16 // Suite of tests for the Reaper, the reaper is used to clean up files
       
    17 // which were deleted whilst in use, the delete is delayed until the 
       
    18 // files are no longer in use and then completed by the reaper.
       
    19 //
       
    20 // 001 Loader Reaper/Clamp Tests
       
    21 // 002 Try deleteing file while Open
       
    22 // 003 Try deleteing file while Clamped and Open
       
    23 // 004 Try deleteing while Clamped
       
    24 // 005 Check moved in sys/del
       
    25 // 006 Check Can't delete
       
    26 // 007 Unclamp and Delete
       
    27 // 008 Copy DLL to drive and delete
       
    28 // 009 Copy DLL, Load, close and RFs::delete
       
    29 // 010 Copy DLL, Load, close and Loader::delete
       
    30 // 011 Copy DLL to drive, load and delete
       
    31 // 012 Check file deleted on close
       
    32 // 013 Try deleting something of the same name twice while loaded.
       
    33 // 
       
    34 
       
    35 //! @SYMTestCaseID			KBASE-T_REAPER-0330
       
    36 //! @SYMTestType			UT
       
    37 //! @SYMPREQ				PREQ1110
       
    38 //! @SYMTestCaseDesc		Demand Paging Reaper.
       
    39 //! @SYMTestActions			001 Loader Reaper/Clamp Tests
       
    40 //! @SYMTestExpectedResults All tests should pass.
       
    41 //! @SYMTestPriority        High
       
    42 //! @SYMTestStatus          Implemented
       
    43 
       
    44 #define __E32TEST_EXTENSION__
       
    45 #include <e32test.h>
       
    46 #include <f32file.h>
       
    47 #include <e32ldr.h>
       
    48 #include <u32hal.h>
       
    49 #include "u32std.h"
       
    50 
       
    51 TBool Verbose = EFalse;
       
    52 RFs TheFs;
       
    53 RLoader Loader;
       
    54 TInt DriveNumber=-1;
       
    55 
       
    56 class TPagingDriveInfo
       
    57 	{
       
    58 public:
       
    59 	TChar iDriveLetter;
       
    60 	TDriveInfo iDriveInfo;
       
    61 	};
       
    62 
       
    63 RArray<TPagingDriveInfo> SupportedDrives;
       
    64 TInt gNumSupportedDrives = 0;
       
    65 
       
    66 
       
    67 LOCAL_D RTest test(_L("T_reaper"));
       
    68 
       
    69 _LIT(KFilePath,":\\sys\\bin\\test.txt");
       
    70 
       
    71 _LIT(KSysPath,"z:\\sys\\bin\\");
       
    72 _LIT(KPathDel,":\\sys\\del\\");
       
    73 _LIT(KDllFile,"t_reaper_test_dll.dll");
       
    74 const TInt KLibNameLength = 50;
       
    75 
       
    76 void CopyDll(const TDesC& aSourceName, const TDesC& aDestName)
       
    77 	{
       
    78 	const TInt KBufferSize = 3333;
       
    79 	TBuf8<KBufferSize> buffer;
       
    80 	RFile in, out;
       
    81 	
       
    82 	test.Printf(_L("  copying %S to %S\n"), &aSourceName, &aDestName);
       
    83 
       
    84 	TInt r = TheFs.MkDirAll(aDestName);
       
    85 	test_Assert(r == KErrNone || r == KErrAlreadyExists, test.Printf(_L("MkDirAll returned %d\n"),r));
       
    86 
       
    87 	test_KErrNone(in.Open(TheFs, aSourceName, EFileRead));
       
    88 	test_KErrNone(out.Replace(TheFs, aDestName, EFileWrite));
       
    89 
       
    90 	TInt size;
       
    91 	test_KErrNone(in.Size(size));
       
    92 	TInt pos = 0;
       
    93 	while (pos < size)
       
    94 		{
       
    95 		test_KErrNone(in.Read(buffer));
       
    96 		test_KErrNone(out.Write(buffer));
       
    97 		pos += buffer.Length();
       
    98 		}
       
    99 	
       
   100 	in.Close();
       
   101 	out.Close();
       
   102 	}
       
   103 
       
   104 void CopyDllToDrive(const TDesC& aSourceName, TUint8 aDrive, TBuf<KLibNameLength>& aDestName)
       
   105 	{
       
   106 		TBuf<KLibNameLength> sourceName;
       
   107 		sourceName=KSysPath;
       
   108 		sourceName.Append(aSourceName);
       
   109 		aDestName=KSysPath;
       
   110 		aDestName.Append(aSourceName);
       
   111 		aDestName[0] = aDrive;
       
   112 		CopyDll(sourceName, aDestName);
       
   113 	}
       
   114 
       
   115 static void CreateTestFile(const TDesC& aTestFile)
       
   116 /**
       
   117 	Create an empty file with the supplied name.  This function is used
       
   118 	to create file which can be deleted with RLoader::Delete.
       
   119 	
       
   120 	@param	aFs				Open file server session.
       
   121 	@param	aTestFile		The test file's name.
       
   122  */
       
   123 	{
       
   124 	TInt r;
       
   125 	TheFs.MkDirAll(aTestFile);
       
   126 	RFile f;
       
   127 	r = f.Replace(TheFs, aTestFile, EFileWrite | EFileStream | EFileShareExclusive);
       
   128 	test_KErrNone(r);
       
   129 	TBuf<256> buf(_L("Space is big, I mean its really big.\n"));
       
   130 	TPtrC8 pBuf((TUint8*)&buf);
       
   131 	f.Write(pBuf);
       
   132 	f.Flush();
       
   133 	f.Close();
       
   134 	}
       
   135 
       
   136 // Get the list of pageable drives
       
   137 void GetSupportedDrives()
       
   138 	{
       
   139 	TChar ch;
       
   140 	TBuf<256> fileSystemName;
       
   141 	TDriveList driveList;
       
   142 	TDriveInfo driveInfo;
       
   143 
       
   144 	TInt r = TheFs.DriveList(driveList);
       
   145     test_KErrNone(r);
       
   146 
       
   147 	TBool NandPageableMediaFound = EFalse;
       
   148 
       
   149 	for (TInt drvNum=0; drvNum<KMaxDrives; ++drvNum)
       
   150 		{
       
   151 	    if(!driveList[drvNum])
       
   152 	        continue;   //-- skip unexisting drive
       
   153 	
       
   154 	    test_KErrNone( TheFs.Drive(driveInfo, drvNum) );
       
   155 		test_KErrNone( TheFs.DriveToChar(drvNum, ch) );
       
   156 		test_KErrNone( TheFs.FileSystemName(fileSystemName, drvNum) );
       
   157 
       
   158 		if (Verbose)
       
   159 			test.Printf(_L("GetSupportedDrives, Drive %c iType %d iDriveAtt %08x iMediaAtt %08X, fileSystemName %S\n"), 
       
   160 				(TInt) ch, driveInfo.iType, driveInfo.iDriveAtt, driveInfo.iMediaAtt, &fileSystemName);
       
   161 	
       
   162 		if ((driveInfo.iDriveAtt & KDriveAttPageable) && (driveInfo.iType == EMediaNANDFlash))
       
   163 			NandPageableMediaFound = ETrue;
       
   164 
       
   165 		TBool pageable = EFalse;
       
   166 		if (driveInfo.iDriveAtt & KDriveAttPageable)
       
   167 			pageable = ETrue;
       
   168 
       
   169 		// If we've already found a pageable NAND drive, then assume the Z: drive is pageable too 
       
   170 		// if it's got a composite file system (fudge)
       
   171 		_LIT(KCompositeName,"Composite");
       
   172 		if (fileSystemName == KCompositeName())
       
   173 			{
       
   174 			if (NandPageableMediaFound)
       
   175 				pageable = ETrue;
       
   176 			driveInfo.iMediaAtt|=KMediaAttWriteProtected;
       
   177 			}
       
   178 		if (pageable)
       
   179 			{
       
   180 			TPagingDriveInfo pagingDriveInfo;
       
   181 			pagingDriveInfo.iDriveLetter = ch;
       
   182 			pagingDriveInfo.iDriveInfo = driveInfo;
       
   183 
       
   184 
       
   185 			test_KErrNone( SupportedDrives.Append(pagingDriveInfo) );
       
   186 			if (Verbose)
       
   187 				test.Printf(_L("Drive %c supports paging\n"), (TInt) pagingDriveInfo.iDriveLetter);
       
   188 			gNumSupportedDrives++;
       
   189 			}
       
   190 		}
       
   191 	}
       
   192 
       
   193 
       
   194 
       
   195 TInt FindDeletedFile(TUint8 aDrive, TDes& aFile, const TDesC& aExcludedFile=KNullDesC)
       
   196 	{
       
   197 	aFile.Zero();
       
   198 
       
   199 	_LIT(KSearchPathDel,"?:\\sys\\del\\*");
       
   200 	TBuf<13> delPath;
       
   201 	delPath = KSearchPathDel;
       
   202 	delPath[0] = aDrive;
       
   203 
       
   204 	CDir* dir=NULL;
       
   205 	
       
   206 	CTrapCleanup* c = CTrapCleanup::New();
       
   207 	TInt r = TheFs.GetDir(delPath,KEntryAttMatchExclude | KEntryAttDir ,ESortNone,dir);
       
   208 	delete c;
       
   209 	if(r==KErrPathNotFound)
       
   210 		return 0; // no files
       
   211 	test_KErrNone(r);
       
   212 
       
   213 	test.Printf(_L("Files: %d\n"),dir->Count() );
       
   214 	TInt count = dir->Count();
       
   215 	TBool found = false;
       
   216 	for (TInt i=count-1;i>=0;i--)
       
   217 		{
       
   218 		TBuf<KLibNameLength> tempPath;
       
   219 		tempPath.Append(aDrive);
       
   220 		tempPath.Append(KPathDel);
       
   221 		tempPath.Append((*dir)[i].iName);
       
   222 		test.Printf(_L("%S\n"), &tempPath);
       
   223 		if(!found && tempPath.CompareF(aExcludedFile)!=0)
       
   224 			{
       
   225 			found = true;
       
   226 			aFile = tempPath;
       
   227 			}
       
   228 		}
       
   229 	
       
   230 	delete dir;
       
   231 	return count;
       
   232 	}
       
   233 
       
   234 
       
   235 void TestDlls(TUint8 aDrive)
       
   236 	{
       
   237 	TEntry e;
       
   238 	TBuf<KLibNameLength> libName;
       
   239 	TBuf<KLibNameLength> delName;
       
   240 	TBuf<KLibNameLength> delName2;
       
   241 	TBuf<KLibNameLength> libName2;
       
   242 
       
   243 	RLibrary library;
       
   244 	RLibrary library2;
       
   245 
       
   246 	test.Next(_L("Copy DLL to drive and delete"));
       
   247 	CopyDllToDrive(KDllFile,aDrive,libName);
       
   248 	test_Equal(0, FindDeletedFile(aDrive,delName)); // check no files in sys/del
       
   249 	test_KErrNone(Loader.Delete(libName));
       
   250 	test_Equal(KErrNotFound, TheFs.Entry(libName,e));
       
   251 	test_Equal(0, FindDeletedFile(aDrive,delName)); // check no files in sys/del
       
   252 	
       
   253 	test.Next(_L("Copy DLL, Load, close and RFs::delete"));
       
   254 	CopyDllToDrive(KDllFile,aDrive,libName);
       
   255 	test_KErrNone( library.Load(libName));
       
   256 	test_Equal(KErrInUse, TheFs.Delete(libName));
       
   257 	CLOSE_AND_WAIT(library);
       
   258 	test_KErrNone( TheFs.Delete(libName));
       
   259 	test_Equal(KErrNotFound, TheFs.Entry(libName,e));
       
   260 	
       
   261 	test.Next(_L("Copy DLL, Load, close and Loader::delete"));
       
   262 	CopyDllToDrive(KDllFile,aDrive,libName);
       
   263 	test_KErrNone( library.Load(libName));
       
   264 	CLOSE_AND_WAIT(library);
       
   265 	test_KErrNone( Loader.Delete(libName));
       
   266 	test_Equal(KErrNotFound, TheFs.Entry(libName,e));
       
   267 	test_Equal(0, FindDeletedFile(aDrive,delName)); // check no files in sys/del
       
   268 
       
   269 	test.Next(_L("Copy DLL to drive, load and delete"));
       
   270 	CopyDllToDrive(KDllFile,aDrive,libName);
       
   271 	test_KErrNone( library.Load(libName));
       
   272 	test_Equal(KErrInUse, TheFs.Delete(libName));
       
   273 	test_KErrNone( Loader.Delete(libName));
       
   274 	test_Equal(KErrNotFound, TheFs.Entry(libName,e));
       
   275 	test_Equal(1, FindDeletedFile(aDrive,delName)); // get name of 'deleted' file
       
   276 	test_Equal(KErrInUse, TheFs.Delete(delName));
       
   277 	
       
   278 	test.Next(_L("Check file deleted on close"));
       
   279 	CLOSE_AND_WAIT(library);
       
   280 	test_Equal(KErrNotFound, TheFs.Entry(delName,e));	
       
   281 
       
   282 	test.Next(_L("Try deleting something of the same name twice while loaded."));
       
   283 	CopyDllToDrive(KDllFile,aDrive,libName);
       
   284 	test_KErrNone( library.Load(libName));
       
   285 	test_KErrNone( Loader.Delete(libName));
       
   286 	test_Equal(KErrNotFound, TheFs.Entry(libName,e));
       
   287 	test_Equal(1, FindDeletedFile(aDrive,delName)); // get name of 'deleted' file
       
   288 	
       
   289 	test.Printf(_L("Load a Secord Copy\n"));
       
   290 	CopyDllToDrive(KDllFile,aDrive,libName);
       
   291 	libName2=libName;
       
   292 	libName2[27]='2';
       
   293 	test_KErrNone( TheFs.Rename(libName,libName2));
       
   294 	test_KErrNone( library2.Load(libName2));
       
   295 	test_KErrNone( TheFs.Rename(libName2,libName));
       
   296 	
       
   297 	test.Printf(_L("Try and delete second copy\n"));
       
   298 	test_KErrNone( Loader.Delete(libName));
       
   299 	test_Equal(2, FindDeletedFile(aDrive,delName2,delName)); // get name of second 'deleted' file
       
   300 
       
   301 	test.Printf(_L("Now close and watch deletions\n"));
       
   302 	CLOSE_AND_WAIT(library);
       
   303 	test_Equal(KErrNotFound, TheFs.Entry(delName,e));
       
   304 		test_KErrNone( TheFs.Entry(delName2,e));
       
   305 	CLOSE_AND_WAIT(library2);
       
   306 	test_Equal(KErrNotFound, TheFs.Entry(delName2,e));
       
   307 	}
       
   308 
       
   309 
       
   310 void BasicTest(TUint8 aDrive)
       
   311 	{
       
   312 	TBuf<256> startFileName;	
       
   313 	TBuf<256> endFileName;
       
   314 	RFile file;
       
   315 	RFileClamp fileClamp;
       
   316 
       
   317 	startFileName.Append((TChar) aDrive);
       
   318 	startFileName+=KFilePath;
       
   319 
       
   320 	
       
   321 	CreateTestFile(startFileName);
       
   322 	
       
   323 	test.Next(_L("Try deleteing file while Open"));
       
   324 	test_KErrNone( file.Open(TheFs,startFileName,EFileShareExclusive|EFileWrite));
       
   325 	test_Equal(KErrInUse, Loader.Delete(startFileName));
       
   326 
       
   327 	test.Next(_L("Try deleteing file while Clamped and Open"));
       
   328 	test_KErrNone( fileClamp.Clamp(file));
       
   329 	test_Equal(KErrInUse, Loader.Delete(startFileName));
       
   330 
       
   331 	test.Next(_L("Try deleteing while Clamped"));
       
   332 	file.Close(); 
       
   333 	test_Equal(0, FindDeletedFile(aDrive,endFileName)); // check no files in sys/del
       
   334 	test_KErrNone( Loader.Delete(startFileName));
       
   335 
       
   336 	test.Next(_L("Check moved in sys/del"));
       
   337 	test_Equal(1, FindDeletedFile(aDrive,endFileName)); // check one file in sys/del
       
   338 	
       
   339 	test.Next(_L("Check Can't delete"));
       
   340 	test_Equal(KErrInUse, TheFs.Delete(endFileName));
       
   341 
       
   342 	test.Next(_L("Unclamp and Delete"));
       
   343 	test_KErrNone( fileClamp.Close(TheFs));
       
   344 	test_KErrNone( TheFs.Delete(endFileName));
       
   345 	
       
   346 	
       
   347 	}
       
   348 
       
   349 
       
   350 //
       
   351 // ParseCommandLine reads the arguments and sets globals accordingly.
       
   352 //
       
   353 
       
   354 TInt ParseCommandLine()
       
   355 	{
       
   356 	TBuf<32> args;
       
   357 	User::CommandLine(args);
       
   358 	TLex lex(args);
       
   359 	TInt err=KErrNone;
       
   360 	FOREVER
       
   361 		{
       
   362 		TPtrC token=lex.NextToken();
       
   363 		if(token.Length()!=0)
       
   364 			{
       
   365 			if ((token.Length()==1) || ((token.Length()==2) && (token[1]==':')))
       
   366 				{
       
   367 				TChar driveLetter = User::UpperCase(token[0]); 
       
   368 				if ((driveLetter>='A') && (driveLetter<='Z'))
       
   369 					DriveNumber=driveLetter - (TChar) 'A';
       
   370 				else 
       
   371 					err=KErrArgument;
       
   372 				}
       
   373 			else if ((token==_L("help")) || (token==_L("-h")) || (token==_L("-?")))
       
   374 				{
       
   375 				test.Printf(_L("\nThis tests the loader's reaper, which is used in code paging to postpone deletions of paged code from disk.\n")); 
       
   376 				test.Printf(_L("\nIf no drive letter is supplied, then all suitable drives are checked.\n\n"));
       
   377 				err=KErrCancel;
       
   378 				}
       
   379 			else
       
   380 				err=KErrArgument;
       
   381 			}
       
   382 		else
       
   383 			break;
       
   384 		
       
   385 		if (err!=KErrNone)
       
   386 			{
       
   387 			if (err==KErrArgument)
       
   388 				test.Printf(_L("\nUnknown argument '%S'\n"), &token);
       
   389 			test.Printf(_L("\nUsage:  t_reaper [-h] [<driveletter>]\n\n"));
       
   390 			test.Getch();
       
   391 			return err;
       
   392 			}
       
   393 		}
       
   394 	return KErrNone;
       
   395 	}
       
   396 
       
   397 
       
   398 
       
   399 GLDEF_C TInt E32Main()
       
   400 	{
       
   401 	TInt i;
       
   402 	test.Title();
       
   403 	
       
   404 	if (ParseCommandLine())
       
   405 		return KErrNone;
       
   406 	
       
   407 	TheFs.Connect();
       
   408 	
       
   409 	TUint32 memModelAttributes=UserSvr::HalFunction(EHalGroupKernel, EKernelHalMemModelInfo, NULL, NULL);
       
   410 	TUint32 pagingPolicy = E32Loader::PagingPolicy();
       
   411 	if((memModelAttributes&EMemModelAttrCodePaging)==0 || pagingPolicy==EKernelConfigCodePagingPolicyNoPaging)
       
   412 		{
       
   413 		test.Start(_L("TESTS NOT RUN - Code paging not enabled on system."));
       
   414 		test.End();
       
   415 		return KErrNone;
       
   416 		}
       
   417 
       
   418 	test.Start(_L("Loader Reaper/Clamp Tests"));
       
   419 	
       
   420 	// Turn off evil lazy dll unloading
       
   421 	RLoader l;
       
   422 	test(l.Connect()==KErrNone);
       
   423 	test(l.CancelLazyDllUnload()==KErrNone);
       
   424 	l.Close();
       
   425 
       
   426 	GetSupportedDrives();
       
   427 	test_Compare(gNumSupportedDrives,>,0);
       
   428 	test_KErrNone( Loader.Connect());
       
   429 	test_KErrNone( Loader.CancelLazyDllUnload());
       
   430 	
       
   431 	for (i=0;i<gNumSupportedDrives;i++)
       
   432 		{
       
   433 		if ((DriveNumber==-1) || (DriveNumber== (TInt) (SupportedDrives[i].iDriveLetter - (TChar) 'A')))
       
   434 			{
       
   435 			test.Printf(_L("Testing drive: %c\n"),(TInt) SupportedDrives[i].iDriveLetter);
       
   436 			if (!(SupportedDrives[i].iDriveInfo.iMediaAtt&KMediaAttWriteProtected))
       
   437 				{
       
   438 				TUint8 drive = SupportedDrives[i].iDriveLetter;
       
   439 				BasicTest(drive);
       
   440 				TestDlls(drive);
       
   441 				}
       
   442 			}
       
   443 				
       
   444 		}
       
   445 		
       
   446 	Loader.Close();
       
   447 	TheFs.Close();
       
   448 	
       
   449 	test.End();
       
   450 	
       
   451 	return KErrNone;
       
   452 	}