os/kernelhwsrv/kerneltest/e32test/secure/t_shal.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2002-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 // e32test\secure\t_shal.cpp
    15 // Overview:
    16 // Test the security aspects of the HAL class.
    17 // API Information:
    18 // HAL
    19 // Details:
    20 // - For a variety of capability sets, get and set HAL hardware attributes
    21 // and check that the results are as expected.
    22 // - Set and restore the XYInputCalibration.
    23 // - Add an event using UserSvr::AddEvent() and verify the resulting capability
    24 // status.
    25 // Platforms/Drives/Compatibility:
    26 // All.
    27 // Assumptions/Requirement/Pre-requisites:
    28 // Failures and causes:
    29 // Base Port information:
    30 // 
    31 //
    32 
    33 #define __INCLUDE_CAPABILITY_NAMES__
    34 
    35 #include <e32test.h>
    36 #include <e32hal.h>
    37 #include <hal.h>
    38 #include <e32svr.h>
    39 
    40 LOCAL_D RTest test(_L("T_SHAL"));
    41 
    42 TCapabilitySet Capabilities;
    43 
    44 TInt PolicingVerified = 0;
    45 
    46 LOCAL_C TBool Check(TInt aResult,TCapability aCap)
    47 	{
    48 	switch(aResult)
    49 		{
    50 	case KErrNotSupported:
    51 		RDebug::Print(_L("  Not Supported"));
    52 		return ETrue;
    53 	case KErrNone:
    54 		RDebug::Print(_L("  No Error"));
    55 		break;
    56 	case KErrPermissionDenied:
    57 		RDebug::Print(_L("  Permission Denied"));
    58 		break;
    59 	default:
    60 		RDebug::Print(_L("  Error %d"),aResult);
    61 		break;
    62 		}
    63 
    64 	if(Capabilities.HasCapability(aCap))
    65 		return aResult==KErrNone;
    66 	else if(PlatSec::IsCapabilityEnforced(aCap))
    67 		return aResult==KErrPermissionDenied;
    68 	else
    69 		return aResult==KErrNone;
    70 	}
    71 
    72 LOCAL_C void GetSetCheck(const char* aText,HALData::TAttribute aAttribute,TCapability aCap)
    73 	{
    74 	TBuf8<256> text=(const TUint8*)"HAL::Set(";
    75 	text.Append((const TUint8*)aText,User::StringLength((const TUint8*)aText));
    76 	text.Append(')'); 
    77 	test.Next(text.Expand());
    78 	TInt x = 0;
    79 	HAL::Get(aAttribute,x);
    80 	TInt r = HAL::Set(aAttribute,x);
    81 	test(Check(r,aCap));
    82 	}
    83 
    84 #define SET_CHECK(a,c) 	GetSetCheck(#a,a,c);
    85 
    86 LOCAL_C TInt DoTests()
    87 	{
    88 	TInt r;
    89 //	TInt x = 0;
    90 
    91 	//
    92 	// ECapabilityReadDeviceData
    93 	//
    94 
    95 #if 0
    96 	test.Start(_L("UserHal::MachineInfo()"));
    97     TMachineInfoV2Buf info;
    98 	r = UserHal::MachineInfo(info);
    99 	test(Check(r,ECapabilityReadDeviceData));
   100 #endif
   101 
   102 	//
   103 	// ECapabilityWriteDeviceData
   104 	//
   105 
   106 	SET_CHECK(HAL::EKeyboardClickState,ECapabilityWriteDeviceData);
   107 	SET_CHECK(HAL::EKeyboardClickVolume,ECapabilityWriteDeviceData);
   108 	SET_CHECK(HAL::EPenClickState,ECapabilityWriteDeviceData);
   109 	SET_CHECK(HAL::EPenClickVolume,ECapabilityWriteDeviceData);
   110 	SET_CHECK(HAL::ELanguageIndex,ECapabilityWriteDeviceData);
   111 	SET_CHECK(HAL::EKeyboardIndex,ECapabilityWriteDeviceData);
   112 	SET_CHECK(HAL::ESystemDrive,ECapabilityWriteDeviceData);
   113 	SET_CHECK(HAL::ECaseSwitchDisplayOn,ECapabilityWriteDeviceData);
   114 	SET_CHECK(HAL::ECaseSwitchDisplayOff,ECapabilityWriteDeviceData);
   115 	SET_CHECK(HAL::EDisplayContrast,ECapabilityWriteDeviceData);
   116 	SET_CHECK(HAL::EDisplayBrightness,ECapabilityWriteDeviceData);
   117 	SET_CHECK(HAL::EBacklightState,ECapabilityWriteDeviceData);
   118 	SET_CHECK(HAL::EPenDisplayOn,ECapabilityWriteDeviceData);
   119 	SET_CHECK(HAL::ELocaleLoaded,ECapabilityWriteDeviceData);
   120 	SET_CHECK(HAL::ECustomResourceDrive,ECapabilityWriteDeviceData);
   121 
   122 	test.Next(_L("UserHal::CalibrationPoints()"));
   123     TDigitizerCalibration xy;
   124 	UserHal::CalibrationPoints(xy);
   125 	r = UserHal::SetXYInputCalibration(xy);
   126 	test(Check(r,ECapabilityWriteDeviceData));
   127 
   128 	test.Next(_L("UserHal::RestoreXYInputCalibration()"));
   129 	UserHal::SaveXYInputCalibration();
   130 	r = UserHal::RestoreXYInputCalibration(ESaved);
   131 	test(Check(r,ECapabilityWriteDeviceData));
   132 
   133 	//
   134 	// ECapabilityMultimediaDD
   135 	//
   136 
   137 	SET_CHECK(HAL::EMouseState,ECapabilityMultimediaDD);
   138 	SET_CHECK(HAL::EMouseSpeed,ECapabilityMultimediaDD);
   139 	SET_CHECK(HAL::EMouseAcceleration,ECapabilityMultimediaDD);
   140 //	SET_CHECK(HAL::EDisplayMode,ECapabilityMultimediaDD);
   141 //	SET_CHECK(HAL::EDisplayPaletteEntry,ECapabilityMultimediaDD);
   142 
   143 	//
   144 	// ECapabilityPowerMgmt
   145 	//
   146 
   147 	SET_CHECK(HAL::EKeyboardBacklightState,ECapabilityPowerMgmt);
   148 	SET_CHECK(HAL::EAccessoryPower,ECapabilityPowerMgmt);
   149 	SET_CHECK(HAL::EDisplayState,ECapabilityPowerMgmt);
   150 	SET_CHECK(HAL::EKeyboardState,ECapabilityPowerMgmt);
   151 	SET_CHECK(HAL::EPenState,ECapabilityPowerMgmt);
   152 /*
   153 	test.Next(_L("UserHal::SwitchOff()"));
   154 	RTimer timer;
   155 	TRequestStatus done;
   156 	timer.CreateLocal();
   157 	TTime wakeup;
   158 	wakeup.HomeTime();
   159 	wakeup+=TTimeIntervalSeconds(4);
   160 	timer.At(done,wakeup);
   161 	r = UserHal::SwitchOff(); // May not actually turn off due to imminent RTimer.At()
   162 	test(Check(r,ECapabilityPowerMgmt));
   163 	User::WaitForRequest(done);
   164 */
   165 	//
   166 	// ECapabilitySwEvent
   167 	//
   168 
   169 	test.Next(_L("UserSvr::AddEvent()"));
   170 	TRawEvent event;
   171 	r = UserSvr::AddEvent(event);
   172 	test(Check(r,ECapabilitySwEvent));
   173 
   174 	//
   175 
   176 	test.End();
   177 
   178 	return 0x55555555;
   179 	}
   180 
   181 
   182 enum TTestProcessFunctions
   183 	{
   184 	ETestProcessDoTests,
   185 	};
   186 
   187 #include "testprocess.h"
   188 
   189 
   190 
   191 GLDEF_C TInt E32Main()
   192     {
   193 	Capabilities = TSecurityInfo(RProcess()).iCaps;
   194 
   195 	test.Title();
   196 
   197 	if(User::CommandLineLength())
   198 		{
   199 		TBuf<128> message;
   200 		__ASSERT_COMPILE(ECapability_Limit<64);
   201 		message.AppendFormat(_L("Tests with capabilities %08x%08x"),((TUint32*)&Capabilities)[1],((TUint32*)&Capabilities)[0]);
   202 		test.Start(message);
   203 		TInt result = DoTests();
   204 		// Don't test.End() so we don't get lots of 'Success's in logs
   205 		return(result);
   206 		}
   207 
   208 	test.Title();
   209 	test.Start(_L("Start"));
   210 	TInt c;
   211 	for(c=0; c<1+ECapability_Limit; c++)
   212 		{
   213 		RTestProcess p;
   214 		TRequestStatus s;
   215 		TBuf<128> message;
   216 		TCapabilitySet caps;
   217 		caps.SetAllSupported();
   218 		if(!caps.HasCapability((TCapability)c))
   219 			continue;
   220 		caps.RemoveCapability((TCapability)c);
   221 		TBuf8<128> capNameBuf;
   222 		capNameBuf.Copy((const TUint8*)CapabilityNames[c]);
   223 		TPtr capName(capNameBuf.Expand());
   224 		message.AppendFormat(_L("Tests with all capabilities except %S"),&capName);
   225 		test.Next(message);
   226 		p.Create(*(TUint32*)&caps,ETestProcessDoTests);
   227 		p.Logon(s);
   228 		p.Resume();
   229 		User::WaitForRequest(s);
   230 		test(p.ExitType()==EExitKill);
   231 		TInt result=s.Int()^0x55555555;
   232 		test(result==0);
   233 		CLOSE_AND_WAIT(p);
   234 		}
   235 
   236 	// Show results requiring manual inspection
   237 	_LIT(KSeperatorText,"----------------------------------------------------------------------------\n"); 
   238 	test.Printf(_L("\n"));
   239 	test.Printf(_L("RESULTS\n")); 
   240 	test.Printf(KSeperatorText);
   241 	TInt verified=1;
   242 	for(c=0; c<ECapability_Limit; c++)
   243 		if(!PlatSec::IsCapabilityEnforced((TCapability)c))
   244 			verified = 0;
   245 
   246 	if(!verified)
   247 		test.Printf(_L("*  Did NOT verify security checking\n"));
   248 	else
   249 		test.Printf(_L("*  Verified security checking\n"));
   250 	test.Printf(KSeperatorText);
   251 
   252 	// Wait for a while, or for a key press
   253 	test.Printf(_L("Waiting a short while for key press...\n"));
   254 	TRequestStatus keyStat;
   255 	test.Console()->Read(keyStat);
   256 	RTimer timer;
   257 	test(timer.CreateLocal()==KErrNone);
   258 	TRequestStatus timerStat;
   259 	timer.After(timerStat,20*1000000);
   260 	User::WaitForRequest(timerStat,keyStat);
   261 	TInt key = 0;
   262 	if(keyStat!=KRequestPending)
   263 		key = test.Console()->KeyCode();
   264 	timer.Cancel();
   265 	test.Console()->ReadCancel();
   266 	User::WaitForAnyRequest();
   267 
   268 	test.End();
   269 	return(0);
   270     }
   271