os/security/cryptoservices/certificateandkeymgmt/tx509/comparisontest.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) 2008-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 *
    16 */
    17 
    18 
    19 #include "comparisontest.h"
    20 #include "t_input.h"
    21 #include "t_output.h"
    22 #include <s32file.h>
    23 #include <x509cert.h>
    24 
    25 _LIT8(KCertificate1Start,"<certificate1>");
    26 _LIT8(KCertificate2Start,"<certificate2>");
    27 _LIT8(KMatchExpectedStart, "<matchexpected>");
    28 
    29 CTestAction* CComparisonTest::NewL(RFs& aFs, CConsoleBase& aConsole, 
    30 		Output& aOut, const TTestActionSpec& aTestActionSpec)
    31 /**
    32 Factory method that creates a new CComparisonTest object.
    33 
    34 @param 	aFs		 		Shared file server session requried by base class
    35 @param 	aConsole 		The console used by the base class for logging
    36 @param	aOut	 		Output utilities for use by base class
    37 @param	aTestActionSpec	Parameters for this test	
    38 @return a new instance of a CComparisonTest
    39 */
    40 	{
    41 	CTestAction* self = CComparisonTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    42 	CleanupStack::Pop(self);
    43 	return self;
    44 	}
    45 
    46 CTestAction* CComparisonTest::NewLC(RFs& aFs, CConsoleBase& aConsole, 
    47 		Output& aOut, const TTestActionSpec& aTestActionSpec)
    48 /**
    49 Factory method that creates a new CComparisonTest object and places the pointer
    50 to the this on the cleanup stack.
    51 @param 	aFs				Shared file server session requried by base class
    52 @param 	aConsole		The console used by the base class for logging
    53 @param	aOut	 		Output utilities for use by base class	
    54 @param	aTestActionSpec	Parameters for this test
    55 @return a new instance of a CComparisonTest
    56 */
    57 	{
    58 	CComparisonTest* self = new(ELeave) CComparisonTest(aFs, aConsole, aOut);
    59 	CleanupStack::PushL(self);
    60 	self->ConstructL(aTestActionSpec);
    61 	return self;
    62 	}
    63 
    64 CComparisonTest::CComparisonTest(RFs& aFs, 
    65 								 CConsoleBase& aConsole,
    66 								 Output& aOut)
    67 : CTestAction(aConsole, aOut), iFs(aFs)
    68 /**
    69 Constructor
    70 @param	aFs			Shared file server session required by base-class
    71 @param	aConsole 	Console implemenation required by base class
    72 @param	aOut		Output utilities required by base class
    73 */
    74 	{	
    75 	}
    76 
    77 void CComparisonTest::ConstructL(const TTestActionSpec& aTestActionSpec)
    78 /**
    79 Second phase constructor
    80 @param	aTestActionSpec		parameters for this test base
    81 */
    82 	{
    83 	CTestAction::ConstructL(aTestActionSpec);
    84 	HBufC8* body = HBufC8::NewLC(aTestActionSpec.iActionBody.Length());
    85 	body->Des().Copy(aTestActionSpec.iActionBody);
    86 	
    87 	TPtrC8 cert1FileName = Input::ParseElement(*body, KCertificate1Start);
    88 	if (! cert1FileName.Length() > 0)
    89 		{
    90 		SetScriptError(ESyntax, _L("Missing tag: certificate1"));
    91 		iFinished = ETrue;
    92 		return;
    93 		}
    94 	
    95 	iCert1 = ReadCertificateL(cert1FileName);
    96 	
    97 	TPtrC8 cert2FileName = Input::ParseElement(*body, KCertificate2Start);
    98 	if (! cert2FileName.Length() > 0)
    99 		{
   100 		SetScriptError(ESyntax, _L("Missing tag: certificate1"));
   101 		iFinished = ETrue;
   102 		return;
   103 		}
   104 	iCert2 = ReadCertificateL(cert2FileName);	
   105 	iMatchExpected = Input::ParseElementBoolL(*body, KMatchExpectedStart);
   106 	CleanupStack::PopAndDestroy(body);
   107 	}
   108 
   109 CX509Certificate* CComparisonTest::ReadCertificateL(const TDesC8& aFileName)
   110 /**
   111 Reads an X.509 certificate from a file.
   112 @param	aFileName	The name of the certificate file.
   113 @return	A pointer to the new certificate object.
   114 */
   115 	{
   116 	TFileName fn;
   117 	fn.Copy(aFileName);
   118 	
   119 	iOut.write(_L("Loading: %S\n"), &fn);
   120 	RFile file;
   121 	User::LeaveIfError(file.Open(iFs, fn, EFileRead | EFileShareReadersOnly));		
   122 	CleanupClosePushL(file);
   123 	TInt size;
   124 	User::LeaveIfError(file.Size(size));
   125 	RBuf8 buf;
   126 	buf.CreateL(size);
   127 	CleanupClosePushL(buf);
   128 	
   129 	User::LeaveIfError(file.Read(buf, size));
   130 	
   131 	CX509Certificate* cert = CX509Certificate::NewL(buf);
   132 	CleanupStack::PopAndDestroy(2, &file);
   133 	return cert;
   134 	}
   135 
   136 CComparisonTest::~CComparisonTest()
   137 /**
   138 Destructor
   139 */
   140 	{
   141 	delete iCert1;
   142 	delete iCert2;
   143 	}
   144 
   145 void CComparisonTest::DoPerformPrerequisite(TRequestStatus& aStatus)
   146 	{
   147 	iActionState = EAction;
   148 	TRequestStatus* status = &aStatus;
   149 	User::RequestComplete(status, KErrNone);
   150 	}
   151 
   152 void CComparisonTest::DoPerformPostrequisite(TRequestStatus& aStatus)
   153 	{
   154 	TRequestStatus* status = &aStatus;
   155 	iFinished = ETrue;
   156 	User::RequestComplete(status, KErrNone);
   157 	}
   158 
   159 void CComparisonTest::PerformAction(TRequestStatus& aStatus)
   160 	{
   161 	TRAPD(err, DoActionL());
   162 	TRequestStatus* status = &aStatus;
   163 	iActionState = EPostrequisite;
   164 	User::RequestComplete(status, err);
   165 	}
   166 
   167 void CComparisonTest::DoActionL()
   168 /**
   169 Compares iCert1 and iCert2 using CX509Certificate::IsEqualL
   170 */
   171 	{
   172 	HBufC* subject1 = iCert1->SubjectL();
   173 	CleanupStack::PushL(subject1);
   174 	HBufC* subject2 = iCert2->SubjectL();
   175 	CleanupStack::PushL(subject2);
   176 	
   177 	HBufC* issuer1 = iCert1->IssuerL();
   178 	CleanupStack::PushL(issuer1);
   179 	HBufC* issuer2 = iCert2->IssuerL();
   180 	CleanupStack::PushL(issuer2);
   181 	
   182 	iConsole.Printf(_L("Comparing certificates\n"));
   183 	iOut.write(_L("Comparing certificates\n"));
   184 
   185 	iConsole.Printf(_L("cert1:\n\tsubject: %S\n\tissuer %S\n"), subject1, issuer1);
   186 	iOut.write(_L("cert1:\n\tsubject: %S\n\tissuer %S\n"), subject1, issuer1);
   187 
   188 	iConsole.Printf(_L("cert2:\n\tsubject: %S\n\tissuer %S\n"), subject2, issuer2);
   189 	iOut.write(_L("cert2:\n\tsubject: %S issuer\n\t%S\n"), subject2, issuer2);
   190 	
   191 	TBool match = iCert1->IsEqualL(*iCert2);	
   192 	iConsole.Printf(_L("Match expected %d, result %d\n"), iMatchExpected, match);
   193 	iOut.write(_L("Match expected %d, result %d\n"), iMatchExpected, match);
   194 	
   195 	iResult = (match == iMatchExpected);
   196 
   197 	if (iResult)
   198 		{
   199 		iConsole.Printf(_L(" Success\n"));
   200 		iOut.writeString(_L(" Success\n"));
   201 		}
   202 	else
   203 		{
   204 		iConsole.Printf(_L(" Failed\n"));
   205 		iOut.writeString(_L(" Failed"));
   206 		};
   207 	CleanupStack::PopAndDestroy(4, subject1);
   208 	}
   209 
   210 void CComparisonTest::DoReportAction()
   211 	{
   212 	}
   213 
   214 void CComparisonTest::DoCheckResult(TInt /*aError*/)
   215 	{
   216 	}