os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_addkey.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2003-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 #include <unifiedkeystore.h>
    20 #include <e32base.h>
    21 #include "t_keystore_actions.h"
    22 #include "t_keystore_defs.h"
    23 #include "t_input.h"
    24 #include "t_output.h"
    25 
    26 _LIT(KEllipsis, "...");
    27 
    28 /////////////////////////////////////////////////////////////////////////////////
    29 // CAddKey
    30 /////////////////////////////////////////////////////////////////////////////////
    31 
    32 CTestAction* CAddKey::NewL(RFs& aFs, 
    33 							CConsoleBase& aConsole, 
    34 							Output& aOut,
    35 							const TTestActionSpec& aTestActionSpec)
    36 {
    37 	CTestAction* self = CAddKey::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    38 	CleanupStack::Pop(self);
    39 	return self;
    40 }
    41 
    42 CTestAction* CAddKey::NewLC(RFs& aFs,
    43 							CConsoleBase& aConsole, 
    44 							Output& aOut,
    45 							const TTestActionSpec& aTestActionSpec)
    46 {
    47 	CAddKey* self = new (ELeave) CAddKey(aFs, aConsole, aOut);
    48 	CleanupStack::PushL(self);
    49 	self->ConstructL(aTestActionSpec);
    50 	return self;
    51 }
    52 
    53 CAddKey::~CAddKey()
    54 {
    55 	if (iKeyInfo != NULL)
    56 		{
    57 		iKeyInfo->Release();
    58 		}
    59 }
    60 
    61 CAddKey::CAddKey(RFs& aFs, CConsoleBase& aConsole, Output& aOut) :
    62 	CKeyStoreTestAction(aFs, aConsole, aOut), iState(EAddKey)
    63 	{
    64 	}
    65 
    66 void CAddKey::ConstructL(const TTestActionSpec& aTestActionSpec)
    67 {
    68 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
    69 
    70 	SetKeySize(Input::ParseElement(aTestActionSpec.iActionBody, KKeySizeStart));
    71 	SetStartDateL(Input::ParseElement(aTestActionSpec.iActionBody, KKeyStartDateStart));
    72 	SetEndDateL(Input::ParseElement(aTestActionSpec.iActionBody, KKeyEndDateStart));
    73 }
    74 
    75 void CAddKey::SetKeySize(const TDesC8& aKeySize)
    76 	{
    77 	TLex8 lex(aKeySize);
    78 	lex.Val(iSize);
    79 	}
    80 
    81 void CAddKey::PerformAction(TRequestStatus& aStatus)
    82 	{
    83 	switch (iState)
    84 		{
    85 		case EAddKey:
    86 			{
    87 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
    88 			if(iKeyStoreImplLabel.Length() != 0)
    89 					{
    90 					SetKeyStoreIndex(keyStore);
    91 					}
    92 					
    93 			#ifdef SYMBIAN_AUTH_SERVER		
    94 			if(iUseNewApi)
    95 				{
    96 				keyStore->CreateKey(iKeyStoreImplIndex, iUsage, iSize, *iLabel, iAlgorithm,
    97 												iAccessType, iStartDate, iEndDate, *iAuthExpression, iFreshness, iKeyInfo, aStatus);
    98 				}
    99 			else
   100 			#endif // SYMBIAN_AUTH_SERVER
   101 				{
   102 				keyStore->CreateKey(iKeyStoreImplIndex, iUsage, iSize, *iLabel, iAlgorithm,
   103 								iAccessType, iStartDate, iEndDate, iKeyInfo, aStatus);
   104 
   105 				}			
   106 			iState = EFinished;
   107 			}
   108 			break;
   109 
   110 		case EFinished:
   111 			{
   112 			TRequestStatus* status = &aStatus;
   113 			User::RequestComplete(status, aStatus.Int());
   114 			if (aStatus == iExpectedResult)
   115 				{
   116 				iResult = ETrue;
   117 				}
   118 			else
   119 				{
   120 				iResult = EFalse;
   121 				}
   122 		
   123 			iActionState = EPostrequisite;
   124 			}
   125 			break;
   126 			
   127 		default:
   128 			break;	//	Nothing to do, for the compiler
   129 		}
   130 	}
   131 
   132 void CAddKey::PerformCancel()
   133     {
   134     if (iState == EFinished)
   135         {
   136         CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   137         keyStore->CancelCreateKey();
   138         }
   139     }
   140 
   141 void CAddKey::Reset()
   142 	{
   143 	iState = EAddKey;
   144 	if (iKeyInfo != NULL)
   145 		{
   146 		iKeyInfo->Release();
   147 		iKeyInfo = NULL;
   148 		}
   149 	}
   150 
   151 void CAddKey::DoReportAction()
   152 {
   153 	_LIT(KAdding, "Adding key...");
   154 	iOut.writeString(KAdding);
   155 
   156 	iOut.writeString(iLabel->Left(32));
   157 	if (iLabel->Length() > 32)
   158 		{
   159 		iOut.writeString(KEllipsis);
   160 		}
   161 	
   162 	iOut.writeNewLine();
   163 }
   164 
   165 
   166 void CAddKey::DoCheckResult(TInt aError)
   167 {
   168 	if (iFinished)
   169 	{
   170 		if (aError == KErrNone)
   171 		{
   172 			_LIT(KSuccessful, "Key added successfully\n");
   173 			iConsole.Write(KSuccessful);
   174 			iOut.writeString(KSuccessful);
   175 			iOut.writeNewLine();
   176 			iOut.writeNewLine();
   177 		}
   178 		else
   179 		{
   180 			if (aError!=iExpectedResult)
   181 			{
   182 				_LIT(KFailed, "!!!Key adding failure!!!\n");
   183 				iConsole.Write(KFailed);
   184 				iOut.writeString(KFailed);
   185 			}
   186 			else
   187 			{
   188 				_LIT(KFailed, "Key adding failed, but expected\n");
   189 				iConsole.Write(KFailed);
   190 				iOut.writeString(KFailed);
   191 			}
   192 
   193 			iOut.writeNewLine();
   194 			iOut.writeNewLine();
   195 		}
   196 	}
   197 }
   198 
   199 
   200 void CAddKey::SetStartDateL(const TDesC8& aData)
   201 	{
   202     if (aData.Length() == 0)
   203 		return;
   204 
   205 	HBufC* buf = HBufC::NewLC(aData.Length());
   206 	TPtr ptr = buf->Des();
   207 	ptr.Copy(aData);	
   208 	User::LeaveIfError(iStartDate.Parse(*buf));
   209 	CleanupStack::PopAndDestroy(buf);
   210 	}
   211 
   212 void CAddKey::SetEndDateL(const TDesC8& aData)
   213 	{
   214     if (aData.Length() == 0)
   215 		return;
   216 
   217 	HBufC* buf = HBufC::NewLC(aData.Length());
   218 	TPtr ptr = buf->Des();
   219 	ptr.Copy(aData);	
   220 	User::LeaveIfError(iEndDate.Parse(*buf));
   221 	CleanupStack::PopAndDestroy(buf);
   222 	}