1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tasn1/testoctetstr.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,179 @@
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 octet string encoding/decoding
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include "testoctetstr.h"
1.24 +#include "tasn1normaltest.h"
1.25 +#include <asn1enc.h>
1.26 +#include <asn1dec.h>
1.27 +
1.28 +#include <e32math.h>
1.29 +#include <e32cons.h>
1.30 +
1.31 +const TInt KMaxStringLength = 1000;
1.32 +
1.33 +CTestOctetString* CTestOctetString::NewL(CASN1NormalTest &aASN1Action)
1.34 + {
1.35 + CTestOctetString* test = new (ELeave) CTestOctetString(aASN1Action);
1.36 + test->ConstructL();
1.37 + return test;
1.38 + }
1.39 +
1.40 +CTestOctetString::CTestOctetString(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
1.41 + {
1.42 + };
1.43 +
1.44 +void CTestOctetString::ConstructL()
1.45 + {
1.46 + toEncodeBuf = HBufC8::NewMaxLC(KMaxStringLength);
1.47 + encodedBuf = HBufC8::NewMaxLC(KMaxStringLength + 10); // extra 10 for tag/length
1.48 + CleanupStack::Pop(2);
1.49 + };
1.50 +
1.51 +void CTestOctetString::FillParameterArray(void)
1.52 + {
1.53 + iParameters->Append(CTestParameter::EString);
1.54 + }
1.55 +
1.56 +
1.57 +CTestOctetString::~CTestOctetString()
1.58 + {
1.59 + delete toEncodeBuf;
1.60 + delete encodedBuf;
1.61 + }
1.62 +
1.63 +void CTestOctetString::GetName(TDes& aBuf)
1.64 + {
1.65 + aBuf.Copy(_L("Test Octet String"));
1.66 + }
1.67 +
1.68 +
1.69 +TBool CTestOctetString::PerformTest(CConsoleBase& aConsole, const TPtr8 &toEncodePtr, const TInt &aTestNumber, const TInt &aTotalTests)
1.70 + {
1.71 + TPtr8 encodedPtr = encodedBuf->Des();
1.72 +
1.73 + // Make the encoder
1.74 + CASN1EncOctetString* encoder = CASN1EncOctetString::NewLC(toEncodePtr);
1.75 +
1.76 + // Do the encoding
1.77 + TUint writeLength = 0;
1.78 + encoder->WriteDERL(encodedPtr, writeLength);
1.79 +
1.80 + // Read it out again
1.81 + TASN1DecOctetString decoder;
1.82 + TInt readLength = 0;
1.83 + HBufC8* readBuf = decoder.DecodeDERL(encodedPtr, readLength);
1.84 + CleanupStack::PushL(readBuf);
1.85 +
1.86 + // Check lengths of reads + values
1.87 + if ((writeLength != STATIC_CAST(TUint, readLength)) || (*readBuf != toEncodePtr))
1.88 + {
1.89 + aConsole.Printf(_L("\nERROR!\n"));
1.90 + iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
1.91 + CleanupStack::PopAndDestroy(2); // encoder, readBuf
1.92 + return(ETrue);
1.93 + }
1.94 + else
1.95 + {
1.96 + iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
1.97 + CleanupStack::PopAndDestroy(2); // encoder, readBuf
1.98 + return(ETrue);
1.99 + }
1.100 + }
1.101 +
1.102 +
1.103 +TBool CTestOctetString::PerformTestsL(CConsoleBase& aConsole)
1.104 + {
1.105 + TInt nLow = Math::Random();
1.106 + TInt64 nHigh((TInt)Math::Random());
1.107 + TInt64 seed((nHigh << 32) + nLow);
1.108 + CTestParameter* test;
1.109 + TInt totalTests, currentTest=0;
1.110 +
1.111 + if(!CountTests(totalTests)) return(EFalse);
1.112 +
1.113 + for(TInt pos = 0; pos < iValues->Count(); pos++)
1.114 + {
1.115 + test = (*iValues)[pos];
1.116 + switch(test->GetType())
1.117 + {
1.118 + case CTestParameter::EString :
1.119 + {
1.120 + CStringTestParameter *testString = REINTERPRET_CAST(CStringTestParameter*, test);
1.121 + HBufC* value = HBufC::NewLC(KMaxStringLength);
1.122 + TPtr tBuf = value->Des();
1.123 +
1.124 + testString->GetValue(tBuf);
1.125 +
1.126 + if((tBuf.Length() % 2) != 0)
1.127 + User::Leave(KErrArgument);
1.128 +
1.129 + TPtr8 toEncodePtr(toEncodeBuf->Des());
1.130 + TUint theOctet;
1.131 + TInt octetPos=0;
1.132 +
1.133 + for(TInt octet = 0; octet < tBuf.Length(); octet+=2)
1.134 + {
1.135 + TLex lex(tBuf.Mid(octet, 2));
1.136 +
1.137 + if(lex.Val(theOctet, EHex)!=KErrNone)
1.138 + User::Leave(KErrArgument);
1.139 + toEncodePtr[octetPos++] = STATIC_CAST(TUint8, theOctet);
1.140 + };
1.141 + if(PerformTest(aConsole, toEncodePtr, currentTest, totalTests))
1.142 + {
1.143 + CleanupStack::PopAndDestroy();
1.144 + currentTest++;
1.145 + }
1.146 + else
1.147 + {
1.148 + CleanupStack::PopAndDestroy();
1.149 + return(EFalse);
1.150 + }
1.151 + break;
1.152 + }
1.153 + case CTestParameter::ERandom :
1.154 + {
1.155 + CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
1.156 +
1.157 + for (TInt count = 0; count <= randomInt->Interations(); ++count)
1.158 + {
1.159 + // Descriptor for part of buffer we'll use; fill with noise
1.160 + TInt toEncodeLength = count % KMaxStringLength;
1.161 + TPtr8 toEncodePtr(toEncodeBuf->Des());
1.162 + for (TInt i = 0; i < toEncodeLength; ++i)
1.163 + {
1.164 + toEncodePtr[i] = STATIC_CAST(TUint8, Math::Rand(seed));
1.165 + }
1.166 + if(!PerformTest(aConsole, toEncodePtr, currentTest, totalTests))
1.167 + return(EFalse);
1.168 + currentTest++;
1.169 + }
1.170 +
1.171 + break;
1.172 + }
1.173 + default:
1.174 + {
1.175 + return EFalse;
1.176 + }
1.177 + }
1.178 + }
1.179 + iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
1.180 + return(ETrue);
1.181 + };
1.182 +