os/security/authorisation/userpromptservice/test/tups/src/tupsdbmanagementstep.cpp
Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "tupsdbmanagementstep.h"
20 #include "tupsintegdefs.h"
21 #include <ups/upstypes.h>
23 using namespace UserPromptService;
25 _LIT(KOperationCreateView, "CreateView");
26 _LIT(KOperationCancelCloseView, "CancelCloseView");
27 _LIT(KOperationNextMatch, "NextMatch");
28 _LIT(KOperationRemoveDecision, "RemoveDecision");
29 _LIT(KOperationUpdateDecisionAllow, "UpdateDecisionAllow");
30 _LIT(KOperationUpdateDecisionDeny, "UpdateDecisionDeny");
31 _LIT(KOperationCancelUpdate, "CancelUpdate");
32 _LIT(KOperationDeleteDatabase, "DeleteDatabase");
33 _LIT(KOperationDecisionCount, "DecisionCount");
36 CUpsDbRequest* CUpsDbRequest::NewL()
38 CUpsDbRequest* me = new (ELeave) CUpsDbRequest();
39 CleanupStack::PushL(me);
41 CleanupStack::Pop(me);
45 CUpsDbRequest::~CUpsDbRequest()
47 delete iDecisionFilter;
50 CUpsDbRequest::CUpsDbRequest()
51 :iOperation(0), iClientSid(0), iEvaluatorId(0), iServiceId(0), iServerSid(0), iFingerprint(0),
52 iClientEntity(0), iDescription(0), iDecisionResult(0), iMajorPolicyVersion(0), iRecordId(0), iEvaluatorInfo(0), iExpectedDecisionCount(0)
56 void CUpsDbRequest::ConstructL()
58 iDecisionFilter = CDecisionFilter::NewL();
61 CUPSDbManagementStep::CUPSDbManagementStep()
63 SetTestStepName(KUPSDbManagementStep);
66 CUPSDbManagementStep::~CUPSDbManagementStep()
68 iUpsManager.CancelAndCloseView();
70 iArraySersToRequest.ResetAndDestroy();
71 INFO_PRINTF2(_L("END CELLS: %d"), User::CountAllocCells());
75 TVerdict CUPSDbManagementStep::doTestStepPreambleL()
77 * @return - TVerdict code
78 * Override of base class virtual
83 INFO_PRINTF2(_L("START CELLS: %d"), User::CountAllocCells());
85 // reads client name and SID
86 TParse clientFullName;
88 clientFullName.Set(client.FullName(),NULL, NULL);
89 iTEFServerName=clientFullName.Name();
90 iExpectedClientSid = client.SecureId() ;
93 // Read how many times the test step needs to be repeated.
94 TName fStepRepeat(_L("StepRepeat"));
97 if(GetIntFromConfig(ConfigSection(),fStepRepeat,repeats))
106 // Read values for test sequence from INI file. (ARRAY of values)
109 fOperation.Format(_L("Operation_%d"), index);
111 fClientSid.Format(_L("ClientSid_%d"),index);
113 fEvaluatorId.Format(_L("EvaluatorId_%d"),index);
115 fServiceId.Format(_L("ServiceId_%d"),index);
117 fServerSid.Format(_L("ServerSid_%d"),index);
119 fFingerprint.Format(_L("Fingerprint_%d"),index);
121 fClientEntity.Format(_L("ClientEntity_%d"),index);
123 fDescription.Format(_L("Description_%d"),index);
124 TName fDecisionResult;
125 fDecisionResult.Format(_L("DecisionResult_%d"),index);
126 TName fMajorPolicyVersion;
127 fMajorPolicyVersion.Format(_L("MajorPolicyVersion_%d"),index);
129 fRecordId.Format(_L("RecordId_%d"),index);
130 TName fEvaluatorInfo;
131 fEvaluatorInfo.Format(_L("EvaluatorInfo_%d"),index);
132 TName fExpectedDecisionCount;
133 fExpectedDecisionCount.Format(_L("ExpectedDecisionCount_%d"),index);
143 TPtrC decisionResult;
144 TInt majorPolicyVersion;
147 TInt expectedDecisionCount;
149 while (GetStringFromConfig(ConfigSection(),fOperation,operation))
152 // Create an instance of a new request
153 CUpsDbRequest* newRequest = CUpsDbRequest::NewL();
154 CleanupStack::PushL(newRequest);
156 // Set the operation to be performed
157 newRequest->iOperation = operation;
159 if(GetHexFromConfig(ConfigSection(),fClientSid,clientSid))
161 newRequest->iClientSid = clientSid;
162 newRequest->iDecisionFilter->SetClientSid(TSecureId(clientSid),EEqual);
165 if(GetHexFromConfig(ConfigSection(),fEvaluatorId,evaluatorId))
167 newRequest->iEvaluatorId = evaluatorId;
168 newRequest->iDecisionFilter->SetEvaluatorId(TUid::Uid(evaluatorId),EEqual);
171 if(GetHexFromConfig(ConfigSection(),fServiceId,serviceId))
173 newRequest->iServiceId = serviceId;
174 newRequest->iDecisionFilter->SetServiceId(TUid::Uid(serviceId),EEqual);
177 if(GetHexFromConfig(ConfigSection(),fServerSid,serverSid))
179 newRequest->iServerSid = serverSid;
180 newRequest->iDecisionFilter->SetServerSid(TSecureId(serverSid),EEqual);
183 if(GetStringFromConfig(ConfigSection(),fFingerprint,fingerprint))
185 HBufC8* fingerprintConverter = HBufC8::NewLC(fingerprint.Length());
186 TPtr8 fingerprintPtr = fingerprintConverter->Des();
187 fingerprintPtr.Copy(fingerprint);
189 newRequest->iFingerprint = fingerprintPtr;
191 HBufC8* binaryFingerprint = StringToBinaryLC(fingerprintPtr);
193 newRequest->iDecisionFilter->SetFingerprintL(*binaryFingerprint,EEqual);
194 CleanupStack::PopAndDestroy(binaryFingerprint);
195 CleanupStack::PopAndDestroy(fingerprintConverter);
198 if(GetStringFromConfig(ConfigSection(),fClientEntity,clientEntity))
200 HBufC8* clientEntityConverter = HBufC8::NewLC(clientEntity.Length());
201 TPtr8 clientEntityPtr = clientEntityConverter->Des();
202 clientEntityPtr.Copy(clientEntity);
204 newRequest->iClientEntity = clientEntityPtr;
205 newRequest->iDecisionFilter->SetClientEntityL(clientEntityPtr,EEqual);
206 CleanupStack::PopAndDestroy(clientEntityConverter);
209 if(GetStringFromConfig(ConfigSection(),fDescription,description))
211 newRequest->iDescription = description;
214 if(GetStringFromConfig(ConfigSection(),fDecisionResult,decisionResult))
216 if(decisionResult.CompareF(_L("Yes"))==0 || decisionResult.CompareF(_L("No"))==0 || decisionResult.CompareF(_L(""))==0 )
218 newRequest->iDecisionResult = decisionResult;
222 ERR_PRINTF3(_L("%S: Unexpected Decision Result - %S"),&iTEFServerName, &decisionResult);
226 if(GetIntFromConfig(ConfigSection(),fMajorPolicyVersion,majorPolicyVersion))
228 newRequest->iMajorPolicyVersion = majorPolicyVersion;
229 newRequest->iDecisionFilter->SetMajorPolicyVersion(majorPolicyVersion,EEqual);
232 if(GetIntFromConfig(ConfigSection(),fRecordId,recordId))
234 newRequest->iRecordId = recordId;
235 newRequest->iDecisionFilter->SetRecordId(recordId,EEqual);
238 if(GetIntFromConfig(ConfigSection(),fEvaluatorInfo,evaluatorInfo))
240 newRequest->iEvaluatorInfo = evaluatorInfo;
243 if(GetIntFromConfig(ConfigSection(),fExpectedDecisionCount,expectedDecisionCount))
245 newRequest->iExpectedDecisionCount = expectedDecisionCount;
248 // Add the new service to be requested to array.
249 iArraySersToRequest.AppendL(newRequest);
251 CleanupStack::Pop(newRequest);
254 fOperation.Format(_L("Operation_%d"), index);
255 fClientSid.Format(_L("ClientSid_%d"),index);
256 fEvaluatorId.Format(_L("EvaluatorId_%d"),index);
257 fServiceId.Format(_L("ServiceId_%d"),index);
258 fServerSid.Format(_L("ServerSid_%d"),index);
259 fFingerprint.Format(_L("Fingerprint_%d"),index);
260 fClientEntity.Format(_L("ClientEntity_%d"),index);
261 fDescription.Format(_L("Description_%d"),index);
262 fDecisionResult.Format(_L("DecisionResult_%d"),index);
263 fMajorPolicyVersion.Format(_L("MajorPolicyVersion_%d"),index);
264 fRecordId.Format(_L("RecordId_%d"),index);
265 fEvaluatorInfo.Format(_L("EvaluatorInfo_%d"),index);
266 fExpectedDecisionCount.Format(_L("ExpectedDecisionCount_%d"),index);
269 SetTestStepResult(EPass);
270 return TestStepResult();
273 TVerdict CUPSDbManagementStep::doTestStepL()
275 // Connect to the UPS Manager
276 User::LeaveIfError(iUpsManager.Connect());
278 // loop to request for different test services.
279 SetTestStepResult(EPass);
280 TInt numServicesToRequest = iArraySersToRequest.Count();
282 if(numServicesToRequest > 0)
284 // repeats the sequence of test services request as many times as specified in iStepRepeat
285 for(TInt repeatTimes = 0;repeatTimes<iStepRepeat;++repeatTimes)
288 INFO_PRINTF3(_L("%S: Repeat number: %d"),&iTEFServerName,repeatTimes);
290 for(TInt index=0;index < numServicesToRequest; ++index )
293 INFO_PRINTF4(_L("%S: Database Request: %d, Command: %S"),&iTEFServerName, index, &iArraySersToRequest[index]->iOperation);
295 TRequestStatus requestStatus;
297 TPtrC op(iArraySersToRequest[index]->iOperation);
299 // *** CREATE VIEW ***
300 if (!op.CompareF(KOperationCreateView))
302 // Create view using the Decision Filter for the Request
303 iUpsManager.CreateView(*iArraySersToRequest[index]->iDecisionFilter,requestStatus);
305 User::WaitForRequest(requestStatus);
307 if(requestStatus != KErrNone)
309 SetTestStepResult(EFail);
310 ERR_PRINTF3(_L("%S: Create View Failure - %d"),&iTEFServerName,requestStatus.Int());
311 User::Leave(requestStatus.Int());
314 // *** CANCEL CLOSE VIEW ***
315 else if (!op.CompareF(KOperationCancelCloseView))
317 iUpsManager.CancelAndCloseView();
319 INFO_PRINTF2(_L("%S: UPS DB Management - Current View Cancelled and Closed"),&iTEFServerName);
321 // *** NEXT MATCH ***
322 else if (!op.CompareF(KOperationNextMatch))
324 CDecisionRecord* record = NULL;
326 // Retrieve the Next Decision according to the Current View
327 record = iUpsManager.NextMatchL();
328 CleanupStack::PushL(record);
330 // If a record is found, check that the record matches any data specified in the INI
333 if(CheckDecisionRecordL(index,*record) == EFalse)
335 iUpsManager.CancelAndCloseView();
336 ERR_PRINTF2(_L("%S: Incorrect Results"),&iTEFServerName);
337 User::Leave(KErrGeneral);
342 iUpsManager.CancelAndCloseView();
343 SetTestStepResult(EFail);
344 ERR_PRINTF2(_L("%S: NextMatchL - No Decision Retrieved"),&iTEFServerName);
345 User::Leave(KErrEof);
348 CleanupStack::PopAndDestroy(record);
351 // *** REMOVE DECISION ***
352 else if (!op.CompareF(KOperationRemoveDecision))
354 // Remove decisions from the database based on the contents of the Decision Filter
355 TRAPD(err, iUpsManager.RemoveDecisionsL(*iArraySersToRequest[index]->iDecisionFilter));
359 INFO_PRINTF2(_L("%S: Remove Decision - Decision(s) Successfully Removed"),&iTEFServerName);
363 SetTestStepResult(EFail);
364 ERR_PRINTF3(_L("%S: ** Error ** Remove Decision Failure - %d"),&iTEFServerName,err);
368 // *** UPDATE DECISION ALLOW ***
369 else if (!op.CompareF(KOperationUpdateDecisionAllow))
371 iUpsManager.UpdateDecision(iArraySersToRequest[index]->iRecordId, ETrue, requestStatus);
373 User::WaitForRequest(requestStatus);
375 if(requestStatus != KErrNone)
377 SetTestStepResult(EFail);
378 ERR_PRINTF3(_L("%S: Update Decision 'Allow' Failure - %d"),&iTEFServerName,requestStatus.Int());
379 User::Leave(requestStatus.Int());
382 // *** UPDATE DECISION DENY ***
383 else if (!op.CompareF(KOperationUpdateDecisionDeny))
385 iUpsManager.UpdateDecision(iArraySersToRequest[index]->iRecordId, EFalse, requestStatus);
387 User::WaitForRequest(requestStatus);
389 if(requestStatus != KErrNone)
391 SetTestStepResult(EFail);
392 ERR_PRINTF3(_L("%S: Update Decision 'Deny' Failure - %d"),&iTEFServerName,requestStatus.Int());
393 User::Leave(requestStatus.Int());
396 // *** CANCEL DECISION UPDATE ***
397 else if (!op.CompareF(KOperationCancelUpdate))
399 iUpsManager.CancelUpdateDecision();
401 INFO_PRINTF2(_L("%S: UPS DB Management - Current Decision Update Cancelled"),&iTEFServerName);
403 // *** DELETE DATABASE ***
404 else if (!op.CompareF(KOperationDeleteDatabase))
406 // Delete the contents of the Decision Database
407 TRAPD(err, iUpsManager.DeleteDatabaseL());
411 INFO_PRINTF2(_L("%S: UPS DB Management - Database Deleted"),&iTEFServerName);
415 SetTestStepResult(EFail);
416 ERR_PRINTF3(_L("%S: ** Error ** Database Deletion Failure - %d"),&iTEFServerName,err);
420 // *** DECISION COUNT ***
421 else if (!op.CompareF(KOperationDecisionCount))
423 iUpsManager.CancelAndCloseView();
425 // Create a new view using the constructed decision filter for the request
426 iUpsManager.CreateView(*iArraySersToRequest[index]->iDecisionFilter,requestStatus);
428 User::WaitForRequest(requestStatus);
430 if(requestStatus.Int() == KErrNone)
432 TInt decisionCount = 0;
433 CDecisionRecord* record = NULL;
435 TInt result = KErrNone;
437 // Whilst records located using the "NextMatchL" method, increment the record count
438 while(result == KErrNone)
440 TRAP(result, record = iUpsManager.NextMatchL());
455 iUpsManager.CancelAndCloseView();
457 if(decisionCount != iArraySersToRequest[index]->iExpectedDecisionCount)
459 SetTestStepResult(EFail);
460 ERR_PRINTF2(_L("%S: Decision Count - Unexpected Number of Database Decisions"),&iTEFServerName);
461 ERR_PRINTF3(_L("%S: Decisions expected: %d"),&iTEFServerName,iArraySersToRequest[index]->iExpectedDecisionCount);
462 ERR_PRINTF3(_L("%S: Decisions in Database: %d"),&iTEFServerName,decisionCount);
463 User::Leave(KErrNotFound);
469 SetTestStepResult(EFail);
470 ERR_PRINTF3(_L("%S: Decision Count - Create View Failure - %d"),&iTEFServerName,requestStatus.Int());
471 User::Leave(requestStatus.Int());
476 SetTestStepResult(EFail);
477 ERR_PRINTF2(_L("%S: Invalid operation"),&iTEFServerName);
478 User::Leave(KErrNotSupported);
481 } // End of second loop
482 }// End of first loop
487 SetTestStepResult(EFail);
488 ERR_PRINTF2(_L("%S: Problem reading ini file"),&iTEFServerName);
489 User::Leave(KErrGeneral);
492 return TestStepResult();
496 TVerdict CUPSDbManagementStep::doTestStepPostambleL()
498 return TestStepResult();
502 TBool CUPSDbManagementStep::CheckDecisionRecordL(TInt aIndex, const CDecisionRecord& aRecord)
504 * Checks the expected record values against those stored within CDecisionRecord retrieved from the database
506 * @param - TInt aIndex: Database request index used to access the required instance of CUpsDbRequest held
507 * within the request array
508 * @param - const CDecisionRecord& aRecord: Decision Record retrieved from the database
509 * @return - TBool: Boolean to determine the result of the value checks (ETrue = Pass, EFalse = Fail)
513 TBool checkPassed = ETrue;
516 if(iArraySersToRequest[aIndex]->iClientSid != aRecord.iClientSid)
518 ERR_PRINTF2(_L("%S: 'Client SID' value mismatch"),&iTEFServerName);
519 checkPassed = EFalse;
522 if(iArraySersToRequest[aIndex]->iEvaluatorId != aRecord.iEvaluatorId.iUid)
524 ERR_PRINTF2(_L("%S: 'Evaluator ID' value mismatch"),&iTEFServerName);
525 checkPassed = EFalse;
528 if(iArraySersToRequest[aIndex]->iServiceId != aRecord.iServiceId.iUid)
530 ERR_PRINTF2(_L("%S: 'Service ID' value mismatch"),&iTEFServerName);
531 checkPassed = EFalse;
534 if(iArraySersToRequest[aIndex]->iServerSid != aRecord.iServerSid)
536 ERR_PRINTF2(_L("%S: 'Server SID' value mismatch"),&iTEFServerName);
537 checkPassed = EFalse;
540 HBufC8* recordFingerprint = StringToHexLC(aRecord.iFingerprint);
542 if(iArraySersToRequest[aIndex]->iFingerprint != *recordFingerprint)
544 HBufC* expectedFingerprint = HBufC::NewLC(iArraySersToRequest[aIndex]->iFingerprint.Length());
545 HBufC* receivedFingerprint = HBufC::NewLC(recordFingerprint->Length());
547 expectedFingerprint->Des().Copy(iArraySersToRequest[aIndex]->iFingerprint);
548 receivedFingerprint->Des().Copy(*recordFingerprint);
550 ERR_PRINTF2(_L("%S: 'Fingerprint' value mismatch."),&iTEFServerName);
551 ERR_PRINTF3(_L("%S: Fingerprint expected: %S"),&iTEFServerName,expectedFingerprint);
552 ERR_PRINTF3(_L("%S: Fingerprint received: %S"),&iTEFServerName,receivedFingerprint);
554 CleanupStack::PopAndDestroy(receivedFingerprint);
555 CleanupStack::PopAndDestroy(expectedFingerprint);
556 checkPassed = EFalse;
559 CleanupStack::PopAndDestroy(recordFingerprint);
561 if(iArraySersToRequest[aIndex]->iClientEntity != aRecord.iClientEntity)
563 HBufC* expectedClientEntity = HBufC::NewLC(iArraySersToRequest[aIndex]->iClientEntity.Length());
564 HBufC* receivedClientEntity = HBufC::NewLC(aRecord.iClientEntity.Length());
566 expectedClientEntity->Des().Copy(iArraySersToRequest[aIndex]->iClientEntity);
567 receivedClientEntity->Des().Copy(aRecord.iClientEntity);
569 ERR_PRINTF2(_L("%S: 'Client Entity' value mismatch."),&iTEFServerName);
571 CleanupStack::PopAndDestroy(receivedClientEntity);
572 CleanupStack::PopAndDestroy(expectedClientEntity);
573 checkPassed = EFalse;
576 if(iArraySersToRequest[aIndex]->iDescription.CompareF(aRecord.iDescription) != 0)
578 TPtrC retrievedDescription(aRecord.iDescription);
579 ERR_PRINTF2(_L("%S: 'Description' value mismatch."),&iTEFServerName);
580 checkPassed = EFalse;
583 TInt expectedDecisionResult = 0;
585 if(iArraySersToRequest[aIndex]->iDecisionResult.CompareF(_L("Yes")) == 0)
587 expectedDecisionResult = 1;
590 if(expectedDecisionResult != aRecord.iResult)
592 ERR_PRINTF2(_L("%S: 'Decision Result' value mismatch."),&iTEFServerName);
593 checkPassed = EFalse;
596 if(iArraySersToRequest[aIndex]->iMajorPolicyVersion != aRecord.iMajorPolicyVersion)
598 ERR_PRINTF2(_L("%S: 'Major Policy Version' value mismatch."),&iTEFServerName);
599 checkPassed = EFalse;
602 if(iArraySersToRequest[aIndex]->iEvaluatorInfo != aRecord.iEvaluatorInfo)
604 ERR_PRINTF2(_L("%S: 'Evaluator Info' value mismatch."),&iTEFServerName);
605 checkPassed = EFalse;
608 if(iArraySersToRequest[aIndex]->iRecordId != aRecord.iRecordId)
610 ERR_PRINTF2(_L("%S: 'Record ID' value mismatch."),&iTEFServerName);
611 checkPassed = EFalse;
619 HBufC8* CUPSDbManagementStep::StringToHexLC(const TDes8 &aString)
621 * Function to convert the contents of a TDes8 into a Hexadecimal format
623 * @param - cosnt TDes8 aString: String to convert into Hexadecimal
624 * @return - HBufC8*: Converted Hexadecial string representation
627 HBufC8* parsedString = HBufC8::NewLC(aString.Length()*2);
629 _LIT8(KFormatHex,"%02x");
631 TPtr8 ptr(parsedString->Des());
632 for(TInt i = 0; i<aString.Length() ; i++)
634 TInt num = (aString)[i];
635 pair.Format(KFormatHex,num);
643 HBufC8* CUPSDbManagementStep::StringToBinaryLC(const TDes8 &aString)
645 * Function to convert the contents of a TDes8 into a Binary format
647 * @param - cosnt TDes8 aString: String to convert into Hexadecimal
648 * @return - HBufC8*: Converted Binary string representation
651 HBufC8* parsedString = HBufC8::NewLC(aString.Length()/2);
653 _LIT8(KFormatBinary,"%c");
655 TPtr8 ptr(parsedString->Des());
656 for(TInt i = 0; i<aString.Length()/2 ; i++)
658 TPtrC8 tempPtr(aString.Mid(i*2,2));
662 binChar.Format(KFormatBinary,val);