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