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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Implementation for testing encoded object output
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include "testoutput.h"
sl@0
    21
#include "tasn1normaltest.h"
sl@0
    22
#include <asn1enc.h>
sl@0
    23
#include <asn1dec.h>
sl@0
    24
#include <e32cons.h>
sl@0
    25
#include <f32file.h>
sl@0
    26
sl@0
    27
sl@0
    28
#include <bigint.h>
sl@0
    29
sl@0
    30
CTestOutput* CTestOutput::NewL(CASN1NormalTest &aASN1Action)
sl@0
    31
	{
sl@0
    32
	CTestOutput* test = new (ELeave) CTestOutput(aASN1Action);
sl@0
    33
	return test;
sl@0
    34
	}
sl@0
    35
sl@0
    36
CTestOutput::CTestOutput(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
sl@0
    37
	{
sl@0
    38
	};
sl@0
    39
sl@0
    40
sl@0
    41
void CTestOutput::GetName(TDes& aBuf)
sl@0
    42
	{
sl@0
    43
	aBuf.Copy(_L("Test Output (to file)"));
sl@0
    44
	}
sl@0
    45
sl@0
    46
sl@0
    47
TBool CTestOutput::PerformTestsL(CConsoleBase& aConsole)
sl@0
    48
	{
sl@0
    49
	// Get object to output
sl@0
    50
	CASN1EncBase* enc = MakeEncoderLC();
sl@0
    51
sl@0
    52
	// Prepare a buffer
sl@0
    53
	TUint length = enc->LengthDER();
sl@0
    54
	HBufC8* buf = HBufC8::NewMaxLC(length);
sl@0
    55
	TPtr8 tBuf = buf->Des();
sl@0
    56
	
sl@0
    57
	// Write into the buffer
sl@0
    58
	TUint writeLength = 0;
sl@0
    59
	enc->WriteDERL(tBuf, writeLength);
sl@0
    60
sl@0
    61
	// Copy to file
sl@0
    62
	RFs fs;
sl@0
    63
	User::LeaveIfError(fs.Connect());
sl@0
    64
	CleanupClosePushL(fs);
sl@0
    65
	TDriveUnit sysDrive (fs.GetSystemDrive());
sl@0
    66
	TDriveName driveName(sysDrive.Name());
sl@0
    67
	TBuf<64> fileName(driveName);
sl@0
    68
	fileName.Append(_L("\\tasn1\\TASN1_test_output"));
sl@0
    69
sl@0
    70
	RFile file;
sl@0
    71
	User::LeaveIfError(file.Replace(fs, fileName, EFileWrite));
sl@0
    72
	CleanupClosePushL(file);
sl@0
    73
	User::LeaveIfError(file.Write(tBuf));
sl@0
    74
sl@0
    75
	// Tidy up
sl@0
    76
	CleanupStack::PopAndDestroy(4); // Close file, close fs, buf, enc
sl@0
    77
	iASN1Action.ReportProgressL(KErrNone, 1, 1);
sl@0
    78
sl@0
    79
	aConsole.Write(_L("Now use DumpASN1 on "));
sl@0
    80
	aConsole.Write(fileName);
sl@0
    81
	aConsole.Write(_L("\n"));
sl@0
    82
	return(ETrue);
sl@0
    83
	}
sl@0
    84
sl@0
    85
sl@0
    86
CASN1EncBase* CTestOutput::MakeEncoderLC(const TBool aNest) const
sl@0
    87
	{
sl@0
    88
	// Sequence we'll be using
sl@0
    89
	CASN1EncSequence* seq = CASN1EncSequence::NewLC();
sl@0
    90
sl@0
    91
	// Add objects
sl@0
    92
	TUint index = aNest ? 0 : 1;
sl@0
    93
	while (CASN1EncBase* enc = MakeEncObjLC(index++))
sl@0
    94
		{
sl@0
    95
		seq->AddChildL(enc);
sl@0
    96
		CleanupStack::Pop(); // enc
sl@0
    97
		}
sl@0
    98
sl@0
    99
	return seq;
sl@0
   100
	}
sl@0
   101
sl@0
   102
sl@0
   103
CASN1EncBase* CTestOutput::MakeEncObjLC(const TUint aIndex) const
sl@0
   104
	{
sl@0
   105
	switch (aIndex)
sl@0
   106
		{
sl@0
   107
		case 0:
sl@0
   108
			// Case 0 is always the nested sequence encoder
sl@0
   109
			return MakeEncoderLC(EFalse);
sl@0
   110
		case 1:
sl@0
   111
			return CASN1EncNull::NewLC();
sl@0
   112
		case 2:
sl@0
   113
			return CASN1EncBoolean::NewLC(ETrue);
sl@0
   114
		case 3:
sl@0
   115
			return CASN1EncBoolean::NewLC(EFalse);
sl@0
   116
		case 4:
sl@0
   117
			return CASN1EncInt::NewLC(12345);
sl@0
   118
		case 5:
sl@0
   119
			return CASN1EncInt::NewLC(-4354);
sl@0
   120
		case 6:
sl@0
   121
			{
sl@0
   122
			RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom);
sl@0
   123
			CleanupStack::PushL(i);
sl@0
   124
			CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i);
sl@0
   125
			CleanupStack::Pop();
sl@0
   126
			CleanupStack::PopAndDestroy(&i);
sl@0
   127
			CleanupStack::PushL(enc);
sl@0
   128
			return enc;
sl@0
   129
			}
sl@0
   130
		case 7: // -ve this time
sl@0
   131
			{
sl@0
   132
			RInteger i = RInteger::NewRandomL(345, TInteger::EAllBitsRandom);
sl@0
   133
			CleanupStack::PushL(i);
sl@0
   134
			i *= -1;
sl@0
   135
			CASN1EncBigInt* enc = CASN1EncBigInt::NewLC(i);
sl@0
   136
			CleanupStack::Pop();
sl@0
   137
			CleanupStack::PopAndDestroy(&i);
sl@0
   138
			CleanupStack::PushL(enc);
sl@0
   139
			return enc;
sl@0
   140
			}
sl@0
   141
		case 8:
sl@0
   142
			{
sl@0
   143
			// INFOSEC policy UID
sl@0
   144
			_LIT(KTestOID, "2.16.840.1.101.2.1.3.10");
sl@0
   145
			return CASN1EncObjectIdentifier::NewLC(KTestOID);
sl@0
   146
			}
sl@0
   147
		case 9:
sl@0
   148
			{
sl@0
   149
			HBufC8* str = HBufC8::NewLC(256);
sl@0
   150
			TPtr8 des = str->Des();
sl@0
   151
			for (TUint i = 0; i < 256; ++i)
sl@0
   152
				{
sl@0
   153
				des.Append(STATIC_CAST(TChar, i));
sl@0
   154
				}
sl@0
   155
			CASN1EncBase* enc = CASN1EncOctetString::NewLC(des);
sl@0
   156
			CleanupStack::Pop();
sl@0
   157
			CleanupStack::PopAndDestroy(); // str
sl@0
   158
			CleanupStack::PushL(enc);
sl@0
   159
			return enc;
sl@0
   160
			}
sl@0
   161
		case 10: // without seconds
sl@0
   162
			{
sl@0
   163
			// DateTime month and day are 0-based
sl@0
   164
			TDateTime dateTime(1972, EDecember, 19, 11, 35, 0, 0);
sl@0
   165
			return CASN1EncGeneralizedTime::NewLC(dateTime); // Implicit conversion to TTime
sl@0
   166
			}
sl@0
   167
		case 11: // with seconds
sl@0
   168
			{
sl@0
   169
			// DateTime month and day are 0-based
sl@0
   170
			TDateTime dateTime(1972, EDecember, 19, 11, 35, 23, 0);
sl@0
   171
			return CASN1EncGeneralizedTime::NewLC(dateTime); // Implicit conversion to TTime
sl@0
   172
			}
sl@0
   173
		case 12:
sl@0
   174
			{
sl@0
   175
			CASN1EncBase* null = CASN1EncNull::NewLC();
sl@0
   176
			CleanupStack::Pop(); // null
sl@0
   177
			return CASN1EncExplicitTag::NewLC(null, 100);
sl@0
   178
			}
sl@0
   179
		default:
sl@0
   180
			return 0;
sl@0
   181
		}
sl@0
   182
	}