1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/secure/t_shal.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,271 @@
1.4 +// Copyright (c) 2002-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 +// e32test\secure\t_shal.cpp
1.18 +// Overview:
1.19 +// Test the security aspects of the HAL class.
1.20 +// API Information:
1.21 +// HAL
1.22 +// Details:
1.23 +// - For a variety of capability sets, get and set HAL hardware attributes
1.24 +// and check that the results are as expected.
1.25 +// - Set and restore the XYInputCalibration.
1.26 +// - Add an event using UserSvr::AddEvent() and verify the resulting capability
1.27 +// status.
1.28 +// Platforms/Drives/Compatibility:
1.29 +// All.
1.30 +// Assumptions/Requirement/Pre-requisites:
1.31 +// Failures and causes:
1.32 +// Base Port information:
1.33 +//
1.34 +//
1.35 +
1.36 +#define __INCLUDE_CAPABILITY_NAMES__
1.37 +
1.38 +#include <e32test.h>
1.39 +#include <e32hal.h>
1.40 +#include <hal.h>
1.41 +#include <e32svr.h>
1.42 +
1.43 +LOCAL_D RTest test(_L("T_SHAL"));
1.44 +
1.45 +TCapabilitySet Capabilities;
1.46 +
1.47 +TInt PolicingVerified = 0;
1.48 +
1.49 +LOCAL_C TBool Check(TInt aResult,TCapability aCap)
1.50 + {
1.51 + switch(aResult)
1.52 + {
1.53 + case KErrNotSupported:
1.54 + RDebug::Print(_L(" Not Supported"));
1.55 + return ETrue;
1.56 + case KErrNone:
1.57 + RDebug::Print(_L(" No Error"));
1.58 + break;
1.59 + case KErrPermissionDenied:
1.60 + RDebug::Print(_L(" Permission Denied"));
1.61 + break;
1.62 + default:
1.63 + RDebug::Print(_L(" Error %d"),aResult);
1.64 + break;
1.65 + }
1.66 +
1.67 + if(Capabilities.HasCapability(aCap))
1.68 + return aResult==KErrNone;
1.69 + else if(PlatSec::IsCapabilityEnforced(aCap))
1.70 + return aResult==KErrPermissionDenied;
1.71 + else
1.72 + return aResult==KErrNone;
1.73 + }
1.74 +
1.75 +LOCAL_C void GetSetCheck(const char* aText,HALData::TAttribute aAttribute,TCapability aCap)
1.76 + {
1.77 + TBuf8<256> text=(const TUint8*)"HAL::Set(";
1.78 + text.Append((const TUint8*)aText,User::StringLength((const TUint8*)aText));
1.79 + text.Append(')');
1.80 + test.Next(text.Expand());
1.81 + TInt x = 0;
1.82 + HAL::Get(aAttribute,x);
1.83 + TInt r = HAL::Set(aAttribute,x);
1.84 + test(Check(r,aCap));
1.85 + }
1.86 +
1.87 +#define SET_CHECK(a,c) GetSetCheck(#a,a,c);
1.88 +
1.89 +LOCAL_C TInt DoTests()
1.90 + {
1.91 + TInt r;
1.92 +// TInt x = 0;
1.93 +
1.94 + //
1.95 + // ECapabilityReadDeviceData
1.96 + //
1.97 +
1.98 +#if 0
1.99 + test.Start(_L("UserHal::MachineInfo()"));
1.100 + TMachineInfoV2Buf info;
1.101 + r = UserHal::MachineInfo(info);
1.102 + test(Check(r,ECapabilityReadDeviceData));
1.103 +#endif
1.104 +
1.105 + //
1.106 + // ECapabilityWriteDeviceData
1.107 + //
1.108 +
1.109 + SET_CHECK(HAL::EKeyboardClickState,ECapabilityWriteDeviceData);
1.110 + SET_CHECK(HAL::EKeyboardClickVolume,ECapabilityWriteDeviceData);
1.111 + SET_CHECK(HAL::EPenClickState,ECapabilityWriteDeviceData);
1.112 + SET_CHECK(HAL::EPenClickVolume,ECapabilityWriteDeviceData);
1.113 + SET_CHECK(HAL::ELanguageIndex,ECapabilityWriteDeviceData);
1.114 + SET_CHECK(HAL::EKeyboardIndex,ECapabilityWriteDeviceData);
1.115 + SET_CHECK(HAL::ESystemDrive,ECapabilityWriteDeviceData);
1.116 + SET_CHECK(HAL::ECaseSwitchDisplayOn,ECapabilityWriteDeviceData);
1.117 + SET_CHECK(HAL::ECaseSwitchDisplayOff,ECapabilityWriteDeviceData);
1.118 + SET_CHECK(HAL::EDisplayContrast,ECapabilityWriteDeviceData);
1.119 + SET_CHECK(HAL::EDisplayBrightness,ECapabilityWriteDeviceData);
1.120 + SET_CHECK(HAL::EBacklightState,ECapabilityWriteDeviceData);
1.121 + SET_CHECK(HAL::EPenDisplayOn,ECapabilityWriteDeviceData);
1.122 + SET_CHECK(HAL::ELocaleLoaded,ECapabilityWriteDeviceData);
1.123 + SET_CHECK(HAL::ECustomResourceDrive,ECapabilityWriteDeviceData);
1.124 +
1.125 + test.Next(_L("UserHal::CalibrationPoints()"));
1.126 + TDigitizerCalibration xy;
1.127 + UserHal::CalibrationPoints(xy);
1.128 + r = UserHal::SetXYInputCalibration(xy);
1.129 + test(Check(r,ECapabilityWriteDeviceData));
1.130 +
1.131 + test.Next(_L("UserHal::RestoreXYInputCalibration()"));
1.132 + UserHal::SaveXYInputCalibration();
1.133 + r = UserHal::RestoreXYInputCalibration(ESaved);
1.134 + test(Check(r,ECapabilityWriteDeviceData));
1.135 +
1.136 + //
1.137 + // ECapabilityMultimediaDD
1.138 + //
1.139 +
1.140 + SET_CHECK(HAL::EMouseState,ECapabilityMultimediaDD);
1.141 + SET_CHECK(HAL::EMouseSpeed,ECapabilityMultimediaDD);
1.142 + SET_CHECK(HAL::EMouseAcceleration,ECapabilityMultimediaDD);
1.143 +// SET_CHECK(HAL::EDisplayMode,ECapabilityMultimediaDD);
1.144 +// SET_CHECK(HAL::EDisplayPaletteEntry,ECapabilityMultimediaDD);
1.145 +
1.146 + //
1.147 + // ECapabilityPowerMgmt
1.148 + //
1.149 +
1.150 + SET_CHECK(HAL::EKeyboardBacklightState,ECapabilityPowerMgmt);
1.151 + SET_CHECK(HAL::EAccessoryPower,ECapabilityPowerMgmt);
1.152 + SET_CHECK(HAL::EDisplayState,ECapabilityPowerMgmt);
1.153 + SET_CHECK(HAL::EKeyboardState,ECapabilityPowerMgmt);
1.154 + SET_CHECK(HAL::EPenState,ECapabilityPowerMgmt);
1.155 +/*
1.156 + test.Next(_L("UserHal::SwitchOff()"));
1.157 + RTimer timer;
1.158 + TRequestStatus done;
1.159 + timer.CreateLocal();
1.160 + TTime wakeup;
1.161 + wakeup.HomeTime();
1.162 + wakeup+=TTimeIntervalSeconds(4);
1.163 + timer.At(done,wakeup);
1.164 + r = UserHal::SwitchOff(); // May not actually turn off due to imminent RTimer.At()
1.165 + test(Check(r,ECapabilityPowerMgmt));
1.166 + User::WaitForRequest(done);
1.167 +*/
1.168 + //
1.169 + // ECapabilitySwEvent
1.170 + //
1.171 +
1.172 + test.Next(_L("UserSvr::AddEvent()"));
1.173 + TRawEvent event;
1.174 + r = UserSvr::AddEvent(event);
1.175 + test(Check(r,ECapabilitySwEvent));
1.176 +
1.177 + //
1.178 +
1.179 + test.End();
1.180 +
1.181 + return 0x55555555;
1.182 + }
1.183 +
1.184 +
1.185 +enum TTestProcessFunctions
1.186 + {
1.187 + ETestProcessDoTests,
1.188 + };
1.189 +
1.190 +#include "testprocess.h"
1.191 +
1.192 +
1.193 +
1.194 +GLDEF_C TInt E32Main()
1.195 + {
1.196 + Capabilities = TSecurityInfo(RProcess()).iCaps;
1.197 +
1.198 + test.Title();
1.199 +
1.200 + if(User::CommandLineLength())
1.201 + {
1.202 + TBuf<128> message;
1.203 + __ASSERT_COMPILE(ECapability_Limit<64);
1.204 + message.AppendFormat(_L("Tests with capabilities %08x%08x"),((TUint32*)&Capabilities)[1],((TUint32*)&Capabilities)[0]);
1.205 + test.Start(message);
1.206 + TInt result = DoTests();
1.207 + // Don't test.End() so we don't get lots of 'Success's in logs
1.208 + return(result);
1.209 + }
1.210 +
1.211 + test.Title();
1.212 + test.Start(_L("Start"));
1.213 + TInt c;
1.214 + for(c=0; c<1+ECapability_Limit; c++)
1.215 + {
1.216 + RTestProcess p;
1.217 + TRequestStatus s;
1.218 + TBuf<128> message;
1.219 + TCapabilitySet caps;
1.220 + caps.SetAllSupported();
1.221 + if(!caps.HasCapability((TCapability)c))
1.222 + continue;
1.223 + caps.RemoveCapability((TCapability)c);
1.224 + TBuf8<128> capNameBuf;
1.225 + capNameBuf.Copy((const TUint8*)CapabilityNames[c]);
1.226 + TPtr capName(capNameBuf.Expand());
1.227 + message.AppendFormat(_L("Tests with all capabilities except %S"),&capName);
1.228 + test.Next(message);
1.229 + p.Create(*(TUint32*)&caps,ETestProcessDoTests);
1.230 + p.Logon(s);
1.231 + p.Resume();
1.232 + User::WaitForRequest(s);
1.233 + test(p.ExitType()==EExitKill);
1.234 + TInt result=s.Int()^0x55555555;
1.235 + test(result==0);
1.236 + CLOSE_AND_WAIT(p);
1.237 + }
1.238 +
1.239 + // Show results requiring manual inspection
1.240 + _LIT(KSeperatorText,"----------------------------------------------------------------------------\n");
1.241 + test.Printf(_L("\n"));
1.242 + test.Printf(_L("RESULTS\n"));
1.243 + test.Printf(KSeperatorText);
1.244 + TInt verified=1;
1.245 + for(c=0; c<ECapability_Limit; c++)
1.246 + if(!PlatSec::IsCapabilityEnforced((TCapability)c))
1.247 + verified = 0;
1.248 +
1.249 + if(!verified)
1.250 + test.Printf(_L("* Did NOT verify security checking\n"));
1.251 + else
1.252 + test.Printf(_L("* Verified security checking\n"));
1.253 + test.Printf(KSeperatorText);
1.254 +
1.255 + // Wait for a while, or for a key press
1.256 + test.Printf(_L("Waiting a short while for key press...\n"));
1.257 + TRequestStatus keyStat;
1.258 + test.Console()->Read(keyStat);
1.259 + RTimer timer;
1.260 + test(timer.CreateLocal()==KErrNone);
1.261 + TRequestStatus timerStat;
1.262 + timer.After(timerStat,20*1000000);
1.263 + User::WaitForRequest(timerStat,keyStat);
1.264 + TInt key = 0;
1.265 + if(keyStat!=KRequestPending)
1.266 + key = test.Console()->KeyCode();
1.267 + timer.Cancel();
1.268 + test.Console()->ReadCancel();
1.269 + User::WaitForAnyRequest();
1.270 +
1.271 + test.End();
1.272 + return(0);
1.273 + }
1.274 +