os/security/cryptoservices/certificateandkeymgmt/tcertstore/t_testcertfordeletable.cpp
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_testcertfordeletable.h"
20 #include "t_certstoredefs.h"
22 #include "t_certstoreout.h"
24 #include "t_WritableCSDummies.h"
27 CTestAction* CTestCertForDeletable::NewL(RFs& aFs,
28 CConsoleBase& aConsole,
30 const TTestActionSpec& aTestActionSpec)
32 CTestCertForDeletable* self = new(ELeave) CTestCertForDeletable(aFs, aConsole, aOut);
33 CleanupStack::PushL(self);
34 self->ConstructL(aTestActionSpec);
35 CleanupStack::Pop(self);
39 CTestCertForDeletable::~CTestCertForDeletable()
42 delete iCertificateURL;
43 delete iCertificateContent;
44 delete iCertificateLabel;
47 void CTestCertForDeletable::TestSerializationL( MCTToken& aToken,
48 TKeyIdentifier* aIssuerKeyId,
49 TKeyIdentifier* aSubjectKeyId,
50 const TInt aCertificateId,
51 const TDesC8* aIssuerHash )
53 CCTCertInfo* cert = CCTCertInfo::NewLC( *iCertificateLabel, // const TDesC&
54 iCertificateFormat, // TCertificateFormat
55 iOwnerType, // TCertificateOwnerType
56 999, // TInt aSize | aCert.Length()
57 aSubjectKeyId, // const TKeyIdentifier*
58 aIssuerKeyId, // const TKeyIdentifier*
60 aCertificateId, // TInt aCertificateId
62 aIssuerHash ); // const TDesC8* aIssuerHash = NULL
66 User::LeaveIfError(fs.Connect());
67 CleanupClosePushL(fs); // . fs -> stack
69 // this is a name of the file that will be opened as
70 // a stream to test externalize/internalize
71 TDriveUnit sysDrive (fs.GetSystemDrive());
72 TBuf<24> fileName (sysDrive.Name());
73 fileName.Append(_L("\\certinfo.dat"));
75 RFileWriteStream write_stream;
76 User::LeaveIfError( write_stream.Replace(fs, fileName, EFileWrite) );
77 CleanupClosePushL(write_stream); // . . write_stream -> stack
79 cert->ExternalizeL(write_stream);
80 CleanupStack::PopAndDestroy(); // . . write_stream <- pop
82 // now internalize back from stream
83 RFileReadStream read_stream;
84 User::LeaveIfError( read_stream.Open(fs, fileName, EFileRead) );
85 CleanupClosePushL(read_stream); // . . read_stream -> stack
87 CCTCertInfo* cert_read =
88 CCTCertInfo::NewLC(read_stream, aToken); // does InternalizeL()
89 // . . . cert_read -> stack
90 // log certinfo params just internalized
91 iOut.writeString(_L("Cert attributes after internalize:"));
93 WriteFormat( cert_read->CertificateFormat() );
94 iOut.writeString(_L("\tiDeletable = "));
95 iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
101 // check deletable flag is persistent
102 if ( cert_read->IsDeletable() != iDeletable )
104 iResultGood = EFalse;
106 // check format is persistent
107 if ( cert_read->CertificateFormat() != iCertificateFormat )
109 iResultGood = EFalse;
113 CleanupStack::PopAndDestroy(cert_read); // . . . cert_read <- pop
114 CleanupStack::PopAndDestroy(); // . . read_stream <- pop
115 CleanupStack::PopAndDestroy(&fs); // . fs <- pop
116 CleanupStack::PopAndDestroy(cert); // initial certinfo <- pop
119 void CTestCertForDeletable::PerformAction(TRequestStatus& aStatus)
122 MCTWritableCertStore& ustore =
123 UnifiedCertStore().WritableCertStore(iStoreIndex);
124 MCTToken& token = ustore.Token();
126 TKeyIdentifier* issuerKeyId = NULL;
127 TKeyIdentifier* subjectKeyId = NULL;
129 if (iOwnerType != ECACertificate)
131 issuerKeyId = & iIssuerKeyId;
132 subjectKeyId = & iSubjectKeyId;
135 const TInt KCertificateId = 0x00000213;
136 _LIT8(KSomeHash, "\x70\xe4\xf4\x54\x5f\x8e\xe6\xf2\xbd\x4e\x76\x2b\x8d\xa1\x83\xd8\xe0\x5d\x4a\x7d");
138 // create some certinfo object
139 ASSERT(iCertificateLabel);
143 case ETestSerialization:
145 iOut.writeString(_L("Checking serialization for a certificate..."));
148 TRAPD( err, TestSerializationL( token, issuerKeyId, subjectKeyId,
149 KCertificateId, &KSomeHash) );
151 TRequestStatus* status = &aStatus;
152 if (err != KErrNone )
154 // TestSerializationL() did leave
156 iResultGood = EFalse;
157 User::RequestComplete(status, err);
161 // TestSerializationL() passed ok
163 User::RequestComplete(status, aStatus.Int());
171 case ETestNewCSClasswDeletable: // check mctwritablecertstore
173 iOut.writeString(_L("Testing MCTWritableCertStore::Add() w deletable..."));
176 // create a writablestore supporting new Add()
177 TNewCSClasswDeletable storeNew;
180 storeNew.Add( *iCertificateLabel,
185 *iCertificateContent, // this is probably unset
195 case ETestOldCSClasswoDeletable: // check mctwritablecertstore
198 iOut.writeString(_L("Testing new MCTWritableCertStore::Add() on old class..."));
201 // create a writablestore that does not have new Add()
202 TOldCSClasswoDeletable storeOld;
204 // call new Add(), expect to get KErrNotSupported
205 storeOld.Add( *iCertificateLabel,
210 *iCertificateContent, // this is probably unset
216 TRequestStatus* status = &aStatus;
217 User::RequestComplete(status, aStatus.Int());
225 if (aStatus == KErrNone)
230 if (aStatus == iExpectedResult)
241 if (aStatus != KErrNoMemory)
246 TRequestStatus* status = &aStatus;
247 User::RequestComplete(status, aStatus.Int());
256 } // CTestCertForDeletable::PerformAction()
258 void CTestCertForDeletable::PerformCancel()
261 MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex);
265 void CTestCertForDeletable::AfterOOMFailure()
269 void CTestCertForDeletable::Reset()
274 void CTestCertForDeletable::DoReportAction()
276 iOut.writeString(_L("\tLabel = "));
277 iOut.writeString(*iCertificateLabel);
279 iOut.writeString(_L("\tOwner type = "));
281 WriteFormat(iCertificateFormat);
283 iOut.writeString(_L("\tSubjectKeyId: "));
284 iOut.writeOctetString(iSubjectKeyId);
287 iOut.writeString(_L("\tiDeletable = "));
288 iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
293 void CTestCertForDeletable::WriteFormat(TCertificateFormat aFormat)
295 iOut.writeString(_L("\tFormat = "));
298 case EX509Certificate:
299 iOut.writeString(_L("X.509\n"));
302 case EWTLSCertificate:
303 iOut.writeString(_L("WTLS\n"));
306 case EX968Certificate:
307 iOut.writeString(_L("X968\n"));
310 case EX509CertificateUrl:
311 iOut.writeString(_L("X.509 URL\n"));
314 case EWTLSCertificateUrl:
315 iOut.writeString(_L("WTLS URL\n"));
318 case EX968CertificateUrl:
319 iOut.writeString(_L("X968 URL\n"));
323 iOut.writeString(_L("Unknown format\n"));
328 void CTestCertForDeletable::WriteOwnerType()
333 iOut.writeString(_L("CA\n"));
336 case EUserCertificate:
337 iOut.writeString(_L("User"));
340 case EPeerCertificate:
341 iOut.writeString(_L("Peer"));
345 iOut.writeString(_L("Unknown"));
350 CTestCertForDeletable::CTestCertForDeletable(RFs& aFs, CConsoleBase& aConsole,
352 : CCertStoreTestAction(aFs, aConsole, aOut), iState(EFinished)
356 void CTestCertForDeletable::ConstructL(const TTestActionSpec& aTestActionSpec)
358 CCertStoreTestAction::ConstructL(aTestActionSpec);
360 SetCertFormatL(Input::ParseElement(aTestActionSpec.iActionBody, KCertFormatStart));
361 SetCertOwnerTypeL(Input::ParseElement(aTestActionSpec.iActionBody, KCertOwnerTypeStart));
362 SetCertLabelL(Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart));
363 SetKeyId(iIssuerKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KIssuerKeyStart));
364 SetKeyId(iSubjectKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KSubjectKeyStart));
365 SetStoreToUse(Input::ParseElement(aTestActionSpec.iActionBody, KStoreToUseStart));
366 SetDeletable(Input::ParseElement(aTestActionSpec.iActionBody, KDeletableStart));
367 SetSubActionL(Input::ParseElement(aTestActionSpec.iActionBody, KSubActionStart));
369 // Setting the expected result
370 HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
371 TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
372 Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
373 CleanupStack::PopAndDestroy(result);
376 void CTestCertForDeletable::SetKeyId(TKeyIdentifier& aKeyIdentifier, const TDesC8& aKeyInfo)
378 TInt size = aKeyInfo.Length();
379 for (TInt i = 0; i < size; i += 2)
381 TInt a = (aKeyInfo[i+1] >= 'a') ? (aKeyInfo[i+1] - 'a' + 10) : (aKeyInfo[i+1] - '0');
382 TInt b = (aKeyInfo[i] >= 'a') ? (aKeyInfo[i] - 'a' + 10) : (aKeyInfo[i] - '0');
383 aKeyIdentifier.Append(a + b * 16);
387 void CTestCertForDeletable::SetCertFormatL(const TDesC8& aFormat)
389 if (aFormat == KX509)
391 iCertificateFormat = EX509Certificate;
393 else if (aFormat == KWTLS)
395 iCertificateFormat = EWTLSCertificate;
397 else if (aFormat == KX968)
399 iCertificateFormat = EX968Certificate;
401 else if (aFormat == KX509URL)
403 iCertificateFormat = EX509CertificateUrl;
405 else if (aFormat == KWTLSURL)
407 iCertificateFormat = EWTLSCertificateUrl;
409 else if (aFormat == KX968URL)
411 iCertificateFormat = EX968CertificateUrl;
413 else if (aFormat == KUnknown)
415 iCertificateFormat = EUnknownCertificate;
419 iOut.write(_L("Unrecognized cert format: "));
420 iOut.writeString(aFormat);
422 User::Leave(KErrArgument);
426 void CTestCertForDeletable::SetCertOwnerTypeL(const TDesC8& aOwnerType)
428 if (aOwnerType == KCACert)
430 iOwnerType = ECACertificate;
432 else if (aOwnerType == KUserCert)
434 iOwnerType = EUserCertificate;
436 else if (aOwnerType == KPeerCert)
438 iOwnerType = EPeerCertificate;
440 else if (aOwnerType == KUnknown)
442 // set dummy bogus owner type
443 iOwnerType = static_cast<TCertificateOwnerType>(EPeerCertificate + 1);
447 iOut.write(_L("Unknown cert owner type: "));
448 iOut.writeString(aOwnerType);
450 User::Leave(KErrArgument);
454 void CTestCertForDeletable::SetSubActionL(const TDesC8& aStringVal)
456 if (aStringVal == KTestSerialization)
458 iState = ETestSerialization;
460 else if (aStringVal == KTestNewCSClasswDeletable)
462 iState = ETestNewCSClasswDeletable;
464 else if (aStringVal == KTestOldCSClasswoDeletable)
466 iState = ETestOldCSClasswoDeletable;
471 iOut.write(_L("Unknown subaction type: "));
472 iOut.writeString(aStringVal);
474 User::Leave(KErrArgument);
479 void CTestCertForDeletable::SetCertLabelL(const TDesC8& aLabel)
481 delete iCertificateLabel;
482 iCertificateLabel = NULL;
483 iCertificateLabel = HBufC::NewL(aLabel.Length());
484 TPtr ptr = iCertificateLabel->Des();
490 void CTestCertForDeletable::SetStoreToUse(const TDesC8& aStoreToUse)
492 TLex8 lex(aStoreToUse);
493 lex.Val(iStoreIndex);
496 void CTestCertForDeletable::SetDeletable(const TDesC8& aStringVal)
498 TLex8 lex(aStringVal);
502 void CTestCertForDeletable::SetCertificateContentL(const TDesC8& aFileName)
505 fileName.Copy(aFileName);
507 User::LeaveIfError(fs.Connect());
508 CleanupClosePushL(fs);
509 __ASSERT_DEBUG(!iCertificateContent, User::Panic(_L("CTestCertForDeletable"), 1));
510 TRAPD(err, iCertificateContent = Input::ReadFileL(fileName, fs));
513 iConsole.Printf(_L("Error reading file : "));
514 iConsole.Printf(fileName);
515 iConsole.Printf(_L("\n"));
518 CleanupStack::PopAndDestroy(); // fs
521 void CTestCertForDeletable::ConstructCertL(const TDesC8& aCert)
524 filename.Copy(aCert);
526 User::LeaveIfError(fs.Connect());
527 CleanupClosePushL(fs);
529 TRAPD(err, certBuf = Input::ReadFileL(filename, fs));
532 iConsole.Printf(_L("Error reading file : "));
533 iConsole.Printf(filename);
534 iConsole.Printf(_L("\n"));
537 CleanupStack::PushL(certBuf);
538 switch (iCertificateFormat)
540 case EX509Certificate:
541 iCertificate = CX509Certificate::NewL(*certBuf);
544 case EWTLSCertificate:
545 iCertificate = CWTLSCertificate::NewL(*certBuf);
549 // Unknown format - do nothing
552 CleanupStack::PopAndDestroy(2); // fs and certBuf
555 void CTestCertForDeletable::DoCheckResult(TInt aError)
559 if (iResult && !iResultGood)
561 iConsole.Write(_L("\ttestcertfordeletable failed\n"));
562 iOut.writeString(_L("\ttestcertfordeletable failed"));
568 iConsole.Write(_L("\ttestcertfordeletable passed ok\n"));
569 iOut.writeString(_L("\ttestcertfordeletable passed ok"));
575 iConsole.Write(_L("\tcertinfo couldn't be tested\n"));
576 iOut.writeString(_L("\tcertinfo couldn't be tested"));
578 iOut.writeString(_L("\t"));
579 iOut.writeError(aError);
580 if (aError == KErrBadName)
582 iOut.writeString(_L(" - Check that the label is unique"));
584 if (aError == KErrAccessDenied)
586 iOut.writeString(_L(" - Can't open filestream. Access denied."));