sl@0: // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // f32test\loader\t_ldrcheck.cpp sl@0: // sl@0: // sl@0: sl@0: sl@0: #include "dlltree.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "t_hash.h" sl@0: sl@0: #if defined (WIN32) && !defined (__X86GCC__) sl@0: #include sl@0: #include sl@0: #endif sl@0: sl@0: sl@0: RTest test(_L("T_LDRCheck")); sl@0: RFs TheFs; sl@0: GLDEF_D TChar gDriveToTest; sl@0: sl@0: sl@0: /** sl@0: Copy modules from z:\\sys\\bin to c:\\sys\\bin sl@0: */ sl@0: void CopyModules() sl@0: { sl@0: test.Next(_L("Copy Modules from ROM to disk")); sl@0: sl@0: TInt r; sl@0: TTime modtime; sl@0: sl@0: r=TheFs.MkDirAll(KSystemLibs); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: CFileMan* fileMan=NULL; sl@0: TRAP(r,fileMan=CFileMan::NewL(TheFs)); sl@0: test(r==KErrNone); sl@0: sl@0: TBuf<40> dllfilenames; sl@0: TBuf<40> dlldestination; sl@0: sl@0: for(TInt i=0;i<14;i++) sl@0: { sl@0: dllfilenames=KDllfilename; sl@0: dllfilenames.AppendNum(i); sl@0: dllfilenames +=KDllExt; sl@0: dlldestination=KNewDllName; sl@0: dlldestination.AppendNum(i); sl@0: dlldestination+=KDllExt; sl@0: #ifndef WIN32 sl@0: r=fileMan->Copy(dllfilenames,dlldestination, 0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=fileMan->Attribs(dlldestination,0, KEntryAttReadOnly,modtime,0); sl@0: test(r==KErrNone); sl@0: #endif sl@0: } sl@0: sl@0: TBuf<40> exefilenames; sl@0: TBuf<40> exedestination; sl@0: sl@0: for(TInt j=14;j<16;j++) sl@0: { sl@0: exefilenames=KExefilename; sl@0: exefilenames.AppendNum(j); sl@0: exefilenames +=KExeExt; sl@0: exedestination=KNewExeName; sl@0: exedestination.AppendNum(j); sl@0: exedestination+=KExeExt; sl@0: #ifndef WIN32 sl@0: r=fileMan->Copy(exefilenames,exedestination,0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=fileMan->Attribs(exedestination, 0, KEntryAttReadOnly, modtime, 0); sl@0: test(r==KErrNone); sl@0: #endif sl@0: } sl@0: delete fileMan; sl@0: } sl@0: sl@0: sl@0: sl@0: #ifndef WIN32 sl@0: /* sl@0: void ModifyModuleCapabilties(TUint32 aNewCap, TInt aModuleNumber) sl@0: // sl@0: // Assign new capabilties to a module using raw disk write sl@0: // sl@0: { sl@0: // test.Next(_L("Modify Module Capabilities")); sl@0: sl@0: test(aModuleNumber<=15 && aModuleNumber>=0); sl@0: sl@0: TBuf<40> dlldestination; sl@0: RFile modFile; sl@0: TInt r=0; sl@0: sl@0: SCapabilitySet caps; sl@0: memset(&caps,0,sizeof(caps)); sl@0: caps[0] = aNewCap; sl@0: TPtrC8 theNewCaps((TUint8*)(&caps),sizeof(caps)); sl@0: sl@0: if(aModuleNumber < 14) sl@0: { sl@0: dlldestination=KNewDllName; sl@0: dlldestination.AppendNum(aModuleNumber); sl@0: dlldestination+=KDllExt; sl@0: } sl@0: else sl@0: { sl@0: dlldestination=KNewExeName; sl@0: dlldestination.AppendNum(aModuleNumber); sl@0: dlldestination+=KExeExt; sl@0: } sl@0: sl@0: r=modFile.Open(TheFs, dlldestination, EFileWrite); sl@0: test(r==KErrNone); sl@0: r=modFile.Write(_FOFF(E32ImageHeaderV,iS.iCaps), theNewCaps,4); sl@0: test(r==KErrNone); sl@0: modFile.Close(); sl@0: sl@0: RLibrary lib; sl@0: TCapabilitySet theCaps; sl@0: RLibrary::TInfoBuf info; sl@0: sl@0: lib.GetInfo(dlldestination, info); sl@0: theCaps=info().iSecurityInfo.iCaps; sl@0: sl@0: lib.Close(); sl@0: test(theCaps==aNewCap); sl@0: sl@0: } sl@0: */ sl@0: #else sl@0: /* sl@0: void ModifyModuleCapabilties(TUint32 aNewCap, TInt aModuleNumber) sl@0: // sl@0: // Wins version (poking values is not quite so simple sl@0: // sl@0: { sl@0: // test.Next(_L("Modify Module Capabilities")); sl@0: test(aModuleNumber<=15 && aModuleNumber>=0); sl@0: sl@0: TBuf<40> dlldestination; sl@0: TInt r=0; sl@0: sl@0: TPtrC8 theNewCaps((TUint8*)(&aNewCap),sizeof(TUint32)); sl@0: sl@0: sl@0: if(aModuleNumber < 14) sl@0: { sl@0: dlldestination=KNewDllName; sl@0: dlldestination.AppendNum(aModuleNumber); sl@0: dlldestination+=KDllExt; sl@0: } sl@0: else sl@0: { sl@0: dlldestination=KNewExeName; sl@0: dlldestination.AppendNum(aModuleNumber); sl@0: dlldestination+=KExeExt; sl@0: } sl@0: sl@0: RLibrary lib2; sl@0: TCapabilitySet theCaps2; sl@0: RLibrary::TInfoBuf info; sl@0: sl@0: lib2.GetInfo(dlldestination, info); sl@0: theCaps2=info().iSecurityInfo.iCaps; sl@0: sl@0: lib2.Close(); sl@0: sl@0: TBuf fileName; sl@0: r = MapEmulatedFileName(fileName, dlldestination); sl@0: sl@0: HANDLE hFile=Emulator::CreateFile((LPCTSTR)fileName.PtrZ(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL); sl@0: if (hFile==INVALID_HANDLE_VALUE) sl@0: return; sl@0: DWORD ret; sl@0: TBuf8 checkedUidBuf; sl@0: checkedUidBuf.SetLength(sizeof(TCheckedUid)); sl@0: ReadFile(hFile,&checkedUidBuf[0],sizeof(TCheckedUid),&ret,NULL); sl@0: if (ret!=sizeof(TCheckedUid)) sl@0: goto close; sl@0: sl@0: //Look at PE file for UID section sl@0: { sl@0: const TInt KPeHeaderAddrAddr=0x3c; sl@0: const TInt KPeHeaderAddrSize=0x01; sl@0: const TInt KNumberOfSectionsOffset=0x06; sl@0: const TInt KNumberOfSectionsSize=0x02; sl@0: const TInt KSectionTableOffset=0xf8; sl@0: const TInt KSectionHeaderSize=0x28; sl@0: const TInt KSectionNameLength=0x08; sl@0: const TInt KPtrToRawDataOffset=0x14; sl@0: const TInt KPtrToRawDataSize=0x04; sl@0: const TText8 peText[4]={'P','E',0,0}; sl@0: const TText8 uidText[8]={'.','S','Y','M','B','I','A','N'}; sl@0: sl@0: //Read address of start of PE header sl@0: if (SetFilePointer(hFile,KPeHeaderAddrAddr,0,FILE_BEGIN)==0xFFFFFFFF) sl@0: goto close; sl@0: TInt peAddr=0; sl@0: ReadFile(hFile,&peAddr,KPeHeaderAddrSize,&ret,NULL); sl@0: if (ret!=KPeHeaderAddrSize) sl@0: goto close; sl@0: sl@0: //Check it really is the start of PE header sl@0: if (SetFilePointer(hFile,peAddr,0,FILE_BEGIN)==0xFFFFFFFF) sl@0: goto close; sl@0: TText8 text[4]; sl@0: ReadFile(hFile,text,4,&ret,NULL); sl@0: if (*(TInt32*)text!=*(TInt32*)peText) sl@0: goto close; sl@0: sl@0: //Read number of sections sl@0: if (SetFilePointer(hFile,peAddr+KNumberOfSectionsOffset,0,FILE_BEGIN)==0xFFFFFFFF) sl@0: goto close; sl@0: TInt sections=0; sl@0: ReadFile(hFile,§ions,KNumberOfSectionsSize,&ret,NULL); sl@0: if (ret!=KNumberOfSectionsSize) sl@0: goto close; sl@0: sl@0: //Go through section headers looking for UID section sl@0: if (SetFilePointer(hFile,peAddr+KSectionTableOffset,0,FILE_BEGIN)==0xFFFFFFFF) sl@0: goto close; sl@0: TInt i=0; sl@0: for(;i dllfilenames; sl@0: RLibrary tlib; sl@0: // TCapability deecaps; sl@0: TCapabilitySet deecaps; sl@0: for(TInt i=0;i<13;i++) sl@0: { sl@0: dllfilenames=KDllfilename; sl@0: dllfilenames.AppendNum(i); sl@0: dllfilenames +=KDllExt; sl@0: // r=tlib.GetCapability(deecaps,dllfilenames); sl@0: tlib.GetInfo(dllfilenames, info); sl@0: theCaps=info().iSecurityInfo.iCaps; sl@0: } sl@0: sl@0: test.Next(_L("Close()")); sl@0: tlib.Close(); sl@0: library.Close(); sl@0: // lib1.Close(); sl@0: // lib2.Close(); sl@0: } sl@0: */ sl@0: sl@0: TInt LoadExe(TInt aModuleNum, RProcess& aProcess) sl@0: // sl@0: // Load executable sl@0: // sl@0: { sl@0: TFileName fn; sl@0: //#ifndef WIN32 sl@0: // fn =KSystemLibs(); sl@0: //#endif sl@0: TBuf<16> cmd; sl@0: fn += MODULE_FILENAME(aModuleNum); sl@0: TInt r=aProcess.Create(fn, cmd); sl@0: return r; sl@0: } sl@0: sl@0: sl@0: void TestLoad(TInt aSequence) sl@0: // sl@0: // Test doing the loads using sequence number to get the results sl@0: // sl@0: { sl@0: test.Next(_L("Test all single EXE/DLL combinations\n")); sl@0: TInt r=0; sl@0: test.Next(_L("New EXE")); sl@0: RProcess p; sl@0: sl@0: r=LoadExe(14,p); sl@0: test.Printf(_L("LoadExe(14)->%d, Expected %d, seq %d \n"),r,ModuleResultsA[aSequence], aSequence); sl@0: test(r==ModuleResultsA[aSequence]); sl@0: if (r==KErrNone) sl@0: p.Terminate(0); sl@0: p.Close(); sl@0: sl@0: r=LoadExe(15,p); sl@0: test.Printf(_L("LoadExe(15)->%d, seq %d\n"),r,aSequence); sl@0: test(r==ModuleResultsA[aSequence]); sl@0: if (r==KErrNone) sl@0: p.Terminate(0); sl@0: p.Close(); sl@0: } sl@0: sl@0: /* sl@0: void TestLoadLibrary() sl@0: // sl@0: // test using load library to load dlls with out rom time dependancies sl@0: // sl@0: { sl@0: test.Next(_L("Test loading libraries fromn this exe")); sl@0: sl@0: RLibrary lib; sl@0: TInt r=0; sl@0: //I want to load 6 7 and 11 as these are of most interest sl@0: sl@0: for(TInt i=0;iDes()); sl@0: CSHA1* hasher=CSHA1::NewL(); sl@0: sl@0: RFile fTarget; sl@0: TInt r= fTarget.Open(TheFs, aFileName, EFileRead); sl@0: test(r==KErrNone); sl@0: TInt size; sl@0: r=fTarget.Size(size); sl@0: TInt offset=0; sl@0: do { sl@0: if((size - offset) < readsize) sl@0: readsize = (size - offset); sl@0: r=fTarget.Read(offset, fileblock0, readsize); sl@0: test(r==KErrNone); sl@0: hasher->Update(fileblock0); sl@0: offset+=readsize; sl@0: } sl@0: while(offset < size); sl@0: sl@0: r=fTarget.Read(fileblock0, (size - offset)); sl@0: test(r==KErrNone); sl@0: hasher->Update(fileblock0); sl@0: sl@0: TBuf8<20> hash;//only outputs a 20 byte hash sl@0: hash = hasher->Final(); sl@0: fTarget.Close(); sl@0: sl@0: // const TUint8 * hptr=hash.Ptr(); sl@0: // for(TInt i=0;i<20;i++) sl@0: // { sl@0: // RDebug::Print(_L("install hash byte %d = 0x%x\n"),i,hptr[i]); sl@0: // } sl@0: sl@0: delete block0; sl@0: delete hasher; sl@0: TEntry entry; sl@0: r=TheFs.Entry(aFileName,entry); sl@0: test(r==KErrNone); sl@0: RFile fHash; sl@0: TBuf<50> hashfile; sl@0: hashfile = KSysHash; sl@0: hashfile[0] = (TUint8) RFs::GetSystemDriveChar(); sl@0: TInt slashpos = aFileName.LocateReverse(KSlash); sl@0: hashfile += aFileName.Mid(slashpos+1); sl@0: sl@0: //-- create directory for dll hashes sl@0: r=TheFs.MkDirAll(hashfile); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: //-- copy / replace given dll hash sl@0: r=fHash.Replace(TheFs, hashfile, EFileWrite); sl@0: test(r==KErrNone); sl@0: r=fHash.Write(hash); sl@0: test(r==KErrNone); sl@0: r=fHash.Size(size); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("hash file size=%d\n"),size); sl@0: fHash.Close(); sl@0: sl@0: } sl@0: sl@0: sl@0: /** sl@0: Corrups a file. sl@0: sl@0: @param aFileName a full path to the file to corrupt sl@0: @return KErrNone on success sl@0: */ sl@0: static TInt CorruptFile(const TDesC& aFileName) sl@0: { sl@0: test.Printf(_L("Corrupting file %S\n"), &aFileName); sl@0: sl@0: RFile corrFile; sl@0: CleanupClosePushL(corrFile); sl@0: sl@0: TInt r=corrFile.Open(TheFs, aFileName, EFileWrite); sl@0: if(r != KErrNone) sl@0: return r; sl@0: sl@0: TInt size; sl@0: r=corrFile.Size(size); sl@0: if(r != KErrNone) sl@0: return r; sl@0: sl@0: TBuf8<2> dat; sl@0: r=corrFile.Read(size - 5, dat); sl@0: if(r != KErrNone) sl@0: return r; sl@0: sl@0: dat[0] = (TUint8) ~dat[0]; sl@0: sl@0: r=corrFile.Write(size - 5, dat); sl@0: if(r != KErrNone) sl@0: return r; sl@0: sl@0: CleanupStack::PopAndDestroy(1); //-- corrFile sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: #ifndef WIN32 sl@0: void TestRemovableMediaWithHash() sl@0: // sl@0: // test loading from removable media and substed drives Both should fail sl@0: // sl@0: { sl@0: test.Next(_L("Testing Removable Media with hashing\n")); sl@0: TInt r=0; sl@0: CFileMan* fileMan=NULL; sl@0: TRAP(r,fileMan=CFileMan::NewL(TheFs)); sl@0: test(r==KErrNone); sl@0: sl@0: TBuf<30> dllfilenames; sl@0: TBuf<30> dlldestination; sl@0: RLibrary lib; sl@0: sl@0: for(TInt i=0;i<14;i++) sl@0: { sl@0: dllfilenames=KNewDllName; sl@0: dllfilenames.AppendNum(i); sl@0: dllfilenames +=KDllExt; sl@0: sl@0: dlldestination=KNewDllName; sl@0: dlldestination[0]=gDriveToTest; sl@0: dlldestination.AppendNum(i); sl@0: dlldestination+=KDllExt; sl@0: sl@0: r=TheFs.MkDirAll(dlldestination); sl@0: test.Printf(_L("MkDirAll(%S) returned %d\n"), &dlldestination, r); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: r=fileMan->Copy(dllfilenames,dlldestination, 0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: sl@0: //take hash of binary sl@0: CreateFileHash(dlldestination); sl@0: sl@0: //load binary as is sl@0: r=lib.Load(dlldestination); sl@0: RDebug::Print(_L("t_ldrcheck: loading %S returned %d\n"),&dlldestination, r); sl@0: sl@0: User::After(100000); sl@0: test(r==KErrNone); sl@0: lib.Close(); sl@0: test.Printf(_L("Close lib on untouched load\n")); sl@0: sl@0: //-- corrupt dll sl@0: r = CorruptFile(dlldestination); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("Binary %S now corrupted\n"), &dlldestination); sl@0: sl@0: //load should fail sl@0: test.Printf(_L("loading %S\n"),&dlldestination); sl@0: r=lib.Load(dlldestination); sl@0: test.Printf(_L("load of %S returned %d\n"),&dlldestination,r); sl@0: test(r==KErrPermissionDenied);//as this process has Root caps and the dlls should be loaded with non sl@0: lib.Close(); sl@0: test.Printf(_L("Lib close 1\n")); sl@0: sl@0: r=TheFs.Delete(dlldestination); sl@0: test.Printf(_L("Delete ret=%d\n"),r); sl@0: test(r==KErrNone); sl@0: }//for(TInt i=0;i<14;i++) sl@0: sl@0: sl@0: TBuf<30> exefilenames; sl@0: TBuf<30> exedestination; sl@0: RProcess p; sl@0: TBuf<16> cmd; sl@0: sl@0: for(TInt j=14;j<16;j++) sl@0: { sl@0: exefilenames=KNewExeName;; sl@0: exefilenames.AppendNum(j); sl@0: exefilenames +=KExeExt; sl@0: sl@0: exedestination=KNewExeName; sl@0: exedestination[0]=gDriveToTest; sl@0: exedestination.AppendNum(j); sl@0: exedestination+=KExeExt; sl@0: sl@0: r=fileMan->Copy(exefilenames,exedestination,0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: CreateFileHash(exedestination); sl@0: sl@0: r=p.Create(exedestination, cmd); sl@0: test(r==KErrNone); sl@0: p.Terminate(0); sl@0: p.Close(); sl@0: sl@0: r = CorruptFile(exedestination); sl@0: test(r==KErrNone); sl@0: sl@0: test.Printf(_L("Binary %S now corrupted\n"), &exedestination); sl@0: sl@0: r=p.Create(exedestination, cmd); sl@0: test(r==KErrPermissionDenied); sl@0: // p.Terminate(0); DON'T DO THIS SINCE CREATION FAILED - HANDLE NOT OPENED sl@0: p.Close(); sl@0: sl@0: r=TheFs.Delete(exedestination); sl@0: test(r==KErrNone); sl@0: sl@0: } sl@0: delete fileMan; sl@0: } sl@0: sl@0: /* sl@0: void TestRemovableMedia() sl@0: // sl@0: // test loading from removable media and substed drives Both should fail sl@0: // sl@0: { sl@0: test.Next(_L("Testing Removeable Media")); sl@0: TInt r=0; sl@0: CFileMan* fileMan=NULL; sl@0: TRAP(r,fileMan=CFileMan::NewL(TheFs)); sl@0: test(r==KErrNone); sl@0: sl@0: TBuf<30> dllfilenames; sl@0: TBuf<30> dlldestination; sl@0: RLibrary lib; sl@0: for(TInt i=0;i<14;i++) sl@0: { sl@0: dllfilenames=KNewDllName; sl@0: dllfilenames.AppendNum(i); sl@0: dllfilenames +=KDllExt; sl@0: sl@0: dlldestination=KNewDllName; sl@0: dlldestination[0]='D'; sl@0: dlldestination.AppendNum(i); sl@0: dlldestination+=KDllExt; sl@0: sl@0: r=TheFs.MkDirAll(dlldestination); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=fileMan->Copy(dllfilenames,dlldestination, 0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=lib.Load(dlldestination); sl@0: test.Printf(_L("load %S ret=%d"),&dlldestination,r); sl@0: RDebug::Print(_L("r=%d"),r); sl@0: test(r==KErrPermissionDenied);//as this process has Root caps and the dlls should be loaded with non sl@0: lib.Close(); sl@0: } sl@0: sl@0: TBuf<30> exefilenames; sl@0: TBuf<30> exedestination; sl@0: RProcess p; sl@0: TBuf<16> cmd; sl@0: sl@0: for(TInt j=14;j<16;j++) sl@0: { sl@0: exefilenames=KNewExeName;; sl@0: exefilenames.AppendNum(j); sl@0: exefilenames +=KExeExt; sl@0: sl@0: exedestination=KNewExeName; sl@0: exedestination[0]='D'; sl@0: exedestination.AppendNum(j); sl@0: exedestination+=KExeExt; sl@0: sl@0: r=fileMan->Copy(exefilenames,exedestination,0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=p.Create(exedestination, cmd); sl@0: test(r==KErrPermissionDenied); sl@0: p.Close(); sl@0: } sl@0: delete fileMan; sl@0: } sl@0: */ sl@0: sl@0: void TestNonSystemSubsted() sl@0: // sl@0: // Test an internal drive on non system dir sl@0: // sl@0: { sl@0: test.Next(_L("Testing Non System and Subst")); sl@0: TInt r=0; sl@0: CFileMan* fileMan=NULL; sl@0: TRAP(r,fileMan=CFileMan::NewL(TheFs)); sl@0: test(r==KErrNone); sl@0: sl@0: TBuf<50> dllfilenames; sl@0: TBuf<50> dlldestination; sl@0: TBuf<50> substed; sl@0: RLibrary lib; sl@0: for(TInt i=0;i<14;i++) sl@0: { sl@0: dllfilenames=KNewDllName; sl@0: dllfilenames.AppendNum(i); sl@0: dllfilenames +=KDllExt; sl@0: sl@0: dlldestination=KAnyDirDll; sl@0: dlldestination+=_L("_X"); sl@0: dlldestination.AppendNum(i); sl@0: dlldestination+=KDllExt; sl@0: sl@0: if(i==0) sl@0: { sl@0: r=TheFs.MkDirAll(dlldestination); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: r=TheFs.SetSubst(KAnyDirShort(), EDriveN); //set up n as the substed drive sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: RDebug::Print(_L("copying from %S to %S"),&dllfilenames,&dlldestination); sl@0: r=fileMan->Copy(dllfilenames,dlldestination, 0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: // sl@0: // Test that loading from fully qualified path fails sl@0: // (ie - C:\Anyoldname\sys\bin\DLLTS_X0.DLL) sl@0: // sl@0: r=lib.Load(dlldestination); sl@0: RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &dlldestination, r); sl@0: test(r==KErrNotFound); sl@0: sl@0: // sl@0: // Test that loading from substituted drive fails sl@0: // (ie - N:\sys\bin\DLLTS_X0.DLL, where N:\ == C:\Anyoldname\) sl@0: // sl@0: substed=KJDllName; sl@0: substed+=_L("_X"); sl@0: substed.AppendNum(i); sl@0: substed+=KDllExt; sl@0: sl@0: r=lib.Load(substed); sl@0: RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &substed, r); sl@0: test(r==KErrNotFound); sl@0: sl@0: // sl@0: // Test that loader search does not find substituted drives when sl@0: // loading library with no drive or path specified. sl@0: // sl@0: substed=KJDllNameOnly; sl@0: substed+=_L("_X"); sl@0: substed.AppendNum(i); sl@0: substed+=KDllExt; sl@0: sl@0: r=lib.Load(substed); sl@0: RDebug::Print(_L("RLibrary::Load(%S) : r=%d"), &substed, r); sl@0: test(r==KErrNotFound); sl@0: } sl@0: sl@0: TBuf<50> exefilenames; sl@0: TBuf<50> exedestination; sl@0: RProcess p; sl@0: TBuf<16> cmd; sl@0: sl@0: for(TInt j=14;j<16;j++) sl@0: { sl@0: exefilenames=KNewExeName;; sl@0: exefilenames.AppendNum(j); sl@0: exefilenames +=KExeExt; sl@0: sl@0: exedestination=KAnyDirExe; sl@0: exedestination+=_L("_X"); sl@0: exedestination.AppendNum(j); sl@0: exedestination+=KExeExt; sl@0: sl@0: RDebug::Print(_L("copying from %S to %S"),&exefilenames,&exedestination); sl@0: r=fileMan->Copy(exefilenames,exedestination,0); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: // sl@0: // Test that loading from fully qualified path fails sl@0: // (ie - C:\Anyoldname\sys\bin\EXETS_X14.EXE) sl@0: // sl@0: r=p.Create(exedestination, cmd); sl@0: RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &exedestination, r); sl@0: test(r==KErrNotFound); sl@0: sl@0: // sl@0: // Test that loading from substituted drive fails sl@0: // (ie - N:\sys\bin\EXETS_X14.EXE, where N:\ == C:\Anyoldname\) sl@0: // sl@0: substed=KJExeName; sl@0: substed+=_L("_X"); sl@0: substed.AppendNum(j); sl@0: substed+=KExeExt; sl@0: sl@0: r=p.Create(substed, cmd); sl@0: RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &substed, r); sl@0: test(r==KErrNotFound); sl@0: sl@0: // sl@0: // Test that loader search does not find substituted drives when sl@0: // loading process with no drive or path specified. sl@0: // sl@0: substed=KJExeNameOnly; sl@0: substed+=_L("_X"); sl@0: substed.AppendNum(j); sl@0: substed+=KExeExt; sl@0: sl@0: r=p.Create(substed, cmd); sl@0: RDebug::Print(_L("RProcess::Create(%S) : r=%d"), &substed, r); sl@0: test(r==KErrNotFound); sl@0: } sl@0: delete fileMan; sl@0: sl@0: // Clear the substituted drive sl@0: r=TheFs.SetSubst(KNullDesC, EDriveN); sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: /* sl@0: void TestSystemBinSubsted() sl@0: // sl@0: // Test an internal drive on system dir sl@0: // sl@0: { sl@0: test.Next(_L("Testing System bin -> Subst")); sl@0: TInt r=0; sl@0: CFileMan* fileMan=NULL; sl@0: TRAP(r,fileMan=CFileMan::NewL(TheFs)); sl@0: test(r==KErrNone); sl@0: sl@0: TBuf<50> dllfilenames; sl@0: TBuf<50> substed; sl@0: RLibrary lib; sl@0: r=TheFs.SetSubst(KSystemLibs, 14); //set up O as the substed drive sl@0: test(r==KErrNone); sl@0: for(TInt i=0;i<14;i++) sl@0: { sl@0: RDebug::Print(_L("doing modify caps = %d"),i); sl@0: ModifyModuleCapabilties(KCapabilityRoot,i); sl@0: dllfilenames=KNewDllName; sl@0: dllfilenames.AppendNum(i); sl@0: dllfilenames +=KDllExt; sl@0: sl@0: RDebug::Print(_L("Module = %S"),&dllfilenames); sl@0: r=lib.Load(dllfilenames); sl@0: test(r==KErrNone);//as this process has Root caps and the dlls should be loaded with non sl@0: substed=_L("O:\\DLLTS"); sl@0: substed.AppendNum(i); sl@0: substed+=KDllExt; sl@0: sl@0: r=lib.Load(substed); sl@0: test(r==KErrNone); sl@0: lib.Close(); sl@0: } sl@0: TBuf<50> exefilenames; sl@0: RProcess p; sl@0: TBuf<16> cmd; sl@0: sl@0: for(TInt j=14;j<16;j++) sl@0: { sl@0: exefilenames=KNewExeName; sl@0: exefilenames.AppendNum(j); sl@0: exefilenames +=KExeExt; sl@0: ModifyModuleCapabilties(KCapabilityRoot,j); sl@0: r=p.Create(exefilenames, cmd); sl@0: test(r==KErrNone); sl@0: p.Terminate(0); sl@0: p.Close(); sl@0: sl@0: sl@0: substed=_L("O:\\EXETS"); sl@0: substed.AppendNum(j); sl@0: substed+=KExeExt; sl@0: RDebug::Print(_L("substed=%S"),&substed); sl@0: r=p.Create(substed, cmd); sl@0: test(r==KErrNone); sl@0: p.Terminate(0); sl@0: p.Close(); sl@0: } sl@0: delete fileMan; sl@0: } sl@0: */ sl@0: #endif sl@0: sl@0: sl@0: sl@0: void HashBM() sl@0: // sl@0: // time how long to load a small dll 100 times and one large one sl@0: // sl@0: { sl@0: test.Next(_L("Next Do Bench Mark\n")); sl@0: TBuf<30> dlldestination; sl@0: dlldestination=KNewDllName; sl@0: dlldestination[0]='D'; sl@0: dlldestination.AppendNum(1); sl@0: dlldestination+=KDllExt; sl@0: RLibrary lib; sl@0: TInt r=0; sl@0: TUint32 startcount = User::NTickCount(); sl@0: for (TInt i=0;i<100;i++) sl@0: { sl@0: r=lib.Load(dlldestination); sl@0: lib.Close(); sl@0: } sl@0: TUint32 endcount = User::NTickCount(); sl@0: test(r==KErrNone); sl@0: RDebug::Print(_L("100 * 4k dll \n")); sl@0: RDebug::Print(_L("start count=%d, end count=%d, dif=%d\n"),startcount,endcount,endcount-startcount); sl@0: sl@0: startcount = User::NTickCount(); sl@0: r=lib.Load(_L("D:\\sys\\bin\\euser.dll")); sl@0: lib.Close(); sl@0: endcount = User::NTickCount(); sl@0: RDebug::Print(_L("r=%d")); sl@0: // test(r==KErrNone); sl@0: RDebug::Print(_L("1 * 233k dll \n")); sl@0: RDebug::Print(_L("start count=%d, end count=%d, dif=%d\n"),startcount,endcount,endcount-startcount); sl@0: } sl@0: sl@0: //------------------------------------------------------- sl@0: sl@0: /** sl@0: testing RLoader::CheckLibraryHash() API sl@0: */ sl@0: void TestCheckLibraryHash() sl@0: { sl@0: test.Next(_L("Testing CheckLibraryHash API\n")); sl@0: sl@0: TInt r=0; sl@0: CFileMan* pFileMan=NULL; sl@0: TRAP(r,pFileMan=CFileMan::NewL(TheFs)); sl@0: test(r==KErrNone); sl@0: sl@0: RLoader loader; sl@0: r=loader.Connect(); sl@0: test(r==KErrNone); sl@0: sl@0: //-- 1. copy test DLL to the specified drive and create hash file for it. sl@0: TBuf<40> dllFileName; sl@0: TBuf<40> dllDestination; sl@0: sl@0: const TInt KDllNumber = 0; sl@0: sl@0: dllFileName=KDllfilename; sl@0: dllFileName.AppendNum(KDllNumber); sl@0: dllFileName+=KDllExt; sl@0: sl@0: dllDestination=KNewDllName; sl@0: dllDestination[0]=(TUint16)gDriveToTest; sl@0: dllDestination.AppendNum(KDllNumber); sl@0: dllDestination+=KDllExt; sl@0: sl@0: test.Printf(_L("Copy %S to %S\n"), &dllFileName, &dllDestination); sl@0: sl@0: r=TheFs.MkDirAll(dllDestination); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=pFileMan->Copy(dllFileName, dllDestination); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=pFileMan->Attribs(dllDestination, 0, KEntryAttReadOnly, TTime(0) ,0); sl@0: test(r==KErrNone); sl@0: sl@0: test.Printf(_L("Creating Dll hash.\n")); sl@0: CreateFileHash(dllDestination); sl@0: sl@0: //-- 2. check if the hash exists sl@0: r=loader.CheckLibraryHash(dllDestination); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("Dll hash exists.\n")); sl@0: sl@0: //-- 2.1 check if the hash exists and valid sl@0: r=loader.CheckLibraryHash(dllDestination, ETrue); sl@0: test(r==KErrNone); sl@0: sl@0: test.Printf(_L("Dll hash exists and valid.\n")); sl@0: sl@0: //-- 3. corrupt dll sl@0: r = CorruptFile(dllDestination); sl@0: test(r==KErrNone); sl@0: sl@0: //-- 3.1 check that the hash exists, but is incorrect. sl@0: r=loader.CheckLibraryHash(dllDestination); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("Dll hash exists.\n")); sl@0: sl@0: r=loader.CheckLibraryHash(dllDestination, ETrue); sl@0: test(r==KErrCorrupt); sl@0: sl@0: test.Printf(_L("Dll hash exists and INVALID.\n")); sl@0: sl@0: //-- 4. try to locte hash fo the unexisting dll. sl@0: r=loader.CheckLibraryHash(_L("Z:\\sys\\bin\\NotExist.dll")); sl@0: test(r==KErrNotFound); sl@0: sl@0: loader.Close(); sl@0: sl@0: delete pFileMan; sl@0: } sl@0: sl@0: //------------------------------------------------------- sl@0: sl@0: void ParseCommandArguments() sl@0: // sl@0: // sl@0: // sl@0: { sl@0: TBuf<0x100> cmd; sl@0: User::CommandLine(cmd); sl@0: TLex lex(cmd); sl@0: TPtrC token=lex.NextToken(); sl@0: TFileName thisfile=RProcess().FileName(); sl@0: if (token.MatchF(thisfile)==0) sl@0: { sl@0: token.Set(lex.NextToken()); sl@0: } sl@0: test.Printf(_L("CLP=%S"),&token); sl@0: sl@0: if(token.Length()!=0) sl@0: { sl@0: gDriveToTest=token[0]; sl@0: gDriveToTest.UpperCase(); sl@0: } sl@0: else sl@0: gDriveToTest='D'; //default to D: sl@0: } sl@0: sl@0: sl@0: //------------------------------------------------------- sl@0: sl@0: void TestExes() sl@0: { sl@0: test.Next(_L("Testing Exes presence.\n")); sl@0: sl@0: TInt r; sl@0: sl@0: RProcess ap; sl@0: r=LoadExe(14,ap); sl@0: test(r==KErrNone); sl@0: ap.Terminate(0); sl@0: ap.Close(); sl@0: sl@0: r=LoadExe(15,ap); sl@0: test(r==KErrNone); sl@0: ap.Terminate(0); sl@0: ap.Close(); sl@0: } sl@0: sl@0: sl@0: //------------------------------------------------------- sl@0: sl@0: static void CallTestsL(void) sl@0: { sl@0: sl@0: #ifdef __WINS__ sl@0: test.Printf(_L("Not testing on WINS !\n")); sl@0: return; sl@0: #else sl@0: sl@0: TestExes(); sl@0: // TestGetCapability(); sl@0: CopyModules(); //copies modules from ROM to disk so they may be modified sl@0: // TestLoading(); sl@0: // TestLoadLibrary(); sl@0: sl@0: sl@0: #ifndef WIN32 sl@0: if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin)) sl@0: TestRemovableMediaWithHash(); sl@0: sl@0: // TestSystemBinSubsted(); sl@0: // TestRemovableMedia(); sl@0: TestNonSystemSubsted(); sl@0: #endif //WIN32 sl@0: // HashBM(); sl@0: sl@0: TestCheckLibraryHash(); sl@0: sl@0: #endif //#ifndef __WINS__ sl@0: sl@0: } sl@0: sl@0: //------------------------------------------------------- sl@0: // sl@0: // main sl@0: // sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: ParseCommandArguments(); //need this for drive letter to test sl@0: sl@0: test.Title(); sl@0: test.Start(_L("Setup\n")); sl@0: CTrapCleanup* cleanup; sl@0: cleanup=CTrapCleanup::New(); sl@0: __UHEAP_MARK; sl@0: sl@0: TBuf<20> sessPath; sl@0: TInt r=0; sl@0: r=TheFs.Connect(); sl@0: test(r==KErrNone); sl@0: r=TheFs.SessionPath(sessPath); sl@0: test(r==KErrNone); sl@0: sl@0: TInt drive; sl@0: RFs::CharToDrive(gDriveToTest, drive); sl@0: TDriveInfo info; sl@0: r=TheFs.Drive(info, drive); sl@0: test(r==KErrNone); sl@0: sl@0: if((info.iDriveAtt & KDriveAttRemovable) == 0) sl@0: { sl@0: test.Printf(_L("Not testing on non-removable media !\n")); sl@0: } sl@0: else sl@0: {//-- testing on removable media sl@0: TRAP(r,CallTestsL()); sl@0: } sl@0: sl@0: TheFs.Close(); sl@0: test.End(); sl@0: test.Close(); sl@0: sl@0: __UHEAP_MARKEND; sl@0: delete cleanup; sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: