1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericservices/taskscheduler/Test/PlatSec/platsectaskhandler.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,183 @@
1.4 +// Copyright (c) 2004-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 "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 +//
1.18 +
1.19 +#include <schinfo.h>
1.20 +#include <schinfointernal.h>
1.21 +#include <schtask.h>
1.22 +#include <s32file.h>
1.23 +#include <e32math.h>
1.24 +#include <e32cons.h>
1.25 +#include <e32test.h>
1.26 +
1.27 +#include "platsectaskcommon.h"
1.28 +
1.29 +
1.30 +// Constants
1.31 +_LIT(KConsoleName, "PlatSecTaskHandler");
1.32 +
1.33 +static void SignalTestExe()
1.34 + {
1.35 + _LIT(KSchSemaphoreName, "SCHMinimalTaskHandler");
1.36 + RSemaphore sem;
1.37 + TInt ret = sem.OpenGlobal(KSchSemaphoreName);
1.38 + if (ret == KErrNone)
1.39 + {
1.40 + sem.Signal();
1.41 + sem.Close();
1.42 + }
1.43 + }
1.44 +
1.45 +//***********************************************************************************
1.46 +LOCAL_C TInt GetRandomNumber(const TInt aLow, const TInt aHigh, TInt64& aSeed)
1.47 + {
1.48 + TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
1.49 + TInt32 rand;
1.50 +
1.51 + // Round to 0 decimal places, ie. the nearest whole numer
1.52 + Math::Round(initialRand, initialRand, 0);
1.53 + Math::Int(rand, initialRand);
1.54 +
1.55 + return (aLow + rand);
1.56 + }
1.57 +
1.58 +//***********************************************************************************
1.59 +LOCAL_C void DestroyArray(TAny* aArray)
1.60 + {
1.61 + static_cast<RArray<TCapability>*>(aArray)->Reset();
1.62 + }
1.63 +/**
1.64 +@SYMTestCaseID SYSLIB-SCHSVR-CT-1345
1.65 +@SYMTestCaseDesc Platform security Task handler test
1.66 +@SYMTestPriority High
1.67 +@SYMTestActions Attempt to test the contents of a task file.
1.68 +@SYMTestExpectedResults Test must not fail
1.69 +@SYMREQ REQ0000
1.70 +*/
1.71 +LOCAL_C void ConstructConsoleL(RFile& aTaskFile, RTest& aTest)
1.72 + {
1.73 +
1.74 + CConsoleBase* console=Console::NewL(KConsoleName, TSize(KConsFullScreen, KConsFullScreen));
1.75 + CleanupStack::PushL(console);
1.76 + console->Printf(_L(" contents of task file\n"));
1.77 +
1.78 + RArray<TCapability> enforcedCaps;
1.79 + CleanupStack::PushL(TCleanupItem(&DestroyArray,&enforcedCaps));
1.80 +
1.81 + for (TInt i=0; i<ECapability_Limit; i++)
1.82 + {
1.83 + // we're checking here to see which capabilities are enforced, skipping
1.84 + // ECapabilityMultimediaDD and ECapabilityWriteDeviceData as it is
1.85 + // needed to write persistent schedules upon which this test relies.
1.86 +
1.87 + if (i == KPlatSecTestCapability)
1.88 + continue;
1.89 + else if (i == ECapabilityWriteDeviceData)
1.90 + continue;
1.91 + else if (PlatSec::IsCapabilityEnforced((TCapability)i))
1.92 + enforcedCaps.Append((TCapability)i);
1.93 + }
1.94 +
1.95 + TBool isTestCapabilityEnforced = PlatSec::IsCapabilityEnforced(KPlatSecTestCapability);
1.96 +
1.97 + CFileStore* store;
1.98 + store = CDirectFileStore::FromLC(aTaskFile);
1.99 +
1.100 + RStoreReadStream instream;
1.101 + instream.OpenLC(*store,store->Root());
1.102 +
1.103 + TInt count = instream.ReadInt32L();
1.104 + for (TInt i=0;i<count;i++)
1.105 + {
1.106 + CScheduledTask* task = CScheduledTask::NewLC(instream);
1.107 +
1.108 + aTest(task->Info().iName.CompareF(KPlatSecTaskName)==0);
1.109 + aTest(task->Data().CompareF(KPlatSecTaskData)==0);
1.110 + aTest(task->SecurityInfo().iSecureId==KPlatSecTestSid); //This is the SID of tschsvrplatsec
1.111 +
1.112 + // check that client has capability it should have
1.113 + if(isTestCapabilityEnforced)
1.114 + aTest(task->SecurityInfo().iCaps.HasCapability(KPlatSecTestCapability));
1.115 +
1.116 + // check that the client has ECapabilityWriteDeviceData
1.117 + aTest(task->SecurityInfo().iCaps.HasCapability(ECapabilityWriteDeviceData));
1.118 +
1.119 + // check that client doesn't have any other capabilities
1.120 + for (TInt j=enforcedCaps.Count()-1; j>=0; --j)
1.121 + {
1.122 + aTest(!task->SecurityInfo().iCaps.HasCapability(enforcedCaps[j]));
1.123 + }
1.124 +
1.125 +
1.126 + CleanupStack::PopAndDestroy(task);
1.127 + }
1.128 +
1.129 + console->Printf(_L("Pausing for a one second..."));
1.130 + User::After(1000000);
1.131 +
1.132 + CleanupStack::PopAndDestroy(4); // instream, store, enforcedCaps, console
1.133 + }
1.134 +
1.135 +
1.136 +//***********************************************************************************
1.137 +LOCAL_C TInt Execute()
1.138 + {
1.139 + TInt err = KErrNoMemory;
1.140 + CTrapCleanup* cleanup=CTrapCleanup::New(); //can fail
1.141 + if (cleanup)
1.142 + {
1.143 + _LIT(KPlatSecTestName, "PlatSectTaskHandlerTest");
1.144 + RTest theTest(KPlatSecTestName);
1.145 + theTest.Start(KPlatSecTestName);
1.146 + theTest.Title();
1.147 +
1.148 + RFile file;
1.149 +
1.150 + // Adopt the task file from the Task Scheduler
1.151 + err = file.AdoptFromCreator(TScheduledTaskFile::FsHandleIndex(),
1.152 + TScheduledTaskFile::FileHandleIndex());
1.153 + if (err != KErrNone)
1.154 + return err;
1.155 +
1.156 + // The aParam is the name of a file where the relevant CTaskExCmdLine is
1.157 + // do the executing
1.158 + theTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1345 "));
1.159 + TRAPD(err, ConstructConsoleL(file, theTest));
1.160 + if(err == KErrNone)
1.161 + {
1.162 + // Sometimes we want to return a bogus error value,
1.163 + // sometimes we don't.
1.164 + TTime now;
1.165 + now.HomeTime();
1.166 + TInt64 seed = now.Int64();
1.167 + err = GetRandomNumber(-50, 200, seed); //20% chance of error being returned
1.168 + }
1.169 +
1.170 + file.Close();// Close the file
1.171 +
1.172 + theTest.End();
1.173 + theTest.Close();
1.174 +
1.175 + delete cleanup;
1.176 + }
1.177 + SignalTestExe();
1.178 + return err;
1.179 + }
1.180 +
1.181 +
1.182 +//***********************************************************************************
1.183 +GLDEF_C TInt E32Main()
1.184 + {
1.185 + return Execute();
1.186 + }