kerneltest/f32test/loader/t_loader_delete.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // \f32test\loader\t_loader_delete.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <f32file.h>
       
    21 
       
    22 #include "t_loader_delete.h"
       
    23 
       
    24 static RTest test(_L("t_loader_delete"));
       
    25 
       
    26 // helper functions
       
    27 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError);
       
    28 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError, const TDesC& aFileName);
       
    29 static void SetHelperCaps(TUint32 aCaps);
       
    30 static void CreateTestFile(RFs& aFs, const TDesC& aTestFile);
       
    31 static void RunHelper(const TDesC& aFileToDelete, TInt aExpectedError);
       
    32 
       
    33 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError)
       
    34 /**
       
    35 	Test calling RLoader::Delete from a process with the supplied capabilities.
       
    36 
       
    37 	@param	aCapMask		Capabilities of process which calls RLoader::Delete.
       
    38 	@param	aExpectedError	Expected error reason.  The launched executable is expected
       
    39 							to panic with category KTldPanicCat and this reason, which
       
    40 							is the expected return code from RLoader::Delete.
       
    41  */
       
    42 	{
       
    43 	TestWithCaps(aCaps, aExpectedError, KTldTcbFile);
       
    44 	TestWithCaps(aCaps, aExpectedError, KTldAllFilesFile);
       
    45 
       
    46 	// the following function function calls should fail with either
       
    47 	// KErrPermissionDenied if this process is not TCB+AllFiles, or with KErrBadName
       
    48 	// because the filename is not fully qualified.
       
    49 	TBool pdExp = (aExpectedError == KErrPermissionDenied);
       
    50 	
       
    51 	// test filenames which are not fully qualified
       
    52 	TInt remapErr = pdExp ? KErrPermissionDenied : KErrBadName;
       
    53 	TestWithCaps(aCaps, remapErr, KTldFileNoPath);
       
    54 	TestWithCaps(aCaps, remapErr, KTldFileNoDrive);
       
    55 	
       
    56 	// test cannot delete non-existent file
       
    57 	TInt rootNonExistErr = pdExp ? KErrPermissionDenied : KErrNotFound;
       
    58 	TestWithCaps(aCaps, rootNonExistErr, KTldFileNonExistRoot);
       
    59 	TInt dirNonExistErr = pdExp ? KErrPermissionDenied : KErrPathNotFound;
       
    60 	TestWithCaps(aCaps, dirNonExistErr, KTldFileNonExistDir);
       
    61 	}
       
    62 
       
    63 static void TestWithCaps(TUint32 aCaps, TInt aExpectedError, const TDesC& aFileName)
       
    64 /**
       
    65 	Helper function for TestWithCaps(TUint32, TInt).  This function invokes
       
    66 	a helper executable with the supplied capabilities and tells it to delete
       
    67 	the supplied filename.
       
    68 	
       
    69  	@param	aCapMask		Capabilities of process which calls RLoader::Delete.
       
    70 	@param	aExpectedError	Expected error reason.  The launched executable is expected
       
    71 							to panic with category KTldPanicCat and this reason, which
       
    72 							is the expected return code from RLoader::Delete.
       
    73 	@param	aFileName		The helper executable is told to delete this file.
       
    74 */
       
    75 	{
       
    76 	test.Printf(
       
    77 		_L("TestWithCaps,aCaps=0x%x,aExpectedError=%d,aFileName=\"%S\"\n"),
       
    78 		aCaps, aExpectedError, &aFileName);
       
    79 
       
    80 	TInt r;
       
    81 
       
    82 	// create the file to delete
       
    83 	RFs fs;
       
    84 	r = fs.Connect();
       
    85 	test(r == KErrNone);
       
    86 
       
    87 	// if this file is expected to exist then create it
       
    88 	TPtrC dirName;
       
    89 	TBool shouldExist = (aFileName == KTldTcbFile || aFileName == KTldAllFilesFile);
       
    90 	if (shouldExist)
       
    91 		{
       
    92 		TParsePtrC pp(aFileName);
       
    93 		dirName.Set(pp.DriveAndPath());
       
    94 		r = fs.MkDirAll(dirName);
       
    95 		test(r == KErrNone || r == KErrAlreadyExists);
       
    96 		CreateTestFile(fs, aFileName);
       
    97 		}
       
    98 
       
    99 	SetHelperCaps(aCaps);
       
   100 	RunHelper(aFileName, aExpectedError);
       
   101 
       
   102 	if (shouldExist)
       
   103 		{
       
   104 		// if the file could not be deleted then delete it now
       
   105 		TEntry e;
       
   106 		// a C++ bool for the following equality operator
       
   107 		bool exists = (fs.Entry(aFileName, e) == KErrNone);
       
   108 		test(exists == (aExpectedError != KErrNone));
       
   109 		
       
   110 		if (exists)
       
   111 			{
       
   112 			r = fs.Delete(aFileName);
       
   113 			test(r == KErrNone);
       
   114 			}
       
   115 
       
   116 		// delete the immediate containing directory.  The error code is not
       
   117 		// used because the directory may be used for something else.
       
   118 		fs.RmDir(dirName);
       
   119 		}
       
   120 
       
   121 	// delete the generated different-caps file
       
   122 	r = fs.Delete(_L("c:\\sys\\bin\\tld_helper_caps.exe"));
       
   123 	test(r == KErrNone);
       
   124 	r = fs.Delete(_L("c:\\sys\\hash\\tld_helper_caps.exe"));
       
   125 	test(r == KErrNone || r == KErrNotFound || r == KErrPathNotFound);
       
   126 
       
   127 	fs.Close();
       
   128 	}
       
   129 
       
   130 static void SetHelperCaps(TUint32 aCaps)
       
   131 /**
       
   132 	Create an instance of the helper executable, tld_helper.exe,
       
   133 	with the supplied capabilities.
       
   134  */
       
   135 	{
       
   136 	TInt r;
       
   137 	_LIT(KCommandLineArgsFormat, "tld_helper.exe %x c:\\sys\\bin\\tld_helper_caps.exe");
       
   138 	TBuf<128> cmdLine;
       
   139 	cmdLine.Format(KCommandLineArgsFormat, aCaps);
       
   140 
       
   141 	RProcess p;
       
   142 	r = p.Create(_L("setcap.exe"), cmdLine);
       
   143 	test(r == KErrNone);
       
   144 
       
   145 	TRequestStatus rs;
       
   146 	p.Logon(rs);
       
   147 	test(rs == KRequestPending);
       
   148 	p.Resume();
       
   149 	User::WaitForRequest(rs);
       
   150 
       
   151 	p.Close();
       
   152 	}
       
   153 
       
   154 static void CreateTestFile(RFs& aFs, const TDesC& aTestFile)
       
   155 /**
       
   156 	Create an empty file with the supplied name.  This function is used
       
   157 	to create file which can be deleted with RLoader::Delete.
       
   158 	
       
   159 	@param	aFs				Open file server session.
       
   160 	@param	aTestFile		The test file's name.
       
   161  */
       
   162 	{
       
   163 	TInt r;
       
   164 
       
   165 	RFile f;
       
   166 	r = f.Replace(aFs, aTestFile, EFileWrite | EFileStream | EFileShareExclusive);
       
   167 	test(r == KErrNone);
       
   168 	f.Close();
       
   169 	}
       
   170 
       
   171 static void RunHelper(const TDesC& aFileToDelete, TInt aExpectedError)
       
   172 /**
       
   173 	Invoke the helper executable, tell it to delete the supplied file.
       
   174 	
       
   175 	@param	aFileToDelete	Name of file which helper executable should delete
       
   176 							with RLoader::Delete.
       
   177 	@param	aExpectedError	The expected return code from RLoader::Delete.
       
   178  */
       
   179 	{
       
   180 	TInt r;
       
   181 	
       
   182 	// run the helper exe, which will try to delete the file with RLoader::Delete
       
   183 	RProcess ph;
       
   184 	r = ph.Create(_L("tld_helper_caps.exe"), aFileToDelete);
       
   185 	test(r == KErrNone);
       
   186 
       
   187 	TRequestStatus rsh;
       
   188 	ph.Logon(rsh);
       
   189 	test(rsh == KRequestPending);
       
   190 	ph.Resume();
       
   191 	User::WaitForRequest(rsh);
       
   192 
       
   193 	// process has died so check the panic category and reason match the expected values
       
   194 	test(ph.ExitType() == EExitPanic);
       
   195 	test(ph.ExitCategory() == KTldPanicCat);
       
   196 	test(ph.ExitReason() == aExpectedError);
       
   197 
       
   198 	ph.Close();
       
   199 	}
       
   200 
       
   201 TInt E32Main()
       
   202 /** 
       
   203 	Executable entrypoint calls test functions within heap check.
       
   204  */
       
   205 	{
       
   206 	test.Title();
       
   207 	test.Start(_L("Testing RLoader::Delete"));
       
   208 
       
   209 	__UHEAP_MARK;
       
   210 	const TUint32 KTcbMask = 1UL << ECapabilityTCB;
       
   211 	const TUint32 KAllFilesMask = 1UL << ECapabilityAllFiles;
       
   212 
       
   213 	//Check whether RLoader::Delete handles the case of a bad descriptor being passed 
       
   214 	//as the filename( KBadDescriptor is not itself the malformed desciptor but
       
   215 	//it trigers the check)
       
   216 	TestWithCaps(KTcbMask | KAllFilesMask     , KErrBadDescriptor, KBadDescriptor);
       
   217 
       
   218 	// TCB | AllFiles sufficient without any other caps
       
   219 	TestWithCaps(KTcbMask | KAllFilesMask, KErrNone);
       
   220 	// TCB necessary
       
   221 	TestWithCaps(~KTcbMask, KErrPermissionDenied);
       
   222 	// AllFiles necessary
       
   223 	TestWithCaps(~KAllFilesMask, KErrPermissionDenied);
       
   224 	// neither TCB nor AllFiles
       
   225 	TestWithCaps(~(KTcbMask | KAllFilesMask), KErrPermissionDenied);
       
   226 	TestWithCaps(0, KErrPermissionDenied);
       
   227 	__UHEAP_MARKEND;
       
   228 
       
   229 	test.End();
       
   230 	return KErrNone;
       
   231     }
       
   232 
       
   233