os/security/cryptoservices/certificateandkeymgmt/tasn1/testoutput.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2001-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 * Implementation for testing encoded object output
    16 *
    17 */
    18 
    19 
    20 #include "testoutput.h"
    21 #include "tasn1normaltest.h"
    22 #include <asn1enc.h>
    23 #include <asn1dec.h>
    24 #include <e32cons.h>
    25 #include <f32file.h>
    26 
    27 
    28 #include <bigint.h>
    29 
    30 CTestOutput* CTestOutput::NewL(CASN1NormalTest &aASN1Action)
    31 	{
    32 	CTestOutput* test = new (ELeave) CTestOutput(aASN1Action);
    33 	return test;
    34 	}
    35 
    36 CTestOutput::CTestOutput(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
    37 	{
    38 	};
    39 
    40 
    41 void CTestOutput::GetName(TDes& aBuf)
    42 	{
    43 	aBuf.Copy(_L("Test Output (to file)"));
    44 	}
    45 
    46 
    47 TBool CTestOutput::PerformTestsL(CConsoleBase& aConsole)
    48 	{
    49 	// Get object to output
    50 	CASN1EncBase* enc = MakeEncoderLC();
    51 
    52 	// Prepare a buffer
    53 	TUint length = enc->LengthDER();
    54 	HBufC8* buf = HBufC8::NewMaxLC(length);
    55 	TPtr8 tBuf = buf->Des();
    56 	
    57 	// Write into the buffer
    58 	TUint writeLength = 0;
    59 	enc->WriteDERL(tBuf, writeLength);
    60 
    61 	// Copy to file
    62 	RFs fs;
    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"));
    69 
    70 	RFile file;
    71 	User::LeaveIfError(file.Replace(fs, fileName, EFileWrite));
    72 	CleanupClosePushL(file);
    73 	User::LeaveIfError(file.Write(tBuf));
    74 
    75 	// Tidy up
    76 	CleanupStack::PopAndDestroy(4); // Close file, close fs, buf, enc
    77 	iASN1Action.ReportProgressL(KErrNone, 1, 1);
    78 
    79 	aConsole.Write(_L("Now use DumpASN1 on "));
    80 	aConsole.Write(fileName);
    81 	aConsole.Write(_L("\n"));
    82 	return(ETrue);
    83 	}
    84 
    85 
    86 CASN1EncBase* CTestOutput::MakeEncoderLC(const TBool aNest) const
    87 	{
    88 	// Sequence we'll be using
    89 	CASN1EncSequence* seq = CASN1EncSequence::NewLC();
    90 
    91 	// Add objects
    92 	TUint index = aNest ? 0 : 1;
    93 	while (CASN1EncBase* enc = MakeEncObjLC(index++))
    94 		{
    95 		seq->AddChildL(enc);
    96 		CleanupStack::Pop(); // enc
    97 		}
    98 
    99 	return seq;
   100 	}
   101 
   102 
   103 CASN1EncBase* CTestOutput::MakeEncObjLC(const TUint aIndex) const
   104 	{
   105 	switch (aIndex)
   106 		{
   107 		case 0:
   108 			// Case 0 is always the nested sequence encoder
   109 			return MakeEncoderLC(EFalse);
   110 		case 1:
   111 			return CASN1EncNull::NewLC();
   112 		case 2:
   113 			return CASN1EncBoolean::NewLC(ETrue);
   114 		case 3:
   115 			return CASN1EncBoolean::NewLC(EFalse);
   116 		case 4:
   117 			return CASN1EncInt::NewLC(12345);
   118 		case 5:
   119 			return CASN1EncInt::NewLC(-4354);
   120 		case 6:
   121 			{
   122 			RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom);
   123 			CleanupStack::PushL(i);
   124 			CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i);
   125 			CleanupStack::Pop();
   126 			CleanupStack::PopAndDestroy(&i);
   127 			CleanupStack::PushL(enc);
   128 			return enc;
   129 			}
   130 		case 7: // -ve this time
   131 			{
   132 			RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom);
   133 			CleanupStack::PushL(i);
   134 			i *= -1;
   135 			CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i);
   136 			CleanupStack::Pop();
   137 			CleanupStack::PopAndDestroy(&i);
   138 			CleanupStack::PushL(enc);
   139 			return enc;
   140 			}
   141 		case 8:
   142 			{
   143 			// INFOSEC policy UID
   144 			_LIT(KTestOID, "2.16.840.1.101.2.1.3.10");
   145 			return CASN1EncObjectIdentifier::NewLC(KTestOID);
   146 			}
   147 		case 9:
   148 			{
   149 			HBufC8* str = HBufC8::NewLC(256);
   150 			TPtr8 des = str->Des();
   151 			for (TUint i = 0; i < 256; ++i)
   152 				{
   153 				des.Append(STATIC_CAST(TChar, i));
   154 				}
   155 			CASN1EncBase* enc = CASN1EncOctetString::NewLC(des);
   156 			CleanupStack::Pop();
   157 			CleanupStack::PopAndDestroy(); // str
   158 			CleanupStack::PushL(enc);
   159 			return enc;
   160 			}
   161 		case 10: // without seconds
   162 			{
   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
   166 			}
   167 		case 11: // with seconds
   168 			{
   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
   172 			}
   173 		case 12:
   174 			{
   175 			CASN1EncBase* null = CASN1EncNull::NewLC();
   176 			CleanupStack::Pop(); // null
   177 			return CASN1EncExplicitTag::NewLC(null, 100);
   178 			}
   179 		default:
   180 			return 0;
   181 		}
   182 	}