os/security/authorisation/userpromptservice/policies/test/tupspolicies/source/tpolicyreader.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
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "tpolicyreader.h"
sl@0
    20
sl@0
    21
#include <ecom/ecom.h>
sl@0
    22
#include "policylist.h"
sl@0
    23
#include "cleanuputils.h"
sl@0
    24
#include "util.h"
sl@0
    25
sl@0
    26
using namespace UserPromptService;
sl@0
    27
sl@0
    28
void CTestPolicyReader::SetupL()
sl@0
    29
/**
sl@0
    30
Initialise policy reader and related resources.
sl@0
    31
 */
sl@0
    32
	{
sl@0
    33
	__UHEAP_MARK;
sl@0
    34
	User::LeaveIfError(iFs.Connect());
sl@0
    35
	}
sl@0
    36
sl@0
    37
void CTestPolicyReader::TearDownL()
sl@0
    38
/**
sl@0
    39
Free resources
sl@0
    40
 */
sl@0
    41
	{
sl@0
    42
	delete iPolicyReader;
sl@0
    43
	iSidList.Close();
sl@0
    44
	iFs.Close();
sl@0
    45
	__UHEAP_MARKEND;
sl@0
    46
	}
sl@0
    47
sl@0
    48
void CTestPolicyReader::TestPolicyReaderL()
sl@0
    49
/**
sl@0
    50
Reads and verifies every entry in a policy file.
sl@0
    51
 */
sl@0
    52
	{
sl@0
    53
	TBool passed(ETrue);
sl@0
    54
	TBool res;
sl@0
    55
	TInt err;
sl@0
    56
	
sl@0
    57
	TPtrC policyFile;
sl@0
    58
	res = iConfig.GetString(_L("policies"), _L("policyfile"), policyFile);
sl@0
    59
	ASSERT_TRUE(res);
sl@0
    60
	
sl@0
    61
	// Test policy list error handling
sl@0
    62
	iExpectedError = KErrNone;	
sl@0
    63
	res = iConfig.GetInt(_L("policies"), _L("expectedpolicylisterror"), iExpectedError);	
sl@0
    64
	TRAP(err, TestPolicyListL(policyFile));
sl@0
    65
	ASSERT_TRUE(CheckExpectedError(Logger(), iExpectedError, err));
sl@0
    66
	// Skip rest of test if the CPolicyList is expected to fail.
sl@0
    67
	if (iExpectedError != KErrNone) return;
sl@0
    68
	
sl@0
    69
	TRAP(err, CreatePolicyReaderL(policyFile));
sl@0
    70
	if (err != KErrNone)
sl@0
    71
		{
sl@0
    72
		ERR_PRINTF2(_L("Error %d creating policy reader"), err);
sl@0
    73
		passed = EFalse;
sl@0
    74
		}
sl@0
    75
	else
sl@0
    76
		{				
sl@0
    77
		TInt numPolicies(-1);
sl@0
    78
		res = iConfig.GetInt(_L("policies"),_L("numpolicies"), numPolicies);
sl@0
    79
		ASSERT_TRUE(res);
sl@0
    80
		ASSERT_TRUE(numPolicies >= 0);
sl@0
    81
sl@0
    82
		for (TInt i = 0; i < numPolicies; ++i)
sl@0
    83
			{
sl@0
    84
			TBuf<16> section(_L("policy"));
sl@0
    85
			section.AppendNum(i);
sl@0
    86
			INFO_PRINTF2(_L("Reading policy %d"), i);			
sl@0
    87
			TRAP(err, passed &= CheckPolicyL(section));
sl@0
    88
			passed &= CheckExpectedError(Logger(), iExpectedError, err);			
sl@0
    89
			}
sl@0
    90
		}
sl@0
    91
	if (!passed) User::Leave(KErrTEFUnitFail);
sl@0
    92
	}
sl@0
    93
sl@0
    94
void CTestPolicyReader::TestPolicyListL(const TDesC& aPolicyFile)
sl@0
    95
	{
sl@0
    96
	INFO_PRINTF2(_L("Creating a CPolicyList from %S"), &aPolicyFile);
sl@0
    97
	CPolicyReader* r = CPolicyReader::NewL(iFs, aPolicyFile);
sl@0
    98
	CleanupStack::PushL(r);
sl@0
    99
	CPolicyList::TId id;
sl@0
   100
	CPolicyList* policyList = CPolicyList::NewL(id, *r);
sl@0
   101
	delete policyList;
sl@0
   102
	CleanupStack::PopAndDestroy(r);
sl@0
   103
	}
sl@0
   104
sl@0
   105
void CTestPolicyReader::CreatePolicyReaderL(const TDesC& aPolicyFile)
sl@0
   106
	{
sl@0
   107
	iPolicyReader = CPolicyReader::NewL(iFs, aPolicyFile);
sl@0
   108
	}
sl@0
   109
sl@0
   110
TBool CTestPolicyReader::CheckPolicyL(const TDesC& aSection)
sl@0
   111
	{
sl@0
   112
	TBool passed;
sl@0
   113
	ReadExpectedValuesL(aSection);
sl@0
   114
	CPolicy* p = iPolicyReader->NextPolicyL();
sl@0
   115
	if (! p)
sl@0
   116
		{
sl@0
   117
		ERR_PRINTF1(_L("Unexpected end of policy list"));
sl@0
   118
		User::Leave(KErrEof);
sl@0
   119
		}
sl@0
   120
	
sl@0
   121
	CleanupStack::PushL(p);
sl@0
   122
	passed = CompareExpectedValues(*p);
sl@0
   123
	CleanupStack::PopAndDestroy(p);
sl@0
   124
	return passed;
sl@0
   125
	}
sl@0
   126
sl@0
   127
sl@0
   128
CTestSuite* CTestPolicyReader::CreateSuiteL(const TDesC& aName)
sl@0
   129
/**
sl@0
   130
Creates the test suite for UPS policy reader
sl@0
   131
@param aName - Suite name
sl@0
   132
@return - Suite
sl@0
   133
*/
sl@0
   134
	{
sl@0
   135
	SUB_SUITE
sl@0
   136
	AddTestCase(lTestSuite, _L("TestPolicyReader"), &CTestPolicyReader::TestPolicyReaderL);
sl@0
   137
	END_SUITE
sl@0
   138
	}
sl@0
   139
sl@0
   140
void CTestPolicyReader::ReadExpectedValuesL(const TDesC& aSection)
sl@0
   141
/**
sl@0
   142
 Reads the expected values of the policy from section in the .ini file.
sl@0
   143
 @param aPolicySection The name of the section containing the expected policy files.
sl@0
   144
 */
sl@0
   145
	{
sl@0
   146
	TBool ret;
sl@0
   147
	
sl@0
   148
	iExpectedError = KErrNone;
sl@0
   149
	ret = iConfig.GetInt(aSection, _L("expectederror"), iExpectedError);
sl@0
   150
	if (ret) return; // If an error is expected then other config settings aren't required.
sl@0
   151
sl@0
   152
	ret = iConfig.GetHex(aSection, _L("sidclasses"), iSidClasses);
sl@0
   153
	if (! ret) iSidClasses = 0x0000FFFF; 
sl@0
   154
sl@0
   155
	iSidList.Reset();
sl@0
   156
	TInt numSids(0);
sl@0
   157
	ret = iConfig.GetInt(aSection, _L("numsids"), numSids);
sl@0
   158
	for (TInt i = 0; i < numSids; ++i)
sl@0
   159
		{
sl@0
   160
		TBuf<8> key;
sl@0
   161
		key.AppendFormat(_L("sid%d"), i);
sl@0
   162
sl@0
   163
		TInt sid;
sl@0
   164
		ret = iConfig.GetHex(aSection, key, sid);
sl@0
   165
		ASSERT_TRUE(ret);
sl@0
   166
		iSidList.AppendL(sid);
sl@0
   167
		}
sl@0
   168
sl@0
   169
	ASSERT_TRUE(iConfig.GetString(aSection, _L("destination"), iDestination));
sl@0
   170
	ASSERT_TRUE(iConfig.GetHex(aSection, _L("options"), iOptions));
sl@0
   171
	ASSERT_TRUE(iConfig.GetHex(aSection, _L("systemserversecurity"), iSystemServerSecurity));
sl@0
   172
sl@0
   173
	TInt policyEvaluator;
sl@0
   174
	ASSERT_TRUE(iConfig.GetHex(aSection, _L("policyevaluator"), policyEvaluator));
sl@0
   175
	iPolicyEvaluator = TUid::Uid(policyEvaluator);
sl@0
   176
	
sl@0
   177
	TInt dialogCreator;
sl@0
   178
	ASSERT_TRUE(iConfig.GetHex(aSection, _L("dialogcreator"), dialogCreator));
sl@0
   179
	iDialogCreator = TUid::Uid(dialogCreator);
sl@0
   180
	ASSERT_TRUE(iConfig.GetHex(aSection, _L("flags"), iFlags));
sl@0
   181
sl@0
   182
	ASSERT_TRUE(iConfig.GetInt(aSection, _L("majorversion"), iMajorVersion));
sl@0
   183
	ASSERT_TRUE(iConfig.GetInt(aSection, _L("minorversion"), iMinorVersion));
sl@0
   184
	ASSERT_TRUE(iConfig.GetBool(aSection, _L("default"), iDefault));
sl@0
   185
	}
sl@0
   186
sl@0
   187
TBool CTestPolicyReader::CompareExpectedValues(const CPolicy& aPolicy)
sl@0
   188
/**
sl@0
   189
 Compares the expected policy values from the ini file to the fields
sl@0
   190
 in the policy object read from the resource file.
sl@0
   191
 @param aPolicy The policy object to compare the ini value to.
sl@0
   192
 @return ETrue, if the values match; otherwise, EFalse is returned.
sl@0
   193
 */
sl@0
   194
	{
sl@0
   195
	TBool ret(ETrue);
sl@0
   196
sl@0
   197
	if (static_cast<TUint16>(iSidClasses) != aPolicy.SidClasses().iSidClasses)
sl@0
   198
		{
sl@0
   199
		ERR_PRINTF3(_L("Expected SID classes 0x%04x, actual SID classes 0x%04x"),
sl@0
   200
				   iSidClasses, aPolicy.SidClasses().iSidClasses);
sl@0
   201
		ret = EFalse;
sl@0
   202
		} 
sl@0
   203
sl@0
   204
	if (iSidList.Count() != aPolicy.SidList().Count())
sl@0
   205
		{
sl@0
   206
		ERR_PRINTF3(_L("Expected number of SIDs = %d, actual number of SIDs = %d"),
sl@0
   207
					iSidList.Count(), aPolicy.SidList().Count());
sl@0
   208
		ret = EFalse;
sl@0
   209
		}
sl@0
   210
		
sl@0
   211
	if (iSystemServerSecurity != aPolicy.SystemServerSecurity())
sl@0
   212
		{
sl@0
   213
		ERR_PRINTF3(_L("Expected systemserversecurity %d, actual systemserversecurity %d"),
sl@0
   214
			iSystemServerSecurity, aPolicy.SystemServerSecurity());
sl@0
   215
		ret = EFalse;
sl@0
   216
		}
sl@0
   217
sl@0
   218
	for (TInt i = 0; i < Min(iSidList.Count(), aPolicy.SidList().Count()); ++i)
sl@0
   219
		{
sl@0
   220
		if (iSidList[i].iId != aPolicy.SidList()[i].iId)
sl@0
   221
			{
sl@0
   222
			ERR_PRINTF4(_L("Expected SID%d = 0x%08x, actual SID 0x%08x"),
sl@0
   223
						i, iSidList[i].iId, aPolicy.SidList()[i].iId);
sl@0
   224
			ret = EFalse;			
sl@0
   225
			}
sl@0
   226
		}
sl@0
   227
	
sl@0
   228
	if (iDestination != aPolicy.Destination())
sl@0
   229
		{
sl@0
   230
		ERR_PRINTF3(_L("Expected destination %S, actual destination %S"),
sl@0
   231
					&iDestination, &aPolicy.Destination());
sl@0
   232
		ret = EFalse;
sl@0
   233
		}
sl@0
   234
sl@0
   235
	if (iOptions != aPolicy.Options())
sl@0
   236
		{
sl@0
   237
		ERR_PRINTF3(_L("Expected options = 0x%08x, actual options = 0x%08x"),
sl@0
   238
					&iOptions, aPolicy.Options());
sl@0
   239
		ret = EFalse;
sl@0
   240
		}
sl@0
   241
sl@0
   242
	if (iPolicyEvaluator != aPolicy.PolicyEvaluator())
sl@0
   243
		{
sl@0
   244
		ERR_PRINTF3(_L("Expected policy evaluator 0x%08x, actual policy evaluator 0x%08x"),
sl@0
   245
					iPolicyEvaluator, aPolicy.PolicyEvaluator());
sl@0
   246
		ret = EFalse;
sl@0
   247
		}
sl@0
   248
sl@0
   249
	if (iDialogCreator != aPolicy.DialogCreator())
sl@0
   250
		{
sl@0
   251
		ERR_PRINTF3(_L("Expected dialog creator 0x%08x, actualy dialog creator 0x%08x"),
sl@0
   252
					iDialogCreator, aPolicy.DialogCreator());
sl@0
   253
		ret = EFalse;
sl@0
   254
		}
sl@0
   255
sl@0
   256
	if (iFlags != aPolicy.Flags())
sl@0
   257
		{
sl@0
   258
		ERR_PRINTF3(_L("Expected flags = 0x%04x, actual flags = 0x%04x"),
sl@0
   259
					iFlags, aPolicy.Flags());
sl@0
   260
		ret = EFalse;
sl@0
   261
		}
sl@0
   262
		
sl@0
   263
	if (iMajorVersion != aPolicy.MajorVersion())
sl@0
   264
		{
sl@0
   265
		ERR_PRINTF3(_L("Expected majorversion = %d, actual major version = %d"),
sl@0
   266
					iMajorVersion, aPolicy.MajorVersion());
sl@0
   267
		ret = EFalse;		
sl@0
   268
		}
sl@0
   269
sl@0
   270
	if (iMinorVersion != aPolicy.MinorVersion())
sl@0
   271
		{
sl@0
   272
		ERR_PRINTF3(_L("Expected majorversion = %d, actual major version = %d"),
sl@0
   273
					iMinorVersion, aPolicy.MinorVersion());
sl@0
   274
		ret = EFalse;		
sl@0
   275
		}		
sl@0
   276
		
sl@0
   277
	if (iDefault != aPolicy.Default())
sl@0
   278
		{
sl@0
   279
		ERR_PRINTF3(_L("Expected default policy = %d, actual default policy= %d"),
sl@0
   280
					iDefault, aPolicy.Default());
sl@0
   281
		ret = EFalse;		
sl@0
   282
		}				
sl@0
   283
	return ret;
sl@0
   284
	}