sl@0: /* sl@0: * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Implementation for testing encoded object output sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "testoutput.h" sl@0: #include "tasn1normaltest.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: #include sl@0: sl@0: CTestOutput* CTestOutput::NewL(CASN1NormalTest &aASN1Action) sl@0: { sl@0: CTestOutput* test = new (ELeave) CTestOutput(aASN1Action); sl@0: return test; sl@0: } sl@0: sl@0: CTestOutput::CTestOutput(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action) sl@0: { sl@0: }; sl@0: sl@0: sl@0: void CTestOutput::GetName(TDes& aBuf) sl@0: { sl@0: aBuf.Copy(_L("Test Output (to file)")); sl@0: } sl@0: sl@0: sl@0: TBool CTestOutput::PerformTestsL(CConsoleBase& aConsole) sl@0: { sl@0: // Get object to output sl@0: CASN1EncBase* enc = MakeEncoderLC(); sl@0: sl@0: // Prepare a buffer sl@0: TUint length = enc->LengthDER(); sl@0: HBufC8* buf = HBufC8::NewMaxLC(length); sl@0: TPtr8 tBuf = buf->Des(); sl@0: sl@0: // Write into the buffer sl@0: TUint writeLength = 0; sl@0: enc->WriteDERL(tBuf, writeLength); sl@0: sl@0: // Copy to file sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: TDriveUnit sysDrive (fs.GetSystemDrive()); sl@0: TDriveName driveName(sysDrive.Name()); sl@0: TBuf<64> fileName(driveName); sl@0: fileName.Append(_L("\\tasn1\\TASN1_test_output")); sl@0: sl@0: RFile file; sl@0: User::LeaveIfError(file.Replace(fs, fileName, EFileWrite)); sl@0: CleanupClosePushL(file); sl@0: User::LeaveIfError(file.Write(tBuf)); sl@0: sl@0: // Tidy up sl@0: CleanupStack::PopAndDestroy(4); // Close file, close fs, buf, enc sl@0: iASN1Action.ReportProgressL(KErrNone, 1, 1); sl@0: sl@0: aConsole.Write(_L("Now use DumpASN1 on ")); sl@0: aConsole.Write(fileName); sl@0: aConsole.Write(_L("\n")); sl@0: return(ETrue); sl@0: } sl@0: sl@0: sl@0: CASN1EncBase* CTestOutput::MakeEncoderLC(const TBool aNest) const sl@0: { sl@0: // Sequence we'll be using sl@0: CASN1EncSequence* seq = CASN1EncSequence::NewLC(); sl@0: sl@0: // Add objects sl@0: TUint index = aNest ? 0 : 1; sl@0: while (CASN1EncBase* enc = MakeEncObjLC(index++)) sl@0: { sl@0: seq->AddChildL(enc); sl@0: CleanupStack::Pop(); // enc sl@0: } sl@0: sl@0: return seq; sl@0: } sl@0: sl@0: sl@0: CASN1EncBase* CTestOutput::MakeEncObjLC(const TUint aIndex) const sl@0: { sl@0: switch (aIndex) sl@0: { sl@0: case 0: sl@0: // Case 0 is always the nested sequence encoder sl@0: return MakeEncoderLC(EFalse); sl@0: case 1: sl@0: return CASN1EncNull::NewLC(); sl@0: case 2: sl@0: return CASN1EncBoolean::NewLC(ETrue); sl@0: case 3: sl@0: return CASN1EncBoolean::NewLC(EFalse); sl@0: case 4: sl@0: return CASN1EncInt::NewLC(12345); sl@0: case 5: sl@0: return CASN1EncInt::NewLC(-4354); sl@0: case 6: sl@0: { sl@0: RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom); sl@0: CleanupStack::PushL(i); sl@0: CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i); sl@0: CleanupStack::Pop(); sl@0: CleanupStack::PopAndDestroy(&i); sl@0: CleanupStack::PushL(enc); sl@0: return enc; sl@0: } sl@0: case 7: // -ve this time sl@0: { sl@0: RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom); sl@0: CleanupStack::PushL(i); sl@0: i *= -1; sl@0: CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i); sl@0: CleanupStack::Pop(); sl@0: CleanupStack::PopAndDestroy(&i); sl@0: CleanupStack::PushL(enc); sl@0: return enc; sl@0: } sl@0: case 8: sl@0: { sl@0: // INFOSEC policy UID sl@0: _LIT(KTestOID, "2.16.840.1.101.2.1.3.10"); sl@0: return CASN1EncObjectIdentifier::NewLC(KTestOID); sl@0: } sl@0: case 9: sl@0: { sl@0: HBufC8* str = HBufC8::NewLC(256); sl@0: TPtr8 des = str->Des(); sl@0: for (TUint i = 0; i < 256; ++i) sl@0: { sl@0: des.Append(STATIC_CAST(TChar, i)); sl@0: } sl@0: CASN1EncBase* enc = CASN1EncOctetString::NewLC(des); sl@0: CleanupStack::Pop(); sl@0: CleanupStack::PopAndDestroy(); // str sl@0: CleanupStack::PushL(enc); sl@0: return enc; sl@0: } sl@0: case 10: // without seconds sl@0: { sl@0: // DateTime month and day are 0-based sl@0: TDateTime dateTime(1972, EDecember, 19, 11, 35, 0, 0); sl@0: return CASN1EncGeneralizedTime::NewLC(dateTime); // Implicit conversion to TTime sl@0: } sl@0: case 11: // with seconds sl@0: { sl@0: // DateTime month and day are 0-based sl@0: TDateTime dateTime(1972, EDecember, 19, 11, 35, 23, 0); sl@0: return CASN1EncGeneralizedTime::NewLC(dateTime); // Implicit conversion to TTime sl@0: } sl@0: case 12: sl@0: { sl@0: CASN1EncBase* null = CASN1EncNull::NewLC(); sl@0: CleanupStack::Pop(); // null sl@0: return CASN1EncExplicitTag::NewLC(null, 100); sl@0: } sl@0: default: sl@0: return 0; sl@0: } sl@0: }