1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tx509/Validatetest.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,194 @@
1.4 +/*
1.5 +* Copyright (c) 1998-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 "ValidateTest.h"
1.23 +#include "t_input.h"
1.24 +
1.25 +_LIT(KFilenameStart, "<filename>");
1.26 +_LIT(KExpectedKeyIDStart, "<expectedkeyid>");
1.27 +_LIT(KResultStart, "<result>");
1.28 +
1.29 +CTestAction* CValidateTest::NewL(RFs& aFs, CConsoleBase& aConsole,
1.30 + Output& aOut, const TTestActionSpec& aTestActionSpec)
1.31 + {
1.32 + CTestAction* self = CValidateTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.33 + CleanupStack::Pop(self);
1.34 + return self;
1.35 + }
1.36 +
1.37 +CTestAction* CValidateTest::NewLC(RFs& aFs, CConsoleBase& aConsole,
1.38 + Output& aOut, const TTestActionSpec& aTestActionSpec)
1.39 + {
1.40 + CValidateTest* self = new(ELeave) CValidateTest(aFs, aConsole, aOut);
1.41 + CleanupStack::PushL(self);
1.42 + self->ConstructL(aTestActionSpec);
1.43 + return self;
1.44 + }
1.45 +
1.46 +
1.47 +CValidateTest::CValidateTest(RFs& aFs,
1.48 + CConsoleBase& aConsole,
1.49 + Output& aOut)
1.50 +: CTestAction(aConsole, aOut), iFs(aFs)
1.51 + {
1.52 + }
1.53 +
1.54 +void CValidateTest::ConstructL(const TTestActionSpec& aTestActionSpec)
1.55 + {
1.56 + CTestAction::ConstructL(aTestActionSpec);
1.57 + TInt pos=0, resultPos=0;
1.58 + HBufC* aBody = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
1.59 +
1.60 + aBody->Des().Copy(aTestActionSpec.iActionBody);
1.61 + // creates the test chan validation object
1.62 + TPtrC ioFilename = Input::ParseElement(*aBody, KFilenameStart, pos);
1.63 + iFilename.Copy(ioFilename);
1.64 + TPtrC ioKeyId = Input::ParseElement(*aBody, KExpectedKeyIDStart, pos);
1.65 +
1.66 + if(ioKeyId.Length() == 40)
1.67 + {
1.68 + iResultKeyID.SetLength(iResultKeyID.MaxLength());
1.69 + for(TInt byte =0; byte < 40; byte+=2)
1.70 + {
1.71 + TUint8 firstHalfOfOutput = ConvertByteA2H(static_cast<TInt8>(ioKeyId[byte]));
1.72 + TUint8 secondHalfOfOutput = ConvertByteA2H(static_cast<TInt8>(ioKeyId[byte+1]));
1.73 + iResultKeyID[resultPos] = static_cast<TInt8>((firstHalfOfOutput << 4)|secondHalfOfOutput);
1.74 + resultPos++;
1.75 + };
1.76 + }
1.77 + TPtrC ioResult = Input::ParseElement(*aBody, KResultStart, pos);
1.78 +
1.79 + if(ioResult.CompareF(_L("Valid"))==0)
1.80 + iValid = ETrue;
1.81 + else
1.82 + iValid = EFalse;
1.83 +
1.84 + CleanupStack::PopAndDestroy(aBody);
1.85 + }
1.86 +
1.87 +CValidateTest::~CValidateTest()
1.88 + {
1.89 + }
1.90 +
1.91 +TUint8 CValidateTest::ConvertByteA2H(TUint8 aInput)
1.92 + {
1.93 + TUint8 output=0;
1.94 + if ((aInput > 0x29) && (aInput < 0x3a))
1.95 + {
1.96 + output = static_cast<TInt8>(aInput-0x30);
1.97 + }
1.98 + if ((aInput > 0x40) && (aInput < 0x47))
1.99 + {
1.100 + output = static_cast<TInt8>(aInput-0x37);
1.101 + }
1.102 + return output;
1.103 + }
1.104 +
1.105 +
1.106 +void CValidateTest::DoPerformPrerequisite(TRequestStatus& aStatus)
1.107 + {
1.108 + TRequestStatus* status = &aStatus;
1.109 + User::RequestComplete(status, KErrNone);
1.110 + iActionState = EAction;
1.111 + }
1.112 +
1.113 +void CValidateTest::DoPerformPostrequisite(TRequestStatus& aStatus)
1.114 + {
1.115 + TRequestStatus* status = &aStatus;
1.116 + User::RequestComplete(status, KErrNone);
1.117 + iFinished = ETrue;
1.118 + }
1.119 +
1.120 +void CValidateTest::PerformAction(TRequestStatus& aStatus)
1.121 + {
1.122 + CX509Certificate* cert = NULL;
1.123 + TInt err;
1.124 + TFileName filename(iFilename);
1.125 +
1.126 + HBufC8* buf=NULL;
1.127 + TRAP(err, buf = Input::ReadFileL(filename, iFs));
1.128 +
1.129 + if (err == KErrNotFound)
1.130 + {
1.131 + iResult = EFalse;
1.132 + iFinished = ETrue;
1.133 + SetScriptError(EFileNotFound, iFilename);
1.134 + TRequestStatus* status = &aStatus;
1.135 + iActionState = EPostrequisite;
1.136 + User::RequestComplete(status, KErrNone);
1.137 + return;
1.138 + }
1.139 + else if (err != KErrNone)
1.140 + User::Leave(err);
1.141 +
1.142 + CleanupStack::PushL(buf);
1.143 + cert = CX509Certificate::NewLC(buf->Des());
1.144 +
1.145 + TKeyIdentifier id = cert->KeyIdentifierL();
1.146 + CleanupStack::PopAndDestroy(2); // buf & cert
1.147 +
1.148 + iConsole.Printf(_L("Validating file "));
1.149 + iConsole.Printf(iFilename);
1.150 + iOut.writeString(_L("Validating file "));
1.151 + iOut.writeString(iFilename);
1.152 +
1.153 + if(id == iResultKeyID)
1.154 + {
1.155 + iConsole.Printf(_L(" Match "));
1.156 + iOut.writeString(_L(" Match "));
1.157 + if(iValid)
1.158 + iResult = ETrue;
1.159 + else
1.160 + iResult = EFalse;
1.161 + }
1.162 + else
1.163 + {
1.164 + iConsole.Printf(_L(" No Match "));
1.165 + iOut.writeString(_L(" No Match "));
1.166 + if(!iValid)
1.167 + iResult = ETrue;
1.168 + else
1.169 + iResult = EFalse;
1.170 + }
1.171 + if(iResult)
1.172 + {
1.173 + iConsole.Printf(_L(" Success\n"));
1.174 + iOut.writeString(_L(" Success"));
1.175 + iOut.writeNewLine();
1.176 + }
1.177 + else
1.178 + {
1.179 + iConsole.Printf(_L(" Failed\n"));
1.180 + iOut.writeString(_L(" Failed"));
1.181 + iOut.writeNewLine();
1.182 + };
1.183 +
1.184 + TRequestStatus* status = &aStatus;
1.185 + iActionState = EPostrequisite;
1.186 + User::RequestComplete(status, KErrNone);
1.187 + }
1.188 +
1.189 +
1.190 +void CValidateTest::DoReportAction()
1.191 + {
1.192 + }
1.193 +
1.194 +void CValidateTest::DoCheckResult(TInt /*aError*/)
1.195 + {
1.196 + }
1.197 +