os/security/cryptoservices/certificateandkeymgmt/twtlscert/SyntaxTest.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "SyntaxTest.h"
sl@0
    20
#include "wtlscert.h"
sl@0
    21
#include "t_input.h"
sl@0
    22
sl@0
    23
TInt CSyntaxTest::nInstances = 0;
sl@0
    24
_LIT(KSyntaxLogFile, "WTLSCERTSyntaxLog.txt");
sl@0
    25
_LIT(KPathStart, "<path>");
sl@0
    26
sl@0
    27
//////////////////////////////////////////////////////////////////////
sl@0
    28
// Construction/Destruction
sl@0
    29
//////////////////////////////////////////////////////////////////////
sl@0
    30
sl@0
    31
CTestAction* CSyntaxTest::NewL(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    32
		Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    33
	{
sl@0
    34
	CTestAction* self = CSyntaxTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0
    35
	CleanupStack::Pop(self);
sl@0
    36
	return self;
sl@0
    37
	}
sl@0
    38
sl@0
    39
CTestAction* CSyntaxTest::NewLC(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    40
		Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    41
	{
sl@0
    42
	CSyntaxTest* self = new(ELeave) CSyntaxTest(aFs, aConsole, aOut);
sl@0
    43
	CleanupStack::PushL(self);
sl@0
    44
	self->ConstructL(aTestActionSpec);
sl@0
    45
	return self;
sl@0
    46
	}
sl@0
    47
sl@0
    48
CSyntaxTest::CSyntaxTest(RFs& aFs, 
sl@0
    49
								 CConsoleBase& aConsole,
sl@0
    50
								 Output& aOut)
sl@0
    51
: CTestAction(aConsole, aOut), iFs(aFs)
sl@0
    52
	{
sl@0
    53
	nFileNumber = 0;
sl@0
    54
	}
sl@0
    55
sl@0
    56
CSyntaxTest::~CSyntaxTest(void)
sl@0
    57
	{
sl@0
    58
	delete iDirList;
sl@0
    59
	delete iWriter;
sl@0
    60
	delete iSyntaxOut;
sl@0
    61
	iLogFile.Close();
sl@0
    62
	};
sl@0
    63
sl@0
    64
void CSyntaxTest::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    65
	{
sl@0
    66
	CTestAction::ConstructL(aTestActionSpec);
sl@0
    67
	if(nInstances==0)
sl@0
    68
		{
sl@0
    69
		nInstances++;
sl@0
    70
		HBufC* body = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
sl@0
    71
sl@0
    72
		body->Des().Copy(aTestActionSpec.iActionBody);
sl@0
    73
		TPtrC chainBuf = Input::ParseElement(*body, KPathStart);
sl@0
    74
		iPath.Copy(chainBuf);
sl@0
    75
		CleanupStack::PopAndDestroy();
sl@0
    76
		User::LeaveIfError(iLogFile.Replace(iFs,KSyntaxLogFile,EFileWrite));
sl@0
    77
		iSyntaxOut = new(ELeave) FileOutput(iLogFile);
sl@0
    78
		iWriter = new(ELeave) WTLSCertWriter(iSyntaxOut);
sl@0
    79
		}
sl@0
    80
	else
sl@0
    81
		{
sl@0
    82
		SetScriptError(ESyntax, _L("Only one syntax test can be run in each script"));
sl@0
    83
		iFinished = ETrue;
sl@0
    84
		}
sl@0
    85
	}
sl@0
    86
sl@0
    87
sl@0
    88
void CSyntaxTest::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
    89
	{
sl@0
    90
	HBufC *searchPath = HBufC::NewLC(iPath.Size() + 1);
sl@0
    91
	TPtr searchPathPtr(searchPath->Des());
sl@0
    92
	searchPathPtr.Copy(iPath);
sl@0
    93
	searchPathPtr.Append(_L("*"));
sl@0
    94
    TInt err = iFs.GetDir(searchPathPtr, KEntryAttMaskSupported, ESortByName, iDirList);
sl@0
    95
	if (err != KErrNone)
sl@0
    96
		{
sl@0
    97
		iConsole.Printf(_L("Error getting directory "));
sl@0
    98
		iConsole.Printf(searchPathPtr);
sl@0
    99
		iConsole.Printf(_L("\n"));
sl@0
   100
		iOut.writeString(_L("Error getting directory "));
sl@0
   101
		iOut.writeString(searchPathPtr);
sl@0
   102
		iOut.writeNewLine();
sl@0
   103
		iFinished = ETrue;
sl@0
   104
		TRequestStatus* status = &aStatus;
sl@0
   105
		User::RequestComplete(status, KErrNone);
sl@0
   106
		SetScriptError(EFileNotFound, searchPathPtr);
sl@0
   107
		}
sl@0
   108
	else
sl@0
   109
		{
sl@0
   110
		iConsole.Printf(_L("Please view "));
sl@0
   111
		iConsole.Printf(KSyntaxLogFile);
sl@0
   112
		iConsole.Printf(_L(" for results\n"));
sl@0
   113
		iOut.writeString(_L("Please view "));
sl@0
   114
		iOut.writeString(KSyntaxLogFile);
sl@0
   115
		iOut.writeString(_L(" for results"));
sl@0
   116
		iOut.writeNewLine();
sl@0
   117
		iActionState = EAction;
sl@0
   118
		TRequestStatus* status = &aStatus;
sl@0
   119
		User::RequestComplete(status, KErrNone);
sl@0
   120
		iResult = ETrue;
sl@0
   121
		}
sl@0
   122
	CleanupStack::PopAndDestroy();
sl@0
   123
	}
sl@0
   124
sl@0
   125
void CSyntaxTest::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
   126
	{
sl@0
   127
	TRequestStatus* status = &aStatus;
sl@0
   128
	iFinished = ETrue;
sl@0
   129
	User::RequestComplete(status, KErrNone);
sl@0
   130
	}
sl@0
   131
sl@0
   132
void CSyntaxTest::ReadAndSyntaxCheckL(const TDesC &aFilename)
sl@0
   133
	{
sl@0
   134
	iSyntaxOut->writeString(aFilename);
sl@0
   135
	iSyntaxOut->writeNewLine();
sl@0
   136
    HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
sl@0
   137
	CWTLSCertificate* cert = CWTLSCertificate::NewLC(buf->Des());
sl@0
   138
sl@0
   139
	iWriter->WriteCert(*cert);
sl@0
   140
	CleanupStack::PopAndDestroy(2);//cert, buf
sl@0
   141
	}
sl@0
   142
sl@0
   143
void CSyntaxTest::PerformAction(TRequestStatus& aStatus)
sl@0
   144
	{
sl@0
   145
	TBuf<256> filename = (*iDirList)[nFileNumber].iName;
sl@0
   146
sl@0
   147
	TRAPD(error, ReadAndSyntaxCheckL(filename));
sl@0
   148
sl@0
   149
	if (error !=KErrNone)
sl@0
   150
		{
sl@0
   151
		iResult = EFalse;
sl@0
   152
		if (error == KErrArgument)
sl@0
   153
			{
sl@0
   154
			iConsole.Printf(_L("certificate file %S wrongly encoded\n"),&filename);
sl@0
   155
			iOut.writeString(filename);
sl@0
   156
			iOut.writeString(_L(" wrongly encoded"));
sl@0
   157
			iOut.writeNewLine();
sl@0
   158
			}
sl@0
   159
		if (error == KErrNotSupported)
sl@0
   160
			{
sl@0
   161
			
sl@0
   162
			iConsole.Printf(_L("certificate file %S not supported\n"),&filename);
sl@0
   163
			iOut.writeString(filename);
sl@0
   164
			iOut.writeString(_L(" not supported"));
sl@0
   165
			iOut.writeNewLine();
sl@0
   166
			}
sl@0
   167
		else
sl@0
   168
			{
sl@0
   169
			iConsole.Printf(_L("err=%d\n"), error);
sl@0
   170
			}
sl@0
   171
		}
sl@0
   172
	else
sl@0
   173
		{
sl@0
   174
		iSyntaxOut->writeNewLine();
sl@0
   175
		iConsole.Printf(_L("certificate %S parsed successfully\n"), &filename);
sl@0
   176
		}
sl@0
   177
sl@0
   178
	nFileNumber++;
sl@0
   179
	if(nFileNumber == iDirList->Count())
sl@0
   180
		{
sl@0
   181
		iActionState = EPostrequisite;
sl@0
   182
		};
sl@0
   183
	TRequestStatus* status = &aStatus;
sl@0
   184
	User::RequestComplete(status, KErrNone);
sl@0
   185
	}
sl@0
   186
sl@0
   187
void CSyntaxTest::DoReportAction()
sl@0
   188
	{
sl@0
   189
	iConsole.Printf(_L("\n"));
sl@0
   190
	}
sl@0
   191
sl@0
   192
void CSyntaxTest::DoCheckResult(TInt /*aError*/)
sl@0
   193
	{
sl@0
   194
	}
sl@0
   195