os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_delete.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 <e32base.h>
    20 #include "t_keystore_actions.h"
    21 #include "t_keystore_defs.h"
    22 #include "t_input.h"
    23 
    24 /*static*/ CTestAction* CDeleteKeys::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
    25 {
    26 	CTestAction* self = CDeleteKeys::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    27 	CleanupStack::Pop(self);
    28 	return self;
    29 }
    30 
    31 /*static*/ CTestAction* CDeleteKeys::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
    32 {
    33 	CDeleteKeys* self = new (ELeave) CDeleteKeys(aFs, aConsole, aOut);
    34 	CleanupStack::PushL(self);
    35 	self->ConstructL(aTestActionSpec);
    36 	return self;
    37 }
    38 
    39 CDeleteKeys::~CDeleteKeys()
    40 {
    41 	iKeys.Close();
    42 }
    43 
    44 void CDeleteKeys::PerformAction(TRequestStatus& aStatus)
    45 {
    46     if (aStatus != KErrNone)
    47         {
    48         iState = EFinished;
    49         }
    50 
    51 	CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
    52 	ASSERT(keyStore);
    53 
    54     switch (iState)
    55 		{		
    56 		case EListKeys:
    57 			keyStore->List(iKeys, iFilter, aStatus);
    58 
    59 			if (*iLabel != KNullDesC)				
    60 				iState = EDeleteKeyByLabel;
    61 			else if (iNonExistentKey)
    62 				iState = EDeleteNonExistentKey;
    63 			else
    64 				iState = EDeleteAllKeys;
    65 				
    66 			break;
    67 
    68 		case EDeleteKeyByLabel:
    69 			for ( ; iDeleteIndex < iKeys.Count() ; ++iDeleteIndex)
    70 				{
    71 				CCTKeyInfo* key = iKeys[iDeleteIndex];
    72 				if (key->Label() == *iLabel)
    73 					{
    74 					keyStore->DeleteKey(*key, aStatus);
    75 					++iDeletedCount;
    76 					break;
    77 					}
    78 				}
    79 
    80 			if (iDeleteIndex == iKeys.Count())
    81 				{
    82 				TRequestStatus* status = &aStatus;				
    83 				User::RequestComplete(status, KErrNotFound);
    84 				}
    85 			
    86 			iState = EFinished;
    87 			break;
    88 
    89 		case EDeleteNonExistentKey:
    90 			{
    91 			// Skip test if we want to delete a non-existant key, but there are
    92 			// no keys present to get token handle from - this happens if
    93 			// previous tests fail
    94 			if (iKeys.Count() == 0)
    95 				{
    96 				_LIT(KSkippingTest, "!!!No keys present, skipping delete non-existant key test!!!\n");
    97 				iConsole.Write(KSkippingTest);
    98 				iOut.writeString(KSkippingTest);
    99 			    iActionState = EPostrequisite;
   100 				TRequestStatus* status = &aStatus;
   101 				User::RequestComplete(status, KErrNone);
   102 				break;
   103 				}
   104 
   105 			CCTKeyInfo* keyInfo = iKeys[0];
   106 			iDeleteHandle = keyInfo->Handle();
   107 			if (iNonExistentKey > 0)
   108 				{
   109 				//	Modify the handle to a key that will not exist
   110 				iDeleteHandle.iObjectId = 0xfbadcafe;
   111 				}
   112 
   113 			iState = EFinished;			
   114 			keyStore->DeleteKey(iDeleteHandle, aStatus); 				
   115 			}
   116 			break;
   117 
   118 		case EDeleteAllKeys:
   119 			if (iDeleteIndex < iKeys.Count())
   120 				{
   121 				iState = EDeleteAllKeys;
   122 				keyStore->DeleteKey(*iKeys[iDeleteIndex], aStatus);
   123 				++iDeleteIndex;
   124 				++iDeletedCount;
   125 				}
   126 			else
   127 				{
   128 				iState = EFinished;
   129 				TRequestStatus* status = &aStatus;				
   130 				User::RequestComplete(status, KErrNone);
   131 				}
   132 			break;			
   133 
   134 		case EFinished:
   135 			{
   136             iOut.writeString(_L("Delete: initial key count == "));
   137             iOut.writeNum(iKeys.Count());
   138             iOut.writeNewLine();
   139             iOut.writeString(_L("Delete: delete count == "));
   140             iOut.writeNum(iDeletedCount);
   141             iOut.writeNewLine();			
   142             iOut.writeString(_L("Delete: status == "));
   143             iOut.writeNum(aStatus.Int());
   144             iOut.writeNewLine();
   145 
   146 			if (aStatus != KErrNone && iDeletedCount > 0)
   147 				{
   148 				// Attempt to delete key failed, adjust count
   149 				--iDeletedCount;
   150 				}
   151             
   152 			iResult = (aStatus == iExpectedResult) &&
   153 				(iExpectedDeleteCount == iDeletedCount || iExpectedDeleteCount == -1);
   154 			
   155 			iActionState = EPostrequisite;
   156             
   157 			TRequestStatus* status = &aStatus;
   158 			User::RequestComplete(status, aStatus.Int());
   159 			}
   160 			break;
   161         
   162 		default:
   163 			User::Invariant();	
   164 	}
   165 }
   166 
   167 void CDeleteKeys::PerformCancel()
   168     {
   169     CUnifiedKeyStore* keystore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   170     ASSERT(keystore);
   171 	keystore->Cancel();
   172     }
   173 
   174 void CDeleteKeys::Reset()
   175 	{
   176 	iState = EListKeys;
   177 	iDeleteIndex = 0;
   178 	iDeletedCount = 0;
   179 	iKeys.Close();
   180 	}
   181 
   182 void CDeleteKeys::DoReportAction()
   183 {
   184 	_LIT(KDeleting, "Deleting...");
   185 	iOut.writeString(KDeleting);
   186 	iOut.writeNewLine();
   187 }
   188 
   189 void CDeleteKeys::DoCheckResult(TInt aError)
   190 {
   191 	if (iFinished)
   192 	{
   193 		TBuf<256> buf;
   194 		if (aError == KErrNone)
   195 		{
   196 			if (iExpectedDeleteCount!=-1)
   197 			{
   198 				_LIT(KSuccessful, "%d Key(s) deleted successfully (expected to delete %d)\n");			
   199 				buf.Format(KSuccessful, iDeletedCount, iExpectedDeleteCount);
   200 				iConsole.Write(buf);
   201 				iOut.writeString(buf);
   202 				iOut.writeNewLine();
   203 			}
   204 		}
   205 		else
   206 		{
   207 			if (aError!=iExpectedResult)
   208 			{
   209 				if (iExpectedDeleteCount!=-1)
   210 				{
   211 					_LIT(KFailed, "!!!Key delete failure %d!!!\n");
   212 					buf.Format(KFailed, aError);
   213 					iConsole.Write(buf);
   214 					iOut.writeString(buf);
   215 				}
   216 			}
   217 			else
   218 			{
   219 				_LIT(KFailed, "Key delete failed, but expected\n");
   220 				iConsole.Write(KFailed);
   221 				iOut.writeString(KFailed);
   222 			}
   223 
   224 			iOut.writeNewLine();
   225 		}
   226 	}
   227 }
   228 
   229 CDeleteKeys::CDeleteKeys(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   230  :	CKeyStoreTestAction(aFs, aConsole, aOut), 
   231 	iInitialKeyCount(-1), 
   232 	iFinalKeyCount(0),
   233 	iDeletedCount(0),
   234 	iDeleteIndex(0),
   235 	iNonExistentKey(0),
   236 	iExpectedDeleteCount(-1)
   237 {}
   238 
   239 void CDeleteKeys::ConstructL(const TTestActionSpec& aTestActionSpec)
   240 {
   241 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
   242 	iFilter.iKeyAlgorithm = iAlgorithm;
   243 
   244 	TPtrC8 buf(0,0);
   245 	buf.Set(Input::ParseElement(aTestActionSpec.iActionBody, KNonExistentKeyStart));
   246 	if (buf.Length() != 0)
   247 		SetNonExistent(buf);
   248 
   249 	buf.Set(Input::ParseElement(aTestActionSpec.iActionBody, KDeleteCountStart));
   250 	if (buf.Length() != 0)
   251 		SetDeleteCount(buf);
   252 	
   253 	buf.Set(Input::ParseElement(aTestActionSpec.iActionBody, KOwnerStart));
   254 	if (buf.Length() != 0)
   255 		{
   256 		User::Leave(KErrNotSupported);
   257 		}
   258 
   259 	if (*iLabel != KNullDesC || iNonExistentKey)
   260 		{
   261 		iFilter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
   262 		}
   263 	else
   264 		{		
   265 		iFilter.iPolicyFilter = TCTKeyAttributeFilter::EManageableKeys;
   266 		}
   267 
   268 	iState = EListKeys;
   269 }
   270 
   271 
   272 void CDeleteKeys::SetNonExistent(const TDesC8& aNonExistent)
   273 {
   274 	TLex8 lexer(aNonExistent);
   275 	lexer.Val(iNonExistentKey);
   276 }
   277 
   278 void CDeleteKeys::SetDeleteCount(const TDesC8& aDeleteCount)
   279 {
   280 	TLex8 lexer(aDeleteCount);
   281 	lexer.Val(iExpectedDeleteCount);
   282 }