1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tx509/comparisontest.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,216 @@
1.4 +/*
1.5 +* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "comparisontest.h"
1.23 +#include "t_input.h"
1.24 +#include "t_output.h"
1.25 +#include <s32file.h>
1.26 +#include <x509cert.h>
1.27 +
1.28 +_LIT8(KCertificate1Start,"<certificate1>");
1.29 +_LIT8(KCertificate2Start,"<certificate2>");
1.30 +_LIT8(KMatchExpectedStart, "<matchexpected>");
1.31 +
1.32 +CTestAction* CComparisonTest::NewL(RFs& aFs, CConsoleBase& aConsole,
1.33 + Output& aOut, const TTestActionSpec& aTestActionSpec)
1.34 +/**
1.35 +Factory method that creates a new CComparisonTest object.
1.36 +
1.37 +@param aFs Shared file server session requried by base class
1.38 +@param aConsole The console used by the base class for logging
1.39 +@param aOut Output utilities for use by base class
1.40 +@param aTestActionSpec Parameters for this test
1.41 +@return a new instance of a CComparisonTest
1.42 +*/
1.43 + {
1.44 + CTestAction* self = CComparisonTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.45 + CleanupStack::Pop(self);
1.46 + return self;
1.47 + }
1.48 +
1.49 +CTestAction* CComparisonTest::NewLC(RFs& aFs, CConsoleBase& aConsole,
1.50 + Output& aOut, const TTestActionSpec& aTestActionSpec)
1.51 +/**
1.52 +Factory method that creates a new CComparisonTest object and places the pointer
1.53 +to the this on the cleanup stack.
1.54 +@param aFs Shared file server session requried by base class
1.55 +@param aConsole The console used by the base class for logging
1.56 +@param aOut Output utilities for use by base class
1.57 +@param aTestActionSpec Parameters for this test
1.58 +@return a new instance of a CComparisonTest
1.59 +*/
1.60 + {
1.61 + CComparisonTest* self = new(ELeave) CComparisonTest(aFs, aConsole, aOut);
1.62 + CleanupStack::PushL(self);
1.63 + self->ConstructL(aTestActionSpec);
1.64 + return self;
1.65 + }
1.66 +
1.67 +CComparisonTest::CComparisonTest(RFs& aFs,
1.68 + CConsoleBase& aConsole,
1.69 + Output& aOut)
1.70 +: CTestAction(aConsole, aOut), iFs(aFs)
1.71 +/**
1.72 +Constructor
1.73 +@param aFs Shared file server session required by base-class
1.74 +@param aConsole Console implemenation required by base class
1.75 +@param aOut Output utilities required by base class
1.76 +*/
1.77 + {
1.78 + }
1.79 +
1.80 +void CComparisonTest::ConstructL(const TTestActionSpec& aTestActionSpec)
1.81 +/**
1.82 +Second phase constructor
1.83 +@param aTestActionSpec parameters for this test base
1.84 +*/
1.85 + {
1.86 + CTestAction::ConstructL(aTestActionSpec);
1.87 + HBufC8* body = HBufC8::NewLC(aTestActionSpec.iActionBody.Length());
1.88 + body->Des().Copy(aTestActionSpec.iActionBody);
1.89 +
1.90 + TPtrC8 cert1FileName = Input::ParseElement(*body, KCertificate1Start);
1.91 + if (! cert1FileName.Length() > 0)
1.92 + {
1.93 + SetScriptError(ESyntax, _L("Missing tag: certificate1"));
1.94 + iFinished = ETrue;
1.95 + return;
1.96 + }
1.97 +
1.98 + iCert1 = ReadCertificateL(cert1FileName);
1.99 +
1.100 + TPtrC8 cert2FileName = Input::ParseElement(*body, KCertificate2Start);
1.101 + if (! cert2FileName.Length() > 0)
1.102 + {
1.103 + SetScriptError(ESyntax, _L("Missing tag: certificate1"));
1.104 + iFinished = ETrue;
1.105 + return;
1.106 + }
1.107 + iCert2 = ReadCertificateL(cert2FileName);
1.108 + iMatchExpected = Input::ParseElementBoolL(*body, KMatchExpectedStart);
1.109 + CleanupStack::PopAndDestroy(body);
1.110 + }
1.111 +
1.112 +CX509Certificate* CComparisonTest::ReadCertificateL(const TDesC8& aFileName)
1.113 +/**
1.114 +Reads an X.509 certificate from a file.
1.115 +@param aFileName The name of the certificate file.
1.116 +@return A pointer to the new certificate object.
1.117 +*/
1.118 + {
1.119 + TFileName fn;
1.120 + fn.Copy(aFileName);
1.121 +
1.122 + iOut.write(_L("Loading: %S\n"), &fn);
1.123 + RFile file;
1.124 + User::LeaveIfError(file.Open(iFs, fn, EFileRead | EFileShareReadersOnly));
1.125 + CleanupClosePushL(file);
1.126 + TInt size;
1.127 + User::LeaveIfError(file.Size(size));
1.128 + RBuf8 buf;
1.129 + buf.CreateL(size);
1.130 + CleanupClosePushL(buf);
1.131 +
1.132 + User::LeaveIfError(file.Read(buf, size));
1.133 +
1.134 + CX509Certificate* cert = CX509Certificate::NewL(buf);
1.135 + CleanupStack::PopAndDestroy(2, &file);
1.136 + return cert;
1.137 + }
1.138 +
1.139 +CComparisonTest::~CComparisonTest()
1.140 +/**
1.141 +Destructor
1.142 +*/
1.143 + {
1.144 + delete iCert1;
1.145 + delete iCert2;
1.146 + }
1.147 +
1.148 +void CComparisonTest::DoPerformPrerequisite(TRequestStatus& aStatus)
1.149 + {
1.150 + iActionState = EAction;
1.151 + TRequestStatus* status = &aStatus;
1.152 + User::RequestComplete(status, KErrNone);
1.153 + }
1.154 +
1.155 +void CComparisonTest::DoPerformPostrequisite(TRequestStatus& aStatus)
1.156 + {
1.157 + TRequestStatus* status = &aStatus;
1.158 + iFinished = ETrue;
1.159 + User::RequestComplete(status, KErrNone);
1.160 + }
1.161 +
1.162 +void CComparisonTest::PerformAction(TRequestStatus& aStatus)
1.163 + {
1.164 + TRAPD(err, DoActionL());
1.165 + TRequestStatus* status = &aStatus;
1.166 + iActionState = EPostrequisite;
1.167 + User::RequestComplete(status, err);
1.168 + }
1.169 +
1.170 +void CComparisonTest::DoActionL()
1.171 +/**
1.172 +Compares iCert1 and iCert2 using CX509Certificate::IsEqualL
1.173 +*/
1.174 + {
1.175 + HBufC* subject1 = iCert1->SubjectL();
1.176 + CleanupStack::PushL(subject1);
1.177 + HBufC* subject2 = iCert2->SubjectL();
1.178 + CleanupStack::PushL(subject2);
1.179 +
1.180 + HBufC* issuer1 = iCert1->IssuerL();
1.181 + CleanupStack::PushL(issuer1);
1.182 + HBufC* issuer2 = iCert2->IssuerL();
1.183 + CleanupStack::PushL(issuer2);
1.184 +
1.185 + iConsole.Printf(_L("Comparing certificates\n"));
1.186 + iOut.write(_L("Comparing certificates\n"));
1.187 +
1.188 + iConsole.Printf(_L("cert1:\n\tsubject: %S\n\tissuer %S\n"), subject1, issuer1);
1.189 + iOut.write(_L("cert1:\n\tsubject: %S\n\tissuer %S\n"), subject1, issuer1);
1.190 +
1.191 + iConsole.Printf(_L("cert2:\n\tsubject: %S\n\tissuer %S\n"), subject2, issuer2);
1.192 + iOut.write(_L("cert2:\n\tsubject: %S issuer\n\t%S\n"), subject2, issuer2);
1.193 +
1.194 + TBool match = iCert1->IsEqualL(*iCert2);
1.195 + iConsole.Printf(_L("Match expected %d, result %d\n"), iMatchExpected, match);
1.196 + iOut.write(_L("Match expected %d, result %d\n"), iMatchExpected, match);
1.197 +
1.198 + iResult = (match == iMatchExpected);
1.199 +
1.200 + if (iResult)
1.201 + {
1.202 + iConsole.Printf(_L(" Success\n"));
1.203 + iOut.writeString(_L(" Success\n"));
1.204 + }
1.205 + else
1.206 + {
1.207 + iConsole.Printf(_L(" Failed\n"));
1.208 + iOut.writeString(_L(" Failed"));
1.209 + };
1.210 + CleanupStack::PopAndDestroy(4, subject1);
1.211 + }
1.212 +
1.213 +void CComparisonTest::DoReportAction()
1.214 + {
1.215 + }
1.216 +
1.217 +void CComparisonTest::DoCheckResult(TInt /*aError*/)
1.218 + {
1.219 + }