os/security/authorisation/userpromptservice/database/test/dumpupsdb/source/dumpupsdb.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 * Defines a tool to export/import UPS Decision Database
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file
    22  @test
    23 */
    24 
    25 #ifndef DUMPUPSDB_H
    26 #define DUMPUPSDB_H
    27  
    28 #include "upsdbw.h"
    29 #include "activewaiter.h"
    30 
    31 #include <e32std.h>
    32 #include <f32file.h>
    33 #include <e32cons.h>
    34 #include <bacline.h>
    35 #include <utf.h>
    36 
    37 using namespace UserPromptService;
    38 
    39 
    40 _LIT(KAppName, "UPS Decision Database Dump Tool");
    41 
    42 //Set flag values
    43 static const TUint16 KLocClientSid			= 0x0001;
    44 static const TUint16 KLocEvaluatorId		= 0x0002;
    45 static const TUint16 KLocServiceId			= 0x0004;
    46 static const TUint16 KLocServerSid			= 0x0008;
    47 static const TUint16 KLocFingerprint		= 0x0010;
    48 static const TUint16 KLocClientEntity		= 0x0020;
    49 static const TUint16 KLocDescription		= 0x0040;
    50 static const TUint16 KLocResult				= 0x0080;
    51 static const TUint16 KLocEvaluatorInfo		= 0x0100;
    52 static const TUint16 KLocMajorPolicyVersion	= 0x0200;
    53 static const TUint16 KLocRecordId			= 0x0400;
    54 
    55 //
    56 //CPrinter
    57 //
    58 
    59 class CPrinter : public CBase
    60 /**
    61  Class that prints text to the console and optionally writes to a log file.
    62  */
    63 	{
    64 public:
    65 	static CPrinter* NewLC(CConsoleBase* aConsole);
    66 	static CPrinter* NewLC(CConsoleBase* aConsole, RFile& aFile);
    67 	static void Usage(CConsoleBase* aConsole);
    68 	
    69 	~CPrinter();
    70 	
    71 	void PrintfL(TRefByValue<const TDesC16> aFormat, ...);
    72 	void Printf8L(TRefByValue<const TDesC8> aFormat, ...);
    73 	void PrintOnlyConsoleL(const TDesC16& aFormat, ...);
    74 	void Wait();
    75 	void ReadNextLineL(TDes8& aLine);
    76 	TInt FileSizeL();
    77 
    78 private	:
    79 	CPrinter(CConsoleBase* aConsole);
    80 	void ConstructL(RFile& aFile);
    81 
    82 private:
    83 	/** Console object to print text to the console*/
    84 	CConsoleBase* iConsole;
    85 	
    86 	/** Optional file handle to write text to the file*/
    87 	RFile iFile;
    88 	
    89 	/** Whether to log to the output to the file */
    90 	TBool iLogToFile;
    91 	
    92 	/** Temporary 16-bit buffer*/
    93 	TBuf<256> iBuffer;
    94 	
    95 	/** Temporary 8-bit buffer*/
    96 	TBuf8<256> iBuffer8;
    97 	
    98 	/** File stream handle*/
    99 	RFileReadStream iReader;
   100 	};
   101 
   102 //
   103 //CDatabase
   104 //
   105 
   106 class CDatabase : public CBase
   107 /**
   108 	This class connects to the database and import/exports it to the console/file
   109  */
   110 	{
   111 public:
   112 	static CDatabase* NewLC(CConsoleBase* aConsole, RFs& aFs, TBool aImport, const TDesC& aDb, const TDesC& aFile);
   113 	
   114 	~CDatabase();
   115 	
   116 	void DumpL();
   117 
   118 private:
   119 	CDatabase(TBool aImport);
   120 	void ConstructL(CConsoleBase* aConsole, RFs& aFs, const TDesC& aDb, const TDesC& aFile);
   121 	
   122 	void PrintDecisionL(CDecisionRecord& aRecord);
   123 	void PrintHeaderL();
   124 	CDecisionRecord* ParseAndCreateRecordLC(TDesC8& aLine);
   125 	TUint8 HexToIntL(TUint8* aPtr);
   126 	void HexToStrL(TDes8& aSource);
   127 	void PrintTestResultsL();
   128 	void PrintTestHeaderL();
   129 	
   130 private:
   131 	/** Indicates the operation type: Import Db is ETrue and Export Db is EFalse*/	
   132 	TBool iImport;
   133 	
   134 	/** Handle to the decision database*/
   135 	CDecisionDbW* iUpsDb;
   136 	
   137 	/** Handle to the printer object*/
   138 	CPrinter* iPrinter;
   139 	
   140 	/** Optional file handle to write text to the file*/
   141 	RFile iFile;
   142 
   143 public:	
   144 	/** Enumerated values to arrange run options. */
   145 	enum TDbOptions
   146 		{
   147 		/** Indicates whether the dummy test result lines are written into the output file. */
   148 		EAppendTestResults = 0x01,
   149 		/** Indicates whether the command line waits for the user input at the end of the operation.*/
   150 		EDoNotStop = 0x02,
   151 		/** Indicates that fingerprint and client entity fields are printed as binary instead hexadecimal. */
   152 		EPrintBinary = 0x04
   153 		};
   154 	
   155 	TUint8 iFlag;	///< A flag for running options.
   156 	};
   157 
   158 
   159 static void MainL(void)
   160 /**
   161 Takes a User Prompt Service decision database file and dumps it as human readable text to the 
   162 console. The user may also specify the name of an output file on the command line. If so, text
   163 is also written to this file. Also recreates a UPS decision database from a dumped file. Where
   164 the database is created is specified by the user.
   165 */
   166 	{
   167 	
   168 	RFs fs;
   169 	
   170 	User::LeaveIfError(fs.Connect());
   171 	CleanupClosePushL(fs);
   172 	
   173 	CConsoleBase *console = Console::NewL(KAppName,TSize(KDefaultConsWidth, KDefaultConsHeight));
   174 	CleanupStack::PushL(console);
   175 	
   176 	CCommandLineArguments *cmdLineArgs = CCommandLineArguments::NewLC();
   177 	
   178 	TInt argc;
   179 	argc = cmdLineArgs->Count();
   180 	
   181 	//At least operation type and database file path must be given
   182 	if(argc < 4)
   183 		{
   184 		CPrinter::Usage(console);
   185 		User::Leave(KErrArgument);
   186 		}
   187 	
   188 	TInt	loopCount(1);
   189 	TBool	import(EFalse);
   190 	TUint16 *dbPath(0);
   191 	TInt	dbPathLen(0);
   192 	TUint16 *filePath(0);
   193 	TInt	filePathLen(0);
   194 	TUint8	flag(0);
   195 	
   196 	while(loopCount+1 <= argc)
   197 		{
   198 		TPtrC argv = cmdLineArgs->Arg(loopCount);
   199 		
   200 		if(argv.Locate('-'))
   201 			{
   202 			break;
   203 			}
   204 			
   205 		switch(argv.Ptr()[1])
   206 			{
   207 			case 'i':
   208 			case 'I':
   209 				{//the operation type is import
   210 				import = ETrue;
   211 				break;
   212 				}
   213 			case 'e':
   214 			case 'E':
   215 				{//the operation type is export
   216 				import = EFalse;
   217 				break;
   218 				}
   219 			case 'd':
   220 			case 'D':
   221 				{//the decision database file path
   222 				TPtrC path = cmdLineArgs->Arg(++loopCount);
   223 				dbPath = (TUint16 *)path.Ptr();
   224 				dbPathLen = path.Length();
   225 				break;
   226 				}
   227 			case 'f': 
   228 			case 'F':
   229 				{//the file which the database will be dumped to or populated from
   230 				TPtrC path = cmdLineArgs->Arg(++loopCount);
   231 				filePath = (TUint16 *)path.Ptr();
   232 				filePathLen = path.Length();
   233 				break;
   234 				}
   235 			case 's':
   236 			case 'S':
   237 				{//This parameter is not published in the help page
   238 				 //It's been added to get rid of waiting at the end of the program.
   239 				flag |= CDatabase::EDoNotStop;
   240 				break;	
   241 				}
   242 			case 't':
   243 			case 'T':
   244 				{//This parameter is not published in the help page
   245 				 //It's been added to get rid of parsing error in the test environment.
   246 				flag |= CDatabase::EAppendTestResults;
   247 				break;	
   248 				}
   249 			case 'b':
   250 			case 'B':
   251 				{//This parameter is not published in the help page
   252 				 //It's been added to print fields as binary instead hexadecimal.
   253 				flag |= CDatabase::EPrintBinary;
   254 				break;	
   255 				}
   256 			default:
   257 				break;
   258 			}//switch
   259 		
   260 		++loopCount;
   261 			
   262 		}//while loop
   263 
   264 	if(loopCount != argc || !dbPath || (import && !filePath))
   265 		{
   266 		CPrinter::Usage(console);
   267 		User::Leave(KErrArgument);
   268 		}
   269 	
   270 	TPtr db(dbPath,dbPathLen);
   271 	db.SetLength(dbPathLen);
   272 	
   273 	CDatabase* database = NULL;	
   274 	
   275 	if(filePath)
   276 		{
   277 		TPtr file(filePath,filePathLen);
   278 		file.SetLength(filePathLen);
   279 		database = CDatabase::NewLC(console, fs, import, db, file);
   280 		}
   281 	else
   282 		{
   283 		database = CDatabase::NewLC(console, fs, import, db, KNullDesC);
   284 		}
   285 	
   286 	database->iFlag = flag;
   287 		
   288 	//Do actual import or export database operation	
   289 	database->DumpL();
   290 	
   291 	CleanupStack::PopAndDestroy(4, &fs);
   292 	}
   293 
   294 
   295 GLDEF_C TInt E32Main()
   296 	{
   297 	CTrapCleanup *dumpupsdbCleanup = CTrapCleanup::New();
   298 	if(dumpupsdbCleanup == NULL)
   299 		{
   300 		return KErrNoMemory;
   301 		}
   302 	
   303 	CActiveScheduler* dumpupsdbScheduler = new(ELeave) CActiveScheduler;
   304 	CActiveScheduler::Install(dumpupsdbScheduler);
   305 		
   306 	__UHEAP_MARK;
   307 	
   308 	TRAPD(retval,MainL());
   309 	
   310 	__UHEAP_MARKEND;
   311 	
   312 	delete dumpupsdbCleanup;
   313 	delete dumpupsdbScheduler;
   314 	
   315 	if(retval == KErrArgument)
   316 		{
   317 		return KErrNone;
   318 		}
   319 	return retval;
   320 	}
   321 	
   322 #endif