os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_compare.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 /**
    20  @file
    21 */
    22   
    23 #include <e32base.h>
    24 #include "t_keystore_actions.h"
    25 #include "t_keystore_defs.h"
    26 #include "t_input.h"
    27 
    28 const int KErrDifferent = -99999;
    29 
    30 /////////////////////////////////////////////////////////////////////////////////
    31 // CCompare
    32 /////////////////////////////////////////////////////////////////////////////////
    33 
    34 CTestAction* CCompare::NewL(RFs& aFs, 
    35 							CConsoleBase& aConsole, 
    36 							Output& aOut,
    37 							const TTestActionSpec& aTestActionSpec)
    38 {
    39 	CTestAction* self = CCompare::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    40 	CleanupStack::Pop(self);
    41 	return self;
    42 }
    43 
    44 CTestAction* CCompare::NewLC(RFs& aFs,
    45 							CConsoleBase& aConsole, 
    46 							Output& aOut,
    47 							const TTestActionSpec& aTestActionSpec)
    48 {
    49 	CCompare* self = new (ELeave) CCompare(aFs, aConsole, aOut);
    50 	CleanupStack::PushL(self);
    51 	self->ConstructL(aTestActionSpec);
    52 	return self;
    53 }
    54 
    55 CCompare::~CCompare()
    56 {
    57 }
    58 
    59 CCompare::CCompare(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
    60 :	CKeyStoreTestAction(aFs, aConsole, aOut)
    61 {
    62  	iState = EComparing;
    63 }
    64 
    65 
    66 void CCompare::ConstructL(const TTestActionSpec& aTestActionSpec)
    67 {
    68 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
    69 
    70 	TInt err = KErrNone;
    71 	TInt pos = 0;
    72 
    73 	TDriveUnit sysDrive (RFs::GetSystemDrive());
    74 	TBuf<128> ramDrive (sysDrive.Name());
    75 	ramDrive.Append(_L("\\tkeystore\\data\\"));
    76 	
    77 	iOriginalFile = ramDrive;
    78 	iNewFile = ramDrive;
    79 	
    80 	TFileName buf;
    81 	buf.FillZ();
    82 
    83 	buf.Copy(Input::ParseElement(aTestActionSpec.iActionBody, 
    84 							KOriginalFile, 
    85 							KOriginalFileEnd, pos, err));	
    86 //	Now the filename itself
    87 	iOriginalFile.Append(buf);
    88 
    89 	buf.Copy(Input::ParseElement(aTestActionSpec.iActionBody, 
    90 							KNewFile, 
    91 							KNewFileEnd, pos, err));
    92 //	Now the filename itself
    93 	iNewFile.Append(buf);
    94 
    95 //	iExpectedResult = SetExpectedResultL(Input::ParseElement(aTestActionSpec.iActionResult, 
    96 //															KReturnStart, 
    97 //															KReturnEnd, pos, err));
    98 }
    99 
   100 
   101 void CCompare::PerformAction(TRequestStatus& aStatus)
   102 	{
   103 	switch (iState)
   104 		{
   105 	case EComparing:
   106 			{
   107 			TInt res = KErrNone;
   108 			TRAPD(err, res  = CompareFilesL());
   109 			if (KErrNone != err)
   110 				{
   111 				res = err;
   112 				}
   113 			iState = EFinished;
   114 			TRequestStatus *status = &aStatus;
   115 			User::RequestComplete(status, res);			
   116 			}
   117 			break;
   118 	case EFinished:
   119 			{
   120 			TRequestStatus* status = &aStatus;
   121 			User::RequestComplete(status, aStatus.Int());
   122 			if (aStatus == iExpectedResult)
   123 				{
   124 				iResult = ETrue;
   125 				}
   126 			else
   127 				{
   128 				iResult = EFalse;
   129 				}
   130 			iFinished = ETrue;
   131 			}
   132 			break;
   133 		}
   134 	}
   135 
   136 
   137 void CCompare::PerformCancel()
   138     {
   139     // nothing to cancel
   140     }
   141  
   142 
   143 void CCompare::Reset()
   144 {}
   145 
   146 
   147 void CCompare::DoReportAction()
   148 {
   149 	_LIT(KComparing, "Comparison in progress...");
   150 	iOut.writeString(KComparing);
   151 	TPtr theLabel(iLabel->Des());
   152 	iOut.writeString(theLabel);
   153 	iOut.writeNewLine();
   154 }
   155 
   156 // Bitwise comparison of iOriginalFile and iNewFile
   157 TInt CCompare::CompareFilesL()
   158 	{
   159 		TInt res = KErrNone;
   160 
   161 		RFs theFs;
   162 		CleanupClosePushL(theFs);
   163 		User::LeaveIfError(theFs.Connect());
   164 			
   165 		RFile original;
   166 		TInt r = original.Open(theFs, iOriginalFile, EFileRead);
   167 		User::LeaveIfError(r);
   168 		CleanupClosePushL(original);
   169 
   170 		RFile generated;
   171 		r = generated.Open(theFs, iNewFile, EFileRead);
   172 		User::LeaveIfError(r);
   173 		CleanupClosePushL(generated);
   174 		
   175 		TInt pos1 = 0;
   176 		TInt pos2 = 0;
   177 		
   178 		TBuf8<1> ptr1;
   179 		TBuf8<1> ptr2;
   180 
   181 		original.Read(pos1, ptr1);
   182 		generated.Read(pos2, ptr2);
   183 		
   184 		while (ptr1.Length()!=0)
   185 			{
   186 			if (ptr1.Compare(ptr2) != 0)
   187 				{
   188 				res = KErrDifferent;				
   189 				pos = pos1;
   190 				break;
   191 				}
   192 			pos1++;
   193 			pos2++;
   194 			original.Read(pos1, ptr1);
   195 			generated.Read(pos2, ptr2);				
   196 			}
   197 		if ((res != KErrDifferent)&& (ptr2.Length()!=0))
   198 			{
   199 			 res = KErrDifferent;			
   200 			pos = pos1;
   201 			}
   202 			
   203 		CleanupStack::PopAndDestroy(3, &theFs);
   204 		
   205 		return res;
   206 	}
   207 
   208 void CCompare::DoCheckResult(TInt aError)
   209 {
   210 	if (iFinished)
   211 	{
   212 		if (aError == KErrNone)
   213 		{
   214 			_LIT(KSuccessful, "Key comparison successfully\n");
   215 			iConsole.Write(KSuccessful);
   216 			iOut.writeString(KSuccessful);
   217 			iOut.writeNewLine();
   218 			iOut.writeNewLine();
   219 		}
   220 		else
   221 		{
   222 			TBuf<256> msg;
   223 			_LIT(KFailedDetails, "First difference at offset %d  \n");			
   224 			msg.Format(KFailedDetails, pos);
   225 			if (aError!=iExpectedResult)
   226 			{
   227 				_LIT(KFailed, "!!!Key comparison failure!!!  Import or export problem...\n");
   228 				iConsole.Write(KFailed);
   229 				iOut.writeString(KFailed);
   230 				iOut.writeNewLine();				
   231 				iConsole.Write(msg);
   232 				iOut.writeString(msg);
   233 			}
   234 			else
   235 			{
   236 				_LIT(KFailed, "Key comparison failed, but expected\n");
   237 				iConsole.Write(KFailed);
   238 				iOut.writeString(KFailed);
   239 				iOut.writeNewLine();				
   240 				iConsole.Write(msg);
   241 				iOut.writeString(msg);
   242 			}
   243 
   244 			iOut.writeNewLine();
   245 			iOut.writeNewLine();
   246 		}
   247 	}
   248 }