os/kernelhwsrv/kerneltest/f32test/loader/security/t_ldrcheck.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1999-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\loader\t_ldrcheck.cpp
    15 // 
    16 //
    17 
    18 
    19 #include "dlltree.h"
    20 #include <e32uid.h>
    21 #include <hal.h>
    22 #include <e32wins.h>
    23 #include <e32svr.h>
    24 #include <e32test.h>
    25 #include <f32file.h>
    26 #include <f32dbg.h>
    27 #include <f32image.h>
    28 #include "t_hash.h"
    29 
    30 #if defined (WIN32) && !defined (__X86GCC__)
    31 #include <emulator.h>
    32 #include <stdlib.h>
    33 #endif
    34 
    35 
    36 RTest test(_L("T_LDRCheck"));
    37 RFs TheFs;
    38 GLDEF_D TChar gDriveToTest;
    39 
    40 
    41 /**
    42     Copy modules from z:\\sys\\bin to c:\\sys\\bin
    43 */
    44 void CopyModules()
    45 	{
    46 	test.Next(_L("Copy Modules from ROM to disk"));
    47 
    48 	TInt r;
    49 	TTime modtime;
    50 
    51 	r=TheFs.MkDirAll(KSystemLibs);
    52 	test(r==KErrNone || r==KErrAlreadyExists);
    53 
    54 	CFileMan* fileMan=NULL;
    55 	TRAP(r,fileMan=CFileMan::NewL(TheFs));
    56 	test(r==KErrNone);
    57 
    58 	TBuf<40> dllfilenames;
    59 	TBuf<40> dlldestination;
    60 	
    61 	for(TInt i=0;i<14;i++)
    62 		{
    63 		dllfilenames=KDllfilename;
    64 		dllfilenames.AppendNum(i);
    65 		dllfilenames +=KDllExt;
    66 		dlldestination=KNewDllName;
    67 		dlldestination.AppendNum(i);
    68 		dlldestination+=KDllExt;
    69 #ifndef WIN32
    70 		r=fileMan->Copy(dllfilenames,dlldestination, 0);
    71 		test(r==KErrNone || r==KErrAlreadyExists);
    72 
    73 		r=fileMan->Attribs(dlldestination,0, KEntryAttReadOnly,modtime,0);
    74 		test(r==KErrNone);
    75 #endif
    76 		}
    77 
    78 	TBuf<40> exefilenames;
    79 	TBuf<40> exedestination;
    80 
    81 	for(TInt j=14;j<16;j++)
    82 		{
    83 		exefilenames=KExefilename;
    84 		exefilenames.AppendNum(j);
    85 		exefilenames +=KExeExt;
    86 		exedestination=KNewExeName;
    87 		exedestination.AppendNum(j);
    88 		exedestination+=KExeExt;
    89 #ifndef WIN32
    90 		r=fileMan->Copy(exefilenames,exedestination,0);
    91 		test(r==KErrNone || r==KErrAlreadyExists);
    92 
    93 		r=fileMan->Attribs(exedestination, 0, KEntryAttReadOnly, modtime, 0);
    94 		test(r==KErrNone);
    95 #endif
    96 		}
    97 	delete fileMan;
    98 	}
    99 
   100 
   101 
   102 #ifndef WIN32
   103 /*
   104 void ModifyModuleCapabilties(TUint32 aNewCap, TInt aModuleNumber)
   105 //
   106 //	Assign new capabilties to a module using raw disk write
   107 //
   108 	{
   109 //	test.Next(_L("Modify Module Capabilities"));
   110 	
   111 	test(aModuleNumber<=15 && aModuleNumber>=0);	
   112 	
   113 	TBuf<40> dlldestination;
   114 	RFile modFile;
   115 	TInt r=0;
   116 
   117 	SCapabilitySet caps;
   118 	memset(&caps,0,sizeof(caps));
   119 	caps[0] = aNewCap;
   120 	TPtrC8 theNewCaps((TUint8*)(&caps),sizeof(caps));
   121 
   122 	if(aModuleNumber < 14)
   123 		{
   124 		dlldestination=KNewDllName;
   125 		dlldestination.AppendNum(aModuleNumber);
   126 		dlldestination+=KDllExt;
   127 		}
   128 	else
   129 		{
   130 		dlldestination=KNewExeName;
   131 		dlldestination.AppendNum(aModuleNumber);
   132 		dlldestination+=KExeExt;
   133 		}
   134 
   135 	r=modFile.Open(TheFs, dlldestination, EFileWrite);
   136 	test(r==KErrNone);
   137 	r=modFile.Write(_FOFF(E32ImageHeaderV,iS.iCaps), theNewCaps,4);
   138 	test(r==KErrNone);
   139 	modFile.Close();	
   140 
   141 	RLibrary lib;
   142 	TCapabilitySet theCaps;
   143 	RLibrary::TInfoBuf info;
   144 
   145 	lib.GetInfo(dlldestination, info);
   146 	theCaps=info().iSecurityInfo.iCaps;
   147 
   148 	lib.Close();
   149 	test(theCaps==aNewCap);
   150 
   151 	}
   152 */
   153 #else
   154 /*
   155 void ModifyModuleCapabilties(TUint32 aNewCap, TInt aModuleNumber)
   156 //
   157 //	Wins version (poking values is not quite so simple
   158 //
   159 	{
   160 //	test.Next(_L("Modify Module Capabilities"));
   161 	test(aModuleNumber<=15 && aModuleNumber>=0);	
   162 	
   163 	TBuf<40> dlldestination;
   164 	TInt r=0;
   165 
   166 	TPtrC8 theNewCaps((TUint8*)(&aNewCap),sizeof(TUint32));
   167 
   168 	
   169 	if(aModuleNumber < 14)
   170 		{
   171 		dlldestination=KNewDllName;
   172 		dlldestination.AppendNum(aModuleNumber);
   173 		dlldestination+=KDllExt;
   174 		}
   175 	else
   176 		{
   177 		dlldestination=KNewExeName;
   178 		dlldestination.AppendNum(aModuleNumber);
   179 		dlldestination+=KExeExt;
   180 		}
   181 
   182 	RLibrary lib2;
   183 	TCapabilitySet theCaps2;
   184 	RLibrary::TInfoBuf info;
   185 
   186 	lib2.GetInfo(dlldestination, info);
   187 	theCaps2=info().iSecurityInfo.iCaps;
   188 
   189 	lib2.Close();
   190 			
   191 	TBuf<MAX_PATH> fileName;
   192 	r = MapEmulatedFileName(fileName, dlldestination);
   193 
   194 	HANDLE hFile=Emulator::CreateFile((LPCTSTR)fileName.PtrZ(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
   195 	if (hFile==INVALID_HANDLE_VALUE)
   196 		return;
   197 	DWORD ret;
   198 	TBuf8<sizeof(TCheckedUid)> checkedUidBuf;
   199 	checkedUidBuf.SetLength(sizeof(TCheckedUid));
   200 	ReadFile(hFile,&checkedUidBuf[0],sizeof(TCheckedUid),&ret,NULL);
   201 	if (ret!=sizeof(TCheckedUid))
   202 		goto close;
   203 
   204 		//Look at PE file for UID section
   205 		{
   206 		const TInt KPeHeaderAddrAddr=0x3c;
   207 		const TInt KPeHeaderAddrSize=0x01;
   208 		const TInt KNumberOfSectionsOffset=0x06;
   209 		const TInt KNumberOfSectionsSize=0x02;
   210 		const TInt KSectionTableOffset=0xf8;
   211 		const TInt KSectionHeaderSize=0x28;
   212 		const TInt KSectionNameLength=0x08;
   213 		const TInt KPtrToRawDataOffset=0x14;
   214 		const TInt KPtrToRawDataSize=0x04;
   215 		const TText8 peText[4]={'P','E',0,0};
   216 		const TText8 uidText[8]={'.','S','Y','M','B','I','A','N'};
   217 		
   218 	//Read address of start of PE header
   219 		if (SetFilePointer(hFile,KPeHeaderAddrAddr,0,FILE_BEGIN)==0xFFFFFFFF)
   220 			goto close;
   221 		TInt peAddr=0;
   222 		ReadFile(hFile,&peAddr,KPeHeaderAddrSize,&ret,NULL);
   223 		if (ret!=KPeHeaderAddrSize)
   224 			goto close;
   225 		
   226 	//Check it really is the start of PE header
   227 		if (SetFilePointer(hFile,peAddr,0,FILE_BEGIN)==0xFFFFFFFF)
   228 			goto close;
   229 		TText8 text[4];
   230 		ReadFile(hFile,text,4,&ret,NULL);
   231 		if (*(TInt32*)text!=*(TInt32*)peText)
   232 			goto close;
   233 		
   234 	//Read number of sections
   235 		if (SetFilePointer(hFile,peAddr+KNumberOfSectionsOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   236 			goto close;
   237 		TInt sections=0;
   238 		ReadFile(hFile,&sections,KNumberOfSectionsSize,&ret,NULL);
   239 		if (ret!=KNumberOfSectionsSize)
   240 			goto close;
   241 
   242 	//Go through section headers looking for UID section
   243 		if (SetFilePointer(hFile,peAddr+KSectionTableOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   244 			goto close;
   245 		TInt i=0;
   246 		for(;i<sections;i++)
   247 			{
   248 			TText8 name[KSectionNameLength];
   249 			ReadFile(hFile,name,KSectionNameLength,&ret,NULL);
   250 			if (ret!=KSectionNameLength)
   251 				goto close;
   252 			if (*(TInt64*)name==*(TInt64*)uidText)
   253 				break;
   254 			if (SetFilePointer(hFile,KSectionHeaderSize-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
   255 				goto close;
   256 			}
   257 		if (i==sections)
   258 			goto close;
   259 
   260 	//Read RVA/Offset
   261 		if (SetFilePointer(hFile,KPtrToRawDataOffset-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
   262 			goto close;
   263 		TInt uidOffset;
   264 		ReadFile(hFile,&uidOffset,KPtrToRawDataSize,&ret,NULL);
   265 		if (ret!=KPtrToRawDataSize)
   266 			goto close;
   267 
   268 	//SYMBIAN Header
   269 		if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   270 			goto close;
   271 
   272 		TEmulatorImageHeader header;
   273 
   274 		ReadFile(hFile,&header,sizeof(header),&ret,NULL);
   275 		if (ret!=sizeof(header))
   276 			goto close;
   277 		
   278 // DON'T modify, for now		header.iS.iCaps[0] = aNewCap;		//assign new caps to binary
   279 
   280 
   281 		if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   282 			goto close;
   283 
   284 		BOOL b = WriteFile(hFile,&header,sizeof(header),&ret,NULL);
   285 		if (b==FALSE)
   286 			goto close;
   287 		CloseHandle(hFile);
   288 		
   289 		RLibrary lib;
   290 		TCapabilitySet theCaps;
   291 		RLibrary::TInfoBuf info;
   292 		lib.GetInfo(dlldestination, info);
   293 		theCaps=info().iSecurityInfo.iCaps;
   294 
   295 		lib.Close();
   296 
   297 		SCapabilitySet& realCaps = (SCapabilitySet&) theCaps;
   298 // broke!		test(realCaps[0] == aNewCap && realCaps[1] == 0);
   299 		return;
   300 		}
   301 
   302 //Close file
   303 close:
   304 	CloseHandle(hFile);
   305 	test(0);
   306 	} 
   307 */
   308 #endif
   309 
   310 /*
   311 void TestGetCapability()
   312 //
   313 //	test RLibrary::GetCapabilites
   314 //
   315 	{
   316 	RLibrary library;
   317 	TInt r;
   318 	test.Next(_L("Test RLibrary::GetCapabilities()"));
   319 
   320 // These tests check the error code returned by the (deprecated) GetCapability method
   321 // and now serve no useful purpose
   322 //	TCapability theCaps;
   323 //	r=library.GetCapability(theCaps,_L("Z:\\Mongolia"));
   324 //	test(r==KErrNotFound);
   325 //	r=library.GetCapability(theCaps,_L("C:\\Malawi.Fip"));
   326 //	test(r==KErrNotFound);
   327 
   328 	TCapabilitySet theCaps;
   329 	RLibrary::TInfoBuf info;
   330 #if defined __WINS__
   331 
   332 	
   333 
   334 
   335 	test.Next(_L("Get caps of an existing library"));
   336 //	r=lib1.GetCapability(theCaps,_L("ECONS"));
   337 //	test(r==KErrNone);
   338 	RLibrary lib1;
   339 //	r=lib1.GetInfo(_L("ECONS"), info);
   340 	r=lib1.GetInfo(_L("Z:\\SYS\\BIN\\ECONS"), info);
   341 	test(r==KErrNone);
   342 	theCaps=info().iSecurityInfo.iCaps;
   343 
   344 	test.Next(_L("And again"));
   345 //	r=lib2.GetCapability(theCaps,_L("ECONS.DLL"));
   346 //	test(r==KErrNone);
   347 	RLibrary lib2;
   348 	lib2.GetInfo(_L("ECONS.DLL"), info);
   349 	theCaps=info().iSecurityInfo.iCaps;
   350 	
   351 #else
   352 // This test checks for the correct return value from the (deprecated)
   353 // method GetCapability, and now serves no useful purpose??
   354 //	r=library.GetCapability(theCaps,_L("Malawi.Fip"));
   355 //	test(r==KErrNotSupported);
   356 	library.GetInfo(_L("Malawi.Fip"), info);
   357 	theCaps=info().iSecurityInfo.iCaps;
   358 
   359 //TODO uncomment when in ssytem bin
   360 //	RLibrary lib1;
   361 //	test.Next(_L("Get caps of an existing library"));
   362 //	r=lib1.GetCapability(theCaps,_L("ECONS"));
   363 //	r=lib1.GetCapability(theCaps,_L("Z:\\Sys\\Bin\\ECONS"));
   364 //	RDebug::Print(_L("r====%d"),r);
   365 //	test(r==KErrNone);
   366 	
   367 //	RLibrary lib2;
   368 //	test.Next(_L("And again"));
   369 //	r=lib2.GetCapability(theCaps,_L("Z:\\Sys\\Bin\\ECONS.DLL"));
   370 //	r=lib2.GetCapability(theCaps,_L("ECONS.DLL"));
   371 //	test(r==KErrNone);
   372 	
   373 	RLibrary lib3;
   374 	test.Next(_L("And again, but searching"));
   375 	r=lib3.GetCapability(theCaps,_L("ECONS.DLL"));
   376 	test(r==KErrNotSupported);
   377 	lib3.Close();
   378 	
   379 #endif
   380 
   381 	test.Next(_L("Search through DLLs from t_ldrcheck"));
   382 
   383 	// Not sure of the benefit of this test ??
   384 
   385 	TBuf<40> dllfilenames;
   386 	RLibrary tlib;
   387 //	TCapability deecaps;
   388 	TCapabilitySet deecaps;
   389 	for(TInt i=0;i<13;i++)
   390 		{
   391 		dllfilenames=KDllfilename;
   392 		dllfilenames.AppendNum(i);
   393 		dllfilenames +=KDllExt;
   394 //		r=tlib.GetCapability(deecaps,dllfilenames);		
   395 		tlib.GetInfo(dllfilenames, info);
   396 		theCaps=info().iSecurityInfo.iCaps;
   397 		}
   398 
   399 	test.Next(_L("Close()"));
   400 	tlib.Close();
   401 	library.Close();
   402 //	lib1.Close();
   403 //	lib2.Close();
   404 	}
   405 */
   406 
   407 TInt LoadExe(TInt aModuleNum,  RProcess& aProcess)
   408 //
   409 //	Load executable
   410 //
   411 	{
   412 	TFileName fn;
   413 //#ifndef WIN32
   414 //	fn =KSystemLibs();
   415 //#endif
   416 	TBuf<16> cmd;
   417 	fn += MODULE_FILENAME(aModuleNum);
   418 	TInt r=aProcess.Create(fn, cmd);
   419 	return r;
   420 	}
   421 
   422 
   423 void TestLoad(TInt aSequence)
   424 //
   425 // Test doing the loads using sequence number to get the results
   426 //
   427 	{
   428 	test.Next(_L("Test all single EXE/DLL combinations\n"));
   429 	TInt r=0;
   430 	test.Next(_L("New EXE"));
   431 	RProcess p;
   432 
   433 	r=LoadExe(14,p);
   434 	test.Printf(_L("LoadExe(14)->%d, Expected %d, seq %d \n"),r,ModuleResultsA[aSequence], aSequence);
   435 	test(r==ModuleResultsA[aSequence]);
   436 	if (r==KErrNone)
   437 		p.Terminate(0);
   438 	p.Close();
   439 
   440 	r=LoadExe(15,p);
   441 	test.Printf(_L("LoadExe(15)->%d, seq %d\n"),r,aSequence);
   442 	test(r==ModuleResultsA[aSequence]);
   443 	if (r==KErrNone)
   444 		p.Terminate(0);
   445 	p.Close();
   446 	}
   447 
   448 /*
   449 void TestLoadLibrary()
   450 //
   451 //	test using load library to load dlls with out rom time dependancies
   452 //
   453 	{
   454 	test.Next(_L("Test loading libraries fromn this exe"));
   455 	
   456 	RLibrary lib;
   457 	TInt r=0;
   458 	//I want to load 6 7 and 11 as these	are of most interest
   459 
   460 	for(TInt i=0;i<KTestCases;i++)
   461 		{
   462 		for(TInt mod=0 ; mod < 16 ; mod++)	//each module
   463 			{
   464 			ModifyModuleCapabilties(ModuleCaps[mod][i],mod);
   465 			}
   466 		r=lib.Load(KDll11);
   467 		test.Printf(_L("mod11, iter=%d, ret=%d, exp=%d"),i,r,ModuleResultsB[i]);
   468 		test(r==ModuleResultsB[i]);
   469 		lib.Close();
   470 		r=lib.Load(KDll6);
   471 		test.Printf(_L("mod6, iter=%d, ret=%d, exp=%d"),i,r,ModuleResultsB[i]);
   472 		test(r==ModuleResultsB[i]);
   473 		lib.Close();
   474 		r=lib.Load(KDll7);
   475 		test.Printf(_L("mod7, iter=%d, ret=%d, exp=%d"),i,r,ModuleResultsC[i]);
   476 		test(r==ModuleResultsC[i]);
   477 		lib.Close();
   478 		}
   479 
   480 	}
   481 */
   482 /*
   483 void TestLoading()
   484 //
   485 //	test loading various dependancies
   486 //
   487 	{
   488 	for(TInt tstcase=0; tstcase < KTestCases; tstcase++)	//each test case
   489 		{
   490 		for(TInt mod=0 ; mod < 16 ; mod++)	//each module
   491 			{
   492 			ModifyModuleCapabilties(ModuleCaps[mod][tstcase],mod);
   493 			}
   494 			TestLoad(tstcase);	
   495 		}
   496 	}
   497 */
   498 
   499 
   500 _LIT(KSysHash,"?:\\Sys\\Hash\\");
   501 const TInt KSlash='\\';
   502 
   503 /**
   504     Creates a hash file in c:\\sys\\hash for a given file
   505     
   506     @param aFileName full path to the file, which hash is to be created
   507 */
   508 void CreateFileHash(const TDesC& aFileName)
   509 	{
   510 
   511 	test.Printf(_L("Create Hash for the file %S\n"), &aFileName);
   512 
   513 	TInt readsize = 1024*2;//to go into header
   514 	HBufC8* block0 = HBufC8::NewL(readsize); 
   515 
   516 	TPtr8 fileblock0(block0->Des());
   517 	CSHA1* hasher=CSHA1::NewL(); 
   518 	
   519 	RFile fTarget;
   520 	TInt r= fTarget.Open(TheFs, aFileName, EFileRead);
   521 	test(r==KErrNone);
   522 	TInt size;
   523 	r=fTarget.Size(size);
   524 	TInt offset=0;
   525 	do	{
   526 		if((size - offset) < readsize)
   527 			readsize = (size - offset);
   528 		r=fTarget.Read(offset, fileblock0, readsize);
   529 		test(r==KErrNone);
   530 		hasher->Update(fileblock0);
   531 		offset+=readsize;
   532 		}
   533 	while(offset < size); 
   534 
   535 	r=fTarget.Read(fileblock0, (size - offset));			
   536 	test(r==KErrNone);
   537 	hasher->Update(fileblock0);
   538 
   539 	TBuf8<20> hash;//only outputs a 20 byte hash
   540 	hash = hasher->Final();
   541 	fTarget.Close();
   542 		
   543 //	const TUint8 * hptr=hash.Ptr();
   544 //	for(TInt i=0;i<20;i++)
   545 //		{
   546 //		RDebug::Print(_L("install hash byte %d = 0x%x\n"),i,hptr[i]);
   547 //		}
   548 	
   549 	delete block0;
   550 	delete hasher;
   551 	TEntry entry;
   552 	r=TheFs.Entry(aFileName,entry);
   553 	test(r==KErrNone);
   554 	RFile fHash;
   555 	TBuf<50> hashfile;
   556 	hashfile = KSysHash;
   557 	hashfile[0] = (TUint8) RFs::GetSystemDriveChar();
   558 	TInt slashpos = aFileName.LocateReverse(KSlash);
   559 	hashfile += aFileName.Mid(slashpos+1);
   560 	
   561 	//-- create directory for dll hashes
   562 	r=TheFs.MkDirAll(hashfile);
   563 	test(r==KErrNone || r==KErrAlreadyExists);
   564 	
   565     //-- copy / replace given dll hash 
   566 	r=fHash.Replace(TheFs, hashfile, EFileWrite);
   567 	test(r==KErrNone);
   568 	r=fHash.Write(hash);
   569 	test(r==KErrNone);
   570 	r=fHash.Size(size);
   571 	test(r==KErrNone);
   572 	test.Printf(_L("hash file size=%d\n"),size);
   573 	fHash.Close();
   574 	
   575     }
   576 
   577 
   578 /**
   579     Corrups a file.
   580     
   581     @param  aFileName   a full path to the file to corrupt
   582     @return KErrNone on success
   583 */
   584 static TInt CorruptFile(const TDesC& aFileName)
   585 {
   586 	test.Printf(_L("Corrupting file %S\n"), &aFileName);
   587 
   588     RFile corrFile;
   589     CleanupClosePushL(corrFile);	
   590 
   591     TInt r=corrFile.Open(TheFs, aFileName, EFileWrite);
   592 	if(r != KErrNone)
   593         return r;
   594 	
   595     TInt size;
   596 	r=corrFile.Size(size);
   597 	if(r != KErrNone)
   598         return r;
   599 	
   600     TBuf8<2> dat;
   601 	r=corrFile.Read(size - 5, dat);
   602 	if(r != KErrNone)
   603         return r;
   604 
   605 	dat[0] = (TUint8) ~dat[0];
   606 	
   607     r=corrFile.Write(size - 5, dat);
   608 	if(r != KErrNone)
   609         return r;
   610 
   611     CleanupStack::PopAndDestroy(1); //-- corrFile
   612 
   613     return KErrNone;
   614 }
   615 
   616 
   617 #ifndef WIN32
   618 void TestRemovableMediaWithHash()
   619 //
   620 //	test loading from removable media and substed drives Both should fail
   621 //
   622 	{
   623 	test.Next(_L("Testing Removable Media with hashing\n"));
   624 	TInt r=0;
   625 	CFileMan* fileMan=NULL;
   626 	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   627 	test(r==KErrNone);
   628 
   629 	TBuf<30> dllfilenames;
   630 	TBuf<30> dlldestination;
   631 	RLibrary lib;
   632 	
   633     for(TInt i=0;i<14;i++)
   634 		{
   635 		dllfilenames=KNewDllName;
   636 		dllfilenames.AppendNum(i);
   637 		dllfilenames +=KDllExt;
   638 		
   639 		dlldestination=KNewDllName;
   640 		dlldestination[0]=gDriveToTest;
   641 		dlldestination.AppendNum(i);
   642 		dlldestination+=KDllExt;
   643 		
   644 		r=TheFs.MkDirAll(dlldestination);
   645 		test.Printf(_L("MkDirAll(%S) returned %d\n"), &dlldestination, r);
   646 		test(r==KErrNone || r==KErrAlreadyExists);
   647 		r=fileMan->Copy(dllfilenames,dlldestination, 0); 
   648         test(r==KErrNone || r==KErrAlreadyExists);	
   649 
   650 
   651 		//take hash of binary
   652 		CreateFileHash(dlldestination);
   653 
   654 		//load binary as is
   655 		r=lib.Load(dlldestination);
   656 		RDebug::Print(_L("t_ldrcheck: loading  %S returned %d\n"),&dlldestination, r);
   657 
   658 		User::After(100000);
   659 		test(r==KErrNone);
   660 		lib.Close();
   661 		test.Printf(_L("Close lib on untouched load\n"));
   662 
   663         //-- corrupt dll
   664         r = CorruptFile(dlldestination);
   665         test(r==KErrNone);
   666 		test.Printf(_L("Binary %S now corrupted\n"), &dlldestination);
   667 
   668 		//load should fail
   669 		test.Printf(_L("loading %S\n"),&dlldestination);
   670 		r=lib.Load(dlldestination);
   671 		test.Printf(_L("load of %S returned %d\n"),&dlldestination,r);
   672 		test(r==KErrPermissionDenied);//as this process has Root caps and the dlls should be loaded with non
   673 		lib.Close();
   674 		test.Printf(_L("Lib close 1\n"));
   675 
   676 		r=TheFs.Delete(dlldestination);
   677 		test.Printf(_L("Delete ret=%d\n"),r);
   678 		test(r==KErrNone);
   679 		}//for(TInt i=0;i<14;i++)
   680 
   681 
   682 	TBuf<30> exefilenames;
   683 	TBuf<30> exedestination;
   684 	RProcess p;
   685 	TBuf<16> cmd;
   686 
   687 	for(TInt j=14;j<16;j++)
   688 		{
   689 		exefilenames=KNewExeName;;
   690 		exefilenames.AppendNum(j);
   691 		exefilenames +=KExeExt;
   692 		
   693 		exedestination=KNewExeName;
   694 		exedestination[0]=gDriveToTest;
   695 		exedestination.AppendNum(j);
   696 		exedestination+=KExeExt;
   697 
   698 		r=fileMan->Copy(exefilenames,exedestination,0);
   699 		test(r==KErrNone || r==KErrAlreadyExists);
   700 
   701 		CreateFileHash(exedestination);
   702 
   703 		r=p.Create(exedestination, cmd);
   704 		test(r==KErrNone);
   705 		p.Terminate(0);
   706 		p.Close();
   707 
   708         r = CorruptFile(exedestination);
   709         test(r==KErrNone);
   710 		
   711 		test.Printf(_L("Binary %S now corrupted\n"), &exedestination);
   712 
   713 		r=p.Create(exedestination, cmd);
   714 		test(r==KErrPermissionDenied);
   715 //		p.Terminate(0);	DON'T DO THIS SINCE CREATION FAILED - HANDLE NOT OPENED
   716 		p.Close();
   717 		
   718 		r=TheFs.Delete(exedestination);
   719 		test(r==KErrNone);
   720 
   721 		}
   722 	delete fileMan;
   723 	}
   724 
   725 /*
   726 void TestRemovableMedia()
   727 //
   728 //	test loading from removable media and substed drives Both should fail
   729 //
   730 	{
   731 	test.Next(_L("Testing Removeable Media"));
   732 	TInt r=0;
   733 	CFileMan* fileMan=NULL;
   734 	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   735 	test(r==KErrNone);
   736 
   737 	TBuf<30> dllfilenames;
   738 	TBuf<30> dlldestination;
   739 	RLibrary lib;
   740 	for(TInt i=0;i<14;i++)
   741 		{
   742 		dllfilenames=KNewDllName;
   743 		dllfilenames.AppendNum(i);
   744 		dllfilenames +=KDllExt;
   745 		
   746 		dlldestination=KNewDllName;
   747 		dlldestination[0]='D';
   748 		dlldestination.AppendNum(i);
   749 		dlldestination+=KDllExt;
   750 		
   751 		r=TheFs.MkDirAll(dlldestination);
   752 		test(r==KErrNone || r==KErrAlreadyExists);
   753 
   754 		r=fileMan->Copy(dllfilenames,dlldestination, 0);
   755 		test(r==KErrNone || r==KErrAlreadyExists);	
   756 		
   757 		r=lib.Load(dlldestination);
   758 		test.Printf(_L("load %S ret=%d"),&dlldestination,r);
   759 		RDebug::Print(_L("r=%d"),r);
   760 		test(r==KErrPermissionDenied);//as this process has Root caps and the dlls should be loaded with non
   761 		lib.Close();
   762 		}
   763 
   764 	TBuf<30> exefilenames;
   765 	TBuf<30> exedestination;
   766 	RProcess p;
   767 	TBuf<16> cmd;
   768 
   769 	for(TInt j=14;j<16;j++)
   770 		{
   771 		exefilenames=KNewExeName;;
   772 		exefilenames.AppendNum(j);
   773 		exefilenames +=KExeExt;
   774 		
   775 		exedestination=KNewExeName;
   776 		exedestination[0]='D';
   777 		exedestination.AppendNum(j);
   778 		exedestination+=KExeExt;
   779 
   780 		r=fileMan->Copy(exefilenames,exedestination,0);
   781 		test(r==KErrNone || r==KErrAlreadyExists);
   782 
   783 		r=p.Create(exedestination, cmd);
   784 		test(r==KErrPermissionDenied);
   785 		p.Close();
   786 		}
   787 	delete fileMan;
   788 	}
   789 */
   790 
   791 void TestNonSystemSubsted()
   792 //
   793 //	Test an internal drive on non system dir
   794 //
   795 	{
   796 	test.Next(_L("Testing Non System and Subst"));
   797 	TInt r=0;
   798 	CFileMan* fileMan=NULL;
   799 	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   800 	test(r==KErrNone);
   801 
   802 	TBuf<50> dllfilenames;
   803 	TBuf<50> dlldestination;
   804 	TBuf<50> substed;
   805 	RLibrary lib;
   806 	for(TInt i=0;i<14;i++)
   807 		{
   808 		dllfilenames=KNewDllName;
   809 		dllfilenames.AppendNum(i);
   810 		dllfilenames +=KDllExt;
   811 
   812 		dlldestination=KAnyDirDll;
   813       dlldestination+=_L("_X");
   814 		dlldestination.AppendNum(i);
   815 		dlldestination+=KDllExt;
   816 
   817 		if(i==0)
   818 			{
   819 			r=TheFs.MkDirAll(dlldestination);
   820 			test(r==KErrNone || r==KErrAlreadyExists);
   821 			r=TheFs.SetSubst(KAnyDirShort(), EDriveN);	//set up n as the substed drive
   822 			test(r==KErrNone);
   823 			}
   824 		
   825 		RDebug::Print(_L("copying from %S to %S"),&dllfilenames,&dlldestination);
   826 		r=fileMan->Copy(dllfilenames,dlldestination, 0);
   827 		test(r==KErrNone || r==KErrAlreadyExists);	
   828 
   829       //
   830       // Test that loading from fully qualified path fails
   831       // (ie - C:\Anyoldname\sys\bin\DLLTS_X0.DLL)
   832       //
   833       r=lib.Load(dlldestination);
   834       RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &dlldestination, r);
   835       test(r==KErrNotFound);
   836 
   837       //
   838       // Test that loading from substituted drive fails
   839       // (ie - N:\sys\bin\DLLTS_X0.DLL, where N:\ == C:\Anyoldname\)
   840       //
   841 		substed=KJDllName;
   842       substed+=_L("_X");
   843 		substed.AppendNum(i);
   844 		substed+=KDllExt;
   845 		
   846 		r=lib.Load(substed);
   847       RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &substed, r);
   848       test(r==KErrNotFound);
   849 
   850       //
   851       // Test that loader search does not find substituted drives when
   852       // loading library with no drive or path specified.
   853       //
   854       substed=KJDllNameOnly;
   855       substed+=_L("_X");
   856       substed.AppendNum(i);
   857       substed+=KDllExt;
   858 
   859       r=lib.Load(substed);
   860       RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &substed, r);
   861       test(r==KErrNotFound);
   862 		}
   863 
   864 	TBuf<50> exefilenames;
   865 	TBuf<50> exedestination;
   866 	RProcess p;
   867 	TBuf<16> cmd;
   868 
   869 	for(TInt j=14;j<16;j++)
   870 		{
   871 		exefilenames=KNewExeName;;
   872 		exefilenames.AppendNum(j);
   873 		exefilenames +=KExeExt;
   874 
   875 		exedestination=KAnyDirExe;
   876       exedestination+=_L("_X");
   877 		exedestination.AppendNum(j);
   878 		exedestination+=KExeExt;
   879 
   880 		RDebug::Print(_L("copying from %S to %S"),&exefilenames,&exedestination);
   881 		r=fileMan->Copy(exefilenames,exedestination,0);
   882 		test(r==KErrNone || r==KErrAlreadyExists);
   883 
   884       //
   885       // Test that loading from fully qualified path fails
   886       // (ie - C:\Anyoldname\sys\bin\EXETS_X14.EXE)
   887       //
   888       r=p.Create(exedestination, cmd);
   889       RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &exedestination, r);
   890       test(r==KErrNotFound);
   891 
   892       //
   893       // Test that loading from substituted drive fails
   894       // (ie - N:\sys\bin\EXETS_X14.EXE, where N:\ == C:\Anyoldname\)
   895       //
   896 		substed=KJExeName;
   897       substed+=_L("_X");
   898 		substed.AppendNum(j);
   899 		substed+=KExeExt;
   900 
   901 		r=p.Create(substed, cmd);
   902       RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &substed, r);
   903       test(r==KErrNotFound);
   904 
   905       //
   906       // Test that loader search does not find substituted drives when
   907       // loading process with no drive or path specified.
   908       //
   909       substed=KJExeNameOnly;
   910       substed+=_L("_X");
   911       substed.AppendNum(j);
   912       substed+=KExeExt;
   913 
   914       r=p.Create(substed, cmd);
   915       RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &substed, r);
   916       test(r==KErrNotFound);
   917 		}
   918 	delete fileMan;
   919 
   920 	// Clear the substituted drive
   921 	r=TheFs.SetSubst(KNullDesC, EDriveN);	
   922 	test(r==KErrNone);
   923 	}
   924 
   925 /*
   926 void TestSystemBinSubsted()
   927 //
   928 //	Test an internal drive on system dir
   929 //
   930 	{
   931 	test.Next(_L("Testing System bin -> Subst"));
   932 	TInt r=0;
   933 	CFileMan* fileMan=NULL;
   934 	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   935 	test(r==KErrNone);
   936 
   937 	TBuf<50> dllfilenames;
   938 	TBuf<50> substed;
   939 	RLibrary lib;
   940 	r=TheFs.SetSubst(KSystemLibs, 14);	//set up O as the substed drive
   941 	test(r==KErrNone);
   942 	for(TInt i=0;i<14;i++)
   943 		{
   944 		RDebug::Print(_L("doing modify caps = %d"),i);
   945 		ModifyModuleCapabilties(KCapabilityRoot,i);
   946 		dllfilenames=KNewDllName;
   947 		dllfilenames.AppendNum(i);
   948 		dllfilenames +=KDllExt;
   949 		
   950 		RDebug::Print(_L("Module = %S"),&dllfilenames);
   951 		r=lib.Load(dllfilenames);
   952 		test(r==KErrNone);//as this process has Root caps and the dlls should be loaded with non
   953 		substed=_L("O:\\DLLTS");
   954 		substed.AppendNum(i);
   955 		substed+=KDllExt;
   956 	
   957 		r=lib.Load(substed);
   958 		test(r==KErrNone);
   959 		lib.Close();
   960 		}
   961 	TBuf<50> exefilenames;
   962 	RProcess p;
   963 	TBuf<16> cmd;
   964 
   965 	for(TInt j=14;j<16;j++)
   966 		{
   967 		exefilenames=KNewExeName;
   968 		exefilenames.AppendNum(j);
   969 		exefilenames +=KExeExt;
   970 		ModifyModuleCapabilties(KCapabilityRoot,j);
   971 		r=p.Create(exefilenames, cmd);
   972 		test(r==KErrNone);
   973 		p.Terminate(0);
   974 		p.Close();
   975 
   976 
   977 		substed=_L("O:\\EXETS");
   978 		substed.AppendNum(j);
   979 		substed+=KExeExt;
   980 		RDebug::Print(_L("substed=%S"),&substed);
   981 		r=p.Create(substed, cmd);
   982 		test(r==KErrNone);
   983 		p.Terminate(0);
   984 		p.Close();
   985 		}
   986 	delete fileMan;
   987 	}
   988 */
   989 #endif
   990 
   991 
   992 
   993 void HashBM()
   994 //
   995 //	time how long to load a small dll 100 times and one large one
   996 //
   997 	{
   998 	test.Next(_L("Next Do Bench Mark\n"));
   999 	TBuf<30> dlldestination;
  1000 	dlldestination=KNewDllName;
  1001 	dlldestination[0]='D';
  1002 	dlldestination.AppendNum(1);
  1003 	dlldestination+=KDllExt;
  1004 	RLibrary lib;
  1005 	TInt r=0;
  1006 	TUint32 startcount = User::NTickCount();
  1007 	for (TInt i=0;i<100;i++)
  1008 		{
  1009 		r=lib.Load(dlldestination);
  1010 		lib.Close();
  1011 		}
  1012 	TUint32 endcount = User::NTickCount();
  1013 	test(r==KErrNone);
  1014 	RDebug::Print(_L("100 * 4k dll \n"));
  1015 	RDebug::Print(_L("start count=%d, end count=%d, dif=%d\n"),startcount,endcount,endcount-startcount);
  1016 
  1017 	startcount = User::NTickCount();
  1018 	r=lib.Load(_L("D:\\sys\\bin\\euser.dll"));
  1019 	lib.Close();
  1020 	endcount = User::NTickCount();
  1021 	RDebug::Print(_L("r=%d")); 
  1022 //	test(r==KErrNone);
  1023 	RDebug::Print(_L("1 * 233k dll \n"));
  1024 	RDebug::Print(_L("start count=%d, end count=%d, dif=%d\n"),startcount,endcount,endcount-startcount);
  1025 	}
  1026 
  1027 //-------------------------------------------------------
  1028 
  1029 /**
  1030     testing RLoader::CheckLibraryHash() API
  1031 */
  1032 void  TestCheckLibraryHash()
  1033 {
  1034     test.Next(_L("Testing CheckLibraryHash API\n"));
  1035 
  1036 	TInt r=0;
  1037 	CFileMan* pFileMan=NULL;
  1038 	TRAP(r,pFileMan=CFileMan::NewL(TheFs));
  1039 	test(r==KErrNone);
  1040 
  1041     RLoader loader;
  1042 	r=loader.Connect();
  1043 	test(r==KErrNone);
  1044     
  1045     //-- 1. copy test DLL to the specified drive and create hash file for it.
  1046 	TBuf<40> dllFileName;
  1047 	TBuf<40> dllDestination;
  1048 	
  1049 	const TInt KDllNumber = 0;
  1050 
  1051     dllFileName=KDllfilename;
  1052 	dllFileName.AppendNum(KDllNumber);
  1053 	dllFileName+=KDllExt;
  1054 	
  1055 	dllDestination=KNewDllName;
  1056 	dllDestination[0]=(TUint16)gDriveToTest;
  1057 	dllDestination.AppendNum(KDllNumber);
  1058 	dllDestination+=KDllExt;
  1059 
  1060     test.Printf(_L("Copy %S to %S\n"), &dllFileName, &dllDestination);
  1061 
  1062 	r=TheFs.MkDirAll(dllDestination);
  1063 	test(r==KErrNone || r==KErrAlreadyExists);
  1064 
  1065 	r=pFileMan->Copy(dllFileName, dllDestination);
  1066 	test(r==KErrNone || r==KErrAlreadyExists);
  1067 
  1068 	r=pFileMan->Attribs(dllDestination, 0, KEntryAttReadOnly, TTime(0) ,0);
  1069 	test(r==KErrNone);
  1070 
  1071     test.Printf(_L("Creating Dll hash.\n"));
  1072     CreateFileHash(dllDestination);
  1073 
  1074     //-- 2. check if the hash exists
  1075     r=loader.CheckLibraryHash(dllDestination);
  1076     test(r==KErrNone);
  1077     test.Printf(_L("Dll hash exists.\n"));
  1078 
  1079     //-- 2.1 check if the hash exists and valid
  1080     r=loader.CheckLibraryHash(dllDestination, ETrue);
  1081     test(r==KErrNone);
  1082     
  1083     test.Printf(_L("Dll hash exists and valid.\n"));
  1084 
  1085     //-- 3. corrupt dll
  1086     r = CorruptFile(dllDestination);
  1087     test(r==KErrNone);
  1088 
  1089     //-- 3.1 check that the hash exists, but is incorrect.
  1090     r=loader.CheckLibraryHash(dllDestination);
  1091     test(r==KErrNone);
  1092     test.Printf(_L("Dll hash exists.\n"));
  1093 
  1094     r=loader.CheckLibraryHash(dllDestination, ETrue);
  1095     test(r==KErrCorrupt);
  1096     
  1097     test.Printf(_L("Dll hash exists and INVALID.\n"));
  1098 
  1099     //-- 4. try to locte hash fo the unexisting dll.
  1100     r=loader.CheckLibraryHash(_L("Z:\\sys\\bin\\NotExist.dll"));
  1101     test(r==KErrNotFound);
  1102 
  1103     loader.Close();
  1104 
  1105     delete pFileMan;
  1106 }
  1107 
  1108 //-------------------------------------------------------
  1109 
  1110 void ParseCommandArguments()
  1111 //
  1112 //
  1113 //
  1114 	{
  1115 	TBuf<0x100> cmd;
  1116 	User::CommandLine(cmd);
  1117 	TLex lex(cmd);
  1118 	TPtrC token=lex.NextToken();
  1119 	TFileName thisfile=RProcess().FileName();
  1120 	if (token.MatchF(thisfile)==0)
  1121 		{
  1122 		token.Set(lex.NextToken());
  1123 		}
  1124 	test.Printf(_L("CLP=%S"),&token);
  1125 
  1126 	if(token.Length()!=0)		
  1127 		{
  1128 		gDriveToTest=token[0];
  1129 		gDriveToTest.UpperCase();
  1130 		}
  1131 	else						
  1132 		gDriveToTest='D';	//default to D:
  1133 	}
  1134 
  1135 
  1136 //-------------------------------------------------------
  1137 
  1138 void TestExes()
  1139 {
  1140     test.Next(_L("Testing Exes presence.\n"));
  1141 	
  1142     TInt r;
  1143 
  1144     RProcess ap;
  1145 	r=LoadExe(14,ap);
  1146 	test(r==KErrNone);
  1147 	ap.Terminate(0);
  1148 	ap.Close();
  1149 	
  1150     r=LoadExe(15,ap);
  1151 	test(r==KErrNone);
  1152 	ap.Terminate(0);
  1153 	ap.Close();
  1154 }
  1155 
  1156 
  1157 //-------------------------------------------------------
  1158 
  1159 static void CallTestsL(void)
  1160 {
  1161 
  1162 #ifdef __WINS__
  1163         test.Printf(_L("Not testing on WINS !\n"));
  1164         return;
  1165 #else
  1166 
  1167         TestExes();
  1168     //	TestGetCapability();
  1169 	    CopyModules();				//copies modules from ROM to disk so they may be modified
  1170     //	TestLoading();
  1171     //	TestLoadLibrary();
  1172 	
  1173 
  1174 #ifndef WIN32
  1175 	    if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin))
  1176 		    TestRemovableMediaWithHash();
  1177 
  1178     //	TestSystemBinSubsted();
  1179     //	TestRemovableMedia();
  1180 	    TestNonSystemSubsted();
  1181 #endif //WIN32
  1182     //	HashBM();
  1183 
  1184         TestCheckLibraryHash();
  1185 
  1186 #endif //#ifndef __WINS__
  1187     
  1188 }
  1189 
  1190 //-------------------------------------------------------
  1191 //
  1192 //	main
  1193 //
  1194 GLDEF_C TInt E32Main()
  1195 	{
  1196 	ParseCommandArguments(); //need this for drive letter to test
  1197 
  1198 	test.Title();
  1199 	test.Start(_L("Setup\n"));
  1200 	CTrapCleanup* cleanup;
  1201 	cleanup=CTrapCleanup::New();
  1202 	__UHEAP_MARK;
  1203 
  1204 	TBuf<20> sessPath;
  1205 	TInt r=0;	
  1206 	r=TheFs.Connect();
  1207 	test(r==KErrNone);
  1208 	r=TheFs.SessionPath(sessPath);
  1209 	test(r==KErrNone);
  1210 
  1211 	TInt drive;
  1212 	RFs::CharToDrive(gDriveToTest, drive);
  1213 	TDriveInfo info;
  1214 	r=TheFs.Drive(info, drive);
  1215 	test(r==KErrNone);
  1216 	
  1217     if((info.iDriveAtt & KDriveAttRemovable) == 0)
  1218 	{
  1219         test.Printf(_L("Not testing on non-removable media !\n"));
  1220     }
  1221     else
  1222     {//-- testing on removable media
  1223         TRAP(r,CallTestsL());
  1224     }
  1225 
  1226 	TheFs.Close();
  1227 	test.End();
  1228 	test.Close();
  1229 
  1230 	__UHEAP_MARKEND;
  1231 	delete cleanup;
  1232 
  1233 	return KErrNone;
  1234 	}
  1235 
  1236