sl@0: /* sl@0: * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "comparisontest.h" sl@0: #include "t_input.h" sl@0: #include "t_output.h" sl@0: #include sl@0: #include sl@0: sl@0: _LIT8(KCertificate1Start,""); sl@0: _LIT8(KCertificate2Start,""); sl@0: _LIT8(KMatchExpectedStart, ""); sl@0: sl@0: CTestAction* CComparisonTest::NewL(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut, const TTestActionSpec& aTestActionSpec) sl@0: /** sl@0: Factory method that creates a new CComparisonTest object. sl@0: sl@0: @param aFs Shared file server session requried by base class sl@0: @param aConsole The console used by the base class for logging sl@0: @param aOut Output utilities for use by base class sl@0: @param aTestActionSpec Parameters for this test sl@0: @return a new instance of a CComparisonTest sl@0: */ sl@0: { sl@0: CTestAction* self = CComparisonTest::NewLC(aFs, aConsole, aOut, aTestActionSpec); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CComparisonTest::NewLC(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut, const TTestActionSpec& aTestActionSpec) sl@0: /** sl@0: Factory method that creates a new CComparisonTest object and places the pointer sl@0: to the this on the cleanup stack. sl@0: @param aFs Shared file server session requried by base class sl@0: @param aConsole The console used by the base class for logging sl@0: @param aOut Output utilities for use by base class sl@0: @param aTestActionSpec Parameters for this test sl@0: @return a new instance of a CComparisonTest sl@0: */ sl@0: { sl@0: CComparisonTest* self = new(ELeave) CComparisonTest(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CComparisonTest::CComparisonTest(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CTestAction(aConsole, aOut), iFs(aFs) sl@0: /** sl@0: Constructor sl@0: @param aFs Shared file server session required by base-class sl@0: @param aConsole Console implemenation required by base class sl@0: @param aOut Output utilities required by base class sl@0: */ sl@0: { sl@0: } sl@0: sl@0: void CComparisonTest::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: /** sl@0: Second phase constructor sl@0: @param aTestActionSpec parameters for this test base sl@0: */ sl@0: { sl@0: CTestAction::ConstructL(aTestActionSpec); sl@0: HBufC8* body = HBufC8::NewLC(aTestActionSpec.iActionBody.Length()); sl@0: body->Des().Copy(aTestActionSpec.iActionBody); sl@0: sl@0: TPtrC8 cert1FileName = Input::ParseElement(*body, KCertificate1Start); sl@0: if (! cert1FileName.Length() > 0) sl@0: { sl@0: SetScriptError(ESyntax, _L("Missing tag: certificate1")); sl@0: iFinished = ETrue; sl@0: return; sl@0: } sl@0: sl@0: iCert1 = ReadCertificateL(cert1FileName); sl@0: sl@0: TPtrC8 cert2FileName = Input::ParseElement(*body, KCertificate2Start); sl@0: if (! cert2FileName.Length() > 0) sl@0: { sl@0: SetScriptError(ESyntax, _L("Missing tag: certificate1")); sl@0: iFinished = ETrue; sl@0: return; sl@0: } sl@0: iCert2 = ReadCertificateL(cert2FileName); sl@0: iMatchExpected = Input::ParseElementBoolL(*body, KMatchExpectedStart); sl@0: CleanupStack::PopAndDestroy(body); sl@0: } sl@0: sl@0: CX509Certificate* CComparisonTest::ReadCertificateL(const TDesC8& aFileName) sl@0: /** sl@0: Reads an X.509 certificate from a file. sl@0: @param aFileName The name of the certificate file. sl@0: @return A pointer to the new certificate object. sl@0: */ sl@0: { sl@0: TFileName fn; sl@0: fn.Copy(aFileName); sl@0: sl@0: iOut.write(_L("Loading: %S\n"), &fn); sl@0: RFile file; sl@0: User::LeaveIfError(file.Open(iFs, fn, EFileRead | EFileShareReadersOnly)); sl@0: CleanupClosePushL(file); sl@0: TInt size; sl@0: User::LeaveIfError(file.Size(size)); sl@0: RBuf8 buf; sl@0: buf.CreateL(size); sl@0: CleanupClosePushL(buf); sl@0: sl@0: User::LeaveIfError(file.Read(buf, size)); sl@0: sl@0: CX509Certificate* cert = CX509Certificate::NewL(buf); sl@0: CleanupStack::PopAndDestroy(2, &file); sl@0: return cert; sl@0: } sl@0: sl@0: CComparisonTest::~CComparisonTest() sl@0: /** sl@0: Destructor sl@0: */ sl@0: { sl@0: delete iCert1; sl@0: delete iCert2; sl@0: } sl@0: sl@0: void CComparisonTest::DoPerformPrerequisite(TRequestStatus& aStatus) sl@0: { sl@0: iActionState = EAction; sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: sl@0: void CComparisonTest::DoPerformPostrequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: iFinished = ETrue; sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: sl@0: void CComparisonTest::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: TRAPD(err, DoActionL()); sl@0: TRequestStatus* status = &aStatus; sl@0: iActionState = EPostrequisite; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: void CComparisonTest::DoActionL() sl@0: /** sl@0: Compares iCert1 and iCert2 using CX509Certificate::IsEqualL sl@0: */ sl@0: { sl@0: HBufC* subject1 = iCert1->SubjectL(); sl@0: CleanupStack::PushL(subject1); sl@0: HBufC* subject2 = iCert2->SubjectL(); sl@0: CleanupStack::PushL(subject2); sl@0: sl@0: HBufC* issuer1 = iCert1->IssuerL(); sl@0: CleanupStack::PushL(issuer1); sl@0: HBufC* issuer2 = iCert2->IssuerL(); sl@0: CleanupStack::PushL(issuer2); sl@0: sl@0: iConsole.Printf(_L("Comparing certificates\n")); sl@0: iOut.write(_L("Comparing certificates\n")); sl@0: sl@0: iConsole.Printf(_L("cert1:\n\tsubject: %S\n\tissuer %S\n"), subject1, issuer1); sl@0: iOut.write(_L("cert1:\n\tsubject: %S\n\tissuer %S\n"), subject1, issuer1); sl@0: sl@0: iConsole.Printf(_L("cert2:\n\tsubject: %S\n\tissuer %S\n"), subject2, issuer2); sl@0: iOut.write(_L("cert2:\n\tsubject: %S issuer\n\t%S\n"), subject2, issuer2); sl@0: sl@0: TBool match = iCert1->IsEqualL(*iCert2); sl@0: iConsole.Printf(_L("Match expected %d, result %d\n"), iMatchExpected, match); sl@0: iOut.write(_L("Match expected %d, result %d\n"), iMatchExpected, match); sl@0: sl@0: iResult = (match == iMatchExpected); sl@0: sl@0: if (iResult) sl@0: { sl@0: iConsole.Printf(_L(" Success\n")); sl@0: iOut.writeString(_L(" Success\n")); sl@0: } sl@0: else sl@0: { sl@0: iConsole.Printf(_L(" Failed\n")); sl@0: iOut.writeString(_L(" Failed")); sl@0: }; sl@0: CleanupStack::PopAndDestroy(4, subject1); sl@0: } sl@0: sl@0: void CComparisonTest::DoReportAction() sl@0: { sl@0: } sl@0: sl@0: void CComparisonTest::DoCheckResult(TInt /*aError*/) sl@0: { sl@0: }