os/security/authorisation/userpromptservice/database/test/dumpupsdb/source/dumpupsdb.cpp
First public contribution.
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.
15 * Implements a tool to export/import UPS Decision Database
21 #include "dumpupsdb.h"
23 using namespace UserPromptService;
29 CPrinter::CPrinter(CConsoleBase* aConsole):iConsole(aConsole)
43 CPrinter* CPrinter::NewLC(CConsoleBase* aConsole)
45 Creates a new printer object and places the pointer on the cleanup stack.
46 @param aConsole The console object to print text to.
47 @return A pointer to the new printer object.
50 CPrinter *self = new(ELeave)CPrinter(aConsole);
51 CleanupStack::PushL(self);
55 CPrinter* CPrinter::NewLC(CConsoleBase* aConsole, RFile& aFile)
57 Creates a new printer object and places the pointer on the cleanup stack.
58 @param aConsole The console object to print text to.
59 @param aFile A handle to a file to write the text to. The handle is duplicated internally.
60 @return A pointer to the new printer object.
63 CPrinter *self = CPrinter::NewLC(aConsole);
64 self->ConstructL(aFile);
69 void CPrinter::ConstructL(RFile& aFile)
70 /** Second phase constructor*/
72 User::LeaveIfError(iFile.Duplicate(aFile));
75 iReader.Attach(aFile);
78 void CPrinter::PrintfL(TRefByValue<const TDesC16> aFormat, ...)
80 Formats and writes 16-bit text to the console and/or file
81 @param aFormat The 16-bit non-modifiable descriptor containing the format string.
85 VA_START (list, aFormat);
88 iBuffer.AppendFormatList(aFormat, list);
90 iConsole->Printf(iBuffer);
94 HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(iBuffer);
95 CleanupStack::PushL(utf8);
96 User::LeaveIfError(iFile.Write(*utf8));
97 CleanupStack::PopAndDestroy(utf8);
104 void CPrinter::Printf8L(TRefByValue<const TDesC8> aFormat, ...)
106 Formats and writes 8-bit text to the console and/or file
107 @param aFormat The 8-bit non-modifiable descriptor containing the format string.
111 VA_START (list, aFormat);
114 iBuffer8.AppendFormatList(aFormat, list);
115 iBuffer.Copy(iBuffer8);
116 iConsole->Printf(iBuffer);
120 User::LeaveIfError(iFile.Write(iBuffer8));
126 void CPrinter::PrintOnlyConsoleL(const TDesC& aFormat, ...)
128 Formats and writes 16-bit text to the console.
129 @param aFormat The 16-bit non-modifiable descriptor containing the format string.
142 void CPrinter::Usage(CConsoleBase* aConsole)
144 Prints how to use DumpUpsDb Tool.
145 @param aConsole A pointer to the console object
148 aConsole->Printf(_L("DUMPUPSDB Version 1, 0\n"));
149 aConsole->Printf(_L("A utility for importing and exporting UPS Decision Database.\n"));
150 aConsole->Printf(_L("Copyright (c) 2007 Symbian Ltd. All rights reserved.\n\n"));
151 aConsole->Printf(_L("error: wrong number of arguments\n"));
152 aConsole->Printf(_L("Usage: DumpUpsDb [-h] [-i] [-e] [-b] -db dbpath [-f filepath]\n\n"));
153 aConsole->Printf(_L("Options : -h Show help page\n"));
154 aConsole->Printf(_L("Options : -i Import an exported database\n"));
155 aConsole->Printf(_L("Options : -e Export the database\n"));
156 aConsole->Printf(_L("Options : -db Database file\n"));
157 aConsole->Printf(_L("Options : -f Output/Input file\n"));
158 aConsole->Printf(_L("Options : -b Import/export Client Entity as binary\n\n"));
159 aConsole->Printf(_L("Press any key to continue\r\n"));
163 void CPrinter::Wait()
165 If no output file is specified then pause after finishing because the console
166 will vanish when it is closed.
169 iConsole->Printf(_L("Press any key to continue\r\n"));
174 void CPrinter::ReadNextLineL(TDes8& aLine)
177 iReader.ReadL(aLine, achar);
180 TInt CPrinter::FileSizeL()
182 return iReader.Source()->SizeL();
188 CDatabase::CDatabase(TBool aImport):iImport(aImport)
194 CDatabase::~CDatabase()
202 CDatabase* CDatabase::NewLC(CConsoleBase* aConsole, RFs& aFs, TBool aImport, const TDesC& aDb, const TDesC& aFile)
204 Creates a new database object and places the pointer on the cleanup stack.
206 @param aConsole Pointer to the console object
207 @param aFs Handle to the file server
208 @param aImport Whether the operation type is import
209 @param aDb The fully qualified path of the decision database
210 @param aFile The fully qualified path of the dump file
211 @return A pointer to the newly created database object
214 CDatabase *self = new(ELeave)CDatabase(aImport);
215 CleanupStack::PushL(self);
216 self->ConstructL(aConsole, aFs, aDb, aFile);
221 void CDatabase::ConstructL(CConsoleBase* aConsole, RFs& aFs, const TDesC& aDb, const TDesC& aFile)
223 Second phase constructor for database object
226 if(aFile.Length() > 0)
230 User::LeaveIfError(iFile.Open(aFs, aFile, EFileWrite|EFileShareExclusive));
234 User::LeaveIfError(iFile.Replace(aFs, aFile, EFileWrite|EFileShareExclusive));
237 iPrinter = CPrinter::NewLC(aConsole, iFile);
241 iPrinter = CPrinter::NewLC(aConsole);
244 CleanupStack::Pop(iPrinter);
246 iUpsDb = CDecisionDbW::NewL(aDb, aFs);
250 void CDatabase::DumpL()
253 {//Dump database to the console and files
254 //Create an empty filter to get all decisions in the table
255 CDecisionFilter *filter = CDecisionFilter::NewLC();
257 //Create a view object
258 CDecisionView *dbView = iUpsDb->CreateViewL(*filter);
259 CleanupStack::PushL(dbView);
261 CActiveWaiter *waiter = new(ELeave)CActiveWaiter();
262 CleanupStack::PushL(waiter);
264 //Fill the decisions into the view object
265 dbView->EvaluateView(waiter->iStatus);
266 waiter->WaitActiveL(KErrNone);
267 if(iFlag & EAppendTestResults)
273 CDecisionRecord *record = NULL;
274 while((record = dbView->NextDecisionL()) != NULL)
276 CleanupStack::PushL(record);
277 PrintDecisionL(*record);
278 CleanupStack::PopAndDestroy(record);
281 CleanupStack::PopAndDestroy(3, filter);
282 iPrinter->PrintOnlyConsoleL(_L("Exported successfully!\n"));
283 if(iFlag & EAppendTestResults)
289 {//Import an exported decision file to the decision database
291 fileSize = iPrinter->FileSizeL();
294 TBool skipFirstLine = ETrue;
295 CDecisionRecord *record = NULL;
298 iPrinter->ReadNextLineL(buffer);
299 readSize +=buffer.Length();
302 skipFirstLine = EFalse;
306 record = ParseAndCreateRecordLC(buffer);
307 iUpsDb->CreateDecisionL(*record);
308 CleanupStack::PopAndDestroy(record);
312 }while(readSize < fileSize);
314 iPrinter->PrintOnlyConsoleL(_L("Imported successfully!\n"));
316 //If both of the flags are not set, wait.
317 if(!(iFlag & EAppendTestResults || iFlag & EDoNotStop))
324 void CDatabase::PrintDecisionL(CDecisionRecord& aRecord)
326 _LIT(KDelimiter, ";");
327 _LIT(KWriteId, "\"%08x\"");
328 _LIT(KWriteString, "\"%S\"");
329 _LIT8(KWriteString8,"\"%S\"");
330 _LIT8(KWriteHex8, "%02x");
333 iPrinter->PrintfL(KWriteId,aRecord.iClientSid.iId);
334 iPrinter->PrintfL(KDelimiter);
335 iPrinter->PrintfL(KWriteId,aRecord.iEvaluatorId.iUid);
336 iPrinter->PrintfL(KDelimiter);
337 iPrinter->PrintfL(KWriteId,aRecord.iServiceId.iUid);
338 iPrinter->PrintfL(KDelimiter);
339 iPrinter->PrintfL(KWriteId,aRecord.iServerSid.iId);
340 iPrinter->PrintfL(KDelimiter);
342 TBuf8<KUpsMaxFingerprintLength*2> hexdump;
343 TUint8 *ptr = (TUint8 *)aRecord.iFingerprint.Ptr();
345 TInt len = aRecord.iFingerprint.Length();
349 hexdump.AppendFormat(KWriteHex8,ptr[i]);
351 iPrinter->Printf8L(KWriteString8,&hexdump);
352 iPrinter->PrintfL(KDelimiter);
354 if(iFlag & EPrintBinary)
356 iPrinter->Printf8L(KWriteString8,&aRecord.iClientEntity);
357 iPrinter->PrintfL(KDelimiter);
362 ptr = (TUint8 *)aRecord.iClientEntity.Ptr();
363 len = aRecord.iClientEntity.Length();
366 hexdump.AppendFormat(KWriteHex8,ptr[i]);
368 iPrinter->Printf8L(KWriteString8,&hexdump);
369 iPrinter->PrintfL(KDelimiter);
372 iPrinter->PrintfL(KWriteString,&aRecord.iDescription);
373 iPrinter->PrintfL(KDelimiter);
375 iPrinter->PrintfL(KWriteId,aRecord.iResult);
376 iPrinter->PrintfL(KDelimiter);
377 iPrinter->PrintfL(KWriteId,aRecord.iEvaluatorInfo);
378 iPrinter->PrintfL(KDelimiter);
379 iPrinter->PrintfL(KWriteId,aRecord.iMajorPolicyVersion);
380 iPrinter->PrintfL(KDelimiter);
381 iPrinter->PrintfL(KWriteId,aRecord.iRecordId);
383 iPrinter->Printf8L(_L8("\n"));
387 void CDatabase::PrintHeaderL()
389 _LIT(KDelimiter, ";");
390 _LIT(KWriteString, "\"%S\"");
392 iPrinter->PrintfL(KWriteString,&KColClientSid);
393 iPrinter->PrintfL(KDelimiter);
394 iPrinter->PrintfL(KWriteString,&KColEvaluatorId);
395 iPrinter->PrintfL(KDelimiter);
396 iPrinter->PrintfL(KWriteString,&KColServiceId);
397 iPrinter->PrintfL(KDelimiter);
398 iPrinter->PrintfL(KWriteString,&KColServerSid);
399 iPrinter->PrintfL(KDelimiter);
400 iPrinter->PrintfL(KWriteString,&KColFingerprint);
401 iPrinter->PrintfL(KDelimiter);
402 iPrinter->PrintfL(KWriteString,&KColClientEntity);
403 iPrinter->PrintfL(KDelimiter);
404 iPrinter->PrintfL(KWriteString,&KColDescription);
405 iPrinter->PrintfL(KDelimiter);
406 iPrinter->PrintfL(KWriteString,&KColResult);
407 iPrinter->PrintfL(KDelimiter);
408 iPrinter->PrintfL(KWriteString,&KColEvaluatorInfo);
409 iPrinter->PrintfL(KDelimiter);
410 iPrinter->PrintfL(KWriteString,&KColMajorPolicyVersion);
411 iPrinter->PrintfL(KDelimiter);
412 iPrinter->PrintfL(KWriteString,&KColRecordId);
413 iPrinter->Printf8L(_L8("\n"));
416 CDecisionRecord* CDatabase::ParseAndCreateRecordLC(TDesC8& aLine)
418 Parse a line and create a decision record from the parsed values
419 @param aLine A line containing a decision record values quoted with double quotes and separated by semi-colon
420 @return A newly created decision record
423 CDecisionRecord* record = NULL;
429 TUint16 flag = 0x078F;
430 TUint16 pos = 0x0001;
435 TSecureId clientSid(0);
436 TSecureId serverSid(0);
437 TUid serviceId = TUid::Null();
438 TUid evaluatorId = TUid::Null();
439 TBuf8<KUpsMaxFingerprintLength*2> fingerprint;
440 TBuf8<KUpsMaxClientEntityLength*2> clientEntity;
443 TUint32 evaluatorInfo=0;
450 achar = parser.Get();
456 //if double quote start or stop token reading
470 TLex8 intToken(parser.MarkedToken());
474 intToken.Val(hexVal,EHex);
475 clientSid.iId = hexVal;
477 case KLocEvaluatorId:
478 intToken.Val(hexVal,EHex);
479 evaluatorId.iUid = hexVal;
482 intToken.Val(hexVal,EHex);
483 serviceId.iUid = hexVal;
486 intToken.Val(hexVal,EHex);
487 serverSid.iId = hexVal;
491 result = (TUint8)value;
493 case KLocEvaluatorInfo:
495 evaluatorInfo = value;
497 case KLocMajorPolicyVersion:
499 policyVer = (TUint16)value;
503 recordId = (TUint32)value;
506 User::Leave(KErrGeneral);
513 case KLocDescription:
515 TPtrC8 tmpDescription = parser.MarkedToken();
516 description.Create(tmpDescription.Length());
517 description.CleanupClosePushL();
518 description.Copy(tmpDescription);
522 case KLocFingerprint:
524 fingerprint = parser.MarkedToken();
525 HexToStrL(fingerprint);
528 case KLocClientEntity:
530 clientEntity = parser.MarkedToken();
531 if(!(iFlag & EPrintBinary))
533 HexToStrL(clientEntity);
538 User::Leave(KErrGeneral);
549 record = CDecisionRecord::NewL(clientSid,evaluatorId,serviceId,serverSid,fingerprint,clientEntity,description,result,policyVer,evaluatorInfo,recordId);
550 CleanupStack::PopAndDestroy(&description);
551 CleanupStack::PushL(record);
556 TUint8 CDatabase::HexToIntL(TUint8* aPtr)
558 Convert a 2-byte hexadecimal representation value to integer (Ex: C2 -> 194).
559 @param aPtr Pointer to source data
560 @return An integer value against 2-byte hexadecimal value
563 //Save and then set third byte NULL
564 TUint8 temp = aPtr[2];
566 //Create a lex string from first two bytes
568 //Convert two bytes hex value (ex:9F) to integer (ex:159)
570 User::LeaveIfError(lex.Val(intVal,EHex));
571 //Put the original value back
573 //return integer value of first two hex bytes
578 void CDatabase::HexToStrL(TDes8& aSource)
580 Convert a string containing hexadecimal representation to another string containing binary representation
581 @param aSource Source data containing a string
584 TUint8 *pSource = (TUint8 *)aSource.Ptr();
585 TUint8 *pDest = (TUint8 *)aSource.Ptr();
587 TInt len = aSource.Length();
591 for(idxSource=0, idxDest=0; idxSource<len; ++idxDest,idxSource+=2)
593 pDest[idxDest] = HexToIntL(pSource+idxSource);
596 aSource.SetLength(idxDest);
600 void CDatabase::PrintTestResultsL()
602 Prints dummy test results to get rid of log file parsing error in showing test results.
606 iPrinter->PrintfL(_L("\n\n0 tests failed out of 1\n"));
608 iPrinter->PrintfL(_L("\n\nTEST STEP SUMMARY:\n"));
609 iPrinter->PrintfL(_L("PASS = 1\n"));
610 iPrinter->PrintfL(_L("FAIL = 0\n"));
611 iPrinter->PrintfL(_L("ABORT = 0\n"));
612 iPrinter->PrintfL(_L("PANIC = 0\n"));
613 iPrinter->PrintfL(_L("INCONCLUSIVE = 0\n"));
614 iPrinter->PrintfL(_L("UNKNOWN = 0\n"));
615 iPrinter->PrintfL(_L("UNEXECUTED = 0\n"));
616 iPrinter->PrintfL(_L("COMMENTED COMMAND'S = 0\n"));
617 iPrinter->PrintfL(_L("TEST CASE SUMMARY:\n"));
618 iPrinter->PrintfL(_L("PASS = 1\n"));
619 iPrinter->PrintfL(_L("FAIL = 0\n"));
620 iPrinter->PrintfL(_L("INCONCLUSIVE = 0\n"));
624 void CDatabase::PrintTestHeaderL()
626 iPrinter->PrintfL(_L("TEST SYNOPSIS:\n"));
627 iPrinter->PrintfL(_L("TEF Version : 2.1.2004\n"));
628 iPrinter->PrintfL(_L("START_TESTCASE COUNT : 1\n"));
629 iPrinter->PrintfL(_L("RUN_TEST_STEP COUNT : 1\n"));
630 iPrinter->PrintfL(_L("RUN_TEST_STEP_RESULT COUNT : 1\n\n"));