os/security/cryptoservices/certificateandkeymgmt/tcertstore/t_certstoreactionsclient.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) 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_certstoreactionsclient.h"
    24 #include "t_certstoredefs.h"
    25 #include "t_certstoreout.h"
    26 #include "t_input.h"
    27 
    28 CTestAction* CInitCertificateAppInfoManager::NewL(RFs &aFs, 
    29 												  CConsoleBase& aConsole, 
    30 												  Output& aOut,
    31 												  const TTestActionSpec& aTestActionSpec)
    32 	{
    33 	CTestAction* self = NewLC(aFs, aConsole, aOut, aTestActionSpec);
    34 	CleanupStack::Pop(self);
    35 	return self;
    36 	}
    37 	
    38 CTestAction* CInitCertificateAppInfoManager::NewLC(RFs &aFs, 
    39 												   CConsoleBase& aConsole, 
    40 												   Output& aOut,
    41 												   const TTestActionSpec& aTestActionSpec)	
    42 	{
    43 	CInitCertificateAppInfoManager* self = 
    44 		new(ELeave) CInitCertificateAppInfoManager(aFs, aConsole, aOut);
    45 	CleanupStack::PushL(self);
    46 	self->ConstructL(aTestActionSpec);
    47 	return self;
    48 	}
    49 
    50 CInitCertificateAppInfoManager::~CInitCertificateAppInfoManager()	
    51 	{
    52 	}
    53 
    54 void CInitCertificateAppInfoManager::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 		if (aStatus == KErrNoMemory)
    79 			{	
    80 			iState = EInit;
    81 			}
    82 		else
    83 			{
    84 			iFinished = ETrue;
    85 			}
    86 		}
    87 		break;
    88 		}
    89 	}
    90 
    91 void CInitCertificateAppInfoManager::PerformCancel()
    92 	{
    93 	}
    94 
    95 void CInitCertificateAppInfoManager::Reset()
    96 	{
    97 	__ASSERT_DEBUG(EFalse, User::Panic(_L("CInitCertificateAppInfoManager::Reset()"), 1));
    98 	}
    99 
   100 CInitCertificateAppInfoManager::CInitCertificateAppInfoManager(RFs &aFs, 
   101 															   CConsoleBase& aConsole,
   102 															   Output& aOut)
   103 
   104 : CCertStoreTestAction(aFs, aConsole, aOut), iState(EInit), iFs(aFs)
   105 	{
   106 	}
   107 
   108 void CInitCertificateAppInfoManager::ConstructL(const TTestActionSpec& aTestActionSpec)
   109 	{
   110 	CTestAction::ConstructL(aTestActionSpec);
   111 	
   112 	// Set expected result
   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 CInitCertificateAppInfoManager::HandleEInit()
   121 	{
   122 	iState = EFinished;
   123 	InitTheCertificateAppInfoManagerL();	
   124 	}
   125 
   126 void CInitCertificateAppInfoManager::DoReportAction()
   127 	{
   128 	iOut.writeString(_L("Creating CertificateAppInfoManager..."));
   129 	iOut.writeNewLine();
   130 	}
   131 
   132 void CInitCertificateAppInfoManager::DoCheckResult(TInt aError)
   133 	{
   134 	if (iFinished)
   135 		{
   136 		if (aError == KErrNone)
   137 			{
   138 			iConsole.Write(_L("\tcertificate app info manager created successfully\n"));
   139 			iOut.writeString(_L("\tcertificate app info manager created successfully"));
   140 			iOut.writeNewLine();
   141 			iOut.writeNewLine();
   142 			}
   143 		else
   144 			{
   145 			iConsole.Write(_L("\tcertificate app info manager creation failed\n"));
   146 			iOut.writeString(_L("\tcertificate app info manager creation failed"));
   147 			iOut.writeNewLine();
   148 			iOut.writeNewLine();
   149 			}
   150 		}
   151 	}
   152 
   153 CTestAction* CDeleteCertificateAppInfoManager::NewL(RFs& aFs,
   154 													CConsoleBase& aConsole, 
   155 													Output& aOut,
   156 													const TTestActionSpec& aTestActionSpec)
   157 	{
   158 	CDeleteCertificateAppInfoManager* self = 
   159 		new(ELeave) CDeleteCertificateAppInfoManager(aFs, aConsole, aOut);
   160 	CleanupStack::PushL(self);
   161 	self->ConstructL(aTestActionSpec);
   162 	CleanupStack::Pop(self);
   163 	return self;
   164 	}
   165 
   166 CDeleteCertificateAppInfoManager::~CDeleteCertificateAppInfoManager()
   167 	{
   168 	}
   169 
   170 CDeleteCertificateAppInfoManager::CDeleteCertificateAppInfoManager(RFs& aFs, CConsoleBase& aConsole,
   171 																   Output& aOut)
   172 : CCertStoreTestAction(aFs, aConsole, aOut), iState(EDelete)
   173 	{
   174 	}
   175 
   176 void CDeleteCertificateAppInfoManager::ConstructL(const TTestActionSpec& aTestActionSpec)
   177 	{
   178 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   179 	
   180 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   181 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   182 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   183 	CleanupStack::PopAndDestroy(result);
   184 	}
   185 
   186 void CDeleteCertificateAppInfoManager::PerformAction(TRequestStatus& aStatus)
   187 	{
   188 	switch (iState)
   189 		{
   190 		case EDelete:
   191 			{
   192 			DeleteTheCertificateAppInfoManager();
   193 			iState = EFinished;
   194 			TRequestStatus* status = &aStatus;
   195 			User::RequestComplete(status, KErrNone);
   196 			}
   197 			break;
   198 
   199 		case EFinished:
   200 			{
   201 			TRequestStatus* status = &aStatus;
   202 			User::RequestComplete(status, aStatus.Int());
   203 			if (aStatus == iExpectedResult)
   204 				{
   205 				iResult = ETrue;
   206 				}
   207 			else
   208 				{
   209 				iResult = EFalse;
   210 				}
   211 			if (aStatus == KErrNoMemory)
   212 				{	
   213 				iState = EDelete;
   214 				}
   215 			else
   216 				{
   217 				iFinished = ETrue;
   218 				}
   219 			}
   220 			break;
   221 		}
   222 	}
   223 
   224 void CDeleteCertificateAppInfoManager::PerformCancel()
   225 	{
   226 	}
   227 
   228 void CDeleteCertificateAppInfoManager::Reset()
   229 	{
   230 	__ASSERT_DEBUG(EFalse, User::Panic(_L("CDeleteCertificateAppInfoManager::Reset()"), 1));
   231 	}
   232 
   233 void CDeleteCertificateAppInfoManager::DoReportAction()
   234 	{
   235 	iOut.writeString(_L("Deleting CertificateAppInfoManager..."));
   236 	iOut.writeNewLine();
   237 	}
   238 
   239 void CDeleteCertificateAppInfoManager::DoCheckResult(TInt aError)
   240 	{
   241 	if (iFinished)
   242 		{
   243 		if (aError == KErrNone)
   244 			{
   245 			iConsole.Write(_L("\tcertificate app info manager deleted successfully\n"));
   246 			iOut.writeString(_L("\tcertificate app info manager deleted successfully"));
   247 			iOut.writeNewLine();
   248 			iOut.writeNewLine();
   249 			}
   250 		else
   251 			{
   252 			iConsole.Write(_L("\tcertificate app info manager deletion failed\n"));
   253 			iOut.writeString(_L("\tcertificate app info manager deletion failed"));
   254 			iOut.writeNewLine();
   255 			iOut.writeNewLine();
   256 			}
   257 		}
   258 	}
   259 
   260 CTestAction* CAddClient::NewL(RFs& aFs,
   261 							  CConsoleBase& aConsole, 
   262 							  Output& aOut,
   263 							  const TTestActionSpec& aTestActionSpec)
   264 	{
   265 	CAddClient* self = new(ELeave) CAddClient(aFs, aConsole, aOut);
   266 	CleanupStack::PushL(self);
   267 	self->ConstructL(aTestActionSpec);
   268 	CleanupStack::Pop(self);
   269 	return self;
   270 	}
   271 
   272 CAddClient::~CAddClient()
   273 	{
   274 	delete iClientInfo;
   275 	}
   276 
   277 void CAddClient::PerformAction(TRequestStatus& aStatus)
   278 	{
   279 	switch (iState)
   280 		{
   281 		case EAdd:
   282 			{
   283 			TInt err = KErrNotReady;
   284 			CCertificateAppInfoManager* appManager = NULL;
   285 			appManager = TheCertificateAppInfoManager();
   286 			if (appManager)
   287 			{
   288 				TRAP(err, appManager->AddL(*iClientInfo));
   289 			}
   290 			
   291 			TRequestStatus* status = &aStatus;
   292 			User::RequestComplete(status, err);
   293 			iState = EFinished;
   294 			}
   295 			break;
   296 
   297 		case EFinished:
   298 			{
   299 			TRequestStatus* status = &aStatus;
   300 			User::RequestComplete(status, aStatus.Int());
   301 			if ( (aStatus == iExpectedResult) || (aStatus == KErrAlreadyExists) )
   302 				{
   303 				iResult = ETrue;
   304 				}
   305 			else
   306 				{
   307 				iResult = EFalse;
   308 				}
   309 			if (aStatus == KErrNoMemory)
   310 				{	
   311 				iState = EAdd;
   312 				}
   313 			else
   314 				{
   315 				iFinished = ETrue;
   316 				}
   317 			}
   318 			break;
   319 		}
   320 	}
   321 
   322 void CAddClient::PerformCancel()
   323 	{
   324 	}
   325 
   326 void CAddClient::Reset()
   327 	{
   328 	__ASSERT_DEBUG(EFalse, User::Panic(_L("CAddClient::Reset()"), 1));
   329 	}
   330 
   331 CAddClient::CAddClient(RFs& aFs, CConsoleBase& aConsole, 
   332 					   Output& aOut)
   333 : CCertStoreTestAction(aFs, aConsole, aOut), iState(EAdd)
   334 	{
   335 	}
   336 
   337 void CAddClient::ConstructL(const TTestActionSpec& aTestActionSpec)
   338 	{
   339 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   340 	TInt err = KErrNone;
   341 	TInt pos = 0;
   342 	const TDesC8& name = Input::ParseElement(aTestActionSpec.iActionBody, KNameStart, KNameEnd,
   343 		pos, err);
   344 	const TDesC8& uid = Input::ParseElement(aTestActionSpec.iActionBody, KUIDStart, KUIDEnd,
   345 		pos, err);
   346 	TLex8 lex(uid);
   347 	TUid u;
   348 	lex.Val(u.iUid);
   349 	TName n;
   350 	n.Copy(name);
   351 	iClientInfo = new(ELeave) TCertificateAppInfo(u, n);
   352 
   353 	// Set expected result
   354 	pos = 0;
   355 	
   356 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   357 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   358 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   359 	CleanupStack::PopAndDestroy(result);
   360 	}
   361 
   362 void CAddClient::DoReportAction()
   363 	{
   364 	iOut.writeString(_L("Adding client..."));
   365 	iOut.writeNewLine();
   366 	}
   367 
   368 void CAddClient::DoCheckResult(TInt aError)
   369 	{
   370 	if (iFinished)
   371 		{
   372 		if (aError == KErrNone)
   373 			{
   374 			iConsole.Write(_L("\tClient added successfully\n"));
   375 			iOut.writeString(_L("\tClient added successfully"));
   376 			iOut.writeNewLine();
   377 			iOut.writeNewLine();
   378 			}
   379 		else
   380 			{
   381 			iConsole.Write(_L("\tClient added successfully\n"));
   382 			iOut.writeString(_L("\tClient added successfully"));
   383 			iOut.writeNewLine();
   384 			iOut.writeNewLine();
   385 			}
   386 		}
   387 	}
   388 
   389 CTestAction* CGetClients::NewL(RFs& aFs,
   390 							   CConsoleBase& aConsole, 
   391 							   Output& aOut,
   392 							   const TTestActionSpec& aTestActionSpec)
   393 	{
   394 	CGetClients* self = new(ELeave) CGetClients(aFs, aConsole, aOut);
   395 	CleanupStack::PushL(self);
   396 	self->ConstructL(aTestActionSpec);
   397 	CleanupStack::Pop();
   398 	return self;
   399 	}
   400 
   401 CGetClients::~CGetClients()
   402 	{
   403 	// We own iExpectedClients but not iClients
   404 	iExpectedClients.Close();
   405 	}
   406 
   407 void CGetClients::PerformAction(TRequestStatus& aStatus)
   408 	{
   409 	switch (iState)
   410 		{
   411 		case EGet:
   412 			{
   413 			TRequestStatus* status = &aStatus;
   414 			CCertificateAppInfoManager* appManager = NULL;
   415 			appManager = TheCertificateAppInfoManager();
   416 			if (appManager)
   417 			{
   418 				iClients = &(appManager->Applications());
   419 				User::RequestComplete(status, KErrNone);
   420 			}
   421 			else
   422 				User::RequestComplete(status, KErrNotReady);			
   423 			
   424 			iState = EFinished;
   425 			}
   426 			break;
   427 
   428 		case EFinished:
   429 			{
   430 			TRequestStatus* status = &aStatus;
   431 			User::RequestComplete(status, aStatus.Int());
   432 			if (aStatus == iExpectedResult)
   433 				{
   434 				// Check that we have the expected uids
   435 				TInt iEnd = iClients->Count();
   436 				if (iEnd == iExpectedClients.Count())
   437 					{
   438 					TInt i = 0;
   439 					for (i = 0; i < iEnd; i++)
   440 						{
   441 						if ((*iClients)[i].Id() != iExpectedClients[i])
   442 							{
   443 							break;
   444 							}
   445 						}
   446 					if (i == iEnd)
   447 						{
   448 						iResult = ETrue;
   449 						}
   450 					else
   451 						{
   452 						iResult = EFalse;
   453 						}
   454 					}
   455 				else
   456 					{
   457 					iResult = EFalse;
   458 					}
   459 				}
   460 			else
   461 				{
   462 				iResult = EFalse;
   463 				}
   464 			if (aStatus == KErrNoMemory)
   465 				{	
   466 				iState = EGet;
   467 				}
   468 			else
   469 				{
   470 				iFinished = ETrue;
   471 				}
   472 			}
   473 			break;
   474 		}
   475 	}
   476 
   477 void CGetClients::PerformCancel()
   478 	{
   479 	}
   480 
   481 void CGetClients::Reset()
   482 	{
   483 	__ASSERT_DEBUG(EFalse, User::Panic(_L("CGetClients::Reset()"), 1));
   484 	}
   485 
   486 CGetClients::CGetClients(RFs& aFs, CConsoleBase& aConsole, 
   487 						 Output& aOut)
   488 	: CCertStoreTestAction(aFs, aConsole, aOut), 
   489 	  iState(EGet)
   490 	{
   491 	}
   492 
   493 void CGetClients::ConstructL(const TTestActionSpec& aTestActionSpec)
   494 	{
   495 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   496 	TInt err = KErrNone;
   497 	TInt pos = 0;
   498 	
   499 	// Set expected result
   500 	pos = 0;
   501 	
   502 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   503 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   504 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   505 	CleanupStack::PopAndDestroy(result);
   506 	
   507 	const TDesC8& uidsString = Input::ParseElement(aTestActionSpec.iActionResult, KUIDStart, KUIDEnd,
   508 												   pos, err);
   509 	TLex8 lex(uidsString);
   510 
   511 	err = KErrNone;
   512 	while (err == KErrNone)
   513 		{
   514 		TUid uid;
   515 		err = lex.Val(uid.iUid);
   516 		if (err == KErrNone)
   517 			{	
   518 			lex.SkipSpace();
   519 			User::LeaveIfError(iExpectedClients.Append(uid));
   520 			}
   521 		}
   522 	}
   523 
   524 void CGetClients::DoReportAction()
   525 	{
   526 	iOut.writeString(_L("Getting client..."));
   527 	iOut.writeNewLine();
   528 	iOut.writeString(_L("\tExpected clients :"));
   529 	TInt iEnd = iExpectedClients.Count();
   530 	for (TInt i = 0; i < iEnd; i++)
   531 		{
   532 		iOut.writeString(_L(" "));
   533 		iOut.writeNum(iExpectedClients[i].iUid);
   534 		}
   535 	iOut.writeNewLine();
   536 	iOut.writeNewLine();
   537 	}
   538 
   539 void CGetClients::DoCheckResult(TInt aError)
   540 	{
   541 	if (iFinished)
   542 		{
   543 		iConsole.Printf(_L("\tClients:\n"));
   544 		iOut.writeString(_L("\tClients: "));
   545 		iOut.writeNewLine();
   546 		iOut.writeString(_L("\t\t"));
   547 		TInt count = iClients->Count();
   548 		for (TInt i = 0; i < count; i++)
   549 			{
   550 			iConsole.Printf(_L("%D "), (*iClients)[i].Id());
   551 			iOut.writeNum((*iClients)[i].Id().iUid);
   552 			iOut.writeString(_L(" "));
   553 			}
   554 		iOut.writeNewLine();
   555 		if (aError == KErrNone)
   556 			{
   557 			iConsole.Printf(_L("\tClients retrieved successfully\n"));
   558 			iOut.writeString(_L("\tClients retrieved successfully"));
   559 			iOut.writeNewLine();
   560 			}
   561 		else
   562 			{
   563 			iConsole.Printf(_L("\tClients retrieved failed\n"));
   564 			iOut.writeString(_L("\tClients retrieved failed"));
   565 			iOut.writeNewLine();
   566 			}
   567 		iOut.writeNewLine();
   568 		}
   569 	}
   570 
   571 CTestAction* CRemoveClient::NewL(RFs& aFs,
   572 								 CConsoleBase& aConsole,
   573 								 Output& aOut,
   574 								 const TTestActionSpec& aTestActionSpec)
   575 	{
   576 	CRemoveClient* self = new(ELeave) CRemoveClient(aFs, aConsole, aOut);
   577 	CleanupStack::PushL(self);
   578 	self->ConstructL(aTestActionSpec);
   579 	CleanupStack::Pop(self);
   580 	return self;
   581 	}
   582 
   583 CRemoveClient::~CRemoveClient()
   584 	{
   585 	}
   586 
   587 void CRemoveClient::PerformAction(TRequestStatus& aStatus)
   588 	{
   589 	switch (iState)
   590 		{
   591 		case ERemove:
   592 			{
   593 			TInt err = KErrNotReady;
   594 			CCertificateAppInfoManager* appManager = NULL;
   595 			appManager = TheCertificateAppInfoManager();
   596 			if (appManager)
   597 			{
   598 				TRAP(err, appManager->RemoveL(iUid));
   599 			}
   600 			TRequestStatus* status = &aStatus;
   601 			User::RequestComplete(status, err);
   602 			iState = EFinished;
   603 			}
   604 			break;
   605 
   606 		case EFinished:
   607 			{
   608 			TRequestStatus* status = &aStatus;
   609 			User::RequestComplete(status, aStatus.Int());
   610 			if (aStatus == iExpectedResult)
   611 				{
   612 				iResult = ETrue;
   613 				}
   614 			else
   615 				{
   616 				iResult = EFalse;
   617 				}
   618 			if (aStatus == KErrNoMemory)
   619 				{	
   620 				iState = ERemove;
   621 				}
   622 			else
   623 				{
   624 				iFinished = ETrue;
   625 				}
   626 			}
   627 			break;
   628 		}
   629 	}
   630 
   631 void CRemoveClient::PerformCancel()
   632 	{
   633 	}
   634 
   635 void CRemoveClient::Reset()
   636 	{
   637 	__ASSERT_DEBUG(EFalse, User::Panic(_L("CRemoveClient::Reset()"), 1));
   638 	}
   639 
   640 CRemoveClient::CRemoveClient(RFs& aFs, CConsoleBase& aConsole, 
   641 							 Output& aOut)
   642 : CCertStoreTestAction(aFs, aConsole, aOut), iState(ERemove)
   643 	{
   644 	}
   645 
   646 void CRemoveClient::ConstructL(const TTestActionSpec& aTestActionSpec)
   647 	{
   648 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   649 	TInt err = KErrNone;
   650 	TInt pos = 0;
   651 	const TDesC8& uid = Input::ParseElement(aTestActionSpec.iActionBody, KUIDStart, KUIDEnd,
   652 		pos, err);
   653 	TLex8 lex(uid);
   654 	lex.Val(iUid.iUid);
   655 	
   656 	// Set expected result
   657 	pos = 0;
   658 	
   659 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   660 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   661 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   662 	CleanupStack::PopAndDestroy(result);
   663 	}
   664 
   665 void CRemoveClient::DoReportAction()
   666 	{
   667 	iOut.writeString(_L("Removing client..."));
   668 	iOut.writeNewLine();
   669 	}
   670 
   671 void CRemoveClient::DoCheckResult(TInt aError)
   672 	{
   673 	if (iFinished)
   674 		{
   675 		if (aError == KErrNone)
   676 			{
   677 			iConsole.Write(_L("\tClient removed successfully\n"));
   678 			iOut.writeString(_L("\tClient removed successfully"));
   679 			iOut.writeNewLine();
   680 			iOut.writeNewLine();
   681 			}
   682 		else if (aError == KErrNotFound)
   683 			{
   684 			iConsole.Write(_L("\tClient not found\n"));
   685 			iOut.writeString(_L("\tClient not found"));
   686 			iOut.writeNewLine();
   687 			iOut.writeNewLine();
   688 			}
   689 		}
   690 	}