os/security/cryptomgmtlibs/securitytestfw/test/testhandler2/t_errorconverter.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptomgmtlibs/securitytestfw/test/testhandler2/t_errorconverter.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,196 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-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 +* T_CErrorConverter.CPP
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include <s32file.h>
    1.24 +#include "t_errorconverter.h"
    1.25 +#include <securityerr.h>
    1.26 +
    1.27 +
    1.28 +class CErrorItem : public CBase
    1.29 +	{
    1.30 +	public:
    1.31 +		static CErrorItem *NewLC(const TDesC &aErrorName, const TInt aError);
    1.32 +		static CErrorItem *NewL(const TDesC &aErrorName, const TInt aError);
    1.33 +		virtual ~CErrorItem(void) { delete iErrorName;};
    1.34 +
    1.35 +	public:
    1.36 +		HBufC8* iErrorName;
    1.37 +		const TInt iError;
    1.38 +
    1.39 +	private:
    1.40 +		void ConstructL(const TDesC &aErrorName);
    1.41 +		CErrorItem(const TInt aError) : iError(aError){}
    1.42 +	};
    1.43 +
    1.44 +CErrorItem *CErrorItem::NewLC(const TDesC &aErrorName, const TInt aError)
    1.45 +	{
    1.46 +	CErrorItem *errorItem = new (ELeave) CErrorItem(aError);
    1.47 +	CleanupStack::PushL(errorItem);
    1.48 +	errorItem->ConstructL(aErrorName);
    1.49 +	return(errorItem);
    1.50 +	}
    1.51 +
    1.52 +CErrorItem *CErrorItem::NewL(const TDesC &aErrorName, const TInt aError)
    1.53 +	{
    1.54 +	CErrorItem *errorItem = CErrorItem::NewLC(aErrorName, aError);
    1.55 +	CleanupStack::Pop(errorItem);
    1.56 +	return(errorItem);
    1.57 +	};
    1.58 +
    1.59 +void CErrorItem::ConstructL(const TDesC &aErrorName)
    1.60 +	{
    1.61 +	iErrorName = HBufC8::NewL(aErrorName.Length());
    1.62 +	iErrorName->Des().Copy(aErrorName);
    1.63 +	};
    1.64 +
    1.65 +EXPORT_C CErrorConverter* CErrorConverter::NewLC(void)
    1.66 +	{
    1.67 +	CErrorConverter *errorConverter = new (ELeave) CErrorConverter();
    1.68 +	CleanupStack::PushL(errorConverter);
    1.69 +	errorConverter->ConstructL();
    1.70 +	return(errorConverter);
    1.71 +	};
    1.72 +EXPORT_C CErrorConverter* CErrorConverter::NewL(void)
    1.73 +	{
    1.74 +	CErrorConverter *errorConverter = CErrorConverter::NewLC();
    1.75 +	CleanupStack::Pop(errorConverter);
    1.76 +	return(errorConverter);
    1.77 +	};
    1.78 +
    1.79 +EXPORT_C CErrorConverter::~CErrorConverter()
    1.80 +	{
    1.81 +	iErrorMap.ResetAndDestroy();
    1.82 +	};
    1.83 +
    1.84 +void CErrorConverter::ConstructL(void)
    1.85 +	{
    1.86 +	CreateErrorMapL();
    1.87 +	};
    1.88 +
    1.89 +EXPORT_C void CErrorConverter::AddErrorItem(const TDesC& aErrorName, const TInt &aErrorCode)
    1.90 +	{
    1.91 +	CErrorItem *errorItem = CErrorItem::NewLC(aErrorName, aErrorCode);
    1.92 +
    1.93 +	User::LeaveIfError(iErrorMap.Append(errorItem));
    1.94 +	CleanupStack::Pop(errorItem);
    1.95 +	};
    1.96 +
    1.97 +void CErrorConverter::CreateErrorMapL()
    1.98 +	{
    1.99 +	AddErrorItem(_L("KErrNone"), KErrNone);
   1.100 +	AddErrorItem(_L("KErrNotFound"), KErrNotFound);
   1.101 +	AddErrorItem(_L("KErrGeneral"), KErrGeneral);
   1.102 +	AddErrorItem(_L("KErrCancel"), KErrCancel);
   1.103 +	AddErrorItem(_L("KErrNoMemory"), KErrNoMemory);
   1.104 +	AddErrorItem(_L("KErrNotSupported"), KErrNotSupported);
   1.105 +	AddErrorItem(_L("KErrArgument"), KErrArgument);
   1.106 +	AddErrorItem(_L("KErrTotalLossOfPrecision"), KErrTotalLossOfPrecision);
   1.107 +	AddErrorItem(_L("KErrBadHandle"), KErrBadHandle);
   1.108 +	AddErrorItem(_L("KErrOverflow"), KErrOverflow);
   1.109 +	AddErrorItem(_L("KErrUnderflow"), KErrUnderflow);
   1.110 +	AddErrorItem(_L("KErrAlreadyExists"), KErrAlreadyExists);
   1.111 +	AddErrorItem(_L("KErrPathNotFound"), KErrPathNotFound);
   1.112 +	AddErrorItem(_L("KErrDied"), KErrDied);
   1.113 +	AddErrorItem(_L("KErrInUse"), KErrInUse);
   1.114 +	AddErrorItem(_L("KErrServerTerminated"), KErrServerTerminated);
   1.115 +	AddErrorItem(_L("KErrServerBusy"), KErrServerBusy);
   1.116 +	AddErrorItem(_L("KErrCompletion"), KErrCompletion);
   1.117 +	AddErrorItem(_L("KErrNotReady"), KErrNotReady);
   1.118 +	AddErrorItem(_L("KErrUnknown"), KErrUnknown);
   1.119 +	AddErrorItem(_L("KErrCorrupt"), KErrCorrupt);
   1.120 +	AddErrorItem(_L("KErrAccessDenied"), KErrAccessDenied);
   1.121 +	AddErrorItem(_L("KErrLocked"), KErrLocked);
   1.122 +	AddErrorItem(_L("KErrWrite"), KErrWrite);
   1.123 +	AddErrorItem(_L("KErrDisMounted"), KErrDisMounted);
   1.124 +	AddErrorItem(_L("KErrEof"), KErrEof);
   1.125 +	AddErrorItem(_L("KErrDiskFull"), KErrDiskFull);
   1.126 +	AddErrorItem(_L("KErrBadDriver"), KErrBadDriver);
   1.127 +	AddErrorItem(_L("KErrBadName"), KErrBadName);
   1.128 +	AddErrorItem(_L("KErrCommsLineFail"), KErrCommsLineFail);
   1.129 +	AddErrorItem(_L("KErrCommsFrame"), KErrCommsFrame);
   1.130 +	AddErrorItem(_L("KErrCommsOverrun"), KErrCommsOverrun);
   1.131 +	AddErrorItem(_L("KErrCommsParity"), KErrCommsParity);
   1.132 +	AddErrorItem(_L("KErrTimedOut"), KErrTimedOut);
   1.133 +	AddErrorItem(_L("KErrCouldNotConnect"), KErrCouldNotConnect);
   1.134 +	AddErrorItem(_L("KErrCouldNotDisconnect"), KErrCouldNotDisconnect);
   1.135 +	AddErrorItem(_L("KErrDisconnected"), KErrDisconnected);
   1.136 +	AddErrorItem(_L("KErrBadLibraryEntryPoint"), KErrBadLibraryEntryPoint);
   1.137 +	AddErrorItem(_L("KErrBadDescriptor"), KErrBadDescriptor);
   1.138 +	AddErrorItem(_L("KErrAbort"), KErrAbort);
   1.139 +	AddErrorItem(_L("KErrTooBig"), KErrTooBig);
   1.140 +	AddErrorItem(_L("KErrDivideByZero"), KErrDivideByZero);
   1.141 +	AddErrorItem(_L("KErrBadPower"), KErrBadPower);
   1.142 +	AddErrorItem(_L("KErrDirFull"), KErrDirFull);
   1.143 +	AddErrorItem(_L("KErrHardwareNotAvailable"), KErrHardwareNotAvailable);
   1.144 +	AddErrorItem(_L("KErrPrivateKeyNotFound"), KErrPrivateKeyNotFound);
   1.145 +	AddErrorItem(_L("KErrPermissionDenied"), KErrPermissionDenied);
   1.146 +	}
   1.147 +
   1.148 +EXPORT_C TBool CErrorConverter::GetExpectedResultL(const TDesC& aResult /* in */, TInt &aError /* out */)
   1.149 +	{
   1.150 +	HBufC8 *resultBuf = HBufC8::NewLC(aResult.Length());
   1.151 +	resultBuf->Des().Copy(aResult);
   1.152 +	TBool result = EFalse;
   1.153 +	CErrorItem *pError;
   1.154 +	TInt errorPos=0;
   1.155 +
   1.156 +	aError = KErrUnknown;
   1.157 +
   1.158 +	for(errorPos = 0; errorPos < iErrorMap.Count() && !result; errorPos++)
   1.159 +		{
   1.160 +		pError = iErrorMap[errorPos];
   1.161 +		if(pError->iErrorName->CompareF(*resultBuf)==0)
   1.162 +			{
   1.163 +			aError = pError->iError;
   1.164 +			result = ETrue;
   1.165 +			};
   1.166 +		}
   1.167 +	CleanupStack::PopAndDestroy(resultBuf);
   1.168 +	
   1.169 +	return(result);
   1.170 +	}
   1.171 +
   1.172 +
   1.173 +EXPORT_C TBool CErrorConverter::GetExpectedResultL(const TInt &aError /* in */, HBufC*& aResult /* out */)
   1.174 +	{
   1.175 +	TBool result = EFalse;
   1.176 +	TInt errorPos=0;
   1.177 +	CErrorItem *pError;
   1.178 +
   1.179 +	__ASSERT_DEBUG(!result, User::Panic(_L("CErrorConverter::GetExpectedResultL"), 1));
   1.180 +	for(errorPos = 0; errorPos < iErrorMap.Count() && !result; errorPos++)
   1.181 +		{
   1.182 +		pError = iErrorMap[errorPos];
   1.183 +		if(pError->iError == aError)
   1.184 +			{
   1.185 +			aResult = HBufC::NewLC(pError->iErrorName->Length());
   1.186 +			aResult->Des().Copy(*pError->iErrorName);
   1.187 +			result = ETrue;
   1.188 +			};
   1.189 +		}
   1.190 +	// just so it always has something to pop even if it didnt find error
   1.191 +	if(!result)
   1.192 +		{
   1.193 +		aResult = HBufC::NewLC(0);
   1.194 +		};
   1.195 +
   1.196 +	return(result);
   1.197 +	}
   1.198 +
   1.199 +