First public contribution.
2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Implementation for testing encoded object output
20 #include "testoutput.h"
21 #include "tasn1normaltest.h"
30 CTestOutput* CTestOutput::NewL(CASN1NormalTest &aASN1Action)
32 CTestOutput* test = new (ELeave) CTestOutput(aASN1Action);
36 CTestOutput::CTestOutput(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
41 void CTestOutput::GetName(TDes& aBuf)
43 aBuf.Copy(_L("Test Output (to file)"));
47 TBool CTestOutput::PerformTestsL(CConsoleBase& aConsole)
49 // Get object to output
50 CASN1EncBase* enc = MakeEncoderLC();
53 TUint length = enc->LengthDER();
54 HBufC8* buf = HBufC8::NewMaxLC(length);
55 TPtr8 tBuf = buf->Des();
57 // Write into the buffer
58 TUint writeLength = 0;
59 enc->WriteDERL(tBuf, writeLength);
63 User::LeaveIfError(fs.Connect());
64 CleanupClosePushL(fs);
65 TDriveUnit sysDrive (fs.GetSystemDrive());
66 TDriveName driveName(sysDrive.Name());
67 TBuf<64> fileName(driveName);
68 fileName.Append(_L("\\tasn1\\TASN1_test_output"));
71 User::LeaveIfError(file.Replace(fs, fileName, EFileWrite));
72 CleanupClosePushL(file);
73 User::LeaveIfError(file.Write(tBuf));
76 CleanupStack::PopAndDestroy(4); // Close file, close fs, buf, enc
77 iASN1Action.ReportProgressL(KErrNone, 1, 1);
79 aConsole.Write(_L("Now use DumpASN1 on "));
80 aConsole.Write(fileName);
81 aConsole.Write(_L("\n"));
86 CASN1EncBase* CTestOutput::MakeEncoderLC(const TBool aNest) const
88 // Sequence we'll be using
89 CASN1EncSequence* seq = CASN1EncSequence::NewLC();
92 TUint index = aNest ? 0 : 1;
93 while (CASN1EncBase* enc = MakeEncObjLC(index++))
96 CleanupStack::Pop(); // enc
103 CASN1EncBase* CTestOutput::MakeEncObjLC(const TUint aIndex) const
108 // Case 0 is always the nested sequence encoder
109 return MakeEncoderLC(EFalse);
111 return CASN1EncNull::NewLC();
113 return CASN1EncBoolean::NewLC(ETrue);
115 return CASN1EncBoolean::NewLC(EFalse);
117 return CASN1EncInt::NewLC(12345);
119 return CASN1EncInt::NewLC(-4354);
122 RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom);
123 CleanupStack::PushL(i);
124 CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i);
126 CleanupStack::PopAndDestroy(&i);
127 CleanupStack::PushL(enc);
130 case 7: // -ve this time
132 RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom);
133 CleanupStack::PushL(i);
135 CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i);
137 CleanupStack::PopAndDestroy(&i);
138 CleanupStack::PushL(enc);
143 // INFOSEC policy UID
144 _LIT(KTestOID, "2.16.840.1.101.2.1.3.10");
145 return CASN1EncObjectIdentifier::NewLC(KTestOID);
149 HBufC8* str = HBufC8::NewLC(256);
150 TPtr8 des = str->Des();
151 for (TUint i = 0; i < 256; ++i)
153 des.Append(STATIC_CAST(TChar, i));
155 CASN1EncBase* enc = CASN1EncOctetString::NewLC(des);
157 CleanupStack::PopAndDestroy(); // str
158 CleanupStack::PushL(enc);
161 case 10: // without seconds
163 // DateTime month and day are 0-based
164 TDateTime dateTime(1972, EDecember, 19, 11, 35, 0, 0);
165 return CASN1EncGeneralizedTime::NewLC(dateTime); // Implicit conversion to TTime
167 case 11: // with seconds
169 // DateTime month and day are 0-based
170 TDateTime dateTime(1972, EDecember, 19, 11, 35, 23, 0);
171 return CASN1EncGeneralizedTime::NewLC(dateTime); // Implicit conversion to TTime
175 CASN1EncBase* null = CASN1EncNull::NewLC();
176 CleanupStack::Pop(); // null
177 return CASN1EncExplicitTag::NewLC(null, 100);