os/security/cryptoservices/certificateandkeymgmt/tcertstore/t_certstoreactionsfilecertstore.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 /**
    20  @file
    21 */
    22 
    23 #include "t_certstoreactionsfilecertstore.h"
    24 #include "t_certstoredefs.h"
    25 #include "t_input.h"
    26 
    27 CTestAction* CCreateFileCertStore::NewL(RFs &aFs, 
    28 										CConsoleBase& aConsole, 
    29 										Output& aOut,
    30 										const TTestActionSpec& aTestActionSpec)
    31 	{
    32 	CTestAction* self = NewLC(aFs, aConsole, aOut, aTestActionSpec);
    33 	CleanupStack::Pop(self);
    34 	return self;
    35 	}
    36 	
    37 CTestAction* CCreateFileCertStore::NewLC(RFs &aFs, 
    38 										 CConsoleBase& aConsole, 
    39 										 Output& aOut,
    40 										 const TTestActionSpec& aTestActionSpec)	
    41 	{
    42 	CCreateFileCertStore* self = 
    43 		new(ELeave) CCreateFileCertStore(aFs, aConsole, aOut);
    44 	CleanupStack::PushL(self);
    45 	self->ConstructL(aTestActionSpec);
    46 	return self;
    47 	}
    48 
    49 CCreateFileCertStore::~CCreateFileCertStore()	
    50 	{
    51 	delete iFileName;
    52 	}
    53 
    54 void CCreateFileCertStore::PerformAction(TRequestStatus& aStatus)
    55 	{
    56 	switch (iState)
    57 		{
    58 	case EInit:
    59 		{
    60 		TRAPD(err, HandleEInit());
    61 		TRequestStatus* status = &aStatus;
    62 		User::RequestComplete(status, err);
    63 		}
    64 		break;
    65 
    66 	case EFinished:
    67 		{
    68 		TRequestStatus* status = &aStatus;
    69 		User::RequestComplete(status, aStatus.Int());
    70 		if (aStatus == iExpectedResult)
    71 			{
    72 			iResult = ETrue;
    73 			}
    74 		else
    75 			{
    76 			iResult = EFalse;
    77 			}
    78 		iFinished = ETrue;
    79 		}
    80 		break;
    81 		}
    82 	}
    83 
    84 void CCreateFileCertStore::PerformCancel()
    85 	{
    86 	}
    87 
    88 void CCreateFileCertStore::Reset()
    89 	{
    90 	__ASSERT_DEBUG(EFalse, User::Panic(_L("CCreateFileCertStore::Reset()"), 1));
    91 	}
    92 
    93 CCreateFileCertStore::CCreateFileCertStore(RFs &aFs, 
    94 										   CConsoleBase& aConsole,
    95 										   Output& aOut)
    96 : CCertStoreTestAction(aFs, aConsole, aOut), 
    97 iState(EInit), iFs(aFs)
    98 	{
    99 	}
   100 
   101 void CCreateFileCertStore::ConstructL(const TTestActionSpec& aTestActionSpec)
   102 	{
   103 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   104 	TInt err = KErrNone;
   105 	TInt pos = 0;
   106 	TPtrC8 fileName = Input::ParseElement(aTestActionSpec.iActionBody, KFileNameStart, 
   107 		KFileNameEnd, pos, err);
   108 	iFileName = HBufC::NewL(fileName.Length());
   109 	iFileName->Des().Copy(fileName);
   110 	
   111 	// Set expected result
   112 	pos = 0;
   113 	
   114 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   115 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   116 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   117 	CleanupStack::PopAndDestroy(result);
   118 	}
   119 
   120 void CCreateFileCertStore::HandleEInit()
   121 	{
   122 	iState = EFinished;
   123 	CreateStoreL(*iFileName, iFs);
   124 	}
   125 
   126 void CCreateFileCertStore::DoReportAction()
   127 	{
   128 	}
   129 
   130 void CCreateFileCertStore::DoCheckResult(TInt /*aError*/)
   131 	{
   132 	}
   133 
   134 void CCreateFileCertStore::CreateStoreL(const TDesC& aFileName, RFs& aFs)
   135 	{
   136 	aFs.MkDirAll(aFileName);
   137 	RFile file;
   138 	User::LeaveIfError(file.Replace(aFs, aFileName, EFileWrite));
   139 
   140 	//TCleanupItem deleteFile(CFileCertStore::DeleteFile, this);//store will revert() if a leave occurs
   141 	//CleanupStack::PushL(deleteFile);
   142 
   143 	CPermanentFileStore* store = CPermanentFileStore::NewLC(file);
   144 	store->SetTypeL(KPermanentFileStoreLayoutUid);	
   145 
   146 	RStoreWriteStream caCertEntryStream;
   147 	TStreamId caCertEntryStreamId = caCertEntryStream.CreateLC(*store);
   148 	caCertEntryStream.WriteInt32L(0);//we have zero ca certs
   149 	caCertEntryStream.CommitL();
   150 	CleanupStack::PopAndDestroy();
   151 
   152 	RStoreWriteStream rootStream;
   153 	TStreamId rootId = rootStream.CreateLC(*store);
   154 
   155 	rootStream << caCertEntryStreamId;
   156 	rootStream.CommitL();
   157 	CleanupStack::PopAndDestroy();
   158 	
   159 	store->SetRootL(rootId);							
   160 	store->CommitL();							
   161 	CleanupStack::PopAndDestroy();//store
   162 	}
   163 
   164