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