os/kernelhwsrv/kerneltest/f32test/loader/t_loader_delete.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/loader/t_loader_delete.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,233 @@
     1.4 +// Copyright (c) 2006-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_loader_delete.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +
    1.22 +#include <e32test.h>
    1.23 +#include <f32file.h>
    1.24 +
    1.25 +#include "t_loader_delete.h"
    1.26 +
    1.27 +static RTest test(_L("t_loader_delete"));
    1.28 +
    1.29 +// helper functions
    1.30 +static void TestWithCaps(TUint32 aCaps, TInt aExpectedError);
    1.31 +static void TestWithCaps(TUint32 aCaps, TInt aExpectedError, const TDesC& aFileName);
    1.32 +static void SetHelperCaps(TUint32 aCaps);
    1.33 +static void CreateTestFile(RFs& aFs, const TDesC& aTestFile);
    1.34 +static void RunHelper(const TDesC& aFileToDelete, TInt aExpectedError);
    1.35 +
    1.36 +static void TestWithCaps(TUint32 aCaps, TInt aExpectedError)
    1.37 +/**
    1.38 +	Test calling RLoader::Delete from a process with the supplied capabilities.
    1.39 +
    1.40 +	@param	aCapMask		Capabilities of process which calls RLoader::Delete.
    1.41 +	@param	aExpectedError	Expected error reason.  The launched executable is expected
    1.42 +							to panic with category KTldPanicCat and this reason, which
    1.43 +							is the expected return code from RLoader::Delete.
    1.44 + */
    1.45 +	{
    1.46 +	TestWithCaps(aCaps, aExpectedError, KTldTcbFile);
    1.47 +	TestWithCaps(aCaps, aExpectedError, KTldAllFilesFile);
    1.48 +
    1.49 +	// the following function function calls should fail with either
    1.50 +	// KErrPermissionDenied if this process is not TCB+AllFiles, or with KErrBadName
    1.51 +	// because the filename is not fully qualified.
    1.52 +	TBool pdExp = (aExpectedError == KErrPermissionDenied);
    1.53 +	
    1.54 +	// test filenames which are not fully qualified
    1.55 +	TInt remapErr = pdExp ? KErrPermissionDenied : KErrBadName;
    1.56 +	TestWithCaps(aCaps, remapErr, KTldFileNoPath);
    1.57 +	TestWithCaps(aCaps, remapErr, KTldFileNoDrive);
    1.58 +	
    1.59 +	// test cannot delete non-existent file
    1.60 +	TInt rootNonExistErr = pdExp ? KErrPermissionDenied : KErrNotFound;
    1.61 +	TestWithCaps(aCaps, rootNonExistErr, KTldFileNonExistRoot);
    1.62 +	TInt dirNonExistErr = pdExp ? KErrPermissionDenied : KErrPathNotFound;
    1.63 +	TestWithCaps(aCaps, dirNonExistErr, KTldFileNonExistDir);
    1.64 +	}
    1.65 +
    1.66 +static void TestWithCaps(TUint32 aCaps, TInt aExpectedError, const TDesC& aFileName)
    1.67 +/**
    1.68 +	Helper function for TestWithCaps(TUint32, TInt).  This function invokes
    1.69 +	a helper executable with the supplied capabilities and tells it to delete
    1.70 +	the supplied filename.
    1.71 +	
    1.72 + 	@param	aCapMask		Capabilities of process which calls RLoader::Delete.
    1.73 +	@param	aExpectedError	Expected error reason.  The launched executable is expected
    1.74 +							to panic with category KTldPanicCat and this reason, which
    1.75 +							is the expected return code from RLoader::Delete.
    1.76 +	@param	aFileName		The helper executable is told to delete this file.
    1.77 +*/
    1.78 +	{
    1.79 +	test.Printf(
    1.80 +		_L("TestWithCaps,aCaps=0x%x,aExpectedError=%d,aFileName=\"%S\"\n"),
    1.81 +		aCaps, aExpectedError, &aFileName);
    1.82 +
    1.83 +	TInt r;
    1.84 +
    1.85 +	// create the file to delete
    1.86 +	RFs fs;
    1.87 +	r = fs.Connect();
    1.88 +	test(r == KErrNone);
    1.89 +
    1.90 +	// if this file is expected to exist then create it
    1.91 +	TPtrC dirName;
    1.92 +	TBool shouldExist = (aFileName == KTldTcbFile || aFileName == KTldAllFilesFile);
    1.93 +	if (shouldExist)
    1.94 +		{
    1.95 +		TParsePtrC pp(aFileName);
    1.96 +		dirName.Set(pp.DriveAndPath());
    1.97 +		r = fs.MkDirAll(dirName);
    1.98 +		test(r == KErrNone || r == KErrAlreadyExists);
    1.99 +		CreateTestFile(fs, aFileName);
   1.100 +		}
   1.101 +
   1.102 +	SetHelperCaps(aCaps);
   1.103 +	RunHelper(aFileName, aExpectedError);
   1.104 +
   1.105 +	if (shouldExist)
   1.106 +		{
   1.107 +		// if the file could not be deleted then delete it now
   1.108 +		TEntry e;
   1.109 +		// a C++ bool for the following equality operator
   1.110 +		bool exists = (fs.Entry(aFileName, e) == KErrNone);
   1.111 +		test(exists == (aExpectedError != KErrNone));
   1.112 +		
   1.113 +		if (exists)
   1.114 +			{
   1.115 +			r = fs.Delete(aFileName);
   1.116 +			test(r == KErrNone);
   1.117 +			}
   1.118 +
   1.119 +		// delete the immediate containing directory.  The error code is not
   1.120 +		// used because the directory may be used for something else.
   1.121 +		fs.RmDir(dirName);
   1.122 +		}
   1.123 +
   1.124 +	// delete the generated different-caps file
   1.125 +	r = fs.Delete(_L("c:\\sys\\bin\\tld_helper_caps.exe"));
   1.126 +	test(r == KErrNone);
   1.127 +	r = fs.Delete(_L("c:\\sys\\hash\\tld_helper_caps.exe"));
   1.128 +	test(r == KErrNone || r == KErrNotFound || r == KErrPathNotFound);
   1.129 +
   1.130 +	fs.Close();
   1.131 +	}
   1.132 +
   1.133 +static void SetHelperCaps(TUint32 aCaps)
   1.134 +/**
   1.135 +	Create an instance of the helper executable, tld_helper.exe,
   1.136 +	with the supplied capabilities.
   1.137 + */
   1.138 +	{
   1.139 +	TInt r;
   1.140 +	_LIT(KCommandLineArgsFormat, "tld_helper.exe %x c:\\sys\\bin\\tld_helper_caps.exe");
   1.141 +	TBuf<128> cmdLine;
   1.142 +	cmdLine.Format(KCommandLineArgsFormat, aCaps);
   1.143 +
   1.144 +	RProcess p;
   1.145 +	r = p.Create(_L("setcap.exe"), cmdLine);
   1.146 +	test(r == KErrNone);
   1.147 +
   1.148 +	TRequestStatus rs;
   1.149 +	p.Logon(rs);
   1.150 +	test(rs == KRequestPending);
   1.151 +	p.Resume();
   1.152 +	User::WaitForRequest(rs);
   1.153 +
   1.154 +	p.Close();
   1.155 +	}
   1.156 +
   1.157 +static void CreateTestFile(RFs& aFs, const TDesC& aTestFile)
   1.158 +/**
   1.159 +	Create an empty file with the supplied name.  This function is used
   1.160 +	to create file which can be deleted with RLoader::Delete.
   1.161 +	
   1.162 +	@param	aFs				Open file server session.
   1.163 +	@param	aTestFile		The test file's name.
   1.164 + */
   1.165 +	{
   1.166 +	TInt r;
   1.167 +
   1.168 +	RFile f;
   1.169 +	r = f.Replace(aFs, aTestFile, EFileWrite | EFileStream | EFileShareExclusive);
   1.170 +	test(r == KErrNone);
   1.171 +	f.Close();
   1.172 +	}
   1.173 +
   1.174 +static void RunHelper(const TDesC& aFileToDelete, TInt aExpectedError)
   1.175 +/**
   1.176 +	Invoke the helper executable, tell it to delete the supplied file.
   1.177 +	
   1.178 +	@param	aFileToDelete	Name of file which helper executable should delete
   1.179 +							with RLoader::Delete.
   1.180 +	@param	aExpectedError	The expected return code from RLoader::Delete.
   1.181 + */
   1.182 +	{
   1.183 +	TInt r;
   1.184 +	
   1.185 +	// run the helper exe, which will try to delete the file with RLoader::Delete
   1.186 +	RProcess ph;
   1.187 +	r = ph.Create(_L("tld_helper_caps.exe"), aFileToDelete);
   1.188 +	test(r == KErrNone);
   1.189 +
   1.190 +	TRequestStatus rsh;
   1.191 +	ph.Logon(rsh);
   1.192 +	test(rsh == KRequestPending);
   1.193 +	ph.Resume();
   1.194 +	User::WaitForRequest(rsh);
   1.195 +
   1.196 +	// process has died so check the panic category and reason match the expected values
   1.197 +	test(ph.ExitType() == EExitPanic);
   1.198 +	test(ph.ExitCategory() == KTldPanicCat);
   1.199 +	test(ph.ExitReason() == aExpectedError);
   1.200 +
   1.201 +	ph.Close();
   1.202 +	}
   1.203 +
   1.204 +TInt E32Main()
   1.205 +/** 
   1.206 +	Executable entrypoint calls test functions within heap check.
   1.207 + */
   1.208 +	{
   1.209 +	test.Title();
   1.210 +	test.Start(_L("Testing RLoader::Delete"));
   1.211 +
   1.212 +	__UHEAP_MARK;
   1.213 +	const TUint32 KTcbMask = 1UL << ECapabilityTCB;
   1.214 +	const TUint32 KAllFilesMask = 1UL << ECapabilityAllFiles;
   1.215 +
   1.216 +	//Check whether RLoader::Delete handles the case of a bad descriptor being passed 
   1.217 +	//as the filename( KBadDescriptor is not itself the malformed desciptor but
   1.218 +	//it trigers the check)
   1.219 +	TestWithCaps(KTcbMask | KAllFilesMask     , KErrBadDescriptor, KBadDescriptor);
   1.220 +
   1.221 +	// TCB | AllFiles sufficient without any other caps
   1.222 +	TestWithCaps(KTcbMask | KAllFilesMask, KErrNone);
   1.223 +	// TCB necessary
   1.224 +	TestWithCaps(~KTcbMask, KErrPermissionDenied);
   1.225 +	// AllFiles necessary
   1.226 +	TestWithCaps(~KAllFilesMask, KErrPermissionDenied);
   1.227 +	// neither TCB nor AllFiles
   1.228 +	TestWithCaps(~(KTcbMask | KAllFilesMask), KErrPermissionDenied);
   1.229 +	TestWithCaps(0, KErrPermissionDenied);
   1.230 +	__UHEAP_MARKEND;
   1.231 +
   1.232 +	test.End();
   1.233 +	return KErrNone;
   1.234 +    }
   1.235 +
   1.236 +