pkiutilities/ocsp/test/transport.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Test transport object.  May be used in place of the default transport used by
       
    15 // the ocsp module.  May later be extended to act as a dummy server producing
       
    16 // a variety of test responses.
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "transport.h"
       
    21 
       
    22 #include <f32file.h>
       
    23 
       
    24 CTOCSPTransport* CTOCSPTransport::NewL(const TDesC& aResponseFile, const TDesC* aRequestFile)
       
    25 	{
       
    26 	CTOCSPTransport* self = new (ELeave) CTOCSPTransport;
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL(aResponseFile, aRequestFile);
       
    29 	CleanupStack::Pop(self);
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 
       
    34 void CTOCSPTransport::ConstructL(const TDesC& aResponseFile, const TDesC* aRequestFile)
       
    35 	{
       
    36 	User::LeaveIfError(iFs.Connect());
       
    37 	User::LeaveIfError(iResponseStream.Open(iFs, aResponseFile, EFileShareReadersOnly));
       
    38 	iTransactions = iResponseStream.ReadUint32L();
       
    39 
       
    40 	if (aRequestFile)
       
    41 		{
       
    42 		iRequestStream = new (ELeave) RFileReadStream;
       
    43 		User::LeaveIfError(iRequestStream->Open(iFs, *aRequestFile, EFileShareReadersOnly));
       
    44 		TInt requests = iRequestStream->ReadUint32L();
       
    45 		__ASSERT_ALWAYS(requests == iTransactions, User::Leave(KErrCorrupt));	
       
    46 		} 		
       
    47 	}
       
    48 
       
    49 
       
    50 CTOCSPTransport::~CTOCSPTransport()
       
    51 	{
       
    52 	iResponseStream.Close();
       
    53 	
       
    54 	if (iRequestStream)
       
    55 		{
       
    56 		iRequestStream->Close();
       
    57 		delete iRequestStream;
       
    58 		}
       
    59 	
       
    60 	iFs.Close();
       
    61 	delete iResponseData;
       
    62 	}
       
    63 
       
    64 
       
    65 void CTOCSPTransport::SendRequest(const TDesC8& aURI,
       
    66 								  const TDesC8& aRequest, 
       
    67 								  const TInt,				// Timeout not used
       
    68 								  TRequestStatus& aStatus)
       
    69 	{
       
    70 	iCallBack = &aStatus;
       
    71 	aStatus = KRequestPending;
       
    72 
       
    73 	TRAPD(error, DoSendRequestL(aURI, aRequest));
       
    74 	User::RequestComplete(iCallBack, error);
       
    75 	}
       
    76 
       
    77 
       
    78 void CTOCSPTransport::DoSendRequestL(const TDesC8& aURI,
       
    79 									 const TDesC8& aRequest)
       
    80 	{	
       
    81 	__ASSERT_ALWAYS(iIndex++ < iTransactions, User::Leave(TOCSP::ETooManyTransactions));
       
    82 
       
    83 	CheckRequestL(aURI, aRequest);
       
    84 
       
    85 	delete iResponseData;
       
    86 	iResponseData = NULL;
       
    87 
       
    88 	iResponseData = ReadDataLC(iResponseStream);
       
    89 	CleanupStack::Pop(iResponseData);
       
    90 	}
       
    91 
       
    92 
       
    93 void CTOCSPTransport::CheckRequestL(const TDesC8& aURI,
       
    94 									const TDesC8& aRequest)
       
    95 	{
       
    96 	if (!iRequestStream)
       
    97 		{
       
    98 		return;
       
    99 		}
       
   100 
       
   101 	HBufC8* expectURI = ReadDataLC(*iRequestStream);
       
   102 	if (aURI != *expectURI)
       
   103 		{
       
   104 		User::Leave(TOCSP::EURIMismatch);
       
   105 		}
       
   106 	CleanupStack::PopAndDestroy(expectURI);
       
   107 	
       
   108 	HBufC8* expectRequest = ReadDataLC(*iRequestStream);
       
   109 	if (aRequest != *expectRequest)
       
   110 		{
       
   111 		User::Leave(TOCSP::ERequestMismatch);
       
   112 		}
       
   113 	CleanupStack::PopAndDestroy(expectRequest);
       
   114 	}
       
   115 
       
   116 HBufC8* CTOCSPTransport::ReadDataLC(RFileReadStream& aStream)
       
   117 	{
       
   118 	TUint32 len = aStream.ReadUint32L();
       
   119 	HBufC8* data = HBufC8::NewLC(len);
       
   120 	TPtr8 dataDes = data->Des();
       
   121 	aStream.ReadL(dataDes, len);
       
   122 	return data;
       
   123 	}
       
   124 
       
   125 void CTOCSPTransport::CancelRequest()
       
   126 	{
       
   127 	// If its not been done already, complete the active object
       
   128 	if (iCallBack)
       
   129 		{
       
   130 		User::RequestComplete(iCallBack, KErrCancel);
       
   131 		}
       
   132 	}
       
   133 
       
   134 
       
   135 TPtrC8 CTOCSPTransport::GetResponse() const
       
   136 	{
       
   137 	__ASSERT_ALWAYS(iResponseData, User::Panic(_L("TOCSP"), KErrNotReady));
       
   138 	return *iResponseData;
       
   139 	}