os/kernelhwsrv/kerneltest/f32test/loader/security/t_ldrcheck.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/loader/security/t_ldrcheck.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1236 @@
     1.4 +// Copyright (c) 1999-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 the License "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 +// f32test\loader\t_ldrcheck.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +
    1.22 +#include "dlltree.h"
    1.23 +#include <e32uid.h>
    1.24 +#include <hal.h>
    1.25 +#include <e32wins.h>
    1.26 +#include <e32svr.h>
    1.27 +#include <e32test.h>
    1.28 +#include <f32file.h>
    1.29 +#include <f32dbg.h>
    1.30 +#include <f32image.h>
    1.31 +#include "t_hash.h"
    1.32 +
    1.33 +#if defined (WIN32) && !defined (__X86GCC__)
    1.34 +#include <emulator.h>
    1.35 +#include <stdlib.h>
    1.36 +#endif
    1.37 +
    1.38 +
    1.39 +RTest test(_L("T_LDRCheck"));
    1.40 +RFs TheFs;
    1.41 +GLDEF_D TChar gDriveToTest;
    1.42 +
    1.43 +
    1.44 +/**
    1.45 +    Copy modules from z:\\sys\\bin to c:\\sys\\bin
    1.46 +*/
    1.47 +void CopyModules()
    1.48 +	{
    1.49 +	test.Next(_L("Copy Modules from ROM to disk"));
    1.50 +
    1.51 +	TInt r;
    1.52 +	TTime modtime;
    1.53 +
    1.54 +	r=TheFs.MkDirAll(KSystemLibs);
    1.55 +	test(r==KErrNone || r==KErrAlreadyExists);
    1.56 +
    1.57 +	CFileMan* fileMan=NULL;
    1.58 +	TRAP(r,fileMan=CFileMan::NewL(TheFs));
    1.59 +	test(r==KErrNone);
    1.60 +
    1.61 +	TBuf<40> dllfilenames;
    1.62 +	TBuf<40> dlldestination;
    1.63 +	
    1.64 +	for(TInt i=0;i<14;i++)
    1.65 +		{
    1.66 +		dllfilenames=KDllfilename;
    1.67 +		dllfilenames.AppendNum(i);
    1.68 +		dllfilenames +=KDllExt;
    1.69 +		dlldestination=KNewDllName;
    1.70 +		dlldestination.AppendNum(i);
    1.71 +		dlldestination+=KDllExt;
    1.72 +#ifndef WIN32
    1.73 +		r=fileMan->Copy(dllfilenames,dlldestination, 0);
    1.74 +		test(r==KErrNone || r==KErrAlreadyExists);
    1.75 +
    1.76 +		r=fileMan->Attribs(dlldestination,0, KEntryAttReadOnly,modtime,0);
    1.77 +		test(r==KErrNone);
    1.78 +#endif
    1.79 +		}
    1.80 +
    1.81 +	TBuf<40> exefilenames;
    1.82 +	TBuf<40> exedestination;
    1.83 +
    1.84 +	for(TInt j=14;j<16;j++)
    1.85 +		{
    1.86 +		exefilenames=KExefilename;
    1.87 +		exefilenames.AppendNum(j);
    1.88 +		exefilenames +=KExeExt;
    1.89 +		exedestination=KNewExeName;
    1.90 +		exedestination.AppendNum(j);
    1.91 +		exedestination+=KExeExt;
    1.92 +#ifndef WIN32
    1.93 +		r=fileMan->Copy(exefilenames,exedestination,0);
    1.94 +		test(r==KErrNone || r==KErrAlreadyExists);
    1.95 +
    1.96 +		r=fileMan->Attribs(exedestination, 0, KEntryAttReadOnly, modtime, 0);
    1.97 +		test(r==KErrNone);
    1.98 +#endif
    1.99 +		}
   1.100 +	delete fileMan;
   1.101 +	}
   1.102 +
   1.103 +
   1.104 +
   1.105 +#ifndef WIN32
   1.106 +/*
   1.107 +void ModifyModuleCapabilties(TUint32 aNewCap, TInt aModuleNumber)
   1.108 +//
   1.109 +//	Assign new capabilties to a module using raw disk write
   1.110 +//
   1.111 +	{
   1.112 +//	test.Next(_L("Modify Module Capabilities"));
   1.113 +	
   1.114 +	test(aModuleNumber<=15 && aModuleNumber>=0);	
   1.115 +	
   1.116 +	TBuf<40> dlldestination;
   1.117 +	RFile modFile;
   1.118 +	TInt r=0;
   1.119 +
   1.120 +	SCapabilitySet caps;
   1.121 +	memset(&caps,0,sizeof(caps));
   1.122 +	caps[0] = aNewCap;
   1.123 +	TPtrC8 theNewCaps((TUint8*)(&caps),sizeof(caps));
   1.124 +
   1.125 +	if(aModuleNumber < 14)
   1.126 +		{
   1.127 +		dlldestination=KNewDllName;
   1.128 +		dlldestination.AppendNum(aModuleNumber);
   1.129 +		dlldestination+=KDllExt;
   1.130 +		}
   1.131 +	else
   1.132 +		{
   1.133 +		dlldestination=KNewExeName;
   1.134 +		dlldestination.AppendNum(aModuleNumber);
   1.135 +		dlldestination+=KExeExt;
   1.136 +		}
   1.137 +
   1.138 +	r=modFile.Open(TheFs, dlldestination, EFileWrite);
   1.139 +	test(r==KErrNone);
   1.140 +	r=modFile.Write(_FOFF(E32ImageHeaderV,iS.iCaps), theNewCaps,4);
   1.141 +	test(r==KErrNone);
   1.142 +	modFile.Close();	
   1.143 +
   1.144 +	RLibrary lib;
   1.145 +	TCapabilitySet theCaps;
   1.146 +	RLibrary::TInfoBuf info;
   1.147 +
   1.148 +	lib.GetInfo(dlldestination, info);
   1.149 +	theCaps=info().iSecurityInfo.iCaps;
   1.150 +
   1.151 +	lib.Close();
   1.152 +	test(theCaps==aNewCap);
   1.153 +
   1.154 +	}
   1.155 +*/
   1.156 +#else
   1.157 +/*
   1.158 +void ModifyModuleCapabilties(TUint32 aNewCap, TInt aModuleNumber)
   1.159 +//
   1.160 +//	Wins version (poking values is not quite so simple
   1.161 +//
   1.162 +	{
   1.163 +//	test.Next(_L("Modify Module Capabilities"));
   1.164 +	test(aModuleNumber<=15 && aModuleNumber>=0);	
   1.165 +	
   1.166 +	TBuf<40> dlldestination;
   1.167 +	TInt r=0;
   1.168 +
   1.169 +	TPtrC8 theNewCaps((TUint8*)(&aNewCap),sizeof(TUint32));
   1.170 +
   1.171 +	
   1.172 +	if(aModuleNumber < 14)
   1.173 +		{
   1.174 +		dlldestination=KNewDllName;
   1.175 +		dlldestination.AppendNum(aModuleNumber);
   1.176 +		dlldestination+=KDllExt;
   1.177 +		}
   1.178 +	else
   1.179 +		{
   1.180 +		dlldestination=KNewExeName;
   1.181 +		dlldestination.AppendNum(aModuleNumber);
   1.182 +		dlldestination+=KExeExt;
   1.183 +		}
   1.184 +
   1.185 +	RLibrary lib2;
   1.186 +	TCapabilitySet theCaps2;
   1.187 +	RLibrary::TInfoBuf info;
   1.188 +
   1.189 +	lib2.GetInfo(dlldestination, info);
   1.190 +	theCaps2=info().iSecurityInfo.iCaps;
   1.191 +
   1.192 +	lib2.Close();
   1.193 +			
   1.194 +	TBuf<MAX_PATH> fileName;
   1.195 +	r = MapEmulatedFileName(fileName, dlldestination);
   1.196 +
   1.197 +	HANDLE hFile=Emulator::CreateFile((LPCTSTR)fileName.PtrZ(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
   1.198 +	if (hFile==INVALID_HANDLE_VALUE)
   1.199 +		return;
   1.200 +	DWORD ret;
   1.201 +	TBuf8<sizeof(TCheckedUid)> checkedUidBuf;
   1.202 +	checkedUidBuf.SetLength(sizeof(TCheckedUid));
   1.203 +	ReadFile(hFile,&checkedUidBuf[0],sizeof(TCheckedUid),&ret,NULL);
   1.204 +	if (ret!=sizeof(TCheckedUid))
   1.205 +		goto close;
   1.206 +
   1.207 +		//Look at PE file for UID section
   1.208 +		{
   1.209 +		const TInt KPeHeaderAddrAddr=0x3c;
   1.210 +		const TInt KPeHeaderAddrSize=0x01;
   1.211 +		const TInt KNumberOfSectionsOffset=0x06;
   1.212 +		const TInt KNumberOfSectionsSize=0x02;
   1.213 +		const TInt KSectionTableOffset=0xf8;
   1.214 +		const TInt KSectionHeaderSize=0x28;
   1.215 +		const TInt KSectionNameLength=0x08;
   1.216 +		const TInt KPtrToRawDataOffset=0x14;
   1.217 +		const TInt KPtrToRawDataSize=0x04;
   1.218 +		const TText8 peText[4]={'P','E',0,0};
   1.219 +		const TText8 uidText[8]={'.','S','Y','M','B','I','A','N'};
   1.220 +		
   1.221 +	//Read address of start of PE header
   1.222 +		if (SetFilePointer(hFile,KPeHeaderAddrAddr,0,FILE_BEGIN)==0xFFFFFFFF)
   1.223 +			goto close;
   1.224 +		TInt peAddr=0;
   1.225 +		ReadFile(hFile,&peAddr,KPeHeaderAddrSize,&ret,NULL);
   1.226 +		if (ret!=KPeHeaderAddrSize)
   1.227 +			goto close;
   1.228 +		
   1.229 +	//Check it really is the start of PE header
   1.230 +		if (SetFilePointer(hFile,peAddr,0,FILE_BEGIN)==0xFFFFFFFF)
   1.231 +			goto close;
   1.232 +		TText8 text[4];
   1.233 +		ReadFile(hFile,text,4,&ret,NULL);
   1.234 +		if (*(TInt32*)text!=*(TInt32*)peText)
   1.235 +			goto close;
   1.236 +		
   1.237 +	//Read number of sections
   1.238 +		if (SetFilePointer(hFile,peAddr+KNumberOfSectionsOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   1.239 +			goto close;
   1.240 +		TInt sections=0;
   1.241 +		ReadFile(hFile,&sections,KNumberOfSectionsSize,&ret,NULL);
   1.242 +		if (ret!=KNumberOfSectionsSize)
   1.243 +			goto close;
   1.244 +
   1.245 +	//Go through section headers looking for UID section
   1.246 +		if (SetFilePointer(hFile,peAddr+KSectionTableOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   1.247 +			goto close;
   1.248 +		TInt i=0;
   1.249 +		for(;i<sections;i++)
   1.250 +			{
   1.251 +			TText8 name[KSectionNameLength];
   1.252 +			ReadFile(hFile,name,KSectionNameLength,&ret,NULL);
   1.253 +			if (ret!=KSectionNameLength)
   1.254 +				goto close;
   1.255 +			if (*(TInt64*)name==*(TInt64*)uidText)
   1.256 +				break;
   1.257 +			if (SetFilePointer(hFile,KSectionHeaderSize-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
   1.258 +				goto close;
   1.259 +			}
   1.260 +		if (i==sections)
   1.261 +			goto close;
   1.262 +
   1.263 +	//Read RVA/Offset
   1.264 +		if (SetFilePointer(hFile,KPtrToRawDataOffset-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
   1.265 +			goto close;
   1.266 +		TInt uidOffset;
   1.267 +		ReadFile(hFile,&uidOffset,KPtrToRawDataSize,&ret,NULL);
   1.268 +		if (ret!=KPtrToRawDataSize)
   1.269 +			goto close;
   1.270 +
   1.271 +	//SYMBIAN Header
   1.272 +		if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   1.273 +			goto close;
   1.274 +
   1.275 +		TEmulatorImageHeader header;
   1.276 +
   1.277 +		ReadFile(hFile,&header,sizeof(header),&ret,NULL);
   1.278 +		if (ret!=sizeof(header))
   1.279 +			goto close;
   1.280 +		
   1.281 +// DON'T modify, for now		header.iS.iCaps[0] = aNewCap;		//assign new caps to binary
   1.282 +
   1.283 +
   1.284 +		if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
   1.285 +			goto close;
   1.286 +
   1.287 +		BOOL b = WriteFile(hFile,&header,sizeof(header),&ret,NULL);
   1.288 +		if (b==FALSE)
   1.289 +			goto close;
   1.290 +		CloseHandle(hFile);
   1.291 +		
   1.292 +		RLibrary lib;
   1.293 +		TCapabilitySet theCaps;
   1.294 +		RLibrary::TInfoBuf info;
   1.295 +		lib.GetInfo(dlldestination, info);
   1.296 +		theCaps=info().iSecurityInfo.iCaps;
   1.297 +
   1.298 +		lib.Close();
   1.299 +
   1.300 +		SCapabilitySet& realCaps = (SCapabilitySet&) theCaps;
   1.301 +// broke!		test(realCaps[0] == aNewCap && realCaps[1] == 0);
   1.302 +		return;
   1.303 +		}
   1.304 +
   1.305 +//Close file
   1.306 +close:
   1.307 +	CloseHandle(hFile);
   1.308 +	test(0);
   1.309 +	} 
   1.310 +*/
   1.311 +#endif
   1.312 +
   1.313 +/*
   1.314 +void TestGetCapability()
   1.315 +//
   1.316 +//	test RLibrary::GetCapabilites
   1.317 +//
   1.318 +	{
   1.319 +	RLibrary library;
   1.320 +	TInt r;
   1.321 +	test.Next(_L("Test RLibrary::GetCapabilities()"));
   1.322 +
   1.323 +// These tests check the error code returned by the (deprecated) GetCapability method
   1.324 +// and now serve no useful purpose
   1.325 +//	TCapability theCaps;
   1.326 +//	r=library.GetCapability(theCaps,_L("Z:\\Mongolia"));
   1.327 +//	test(r==KErrNotFound);
   1.328 +//	r=library.GetCapability(theCaps,_L("C:\\Malawi.Fip"));
   1.329 +//	test(r==KErrNotFound);
   1.330 +
   1.331 +	TCapabilitySet theCaps;
   1.332 +	RLibrary::TInfoBuf info;
   1.333 +#if defined __WINS__
   1.334 +
   1.335 +	
   1.336 +
   1.337 +
   1.338 +	test.Next(_L("Get caps of an existing library"));
   1.339 +//	r=lib1.GetCapability(theCaps,_L("ECONS"));
   1.340 +//	test(r==KErrNone);
   1.341 +	RLibrary lib1;
   1.342 +//	r=lib1.GetInfo(_L("ECONS"), info);
   1.343 +	r=lib1.GetInfo(_L("Z:\\SYS\\BIN\\ECONS"), info);
   1.344 +	test(r==KErrNone);
   1.345 +	theCaps=info().iSecurityInfo.iCaps;
   1.346 +
   1.347 +	test.Next(_L("And again"));
   1.348 +//	r=lib2.GetCapability(theCaps,_L("ECONS.DLL"));
   1.349 +//	test(r==KErrNone);
   1.350 +	RLibrary lib2;
   1.351 +	lib2.GetInfo(_L("ECONS.DLL"), info);
   1.352 +	theCaps=info().iSecurityInfo.iCaps;
   1.353 +	
   1.354 +#else
   1.355 +// This test checks for the correct return value from the (deprecated)
   1.356 +// method GetCapability, and now serves no useful purpose??
   1.357 +//	r=library.GetCapability(theCaps,_L("Malawi.Fip"));
   1.358 +//	test(r==KErrNotSupported);
   1.359 +	library.GetInfo(_L("Malawi.Fip"), info);
   1.360 +	theCaps=info().iSecurityInfo.iCaps;
   1.361 +
   1.362 +//TODO uncomment when in ssytem bin
   1.363 +//	RLibrary lib1;
   1.364 +//	test.Next(_L("Get caps of an existing library"));
   1.365 +//	r=lib1.GetCapability(theCaps,_L("ECONS"));
   1.366 +//	r=lib1.GetCapability(theCaps,_L("Z:\\Sys\\Bin\\ECONS"));
   1.367 +//	RDebug::Print(_L("r====%d"),r);
   1.368 +//	test(r==KErrNone);
   1.369 +	
   1.370 +//	RLibrary lib2;
   1.371 +//	test.Next(_L("And again"));
   1.372 +//	r=lib2.GetCapability(theCaps,_L("Z:\\Sys\\Bin\\ECONS.DLL"));
   1.373 +//	r=lib2.GetCapability(theCaps,_L("ECONS.DLL"));
   1.374 +//	test(r==KErrNone);
   1.375 +	
   1.376 +	RLibrary lib3;
   1.377 +	test.Next(_L("And again, but searching"));
   1.378 +	r=lib3.GetCapability(theCaps,_L("ECONS.DLL"));
   1.379 +	test(r==KErrNotSupported);
   1.380 +	lib3.Close();
   1.381 +	
   1.382 +#endif
   1.383 +
   1.384 +	test.Next(_L("Search through DLLs from t_ldrcheck"));
   1.385 +
   1.386 +	// Not sure of the benefit of this test ??
   1.387 +
   1.388 +	TBuf<40> dllfilenames;
   1.389 +	RLibrary tlib;
   1.390 +//	TCapability deecaps;
   1.391 +	TCapabilitySet deecaps;
   1.392 +	for(TInt i=0;i<13;i++)
   1.393 +		{
   1.394 +		dllfilenames=KDllfilename;
   1.395 +		dllfilenames.AppendNum(i);
   1.396 +		dllfilenames +=KDllExt;
   1.397 +//		r=tlib.GetCapability(deecaps,dllfilenames);		
   1.398 +		tlib.GetInfo(dllfilenames, info);
   1.399 +		theCaps=info().iSecurityInfo.iCaps;
   1.400 +		}
   1.401 +
   1.402 +	test.Next(_L("Close()"));
   1.403 +	tlib.Close();
   1.404 +	library.Close();
   1.405 +//	lib1.Close();
   1.406 +//	lib2.Close();
   1.407 +	}
   1.408 +*/
   1.409 +
   1.410 +TInt LoadExe(TInt aModuleNum,  RProcess& aProcess)
   1.411 +//
   1.412 +//	Load executable
   1.413 +//
   1.414 +	{
   1.415 +	TFileName fn;
   1.416 +//#ifndef WIN32
   1.417 +//	fn =KSystemLibs();
   1.418 +//#endif
   1.419 +	TBuf<16> cmd;
   1.420 +	fn += MODULE_FILENAME(aModuleNum);
   1.421 +	TInt r=aProcess.Create(fn, cmd);
   1.422 +	return r;
   1.423 +	}
   1.424 +
   1.425 +
   1.426 +void TestLoad(TInt aSequence)
   1.427 +//
   1.428 +// Test doing the loads using sequence number to get the results
   1.429 +//
   1.430 +	{
   1.431 +	test.Next(_L("Test all single EXE/DLL combinations\n"));
   1.432 +	TInt r=0;
   1.433 +	test.Next(_L("New EXE"));
   1.434 +	RProcess p;
   1.435 +
   1.436 +	r=LoadExe(14,p);
   1.437 +	test.Printf(_L("LoadExe(14)->%d, Expected %d, seq %d \n"),r,ModuleResultsA[aSequence], aSequence);
   1.438 +	test(r==ModuleResultsA[aSequence]);
   1.439 +	if (r==KErrNone)
   1.440 +		p.Terminate(0);
   1.441 +	p.Close();
   1.442 +
   1.443 +	r=LoadExe(15,p);
   1.444 +	test.Printf(_L("LoadExe(15)->%d, seq %d\n"),r,aSequence);
   1.445 +	test(r==ModuleResultsA[aSequence]);
   1.446 +	if (r==KErrNone)
   1.447 +		p.Terminate(0);
   1.448 +	p.Close();
   1.449 +	}
   1.450 +
   1.451 +/*
   1.452 +void TestLoadLibrary()
   1.453 +//
   1.454 +//	test using load library to load dlls with out rom time dependancies
   1.455 +//
   1.456 +	{
   1.457 +	test.Next(_L("Test loading libraries fromn this exe"));
   1.458 +	
   1.459 +	RLibrary lib;
   1.460 +	TInt r=0;
   1.461 +	//I want to load 6 7 and 11 as these	are of most interest
   1.462 +
   1.463 +	for(TInt i=0;i<KTestCases;i++)
   1.464 +		{
   1.465 +		for(TInt mod=0 ; mod < 16 ; mod++)	//each module
   1.466 +			{
   1.467 +			ModifyModuleCapabilties(ModuleCaps[mod][i],mod);
   1.468 +			}
   1.469 +		r=lib.Load(KDll11);
   1.470 +		test.Printf(_L("mod11, iter=%d, ret=%d, exp=%d"),i,r,ModuleResultsB[i]);
   1.471 +		test(r==ModuleResultsB[i]);
   1.472 +		lib.Close();
   1.473 +		r=lib.Load(KDll6);
   1.474 +		test.Printf(_L("mod6, iter=%d, ret=%d, exp=%d"),i,r,ModuleResultsB[i]);
   1.475 +		test(r==ModuleResultsB[i]);
   1.476 +		lib.Close();
   1.477 +		r=lib.Load(KDll7);
   1.478 +		test.Printf(_L("mod7, iter=%d, ret=%d, exp=%d"),i,r,ModuleResultsC[i]);
   1.479 +		test(r==ModuleResultsC[i]);
   1.480 +		lib.Close();
   1.481 +		}
   1.482 +
   1.483 +	}
   1.484 +*/
   1.485 +/*
   1.486 +void TestLoading()
   1.487 +//
   1.488 +//	test loading various dependancies
   1.489 +//
   1.490 +	{
   1.491 +	for(TInt tstcase=0; tstcase < KTestCases; tstcase++)	//each test case
   1.492 +		{
   1.493 +		for(TInt mod=0 ; mod < 16 ; mod++)	//each module
   1.494 +			{
   1.495 +			ModifyModuleCapabilties(ModuleCaps[mod][tstcase],mod);
   1.496 +			}
   1.497 +			TestLoad(tstcase);	
   1.498 +		}
   1.499 +	}
   1.500 +*/
   1.501 +
   1.502 +
   1.503 +_LIT(KSysHash,"?:\\Sys\\Hash\\");
   1.504 +const TInt KSlash='\\';
   1.505 +
   1.506 +/**
   1.507 +    Creates a hash file in c:\\sys\\hash for a given file
   1.508 +    
   1.509 +    @param aFileName full path to the file, which hash is to be created
   1.510 +*/
   1.511 +void CreateFileHash(const TDesC& aFileName)
   1.512 +	{
   1.513 +
   1.514 +	test.Printf(_L("Create Hash for the file %S\n"), &aFileName);
   1.515 +
   1.516 +	TInt readsize = 1024*2;//to go into header
   1.517 +	HBufC8* block0 = HBufC8::NewL(readsize); 
   1.518 +
   1.519 +	TPtr8 fileblock0(block0->Des());
   1.520 +	CSHA1* hasher=CSHA1::NewL(); 
   1.521 +	
   1.522 +	RFile fTarget;
   1.523 +	TInt r= fTarget.Open(TheFs, aFileName, EFileRead);
   1.524 +	test(r==KErrNone);
   1.525 +	TInt size;
   1.526 +	r=fTarget.Size(size);
   1.527 +	TInt offset=0;
   1.528 +	do	{
   1.529 +		if((size - offset) < readsize)
   1.530 +			readsize = (size - offset);
   1.531 +		r=fTarget.Read(offset, fileblock0, readsize);
   1.532 +		test(r==KErrNone);
   1.533 +		hasher->Update(fileblock0);
   1.534 +		offset+=readsize;
   1.535 +		}
   1.536 +	while(offset < size); 
   1.537 +
   1.538 +	r=fTarget.Read(fileblock0, (size - offset));			
   1.539 +	test(r==KErrNone);
   1.540 +	hasher->Update(fileblock0);
   1.541 +
   1.542 +	TBuf8<20> hash;//only outputs a 20 byte hash
   1.543 +	hash = hasher->Final();
   1.544 +	fTarget.Close();
   1.545 +		
   1.546 +//	const TUint8 * hptr=hash.Ptr();
   1.547 +//	for(TInt i=0;i<20;i++)
   1.548 +//		{
   1.549 +//		RDebug::Print(_L("install hash byte %d = 0x%x\n"),i,hptr[i]);
   1.550 +//		}
   1.551 +	
   1.552 +	delete block0;
   1.553 +	delete hasher;
   1.554 +	TEntry entry;
   1.555 +	r=TheFs.Entry(aFileName,entry);
   1.556 +	test(r==KErrNone);
   1.557 +	RFile fHash;
   1.558 +	TBuf<50> hashfile;
   1.559 +	hashfile = KSysHash;
   1.560 +	hashfile[0] = (TUint8) RFs::GetSystemDriveChar();
   1.561 +	TInt slashpos = aFileName.LocateReverse(KSlash);
   1.562 +	hashfile += aFileName.Mid(slashpos+1);
   1.563 +	
   1.564 +	//-- create directory for dll hashes
   1.565 +	r=TheFs.MkDirAll(hashfile);
   1.566 +	test(r==KErrNone || r==KErrAlreadyExists);
   1.567 +	
   1.568 +    //-- copy / replace given dll hash 
   1.569 +	r=fHash.Replace(TheFs, hashfile, EFileWrite);
   1.570 +	test(r==KErrNone);
   1.571 +	r=fHash.Write(hash);
   1.572 +	test(r==KErrNone);
   1.573 +	r=fHash.Size(size);
   1.574 +	test(r==KErrNone);
   1.575 +	test.Printf(_L("hash file size=%d\n"),size);
   1.576 +	fHash.Close();
   1.577 +	
   1.578 +    }
   1.579 +
   1.580 +
   1.581 +/**
   1.582 +    Corrups a file.
   1.583 +    
   1.584 +    @param  aFileName   a full path to the file to corrupt
   1.585 +    @return KErrNone on success
   1.586 +*/
   1.587 +static TInt CorruptFile(const TDesC& aFileName)
   1.588 +{
   1.589 +	test.Printf(_L("Corrupting file %S\n"), &aFileName);
   1.590 +
   1.591 +    RFile corrFile;
   1.592 +    CleanupClosePushL(corrFile);	
   1.593 +
   1.594 +    TInt r=corrFile.Open(TheFs, aFileName, EFileWrite);
   1.595 +	if(r != KErrNone)
   1.596 +        return r;
   1.597 +	
   1.598 +    TInt size;
   1.599 +	r=corrFile.Size(size);
   1.600 +	if(r != KErrNone)
   1.601 +        return r;
   1.602 +	
   1.603 +    TBuf8<2> dat;
   1.604 +	r=corrFile.Read(size - 5, dat);
   1.605 +	if(r != KErrNone)
   1.606 +        return r;
   1.607 +
   1.608 +	dat[0] = (TUint8) ~dat[0];
   1.609 +	
   1.610 +    r=corrFile.Write(size - 5, dat);
   1.611 +	if(r != KErrNone)
   1.612 +        return r;
   1.613 +
   1.614 +    CleanupStack::PopAndDestroy(1); //-- corrFile
   1.615 +
   1.616 +    return KErrNone;
   1.617 +}
   1.618 +
   1.619 +
   1.620 +#ifndef WIN32
   1.621 +void TestRemovableMediaWithHash()
   1.622 +//
   1.623 +//	test loading from removable media and substed drives Both should fail
   1.624 +//
   1.625 +	{
   1.626 +	test.Next(_L("Testing Removable Media with hashing\n"));
   1.627 +	TInt r=0;
   1.628 +	CFileMan* fileMan=NULL;
   1.629 +	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   1.630 +	test(r==KErrNone);
   1.631 +
   1.632 +	TBuf<30> dllfilenames;
   1.633 +	TBuf<30> dlldestination;
   1.634 +	RLibrary lib;
   1.635 +	
   1.636 +    for(TInt i=0;i<14;i++)
   1.637 +		{
   1.638 +		dllfilenames=KNewDllName;
   1.639 +		dllfilenames.AppendNum(i);
   1.640 +		dllfilenames +=KDllExt;
   1.641 +		
   1.642 +		dlldestination=KNewDllName;
   1.643 +		dlldestination[0]=gDriveToTest;
   1.644 +		dlldestination.AppendNum(i);
   1.645 +		dlldestination+=KDllExt;
   1.646 +		
   1.647 +		r=TheFs.MkDirAll(dlldestination);
   1.648 +		test.Printf(_L("MkDirAll(%S) returned %d\n"), &dlldestination, r);
   1.649 +		test(r==KErrNone || r==KErrAlreadyExists);
   1.650 +		r=fileMan->Copy(dllfilenames,dlldestination, 0); 
   1.651 +        test(r==KErrNone || r==KErrAlreadyExists);	
   1.652 +
   1.653 +
   1.654 +		//take hash of binary
   1.655 +		CreateFileHash(dlldestination);
   1.656 +
   1.657 +		//load binary as is
   1.658 +		r=lib.Load(dlldestination);
   1.659 +		RDebug::Print(_L("t_ldrcheck: loading  %S returned %d\n"),&dlldestination, r);
   1.660 +
   1.661 +		User::After(100000);
   1.662 +		test(r==KErrNone);
   1.663 +		lib.Close();
   1.664 +		test.Printf(_L("Close lib on untouched load\n"));
   1.665 +
   1.666 +        //-- corrupt dll
   1.667 +        r = CorruptFile(dlldestination);
   1.668 +        test(r==KErrNone);
   1.669 +		test.Printf(_L("Binary %S now corrupted\n"), &dlldestination);
   1.670 +
   1.671 +		//load should fail
   1.672 +		test.Printf(_L("loading %S\n"),&dlldestination);
   1.673 +		r=lib.Load(dlldestination);
   1.674 +		test.Printf(_L("load of %S returned %d\n"),&dlldestination,r);
   1.675 +		test(r==KErrPermissionDenied);//as this process has Root caps and the dlls should be loaded with non
   1.676 +		lib.Close();
   1.677 +		test.Printf(_L("Lib close 1\n"));
   1.678 +
   1.679 +		r=TheFs.Delete(dlldestination);
   1.680 +		test.Printf(_L("Delete ret=%d\n"),r);
   1.681 +		test(r==KErrNone);
   1.682 +		}//for(TInt i=0;i<14;i++)
   1.683 +
   1.684 +
   1.685 +	TBuf<30> exefilenames;
   1.686 +	TBuf<30> exedestination;
   1.687 +	RProcess p;
   1.688 +	TBuf<16> cmd;
   1.689 +
   1.690 +	for(TInt j=14;j<16;j++)
   1.691 +		{
   1.692 +		exefilenames=KNewExeName;;
   1.693 +		exefilenames.AppendNum(j);
   1.694 +		exefilenames +=KExeExt;
   1.695 +		
   1.696 +		exedestination=KNewExeName;
   1.697 +		exedestination[0]=gDriveToTest;
   1.698 +		exedestination.AppendNum(j);
   1.699 +		exedestination+=KExeExt;
   1.700 +
   1.701 +		r=fileMan->Copy(exefilenames,exedestination,0);
   1.702 +		test(r==KErrNone || r==KErrAlreadyExists);
   1.703 +
   1.704 +		CreateFileHash(exedestination);
   1.705 +
   1.706 +		r=p.Create(exedestination, cmd);
   1.707 +		test(r==KErrNone);
   1.708 +		p.Terminate(0);
   1.709 +		p.Close();
   1.710 +
   1.711 +        r = CorruptFile(exedestination);
   1.712 +        test(r==KErrNone);
   1.713 +		
   1.714 +		test.Printf(_L("Binary %S now corrupted\n"), &exedestination);
   1.715 +
   1.716 +		r=p.Create(exedestination, cmd);
   1.717 +		test(r==KErrPermissionDenied);
   1.718 +//		p.Terminate(0);	DON'T DO THIS SINCE CREATION FAILED - HANDLE NOT OPENED
   1.719 +		p.Close();
   1.720 +		
   1.721 +		r=TheFs.Delete(exedestination);
   1.722 +		test(r==KErrNone);
   1.723 +
   1.724 +		}
   1.725 +	delete fileMan;
   1.726 +	}
   1.727 +
   1.728 +/*
   1.729 +void TestRemovableMedia()
   1.730 +//
   1.731 +//	test loading from removable media and substed drives Both should fail
   1.732 +//
   1.733 +	{
   1.734 +	test.Next(_L("Testing Removeable Media"));
   1.735 +	TInt r=0;
   1.736 +	CFileMan* fileMan=NULL;
   1.737 +	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   1.738 +	test(r==KErrNone);
   1.739 +
   1.740 +	TBuf<30> dllfilenames;
   1.741 +	TBuf<30> dlldestination;
   1.742 +	RLibrary lib;
   1.743 +	for(TInt i=0;i<14;i++)
   1.744 +		{
   1.745 +		dllfilenames=KNewDllName;
   1.746 +		dllfilenames.AppendNum(i);
   1.747 +		dllfilenames +=KDllExt;
   1.748 +		
   1.749 +		dlldestination=KNewDllName;
   1.750 +		dlldestination[0]='D';
   1.751 +		dlldestination.AppendNum(i);
   1.752 +		dlldestination+=KDllExt;
   1.753 +		
   1.754 +		r=TheFs.MkDirAll(dlldestination);
   1.755 +		test(r==KErrNone || r==KErrAlreadyExists);
   1.756 +
   1.757 +		r=fileMan->Copy(dllfilenames,dlldestination, 0);
   1.758 +		test(r==KErrNone || r==KErrAlreadyExists);	
   1.759 +		
   1.760 +		r=lib.Load(dlldestination);
   1.761 +		test.Printf(_L("load %S ret=%d"),&dlldestination,r);
   1.762 +		RDebug::Print(_L("r=%d"),r);
   1.763 +		test(r==KErrPermissionDenied);//as this process has Root caps and the dlls should be loaded with non
   1.764 +		lib.Close();
   1.765 +		}
   1.766 +
   1.767 +	TBuf<30> exefilenames;
   1.768 +	TBuf<30> exedestination;
   1.769 +	RProcess p;
   1.770 +	TBuf<16> cmd;
   1.771 +
   1.772 +	for(TInt j=14;j<16;j++)
   1.773 +		{
   1.774 +		exefilenames=KNewExeName;;
   1.775 +		exefilenames.AppendNum(j);
   1.776 +		exefilenames +=KExeExt;
   1.777 +		
   1.778 +		exedestination=KNewExeName;
   1.779 +		exedestination[0]='D';
   1.780 +		exedestination.AppendNum(j);
   1.781 +		exedestination+=KExeExt;
   1.782 +
   1.783 +		r=fileMan->Copy(exefilenames,exedestination,0);
   1.784 +		test(r==KErrNone || r==KErrAlreadyExists);
   1.785 +
   1.786 +		r=p.Create(exedestination, cmd);
   1.787 +		test(r==KErrPermissionDenied);
   1.788 +		p.Close();
   1.789 +		}
   1.790 +	delete fileMan;
   1.791 +	}
   1.792 +*/
   1.793 +
   1.794 +void TestNonSystemSubsted()
   1.795 +//
   1.796 +//	Test an internal drive on non system dir
   1.797 +//
   1.798 +	{
   1.799 +	test.Next(_L("Testing Non System and Subst"));
   1.800 +	TInt r=0;
   1.801 +	CFileMan* fileMan=NULL;
   1.802 +	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   1.803 +	test(r==KErrNone);
   1.804 +
   1.805 +	TBuf<50> dllfilenames;
   1.806 +	TBuf<50> dlldestination;
   1.807 +	TBuf<50> substed;
   1.808 +	RLibrary lib;
   1.809 +	for(TInt i=0;i<14;i++)
   1.810 +		{
   1.811 +		dllfilenames=KNewDllName;
   1.812 +		dllfilenames.AppendNum(i);
   1.813 +		dllfilenames +=KDllExt;
   1.814 +
   1.815 +		dlldestination=KAnyDirDll;
   1.816 +      dlldestination+=_L("_X");
   1.817 +		dlldestination.AppendNum(i);
   1.818 +		dlldestination+=KDllExt;
   1.819 +
   1.820 +		if(i==0)
   1.821 +			{
   1.822 +			r=TheFs.MkDirAll(dlldestination);
   1.823 +			test(r==KErrNone || r==KErrAlreadyExists);
   1.824 +			r=TheFs.SetSubst(KAnyDirShort(), EDriveN);	//set up n as the substed drive
   1.825 +			test(r==KErrNone);
   1.826 +			}
   1.827 +		
   1.828 +		RDebug::Print(_L("copying from %S to %S"),&dllfilenames,&dlldestination);
   1.829 +		r=fileMan->Copy(dllfilenames,dlldestination, 0);
   1.830 +		test(r==KErrNone || r==KErrAlreadyExists);	
   1.831 +
   1.832 +      //
   1.833 +      // Test that loading from fully qualified path fails
   1.834 +      // (ie - C:\Anyoldname\sys\bin\DLLTS_X0.DLL)
   1.835 +      //
   1.836 +      r=lib.Load(dlldestination);
   1.837 +      RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &dlldestination, r);
   1.838 +      test(r==KErrNotFound);
   1.839 +
   1.840 +      //
   1.841 +      // Test that loading from substituted drive fails
   1.842 +      // (ie - N:\sys\bin\DLLTS_X0.DLL, where N:\ == C:\Anyoldname\)
   1.843 +      //
   1.844 +		substed=KJDllName;
   1.845 +      substed+=_L("_X");
   1.846 +		substed.AppendNum(i);
   1.847 +		substed+=KDllExt;
   1.848 +		
   1.849 +		r=lib.Load(substed);
   1.850 +      RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &substed, r);
   1.851 +      test(r==KErrNotFound);
   1.852 +
   1.853 +      //
   1.854 +      // Test that loader search does not find substituted drives when
   1.855 +      // loading library with no drive or path specified.
   1.856 +      //
   1.857 +      substed=KJDllNameOnly;
   1.858 +      substed+=_L("_X");
   1.859 +      substed.AppendNum(i);
   1.860 +      substed+=KDllExt;
   1.861 +
   1.862 +      r=lib.Load(substed);
   1.863 +      RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &substed, r);
   1.864 +      test(r==KErrNotFound);
   1.865 +		}
   1.866 +
   1.867 +	TBuf<50> exefilenames;
   1.868 +	TBuf<50> exedestination;
   1.869 +	RProcess p;
   1.870 +	TBuf<16> cmd;
   1.871 +
   1.872 +	for(TInt j=14;j<16;j++)
   1.873 +		{
   1.874 +		exefilenames=KNewExeName;;
   1.875 +		exefilenames.AppendNum(j);
   1.876 +		exefilenames +=KExeExt;
   1.877 +
   1.878 +		exedestination=KAnyDirExe;
   1.879 +      exedestination+=_L("_X");
   1.880 +		exedestination.AppendNum(j);
   1.881 +		exedestination+=KExeExt;
   1.882 +
   1.883 +		RDebug::Print(_L("copying from %S to %S"),&exefilenames,&exedestination);
   1.884 +		r=fileMan->Copy(exefilenames,exedestination,0);
   1.885 +		test(r==KErrNone || r==KErrAlreadyExists);
   1.886 +
   1.887 +      //
   1.888 +      // Test that loading from fully qualified path fails
   1.889 +      // (ie - C:\Anyoldname\sys\bin\EXETS_X14.EXE)
   1.890 +      //
   1.891 +      r=p.Create(exedestination, cmd);
   1.892 +      RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &exedestination, r);
   1.893 +      test(r==KErrNotFound);
   1.894 +
   1.895 +      //
   1.896 +      // Test that loading from substituted drive fails
   1.897 +      // (ie - N:\sys\bin\EXETS_X14.EXE, where N:\ == C:\Anyoldname\)
   1.898 +      //
   1.899 +		substed=KJExeName;
   1.900 +      substed+=_L("_X");
   1.901 +		substed.AppendNum(j);
   1.902 +		substed+=KExeExt;
   1.903 +
   1.904 +		r=p.Create(substed, cmd);
   1.905 +      RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &substed, r);
   1.906 +      test(r==KErrNotFound);
   1.907 +
   1.908 +      //
   1.909 +      // Test that loader search does not find substituted drives when
   1.910 +      // loading process with no drive or path specified.
   1.911 +      //
   1.912 +      substed=KJExeNameOnly;
   1.913 +      substed+=_L("_X");
   1.914 +      substed.AppendNum(j);
   1.915 +      substed+=KExeExt;
   1.916 +
   1.917 +      r=p.Create(substed, cmd);
   1.918 +      RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &substed, r);
   1.919 +      test(r==KErrNotFound);
   1.920 +		}
   1.921 +	delete fileMan;
   1.922 +
   1.923 +	// Clear the substituted drive
   1.924 +	r=TheFs.SetSubst(KNullDesC, EDriveN);	
   1.925 +	test(r==KErrNone);
   1.926 +	}
   1.927 +
   1.928 +/*
   1.929 +void TestSystemBinSubsted()
   1.930 +//
   1.931 +//	Test an internal drive on system dir
   1.932 +//
   1.933 +	{
   1.934 +	test.Next(_L("Testing System bin -> Subst"));
   1.935 +	TInt r=0;
   1.936 +	CFileMan* fileMan=NULL;
   1.937 +	TRAP(r,fileMan=CFileMan::NewL(TheFs));
   1.938 +	test(r==KErrNone);
   1.939 +
   1.940 +	TBuf<50> dllfilenames;
   1.941 +	TBuf<50> substed;
   1.942 +	RLibrary lib;
   1.943 +	r=TheFs.SetSubst(KSystemLibs, 14);	//set up O as the substed drive
   1.944 +	test(r==KErrNone);
   1.945 +	for(TInt i=0;i<14;i++)
   1.946 +		{
   1.947 +		RDebug::Print(_L("doing modify caps = %d"),i);
   1.948 +		ModifyModuleCapabilties(KCapabilityRoot,i);
   1.949 +		dllfilenames=KNewDllName;
   1.950 +		dllfilenames.AppendNum(i);
   1.951 +		dllfilenames +=KDllExt;
   1.952 +		
   1.953 +		RDebug::Print(_L("Module = %S"),&dllfilenames);
   1.954 +		r=lib.Load(dllfilenames);
   1.955 +		test(r==KErrNone);//as this process has Root caps and the dlls should be loaded with non
   1.956 +		substed=_L("O:\\DLLTS");
   1.957 +		substed.AppendNum(i);
   1.958 +		substed+=KDllExt;
   1.959 +	
   1.960 +		r=lib.Load(substed);
   1.961 +		test(r==KErrNone);
   1.962 +		lib.Close();
   1.963 +		}
   1.964 +	TBuf<50> exefilenames;
   1.965 +	RProcess p;
   1.966 +	TBuf<16> cmd;
   1.967 +
   1.968 +	for(TInt j=14;j<16;j++)
   1.969 +		{
   1.970 +		exefilenames=KNewExeName;
   1.971 +		exefilenames.AppendNum(j);
   1.972 +		exefilenames +=KExeExt;
   1.973 +		ModifyModuleCapabilties(KCapabilityRoot,j);
   1.974 +		r=p.Create(exefilenames, cmd);
   1.975 +		test(r==KErrNone);
   1.976 +		p.Terminate(0);
   1.977 +		p.Close();
   1.978 +
   1.979 +
   1.980 +		substed=_L("O:\\EXETS");
   1.981 +		substed.AppendNum(j);
   1.982 +		substed+=KExeExt;
   1.983 +		RDebug::Print(_L("substed=%S"),&substed);
   1.984 +		r=p.Create(substed, cmd);
   1.985 +		test(r==KErrNone);
   1.986 +		p.Terminate(0);
   1.987 +		p.Close();
   1.988 +		}
   1.989 +	delete fileMan;
   1.990 +	}
   1.991 +*/
   1.992 +#endif
   1.993 +
   1.994 +
   1.995 +
   1.996 +void HashBM()
   1.997 +//
   1.998 +//	time how long to load a small dll 100 times and one large one
   1.999 +//
  1.1000 +	{
  1.1001 +	test.Next(_L("Next Do Bench Mark\n"));
  1.1002 +	TBuf<30> dlldestination;
  1.1003 +	dlldestination=KNewDllName;
  1.1004 +	dlldestination[0]='D';
  1.1005 +	dlldestination.AppendNum(1);
  1.1006 +	dlldestination+=KDllExt;
  1.1007 +	RLibrary lib;
  1.1008 +	TInt r=0;
  1.1009 +	TUint32 startcount = User::NTickCount();
  1.1010 +	for (TInt i=0;i<100;i++)
  1.1011 +		{
  1.1012 +		r=lib.Load(dlldestination);
  1.1013 +		lib.Close();
  1.1014 +		}
  1.1015 +	TUint32 endcount = User::NTickCount();
  1.1016 +	test(r==KErrNone);
  1.1017 +	RDebug::Print(_L("100 * 4k dll \n"));
  1.1018 +	RDebug::Print(_L("start count=%d, end count=%d, dif=%d\n"),startcount,endcount,endcount-startcount);
  1.1019 +
  1.1020 +	startcount = User::NTickCount();
  1.1021 +	r=lib.Load(_L("D:\\sys\\bin\\euser.dll"));
  1.1022 +	lib.Close();
  1.1023 +	endcount = User::NTickCount();
  1.1024 +	RDebug::Print(_L("r=%d")); 
  1.1025 +//	test(r==KErrNone);
  1.1026 +	RDebug::Print(_L("1 * 233k dll \n"));
  1.1027 +	RDebug::Print(_L("start count=%d, end count=%d, dif=%d\n"),startcount,endcount,endcount-startcount);
  1.1028 +	}
  1.1029 +
  1.1030 +//-------------------------------------------------------
  1.1031 +
  1.1032 +/**
  1.1033 +    testing RLoader::CheckLibraryHash() API
  1.1034 +*/
  1.1035 +void  TestCheckLibraryHash()
  1.1036 +{
  1.1037 +    test.Next(_L("Testing CheckLibraryHash API\n"));
  1.1038 +
  1.1039 +	TInt r=0;
  1.1040 +	CFileMan* pFileMan=NULL;
  1.1041 +	TRAP(r,pFileMan=CFileMan::NewL(TheFs));
  1.1042 +	test(r==KErrNone);
  1.1043 +
  1.1044 +    RLoader loader;
  1.1045 +	r=loader.Connect();
  1.1046 +	test(r==KErrNone);
  1.1047 +    
  1.1048 +    //-- 1. copy test DLL to the specified drive and create hash file for it.
  1.1049 +	TBuf<40> dllFileName;
  1.1050 +	TBuf<40> dllDestination;
  1.1051 +	
  1.1052 +	const TInt KDllNumber = 0;
  1.1053 +
  1.1054 +    dllFileName=KDllfilename;
  1.1055 +	dllFileName.AppendNum(KDllNumber);
  1.1056 +	dllFileName+=KDllExt;
  1.1057 +	
  1.1058 +	dllDestination=KNewDllName;
  1.1059 +	dllDestination[0]=(TUint16)gDriveToTest;
  1.1060 +	dllDestination.AppendNum(KDllNumber);
  1.1061 +	dllDestination+=KDllExt;
  1.1062 +
  1.1063 +    test.Printf(_L("Copy %S to %S\n"), &dllFileName, &dllDestination);
  1.1064 +
  1.1065 +	r=TheFs.MkDirAll(dllDestination);
  1.1066 +	test(r==KErrNone || r==KErrAlreadyExists);
  1.1067 +
  1.1068 +	r=pFileMan->Copy(dllFileName, dllDestination);
  1.1069 +	test(r==KErrNone || r==KErrAlreadyExists);
  1.1070 +
  1.1071 +	r=pFileMan->Attribs(dllDestination, 0, KEntryAttReadOnly, TTime(0) ,0);
  1.1072 +	test(r==KErrNone);
  1.1073 +
  1.1074 +    test.Printf(_L("Creating Dll hash.\n"));
  1.1075 +    CreateFileHash(dllDestination);
  1.1076 +
  1.1077 +    //-- 2. check if the hash exists
  1.1078 +    r=loader.CheckLibraryHash(dllDestination);
  1.1079 +    test(r==KErrNone);
  1.1080 +    test.Printf(_L("Dll hash exists.\n"));
  1.1081 +
  1.1082 +    //-- 2.1 check if the hash exists and valid
  1.1083 +    r=loader.CheckLibraryHash(dllDestination, ETrue);
  1.1084 +    test(r==KErrNone);
  1.1085 +    
  1.1086 +    test.Printf(_L("Dll hash exists and valid.\n"));
  1.1087 +
  1.1088 +    //-- 3. corrupt dll
  1.1089 +    r = CorruptFile(dllDestination);
  1.1090 +    test(r==KErrNone);
  1.1091 +
  1.1092 +    //-- 3.1 check that the hash exists, but is incorrect.
  1.1093 +    r=loader.CheckLibraryHash(dllDestination);
  1.1094 +    test(r==KErrNone);
  1.1095 +    test.Printf(_L("Dll hash exists.\n"));
  1.1096 +
  1.1097 +    r=loader.CheckLibraryHash(dllDestination, ETrue);
  1.1098 +    test(r==KErrCorrupt);
  1.1099 +    
  1.1100 +    test.Printf(_L("Dll hash exists and INVALID.\n"));
  1.1101 +
  1.1102 +    //-- 4. try to locte hash fo the unexisting dll.
  1.1103 +    r=loader.CheckLibraryHash(_L("Z:\\sys\\bin\\NotExist.dll"));
  1.1104 +    test(r==KErrNotFound);
  1.1105 +
  1.1106 +    loader.Close();
  1.1107 +
  1.1108 +    delete pFileMan;
  1.1109 +}
  1.1110 +
  1.1111 +//-------------------------------------------------------
  1.1112 +
  1.1113 +void ParseCommandArguments()
  1.1114 +//
  1.1115 +//
  1.1116 +//
  1.1117 +	{
  1.1118 +	TBuf<0x100> cmd;
  1.1119 +	User::CommandLine(cmd);
  1.1120 +	TLex lex(cmd);
  1.1121 +	TPtrC token=lex.NextToken();
  1.1122 +	TFileName thisfile=RProcess().FileName();
  1.1123 +	if (token.MatchF(thisfile)==0)
  1.1124 +		{
  1.1125 +		token.Set(lex.NextToken());
  1.1126 +		}
  1.1127 +	test.Printf(_L("CLP=%S"),&token);
  1.1128 +
  1.1129 +	if(token.Length()!=0)		
  1.1130 +		{
  1.1131 +		gDriveToTest=token[0];
  1.1132 +		gDriveToTest.UpperCase();
  1.1133 +		}
  1.1134 +	else						
  1.1135 +		gDriveToTest='D';	//default to D:
  1.1136 +	}
  1.1137 +
  1.1138 +
  1.1139 +//-------------------------------------------------------
  1.1140 +
  1.1141 +void TestExes()
  1.1142 +{
  1.1143 +    test.Next(_L("Testing Exes presence.\n"));
  1.1144 +	
  1.1145 +    TInt r;
  1.1146 +
  1.1147 +    RProcess ap;
  1.1148 +	r=LoadExe(14,ap);
  1.1149 +	test(r==KErrNone);
  1.1150 +	ap.Terminate(0);
  1.1151 +	ap.Close();
  1.1152 +	
  1.1153 +    r=LoadExe(15,ap);
  1.1154 +	test(r==KErrNone);
  1.1155 +	ap.Terminate(0);
  1.1156 +	ap.Close();
  1.1157 +}
  1.1158 +
  1.1159 +
  1.1160 +//-------------------------------------------------------
  1.1161 +
  1.1162 +static void CallTestsL(void)
  1.1163 +{
  1.1164 +
  1.1165 +#ifdef __WINS__
  1.1166 +        test.Printf(_L("Not testing on WINS !\n"));
  1.1167 +        return;
  1.1168 +#else
  1.1169 +
  1.1170 +        TestExes();
  1.1171 +    //	TestGetCapability();
  1.1172 +	    CopyModules();				//copies modules from ROM to disk so they may be modified
  1.1173 +    //	TestLoading();
  1.1174 +    //	TestLoadLibrary();
  1.1175 +	
  1.1176 +
  1.1177 +#ifndef WIN32
  1.1178 +	    if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin))
  1.1179 +		    TestRemovableMediaWithHash();
  1.1180 +
  1.1181 +    //	TestSystemBinSubsted();
  1.1182 +    //	TestRemovableMedia();
  1.1183 +	    TestNonSystemSubsted();
  1.1184 +#endif //WIN32
  1.1185 +    //	HashBM();
  1.1186 +
  1.1187 +        TestCheckLibraryHash();
  1.1188 +
  1.1189 +#endif //#ifndef __WINS__
  1.1190 +    
  1.1191 +}
  1.1192 +
  1.1193 +//-------------------------------------------------------
  1.1194 +//
  1.1195 +//	main
  1.1196 +//
  1.1197 +GLDEF_C TInt E32Main()
  1.1198 +	{
  1.1199 +	ParseCommandArguments(); //need this for drive letter to test
  1.1200 +
  1.1201 +	test.Title();
  1.1202 +	test.Start(_L("Setup\n"));
  1.1203 +	CTrapCleanup* cleanup;
  1.1204 +	cleanup=CTrapCleanup::New();
  1.1205 +	__UHEAP_MARK;
  1.1206 +
  1.1207 +	TBuf<20> sessPath;
  1.1208 +	TInt r=0;	
  1.1209 +	r=TheFs.Connect();
  1.1210 +	test(r==KErrNone);
  1.1211 +	r=TheFs.SessionPath(sessPath);
  1.1212 +	test(r==KErrNone);
  1.1213 +
  1.1214 +	TInt drive;
  1.1215 +	RFs::CharToDrive(gDriveToTest, drive);
  1.1216 +	TDriveInfo info;
  1.1217 +	r=TheFs.Drive(info, drive);
  1.1218 +	test(r==KErrNone);
  1.1219 +	
  1.1220 +    if((info.iDriveAtt & KDriveAttRemovable) == 0)
  1.1221 +	{
  1.1222 +        test.Printf(_L("Not testing on non-removable media !\n"));
  1.1223 +    }
  1.1224 +    else
  1.1225 +    {//-- testing on removable media
  1.1226 +        TRAP(r,CallTestsL());
  1.1227 +    }
  1.1228 +
  1.1229 +	TheFs.Close();
  1.1230 +	test.End();
  1.1231 +	test.Close();
  1.1232 +
  1.1233 +	__UHEAP_MARKEND;
  1.1234 +	delete cleanup;
  1.1235 +
  1.1236 +	return KErrNone;
  1.1237 +	}
  1.1238 +
  1.1239 +