os/security/cryptoservices/certificateandkeymgmt/tcertstore/T_unifiedcertstoreapplications.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
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_unifiedcertstoreapplications.h"
    24 #include "t_input.h"
    25 #include "t_certstoredefs.h"
    26 #include "t_certstoreout.h"
    27 #include <ccertattributefilter.h>
    28 
    29 CTestAction* CUnifiedCertStoreApplications::NewL(RFs& aFs,
    30 												 CConsoleBase& aConsole,	
    31 												 Output& aOut, 
    32 												 const TTestActionSpec& aTestActionSpec)
    33 	{
    34 	CUnifiedCertStoreApplications* self = 
    35 		new(ELeave) CUnifiedCertStoreApplications(aFs, aConsole, aOut);
    36 	CleanupStack::PushL(self);
    37 	self->ConstructL(aTestActionSpec);
    38 	CleanupStack::Pop(self);
    39 	return self;
    40 	}
    41 
    42 CUnifiedCertStoreApplications::~CUnifiedCertStoreApplications()
    43 	{
    44 	iCertInfos.Close();
    45 	iExpectedApplications.Reset();
    46 	delete iFilter;
    47 	iApplications.Close();
    48 	}
    49 
    50 CUnifiedCertStoreApplications::CUnifiedCertStoreApplications(RFs& aFs, CConsoleBase& aConsole,	
    51 															 Output& aOut)
    52 : CCertStoreTestAction(aFs, aConsole, aOut), iState(EGetCert)
    53 	{
    54 	}
    55 
    56 void CUnifiedCertStoreApplications::ConstructL(const TTestActionSpec& aTestActionSpec)
    57 	{
    58 	CCertStoreTestAction::ConstructL(aTestActionSpec);
    59 	iFilter = CCertAttributeFilter::NewL();
    60 	TInt err = KErrNone;
    61 	TInt pos = 0;
    62 	
    63 	TPtrC8 pLabel = Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart, KCertLabelEnd, pos, err);
    64 	if (err != KErrNone)
    65 		{
    66 		User::Leave(err);
    67 		}
    68 	iCertificateLabel.Copy(pLabel);
    69 
    70 	// Set expected result
    71 
    72 	pos = 0;
    73 	
    74 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
    75 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
    76 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
    77 	CleanupStack::PopAndDestroy(result);
    78 	
    79 	const TDesC8& uidsString = Input::ParseElement(aTestActionSpec.iActionResult, KUIDStart, KUIDEnd,
    80 		pos, err);
    81 	TLex8 lex(uidsString);
    82 
    83 	err = KErrNone;
    84 	while (err == KErrNone)
    85 		{
    86 		TUid uid;
    87 		err = lex.Val(uid.iUid);
    88 		if (err == KErrNone)
    89 			{	
    90 			lex.SkipSpace();
    91 			User::LeaveIfError(iExpectedApplications.Append(uid));
    92 			}
    93 		}
    94 	}
    95 
    96 void CUnifiedCertStoreApplications::GetApplications(TRequestStatus& aStatus)
    97 	{
    98 	TRequestStatus* status = &aStatus;
    99 	TInt ix = KErrNotFound;
   100 	TInt count = iCertInfos.Count();
   101 	for (TInt i = 0; i < count; i++)
   102 		{
   103 		if (iCertInfos[i]->Label() == iCertificateLabel)
   104 			{
   105 			ix = i;
   106 			break;
   107 			}
   108 		}
   109 	if (ix == KErrNotFound)
   110 		{
   111 		User::RequestComplete(status, ix);
   112 		}	
   113 	else
   114 		{
   115 		iCertInfoForApplications = iCertInfos[ix];
   116 		CertStore().Applications(*iCertInfoForApplications, iApplications, aStatus);
   117 		}
   118 	}
   119 
   120 void CUnifiedCertStoreApplications::PerformAction(TRequestStatus& aStatus)
   121 	{
   122 	switch (iState)
   123 		{
   124 		case EGetCert:
   125 			iState = EGettingApplications;
   126 			//get the certs
   127 			CertStore().List(iCertInfos, *iFilter, aStatus);
   128 			break;
   129 			
   130 		case EGettingApplications:
   131 			iState = EFinished;
   132 			//get the certificates associated applications
   133 			GetApplications(aStatus);
   134 			break;
   135 			
   136 		case EFinished:
   137 			{
   138 			TRequestStatus* status = &aStatus;
   139 			User::RequestComplete(status, aStatus.Int());
   140 			if (aStatus == iExpectedResult)
   141 				{
   142 				// Check that we have the expected uids
   143 				TInt iEnd = iApplications.Count();
   144 				if (iEnd == iExpectedApplications.Count())
   145 					{
   146 					TInt i = 0;
   147 					for (i = 0; i < iEnd; i++)
   148 						{
   149 						if ((iApplications)[i] != iExpectedApplications[i])
   150 							{
   151 							break;
   152 							}
   153 						}
   154 					if (i == iEnd)
   155 						{
   156 						iResult = ETrue;
   157 						}
   158 					else
   159 						{
   160 						iResult = EFalse;
   161 						}
   162 					}
   163 				else
   164 					{
   165 					iResult = EFalse;
   166 					}
   167 				}
   168 			else
   169 				{
   170 				iResult = EFalse;
   171 				}
   172             
   173             iFinished = ETrue;
   174 			}
   175 			break;
   176 		}
   177 	}
   178 
   179 void CUnifiedCertStoreApplications::PerformCancel()
   180 	{
   181 	switch (iState)
   182 		{
   183 		case EGettingApplications:
   184 			CertStore().CancelList();
   185 			break;
   186 
   187 		case EFinished:
   188 			CertStore().CancelApplications();
   189 			break;
   190 
   191 		default:
   192 			break;
   193 		}
   194 	}
   195 
   196 void CUnifiedCertStoreApplications::Reset()
   197 	{
   198     iCertInfos.Close();
   199     iState = EGetCert;
   200 	}
   201 
   202 void CUnifiedCertStoreApplications::DoReportAction()
   203 	{
   204 	iOut.writeString(_L("Getting applications..."));
   205 	iOut.writeNewLine();
   206 	iOut.writeString(_L("\tExpected applications :"));
   207 	TInt iEnd = iExpectedApplications.Count();
   208 	for (TInt i = 0; i < iEnd; i++)
   209 		{
   210 		iOut.writeString(_L(" "));
   211 		iOut.writeNum(iExpectedApplications[i].iUid);
   212 		}
   213 	iOut.writeNewLine();
   214 	iOut.writeNewLine();
   215 	}	
   216 
   217 void CUnifiedCertStoreApplications::DoCheckResult(TInt /*aError*/)
   218 	{
   219 	if (iFinished)
   220 		{
   221 		iConsole.Printf(_L("\tApplications : "));
   222 		iOut.writeString(_L("\tApplications : "));
   223 		TInt count = iApplications.Count();
   224 		for (TInt i = 0; i < count; i++)
   225 			{
   226 			iConsole.Printf(_L("%D "), (iApplications)[i]);
   227 			iOut.writeNum((iApplications)[i].iUid);
   228 			iOut.writeString(_L(" "));
   229 			}
   230 		iOut.writeNewLine();
   231 		if (iResult)
   232 			{
   233 			iConsole.Printf(_L("\n\tApplications retrieved successfully\n"));
   234 			iOut.writeString(_L("\tApplications retrieved successfully"));
   235 			iOut.writeNewLine();
   236 			}
   237 		else
   238 			{
   239 			iConsole.Printf(_L("\n\tApplications retrieved failed\n"));
   240 			iOut.writeString(_L("\tApplications retrieved failed"));
   241 			iOut.writeNewLine();
   242 			}
   243 		iOut.writeNewLine();
   244 		}
   245 	}