1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tasn1/testboolean.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,124 @@
1.4 +/*
1.5 +* Copyright (c) 2001-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 +* Implementation for testing boolean object encoding/decoding
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include "testboolean.h"
1.24 +#include "tasn1normaltest.h"
1.25 +#include <asn1enc.h>
1.26 +#include <asn1dec.h>
1.27 +
1.28 +#include <e32cons.h>
1.29 +
1.30 +CTestBoolean* CTestBoolean::NewL(CASN1NormalTest &aASN1Action)
1.31 + {
1.32 + CTestBoolean* test = new (ELeave) CTestBoolean(aASN1Action);
1.33 + return test;
1.34 + }
1.35 +
1.36 +CTestBoolean::CTestBoolean(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
1.37 + {
1.38 + };
1.39 +
1.40 +void CTestBoolean::GetName(TDes& aBuf)
1.41 + {
1.42 + aBuf.Copy(_L("Test Boolean"));
1.43 + }
1.44 +
1.45 +void CTestBoolean::FillParameterArray(void)
1.46 + {
1.47 + iParameters->Append(CTestParameter::EInt);
1.48 + }
1.49 +
1.50 +
1.51 +TBool CTestBoolean::PerformTest(CConsoleBase& aConsole, const TBool& aTest)
1.52 + {
1.53 + // Make the encoder
1.54 + CASN1EncBoolean* encoder = CASN1EncBoolean::NewLC(aTest);
1.55 +
1.56 + // Prepare a buffer
1.57 + TInt length = encoder->LengthDER();
1.58 + HBufC8* buf = HBufC8::NewMaxLC(length);
1.59 + TPtr8 tBuf = buf->Des();
1.60 +
1.61 + // Write into the buffer
1.62 + TUint writeLength = 0;
1.63 + encoder->WriteDERL(tBuf, writeLength);
1.64 +
1.65 + // Read it out again + check lengths
1.66 + TASN1DecBoolean decoder;
1.67 + TInt readLength = 0;
1.68 + TBool decodedValue = decoder.DecodeDERL(tBuf, readLength);
1.69 +
1.70 + if (writeLength != STATIC_CAST(TUint, readLength) || decodedValue != aTest)
1.71 + {
1.72 + aConsole.Write(_L("ERROR!\n"));
1.73 + iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
1.74 + CleanupStack::PopAndDestroy(2); // buf, encoder
1.75 + return(EFalse);
1.76 + }
1.77 + else
1.78 + {
1.79 + iASN1Action.ReportProgressL(KErrNone, 1, 1);
1.80 + CleanupStack::PopAndDestroy(2); // buf, encoder
1.81 + return(ETrue);
1.82 + }
1.83 + }
1.84 +
1.85 +
1.86 +TBool CTestBoolean::PerformTestsL(CConsoleBase& aConsole)
1.87 + {
1.88 + CTestParameter* test;
1.89 + TInt totalTests;
1.90 +
1.91 + if(!CountTests(totalTests)) return(EFalse);
1.92 +
1.93 + for(TInt pos = 0; pos < iValues->Count(); pos++)
1.94 + {
1.95 + test = (*iValues)[pos];
1.96 +
1.97 + switch(test->GetType())
1.98 + {
1.99 + case CTestParameter::EInt :
1.100 + {
1.101 + CIntTestParameter *rangeInt = REINTERPRET_CAST(CIntTestParameter*, test);
1.102 +
1.103 + if(!PerformTest(aConsole, rangeInt->Value()))
1.104 + return(EFalse);
1.105 + break;
1.106 + }
1.107 + case CTestParameter::EIntRange :
1.108 + {
1.109 + CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
1.110 +
1.111 + for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++)
1.112 + {
1.113 + if(!PerformTest(aConsole, test))
1.114 + return(EFalse);
1.115 + }
1.116 + break;
1.117 + }
1.118 + default:
1.119 + {
1.120 + return EFalse;
1.121 + }
1.122 + }
1.123 + }
1.124 + iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
1.125 + return(ETrue);
1.126 + }
1.127 +