os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_setusers.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 /////////////////////////////////////////////////////////////////////////////////
    27 // CSetUsers
    28 /////////////////////////////////////////////////////////////////////////////////
    29 
    30 CTestAction* CSetUsers::NewL(RFs& aFs, 
    31 							 CConsoleBase& aConsole, 
    32 							 Output& aOut,
    33 							 const TTestActionSpec& aTestActionSpec)
    34 	{
    35 	CTestAction* self = CSetUsers::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    36 	CleanupStack::Pop(self);
    37 	return self;
    38 	}
    39 
    40 CTestAction* CSetUsers::NewLC(RFs& aFs,
    41 							  CConsoleBase& aConsole, 
    42 							  Output& aOut,
    43 							  const TTestActionSpec& aTestActionSpec)
    44 	{
    45 	CSetUsers* self = new (ELeave) CSetUsers(aFs, aConsole, aOut);
    46 	CleanupStack::PushL(self);
    47 	self->ConstructL(aTestActionSpec);
    48 	return self;
    49 	}
    50 
    51 CSetUsers::~CSetUsers()
    52 	{
    53 	iUsers.Close();
    54 	iKeys.Close();
    55 	}
    56 
    57 CSetUsers::CSetUsers(RFs& aFs, CConsoleBase& aConsole, Output& aOut) :
    58 	CKeyStoreTestAction(aFs, aConsole, aOut), iState(EListKeys)
    59 	{
    60 	}
    61 
    62 void CSetUsers::ConstructL(const TTestActionSpec& aTestActionSpec)
    63 	{
    64 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
    65 
    66 	// Set users
    67 	TInt err = KErrNone;
    68 	TInt pos = 0;
    69 	while (AddUserL(Input::ParseElement(aTestActionSpec.iActionBody, KUserStart, KUserEnd, pos, err)))
    70 		/* do nothing */;
    71 	}
    72 
    73 TBool CSetUsers::AddUserL(const TDesC8& aData)
    74 	{
    75 	if (aData.Length() == 0)
    76 		{
    77 		return EFalse;
    78 		}
    79 	
    80 	TLex8 lex(aData);
    81 	TUid uid;
    82 	lex.Val(uid.iUid);
    83 	User::LeaveIfError(iUsers.Append(uid));
    84 
    85 	return ETrue;
    86 	}
    87 
    88 void CSetUsers::PerformAction(TRequestStatus& aStatus)
    89 	{
    90 	switch (iState)
    91 		{
    92 		case EListKeys:
    93 			{
    94 			// Currently uses first store
    95 			CUnifiedKeyStore& keystore = *CSharedKeyStores::TheUnifiedKeyStores().operator[](0);
    96 
    97 			TCTKeyAttributeFilter filter;
    98 			keystore.List(iKeys, filter, aStatus);			
    99 			iState = EMain;
   100 			break;
   101 			}
   102 
   103 		case EMain:
   104 			{			
   105 			iState = EFinished;
   106 			TRequestStatus* status = &aStatus;
   107 			
   108 			if (iKeys.Count() == 0)
   109 				{
   110 				User::RequestComplete(status, KErrNotFound);
   111 				return;
   112 				}
   113 
   114 			const CCTKeyInfo* key = NULL;
   115 			for (TInt index = 0 ; index < iKeys.Count() ; ++index)
   116 				{
   117 				const CCTKeyInfo* k = iKeys[index];
   118 				if (k->Label() == *iLabel)
   119 					{
   120 					key = k;
   121 					break;
   122 					}
   123 				}
   124 
   125 			if (!key)
   126 				{
   127 				iOut.writeString(_L("Key not found: "));
   128 				iOut.writeString(*iLabel);
   129 				iOut.writeNewLine();
   130 				User::RequestComplete(status, KErrNotFound);
   131 				return;
   132 				}
   133 
   134 
   135 			// Currently uses first store
   136 			CUnifiedKeyStore& keystore = *CSharedKeyStores::TheUnifiedKeyStores().operator[](0);
   137 
   138 			keystore.SetUsers(*key, iUsers, aStatus);
   139 			break;
   140 			}
   141 
   142 		case EFinished:
   143 			{
   144 			TRequestStatus* status = &aStatus;
   145 			User::RequestComplete(status, aStatus.Int());
   146 			if (aStatus == iExpectedResult)
   147 				{
   148 				iResult = ETrue;
   149 				}
   150 			else
   151 				{
   152 				iResult = EFalse;
   153 				}
   154 			
   155 			iActionState = EPostrequisite;
   156 			}
   157 			break;
   158 		}
   159 	}
   160 
   161 void CSetUsers::PerformCancel()
   162 	{
   163     CUnifiedKeyStore* keystore = CSharedKeyStores::TheUnifiedKeyStores().operator[](0);
   164     ASSERT(keystore);
   165     
   166     switch (iState)
   167         {
   168         case EMain:
   169             keystore->CancelList();
   170             break;
   171 
   172         case EFinished:
   173             keystore->CancelSetUsers();
   174             break;
   175             
   176         default:
   177             break;
   178         }
   179 	}
   180 
   181 void CSetUsers::Reset()
   182 	{
   183 	iState = EListKeys;
   184 	iKeys.Close();
   185 	}
   186 
   187 void CSetUsers::DoReportAction()
   188 	{
   189 	iOut.writeString(_L("Setting users..."));
   190 	iOut.writeNewLine();
   191 	}
   192 
   193 
   194 void CSetUsers::DoCheckResult(TInt aError)
   195 	{
   196 	if (iFinished)
   197 		{
   198 		if (aError == KErrNone)
   199 			{
   200 			_LIT(KSuccessful, "Users set successfully\n");
   201 			iConsole.Write(KSuccessful);
   202 			iOut.writeString(KSuccessful);
   203 			iOut.writeNewLine();
   204 			iOut.writeNewLine();
   205 			}
   206 		else
   207 			{
   208 			if (aError!=iExpectedResult)
   209 				{
   210 				_LIT(KFailed, "!!!Set users failure!!!\n");
   211 				iConsole.Write(KFailed);
   212 				iOut.writeString(KFailed);
   213 				}
   214 			else
   215 				{
   216 				_LIT(KFailed, "Set users failed, but expected\n");
   217 				iConsole.Write(KFailed);
   218 				iOut.writeString(KFailed);
   219 				}
   220 
   221 			iOut.writeNewLine();
   222 			iOut.writeNewLine();
   223 			}
   224 		}
   225 	}
   226 
   227