os/security/authorisation/userpromptservice/database/test/tupsdb/source/tupsdbstep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/authorisation/userpromptservice/database/test/tupsdb/source/tupsdbstep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1013 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2010 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 +* Performs decision filter, record and database tests
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +/**
1.24 + @file
1.25 + @test
1.26 +*/
1.27 +
1.28 +#include "tupsdbstep.h"
1.29 +#include <test/testexecutelog.h>
1.30 +
1.31 +using namespace UserPromptService;
1.32 +
1.33 +
1.34 +CUpsDbStepUnitTest::CUpsDbStepUnitTest()
1.35 +/**
1.36 + Constructer
1.37 + */
1.38 + {
1.39 + iComparison = EEqual;
1.40 + iCancel = EFalse;
1.41 + }
1.42 +
1.43 +CUpsDbStepUnitTest::~CUpsDbStepUnitTest()
1.44 +/**
1.45 + Destructor
1.46 + */
1.47 + {
1.48 + iFs.Close();
1.49 + iFileWriteStream.Close();
1.50 + iFileReadStream.Close();
1.51 + delete iWaiter;
1.52 + delete iScheduler;
1.53 + }
1.54 +
1.55 +
1.56 +TVerdict CUpsDbStepUnitTest::doTestStepPreambleL()
1.57 +/**
1.58 + This function is called at the beginning of the test step. First reads the operation type
1.59 + and converts it to a proper enumaration value (TUpsDbUnitTest). Then gets the specified
1.60 + database location. The test fails if the database location can not be found, except
1.61 + the unit tests of the decision record and filter objects.
1.62 + */
1.63 + {
1.64 + iScheduler = new(ELeave) CActiveScheduler;
1.65 + CActiveScheduler::Install(iScheduler);
1.66 + iWaiter = new(ELeave)CActiveWaiter(Logger());
1.67 +
1.68 + iLog = EFalse;
1.69 +
1.70 + TPtrC opType;
1.71 +
1.72 + if(EFalse == GetStringFromConfig(ConfigSection(),KOpType,opType))
1.73 + {
1.74 + ERR_PRINTF2(_L("Failed to read '%S' key of configuration"),&KOpType);
1.75 + SetTestStepResult(EFail);
1.76 + }
1.77 +
1.78 + if(!opType.CompareF(KInsertRecord))
1.79 + {
1.80 + iOpType = EInsertRecord;
1.81 + }
1.82 + else if(!opType.CompareF(KDeleteRecord))
1.83 + {
1.84 + iOpType = EDeleteRecord;
1.85 + }
1.86 + else if(!opType.CompareF(KSingleLookup))
1.87 + {
1.88 + iOpType = ESingleLookup;
1.89 + }
1.90 + else if(!opType.CompareF(KMultipleLookup))
1.91 + {
1.92 + iOpType = EMultipleLookup;
1.93 + }
1.94 + else if(!opType.CompareF(KVerifyRecord))
1.95 + {
1.96 + iOpType = EVerifyRecord;
1.97 + }
1.98 + else if(!opType.CompareF(KCreateDatabase))
1.99 + {
1.100 + iOpType = ECreateDatabase;
1.101 + }
1.102 + else if(!opType.CompareF(KDeleteDatabase))
1.103 + {
1.104 + iOpType = EDeleteDatabase;
1.105 + }
1.106 + else if(!opType.CompareF(KCreateFilterSingleCall))
1.107 + {
1.108 + iOpType = ECreateFilterSingleCall;
1.109 + }
1.110 + else if(!opType.CompareF(KCreateFilterMultipleCalls))
1.111 + {
1.112 + iOpType = ECreateFilterMultipleCalls;
1.113 + }
1.114 + else if(!opType.CompareF(KCreateDecision))
1.115 + {
1.116 + iOpType = ECreateDecision;
1.117 + }
1.118 + else if(!opType.CompareF(KFillTable))
1.119 + {
1.120 + iOpType = EFillTable;
1.121 + }
1.122 + else if(!opType.CompareF(KAllFingerprints))
1.123 + {
1.124 + iOpType = EAllFingerprints;
1.125 + }
1.126 + else if(!opType.CompareF(KCompactDatabase))
1.127 + {
1.128 + iOpType = ECompact;
1.129 + }
1.130 + else if(!opType.CompareF(KUpdateRecord))
1.131 + {
1.132 + iOpType = EUpdate;
1.133 + }
1.134 + else
1.135 + {
1.136 + iOpType = ENotSupported;
1.137 + }
1.138 +
1.139 + if(EFalse == GetStringFromConfig(ConfigSection(),KDbLocation,iDbLocation))
1.140 + {
1.141 + if((iOpType != ECreateFilterSingleCall) && (iOpType != ECreateDecision) &&
1.142 + (iOpType != ECreateFilterMultipleCalls) && (iOpType != ENotSupported))
1.143 + {
1.144 + ERR_PRINTF2(_L("Failed to read '%S' key of configuration"),&KDbLocation);
1.145 + SetTestStepResult(EFail);
1.146 + User::Leave(KErrNotFound);
1.147 + }
1.148 + }
1.149 +
1.150 + GetBoolFromConfig(ConfigSection(), KOOMCondition,iOOMCondition);
1.151 +
1.152 + User::LeaveIfError(iFs.Connect());
1.153 + User::LeaveIfError(iFs.CreatePrivatePath(EDriveC));
1.154 + return TestStepResult();
1.155 + }
1.156 +
1.157 +
1.158 +TVerdict CUpsDbStepUnitTest::doTestStepL()
1.159 +/**
1.160 + Calls actual test step or OOM test
1.161 + */
1.162 + {
1.163 + if (!iOOMCondition)
1.164 + {
1.165 + doTestL();
1.166 + }
1.167 + else
1.168 + {
1.169 + return doOOMTestL();
1.170 + }
1.171 +
1.172 + return TestStepResult();
1.173 + }
1.174 +
1.175 +
1.176 +TVerdict CUpsDbStepUnitTest::doTestL()
1.177 +/**
1.178 + Perform the actual test steps. Unit tests for all provided database operations
1.179 + and creation of both filter and decision record objects.
1.180 + */
1.181 + {
1.182 +
1.183 + //Define required objects
1.184 + CDecisionDbW *upsDb = NULL;
1.185 + CDecisionRecord *record = NULL;
1.186 + CDecisionFilter *filter = NULL;
1.187 + CleanupClosePushL(iFingerprint);
1.188 + CleanupClosePushL(iClientEntity);
1.189 +
1.190 + switch(iOpType)
1.191 + {
1.192 + case ECreateDatabase:
1.193 + {
1.194 + //Make sure the database file does not exist
1.195 + iFs.Delete(iDbLocation);
1.196 +
1.197 + //Create the database
1.198 + upsDb = CreateDatabaseLC(iDbLocation, iFs);
1.199 +
1.200 + //Disconnect
1.201 + CleanupStack::PopAndDestroy(upsDb);
1.202 + break;
1.203 + }
1.204 +
1.205 + case EDeleteDatabase:
1.206 + {
1.207 + //Connect to the database
1.208 + upsDb = CDecisionDbW::NewL(iDbLocation, iFs);
1.209 + CleanupStack::PushL(upsDb);
1.210 +
1.211 + //Delete all records and database
1.212 + RemoveAllDecisionsL(*upsDb, iFs);
1.213 +
1.214 + //Disconnect
1.215 + CleanupStack::PopAndDestroy(upsDb);
1.216 + break;
1.217 + }
1.218 +
1.219 + case ECreateFilterSingleCall:
1.220 + {
1.221 + //Read the decision values from the config file
1.222 + ReadDecisionFieldsL(EFalse);
1.223 +
1.224 + TPtrC strExpectedResult;
1.225 + if(EFalse == GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
1.226 + {
1.227 + ERR_PRINTF1(_L("Expected result is missing!"));
1.228 + SetTestStepResult(EFail);
1.229 + break;
1.230 + }
1.231 + TInt expectedResult = ConvertNumber(strExpectedResult);
1.232 +
1.233 + //First method is to create a filter with single function call
1.234 + TRAPD(error,filter = CDecisionFilter::NewL(TUid::Uid(iClientSid),TUid::Uid(iEvaluatorId),TUid::Uid(iServiceId),TUid::Uid(iServerSid),iFingerprint,iClientEntity,iMajorPolicyVersion));
1.235 +
1.236 + if(error != expectedResult)
1.237 + {
1.238 + ERR_PRINTF3(_L("In the single call method, the result (%d) is different from the expected result (%d)"), error, expectedResult);
1.239 + SetTestStepResult(EFail);
1.240 + }
1.241 +
1.242 + if(error == KErrNone)
1.243 + {
1.244 + INFO_PRINTF1(_L("Successful to create a decision filter using the single call method"));
1.245 + delete filter;
1.246 + }
1.247 + else
1.248 + {
1.249 + INFO_PRINTF1(_L("Failed to create a decision filter using the single call method"));
1.250 + }
1.251 +
1.252 + break;
1.253 + }
1.254 +
1.255 + case ECreateFilterMultipleCalls:
1.256 + {
1.257 + //Read the decision values from the config file
1.258 + ReadDecisionFieldsL(EFalse);
1.259 +
1.260 + TPtrC strExpectedResult;
1.261 + if(EFalse == GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
1.262 + {
1.263 + ERR_PRINTF1(_L("Expected result is missing!"));
1.264 + SetTestStepResult(EFail);
1.265 + break;
1.266 + }
1.267 + TInt expectedResult = ConvertNumber(strExpectedResult);
1.268 +
1.269 + //Second method is to create an empty filter and then set provided fields
1.270 + TRAPD(error,filter = CreateFilterL(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison));
1.271 +
1.272 + if(error != expectedResult)
1.273 + {
1.274 + ERR_PRINTF3(_L("In the multiple calls method, the result (%d) is different from the expected result (%d)"), error, expectedResult);
1.275 + SetTestStepResult(EFail);
1.276 + }
1.277 +
1.278 + if(error == KErrNone)
1.279 + {
1.280 + INFO_PRINTF1(_L("Successful to create a decision filter using the multiple calls method"));
1.281 + delete filter;
1.282 + }
1.283 + else
1.284 + {
1.285 + INFO_PRINTF1(_L("Failed to create a decision filter using the multiple calls method"));
1.286 + }
1.287 +
1.288 + break;
1.289 + }
1.290 +
1.291 + case ECreateDecision:
1.292 + {
1.293 + //Read the decision values from the config file
1.294 + ReadDecisionFieldsL(EFalse);
1.295 +
1.296 + TPtrC strExpectedResult;
1.297 + if(EFalse == GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
1.298 + {
1.299 + ERR_PRINTF1(_L("Expected result is missing!"));
1.300 + SetTestStepResult(EFail);
1.301 + break;
1.302 + }
1.303 + TInt expectedResult = ConvertNumber(strExpectedResult);
1.304 +
1.305 + TRAPD(error,record = CreateRecordL(iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iDescription,iResult,iMajorPolicyVersion,iEvaluatorInfo,iRecordId));
1.306 +
1.307 + if(error != expectedResult)
1.308 + {
1.309 + ERR_PRINTF3(_L("The result (%d) is different from the expected result (%d)"), error, expectedResult);
1.310 + SetTestStepResult(EFail);
1.311 + }
1.312 +
1.313 + if(error == KErrNone)
1.314 + {
1.315 + INFO_PRINTF1(_L("Successful to create a decision record"));
1.316 + delete record;
1.317 + }
1.318 + else
1.319 + {
1.320 + INFO_PRINTF1(_L("Failed to create a decision record"));
1.321 + }
1.322 +
1.323 + break;
1.324 + }
1.325 +
1.326 + case EInsertRecord:
1.327 + {
1.328 + //Read the decision values from the config file
1.329 + ReadDecisionFieldsL(ETrue);
1.330 +
1.331 + //ClientSid,EvaluatorId,ServiceId,ServerSid and Fingerprint must be provided
1.332 + if((iSetFlag & (KSetClientSid|KSetEvaluatorId|KSetServiceId|KSetServerSid|KSetFingerprint))!=
1.333 + (KSetClientSid|KSetEvaluatorId|KSetServiceId|KSetServerSid|KSetFingerprint))
1.334 + {
1.335 + SetTestStepResult(EFail);
1.336 + break;
1.337 + }
1.338 +
1.339 + //Create decision record
1.340 + record = CreateRecordL(iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iDescription,iResult,iMajorPolicyVersion,iEvaluatorInfo,iRecordId);
1.341 + CleanupStack::PushL(record);
1.342 +
1.343 + //Connect to the database
1.344 + upsDb = CDecisionDbW::NewL(iDbLocation,iFs);
1.345 + CleanupStack::PushL(upsDb);
1.346 +
1.347 + //Insert the decision
1.348 + InsertRecordL(*upsDb, *record);
1.349 +
1.350 + //Disconnect and destroy objects
1.351 + CleanupStack::PopAndDestroy(2,record);
1.352 +
1.353 + break;
1.354 + }
1.355 +
1.356 + case EDeleteRecord:
1.357 + {
1.358 + //Read the decision values from the config file
1.359 + ReadDecisionFieldsL(ETrue);
1.360 +
1.361 + if(!iSetFlag)
1.362 + {
1.363 + ERR_PRINTF1(_L("A filter key must be supplied at least!"));
1.364 + SetTestStepResult(EFail);
1.365 + break;
1.366 + }
1.367 +
1.368 + //Create filter
1.369 + filter = CreateFilterLC(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison);
1.370 +
1.371 + //Connect to the database
1.372 + upsDb = CDecisionDbW::NewLC(iDbLocation,iFs);
1.373 +
1.374 + //Delete the decision
1.375 + RemoveDecisionsL(*upsDb, *filter);
1.376 +
1.377 + CleanupStack::PopAndDestroy(2,filter);
1.378 + break;
1.379 + }
1.380 +
1.381 + case ESingleLookup:
1.382 + {
1.383 + //Read the decision values from the config file
1.384 + ReadDecisionFieldsL(ETrue);
1.385 +
1.386 + TInt expectedRecords;
1.387 + if(EFalse == GetIntFromConfig(ConfigSection(),KExpectedRecords,expectedRecords))
1.388 + {
1.389 + ERR_PRINTF1(_L("Expected result is missing!"));
1.390 + SetTestStepResult(EFail);
1.391 + break;
1.392 + }
1.393 +
1.394 + TPtrC strExpectedResult;
1.395 + TInt expectedResult=KErrNone;
1.396 + if(GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
1.397 + {
1.398 + expectedResult = ConvertNumber(strExpectedResult);
1.399 + }
1.400 +
1.401 + //Create filter
1.402 + filter = CreateFilterL(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison);
1.403 + CleanupStack::PushL(filter);
1.404 +
1.405 + //Connect to the database
1.406 + upsDb = CDecisionDbW::NewLC(iDbLocation,iFs);
1.407 +
1.408 + //Retrieve single record
1.409 + TRAPD(err,record = GetDecisionLC(*upsDb, *filter);if(record)CleanupStack::Pop(record););
1.410 + if((err==KErrNone)&&(record))
1.411 + {
1.412 + CleanupStack::PushL(record);
1.413 + }
1.414 +
1.415 + if(err!=expectedResult)
1.416 + {
1.417 + SetTestStepResult(EFail);
1.418 + }
1.419 +
1.420 + if(record)
1.421 + {
1.422 + OpenDecisionFileL(EFileWrite);
1.423 + WriteDecisionL(*record);
1.424 + CloseDecisionFile();
1.425 + CleanupStack::PopAndDestroy();
1.426 + }
1.427 + else
1.428 + {
1.429 + if(expectedRecords)
1.430 + {
1.431 + ERR_PRINTF1(_L("Failed to retrieve a single record"));
1.432 + SetTestStepResult(EFail);
1.433 + }
1.434 + }
1.435 +
1.436 +
1.437 + CleanupStack::PopAndDestroy(upsDb);
1.438 + CleanupStack::PopAndDestroy(filter);
1.439 + break;
1.440 + }
1.441 +
1.442 + case EMultipleLookup:
1.443 + {
1.444 + //Read the decision values from the config file
1.445 + ReadDecisionFieldsL(ETrue);
1.446 +
1.447 + //Create filter
1.448 + filter = CreateFilterL(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison);
1.449 + CleanupStack::PushL(filter);
1.450 +
1.451 + //Connect to the database
1.452 + upsDb = CDecisionDbW::NewL(iDbLocation,iFs);
1.453 + CleanupStack::PushL(upsDb);
1.454 +
1.455 + RPointerArray<CDecisionRecord> recordList;
1.456 + CleanupResetAndDestroyPushL(recordList);
1.457 + GetDecisionsL(*upsDb, *filter, iCancel, recordList);
1.458 +
1.459 + TInt recNum;
1.460 + recNum = recordList.Count();
1.461 + if(recNum)
1.462 + {
1.463 + OpenDecisionFileL(EFileWrite);
1.464 +
1.465 + for(TInt i=0; i<recNum; ++i)
1.466 + {
1.467 + WriteDecisionL(*recordList[i]);
1.468 + }
1.469 + CloseDecisionFile();
1.470 + }
1.471 + CleanupStack::PopAndDestroy(&recordList);
1.472 +
1.473 + TInt expectedRecords;
1.474 + if(EFalse == GetIntFromConfig(ConfigSection(),KExpectedRecords,expectedRecords))
1.475 + {
1.476 + expectedRecords=-1;
1.477 + }
1.478 +
1.479 + if(!iCancel && (((expectedRecords>=0) && (recNum != expectedRecords))||((expectedRecords<0) && (recNum<=0))))
1.480 + {
1.481 + SetTestStepResult(EFail);
1.482 + }
1.483 +
1.484 + CleanupStack::PopAndDestroy(2,filter);
1.485 + break;
1.486 + }
1.487 +
1.488 + case EVerifyRecord:
1.489 + {
1.490 + //Read the decision values from the config file
1.491 + ReadDecisionFieldsL(ETrue);
1.492 + TBool verified = ETrue;
1.493 +
1.494 + OpenDecisionFileL(EFileRead);
1.495 +
1.496 + while((record = ReadDecisionL()) != NULL)
1.497 + {
1.498 + CleanupStack::PushL(record);
1.499 +
1.500 + if(iSetFlag & KSetClientSid)
1.501 + {
1.502 + if(record->iClientSid != TSecureId(iClientSid))
1.503 + {
1.504 + verified = EFalse;
1.505 + break;
1.506 + }
1.507 + }
1.508 +
1.509 + if(iSetFlag & KSetEvaluatorId)
1.510 + {
1.511 + if(record->iEvaluatorId != TUid::Uid(iEvaluatorId))
1.512 + {
1.513 + verified = EFalse;
1.514 + break;
1.515 + }
1.516 + }
1.517 +
1.518 + if(iSetFlag & KSetServiceId)
1.519 + {
1.520 + if(record->iServiceId != TUid::Uid(iServiceId))
1.521 + {
1.522 + verified = EFalse;
1.523 + break;
1.524 + }
1.525 + }
1.526 +
1.527 + if(iSetFlag & KSetServerSid)
1.528 + {
1.529 + if(record->iServerSid != TSecureId(iServerSid))
1.530 + {
1.531 + verified = EFalse;
1.532 + break;
1.533 + }
1.534 + }
1.535 +
1.536 + if(iSetFlag & KSetFingerprint)
1.537 + {
1.538 + if(record->iFingerprint.Compare(iFingerprint))
1.539 + {
1.540 + verified = EFalse;
1.541 + break;
1.542 + }
1.543 + }
1.544 +
1.545 + if(iSetFlag & KSetClientEntity)
1.546 + {
1.547 + if(record->iClientEntity.Compare(iClientEntity))
1.548 + {
1.549 + verified = EFalse;
1.550 + break;
1.551 + }
1.552 + }
1.553 +
1.554 + if(iSetFlag & KSetDescription)
1.555 + {
1.556 + if(record->iDescription.Compare(iDescription))
1.557 + {
1.558 + verified = EFalse;
1.559 + break;
1.560 + }
1.561 + }
1.562 +
1.563 + if(iSetFlag & KSetResult)
1.564 + {
1.565 + if(record->iResult != iResult)
1.566 + {
1.567 + verified = EFalse;
1.568 + break;
1.569 + }
1.570 + }
1.571 +
1.572 + if(iSetFlag & KSetEvaluatorInfo)
1.573 + {
1.574 + if(record->iEvaluatorInfo != iEvaluatorInfo)
1.575 + {
1.576 + verified = EFalse;
1.577 + break;
1.578 + }
1.579 + }
1.580 +
1.581 + if(iSetFlag & KSetMajorPolicyVersion)
1.582 + {
1.583 + if(record->iMajorPolicyVersion != iMajorPolicyVersion)
1.584 + {
1.585 + verified = EFalse;
1.586 + break;
1.587 + }
1.588 + }
1.589 +
1.590 + if(iSetFlag & KSetRecordId)
1.591 + {
1.592 + if(record->iRecordId != iRecordId)
1.593 + {
1.594 + verified = EFalse;
1.595 + break;
1.596 + }
1.597 + }
1.598 +
1.599 + CleanupStack::PopAndDestroy(record);
1.600 + }//while
1.601 +
1.602 +
1.603 + if(verified == EFalse)
1.604 + {
1.605 + ERR_PRINTF1(_L("Retrieved record(s) is(are) different from the supplied one"));
1.606 + SetTestStepResult(EFail);
1.607 + CleanupStack::PopAndDestroy(record);
1.608 + }
1.609 + else
1.610 + {
1.611 + INFO_PRINTF1(_L("Retrieved record(s) have been verified successfully"));
1.612 + }
1.613 +
1.614 + CloseDecisionFile();
1.615 +
1.616 + break;
1.617 + }
1.618 +
1.619 + case EFillTable:
1.620 + {
1.621 + TBuf<KUpsDescriptionLength> keyNotFound;
1.622 + ReadConfigurationFile(keyNotFound);
1.623 + TInt len = keyNotFound.Length();
1.624 + if(len > 0)
1.625 + {
1.626 + //if more than one keys could not read or retrieve_num is not in the list
1.627 + if((len > 13) || (keyNotFound.Find(KRetrieveNum) == KErrNotFound))
1.628 + {
1.629 + ERR_PRINTF2(_L("Failed to read '%S' key of configuration"),&keyNotFound);
1.630 + SetTestStepResult(EFail);
1.631 + User::Leave(KErrNotFound);
1.632 + }
1.633 + }
1.634 + //Connect to the decision database
1.635 + CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
1.636 +
1.637 + //Fill the table with a specified number of decision records
1.638 + FillTableL(*upsDb,iRecordNum,iClientNum,iServiceNum,iServerNum,iEvaluatorNum);
1.639 +
1.640 + //Disconnect
1.641 + CleanupStack::PopAndDestroy(upsDb);
1.642 + break;
1.643 + }
1.644 +
1.645 + case EAllFingerprints:
1.646 + {
1.647 + //Connect to the decision database
1.648 + CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
1.649 + TestFingerprintValuesL(*upsDb);
1.650 + //Disconnect
1.651 + CleanupStack::PopAndDestroy(upsDb);
1.652 + break;
1.653 + }
1.654 +
1.655 + case ECompact:
1.656 + {
1.657 + ReadDecisionFieldsL(ETrue);
1.658 + //Connect to the decision database
1.659 + CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
1.660 + CompactDatabaseL(*upsDb,iCancel);
1.661 + //Disconnect
1.662 + CleanupStack::PopAndDestroy(upsDb);
1.663 + break;
1.664 + }
1.665 +
1.666 + case EUpdate:
1.667 + {
1.668 + //Open the decision file containing the record which will be updated
1.669 + OpenDecisionFileL(EFileRead);
1.670 + //Read the record
1.671 + record = ReadDecisionL();
1.672 + //Close the decision file
1.673 + CloseDecisionFile();
1.674 + //If we could not read the record, the test step fails
1.675 + if(!record)
1.676 + {
1.677 + SetTestStepResult(EFail);
1.678 + break;
1.679 + }
1.680 + CleanupStack::PushL(record);
1.681 +
1.682 + //Create filter from the record which has been read from the decision file
1.683 + TInt flag = (KSetClientSid|KSetEvaluatorId|KSetServiceId|KSetServerSid|KSetFingerprint|KSetClientEntity|KSetMajorPolicyVersion);
1.684 + filter = CreateFilterL(flag,record->iClientSid.iId,record->iEvaluatorId.iUid,record->iServiceId.iUid,record->iServerSid.iId,record->iFingerprint,record->iClientEntity,record->iMajorPolicyVersion,record->iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison);
1.685 + CleanupStack::PushL(filter);
1.686 +
1.687 + //Create a new file
1.688 + ReadDecisionFieldsL(ETrue);
1.689 + //Create a new record from the values in the configuration file
1.690 + CDecisionRecord *newRecord = CreateRecordL(iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iDescription,iResult,iMajorPolicyVersion,iEvaluatorInfo,iRecordId);
1.691 + CleanupStack::PushL(newRecord);
1.692 +
1.693 + //Connect to the decision database
1.694 + CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
1.695 + //Update the record
1.696 + UpdateRecordL(*upsDb, *filter, *newRecord);
1.697 +
1.698 + CleanupStack::PopAndDestroy(4, record);
1.699 +
1.700 + break;
1.701 + }
1.702 +
1.703 + case ENotSupported:
1.704 + {
1.705 + ERR_PRINTF1(_L("The provided key is not supported!"));
1.706 + SetTestStepResult(EFail);
1.707 + break;
1.708 + }
1.709 + }
1.710 +
1.711 + CleanupStack::PopAndDestroy(2); //Pop RBufs
1.712 + return TestStepResult();
1.713 + }
1.714 +
1.715 +
1.716 +TVerdict CUpsDbStepUnitTest::doOOMTestL()
1.717 +/**
1.718 + Performs OOM test
1.719 + */
1.720 + {
1.721 + TVerdict verdict = EFail;
1.722 + TInt countAfter = 0;
1.723 + TInt countBefore = 0;
1.724 + for (TInt oomCount = 0; ; oomCount++)
1.725 + {
1.726 + INFO_PRINTF2(_L("\n ==== Number of memory allocations %d ===="), oomCount);
1.727 + verdict = EFail;
1.728 + __UHEAP_RESET;
1.729 + __UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
1.730 + countBefore = User::CountAllocCells();
1.731 + TRAPD(error, doTestL());// ----> This is the actual test that runs under OOM conditions.
1.732 + countAfter = User::CountAllocCells();
1.733 + __UHEAP_RESET;
1.734 +
1.735 + TESTL(error == KErrNone || error == KErrNoMemory);
1.736 +
1.737 + if (error == KErrNone)
1.738 + {
1.739 + verdict = EPass;
1.740 + INFO_PRINTF2(_L("OOM Status %d"),error);
1.741 + INFO_PRINTF1(_L("Test outcome : Passed"));
1.742 + break;
1.743 + }
1.744 + else
1.745 + {
1.746 + if (countBefore != countAfter)
1.747 + {
1.748 + INFO_PRINTF2(_L("OOM Status %d"),error);
1.749 + INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
1.750 + SetTestStepResult(verdict);
1.751 + break;
1.752 + }
1.753 + }
1.754 + INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
1.755 + }
1.756 + INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);
1.757 + SetTestStepResult(verdict);
1.758 + if (verdict==EFail)
1.759 + {
1.760 + User::Leave(KErrGeneral);
1.761 + }
1.762 + return verdict;
1.763 + }
1.764 +
1.765 +
1.766 +TVerdict CUpsDbStepUnitTest::doTestStepPostambleL()
1.767 +/**
1.768 + Runs at the end of the step. Do nothing.
1.769 + */
1.770 + {
1.771 + iFs.Close();
1.772 + return TestStepResult();
1.773 + }
1.774 +
1.775 +
1.776 +void CUpsDbStepUnitTest::ReadDecisionFieldsL(TBool aHash)
1.777 +/**
1.778 + Reads the fileds required to create a decision record or filter.
1.779 +
1.780 + @param aHash Hashes fingerprint and client entity values if ETrue.
1.781 + */
1.782 + {
1.783 + if(GetHexFromConfig(ConfigSection(),KColClientSid,iClientSid))
1.784 + {
1.785 + iSetFlag |= KSetClientSid;
1.786 + }
1.787 +
1.788 + if(GetHexFromConfig(ConfigSection(),KColEvaluatorId,iEvaluatorId))
1.789 + {
1.790 + iSetFlag |= KSetEvaluatorId;
1.791 + }
1.792 +
1.793 + if(GetHexFromConfig(ConfigSection(),KColServiceId,iServiceId))
1.794 + {
1.795 + iSetFlag |= KSetServiceId;
1.796 + }
1.797 +
1.798 + if(GetHexFromConfig(ConfigSection(),KColServerSid,iServerSid))
1.799 + {
1.800 + iSetFlag |= KSetServerSid;
1.801 + }
1.802 +
1.803 + TPtrC string; //16-bit temporary pointer
1.804 + CMD5 *md5 = CMD5::NewL();
1.805 + CleanupStack::PushL(md5);
1.806 +
1.807 + if(GetStringFromConfig(ConfigSection(),KColFingerprint,string))
1.808 + {
1.809 + if(string.Length() > 0)
1.810 + {
1.811 + //Copy to convert 16-bit to 8-bit
1.812 + iFingerprint.CreateL(string.Length()*2);
1.813 + iFingerprint.Copy(string);
1.814 + if(aHash)
1.815 + {
1.816 + TPtrC8 hashed = md5->Hash(iFingerprint);
1.817 + if(string.Length()*2 < hashed.Length())
1.818 + {
1.819 + iFingerprint.ReAlloc(hashed.Length());
1.820 + }
1.821 + iFingerprint.Copy(hashed);
1.822 + md5->Reset();
1.823 + }
1.824 + }
1.825 + iSetFlag |= KSetFingerprint;
1.826 + }
1.827 +
1.828 + if(GetStringFromConfig(ConfigSection(),KColClientEntity,string))
1.829 + {
1.830 + if(string.Length() > 0)
1.831 + {
1.832 + //Copy to convert 16-bit to 8-bit
1.833 + iClientEntity.CreateL(string.Length()*2);
1.834 + iClientEntity.Copy(string);
1.835 + if(aHash)
1.836 + {
1.837 + TPtrC8 hashed = md5->Hash(iClientEntity);
1.838 + if(string.Length()*2 < hashed.Length())
1.839 + {
1.840 + iClientEntity.ReAlloc(hashed.Length());
1.841 + }
1.842 + iClientEntity.Copy(hashed);
1.843 + }
1.844 + }
1.845 + iSetFlag |= KSetClientEntity;
1.846 + }
1.847 +
1.848 + CleanupStack::PopAndDestroy(md5);
1.849 +
1.850 + if(GetStringFromConfig(ConfigSection(),KColDescription,iDescription))
1.851 + {
1.852 + iSetFlag |= KSetDescription;
1.853 + }
1.854 +
1.855 + if(GetIntFromConfig(ConfigSection(),KColResult,iResult))
1.856 + {
1.857 + iSetFlag |= KSetResult;
1.858 + }
1.859 +
1.860 + if(GetIntFromConfig(ConfigSection(),KColEvaluatorInfo,iEvaluatorInfo))
1.861 + {
1.862 + iSetFlag |= KSetEvaluatorInfo;
1.863 + }
1.864 +
1.865 + if(GetIntFromConfig(ConfigSection(),KColMajorPolicyVersion,iMajorPolicyVersion))
1.866 + {
1.867 + iSetFlag |= KSetMajorPolicyVersion;
1.868 + }
1.869 +
1.870 + if(GetIntFromConfig(ConfigSection(),KColRecordId,iRecordId))
1.871 + {
1.872 + iSetFlag |= KSetRecordId;
1.873 + }
1.874 +
1.875 + TInt compOp;
1.876 + if(GetHexFromConfig(ConfigSection(),KComparison,compOp))
1.877 + {
1.878 + iComparison = static_cast<TComparisonOp>(compOp);
1.879 + }
1.880 +
1.881 + GetIntFromConfig(ConfigSection(),KCancel,iCancel);
1.882 + }
1.883 +
1.884 +
1.885 +void CUpsDbStepUnitTest::OpenDecisionFileL(TFileMode aMode)
1.886 +/**
1.887 + Opens a log file which is used to write/read decision records.
1.888 + @param aMode File open mode. If EFileWrite, the existed file is replaced with new one.
1.889 + */
1.890 + {
1.891 + //The file server connection will be closed in the destructor
1.892 + User::LeaveIfError(iFs.CreatePrivatePath(EDriveC));
1.893 +
1.894 + User::LeaveIfError(iFs.PrivatePath(iDecisionFileName));
1.895 + iDecisionFileName.Append(KDecisionFileName);
1.896 +
1.897 + if((aMode & EFileWrite)== EFileWrite)
1.898 + {
1.899 + User::LeaveIfError(iFileWriteStream.Replace(iFs,iDecisionFileName,aMode));
1.900 + }
1.901 + else if((aMode & EFileRead)== EFileRead)
1.902 + {
1.903 + User::LeaveIfError(iFileReadStream.Open(iFs,iDecisionFileName,aMode));
1.904 + }
1.905 + //Note that the streams will be closed in the destructor
1.906 + }
1.907 +
1.908 +void CUpsDbStepUnitTest::WriteDecisionL(CDecisionRecord &aRecord)
1.909 +/**
1.910 + Writes a provided decision into the decision log file. This file is then used
1.911 + to verify the written records.
1.912 + */
1.913 + {
1.914 + iFileWriteStream.WriteInt32L(aRecord.iClientSid.iId);
1.915 + iFileWriteStream.WriteInt32L(aRecord.iEvaluatorId.iUid);
1.916 + iFileWriteStream.WriteInt32L(aRecord.iServiceId.iUid);
1.917 + iFileWriteStream.WriteInt32L(aRecord.iServerSid.iId);
1.918 + iFileWriteStream.WriteUint8L(aRecord.iResult);
1.919 + iFileWriteStream.WriteUint32L(aRecord.iEvaluatorInfo);
1.920 + iFileWriteStream.WriteUint16L(aRecord.iMajorPolicyVersion);
1.921 + iFileWriteStream.WriteUint32L(aRecord.iRecordId);
1.922 +
1.923 + iFileWriteStream.WriteInt16L(aRecord.iFingerprint.Length());
1.924 + iFileWriteStream.WriteL(aRecord.iFingerprint);
1.925 +
1.926 + if(aRecord.iClientEntity.Length())
1.927 + {
1.928 + iFileWriteStream.WriteInt16L(aRecord.iClientEntity.Length());
1.929 + iFileWriteStream.WriteL(aRecord.iClientEntity);
1.930 + }
1.931 + else
1.932 + {
1.933 + iFileWriteStream.WriteInt16L(4);
1.934 + iFileWriteStream.WriteL(_L8("NULL"));
1.935 + }
1.936 +
1.937 + if(aRecord.iDescription.Length())
1.938 + {
1.939 + iFileWriteStream.WriteInt16L(aRecord.iDescription.Length());
1.940 + iFileWriteStream.WriteL(aRecord.iDescription);
1.941 + }
1.942 + else
1.943 + {
1.944 + iFileWriteStream.WriteInt16L(4);
1.945 + iFileWriteStream.WriteL(_L("NULL"));
1.946 + }
1.947 +
1.948 + iFileWriteStream.CommitL();
1.949 + }
1.950 +
1.951 +
1.952 +CDecisionRecord *CUpsDbStepUnitTest::ReadDecisionL()
1.953 +/**
1.954 + Reads and returns a decision record from the log file. If reaches the file end, returns NULL.
1.955 + The records retrieved from the log file are compared with the records defined in the configuration file.
1.956 + */
1.957 + {
1.958 +
1.959 + TBuf8<KDbMaxName> fingerPrint;
1.960 + TBuf8<KDbMaxName> clientEntity;
1.961 + TBuf<KDbMaxName> description;
1.962 + TInt len;
1.963 + CDecisionRecord *record = NULL;
1.964 +
1.965 + TStreamPos currentPosition = iFileReadStream.Source()->TellL(MStreamBuf::ERead);
1.966 + TStreamPos endPosition = iFileReadStream.Source()->SeekL(MStreamBuf::ERead,EStreamEnd);
1.967 +
1.968 + if(currentPosition == endPosition)
1.969 + {
1.970 + INFO_PRINTF1(_L("Reached to the end of the decision file"));
1.971 + return record;
1.972 + }
1.973 +
1.974 + iFileReadStream.Source()->SeekL(MStreamBuf::ERead,currentPosition);
1.975 + CleanupClosePushL(iFileReadStream);//In first read a byte is allocated inside the stream, so push it
1.976 + TInt32 clientId = iFileReadStream.ReadInt32L();
1.977 + TInt32 evaluatorId = iFileReadStream.ReadInt32L();
1.978 + TInt32 serviceId = iFileReadStream.ReadInt32L();
1.979 + TInt32 serverId = iFileReadStream.ReadInt32L();
1.980 + TUint8 result = iFileReadStream.ReadUint8L();
1.981 + TUint32 evaluatorInfo = iFileReadStream.ReadUint32L();
1.982 + TUint16 policyMajorVersion = iFileReadStream.ReadUint16L();
1.983 + TUint32 recordId = iFileReadStream.ReadUint32L();
1.984 +
1.985 + len = iFileReadStream.ReadInt16L();
1.986 + iFileReadStream.ReadL(fingerPrint,len);
1.987 + len = iFileReadStream.ReadInt16L();
1.988 + iFileReadStream.ReadL(clientEntity,len);
1.989 + len = iFileReadStream.ReadInt16L();
1.990 + iFileReadStream.ReadL(description,len);
1.991 +
1.992 + if(!clientEntity.Compare(_L8("NULL")))
1.993 + {
1.994 + clientEntity.SetLength(0);
1.995 + }
1.996 +
1.997 + if(!description.Compare(_L("NULL")))
1.998 + {
1.999 + description.SetLength(0);
1.1000 + }
1.1001 +
1.1002 + //Create a decision record from readings
1.1003 + record = CreateRecordL((TInt)clientId,(TInt)evaluatorId,(TInt)serviceId,(TInt)serverId,fingerPrint,clientEntity,description,(TInt)result,(TInt)policyMajorVersion,(TInt)evaluatorInfo,(TInt)recordId);
1.1004 + CleanupStack::Pop(&iFileReadStream);
1.1005 + return record;
1.1006 + }
1.1007 +
1.1008 +void CUpsDbStepUnitTest::CloseDecisionFile()
1.1009 +/**
1.1010 + Closes the file server session and read/write stream objects
1.1011 + */
1.1012 + {
1.1013 + iFileReadStream.Close();
1.1014 + iFileWriteStream.Close();
1.1015 + }
1.1016 +