cryptoservices/certificateandkeymgmt/twtlscert/SyntaxTest.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 1998-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 "SyntaxTest.h"
       
    20 #include "wtlscert.h"
       
    21 #include "t_input.h"
       
    22 
       
    23 TInt CSyntaxTest::nInstances = 0;
       
    24 _LIT(KSyntaxLogFile, "WTLSCERTSyntaxLog.txt");
       
    25 _LIT(KPathStart, "<path>");
       
    26 
       
    27 //////////////////////////////////////////////////////////////////////
       
    28 // Construction/Destruction
       
    29 //////////////////////////////////////////////////////////////////////
       
    30 
       
    31 CTestAction* CSyntaxTest::NewL(RFs& aFs, CConsoleBase& aConsole, 
       
    32 		Output& aOut, const TTestActionSpec& aTestActionSpec)
       
    33 	{
       
    34 	CTestAction* self = CSyntaxTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
       
    35 	CleanupStack::Pop(self);
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 CTestAction* CSyntaxTest::NewLC(RFs& aFs, CConsoleBase& aConsole, 
       
    40 		Output& aOut, const TTestActionSpec& aTestActionSpec)
       
    41 	{
       
    42 	CSyntaxTest* self = new(ELeave) CSyntaxTest(aFs, aConsole, aOut);
       
    43 	CleanupStack::PushL(self);
       
    44 	self->ConstructL(aTestActionSpec);
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 CSyntaxTest::CSyntaxTest(RFs& aFs, 
       
    49 								 CConsoleBase& aConsole,
       
    50 								 Output& aOut)
       
    51 : CTestAction(aConsole, aOut), iFs(aFs)
       
    52 	{
       
    53 	nFileNumber = 0;
       
    54 	}
       
    55 
       
    56 CSyntaxTest::~CSyntaxTest(void)
       
    57 	{
       
    58 	delete iDirList;
       
    59 	delete iWriter;
       
    60 	delete iSyntaxOut;
       
    61 	iLogFile.Close();
       
    62 	};
       
    63 
       
    64 void CSyntaxTest::ConstructL(const TTestActionSpec& aTestActionSpec)
       
    65 	{
       
    66 	CTestAction::ConstructL(aTestActionSpec);
       
    67 	if(nInstances==0)
       
    68 		{
       
    69 		nInstances++;
       
    70 		HBufC* body = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
       
    71 
       
    72 		body->Des().Copy(aTestActionSpec.iActionBody);
       
    73 		TPtrC chainBuf = Input::ParseElement(*body, KPathStart);
       
    74 		iPath.Copy(chainBuf);
       
    75 		CleanupStack::PopAndDestroy();
       
    76 		User::LeaveIfError(iLogFile.Replace(iFs,KSyntaxLogFile,EFileWrite));
       
    77 		iSyntaxOut = new(ELeave) FileOutput(iLogFile);
       
    78 		iWriter = new(ELeave) WTLSCertWriter(iSyntaxOut);
       
    79 		}
       
    80 	else
       
    81 		{
       
    82 		SetScriptError(ESyntax, _L("Only one syntax test can be run in each script"));
       
    83 		iFinished = ETrue;
       
    84 		}
       
    85 	}
       
    86 
       
    87 
       
    88 void CSyntaxTest::DoPerformPrerequisite(TRequestStatus& aStatus)
       
    89 	{
       
    90 	HBufC *searchPath = HBufC::NewLC(iPath.Size() + 1);
       
    91 	TPtr searchPathPtr(searchPath->Des());
       
    92 	searchPathPtr.Copy(iPath);
       
    93 	searchPathPtr.Append(_L("*"));
       
    94     TInt err = iFs.GetDir(searchPathPtr, KEntryAttMaskSupported, ESortByName, iDirList);
       
    95 	if (err != KErrNone)
       
    96 		{
       
    97 		iConsole.Printf(_L("Error getting directory "));
       
    98 		iConsole.Printf(searchPathPtr);
       
    99 		iConsole.Printf(_L("\n"));
       
   100 		iOut.writeString(_L("Error getting directory "));
       
   101 		iOut.writeString(searchPathPtr);
       
   102 		iOut.writeNewLine();
       
   103 		iFinished = ETrue;
       
   104 		TRequestStatus* status = &aStatus;
       
   105 		User::RequestComplete(status, KErrNone);
       
   106 		SetScriptError(EFileNotFound, searchPathPtr);
       
   107 		}
       
   108 	else
       
   109 		{
       
   110 		iConsole.Printf(_L("Please view "));
       
   111 		iConsole.Printf(KSyntaxLogFile);
       
   112 		iConsole.Printf(_L(" for results\n"));
       
   113 		iOut.writeString(_L("Please view "));
       
   114 		iOut.writeString(KSyntaxLogFile);
       
   115 		iOut.writeString(_L(" for results"));
       
   116 		iOut.writeNewLine();
       
   117 		iActionState = EAction;
       
   118 		TRequestStatus* status = &aStatus;
       
   119 		User::RequestComplete(status, KErrNone);
       
   120 		iResult = ETrue;
       
   121 		}
       
   122 	CleanupStack::PopAndDestroy();
       
   123 	}
       
   124 
       
   125 void CSyntaxTest::DoPerformPostrequisite(TRequestStatus& aStatus)
       
   126 	{
       
   127 	TRequestStatus* status = &aStatus;
       
   128 	iFinished = ETrue;
       
   129 	User::RequestComplete(status, KErrNone);
       
   130 	}
       
   131 
       
   132 void CSyntaxTest::ReadAndSyntaxCheckL(const TDesC &aFilename)
       
   133 	{
       
   134 	iSyntaxOut->writeString(aFilename);
       
   135 	iSyntaxOut->writeNewLine();
       
   136     HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
       
   137 	CWTLSCertificate* cert = CWTLSCertificate::NewLC(buf->Des());
       
   138 
       
   139 	iWriter->WriteCert(*cert);
       
   140 	CleanupStack::PopAndDestroy(2);//cert, buf
       
   141 	}
       
   142 
       
   143 void CSyntaxTest::PerformAction(TRequestStatus& aStatus)
       
   144 	{
       
   145 	TBuf<256> filename = (*iDirList)[nFileNumber].iName;
       
   146 
       
   147 	TRAPD(error, ReadAndSyntaxCheckL(filename));
       
   148 
       
   149 	if (error !=KErrNone)
       
   150 		{
       
   151 		iResult = EFalse;
       
   152 		if (error == KErrArgument)
       
   153 			{
       
   154 			iConsole.Printf(_L("certificate file %S wrongly encoded\n"),&filename);
       
   155 			iOut.writeString(filename);
       
   156 			iOut.writeString(_L(" wrongly encoded"));
       
   157 			iOut.writeNewLine();
       
   158 			}
       
   159 		if (error == KErrNotSupported)
       
   160 			{
       
   161 			
       
   162 			iConsole.Printf(_L("certificate file %S not supported\n"),&filename);
       
   163 			iOut.writeString(filename);
       
   164 			iOut.writeString(_L(" not supported"));
       
   165 			iOut.writeNewLine();
       
   166 			}
       
   167 		else
       
   168 			{
       
   169 			iConsole.Printf(_L("err=%d\n"), error);
       
   170 			}
       
   171 		}
       
   172 	else
       
   173 		{
       
   174 		iSyntaxOut->writeNewLine();
       
   175 		iConsole.Printf(_L("certificate %S parsed successfully\n"), &filename);
       
   176 		}
       
   177 
       
   178 	nFileNumber++;
       
   179 	if(nFileNumber == iDirList->Count())
       
   180 		{
       
   181 		iActionState = EPostrequisite;
       
   182 		};
       
   183 	TRequestStatus* status = &aStatus;
       
   184 	User::RequestComplete(status, KErrNone);
       
   185 	}
       
   186 
       
   187 void CSyntaxTest::DoReportAction()
       
   188 	{
       
   189 	iConsole.Printf(_L("\n"));
       
   190 	}
       
   191 
       
   192 void CSyntaxTest::DoCheckResult(TInt /*aError*/)
       
   193 	{
       
   194 	}