authorisation/userpromptservice/database/test/tupsdb/source/tupsdbstep.cpp
changeset 8 35751d3474b7
child 47 b54b58ee9d58
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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 * Performs decision filter, record and database tests
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @test
       
    23 */
       
    24 
       
    25 #include "tupsdbstep.h"
       
    26 #include <test/testexecutelog.h>
       
    27 
       
    28 using namespace UserPromptService;
       
    29 
       
    30 
       
    31 CUpsDbStepUnitTest::CUpsDbStepUnitTest()
       
    32 /**
       
    33 	Constructer
       
    34  */
       
    35 	{
       
    36 	iComparison = EEqual;
       
    37 	iCancel = EFalse;	
       
    38 	}
       
    39 	
       
    40 CUpsDbStepUnitTest::~CUpsDbStepUnitTest()
       
    41 /**
       
    42 	Destructor
       
    43  */
       
    44 	{
       
    45 	iFs.Close();
       
    46 	iFileWriteStream.Close();
       
    47 	iFileReadStream.Close();
       
    48 	delete iWaiter;
       
    49 	delete iScheduler;
       
    50 	}
       
    51 
       
    52 	
       
    53 TVerdict CUpsDbStepUnitTest::doTestStepPreambleL()
       
    54 /**
       
    55 	This function is called at the beginning of the test step. First reads the operation type
       
    56 	and converts it to a proper enumaration value (TUpsDbUnitTest). Then gets the specified 
       
    57 	database location. The test fails if the database location can not be found, except
       
    58 	the unit tests of the decision record and filter objects.
       
    59  */
       
    60 	{
       
    61 	iScheduler = new(ELeave) CActiveScheduler;
       
    62 	CActiveScheduler::Install(iScheduler);
       
    63 	iWaiter = new(ELeave)CActiveWaiter(Logger());
       
    64 	
       
    65 	iLog = EFalse;
       
    66 	
       
    67 	TPtrC opType;
       
    68 
       
    69 	if(EFalse == GetStringFromConfig(ConfigSection(),KOpType,opType))
       
    70 		{
       
    71 		ERR_PRINTF2(_L("Failed to read '%S' key of configuration"),&KOpType);
       
    72 		SetTestStepResult(EFail);
       
    73 		}
       
    74 	
       
    75 	if(!opType.CompareF(KInsertRecord))
       
    76 		{
       
    77 		iOpType = EInsertRecord;
       
    78 		}
       
    79 	else if(!opType.CompareF(KDeleteRecord))
       
    80 		{
       
    81 		iOpType = EDeleteRecord;
       
    82 		}
       
    83 	else if(!opType.CompareF(KSingleLookup))
       
    84 		{
       
    85 		iOpType = ESingleLookup;
       
    86 		}
       
    87 	else if(!opType.CompareF(KMultipleLookup))
       
    88 		{
       
    89 		iOpType = EMultipleLookup;
       
    90 		}
       
    91 	else if(!opType.CompareF(KVerifyRecord))
       
    92 		{
       
    93 		iOpType = EVerifyRecord;
       
    94 		}
       
    95 	else if(!opType.CompareF(KCreateDatabase))
       
    96 		{
       
    97 		iOpType = ECreateDatabase;
       
    98 		}
       
    99 	else if(!opType.CompareF(KDeleteDatabase))
       
   100 		{
       
   101 		iOpType = EDeleteDatabase;
       
   102 		}
       
   103 	else if(!opType.CompareF(KCreateFilterSingleCall))
       
   104 		{
       
   105 		iOpType = ECreateFilterSingleCall;
       
   106 		}
       
   107 	else if(!opType.CompareF(KCreateFilterMultipleCalls))
       
   108 		{
       
   109 		iOpType = ECreateFilterMultipleCalls;
       
   110 		}
       
   111 	else if(!opType.CompareF(KCreateDecision))
       
   112 		{
       
   113 		iOpType = ECreateDecision;
       
   114 		}
       
   115 	else if(!opType.CompareF(KFillTable))
       
   116 		{
       
   117 		iOpType = EFillTable;
       
   118 		}
       
   119 	else if(!opType.CompareF(KAllFingerprints))
       
   120 		{
       
   121 		iOpType = EAllFingerprints;
       
   122 		}
       
   123 	else if(!opType.CompareF(KCompactDatabase))
       
   124 		{
       
   125 		iOpType = ECompact;
       
   126 		}
       
   127 	else if(!opType.CompareF(KUpdateRecord))
       
   128 		{
       
   129 		iOpType = EUpdate;
       
   130 		}
       
   131 	else
       
   132 		{
       
   133 		iOpType = ENotSupported;
       
   134 		}
       
   135 	
       
   136 	if(EFalse == GetStringFromConfig(ConfigSection(),KDbLocation,iDbLocation))
       
   137 		{
       
   138 		if((iOpType != ECreateFilterSingleCall) && (iOpType != ECreateDecision) && 
       
   139 		   (iOpType != ECreateFilterMultipleCalls) && (iOpType != ENotSupported))
       
   140 			{
       
   141 			ERR_PRINTF2(_L("Failed to read '%S' key of configuration"),&KDbLocation);
       
   142 			SetTestStepResult(EFail);
       
   143 			User::Leave(KErrNotFound);
       
   144 			}
       
   145 		}
       
   146 	
       
   147 	GetBoolFromConfig(ConfigSection(), KOOMCondition,iOOMCondition);
       
   148 	
       
   149 	User::LeaveIfError(iFs.Connect());
       
   150 	User::LeaveIfError(iFs.CreatePrivatePath(EDriveC));			
       
   151 	return TestStepResult();
       
   152 	}
       
   153 
       
   154 
       
   155 TVerdict CUpsDbStepUnitTest::doTestStepL()
       
   156 /**
       
   157 	Calls actual test step or OOM test
       
   158  */
       
   159 	{
       
   160 	if (!iOOMCondition)
       
   161 		{
       
   162 		 doTestL(); 
       
   163 		}
       
   164 	else
       
   165 		{
       
   166  		return doOOMTestL();
       
   167 	    }	
       
   168 
       
   169    	 return TestStepResult();
       
   170 	}
       
   171 
       
   172 
       
   173 TVerdict CUpsDbStepUnitTest::doTestL()
       
   174 /**
       
   175 	Perform the actual test steps. Unit tests for all provided database operations
       
   176 	and creation of both filter and decision record objects.
       
   177  */
       
   178 	{
       
   179 	
       
   180 	//Define required objects
       
   181 	CDecisionDbW    *upsDb  = NULL;
       
   182 	CDecisionRecord *record = NULL;
       
   183 	CDecisionFilter *filter = NULL;
       
   184 	CleanupClosePushL(iFingerprint);
       
   185 	CleanupClosePushL(iClientEntity);
       
   186 		
       
   187 	switch(iOpType)
       
   188 		{
       
   189 		case ECreateDatabase:
       
   190 			{
       
   191 			//Make sure the database file does not exist
       
   192 			iFs.Delete(iDbLocation);
       
   193 			
       
   194 			//Create the database
       
   195 			upsDb = CreateDatabaseLC(iDbLocation, iFs);
       
   196 			
       
   197 			//Disconnect
       
   198 			CleanupStack::PopAndDestroy(upsDb);
       
   199 			break;
       
   200 			}
       
   201 		
       
   202 		case EDeleteDatabase:
       
   203 			{
       
   204 			//Connect to the database
       
   205 			upsDb = CDecisionDbW::NewL(iDbLocation, iFs);
       
   206 			CleanupStack::PushL(upsDb);
       
   207 			
       
   208 			//Delete all records and database
       
   209 			RemoveAllDecisionsL(*upsDb, iFs);
       
   210 			
       
   211 			//Disconnect
       
   212 			CleanupStack::PopAndDestroy(upsDb);
       
   213 			break;
       
   214 			}
       
   215 		
       
   216 		case ECreateFilterSingleCall:
       
   217 			{
       
   218 			//Read the decision values from the config file
       
   219 			ReadDecisionFieldsL(EFalse);
       
   220 			
       
   221 			TPtrC strExpectedResult;
       
   222 			if(EFalse == GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
       
   223 				{
       
   224 				ERR_PRINTF1(_L("Expected result is missing!"));
       
   225 				SetTestStepResult(EFail);
       
   226 				break;
       
   227 				}
       
   228 			TInt expectedResult = ConvertNumber(strExpectedResult);
       
   229 				
       
   230 			//First method is to create a filter with single function call	
       
   231 			TRAPD(error,filter = CDecisionFilter::NewL(TUid::Uid(iClientSid),TUid::Uid(iEvaluatorId),TUid::Uid(iServiceId),TUid::Uid(iServerSid),iFingerprint,iClientEntity,iMajorPolicyVersion));	
       
   232 			
       
   233 			if(error != expectedResult)
       
   234 				{
       
   235 				ERR_PRINTF3(_L("In the single call method, the result (%d) is different from the expected result (%d)"), error, expectedResult);
       
   236 				SetTestStepResult(EFail);
       
   237 				}
       
   238 				
       
   239 			if(error == KErrNone)
       
   240 				{
       
   241 				INFO_PRINTF1(_L("Successful to create a decision filter using the single call method"));
       
   242 				delete filter;
       
   243 				}
       
   244 			else
       
   245 				{
       
   246 				INFO_PRINTF1(_L("Failed to create a decision filter using the single call method"));
       
   247 				}
       
   248 				
       
   249 			break;
       
   250 			}
       
   251 		
       
   252 		case ECreateFilterMultipleCalls:
       
   253 			{
       
   254 			//Read the decision values from the config file
       
   255 			ReadDecisionFieldsL(EFalse);
       
   256 			
       
   257 			TPtrC strExpectedResult;
       
   258 			if(EFalse == GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
       
   259 				{
       
   260 				ERR_PRINTF1(_L("Expected result is missing!"));
       
   261 				SetTestStepResult(EFail);
       
   262 				break;
       
   263 				}
       
   264 			TInt expectedResult = ConvertNumber(strExpectedResult);
       
   265 				
       
   266 			//Second method is to create an empty filter and then set provided fields
       
   267 			TRAPD(error,filter = CreateFilterL(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison));		
       
   268 			
       
   269 				if(error != expectedResult)
       
   270 				{
       
   271 				ERR_PRINTF3(_L("In the multiple calls method, the result (%d) is different from the expected result (%d)"), error, expectedResult);
       
   272 				SetTestStepResult(EFail);
       
   273 				}
       
   274 				
       
   275 			if(error == KErrNone)
       
   276 				{
       
   277 				INFO_PRINTF1(_L("Successful to create a decision filter using the multiple calls method"));
       
   278 				delete filter;
       
   279 				}
       
   280 			else
       
   281 				{
       
   282 				INFO_PRINTF1(_L("Failed to create a decision filter using the multiple calls method"));
       
   283 				}
       
   284 				
       
   285 			break;
       
   286 			}
       
   287 			
       
   288 		case ECreateDecision:
       
   289 			{
       
   290 			//Read the decision values from the config file
       
   291 			ReadDecisionFieldsL(EFalse);
       
   292 			
       
   293 			TPtrC strExpectedResult;
       
   294 			if(EFalse == GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
       
   295 				{
       
   296 				ERR_PRINTF1(_L("Expected result is missing!"));
       
   297 				SetTestStepResult(EFail);
       
   298 				break;
       
   299 				}
       
   300 			TInt expectedResult = ConvertNumber(strExpectedResult);
       
   301 			
       
   302 			TRAPD(error,record = CreateRecordL(iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iDescription,iResult,iMajorPolicyVersion,iEvaluatorInfo,iRecordId));
       
   303 			
       
   304 			if(error != expectedResult)
       
   305 				{
       
   306 				ERR_PRINTF3(_L("The result (%d) is different from the expected result (%d)"), error, expectedResult);
       
   307 				SetTestStepResult(EFail);
       
   308 				}
       
   309 				
       
   310 				if(error == KErrNone)
       
   311 				{
       
   312 				INFO_PRINTF1(_L("Successful to create a decision record"));
       
   313 				delete record;
       
   314 				}
       
   315 			else
       
   316 				{
       
   317 				INFO_PRINTF1(_L("Failed to create a decision record"));
       
   318 				}
       
   319 				
       
   320 			break;
       
   321 			}
       
   322 			
       
   323 		case EInsertRecord:
       
   324 			{
       
   325 			//Read the decision values from the config file
       
   326 			ReadDecisionFieldsL(ETrue);
       
   327 			
       
   328 			//ClientSid,EvaluatorId,ServiceId,ServerSid and Fingerprint must be provided
       
   329 			if((iSetFlag & (KSetClientSid|KSetEvaluatorId|KSetServiceId|KSetServerSid|KSetFingerprint))!=
       
   330 			   (KSetClientSid|KSetEvaluatorId|KSetServiceId|KSetServerSid|KSetFingerprint))
       
   331 				{
       
   332 				SetTestStepResult(EFail);
       
   333 				break;
       
   334 				}
       
   335 				
       
   336 			//Create decision record
       
   337 			record = CreateRecordL(iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iDescription,iResult,iMajorPolicyVersion,iEvaluatorInfo,iRecordId);
       
   338 			CleanupStack::PushL(record);
       
   339 			
       
   340 			//Connect to the database
       
   341 			upsDb = CDecisionDbW::NewL(iDbLocation,iFs);
       
   342 			CleanupStack::PushL(upsDb);
       
   343 			
       
   344 			//Insert the decision
       
   345 			InsertRecordL(*upsDb, *record);
       
   346 			
       
   347 			//Disconnect and destroy objects
       
   348 			CleanupStack::PopAndDestroy(2,record);
       
   349 			
       
   350 			break;
       
   351 			}
       
   352 			
       
   353 		case EDeleteRecord:
       
   354 			{
       
   355 			//Read the decision values from the config file
       
   356 			ReadDecisionFieldsL(ETrue);
       
   357 		
       
   358 			if(!iSetFlag)
       
   359 				{
       
   360 				ERR_PRINTF1(_L("A filter key must be supplied at least!"));
       
   361 				SetTestStepResult(EFail);
       
   362 				break;
       
   363 				}
       
   364 								
       
   365 			//Create filter
       
   366 			filter = CreateFilterLC(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison);
       
   367 			
       
   368 			//Connect to the database
       
   369 			upsDb = CDecisionDbW::NewLC(iDbLocation,iFs);	
       
   370 			
       
   371 			//Delete the decision
       
   372 			RemoveDecisionsL(*upsDb, *filter);
       
   373 		
       
   374 			CleanupStack::PopAndDestroy(2,filter);
       
   375 			break;
       
   376 			}
       
   377 		
       
   378 		case ESingleLookup:
       
   379 			{
       
   380 			//Read the decision values from the config file
       
   381 			ReadDecisionFieldsL(ETrue);
       
   382 			
       
   383 			TInt expectedRecords;
       
   384 			if(EFalse == GetIntFromConfig(ConfigSection(),KExpectedRecords,expectedRecords))
       
   385 				{
       
   386 				ERR_PRINTF1(_L("Expected result is missing!"));
       
   387 				SetTestStepResult(EFail);
       
   388 				break;
       
   389 				}
       
   390 			
       
   391 			TPtrC strExpectedResult;
       
   392 			TInt expectedResult=KErrNone;
       
   393 			if(GetStringFromConfig(ConfigSection(),KExpectedResult,strExpectedResult))
       
   394 				{
       
   395 				expectedResult = ConvertNumber(strExpectedResult);
       
   396 				}
       
   397 				
       
   398 			//Create filter
       
   399 			filter = CreateFilterL(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison);
       
   400 			CleanupStack::PushL(filter);
       
   401 			
       
   402 			//Connect to the database
       
   403 			upsDb = CDecisionDbW::NewLC(iDbLocation,iFs);
       
   404 			
       
   405 			//Retrieve single record
       
   406 			TRAPD(err,record = GetDecisionLC(*upsDb, *filter);if(record)CleanupStack::Pop(record););
       
   407 			if((err==KErrNone)&&(record))
       
   408 				{
       
   409 				CleanupStack::PushL(record);
       
   410 				}
       
   411 			  
       
   412 			if(err!=expectedResult)
       
   413 				{
       
   414 				SetTestStepResult(EFail);
       
   415 				}
       
   416 				
       
   417 			if(record)
       
   418 				{
       
   419 				OpenDecisionFileL(EFileWrite);
       
   420 				WriteDecisionL(*record);
       
   421 				CloseDecisionFile();
       
   422 				CleanupStack::PopAndDestroy();
       
   423 				}
       
   424 			else
       
   425 				{
       
   426 				if(expectedRecords)
       
   427 					{
       
   428 					ERR_PRINTF1(_L("Failed to retrieve a single record"));
       
   429 					SetTestStepResult(EFail);
       
   430 					}
       
   431 				}
       
   432 				
       
   433 	
       
   434 			CleanupStack::PopAndDestroy(upsDb);
       
   435 			CleanupStack::PopAndDestroy(filter);
       
   436 			break;			
       
   437 			}
       
   438 		
       
   439 		case EMultipleLookup:
       
   440 			{
       
   441 			//Read the decision values from the config file
       
   442 			ReadDecisionFieldsL(ETrue);
       
   443 			
       
   444 			//Create filter
       
   445 			filter = CreateFilterL(iSetFlag,iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iMajorPolicyVersion,iRecordId,iDescription,iResult,iEvaluatorInfo,iComparison);
       
   446 			CleanupStack::PushL(filter);
       
   447 			
       
   448 			//Connect to the database
       
   449 			upsDb = CDecisionDbW::NewL(iDbLocation,iFs);
       
   450 			CleanupStack::PushL(upsDb);
       
   451 			
       
   452 			RPointerArray<CDecisionRecord> recordList = GetDecisionsL(*upsDb,*filter,iCancel);
       
   453 			
       
   454 			TInt recNum;
       
   455 			recNum = recordList.Count();
       
   456 			if(recNum)
       
   457 				{
       
   458 				CleanupClosePushL(recordList);
       
   459 				OpenDecisionFileL(EFileWrite);
       
   460 			
       
   461 				for(TInt i=0; i<recNum; ++i)
       
   462 					{
       
   463 					WriteDecisionL(*recordList[i]);
       
   464 					}
       
   465 				CloseDecisionFile();
       
   466 				CleanupStack::Pop(&recordList);
       
   467 				}
       
   468 			recordList.Close();
       
   469 				
       
   470 			TInt expectedRecords;
       
   471 			if(EFalse == GetIntFromConfig(ConfigSection(),KExpectedRecords,expectedRecords))
       
   472 				{
       
   473 				expectedRecords=-1;
       
   474 				}
       
   475 			
       
   476 			if(!iCancel && (((expectedRecords>=0) && (recNum != expectedRecords))||((expectedRecords<0) && (recNum<=0))))
       
   477 				{
       
   478 				SetTestStepResult(EFail);
       
   479 				}
       
   480 		
       
   481 			CleanupStack::PopAndDestroy(2,filter);
       
   482 			break;	
       
   483 			}
       
   484 			
       
   485 		case EVerifyRecord:
       
   486 			{
       
   487 			//Read the decision values from the config file
       
   488 			ReadDecisionFieldsL(ETrue);
       
   489 			TBool verified = ETrue;
       
   490 			
       
   491 			OpenDecisionFileL(EFileRead);
       
   492 				
       
   493 			while((record = ReadDecisionL()) != NULL)
       
   494 				{
       
   495 				CleanupStack::PushL(record);
       
   496 				
       
   497 				if(iSetFlag & KSetClientSid)
       
   498 					{
       
   499 					if(record->iClientSid != TSecureId(iClientSid))
       
   500 						{
       
   501 						verified = EFalse;
       
   502 						break;
       
   503 						}
       
   504 					}
       
   505 				
       
   506 				if(iSetFlag & KSetEvaluatorId)
       
   507 					{
       
   508 					if(record->iEvaluatorId != TUid::Uid(iEvaluatorId))
       
   509 						{
       
   510 						verified = EFalse;
       
   511 						break;
       
   512 						}
       
   513 					}
       
   514 				
       
   515 				if(iSetFlag & KSetServiceId)
       
   516 					{
       
   517 					if(record->iServiceId != TUid::Uid(iServiceId))
       
   518 						{
       
   519 						verified = EFalse;
       
   520 						break;
       
   521 						}
       
   522 					}
       
   523 					
       
   524 				if(iSetFlag & KSetServerSid)
       
   525 					{
       
   526 					if(record->iServerSid != TSecureId(iServerSid))
       
   527 						{
       
   528 						verified = EFalse;
       
   529 						break;
       
   530 						}
       
   531 					}
       
   532 				
       
   533 				if(iSetFlag & KSetFingerprint)
       
   534 					{
       
   535 					if(record->iFingerprint.Compare(iFingerprint))
       
   536 						{
       
   537 						verified = EFalse;
       
   538 						break;
       
   539 						}
       
   540 					}
       
   541 				
       
   542 				if(iSetFlag & KSetClientEntity)
       
   543 					{
       
   544 					if(record->iClientEntity.Compare(iClientEntity))
       
   545 						{
       
   546 						verified = EFalse;
       
   547 						break;
       
   548 						}
       
   549 					}
       
   550 				
       
   551 				if(iSetFlag & KSetDescription)
       
   552 					{
       
   553 					if(record->iDescription.Compare(iDescription))
       
   554 						{
       
   555 						verified = EFalse;
       
   556 						break;
       
   557 						}
       
   558 					}
       
   559 				
       
   560 				if(iSetFlag & KSetResult)
       
   561 					{
       
   562 					if(record->iResult != iResult)
       
   563 						{
       
   564 						verified = EFalse;
       
   565 						break;
       
   566 						}
       
   567 					}
       
   568 				
       
   569 				if(iSetFlag & KSetEvaluatorInfo)
       
   570 					{
       
   571 					if(record->iEvaluatorInfo != iEvaluatorInfo)
       
   572 						{
       
   573 						verified = EFalse;
       
   574 						break;
       
   575 						}
       
   576 					}
       
   577 				
       
   578 				if(iSetFlag & KSetMajorPolicyVersion)
       
   579 					{
       
   580 					if(record->iMajorPolicyVersion != iMajorPolicyVersion)
       
   581 						{
       
   582 						verified = EFalse;
       
   583 						break;
       
   584 						}
       
   585 					}
       
   586 				
       
   587 				if(iSetFlag & KSetRecordId)
       
   588 					{
       
   589 					if(record->iRecordId != iRecordId)
       
   590 						{
       
   591 						verified = EFalse;
       
   592 						break;
       
   593 						}
       
   594 					}
       
   595 						
       
   596 				CleanupStack::PopAndDestroy(record);						
       
   597 				}//while
       
   598 			
       
   599 			
       
   600 			if(verified == EFalse)
       
   601 				{
       
   602 				ERR_PRINTF1(_L("Retrieved record(s) is(are) different from the supplied one"));
       
   603 				SetTestStepResult(EFail);
       
   604 				CleanupStack::PopAndDestroy(record);
       
   605 				}
       
   606 			else
       
   607 				{
       
   608 				INFO_PRINTF1(_L("Retrieved record(s) have been verified successfully"));
       
   609 				}
       
   610 				
       
   611 			CloseDecisionFile();
       
   612 			
       
   613 			break;
       
   614 			}
       
   615 		
       
   616 		case EFillTable:
       
   617 			{
       
   618 			TBuf<KUpsDescriptionLength> keyNotFound;
       
   619 			ReadConfigurationFile(keyNotFound);
       
   620 			TInt len = keyNotFound.Length();
       
   621 			if(len > 0)
       
   622     			{
       
   623     			//if more than one keys could not read or retrieve_num is not in the list
       
   624     			if((len > 13) || (keyNotFound.Find(KRetrieveNum) == KErrNotFound))
       
   625     				{
       
   626     				ERR_PRINTF2(_L("Failed to read '%S' key of configuration"),&keyNotFound);
       
   627     				SetTestStepResult(EFail);
       
   628     				User::Leave(KErrNotFound);
       
   629     				}	
       
   630     			}
       
   631     		//Connect to the decision database
       
   632 			CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
       
   633 	
       
   634 			//Fill the table with a specified number of decision records	
       
   635 			FillTableL(*upsDb,iRecordNum,iClientNum,iServiceNum,iServerNum,iEvaluatorNum);
       
   636 			
       
   637 			//Disconnect
       
   638 			CleanupStack::PopAndDestroy(upsDb);
       
   639 			break;
       
   640 			}
       
   641 		
       
   642 		case EAllFingerprints:
       
   643 			{
       
   644 			//Connect to the decision database
       
   645 			CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
       
   646 			TestFingerprintValuesL(*upsDb);
       
   647 			//Disconnect
       
   648 			CleanupStack::PopAndDestroy(upsDb);
       
   649 			break;
       
   650 			}
       
   651 		
       
   652 		case ECompact:
       
   653 			{
       
   654 			ReadDecisionFieldsL(ETrue);
       
   655 			//Connect to the decision database
       
   656 			CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
       
   657 			CompactDatabaseL(*upsDb,iCancel);
       
   658 			//Disconnect
       
   659 			CleanupStack::PopAndDestroy(upsDb);
       
   660 			break;
       
   661 			}
       
   662 		
       
   663 		case EUpdate:
       
   664 			{
       
   665 			//Open the decision file containing the record which will be updated
       
   666 			OpenDecisionFileL(EFileRead);
       
   667 			//Read the record	
       
   668 			record = ReadDecisionL();
       
   669 			//Close the decision file
       
   670 			CloseDecisionFile();
       
   671 			//If we could not read the record, the test step fails
       
   672 			if(!record)
       
   673 				{
       
   674 				SetTestStepResult(EFail);
       
   675 				break;
       
   676 				}
       
   677 			CleanupStack::PushL(record);
       
   678 			
       
   679 			//Create filter from the record which has been read from the decision file
       
   680 			TInt flag = (KSetClientSid|KSetEvaluatorId|KSetServiceId|KSetServerSid|KSetFingerprint|KSetClientEntity|KSetMajorPolicyVersion);
       
   681 			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);
       
   682 			CleanupStack::PushL(filter);
       
   683 			
       
   684 			//Create a new file
       
   685 			ReadDecisionFieldsL(ETrue);
       
   686 			//Create a new record from the values in the configuration file
       
   687 			CDecisionRecord *newRecord = CreateRecordL(iClientSid,iEvaluatorId,iServiceId,iServerSid,iFingerprint,iClientEntity,iDescription,iResult,iMajorPolicyVersion,iEvaluatorInfo,iRecordId);
       
   688 			CleanupStack::PushL(newRecord);
       
   689 										
       
   690 			//Connect to the decision database
       
   691 			CDecisionDbW* upsDb = CreateDatabaseLC(iDbLocation,iFs);
       
   692 			//Update the record
       
   693 			UpdateRecordL(*upsDb, *filter, *newRecord);
       
   694 			
       
   695 			CleanupStack::PopAndDestroy(4, record);
       
   696 			
       
   697 			break;
       
   698 			}
       
   699 			
       
   700 		case ENotSupported:
       
   701 			{
       
   702 			ERR_PRINTF1(_L("The provided key is not supported!"));
       
   703 			SetTestStepResult(EFail);
       
   704 			break;
       
   705 			}
       
   706 		}
       
   707 		
       
   708 	CleanupStack::PopAndDestroy(2); //Pop RBufs	
       
   709 	return TestStepResult();
       
   710 	}
       
   711 
       
   712 
       
   713 TVerdict CUpsDbStepUnitTest::doOOMTestL()
       
   714 /**
       
   715 	Performs OOM test
       
   716  */
       
   717 	{
       
   718 	TVerdict verdict = EFail;
       
   719  	TInt countAfter = 0;
       
   720 	TInt countBefore = 0;
       
   721  	for (TInt oomCount = 0; ; oomCount++)
       
   722  		{
       
   723  		INFO_PRINTF2(_L("\n ==== Number of memory allocations %d ===="), oomCount);
       
   724  		verdict = EFail;
       
   725  		__UHEAP_RESET;
       
   726  		__UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
       
   727  		countBefore = User::CountAllocCells();
       
   728  		TRAPD(error, doTestL());// ----> This is the actual test that runs under OOM conditions.
       
   729  		countAfter = User::CountAllocCells();
       
   730  		__UHEAP_RESET;
       
   731  		
       
   732  		TESTL(error == KErrNone || error == KErrNoMemory);
       
   733  		
       
   734  		if (error == KErrNone)
       
   735  			{
       
   736  			verdict = EPass;
       
   737  			INFO_PRINTF2(_L("OOM Status %d"),error);
       
   738 			INFO_PRINTF1(_L("Test outcome : Passed"));
       
   739   			break;
       
   740  			}
       
   741  		else
       
   742  			{
       
   743  			if (countBefore != countAfter)
       
   744  				{
       
   745  				INFO_PRINTF2(_L("OOM Status %d"),error);
       
   746  				INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
       
   747  				SetTestStepResult(verdict);  
       
   748  				break;
       
   749  				}
       
   750  			}
       
   751  		INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
       
   752 		}
       
   753 	INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);
       
   754  	SetTestStepResult(verdict);
       
   755  	if (verdict==EFail)
       
   756 	 	{
       
   757  		User::Leave(KErrGeneral);	 		
       
   758 	 	}	 	
       
   759  	return verdict;
       
   760 	}
       
   761 	
       
   762 	
       
   763 TVerdict CUpsDbStepUnitTest::doTestStepPostambleL()
       
   764 /**
       
   765 	Runs at the end of the step. Do nothing.
       
   766  */
       
   767 	{
       
   768 	iFs.Close();
       
   769 	return TestStepResult();
       
   770 	}
       
   771 
       
   772 
       
   773 void CUpsDbStepUnitTest::ReadDecisionFieldsL(TBool aHash)
       
   774 /**
       
   775 	Reads the fileds required to create a decision record or filter.
       
   776 	
       
   777 	@param aHash Hashes fingerprint and client entity values if ETrue.
       
   778  */
       
   779 	{
       
   780 	if(GetHexFromConfig(ConfigSection(),KColClientSid,iClientSid))
       
   781 		{
       
   782 		iSetFlag |= KSetClientSid;
       
   783 		}
       
   784 		
       
   785 	if(GetHexFromConfig(ConfigSection(),KColEvaluatorId,iEvaluatorId))
       
   786 		{
       
   787 		iSetFlag |= KSetEvaluatorId;
       
   788 		}
       
   789 		
       
   790 	if(GetHexFromConfig(ConfigSection(),KColServiceId,iServiceId))
       
   791 		{
       
   792 		iSetFlag |= KSetServiceId;
       
   793 		}
       
   794 		
       
   795 	if(GetHexFromConfig(ConfigSection(),KColServerSid,iServerSid))
       
   796 		{
       
   797 		iSetFlag |= KSetServerSid;
       
   798 		}
       
   799 		
       
   800 	TPtrC string; //16-bit temporary pointer
       
   801 	CMD5 *md5 = CMD5::NewL();
       
   802 	CleanupStack::PushL(md5);
       
   803 		
       
   804 	if(GetStringFromConfig(ConfigSection(),KColFingerprint,string))
       
   805 		{
       
   806 		if(string.Length() > 0)
       
   807 			{
       
   808 			//Copy to convert 16-bit to 8-bit
       
   809 			iFingerprint.CreateL(string.Length()*2);
       
   810 			iFingerprint.Copy(string);
       
   811 			if(aHash)
       
   812 				{
       
   813 				TPtrC8 hashed = md5->Hash(iFingerprint);
       
   814 				if(string.Length()*2 < hashed.Length())
       
   815 					{
       
   816 					iFingerprint.ReAlloc(hashed.Length());
       
   817 					}
       
   818 				iFingerprint.Copy(hashed);
       
   819 				md5->Reset();
       
   820 				}
       
   821 			}
       
   822 		iSetFlag |= KSetFingerprint;
       
   823 		}
       
   824 			
       
   825 	if(GetStringFromConfig(ConfigSection(),KColClientEntity,string))
       
   826 		{
       
   827 		if(string.Length() > 0)
       
   828 			{
       
   829 			//Copy to convert 16-bit to 8-bit
       
   830 			iClientEntity.CreateL(string.Length()*2);
       
   831 			iClientEntity.Copy(string);
       
   832 			if(aHash)
       
   833 				{
       
   834 				TPtrC8 hashed = md5->Hash(iClientEntity);
       
   835 				if(string.Length()*2 < hashed.Length())
       
   836 					{
       
   837 					iClientEntity.ReAlloc(hashed.Length());
       
   838 					}
       
   839 				iClientEntity.Copy(hashed);
       
   840 				}
       
   841 			}
       
   842 		iSetFlag |= KSetClientEntity;
       
   843 		}
       
   844 	
       
   845 	CleanupStack::PopAndDestroy(md5);
       
   846 	
       
   847 	if(GetStringFromConfig(ConfigSection(),KColDescription,iDescription))
       
   848 		{
       
   849 		iSetFlag |= KSetDescription;	
       
   850 		}
       
   851 	
       
   852 	if(GetIntFromConfig(ConfigSection(),KColResult,iResult))
       
   853 		{
       
   854 		iSetFlag |= KSetResult;	
       
   855 		}
       
   856 					
       
   857 	if(GetIntFromConfig(ConfigSection(),KColEvaluatorInfo,iEvaluatorInfo))
       
   858 		{
       
   859 		iSetFlag |= KSetEvaluatorInfo;	
       
   860 		}
       
   861 	
       
   862 	if(GetIntFromConfig(ConfigSection(),KColMajorPolicyVersion,iMajorPolicyVersion))
       
   863 		{
       
   864 		iSetFlag |= KSetMajorPolicyVersion;
       
   865 		}
       
   866 	
       
   867 	if(GetIntFromConfig(ConfigSection(),KColRecordId,iRecordId))
       
   868 		{
       
   869 		iSetFlag |= KSetRecordId;
       
   870 		}
       
   871 	
       
   872 	TInt compOp;
       
   873 	if(GetHexFromConfig(ConfigSection(),KComparison,compOp))
       
   874 		{
       
   875 		iComparison = static_cast<TComparisonOp>(compOp);
       
   876 		}		
       
   877 	
       
   878 	GetIntFromConfig(ConfigSection(),KCancel,iCancel);
       
   879 	}
       
   880 
       
   881 
       
   882 void CUpsDbStepUnitTest::OpenDecisionFileL(TFileMode aMode)
       
   883 /**
       
   884 	Opens a log file which is used to write/read decision records.
       
   885 	@param aMode File open mode. If EFileWrite, the existed file is replaced with new one.
       
   886  */
       
   887 	{
       
   888 	//The file server connection will be closed in the destructor
       
   889 	User::LeaveIfError(iFs.CreatePrivatePath(EDriveC));
       
   890 	
       
   891 	User::LeaveIfError(iFs.PrivatePath(iDecisionFileName));
       
   892 	iDecisionFileName.Append(KDecisionFileName);
       
   893 	
       
   894 	if((aMode & EFileWrite)== EFileWrite)
       
   895 		{
       
   896 		User::LeaveIfError(iFileWriteStream.Replace(iFs,iDecisionFileName,aMode));
       
   897 		}
       
   898 	else if((aMode & EFileRead)== EFileRead)
       
   899 		{
       
   900 		User::LeaveIfError(iFileReadStream.Open(iFs,iDecisionFileName,aMode));
       
   901 		}
       
   902 	//Note that the streams will be closed in the destructor
       
   903 	}
       
   904 
       
   905 void CUpsDbStepUnitTest::WriteDecisionL(CDecisionRecord &aRecord)
       
   906 /**
       
   907 	Writes a provided decision into the decision log file. This file is then used
       
   908 	to verify the written records.
       
   909  */
       
   910 	{
       
   911 	iFileWriteStream.WriteInt32L(aRecord.iClientSid.iId);
       
   912 	iFileWriteStream.WriteInt32L(aRecord.iEvaluatorId.iUid);
       
   913 	iFileWriteStream.WriteInt32L(aRecord.iServiceId.iUid);
       
   914 	iFileWriteStream.WriteInt32L(aRecord.iServerSid.iId);
       
   915 	iFileWriteStream.WriteUint8L(aRecord.iResult);
       
   916 	iFileWriteStream.WriteUint32L(aRecord.iEvaluatorInfo);
       
   917 	iFileWriteStream.WriteUint16L(aRecord.iMajorPolicyVersion);
       
   918 	iFileWriteStream.WriteUint32L(aRecord.iRecordId);
       
   919 	
       
   920 	iFileWriteStream.WriteInt16L(aRecord.iFingerprint.Length());
       
   921 	iFileWriteStream.WriteL(aRecord.iFingerprint);
       
   922 	
       
   923 	if(aRecord.iClientEntity.Length())
       
   924 		{
       
   925 		iFileWriteStream.WriteInt16L(aRecord.iClientEntity.Length());
       
   926 		iFileWriteStream.WriteL(aRecord.iClientEntity);
       
   927 		}
       
   928 	else
       
   929 		{
       
   930 		iFileWriteStream.WriteInt16L(4);
       
   931 		iFileWriteStream.WriteL(_L8("NULL"));	
       
   932 		}
       
   933 	
       
   934 	if(aRecord.iDescription.Length())
       
   935 		{
       
   936 		iFileWriteStream.WriteInt16L(aRecord.iDescription.Length());
       
   937 		iFileWriteStream.WriteL(aRecord.iDescription);
       
   938 		}
       
   939 	else
       
   940 		{
       
   941 		iFileWriteStream.WriteInt16L(4);
       
   942 		iFileWriteStream.WriteL(_L("NULL"));
       
   943 		}
       
   944 	
       
   945 	iFileWriteStream.CommitL();
       
   946 	}
       
   947 	
       
   948 	
       
   949 CDecisionRecord *CUpsDbStepUnitTest::ReadDecisionL()
       
   950 /**
       
   951 	Reads and returns a decision record from the log file. If reaches the file end, returns NULL.
       
   952 	The records retrieved from the log file are compared with the records defined in the configuration file.
       
   953  */
       
   954 	{	
       
   955 	
       
   956 	TBuf8<KDbMaxName> fingerPrint;
       
   957 	TBuf8<KDbMaxName> clientEntity;
       
   958 	TBuf<KDbMaxName> description;
       
   959 	TInt len;
       
   960 	CDecisionRecord *record = NULL;
       
   961 	
       
   962 	TStreamPos currentPosition = iFileReadStream.Source()->TellL(MStreamBuf::ERead);
       
   963 	TStreamPos endPosition = iFileReadStream.Source()->SeekL(MStreamBuf::ERead,EStreamEnd);
       
   964 	
       
   965 	if(currentPosition == endPosition)
       
   966 		{
       
   967 		INFO_PRINTF1(_L("Reached to the end of the decision file"));
       
   968 		return record;
       
   969 		}
       
   970 			
       
   971 	iFileReadStream.Source()->SeekL(MStreamBuf::ERead,currentPosition);
       
   972 	CleanupClosePushL(iFileReadStream);//In first read a byte is allocated inside the stream, so push it
       
   973 	TInt32  clientId 	  		= iFileReadStream.ReadInt32L();
       
   974 	TInt32  evaluatorId   		= iFileReadStream.ReadInt32L();
       
   975 	TInt32  serviceId 	  		= iFileReadStream.ReadInt32L();
       
   976 	TInt32  serverId	  		= iFileReadStream.ReadInt32L();
       
   977 	TUint8  result		  		= iFileReadStream.ReadUint8L();
       
   978 	TUint32 evaluatorInfo 		= iFileReadStream.ReadUint32L();
       
   979 	TUint16 policyMajorVersion 	= iFileReadStream.ReadUint16L();
       
   980 	TUint32 recordId 	  		= iFileReadStream.ReadUint32L();
       
   981 	
       
   982 	len = iFileReadStream.ReadInt16L();
       
   983 	iFileReadStream.ReadL(fingerPrint,len);
       
   984 	len = iFileReadStream.ReadInt16L();
       
   985 	iFileReadStream.ReadL(clientEntity,len);
       
   986 	len = iFileReadStream.ReadInt16L();
       
   987 	iFileReadStream.ReadL(description,len);
       
   988 	
       
   989 	if(!clientEntity.Compare(_L8("NULL")))
       
   990 		{
       
   991 		clientEntity.SetLength(0);
       
   992 		}
       
   993 		
       
   994 	if(!description.Compare(_L("NULL")))
       
   995 		{
       
   996 		description.SetLength(0);
       
   997 		}
       
   998 	
       
   999 	//Create a decision record from readings
       
  1000 	record = CreateRecordL((TInt)clientId,(TInt)evaluatorId,(TInt)serviceId,(TInt)serverId,fingerPrint,clientEntity,description,(TInt)result,(TInt)policyMajorVersion,(TInt)evaluatorInfo,(TInt)recordId);
       
  1001 	CleanupStack::Pop(&iFileReadStream);	
       
  1002 	return record;
       
  1003 	}
       
  1004 	
       
  1005 void CUpsDbStepUnitTest::CloseDecisionFile()
       
  1006 /**
       
  1007 	Closes the file server session and read/write stream objects
       
  1008  */
       
  1009 	{
       
  1010 	iFileReadStream.Close();
       
  1011 	iFileWriteStream.Close();
       
  1012 	}
       
  1013