sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Implements a tool to export/import UPS Decision Database sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #include "dumpupsdb.h" sl@0: sl@0: using namespace UserPromptService; sl@0: sl@0: // sl@0: //CPrinter sl@0: // sl@0: sl@0: CPrinter::CPrinter(CConsoleBase* aConsole):iConsole(aConsole) sl@0: /** Constructor */ sl@0: { sl@0: sl@0: } sl@0: sl@0: CPrinter::~CPrinter() sl@0: /** Destructor */ sl@0: { sl@0: iReader.Close(); sl@0: iFile.Close(); sl@0: } sl@0: sl@0: sl@0: CPrinter* CPrinter::NewLC(CConsoleBase* aConsole) sl@0: /** sl@0: Creates a new printer object and places the pointer on the cleanup stack. sl@0: @param aConsole The console object to print text to. sl@0: @return A pointer to the new printer object. sl@0: */ sl@0: { sl@0: CPrinter *self = new(ELeave)CPrinter(aConsole); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: CPrinter* CPrinter::NewLC(CConsoleBase* aConsole, RFile& aFile) sl@0: /** sl@0: Creates a new printer object and places the pointer on the cleanup stack. sl@0: @param aConsole The console object to print text to. sl@0: @param aFile A handle to a file to write the text to. The handle is duplicated internally. sl@0: @return A pointer to the new printer object. sl@0: */ sl@0: { sl@0: CPrinter *self = CPrinter::NewLC(aConsole); sl@0: self->ConstructL(aFile); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: void CPrinter::ConstructL(RFile& aFile) sl@0: /** Second phase constructor*/ sl@0: { sl@0: User::LeaveIfError(iFile.Duplicate(aFile)); sl@0: iLogToFile = ETrue; sl@0: //iReader.Set(iFile); sl@0: iReader.Attach(aFile); sl@0: } sl@0: sl@0: void CPrinter::PrintfL(TRefByValue aFormat, ...) sl@0: /** sl@0: Formats and writes 16-bit text to the console and/or file sl@0: @param aFormat The 16-bit non-modifiable descriptor containing the format string. sl@0: */ sl@0: { sl@0: VA_LIST list; sl@0: VA_START (list, aFormat); sl@0: sl@0: iBuffer.Zero(); sl@0: iBuffer.AppendFormatList(aFormat, list); sl@0: sl@0: iConsole->Printf(iBuffer); sl@0: sl@0: if(iLogToFile) sl@0: { sl@0: HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(iBuffer); sl@0: CleanupStack::PushL(utf8); sl@0: User::LeaveIfError(iFile.Write(*utf8)); sl@0: CleanupStack::PopAndDestroy(utf8); sl@0: } sl@0: sl@0: VA_END(list); sl@0: } sl@0: sl@0: sl@0: void CPrinter::Printf8L(TRefByValue aFormat, ...) sl@0: /** sl@0: Formats and writes 8-bit text to the console and/or file sl@0: @param aFormat The 8-bit non-modifiable descriptor containing the format string. sl@0: */ sl@0: { sl@0: VA_LIST list; sl@0: VA_START (list, aFormat); sl@0: sl@0: iBuffer8.Zero(); sl@0: iBuffer8.AppendFormatList(aFormat, list); sl@0: iBuffer.Copy(iBuffer8); sl@0: iConsole->Printf(iBuffer); sl@0: sl@0: if(iLogToFile) sl@0: { sl@0: User::LeaveIfError(iFile.Write(iBuffer8)); sl@0: } sl@0: sl@0: VA_END(list); sl@0: } sl@0: sl@0: void CPrinter::PrintOnlyConsoleL(const TDesC& aFormat, ...) sl@0: /** sl@0: Formats and writes 16-bit text to the console. sl@0: @param aFormat The 16-bit non-modifiable descriptor containing the format string. sl@0: */ sl@0: { sl@0: TBool temp; sl@0: temp = iLogToFile; sl@0: iLogToFile = EFalse; sl@0: sl@0: PrintfL(aFormat); sl@0: sl@0: iLogToFile = temp; sl@0: } sl@0: sl@0: sl@0: void CPrinter::Usage(CConsoleBase* aConsole) sl@0: /** sl@0: Prints how to use DumpUpsDb Tool. sl@0: @param aConsole A pointer to the console object sl@0: */ sl@0: { sl@0: aConsole->Printf(_L("DUMPUPSDB Version 1, 0\n")); sl@0: aConsole->Printf(_L("A utility for importing and exporting UPS Decision Database.\n")); sl@0: aConsole->Printf(_L("Copyright (c) 2007 Symbian Ltd. All rights reserved.\n\n")); sl@0: aConsole->Printf(_L("error: wrong number of arguments\n")); sl@0: aConsole->Printf(_L("Usage: DumpUpsDb [-h] [-i] [-e] [-b] -db dbpath [-f filepath]\n\n")); sl@0: aConsole->Printf(_L("Options : -h Show help page\n")); sl@0: aConsole->Printf(_L("Options : -i Import an exported database\n")); sl@0: aConsole->Printf(_L("Options : -e Export the database\n")); sl@0: aConsole->Printf(_L("Options : -db Database file\n")); sl@0: aConsole->Printf(_L("Options : -f Output/Input file\n")); sl@0: aConsole->Printf(_L("Options : -b Import/export Client Entity as binary\n\n")); sl@0: aConsole->Printf(_L("Press any key to continue\r\n")); sl@0: aConsole->Getch(); sl@0: } sl@0: sl@0: void CPrinter::Wait() sl@0: /** sl@0: If no output file is specified then pause after finishing because the console sl@0: will vanish when it is closed. sl@0: */ sl@0: { sl@0: iConsole->Printf(_L("Press any key to continue\r\n")); sl@0: iConsole->Getch(); sl@0: } sl@0: sl@0: sl@0: void CPrinter::ReadNextLineL(TDes8& aLine) sl@0: { sl@0: TChar achar = '\n'; sl@0: iReader.ReadL(aLine, achar); sl@0: } sl@0: sl@0: TInt CPrinter::FileSizeL() sl@0: { sl@0: return iReader.Source()->SizeL(); sl@0: } sl@0: // sl@0: //CDatabase sl@0: // sl@0: sl@0: CDatabase::CDatabase(TBool aImport):iImport(aImport) sl@0: /** Constructor */ sl@0: { sl@0: sl@0: } sl@0: sl@0: CDatabase::~CDatabase() sl@0: /** Destructor */ sl@0: { sl@0: delete iPrinter; sl@0: delete iUpsDb; sl@0: } sl@0: sl@0: sl@0: CDatabase* CDatabase::NewLC(CConsoleBase* aConsole, RFs& aFs, TBool aImport, const TDesC& aDb, const TDesC& aFile) sl@0: /** sl@0: Creates a new database object and places the pointer on the cleanup stack. sl@0: sl@0: @param aConsole Pointer to the console object sl@0: @param aFs Handle to the file server sl@0: @param aImport Whether the operation type is import sl@0: @param aDb The fully qualified path of the decision database sl@0: @param aFile The fully qualified path of the dump file sl@0: @return A pointer to the newly created database object sl@0: */ sl@0: { sl@0: CDatabase *self = new(ELeave)CDatabase(aImport); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aConsole, aFs, aDb, aFile); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: void CDatabase::ConstructL(CConsoleBase* aConsole, RFs& aFs, const TDesC& aDb, const TDesC& aFile) sl@0: /** sl@0: Second phase constructor for database object sl@0: */ sl@0: { sl@0: if(aFile.Length() > 0) sl@0: { sl@0: if(iImport) sl@0: { sl@0: User::LeaveIfError(iFile.Open(aFs, aFile, EFileWrite|EFileShareExclusive)); sl@0: } sl@0: else sl@0: { sl@0: User::LeaveIfError(iFile.Replace(aFs, aFile, EFileWrite|EFileShareExclusive)); sl@0: } sl@0: sl@0: iPrinter = CPrinter::NewLC(aConsole, iFile); sl@0: } sl@0: else sl@0: { sl@0: iPrinter = CPrinter::NewLC(aConsole); sl@0: } sl@0: sl@0: CleanupStack::Pop(iPrinter); sl@0: sl@0: iUpsDb = CDecisionDbW::NewL(aDb, aFs); sl@0: sl@0: } sl@0: sl@0: void CDatabase::DumpL() sl@0: { sl@0: if(!iImport) sl@0: {//Dump database to the console and files sl@0: //Create an empty filter to get all decisions in the table sl@0: CDecisionFilter *filter = CDecisionFilter::NewLC(); sl@0: sl@0: //Create a view object sl@0: CDecisionView *dbView = iUpsDb->CreateViewL(*filter); sl@0: CleanupStack::PushL(dbView); sl@0: sl@0: CActiveWaiter *waiter = new(ELeave)CActiveWaiter(); sl@0: CleanupStack::PushL(waiter); sl@0: sl@0: //Fill the decisions into the view object sl@0: dbView->EvaluateView(waiter->iStatus); sl@0: waiter->WaitActiveL(KErrNone); sl@0: if(iFlag & EAppendTestResults) sl@0: { sl@0: PrintTestHeaderL(); sl@0: } sl@0: PrintHeaderL(); sl@0: sl@0: CDecisionRecord *record = NULL; sl@0: while((record = dbView->NextDecisionL()) != NULL) sl@0: { sl@0: CleanupStack::PushL(record); sl@0: PrintDecisionL(*record); sl@0: CleanupStack::PopAndDestroy(record); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(3, filter); sl@0: iPrinter->PrintOnlyConsoleL(_L("Exported successfully!\n")); sl@0: if(iFlag & EAppendTestResults) sl@0: { sl@0: PrintTestResultsL(); sl@0: } sl@0: } sl@0: else sl@0: {//Import an exported decision file to the decision database sl@0: TInt fileSize; sl@0: fileSize = iPrinter->FileSizeL(); sl@0: TInt readSize=0; sl@0: TBuf8<256> buffer; sl@0: TBool skipFirstLine = ETrue; sl@0: CDecisionRecord *record = NULL; sl@0: do sl@0: { sl@0: iPrinter->ReadNextLineL(buffer); sl@0: readSize +=buffer.Length(); sl@0: if(skipFirstLine) sl@0: { sl@0: skipFirstLine = EFalse; sl@0: } sl@0: else sl@0: { sl@0: record = ParseAndCreateRecordLC(buffer); sl@0: iUpsDb->CreateDecisionL(*record); sl@0: CleanupStack::PopAndDestroy(record); sl@0: } sl@0: sl@0: buffer.Zero(); sl@0: }while(readSize < fileSize); sl@0: sl@0: iPrinter->PrintOnlyConsoleL(_L("Imported successfully!\n")); sl@0: } sl@0: //If both of the flags are not set, wait. sl@0: if(!(iFlag & EAppendTestResults || iFlag & EDoNotStop)) sl@0: { sl@0: iPrinter->Wait(); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CDatabase::PrintDecisionL(CDecisionRecord& aRecord) sl@0: { sl@0: _LIT(KDelimiter, ";"); sl@0: _LIT(KWriteId, "\"%08x\""); sl@0: _LIT(KWriteString, "\"%S\""); sl@0: _LIT8(KWriteString8,"\"%S\""); sl@0: _LIT8(KWriteHex8, "%02x"); sl@0: sl@0: sl@0: iPrinter->PrintfL(KWriteId,aRecord.iClientSid.iId); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteId,aRecord.iEvaluatorId.iUid); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteId,aRecord.iServiceId.iUid); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteId,aRecord.iServerSid.iId); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: sl@0: TBuf8 hexdump; sl@0: TUint8 *ptr = (TUint8 *)aRecord.iFingerprint.Ptr(); sl@0: TInt i; sl@0: TInt len = aRecord.iFingerprint.Length(); sl@0: sl@0: for(i=0; iPrintf8L(KWriteString8,&hexdump); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: sl@0: if(iFlag & EPrintBinary) sl@0: { sl@0: iPrinter->Printf8L(KWriteString8,&aRecord.iClientEntity); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: } sl@0: else sl@0: { sl@0: hexdump.Zero(); sl@0: ptr = (TUint8 *)aRecord.iClientEntity.Ptr(); sl@0: len = aRecord.iClientEntity.Length(); sl@0: for(i=0; iPrintf8L(KWriteString8,&hexdump); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: } sl@0: sl@0: iPrinter->PrintfL(KWriteString,&aRecord.iDescription); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: sl@0: iPrinter->PrintfL(KWriteId,aRecord.iResult); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteId,aRecord.iEvaluatorInfo); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteId,aRecord.iMajorPolicyVersion); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteId,aRecord.iRecordId); sl@0: sl@0: iPrinter->Printf8L(_L8("\n")); sl@0: } sl@0: sl@0: sl@0: void CDatabase::PrintHeaderL() sl@0: { sl@0: _LIT(KDelimiter, ";"); sl@0: _LIT(KWriteString, "\"%S\""); sl@0: sl@0: iPrinter->PrintfL(KWriteString,&KColClientSid); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColEvaluatorId); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColServiceId); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColServerSid); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColFingerprint); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColClientEntity); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColDescription); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColResult); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColEvaluatorInfo); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColMajorPolicyVersion); sl@0: iPrinter->PrintfL(KDelimiter); sl@0: iPrinter->PrintfL(KWriteString,&KColRecordId); sl@0: iPrinter->Printf8L(_L8("\n")); sl@0: } sl@0: sl@0: CDecisionRecord* CDatabase::ParseAndCreateRecordLC(TDesC8& aLine) sl@0: /** sl@0: Parse a line and create a decision record from the parsed values sl@0: @param aLine A line containing a decision record values quoted with double quotes and separated by semi-colon sl@0: @return A newly created decision record sl@0: */ sl@0: { sl@0: CDecisionRecord* record = NULL; sl@0: sl@0: TLex8 parser(aLine); sl@0: TChar achar; sl@0: TBool start = ETrue; sl@0: sl@0: TUint16 flag = 0x078F; sl@0: TUint16 pos = 0x0001; sl@0: sl@0: TInt32 value=0; sl@0: TUint32 hexVal = 0; sl@0: sl@0: TSecureId clientSid(0); sl@0: TSecureId serverSid(0); sl@0: TUid serviceId = TUid::Null(); sl@0: TUid evaluatorId = TUid::Null(); sl@0: TBuf8 fingerprint; sl@0: TBuf8 clientEntity; sl@0: RBuf description; sl@0: TUint8 result=0; sl@0: TUint32 evaluatorInfo=0; sl@0: TUint16 policyVer=0; sl@0: TUint32 recordId=0; sl@0: sl@0: while(!parser.Eos()) sl@0: { sl@0: //Get cyrrent char sl@0: achar = parser.Get(); sl@0: //Skip delimiter sl@0: if(achar == ';') sl@0: { sl@0: start = ETrue; sl@0: } sl@0: //if double quote start or stop token reading sl@0: if('\"' == achar) sl@0: { sl@0: if(start) sl@0: { sl@0: parser.Mark(); sl@0: start = EFalse; sl@0: } sl@0: else sl@0: { sl@0: parser.UnGet(); sl@0: sl@0: if(flag & pos) sl@0: { sl@0: TLex8 intToken(parser.MarkedToken()); sl@0: switch(pos) sl@0: { sl@0: case KLocClientSid: sl@0: intToken.Val(hexVal,EHex); sl@0: clientSid.iId = hexVal; sl@0: break; sl@0: case KLocEvaluatorId: sl@0: intToken.Val(hexVal,EHex); sl@0: evaluatorId.iUid = hexVal; sl@0: break; sl@0: case KLocServiceId: sl@0: intToken.Val(hexVal,EHex); sl@0: serviceId.iUid = hexVal; sl@0: break; sl@0: case KLocServerSid: sl@0: intToken.Val(hexVal,EHex); sl@0: serverSid.iId = hexVal; sl@0: break; sl@0: case KLocResult: sl@0: intToken.Val(value); sl@0: result = (TUint8)value; sl@0: break; sl@0: case KLocEvaluatorInfo: sl@0: intToken.Val(value); sl@0: evaluatorInfo = value; sl@0: break; sl@0: case KLocMajorPolicyVersion: sl@0: intToken.Val(value); sl@0: policyVer = (TUint16)value; sl@0: break; sl@0: case KLocRecordId: sl@0: intToken.Val(value); sl@0: recordId = (TUint32)value; sl@0: break; sl@0: default: sl@0: User::Leave(KErrGeneral); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: switch(pos) sl@0: { sl@0: case KLocDescription: sl@0: { sl@0: TPtrC8 tmpDescription = parser.MarkedToken(); sl@0: description.Create(tmpDescription.Length()); sl@0: description.CleanupClosePushL(); sl@0: description.Copy(tmpDescription); sl@0: break; sl@0: } sl@0: sl@0: case KLocFingerprint: sl@0: { sl@0: fingerprint = parser.MarkedToken(); sl@0: HexToStrL(fingerprint); sl@0: break; sl@0: } sl@0: case KLocClientEntity: sl@0: { sl@0: clientEntity = parser.MarkedToken(); sl@0: if(!(iFlag & EPrintBinary)) sl@0: { sl@0: HexToStrL(clientEntity); sl@0: } sl@0: break; sl@0: } sl@0: default: sl@0: User::Leave(KErrGeneral); sl@0: } sl@0: } sl@0: sl@0: sl@0: start = ETrue; sl@0: pos = pos<<1; sl@0: sl@0: } sl@0: } sl@0: } sl@0: record = CDecisionRecord::NewL(clientSid,evaluatorId,serviceId,serverSid,fingerprint,clientEntity,description,result,policyVer,evaluatorInfo,recordId); sl@0: CleanupStack::PopAndDestroy(&description); sl@0: CleanupStack::PushL(record); sl@0: return record; sl@0: } sl@0: sl@0: sl@0: TUint8 CDatabase::HexToIntL(TUint8* aPtr) sl@0: /** sl@0: Convert a 2-byte hexadecimal representation value to integer (Ex: C2 -> 194). sl@0: @param aPtr Pointer to source data sl@0: @return An integer value against 2-byte hexadecimal value sl@0: */ sl@0: { sl@0: //Save and then set third byte NULL sl@0: TUint8 temp = aPtr[2]; sl@0: aPtr[2] = '\n'; sl@0: //Create a lex string from first two bytes sl@0: TLex8 lex(aPtr); sl@0: //Convert two bytes hex value (ex:9F) to integer (ex:159) sl@0: TUint8 intVal; sl@0: User::LeaveIfError(lex.Val(intVal,EHex)); sl@0: //Put the original value back sl@0: aPtr[2] = temp; sl@0: //return integer value of first two hex bytes sl@0: return intVal; sl@0: } sl@0: sl@0: sl@0: void CDatabase::HexToStrL(TDes8& aSource) sl@0: /** sl@0: Convert a string containing hexadecimal representation to another string containing binary representation sl@0: @param aSource Source data containing a string sl@0: */ sl@0: { sl@0: TUint8 *pSource = (TUint8 *)aSource.Ptr(); sl@0: TUint8 *pDest = (TUint8 *)aSource.Ptr(); sl@0: sl@0: TInt len = aSource.Length(); sl@0: TInt idxSource; sl@0: TInt idxDest; sl@0: sl@0: for(idxSource=0, idxDest=0; idxSourcePrintfL(_L("\n\n0 tests failed out of 1\n")); sl@0: #else sl@0: iPrinter->PrintfL(_L("\n\nTEST STEP SUMMARY:\n")); sl@0: iPrinter->PrintfL(_L("PASS = 1\n")); sl@0: iPrinter->PrintfL(_L("FAIL = 0\n")); sl@0: iPrinter->PrintfL(_L("ABORT = 0\n")); sl@0: iPrinter->PrintfL(_L("PANIC = 0\n")); sl@0: iPrinter->PrintfL(_L("INCONCLUSIVE = 0\n")); sl@0: iPrinter->PrintfL(_L("UNKNOWN = 0\n")); sl@0: iPrinter->PrintfL(_L("UNEXECUTED = 0\n")); sl@0: iPrinter->PrintfL(_L("COMMENTED COMMAND'S = 0\n")); sl@0: iPrinter->PrintfL(_L("TEST CASE SUMMARY:\n")); sl@0: iPrinter->PrintfL(_L("PASS = 1\n")); sl@0: iPrinter->PrintfL(_L("FAIL = 0\n")); sl@0: iPrinter->PrintfL(_L("INCONCLUSIVE = 0\n")); sl@0: #endif sl@0: } sl@0: sl@0: void CDatabase::PrintTestHeaderL() sl@0: { sl@0: iPrinter->PrintfL(_L("TEST SYNOPSIS:\n")); sl@0: iPrinter->PrintfL(_L("TEF Version : 2.1.2004\n")); sl@0: iPrinter->PrintfL(_L("START_TESTCASE COUNT : 1\n")); sl@0: iPrinter->PrintfL(_L("RUN_TEST_STEP COUNT : 1\n")); sl@0: iPrinter->PrintfL(_L("RUN_TEST_STEP_RESULT COUNT : 1\n\n")); sl@0: } sl@0: