os/security/cryptomgmtlibs/securitytestfw/test/testhandler2/t_errorconverter.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1998-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 * T_CErrorConverter.CPP
    16 *
    17 */
    18 
    19 
    20 #include <s32file.h>
    21 #include "t_errorconverter.h"
    22 #include <securityerr.h>
    23 
    24 
    25 class CErrorItem : public CBase
    26 	{
    27 	public:
    28 		static CErrorItem *NewLC(const TDesC &aErrorName, const TInt aError);
    29 		static CErrorItem *NewL(const TDesC &aErrorName, const TInt aError);
    30 		virtual ~CErrorItem(void) { delete iErrorName;};
    31 
    32 	public:
    33 		HBufC8* iErrorName;
    34 		const TInt iError;
    35 
    36 	private:
    37 		void ConstructL(const TDesC &aErrorName);
    38 		CErrorItem(const TInt aError) : iError(aError){}
    39 	};
    40 
    41 CErrorItem *CErrorItem::NewLC(const TDesC &aErrorName, const TInt aError)
    42 	{
    43 	CErrorItem *errorItem = new (ELeave) CErrorItem(aError);
    44 	CleanupStack::PushL(errorItem);
    45 	errorItem->ConstructL(aErrorName);
    46 	return(errorItem);
    47 	}
    48 
    49 CErrorItem *CErrorItem::NewL(const TDesC &aErrorName, const TInt aError)
    50 	{
    51 	CErrorItem *errorItem = CErrorItem::NewLC(aErrorName, aError);
    52 	CleanupStack::Pop(errorItem);
    53 	return(errorItem);
    54 	};
    55 
    56 void CErrorItem::ConstructL(const TDesC &aErrorName)
    57 	{
    58 	iErrorName = HBufC8::NewL(aErrorName.Length());
    59 	iErrorName->Des().Copy(aErrorName);
    60 	};
    61 
    62 EXPORT_C CErrorConverter* CErrorConverter::NewLC(void)
    63 	{
    64 	CErrorConverter *errorConverter = new (ELeave) CErrorConverter();
    65 	CleanupStack::PushL(errorConverter);
    66 	errorConverter->ConstructL();
    67 	return(errorConverter);
    68 	};
    69 EXPORT_C CErrorConverter* CErrorConverter::NewL(void)
    70 	{
    71 	CErrorConverter *errorConverter = CErrorConverter::NewLC();
    72 	CleanupStack::Pop(errorConverter);
    73 	return(errorConverter);
    74 	};
    75 
    76 EXPORT_C CErrorConverter::~CErrorConverter()
    77 	{
    78 	iErrorMap.ResetAndDestroy();
    79 	};
    80 
    81 void CErrorConverter::ConstructL(void)
    82 	{
    83 	CreateErrorMapL();
    84 	};
    85 
    86 EXPORT_C void CErrorConverter::AddErrorItem(const TDesC& aErrorName, const TInt &aErrorCode)
    87 	{
    88 	CErrorItem *errorItem = CErrorItem::NewLC(aErrorName, aErrorCode);
    89 
    90 	User::LeaveIfError(iErrorMap.Append(errorItem));
    91 	CleanupStack::Pop(errorItem);
    92 	};
    93 
    94 void CErrorConverter::CreateErrorMapL()
    95 	{
    96 	AddErrorItem(_L("KErrNone"), KErrNone);
    97 	AddErrorItem(_L("KErrNotFound"), KErrNotFound);
    98 	AddErrorItem(_L("KErrGeneral"), KErrGeneral);
    99 	AddErrorItem(_L("KErrCancel"), KErrCancel);
   100 	AddErrorItem(_L("KErrNoMemory"), KErrNoMemory);
   101 	AddErrorItem(_L("KErrNotSupported"), KErrNotSupported);
   102 	AddErrorItem(_L("KErrArgument"), KErrArgument);
   103 	AddErrorItem(_L("KErrTotalLossOfPrecision"), KErrTotalLossOfPrecision);
   104 	AddErrorItem(_L("KErrBadHandle"), KErrBadHandle);
   105 	AddErrorItem(_L("KErrOverflow"), KErrOverflow);
   106 	AddErrorItem(_L("KErrUnderflow"), KErrUnderflow);
   107 	AddErrorItem(_L("KErrAlreadyExists"), KErrAlreadyExists);
   108 	AddErrorItem(_L("KErrPathNotFound"), KErrPathNotFound);
   109 	AddErrorItem(_L("KErrDied"), KErrDied);
   110 	AddErrorItem(_L("KErrInUse"), KErrInUse);
   111 	AddErrorItem(_L("KErrServerTerminated"), KErrServerTerminated);
   112 	AddErrorItem(_L("KErrServerBusy"), KErrServerBusy);
   113 	AddErrorItem(_L("KErrCompletion"), KErrCompletion);
   114 	AddErrorItem(_L("KErrNotReady"), KErrNotReady);
   115 	AddErrorItem(_L("KErrUnknown"), KErrUnknown);
   116 	AddErrorItem(_L("KErrCorrupt"), KErrCorrupt);
   117 	AddErrorItem(_L("KErrAccessDenied"), KErrAccessDenied);
   118 	AddErrorItem(_L("KErrLocked"), KErrLocked);
   119 	AddErrorItem(_L("KErrWrite"), KErrWrite);
   120 	AddErrorItem(_L("KErrDisMounted"), KErrDisMounted);
   121 	AddErrorItem(_L("KErrEof"), KErrEof);
   122 	AddErrorItem(_L("KErrDiskFull"), KErrDiskFull);
   123 	AddErrorItem(_L("KErrBadDriver"), KErrBadDriver);
   124 	AddErrorItem(_L("KErrBadName"), KErrBadName);
   125 	AddErrorItem(_L("KErrCommsLineFail"), KErrCommsLineFail);
   126 	AddErrorItem(_L("KErrCommsFrame"), KErrCommsFrame);
   127 	AddErrorItem(_L("KErrCommsOverrun"), KErrCommsOverrun);
   128 	AddErrorItem(_L("KErrCommsParity"), KErrCommsParity);
   129 	AddErrorItem(_L("KErrTimedOut"), KErrTimedOut);
   130 	AddErrorItem(_L("KErrCouldNotConnect"), KErrCouldNotConnect);
   131 	AddErrorItem(_L("KErrCouldNotDisconnect"), KErrCouldNotDisconnect);
   132 	AddErrorItem(_L("KErrDisconnected"), KErrDisconnected);
   133 	AddErrorItem(_L("KErrBadLibraryEntryPoint"), KErrBadLibraryEntryPoint);
   134 	AddErrorItem(_L("KErrBadDescriptor"), KErrBadDescriptor);
   135 	AddErrorItem(_L("KErrAbort"), KErrAbort);
   136 	AddErrorItem(_L("KErrTooBig"), KErrTooBig);
   137 	AddErrorItem(_L("KErrDivideByZero"), KErrDivideByZero);
   138 	AddErrorItem(_L("KErrBadPower"), KErrBadPower);
   139 	AddErrorItem(_L("KErrDirFull"), KErrDirFull);
   140 	AddErrorItem(_L("KErrHardwareNotAvailable"), KErrHardwareNotAvailable);
   141 	AddErrorItem(_L("KErrPrivateKeyNotFound"), KErrPrivateKeyNotFound);
   142 	AddErrorItem(_L("KErrPermissionDenied"), KErrPermissionDenied);
   143 	}
   144 
   145 EXPORT_C TBool CErrorConverter::GetExpectedResultL(const TDesC& aResult /* in */, TInt &aError /* out */)
   146 	{
   147 	HBufC8 *resultBuf = HBufC8::NewLC(aResult.Length());
   148 	resultBuf->Des().Copy(aResult);
   149 	TBool result = EFalse;
   150 	CErrorItem *pError;
   151 	TInt errorPos=0;
   152 
   153 	aError = KErrUnknown;
   154 
   155 	for(errorPos = 0; errorPos < iErrorMap.Count() && !result; errorPos++)
   156 		{
   157 		pError = iErrorMap[errorPos];
   158 		if(pError->iErrorName->CompareF(*resultBuf)==0)
   159 			{
   160 			aError = pError->iError;
   161 			result = ETrue;
   162 			};
   163 		}
   164 	CleanupStack::PopAndDestroy(resultBuf);
   165 	
   166 	return(result);
   167 	}
   168 
   169 
   170 EXPORT_C TBool CErrorConverter::GetExpectedResultL(const TInt &aError /* in */, HBufC*& aResult /* out */)
   171 	{
   172 	TBool result = EFalse;
   173 	TInt errorPos=0;
   174 	CErrorItem *pError;
   175 
   176 	__ASSERT_DEBUG(!result, User::Panic(_L("CErrorConverter::GetExpectedResultL"), 1));
   177 	for(errorPos = 0; errorPos < iErrorMap.Count() && !result; errorPos++)
   178 		{
   179 		pError = iErrorMap[errorPos];
   180 		if(pError->iError == aError)
   181 			{
   182 			aResult = HBufC::NewLC(pError->iErrorName->Length());
   183 			aResult->Des().Copy(*pError->iErrorName);
   184 			result = ETrue;
   185 			};
   186 		}
   187 	// just so it always has something to pop even if it didnt find error
   188 	if(!result)
   189 		{
   190 		aResult = HBufC::NewLC(0);
   191 		};
   192 
   193 	return(result);
   194 	}
   195 
   196