First public contribution.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "t_retrieve.h"
20 #include "t_certstoreout.h"
22 #include "t_certstoredefs.h"
23 #include "t_concurrentcertstore.h"
24 #include <ccertattributefilter.h>
26 ///////////////////////////////////////////////////////////////////////////////
27 //CRetrieveCertificate
28 ///////////////////////////////////////////////////////////////////////////////
30 CTestAction* CRetrieveCertificate::NewL(RFs& aFs,
31 CConsoleBase& aConsole,
33 const TTestActionSpec& aTestActionSpec)
35 CRetrieveCertificate* self = new (ELeave) CRetrieveCertificate(aFs, aConsole, aOut);
36 CleanupStack::PushL(self);
37 self->ConstructL(aTestActionSpec);
38 CleanupStack::Pop(self);
42 CRetrieveCertificate::CRetrieveCertificate(RFs& aFs,
43 CConsoleBase& aConsole,
45 CCertStoreTestAction(aFs, aConsole, aOut), iDataPtr(NULL, 0),
46 iFormat(EUnknownCertificate), iDeletable(EFalse), iTestForDeletable(EFalse)
50 void CRetrieveCertificate::ConstructL(const TTestActionSpec& aTestActionSpec)
52 CCertStoreTestAction::ConstructL(aTestActionSpec);
54 iFilter = CCertAttributeFilter::NewL();
56 HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
57 TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
58 Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
59 CleanupStack::PopAndDestroy(result);
61 iLabel.Copy(Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart));
62 if (iLabel.Length() == 0)
64 User::Leave(KErrArgument);
67 // check for a possible deletable flag value for the certificate
70 const TDesC8& deletableStr = Input::ParseElement(aTestActionSpec.iActionResult,
76 // set the deletable attribute if a value was found for the certificate
79 SetDeletable(deletableStr);
81 // if deletable is found then format value must also be present
82 SetFormatL(Input::ParseElement(aTestActionSpec.iActionResult, KCertFormatStart));
86 CRetrieveCertificate::~CRetrieveCertificate()
92 void CRetrieveCertificate::SetDeletable(const TDesC8& aDeletableString)
94 iTestForDeletable = ETrue;
95 if (aDeletableString.Compare(KTrue)==0)
105 void CRetrieveCertificate::SetFormatL(const TDesC8& aFormatString)
107 if (aFormatString == KNullDesC8)
109 User::Leave(KErrArgument);
111 if (aFormatString == KWTLS)
113 iFormat = EWTLSCertificate;
115 else if (aFormatString == KX509)
117 iFormat = EX509Certificate;
119 else if (aFormatString == KWTLSURL)
121 iFormat = EWTLSCertificateUrl;
123 else if (aFormatString == KX509URL)
125 iFormat = EX509CertificateUrl;
129 iOut.write(_L("Unknown cert format: "));
130 iOut.writeString(aFormatString);
132 User::Leave(KErrNotSupported);
136 void CRetrieveCertificate::PerformAction(TRequestStatus& aStatus)
138 if (aStatus != KErrNone && iState != ECheckRetrieve)
146 CertStore().List(iCertInfos, *iFilter, aStatus);
147 iState = ERetrieveData;
152 for (TInt i = 0 ; i < iCertInfos.Count() ; ++i)
154 CCTCertInfo* certInfo = iCertInfos[i];
155 if (certInfo->Label() == iLabel)
157 iCertInfo = certInfo;
165 TRequestStatus* status = &aStatus;
166 User::RequestComplete(status, KErrNotFound);
170 TRAPD(err, iData = HBufC8::NewL(iCertInfo->Size()));
174 TRequestStatus* status = &aStatus;
175 User::RequestComplete(status, err);
180 // iDataPtr has to be be a member as it's passed to asyc retrieve operation
181 iDataPtr.Set(iData->Des());
182 CertStore().Retrieve(*iCertInfo, iDataPtr, aStatus);
190 switch (iCertInfo->CertificateFormat())
192 case EX509Certificate:
194 CX509Certificate* cert = CX509Certificate::NewLC(*iData);
195 X509CertWriter writer(iOut);
196 writer.WriteCert(*cert);
197 CleanupStack::PopAndDestroy();
202 case EWTLSCertificate:
204 CWTLSCertificate* cert = CWTLSCertificate::NewLC(*iData);
205 WTLSCertWriter writer(iOut);
206 writer.WriteCert(*cert);
207 CleanupStack::PopAndDestroy(cert);
216 iState = ERetrieveCert;
217 TRequestStatus* status = &aStatus;
218 User::RequestComplete(status, KErrNone);
223 if (CertStoreType() != EUnifiedCertStore)
226 TRequestStatus* status = &aStatus;
227 User::RequestComplete(status, KErrNone);
231 iState = ECheckRetrieve;
232 UnifiedCertStore().Retrieve(*iCertInfo, iCert, aStatus);
238 TCertificateFormat format = iCertInfo->CertificateFormat();
239 TInt expectedErr = KErrNotSupported;
240 if (format == EX509Certificate || format == EWTLSCertificate)
242 expectedErr = KErrNone;
244 TInt err = (aStatus.Int() == expectedErr) ? KErrNone : KErrGeneral;
246 TRequestStatus* status = &aStatus;
247 User::RequestComplete(status, err);
253 iActionState = EPostrequisite;
255 TRequestStatus* status = &aStatus;
256 User::RequestComplete(status, aStatus.Int());
265 void CRetrieveCertificate::PerformCancel()
270 CertStore().CancelList();
275 CertStore().CancelRetrieve();
283 void CRetrieveCertificate::Reset()
293 void CRetrieveCertificate::DoReportAction()
295 iOut.write(_L("Retrieving certificate...\n"));
296 iOut.write(_L("\tLabel: %S\n"), &iLabel);
297 if (iTestForDeletable)
299 iOut.writeString(_L("\tDeletable = "));
300 iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
305 void CRetrieveCertificate::DoPerformPostrequisite(TRequestStatus& aStatus)
308 TRequestStatus* status = &aStatus;
309 User::RequestComplete(status, aStatus.Int());
312 TBool CRetrieveCertificate::ValidCertInfo()
314 // check iFormat and deletable flag of the cert info object
315 return ((iCertInfo->IsDeletable() == iDeletable) &&
316 (iFormat == iCertInfo->CertificateFormat()));
319 void CRetrieveCertificate::DoCheckResult(TInt aError)
323 iResult = (aError == iExpectedResult);
327 if (iTestForDeletable)
329 iResult = iResult && ValidCertInfo();
333 iOut.writeString(_L("\tRetrieved certificate info successfully - \n\n"));
339 iOut.writeString(_L("\tRetrieved certificate successfully\n\n"));
344 iOut.writeString(_L("\tRetrieve certificate failed\n\n"));
345 // If running tests with multiple threads, failure may be expected
346 // need to add this back in when we get concurrent tests working
347 //CConcurrentTester::SanitizeTestResult(iOut, iResult);