testexecfw/stf/stfext/testmodules/teftestmod/teftestmodulefw/tefutilities/platsec/src/testplatsecsetcapabilities.cpp
changeset 2 8bb370ba6d1d
equal deleted inserted replaced
1:bbd31066657e 2:8bb370ba6d1d
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 * This contains CTestPlatSecSetCapabilities class functions implementation.
       
    16 * This class sets the capabilities of the executables and libraries.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include "testplatsecsetcapabilities.h"
       
    22 
       
    23 //TEF include
       
    24 #include <test/testexecutelog.h>
       
    25 
       
    26 /*@{*/
       
    27 _LIT(KExecutableName,		"executable");
       
    28 _LIT(KCapabalities,			"capabilities");
       
    29 _LIT(KDestinationPath,		"destinationPath");
       
    30 /*@}*/
       
    31 
       
    32 // Test step name
       
    33 _LIT(KSetCapabilitiesStep,	"SetCapabilities");
       
    34 
       
    35 /**
       
    36  Destructor
       
    37 */
       
    38 CTestPlatSecSetCapabilities::~CTestPlatSecSetCapabilities()
       
    39 	{
       
    40 	}
       
    41 
       
    42 /** 
       
    43  Constructor
       
    44 */
       
    45 CTestPlatSecSetCapabilities::CTestPlatSecSetCapabilities()
       
    46 	{
       
    47 	SetTestStepName(KSetCapabilitiesStep);
       
    48 	}
       
    49 
       
    50 /**
       
    51  The implementation of the CTestStepPlatTest::doTestStepL() pure virutal
       
    52  function. Sets the defined capabilities to the executables. It reads 
       
    53  the executable name, destination executable name and its capabilities 
       
    54  from the ini file.
       
    55  @return TVerdict - result of the test step
       
    56  @leave - system wide error codes
       
    57 */
       
    58 TVerdict CTestPlatSecSetCapabilities::doTestStepL()
       
    59 	{
       
    60 	INFO_PRINTF1(_L("Setting the capabilities of the executable."));
       
    61 
       
    62 	TPtrC ServerName(GetServerName());
       
    63 	INFO_PRINTF1(_L("Server Name"));
       
    64 	INFO_PRINTF2(_L("Server Name %S"), &ServerName);
       
    65 	
       
    66 	//read the executable name from the ini file
       
    67 	TPtrC	executableName;
       
    68 	if(!GetStringFromConfig(ConfigSection(), KExecutableName, executableName))
       
    69 		{
       
    70 		ERR_PRINTF1(_L("Executable/Server name not provided in INI file"));
       
    71 		SetTestStepResult(EFail);
       
    72 		}
       
    73 	else
       
    74 		{
       
    75 		INFO_PRINTF2(_L("Executable name read from the ini file is  %S"),&executableName); 
       
    76 		//read capabilities to be set
       
    77 		TPtrC capabilities;
       
    78 		if(!GetStringFromConfig(ConfigSection(), KCapabalities, capabilities))
       
    79 			{
       
    80 			ERR_PRINTF1(_L("Capabilities to be set are not specified in the INI file")); 
       
    81 			SetTestStepResult(EFail);
       
    82 			}
       
    83 		else
       
    84 			{
       
    85 			INFO_PRINTF2(_L("New capabilities for the executable :  %S"),&capabilities); 
       
    86 			//read the destination executable name including path
       
    87 			TPtrC destination;
       
    88 			if(!GetStringFromConfig(ConfigSection(), KDestinationPath, destination))
       
    89 				{
       
    90 				ERR_PRINTF1(_L("Destination  not specified in the INI file")); 
       
    91 				SetTestStepResult(EFail);
       
    92 				}
       
    93 			else
       
    94 				{
       
    95 				INFO_PRINTF2(_L("Destination path specified is  :  %S"),&destination); 
       
    96 				//set the capabilities
       
    97 				SetCapabilitiesL(executableName, capabilities,destination);
       
    98 				}
       
    99 			}
       
   100 		}
       
   101 	return TestStepResult();
       
   102 	}
       
   103 
       
   104 /**
       
   105  Sets the capabilites for a given executable. Uses SetCap.exe
       
   106  @param aExecutable - name of the executable
       
   107  @param aCapabilities - capabilities to be set for the executable
       
   108  @param aDestination - name of the output executable
       
   109  @leave - system wide error codes
       
   110  @return None
       
   111 */
       
   112 void CTestPlatSecSetCapabilities::SetCapabilitiesL(const TDesC& aExecutable, const TDesC& aCapabilities,const TDesC& aDestination)
       
   113 	{
       
   114 	//create the commandline arguments
       
   115 	TBuf<KMaxTestExecuteCommandLength> buf(aCapabilities);
       
   116 	TUint hexCapability = CreateHexCapabilities(buf);
       
   117 
       
   118 	INFO_PRINTF2( _L("Capality Hex value is %X"),hexCapability);
       
   119 	TBuf<KMaxTestExecuteCommandLength> commandLine(aExecutable);
       
   120 	commandLine.Append(_L(" "));
       
   121 	commandLine.AppendNumUC(hexCapability,EHex);
       
   122 	commandLine.Append(_L(" "));
       
   123 	commandLine.Append(aDestination);
       
   124 
       
   125 	INFO_PRINTF2( _L("command line arguments for setcap  is  %S"),&commandLine);
       
   126 	
       
   127 	//instantiate Setcap.exe as a new process with the executable and capabilities as command line arguments
       
   128 	RProcess process;
       
   129 	CleanupClosePushL(process);
       
   130 	TInt err=KErrNone;
       
   131 	User::LeaveIfError(err = process.Create(_L("setcap.exe"),commandLine));
       
   132 	if(err!=KErrNone)
       
   133 		{
       
   134 		ERR_PRINTF2(_L("Creating the process failed with error : %D"),err);
       
   135 		SetTestStepResult(EFail);
       
   136 		}
       
   137 	else
       
   138 		{
       
   139 		TRAPD(error,process.Resume();)
       
   140 		if(error!=KErrNone)
       
   141 			{
       
   142 			ERR_PRINTF2(_L("Creating the process failed with error : %D"),err);
       
   143 			SetTestStepResult(EFail);
       
   144 			}
       
   145 		}
       
   146 	CleanupStack::PopAndDestroy(1);
       
   147 	}
       
   148 
       
   149 /**
       
   150  Creates the hex values to be passed to SetCap.exe.
       
   151  @param aCapabilities - capabilites for which hex values to be created
       
   152  @return TInt - integer value after manipulating all capabilities
       
   153  @leave - none
       
   154 */
       
   155 TInt  CTestPlatSecSetCapabilities::CreateHexCapabilities(TDesC& aCapabilities )
       
   156 	{
       
   157 	TInt capability=0;
       
   158 	TInt capCountCheck=0;
       
   159 	TLex parser(aCapabilities);
       
   160 	
       
   161 	//create the capability hex value
       
   162 	while(!parser.Eos())
       
   163 		{
       
   164 		TPtrC token(parser.NextToken());
       
   165 		INFO_PRINTF2( _L("Adding capability %S"),&token);
       
   166 
       
   167 		TCapability capabilityValue;
       
   168 		
       
   169 		TInt err = GetCapability(token, capabilityValue);
       
   170 		if (err != KErrNone)
       
   171 			{
       
   172 			WARN_PRINTF1(_L("Unrecognised capability, rejected"));
       
   173 			}
       
   174 		else
       
   175 			{
       
   176 			capCountCheck++;
       
   177 			if(capabilityValue == ECapability_None)
       
   178 				{
       
   179 				capability = ECapability_None;
       
   180 				break;
       
   181 				}
       
   182 			else
       
   183 				{
       
   184 				capability = capability | (1<<capabilityValue);
       
   185 				}
       
   186 			}
       
   187 		if(capCountCheck == 0)
       
   188 			{
       
   189 			WARN_PRINTF1(_L("No capability given is valid, capability set to None"));
       
   190 			capability = ECapability_None;
       
   191 			}
       
   192 		}
       
   193 	return capability;
       
   194 	}
       
   195 	
       
   196 /**
       
   197  Get the capability id (enum value) for the capability name 
       
   198  given as string.
       
   199  @param aCapability - Capability name 
       
   200  @param aCapabilityValue - value for the capability name
       
   201  @return TInt - error codes
       
   202  @leave - None
       
   203 */
       
   204 TInt CTestPlatSecSetCapabilities::GetCapability(TPtrC aCapability, TCapability& aCapabilityValue)
       
   205 	{
       
   206 	INFO_PRINTF2(_L("The capability get is %S"), &aCapability);
       
   207 	TInt	ret=KErrNone;
       
   208 
       
   209 	TBuf<KMaxTestExecuteCommandLength>	capCaseValue(aCapability);
       
   210 	capCaseValue.LowerCase();
       
   211 
       
   212 	if(!capCaseValue.Compare(_L("tcb" )))
       
   213 		{
       
   214 		aCapabilityValue=ECapabilityTCB;
       
   215 		}
       
   216 	else if(!capCaseValue.Compare(_L("commdd" )))
       
   217 		{
       
   218 		aCapabilityValue=ECapabilityCommDD;
       
   219 		}
       
   220 	else if(!capCaseValue.Compare(_L("powermgmt" )))
       
   221 		{
       
   222 		aCapabilityValue=ECapabilityPowerMgmt;
       
   223 		}
       
   224 	else if(!capCaseValue.Compare(_L("multimediadd" )))
       
   225 		{
       
   226 		aCapabilityValue=ECapabilityMultimediaDD;
       
   227 		}
       
   228 	else if(!capCaseValue.Compare(_L("readdevicedata" )))
       
   229 		{
       
   230 		aCapabilityValue=ECapabilityReadDeviceData;
       
   231 		}
       
   232 	else if(!capCaseValue.Compare(_L("writedevicedata" )))
       
   233 		{
       
   234 		aCapabilityValue=ECapabilityWriteDeviceData;
       
   235 		}
       
   236 	else if(!capCaseValue.Compare(_L("drm" )))
       
   237 		{
       
   238 		aCapabilityValue=ECapabilityDRM;
       
   239 		}
       
   240 	else if(!capCaseValue.Compare(_L("trustedui" )))
       
   241 		{
       
   242 		aCapabilityValue=ECapabilityTrustedUI;
       
   243 		}
       
   244 	else if(!capCaseValue.Compare(_L("protserv" )))
       
   245 		{
       
   246 		aCapabilityValue=ECapabilityProtServ;
       
   247 		}
       
   248 	else if(!capCaseValue.Compare(_L("diskadmin" )))
       
   249 		{
       
   250 		aCapabilityValue=ECapabilityDiskAdmin;
       
   251 		}
       
   252 	else if(!capCaseValue.Compare(_L("networkcontrol" )))
       
   253 		{
       
   254 		aCapabilityValue=ECapabilityNetworkControl;
       
   255 		}
       
   256 	else if(!capCaseValue.Compare(_L("allfiles" )))
       
   257 		{
       
   258 		aCapabilityValue=ECapabilityAllFiles;
       
   259 		}
       
   260 	else if(!capCaseValue.Compare(_L("swevent" )))
       
   261 		{
       
   262 		aCapabilityValue=ECapabilitySwEvent;
       
   263 		}
       
   264 	else if(!capCaseValue.Compare(_L("networkservices" )))
       
   265 		{
       
   266 		aCapabilityValue=ECapabilityNetworkServices;
       
   267 		}
       
   268 	else if(!capCaseValue.Compare(_L("localservices" )))
       
   269 		{
       
   270 		aCapabilityValue=ECapabilityLocalServices;
       
   271 		}
       
   272 	else if(!capCaseValue.Compare(_L("readuserdata" )))
       
   273 		{
       
   274 		aCapabilityValue=ECapabilityReadUserData;
       
   275 		}
       
   276 	else if(!capCaseValue.Compare(_L("writeuserdata")))
       
   277 		{
       
   278 		aCapabilityValue=ECapabilityWriteUserData;
       
   279 		}
       
   280 	else if(!capCaseValue.Compare(_L("location")))
       
   281 		{
       
   282 		aCapabilityValue=ECapabilityLocation;
       
   283 		}
       
   284 	else if(!capCaseValue.Compare(_L("surroundingsdd")))
       
   285 		{
       
   286 		aCapabilityValue=ECapabilitySurroundingsDD;
       
   287 		}
       
   288 	else if(!capCaseValue.Compare(_L("userenvironment")))
       
   289 		{
       
   290 		aCapabilityValue=ECapabilityUserEnvironment;
       
   291 		} 		
       
   292 	else if(!capCaseValue.Compare(_L("none")))
       
   293 		{
       
   294 		aCapabilityValue=ECapability_None;
       
   295 		}
       
   296 	else
       
   297 		{
       
   298 		INFO_PRINTF2(_L("Unrecognised capability %S, will be rejected"), &capCaseValue);
       
   299 		ret=KErrNotFound;
       
   300 		}
       
   301 
       
   302 	return ret;
       
   303 	}