os/security/authorisation/userpromptservice/policies/test/tupspolicies/source/tpolicyreader.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/authorisation/userpromptservice/policies/test/tupspolicies/source/tpolicyreader.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,284 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "tpolicyreader.h"
    1.23 +
    1.24 +#include <ecom/ecom.h>
    1.25 +#include "policylist.h"
    1.26 +#include "cleanuputils.h"
    1.27 +#include "util.h"
    1.28 +
    1.29 +using namespace UserPromptService;
    1.30 +
    1.31 +void CTestPolicyReader::SetupL()
    1.32 +/**
    1.33 +Initialise policy reader and related resources.
    1.34 + */
    1.35 +	{
    1.36 +	__UHEAP_MARK;
    1.37 +	User::LeaveIfError(iFs.Connect());
    1.38 +	}
    1.39 +
    1.40 +void CTestPolicyReader::TearDownL()
    1.41 +/**
    1.42 +Free resources
    1.43 + */
    1.44 +	{
    1.45 +	delete iPolicyReader;
    1.46 +	iSidList.Close();
    1.47 +	iFs.Close();
    1.48 +	__UHEAP_MARKEND;
    1.49 +	}
    1.50 +
    1.51 +void CTestPolicyReader::TestPolicyReaderL()
    1.52 +/**
    1.53 +Reads and verifies every entry in a policy file.
    1.54 + */
    1.55 +	{
    1.56 +	TBool passed(ETrue);
    1.57 +	TBool res;
    1.58 +	TInt err;
    1.59 +	
    1.60 +	TPtrC policyFile;
    1.61 +	res = iConfig.GetString(_L("policies"), _L("policyfile"), policyFile);
    1.62 +	ASSERT_TRUE(res);
    1.63 +	
    1.64 +	// Test policy list error handling
    1.65 +	iExpectedError = KErrNone;	
    1.66 +	res = iConfig.GetInt(_L("policies"), _L("expectedpolicylisterror"), iExpectedError);	
    1.67 +	TRAP(err, TestPolicyListL(policyFile));
    1.68 +	ASSERT_TRUE(CheckExpectedError(Logger(), iExpectedError, err));
    1.69 +	// Skip rest of test if the CPolicyList is expected to fail.
    1.70 +	if (iExpectedError != KErrNone) return;
    1.71 +	
    1.72 +	TRAP(err, CreatePolicyReaderL(policyFile));
    1.73 +	if (err != KErrNone)
    1.74 +		{
    1.75 +		ERR_PRINTF2(_L("Error %d creating policy reader"), err);
    1.76 +		passed = EFalse;
    1.77 +		}
    1.78 +	else
    1.79 +		{				
    1.80 +		TInt numPolicies(-1);
    1.81 +		res = iConfig.GetInt(_L("policies"),_L("numpolicies"), numPolicies);
    1.82 +		ASSERT_TRUE(res);
    1.83 +		ASSERT_TRUE(numPolicies >= 0);
    1.84 +
    1.85 +		for (TInt i = 0; i < numPolicies; ++i)
    1.86 +			{
    1.87 +			TBuf<16> section(_L("policy"));
    1.88 +			section.AppendNum(i);
    1.89 +			INFO_PRINTF2(_L("Reading policy %d"), i);			
    1.90 +			TRAP(err, passed &= CheckPolicyL(section));
    1.91 +			passed &= CheckExpectedError(Logger(), iExpectedError, err);			
    1.92 +			}
    1.93 +		}
    1.94 +	if (!passed) User::Leave(KErrTEFUnitFail);
    1.95 +	}
    1.96 +
    1.97 +void CTestPolicyReader::TestPolicyListL(const TDesC& aPolicyFile)
    1.98 +	{
    1.99 +	INFO_PRINTF2(_L("Creating a CPolicyList from %S"), &aPolicyFile);
   1.100 +	CPolicyReader* r = CPolicyReader::NewL(iFs, aPolicyFile);
   1.101 +	CleanupStack::PushL(r);
   1.102 +	CPolicyList::TId id;
   1.103 +	CPolicyList* policyList = CPolicyList::NewL(id, *r);
   1.104 +	delete policyList;
   1.105 +	CleanupStack::PopAndDestroy(r);
   1.106 +	}
   1.107 +
   1.108 +void CTestPolicyReader::CreatePolicyReaderL(const TDesC& aPolicyFile)
   1.109 +	{
   1.110 +	iPolicyReader = CPolicyReader::NewL(iFs, aPolicyFile);
   1.111 +	}
   1.112 +
   1.113 +TBool CTestPolicyReader::CheckPolicyL(const TDesC& aSection)
   1.114 +	{
   1.115 +	TBool passed;
   1.116 +	ReadExpectedValuesL(aSection);
   1.117 +	CPolicy* p = iPolicyReader->NextPolicyL();
   1.118 +	if (! p)
   1.119 +		{
   1.120 +		ERR_PRINTF1(_L("Unexpected end of policy list"));
   1.121 +		User::Leave(KErrEof);
   1.122 +		}
   1.123 +	
   1.124 +	CleanupStack::PushL(p);
   1.125 +	passed = CompareExpectedValues(*p);
   1.126 +	CleanupStack::PopAndDestroy(p);
   1.127 +	return passed;
   1.128 +	}
   1.129 +
   1.130 +
   1.131 +CTestSuite* CTestPolicyReader::CreateSuiteL(const TDesC& aName)
   1.132 +/**
   1.133 +Creates the test suite for UPS policy reader
   1.134 +@param aName - Suite name
   1.135 +@return - Suite
   1.136 +*/
   1.137 +	{
   1.138 +	SUB_SUITE
   1.139 +	AddTestCase(lTestSuite, _L("TestPolicyReader"), &CTestPolicyReader::TestPolicyReaderL);
   1.140 +	END_SUITE
   1.141 +	}
   1.142 +
   1.143 +void CTestPolicyReader::ReadExpectedValuesL(const TDesC& aSection)
   1.144 +/**
   1.145 + Reads the expected values of the policy from section in the .ini file.
   1.146 + @param aPolicySection The name of the section containing the expected policy files.
   1.147 + */
   1.148 +	{
   1.149 +	TBool ret;
   1.150 +	
   1.151 +	iExpectedError = KErrNone;
   1.152 +	ret = iConfig.GetInt(aSection, _L("expectederror"), iExpectedError);
   1.153 +	if (ret) return; // If an error is expected then other config settings aren't required.
   1.154 +
   1.155 +	ret = iConfig.GetHex(aSection, _L("sidclasses"), iSidClasses);
   1.156 +	if (! ret) iSidClasses = 0x0000FFFF; 
   1.157 +
   1.158 +	iSidList.Reset();
   1.159 +	TInt numSids(0);
   1.160 +	ret = iConfig.GetInt(aSection, _L("numsids"), numSids);
   1.161 +	for (TInt i = 0; i < numSids; ++i)
   1.162 +		{
   1.163 +		TBuf<8> key;
   1.164 +		key.AppendFormat(_L("sid%d"), i);
   1.165 +
   1.166 +		TInt sid;
   1.167 +		ret = iConfig.GetHex(aSection, key, sid);
   1.168 +		ASSERT_TRUE(ret);
   1.169 +		iSidList.AppendL(sid);
   1.170 +		}
   1.171 +
   1.172 +	ASSERT_TRUE(iConfig.GetString(aSection, _L("destination"), iDestination));
   1.173 +	ASSERT_TRUE(iConfig.GetHex(aSection, _L("options"), iOptions));
   1.174 +	ASSERT_TRUE(iConfig.GetHex(aSection, _L("systemserversecurity"), iSystemServerSecurity));
   1.175 +
   1.176 +	TInt policyEvaluator;
   1.177 +	ASSERT_TRUE(iConfig.GetHex(aSection, _L("policyevaluator"), policyEvaluator));
   1.178 +	iPolicyEvaluator = TUid::Uid(policyEvaluator);
   1.179 +	
   1.180 +	TInt dialogCreator;
   1.181 +	ASSERT_TRUE(iConfig.GetHex(aSection, _L("dialogcreator"), dialogCreator));
   1.182 +	iDialogCreator = TUid::Uid(dialogCreator);
   1.183 +	ASSERT_TRUE(iConfig.GetHex(aSection, _L("flags"), iFlags));
   1.184 +
   1.185 +	ASSERT_TRUE(iConfig.GetInt(aSection, _L("majorversion"), iMajorVersion));
   1.186 +	ASSERT_TRUE(iConfig.GetInt(aSection, _L("minorversion"), iMinorVersion));
   1.187 +	ASSERT_TRUE(iConfig.GetBool(aSection, _L("default"), iDefault));
   1.188 +	}
   1.189 +
   1.190 +TBool CTestPolicyReader::CompareExpectedValues(const CPolicy& aPolicy)
   1.191 +/**
   1.192 + Compares the expected policy values from the ini file to the fields
   1.193 + in the policy object read from the resource file.
   1.194 + @param aPolicy The policy object to compare the ini value to.
   1.195 + @return ETrue, if the values match; otherwise, EFalse is returned.
   1.196 + */
   1.197 +	{
   1.198 +	TBool ret(ETrue);
   1.199 +
   1.200 +	if (static_cast<TUint16>(iSidClasses) != aPolicy.SidClasses().iSidClasses)
   1.201 +		{
   1.202 +		ERR_PRINTF3(_L("Expected SID classes 0x%04x, actual SID classes 0x%04x"),
   1.203 +				   iSidClasses, aPolicy.SidClasses().iSidClasses);
   1.204 +		ret = EFalse;
   1.205 +		} 
   1.206 +
   1.207 +	if (iSidList.Count() != aPolicy.SidList().Count())
   1.208 +		{
   1.209 +		ERR_PRINTF3(_L("Expected number of SIDs = %d, actual number of SIDs = %d"),
   1.210 +					iSidList.Count(), aPolicy.SidList().Count());
   1.211 +		ret = EFalse;
   1.212 +		}
   1.213 +		
   1.214 +	if (iSystemServerSecurity != aPolicy.SystemServerSecurity())
   1.215 +		{
   1.216 +		ERR_PRINTF3(_L("Expected systemserversecurity %d, actual systemserversecurity %d"),
   1.217 +			iSystemServerSecurity, aPolicy.SystemServerSecurity());
   1.218 +		ret = EFalse;
   1.219 +		}
   1.220 +
   1.221 +	for (TInt i = 0; i < Min(iSidList.Count(), aPolicy.SidList().Count()); ++i)
   1.222 +		{
   1.223 +		if (iSidList[i].iId != aPolicy.SidList()[i].iId)
   1.224 +			{
   1.225 +			ERR_PRINTF4(_L("Expected SID%d = 0x%08x, actual SID 0x%08x"),
   1.226 +						i, iSidList[i].iId, aPolicy.SidList()[i].iId);
   1.227 +			ret = EFalse;			
   1.228 +			}
   1.229 +		}
   1.230 +	
   1.231 +	if (iDestination != aPolicy.Destination())
   1.232 +		{
   1.233 +		ERR_PRINTF3(_L("Expected destination %S, actual destination %S"),
   1.234 +					&iDestination, &aPolicy.Destination());
   1.235 +		ret = EFalse;
   1.236 +		}
   1.237 +
   1.238 +	if (iOptions != aPolicy.Options())
   1.239 +		{
   1.240 +		ERR_PRINTF3(_L("Expected options = 0x%08x, actual options = 0x%08x"),
   1.241 +					&iOptions, aPolicy.Options());
   1.242 +		ret = EFalse;
   1.243 +		}
   1.244 +
   1.245 +	if (iPolicyEvaluator != aPolicy.PolicyEvaluator())
   1.246 +		{
   1.247 +		ERR_PRINTF3(_L("Expected policy evaluator 0x%08x, actual policy evaluator 0x%08x"),
   1.248 +					iPolicyEvaluator, aPolicy.PolicyEvaluator());
   1.249 +		ret = EFalse;
   1.250 +		}
   1.251 +
   1.252 +	if (iDialogCreator != aPolicy.DialogCreator())
   1.253 +		{
   1.254 +		ERR_PRINTF3(_L("Expected dialog creator 0x%08x, actualy dialog creator 0x%08x"),
   1.255 +					iDialogCreator, aPolicy.DialogCreator());
   1.256 +		ret = EFalse;
   1.257 +		}
   1.258 +
   1.259 +	if (iFlags != aPolicy.Flags())
   1.260 +		{
   1.261 +		ERR_PRINTF3(_L("Expected flags = 0x%04x, actual flags = 0x%04x"),
   1.262 +					iFlags, aPolicy.Flags());
   1.263 +		ret = EFalse;
   1.264 +		}
   1.265 +		
   1.266 +	if (iMajorVersion != aPolicy.MajorVersion())
   1.267 +		{
   1.268 +		ERR_PRINTF3(_L("Expected majorversion = %d, actual major version = %d"),
   1.269 +					iMajorVersion, aPolicy.MajorVersion());
   1.270 +		ret = EFalse;		
   1.271 +		}
   1.272 +
   1.273 +	if (iMinorVersion != aPolicy.MinorVersion())
   1.274 +		{
   1.275 +		ERR_PRINTF3(_L("Expected majorversion = %d, actual major version = %d"),
   1.276 +					iMinorVersion, aPolicy.MinorVersion());
   1.277 +		ret = EFalse;		
   1.278 +		}		
   1.279 +		
   1.280 +	if (iDefault != aPolicy.Default())
   1.281 +		{
   1.282 +		ERR_PRINTF3(_L("Expected default policy = %d, actual default policy= %d"),
   1.283 +					iDefault, aPolicy.Default());
   1.284 +		ret = EFalse;		
   1.285 +		}				
   1.286 +	return ret;
   1.287 +	}