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