os/security/authorisation/userpromptservice/database/test/dumpupsdb/source/dumpupsdb.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/authorisation/userpromptservice/database/test/dumpupsdb/source/dumpupsdb.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,322 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Defines a tool to export/import UPS Decision Database
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @test
    1.26 +*/
    1.27 +
    1.28 +#ifndef DUMPUPSDB_H
    1.29 +#define DUMPUPSDB_H
    1.30 + 
    1.31 +#include "upsdbw.h"
    1.32 +#include "activewaiter.h"
    1.33 +
    1.34 +#include <e32std.h>
    1.35 +#include <f32file.h>
    1.36 +#include <e32cons.h>
    1.37 +#include <bacline.h>
    1.38 +#include <utf.h>
    1.39 +
    1.40 +using namespace UserPromptService;
    1.41 +
    1.42 +
    1.43 +_LIT(KAppName, "UPS Decision Database Dump Tool");
    1.44 +
    1.45 +//Set flag values
    1.46 +static const TUint16 KLocClientSid			= 0x0001;
    1.47 +static const TUint16 KLocEvaluatorId		= 0x0002;
    1.48 +static const TUint16 KLocServiceId			= 0x0004;
    1.49 +static const TUint16 KLocServerSid			= 0x0008;
    1.50 +static const TUint16 KLocFingerprint		= 0x0010;
    1.51 +static const TUint16 KLocClientEntity		= 0x0020;
    1.52 +static const TUint16 KLocDescription		= 0x0040;
    1.53 +static const TUint16 KLocResult				= 0x0080;
    1.54 +static const TUint16 KLocEvaluatorInfo		= 0x0100;
    1.55 +static const TUint16 KLocMajorPolicyVersion	= 0x0200;
    1.56 +static const TUint16 KLocRecordId			= 0x0400;
    1.57 +
    1.58 +//
    1.59 +//CPrinter
    1.60 +//
    1.61 +
    1.62 +class CPrinter : public CBase
    1.63 +/**
    1.64 + Class that prints text to the console and optionally writes to a log file.
    1.65 + */
    1.66 +	{
    1.67 +public:
    1.68 +	static CPrinter* NewLC(CConsoleBase* aConsole);
    1.69 +	static CPrinter* NewLC(CConsoleBase* aConsole, RFile& aFile);
    1.70 +	static void Usage(CConsoleBase* aConsole);
    1.71 +	
    1.72 +	~CPrinter();
    1.73 +	
    1.74 +	void PrintfL(TRefByValue<const TDesC16> aFormat, ...);
    1.75 +	void Printf8L(TRefByValue<const TDesC8> aFormat, ...);
    1.76 +	void PrintOnlyConsoleL(const TDesC16& aFormat, ...);
    1.77 +	void Wait();
    1.78 +	void ReadNextLineL(TDes8& aLine);
    1.79 +	TInt FileSizeL();
    1.80 +
    1.81 +private	:
    1.82 +	CPrinter(CConsoleBase* aConsole);
    1.83 +	void ConstructL(RFile& aFile);
    1.84 +
    1.85 +private:
    1.86 +	/** Console object to print text to the console*/
    1.87 +	CConsoleBase* iConsole;
    1.88 +	
    1.89 +	/** Optional file handle to write text to the file*/
    1.90 +	RFile iFile;
    1.91 +	
    1.92 +	/** Whether to log to the output to the file */
    1.93 +	TBool iLogToFile;
    1.94 +	
    1.95 +	/** Temporary 16-bit buffer*/
    1.96 +	TBuf<256> iBuffer;
    1.97 +	
    1.98 +	/** Temporary 8-bit buffer*/
    1.99 +	TBuf8<256> iBuffer8;
   1.100 +	
   1.101 +	/** File stream handle*/
   1.102 +	RFileReadStream iReader;
   1.103 +	};
   1.104 +
   1.105 +//
   1.106 +//CDatabase
   1.107 +//
   1.108 +
   1.109 +class CDatabase : public CBase
   1.110 +/**
   1.111 +	This class connects to the database and import/exports it to the console/file
   1.112 + */
   1.113 +	{
   1.114 +public:
   1.115 +	static CDatabase* NewLC(CConsoleBase* aConsole, RFs& aFs, TBool aImport, const TDesC& aDb, const TDesC& aFile);
   1.116 +	
   1.117 +	~CDatabase();
   1.118 +	
   1.119 +	void DumpL();
   1.120 +
   1.121 +private:
   1.122 +	CDatabase(TBool aImport);
   1.123 +	void ConstructL(CConsoleBase* aConsole, RFs& aFs, const TDesC& aDb, const TDesC& aFile);
   1.124 +	
   1.125 +	void PrintDecisionL(CDecisionRecord& aRecord);
   1.126 +	void PrintHeaderL();
   1.127 +	CDecisionRecord* ParseAndCreateRecordLC(TDesC8& aLine);
   1.128 +	TUint8 HexToIntL(TUint8* aPtr);
   1.129 +	void HexToStrL(TDes8& aSource);
   1.130 +	void PrintTestResultsL();
   1.131 +	void PrintTestHeaderL();
   1.132 +	
   1.133 +private:
   1.134 +	/** Indicates the operation type: Import Db is ETrue and Export Db is EFalse*/	
   1.135 +	TBool iImport;
   1.136 +	
   1.137 +	/** Handle to the decision database*/
   1.138 +	CDecisionDbW* iUpsDb;
   1.139 +	
   1.140 +	/** Handle to the printer object*/
   1.141 +	CPrinter* iPrinter;
   1.142 +	
   1.143 +	/** Optional file handle to write text to the file*/
   1.144 +	RFile iFile;
   1.145 +
   1.146 +public:	
   1.147 +	/** Enumerated values to arrange run options. */
   1.148 +	enum TDbOptions
   1.149 +		{
   1.150 +		/** Indicates whether the dummy test result lines are written into the output file. */
   1.151 +		EAppendTestResults = 0x01,
   1.152 +		/** Indicates whether the command line waits for the user input at the end of the operation.*/
   1.153 +		EDoNotStop = 0x02,
   1.154 +		/** Indicates that fingerprint and client entity fields are printed as binary instead hexadecimal. */
   1.155 +		EPrintBinary = 0x04
   1.156 +		};
   1.157 +	
   1.158 +	TUint8 iFlag;	///< A flag for running options.
   1.159 +	};
   1.160 +
   1.161 +
   1.162 +static void MainL(void)
   1.163 +/**
   1.164 +Takes a User Prompt Service decision database file and dumps it as human readable text to the 
   1.165 +console. The user may also specify the name of an output file on the command line. If so, text
   1.166 +is also written to this file. Also recreates a UPS decision database from a dumped file. Where
   1.167 +the database is created is specified by the user.
   1.168 +*/
   1.169 +	{
   1.170 +	
   1.171 +	RFs fs;
   1.172 +	
   1.173 +	User::LeaveIfError(fs.Connect());
   1.174 +	CleanupClosePushL(fs);
   1.175 +	
   1.176 +	CConsoleBase *console = Console::NewL(KAppName,TSize(KDefaultConsWidth, KDefaultConsHeight));
   1.177 +	CleanupStack::PushL(console);
   1.178 +	
   1.179 +	CCommandLineArguments *cmdLineArgs = CCommandLineArguments::NewLC();
   1.180 +	
   1.181 +	TInt argc;
   1.182 +	argc = cmdLineArgs->Count();
   1.183 +	
   1.184 +	//At least operation type and database file path must be given
   1.185 +	if(argc < 4)
   1.186 +		{
   1.187 +		CPrinter::Usage(console);
   1.188 +		User::Leave(KErrArgument);
   1.189 +		}
   1.190 +	
   1.191 +	TInt	loopCount(1);
   1.192 +	TBool	import(EFalse);
   1.193 +	TUint16 *dbPath(0);
   1.194 +	TInt	dbPathLen(0);
   1.195 +	TUint16 *filePath(0);
   1.196 +	TInt	filePathLen(0);
   1.197 +	TUint8	flag(0);
   1.198 +	
   1.199 +	while(loopCount+1 <= argc)
   1.200 +		{
   1.201 +		TPtrC argv = cmdLineArgs->Arg(loopCount);
   1.202 +		
   1.203 +		if(argv.Locate('-'))
   1.204 +			{
   1.205 +			break;
   1.206 +			}
   1.207 +			
   1.208 +		switch(argv.Ptr()[1])
   1.209 +			{
   1.210 +			case 'i':
   1.211 +			case 'I':
   1.212 +				{//the operation type is import
   1.213 +				import = ETrue;
   1.214 +				break;
   1.215 +				}
   1.216 +			case 'e':
   1.217 +			case 'E':
   1.218 +				{//the operation type is export
   1.219 +				import = EFalse;
   1.220 +				break;
   1.221 +				}
   1.222 +			case 'd':
   1.223 +			case 'D':
   1.224 +				{//the decision database file path
   1.225 +				TPtrC path = cmdLineArgs->Arg(++loopCount);
   1.226 +				dbPath = (TUint16 *)path.Ptr();
   1.227 +				dbPathLen = path.Length();
   1.228 +				break;
   1.229 +				}
   1.230 +			case 'f': 
   1.231 +			case 'F':
   1.232 +				{//the file which the database will be dumped to or populated from
   1.233 +				TPtrC path = cmdLineArgs->Arg(++loopCount);
   1.234 +				filePath = (TUint16 *)path.Ptr();
   1.235 +				filePathLen = path.Length();
   1.236 +				break;
   1.237 +				}
   1.238 +			case 's':
   1.239 +			case 'S':
   1.240 +				{//This parameter is not published in the help page
   1.241 +				 //It's been added to get rid of waiting at the end of the program.
   1.242 +				flag |= CDatabase::EDoNotStop;
   1.243 +				break;	
   1.244 +				}
   1.245 +			case 't':
   1.246 +			case 'T':
   1.247 +				{//This parameter is not published in the help page
   1.248 +				 //It's been added to get rid of parsing error in the test environment.
   1.249 +				flag |= CDatabase::EAppendTestResults;
   1.250 +				break;	
   1.251 +				}
   1.252 +			case 'b':
   1.253 +			case 'B':
   1.254 +				{//This parameter is not published in the help page
   1.255 +				 //It's been added to print fields as binary instead hexadecimal.
   1.256 +				flag |= CDatabase::EPrintBinary;
   1.257 +				break;	
   1.258 +				}
   1.259 +			default:
   1.260 +				break;
   1.261 +			}//switch
   1.262 +		
   1.263 +		++loopCount;
   1.264 +			
   1.265 +		}//while loop
   1.266 +
   1.267 +	if(loopCount != argc || !dbPath || (import && !filePath))
   1.268 +		{
   1.269 +		CPrinter::Usage(console);
   1.270 +		User::Leave(KErrArgument);
   1.271 +		}
   1.272 +	
   1.273 +	TPtr db(dbPath,dbPathLen);
   1.274 +	db.SetLength(dbPathLen);
   1.275 +	
   1.276 +	CDatabase* database = NULL;	
   1.277 +	
   1.278 +	if(filePath)
   1.279 +		{
   1.280 +		TPtr file(filePath,filePathLen);
   1.281 +		file.SetLength(filePathLen);
   1.282 +		database = CDatabase::NewLC(console, fs, import, db, file);
   1.283 +		}
   1.284 +	else
   1.285 +		{
   1.286 +		database = CDatabase::NewLC(console, fs, import, db, KNullDesC);
   1.287 +		}
   1.288 +	
   1.289 +	database->iFlag = flag;
   1.290 +		
   1.291 +	//Do actual import or export database operation	
   1.292 +	database->DumpL();
   1.293 +	
   1.294 +	CleanupStack::PopAndDestroy(4, &fs);
   1.295 +	}
   1.296 +
   1.297 +
   1.298 +GLDEF_C TInt E32Main()
   1.299 +	{
   1.300 +	CTrapCleanup *dumpupsdbCleanup = CTrapCleanup::New();
   1.301 +	if(dumpupsdbCleanup == NULL)
   1.302 +		{
   1.303 +		return KErrNoMemory;
   1.304 +		}
   1.305 +	
   1.306 +	CActiveScheduler* dumpupsdbScheduler = new(ELeave) CActiveScheduler;
   1.307 +	CActiveScheduler::Install(dumpupsdbScheduler);
   1.308 +		
   1.309 +	__UHEAP_MARK;
   1.310 +	
   1.311 +	TRAPD(retval,MainL());
   1.312 +	
   1.313 +	__UHEAP_MARKEND;
   1.314 +	
   1.315 +	delete dumpupsdbCleanup;
   1.316 +	delete dumpupsdbScheduler;
   1.317 +	
   1.318 +	if(retval == KErrArgument)
   1.319 +		{
   1.320 +		return KErrNone;
   1.321 +		}
   1.322 +	return retval;
   1.323 +	}
   1.324 +	
   1.325 +#endif