os/security/authorisation/userpromptservice/database/test/tupsdb/source/tupsdbbase.cpp
First public contribution.
2 * Copyright (c) 2007-2010 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.
15 * Implements basic database test functions.
25 #include "tupsdbstep.h"
26 #include <test/testexecutelog.h>
28 using namespace UserPromptService;
35 CUpsDbBaseStep::CUpsDbBaseStep()
43 CUpsDbBaseStep::~CUpsDbBaseStep()
52 CDecisionFilter *CUpsDbBaseStep::CreateFilterL(TInt aFlag, TInt aClientSid, TInt aEvaluatorId, TInt aServiceId, TInt aServerSid, TDesC8 &aFingerprint, TDesC8& aClientEntity,TInt aMajorPolicyVersion, TInt aRecordId, TDesC& aDescription, TInt aResult, TInt aEvaluatorInfo, TComparisonOp aComparisonOp)
54 Creates an empty filter object and then passes it to SetFilterFiledsL to get assigned all filter keys separately.
55 This function is used to test NewL method of CDecisionFilter
57 @param aFlag Shows which filter keys have been set
58 @param aClientSid Secure Id of client application
59 @param aEvaluatorId UID for the policy evaluator DLL
60 @param aServiceId UID for service
61 @param aServerSid UID for the system server
62 @param aFingerprint Hash of the destination and/or opaque data.
63 @param aClientEntity The name of the entity within the client that requested the service.
64 @param aMajorPolicyVersion The major version of the policy file.
65 @param aRecordId An auto-incrementing record number.
66 @param aDescription A copy of description and/or opaque
67 @param aResult Whether the request should be approved
68 @param aEvaluatorInfo Policy evaluator specific data
69 @param aComparisonOp Comparison Operator (=,!=,<,> etc.)
71 @return A pointer to the newly allocated filter object.
74 //Create an empty filter
75 CDecisionFilter *filter = CDecisionFilter::NewL();
76 CleanupStack::PushL(filter);
78 SetFilterFiledsL(*filter, aFlag, aClientSid, aEvaluatorId, aServiceId, aServerSid,aFingerprint, aClientEntity, aMajorPolicyVersion, aRecordId, aDescription, aResult, aEvaluatorInfo, aComparisonOp);
80 CleanupStack::Pop(filter);
85 CDecisionFilter *CUpsDbBaseStep::CreateFilterLC(TInt aFlag, TInt aClientSid, TInt aEvaluatorId, TInt aServiceId, TInt aServerSid, TDesC8 &aFingerprint, TDesC8& aClientEntity,TInt aMajorPolicyVersion, TInt aRecordId, TDesC& aDescription, TInt aResult, TInt aEvaluatorInfo, TComparisonOp aComparisonOp)
87 Creates an empty filter object and then calls SetFilterFiledsL to get assigned all filter keys separately.
88 This function is used to test NewLC method of CDecisionFilter
90 @see CUpsDbBaseStep::CreateFilterL
92 @return A pointer to the newly allocated filter object. The pointer is also put onto the cleanup stack.
95 //Create an empty filter
96 CDecisionFilter *filter = CDecisionFilter::NewLC();
98 SetFilterFiledsL(*filter, aFlag, aClientSid, aEvaluatorId, aServiceId, aServerSid,aFingerprint, aClientEntity, aMajorPolicyVersion, aRecordId, aDescription, aResult, aEvaluatorInfo, aComparisonOp);
104 void CUpsDbBaseStep::SetFilterFiledsL(CDecisionFilter &aFilter, TInt aFlag, TInt aClientSid, TInt aEvaluatorId, TInt aServiceId, TInt aServerSid, TDesC8 &aFingerprint, TDesC8& aClientEntity, TInt aMajorPolicyVersion, TInt aRecordId, TDesC& aDescription, TInt aResult, TInt aEvaluatorInfo, TComparisonOp aComparisonOp)
106 This method assigns all the fields of a provided filter object with the supplied parameters.
107 The method is called by CreateFilterL and CreateFilterLC methods.
109 @see CUpsDbBaseStep::CreateFilterL
110 @param aFilter Empty decision filter object
113 //Sets the variables using aFlag
114 if(aFlag & KSetClientSid)
116 aFilter.SetClientSid(TSecureId(aClientSid), aComparisonOp);
119 if(aFlag & KSetEvaluatorId)
121 aFilter.SetEvaluatorId(TUid::Uid(aEvaluatorId),aComparisonOp);
124 if(aFlag & KSetServiceId)
126 aFilter.SetServiceId(TUid::Uid(aServiceId),aComparisonOp);
129 if(aFlag & KSetServerSid)
131 aFilter.SetServerSid(TSecureId(aServerSid),aComparisonOp);
134 if(aFlag & KSetFingerprint)
136 aFilter.SetFingerprintL(aFingerprint,aComparisonOp);
139 if(aFlag & KSetClientEntity)
141 aFilter.SetClientEntityL(aClientEntity,aComparisonOp);
144 if(aFlag & KSetMajorPolicyVersion)
146 aFilter.SetMajorPolicyVersion(aMajorPolicyVersion,aComparisonOp);
149 if(aFlag & KSetRecordId)
151 aFilter.SetRecordId(aRecordId,aComparisonOp);
154 if(aFlag & KSetDescription)
156 aFilter.SetDescriptionL(aDescription,aComparisonOp);
159 if(aFlag & KSetResult)
161 aFilter.SetResult(aResult,aComparisonOp);
164 if(aFlag & KSetEvaluatorInfo)
166 aFilter.SetEvaluatorInfo(aEvaluatorInfo,aComparisonOp);
171 CDecisionRecord *CUpsDbBaseStep::CreateRecordL(TInt aClientSid, TInt aEvaluatorId, TInt aServiceId, TInt aServerSid, TDesC8 &aFingerprint, TDesC8 &aClientEntity,
172 TDesC &aDecription, TInt aResult, TInt aMajorPolicyVersion, TInt aEvaluatorInfo, TInt aRecordId)
174 Creates a decision record from provided parameters.
176 @param aClientSid Secure Id of client application
177 @param aEvaluatorId UID for the policy evaluator DLL
178 @param aServiceId UID for service e.g. sms, mms, telephony, gprs
179 @param aServerSid UID for the system server that the decision applies to
180 @param aFingerprint Hash of the destination and/or opaque data. Maximum length is 32 bytes.
181 @param aClientEntity The name of the entity within the client that requested the service. Maximum length is 32 bytes.
182 @param aDescription A copy of description and/or opaque.
183 @param aResult Whether the request should be approved (=1) or denied (=0)
184 @param aMajorPolicyVersion The major version of the policy file.
185 @param aEvaluatorInfo Policy evaluator specific data
186 @param aRecordId An auto-incrementing record number.
188 @return A pointer to the newly allocated decision record object, if creation is successful.
192 CDecisionRecord *newRecord;
194 newRecord = CDecisionRecord::NewL(TSecureId(aClientSid),TUid::Uid(aEvaluatorId),TUid::Uid(aServiceId),TSecureId(aServerSid),aFingerprint,aClientEntity,aDecription,aResult,aMajorPolicyVersion,aEvaluatorInfo, aRecordId);
200 void CUpsDbBaseStep::StartTimer(TTime &aTime)
202 Assigns current time to the provided parameter
203 @param aTime Keeps the current time
210 void CUpsDbBaseStep::StopTimer(TTime &aStartTime, TInt64 &aElapsedTime)
212 Substracts the provided time from current time and returns the elapsed time.
213 @param aStartTime First measured time
214 @param aElapsedTime The elapsed time from the first measurement
220 TTimeIntervalMicroSeconds timeDiff = endTime.MicroSecondsFrom(aStartTime);
221 aElapsedTime = timeDiff.Int64();
225 _LIT8(KMessage,"%Ld");
226 iBuffer.AppendFormat(KMessage,aElapsedTime);
227 iFile.Write(iBuffer);
228 iFile.Write(_L8(";"));
234 void CUpsDbBaseStep::FillTableL(CDecisionDbW &aUpsDb, TInt aRecordNum, TInt aClientNum, TInt aServiceNum, TInt aServerNum, TInt aEvaluatorNum)
236 Fill the decision table with a specified number of decision records.
237 The parameteres are used as seeds to create sample decision records.
239 @param aUpsDb Handle to the database
240 @param aRecordNum Number of records which will be inserted into the database
241 @param aClientNum Number of different clients
242 @param aServiceNum Number of different services provided by each server
243 @param aServerNum Number of different servers
244 @param aEvaluatorNum Number of different policy evaluator DLLs
247 TSecureId clientId = TSecureId(0);
248 TSecureId serverId = TSecureId(0);
249 TUid serviceId =TUid::Null();
250 TUid evaluatorId =TUid::Null();
253 TUint16 policyMajorVersion = 2;
255 TBuf8<KDbMaxName> bufFP;
256 TBuf8<KDbMaxName> bufCE;
257 TBuf<KDbMaxName> bufD;
259 CDecisionRecord* newRecord = NULL;
261 for(TInt i=0; i<aRecordNum; i++)
263 bufFP.AppendFormat(KFingerprint,i);
264 bufCE.AppendFormat(KClientEntity,i);
265 bufD.AppendFormat(KDescription,i);
267 CMD5* md5FP = CMD5::NewL();
268 CleanupStack::PushL(md5FP);
270 CMD5* md5CE = CMD5::NewL();
271 CleanupStack::PushL(md5CE);
273 clientId=TSecureId(i%aClientNum + aRecordNum);
274 serviceId=TUid::Uid(i%aServiceNum + 10000 + aRecordNum);
275 serverId =TSecureId(i%aServerNum + 100000 + aRecordNum);
276 evaluatorId = TUid::Uid(i%aEvaluatorNum + 1000000 + aRecordNum);
280 newRecord = CDecisionRecord::NewLC(clientId,evaluatorId,serviceId,serverId,md5FP->Hash(bufFP),md5CE->Hash(bufCE),bufD, result,policyMajorVersion, 0);
282 aUpsDb.CreateDecisionL(*newRecord);
284 CleanupStack::PopAndDestroy(newRecord);
285 CleanupStack::PopAndDestroy(2); //md5FP and md5CE
294 CDecisionDbW *CUpsDbBaseStep::CreateDatabaseLC(TDesC& aDbLocation, RFs& aFs)
296 Create a writable decision database object. Prints the consumed time to perform this operation.
298 @param aDbLocation Where the database will be created
303 StartTimer(startTime);
305 //Database is created
306 CDecisionDbW *upsDb = CDecisionDbW::NewLC(aDbLocation, aFs);
309 TInt64 elapsedTime = 0;
310 StopTimer(startTime,elapsedTime);
312 INFO_PRINTF2(_L("Decision database created in %Ld microseconds."), elapsedTime);
318 CDecisionRecord *CUpsDbBaseStep::GetDecisionLC(CDecisionDbW &aUpsDb, CDecisionFilter &aFilter)
320 Get a decision record from the database. This function uses single record lookup method (RDbTable).
321 Prints the consumed time to perform this operation.
323 @param aUpsDb Handle to the database
324 @param aFilter Filter
329 StartTimer(startTime);
331 CDecisionRecord *retRecord = NULL;
332 retRecord = aUpsDb.GetDecisionL(aFilter);
335 CleanupStack::PushL(retRecord);
339 TInt64 elapsedTime = 0;
340 StopTimer(startTime,elapsedTime);
344 INFO_PRINTF2(_L("A single record retrieved in %Ld microseconds."), elapsedTime);
351 void CUpsDbBaseStep::GetDecisionsL(CDecisionDbW &aUpsDb, CDecisionFilter &aFilter, TBool aCancel, RPointerArray<CDecisionRecord>& aRecordList)
353 Get a set of decision records from the database. Uses multiple records lookup method (RDbView).
354 Prints the consumed time to perform this operation.
359 StartTimer(startTime);
361 CDecisionView *dbView = aUpsDb.CreateViewL(aFilter);
362 CleanupStack::PushL(dbView);
364 dbView->EvaluateView(iWaiter->iStatus);
369 iWaiter->WaitActiveL(KErrCancel);
373 iWaiter->WaitActiveL(KErrNone);
374 CDecisionRecord *record = NULL;
376 while((record = dbView->NextDecisionL()) != NULL)
378 CleanupStack::PushL(record);
380 aRecordList.AppendL(record);
381 CleanupStack::Pop(record);
386 TInt64 elapsedTime = 0;
387 StopTimer(startTime,elapsedTime);
389 INFO_PRINTF3(_L("%d records retrieved in %Ld microseconds."), num, elapsedTime);
390 CleanupStack::PopAndDestroy(dbView);
394 void CUpsDbBaseStep::RemoveDecisionsL(CDecisionDbW &aUpsDb, CDecisionFilter &aFilter)
396 Delete a set of records. Prints the consumed time to perform this operation.
401 StartTimer(startTime);
403 aUpsDb.RemoveDecisionsL(aFilter);
406 TInt64 elapsedTime = 0;
407 StopTimer(startTime,elapsedTime);
409 INFO_PRINTF2(_L("A set of records deleted in %Ld microseconds."), elapsedTime);
413 void CUpsDbBaseStep::RemoveAllDecisionsL(CDecisionDbW &aUpsDb, RFs& aFs)
415 Deletes all records in the database. Actually delete all database.
416 Prints the consumed time to perform this operation.
421 StartTimer(startTime);
423 aUpsDb.DeleteDatabaseL(aFs);
426 TInt64 elapsedTime = 0;
427 StopTimer(startTime,elapsedTime);
429 INFO_PRINTF2(_L("All records deleted in %Ld microseconds."), elapsedTime);
433 void CUpsDbBaseStep::InsertRecordL(CDecisionDbW &aUpsDb, CDecisionRecord &aRecord)
435 Inserts the provided record into the decision database. Prints the consumed time to perform this operation.
440 StartTimer(startTime);
442 aUpsDb.CreateDecisionL(aRecord);
445 TInt64 elapsedTime = 0;
446 StopTimer(startTime,elapsedTime);
448 INFO_PRINTF2(_L("A record inserted in %Ld microseconds."), elapsedTime);
452 void CUpsDbBaseStep::ReadConfigurationFile(TDes &aKeyNotFound)
454 Reads the key fileds from the ini file which are used to fill table with a specified number of
456 @param aKeyNotFound The appended list of the key names which have not been found
460 if(EFalse == GetIntFromConfig(ConfigSection(),KRecordNum, iRecordNum))
462 aKeyNotFound.Append(KRecordNum);
463 aKeyNotFound.Append(_L(" "));
465 else if(EFalse == GetIntFromConfig(ConfigSection(),KServerNum, iServerNum))
467 aKeyNotFound.Append(KServerNum);
468 aKeyNotFound.Append(_L(" "));
470 else if(EFalse == GetIntFromConfig(ConfigSection(),KServiceNum, iServiceNum))
472 aKeyNotFound.Append(KServiceNum);
473 aKeyNotFound.Append(_L(" "));
475 else if(EFalse == GetIntFromConfig(ConfigSection(),KClientNum, iClientNum))
477 aKeyNotFound.Append(KClientNum);
478 aKeyNotFound.Append(_L(" "));
480 else if(EFalse == GetIntFromConfig(ConfigSection(),KEvaluatorNum, iEvaluatorNum))
482 aKeyNotFound.Append(KEvaluatorNum);
483 aKeyNotFound.Append(_L(" "));
485 else if(EFalse == GetIntFromConfig(ConfigSection(),KRetrieveNum, iRetrieveNum))
487 aKeyNotFound.Append(KRetrieveNum);
488 aKeyNotFound.Append(_L(" "));
493 TInt CUpsDbBaseStep::ConvertNumber(TDesC& aError)
495 Converts error string to number
496 @param aError Error string
499 TInt error = KErrNotFound;
501 if(!aError.CompareF(KStrErrNone))
505 else if(!aError.CompareF(KStrErrUpsBadFingerprintLength))
507 error = KErrUpsBadFingerprintLength;
509 else if(!aError.CompareF(KStrErrUpsMissingArgument))
511 error = KErrUpsMissingArgument;
513 else if(!aError.CompareF(KStrErrUpsBadClientEntityLength))
515 error = KErrUpsBadClientEntityLength;
521 static void PopulateFingerPrint(TDes8& aBuf, TInt aStart)
523 TUint8 *ptr = (TUint8 *)aBuf.Ptr();
528 for(pos = 0, value = aStart; pos < KUpsMaxFingerprintLength; ++pos, ++value )
535 void CUpsDbBaseStep::TestFingerprintValuesL(CDecisionDbW& aUpsDb)
537 Creates a number of decision records by using all characters
538 between 0x01 and 0xFF in Fingerprint value.
545 TInt evaluatorId = 4;
546 TInt evaluatorInfo = 5;
548 TInt policyVersion = 9;
551 CDecisionRecord *record = NULL;
552 CDecisionFilter *filter = NULL;
553 RPointerArray<CDecisionRecord> recordList;
554 TBuf8<KUpsMaxFingerprintLength> bufFp;
555 TBuf8<KUpsMaxClientEntityLength> bufCe;
556 TBuf<KUpsDescriptionLength> bufDes;
557 bufFp.SetLength(KUpsMaxFingerprintLength);
558 bufCe.SetLength(KUpsMaxClientEntityLength);
559 TInt flag = KSetClientSid|KSetEvaluatorId|KSetServiceId|KSetServerSid|KSetFingerprint|KSetClientEntity;
562 for(pos=0; pos <= KUpsDescriptionLength+1; pos+= KUpsMaxFingerprintLength)
564 PopulateFingerPrint(bufFp, pos);
565 record = CreateRecordL(++clientId,++evaluatorId,++serviceId,++serverId, bufFp, bufCe, bufDes, result, policyVersion, evaluatorInfo, recordId);
566 CleanupStack::PushL(record);
567 InsertRecordL(aUpsDb,*record);
569 filter = CreateFilterLC(flag,clientId,evaluatorId,serviceId,serverId,bufFp,bufCe,policyVersion,recordId,bufDes,result,evaluatorInfo,EEqual);
570 CleanupResetAndDestroyPushL(recordList);
571 GetDecisionsL(aUpsDb, *filter, EFalse, recordList);
572 if(recordList.Count() != 1)
574 SetTestStepResult(EFail);
575 CleanupStack::PopAndDestroy(3, record);
578 CleanupStack::PopAndDestroy(3, record);
583 void CUpsDbBaseStep::CompactDatabaseL(CDecisionDbW& aUpsDb, TBool aCancel)
585 Compacts the decision database.
590 StartTimer(startTime);
592 CDecisionDbCompactor *dbCompact = aUpsDb.PrepareCompactionLC();
593 dbCompact->Compact(iWaiter->iStatus);
598 iWaiter->WaitActiveL(KErrCancel);
602 iWaiter->WaitActiveL(KErrNone);
606 TInt64 elapsedTime = 0;
607 StopTimer(startTime,elapsedTime);
609 if(iWaiter->iStatus == 0)
611 INFO_PRINTF2(_L("Compaction has been achieved in %Ld microseconds."), elapsedTime);
615 if(!aCancel && iWaiter->iStatus!=KErrCancel)
617 ERR_PRINTF1(_L("Failed to compact database!"));
618 SetTestStepResult(EFail);
622 CleanupStack::PopAndDestroy(dbCompact);
626 void CUpsDbBaseStep::UpdateRecordL(CDecisionDbW& aUpsDb, CDecisionFilter& aFilter, CDecisionRecord& aRecord)
628 Updates a decision record from the decision database.
630 @param aUpsDb A pointer to the decision database object
631 @param aFilter Filter used to find the record which will be updated
632 @param aRecord New record containing the values which will be updated
637 StartTimer(startTime);
639 if(EFalse == aUpsDb.UpdateDecisionL(aFilter,aRecord))
641 SetTestStepResult(EFail);
642 User::Leave(KErrNotFound);
647 TInt64 elapsedTime = 0;
648 StopTimer(startTime,elapsedTime);
649 INFO_PRINTF2(_L("The record was updated in %Ld microseconds."), elapsedTime);