networksecurity/tls/ts_tls/TlsTestStep1.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-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 //
       
    15 
       
    16 // EPOC includes
       
    17 #include <e32base.h>
       
    18 
       
    19 #include <e32cons.h>
       
    20 #include <c32comm.h>
       
    21 #include <f32file.h>
       
    22 #include <es_sock.h>
       
    23 #include <securesocketinterface.h>
       
    24 #include <securesocket.h>
       
    25 
       
    26 #include "T_TLS_test.h"
       
    27 
       
    28 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    29 #include <ssl_internal.h>
       
    30 #endif
       
    31 
       
    32 
       
    33 // Test system includes
       
    34 #include <networking/log.h>
       
    35 #include <networking/teststep.h>
       
    36 
       
    37 #include "TestSuiteTls.h"
       
    38 #include "TlsTestStep1.h"
       
    39 
       
    40 #include "T_TLS_cntrl.h"
       
    41 
       
    42 
       
    43 #include "tlsconnection.h"
       
    44 
       
    45 GLREF_C void CommInitL();
       
    46 
       
    47 _LIT( KTxtTLS, "T_TLS" );
       
    48 
       
    49 CTestStepT_Tls::CTestStepT_Tls()
       
    50 /**
       
    51  * Constructor.
       
    52  * Store the name of this test case
       
    53  */
       
    54 {
       
    55 	iTestStepName = _L("t_tls");
       
    56 }
       
    57 
       
    58 // destructor
       
    59 CTestStepT_Tls::~CTestStepT_Tls()
       
    60 {
       
    61 }
       
    62 
       
    63 
       
    64 TVerdict CTestStepT_Tls::doTestStepL( )
       
    65 /**
       
    66  * This is the test code for t_tls.
       
    67  */
       
    68 {
       
    69 	// if the test has not left yet it must be a Pass 
       
    70 	iTestStepResult = EPass;
       
    71 
       
    72 	TRAPD(error,CommInitL()); // init needed comms libs
       
    73 	__ASSERT_ALWAYS(!error,User::Panic(KTxtTLS,error));
       
    74 
       
    75 	TRAP(error,TLSTestL()); // more initialization, then do example
       
    76 	__ASSERT_ALWAYS(!error,User::Panic(KTxtTLS,error));
       
    77 
       
    78 	return iTestStepResult;
       
    79 }
       
    80 
       
    81 
       
    82 // constructor
       
    83 CTlsRenegotiateTest::CTlsRenegotiateTest()
       
    84 {
       
    85 	// store the name of this test case
       
    86 	iTestStepName = _L("renegotiate");
       
    87 
       
    88 	iTestType = TLS_TEST_RENEGOTIATE;
       
    89 }
       
    90 
       
    91 TVerdict CTlsRenegotiateTest::doTestStepL( )
       
    92 {
       
    93 	return CTestStepT_Tls::doTestStepL(  );
       
    94 }
       
    95 
       
    96 // constructor
       
    97 CTlsCancelRecvTest::CTlsCancelRecvTest()
       
    98 /**
       
    99  * Store the name of this test case.
       
   100  */
       
   101 {
       
   102 	iTestStepName = _L("CancelRecv");
       
   103 
       
   104 	iTestType = TLS_TEST_CANCEL_RECV;
       
   105 }
       
   106 
       
   107 TVerdict CTlsCancelRecvTest::doTestStepL( )
       
   108 {
       
   109 	return CTestStepT_Tls::doTestStepL(  );
       
   110 }
       
   111 
       
   112 
       
   113 CTlsOldGetOptsTest::CTlsOldGetOptsTest()
       
   114 /**
       
   115  * Constructor.
       
   116  * Store the name of this test case.
       
   117  */
       
   118 {
       
   119 	iTestStepName = _L("oldgetOpts");
       
   120 
       
   121 	iTestType = TLS_TEST_OLD_GETOPTS;
       
   122 }
       
   123 
       
   124 TVerdict CTlsOldGetOptsTest::doTestStepL( )
       
   125 {
       
   126 	return CTestStepT_Tls::doTestStepL(  );
       
   127 }
       
   128 
       
   129 CTlsOpenConnection::CTlsOpenConnection()
       
   130 /**
       
   131  * Constructor.
       
   132  * Store the name of this test case.
       
   133  */
       
   134 {
       
   135 	iTestStepName = _L("OpenConnection");
       
   136 }
       
   137 
       
   138 TVerdict CTlsOpenConnection::doTestStepL( )
       
   139 {
       
   140 	_LIT(KSSLProtocol,"tls1.0");
       
   141 	TRequestStatus Status;
       
   142 
       
   143 	TRAPD(error,CommInitL()); // init needed comms libs
       
   144 	__ASSERT_ALWAYS(!error,User::Panic(KTxtTLS,error));
       
   145 
       
   146 	// Create an active scheduler
       
   147 	CActiveScheduler* myActiveScheduler;
       
   148 	myActiveScheduler = new(ELeave) CActiveScheduler();
       
   149 	CleanupStack::PushL( myActiveScheduler );
       
   150 	CActiveScheduler::Install( myActiveScheduler );
       
   151 
       
   152 	// get address and port number 
       
   153 	TPtrC addr = iAddress;
       
   154 	TESTL(GetStringFromConfig(KSectionName, KCfgIPAddress, addr));
       
   155 	iAddress.Copy( addr );
       
   156 
       
   157 	TESTL(GetIntFromConfig(KSectionName, KCfgIPPort, iPortNum));
       
   158 
       
   159 	// Connect the socket server
       
   160 	User::LeaveIfError( iTlsSuite->iSocketServer.Connect() );
       
   161 
       
   162 	// Open the socket
       
   163 	User::LeaveIfError( iInetAddr.Input( iAddress ));
       
   164 	iInetAddr.SetPort( iPortNum );
       
   165 
       
   166 	Log(_L("Connecting to %s:%d"), iAddress.PtrZ(), iPortNum );
       
   167 	User::LeaveIfError( iTlsSuite->iSocket.Open( iTlsSuite->iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );	
       
   168 
       
   169 	// connect the socket
       
   170 	iTlsSuite->iSocket.Connect( iInetAddr, Status );	
       
   171 	User::WaitForRequest(Status);
       
   172 
       
   173 	Log(_L("Connect result is %d"), Status.Int() );
       
   174 	TESTEL(Status==KErrNone, Status.Int());
       
   175 
       
   176 	User::LeaveIfNull(iTlsSuite->iSecureSocket = CSecureSocket::NewL( iTlsSuite->iSocket,KSSLProtocol()));
       
   177 
       
   178 	// Remove objects from the cleanup stack
       
   179 	CleanupStack::PopAndDestroy( 1 ); // myActiveScheduler, 
       
   180 
       
   181 	return EPass;
       
   182 }
       
   183 
       
   184 CTlsCloseConnection::CTlsCloseConnection()
       
   185 /**
       
   186  * Constructor.
       
   187  * Store the name of this test case.
       
   188  */
       
   189 {
       
   190 	iTestStepName = _L("CloseConnection");
       
   191 }
       
   192 
       
   193 TVerdict CTlsCloseConnection::doTestStepL( )
       
   194 {
       
   195 	Log(_L("Disconnecting"));
       
   196 	iTlsSuite->iSocket.Close() ;	
       
   197 	iTlsSuite->iSocketServer.Close();
       
   198 
       
   199 	delete iTlsSuite->iSecureSocket;
       
   200 
       
   201 	return EPass;
       
   202 }
       
   203 
       
   204 
       
   205 
       
   206 void CTestStepT_Tls::TLSTestL()
       
   207 	{
       
   208 	__UHEAP_MARK; 
       
   209 
       
   210 	CActiveScheduler* myActiveScheduler;
       
   211 	CController* myController;
       
   212 	
       
   213 	// Create an active scheduler
       
   214 	myActiveScheduler = new(ELeave) CActiveScheduler();
       
   215 	CleanupStack::PushL( myActiveScheduler );
       
   216 	CActiveScheduler::Install( myActiveScheduler );
       
   217 
       
   218 	// Create the controller active object
       
   219 	myController = CController::NewL();
       
   220 
       
   221 	// Initiate the controllers timer request
       
   222 	myController->Start( this );
       
   223 
       
   224 	// Start the scheduler
       
   225 	myActiveScheduler->Start();
       
   226 		
       
   227 	delete myController;
       
   228 
       
   229 	// Remove objects from the cleanup stack
       
   230 	CleanupStack::PopAndDestroy( 1 ); // myActiveScheduler, 
       
   231 	__UHEAP_MARKEND;
       
   232 
       
   233 	}
       
   234 
       
   235 
       
   236 CTlsFailSuiteSelection::CTlsFailSuiteSelection()
       
   237 /**
       
   238  * Constructor.
       
   239  * Store the name of this test case.
       
   240  */
       
   241 {
       
   242 	iTestStepName = _L("FailSuiteSelection");
       
   243 }
       
   244 
       
   245 TVerdict CTlsFailSuiteSelection::doTestStepL( )
       
   246 {
       
   247 	_LIT(KSSLProtocol,"tls1.0");
       
   248 	_LIT(KFSSectionName,"FailSuiteSelection");
       
   249 	TRequestStatus Status;
       
   250 	TVerdict result = EFail;
       
   251 
       
   252 	TRAPD(error,CommInitL()); // init needed comms libs
       
   253 	__ASSERT_ALWAYS(!error,User::Panic(KTxtTLS,error));
       
   254 
       
   255 	// Create an active scheduler
       
   256 	CActiveScheduler* myActiveScheduler;
       
   257 	myActiveScheduler = new(ELeave) CActiveScheduler();
       
   258 	CleanupStack::PushL( myActiveScheduler );
       
   259 	CActiveScheduler::Install( myActiveScheduler );
       
   260 
       
   261 	// get address, port number & cipher suite 
       
   262 	TPtrC addr = iAddress;
       
   263 	TESTL(GetStringFromConfig(KSectionName, KCfgIPAddress, addr));
       
   264 	iAddress.Copy( addr );
       
   265 	TESTL(GetIntFromConfig(KSectionName, KCfgIPPort, iPortNum));
       
   266 	TPtrC PtrResult;
       
   267 	TPtrC* res=&PtrResult;
       
   268 	TESTL(GetStringFromConfig(KFSSectionName, KCfgCipherSuites, PtrResult));
       
   269 	iCipherSuites.Copy( PtrResult );
       
   270 	Log( _L("CipherSuites: %S"), res);
       
   271 		
       
   272 	// Connect the socket server
       
   273 	User::LeaveIfError( iTlsSuite->iSocketServer.Connect() );
       
   274 
       
   275 	// configure address and port
       
   276 	User::LeaveIfError( iInetAddr.Input( iAddress ));
       
   277 	iInetAddr.SetPort( iPortNum );
       
   278 
       
   279 	// Open the socket
       
   280 	Log(_L("Connecting to %s:%d"), iAddress.PtrZ(), iPortNum );
       
   281 	User::LeaveIfError( iTlsSuite->iSocket.Open( iTlsSuite->iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );	
       
   282 
       
   283 	// connect the socket
       
   284 	iTlsSuite->iSocket.Connect( iInetAddr, Status );	
       
   285 	User::WaitForRequest(Status);
       
   286 	Log(_L("Connect result is %d"), Status.Int() );
       
   287 	TESTEL(Status==KErrNone, Status.Int());
       
   288 
       
   289 	// create a secureSocket
       
   290 	User::LeaveIfNull(iSecureSocket = CSecureSocket::NewL( iTlsSuite->iSocket,KSSLProtocol()));
       
   291 
       
   292 	// configure invalid cipher suite
       
   293 	TBuf8<KCipherBufSize>	cipherBuf;
       
   294 	TBuf8<3>	tempBuf;
       
   295 	TInt	i;
       
   296 	TLex8	myLex;
       
   297 	TInt	cCount = 0;			// used as an array index into the cipherBuf descriptor
       
   298 	TInt	ret;
       
   299 	TInt	value;
       
   300 
       
   301 	cipherBuf.SetLength( iCipherSuites.Length() );
       
   302 
       
   303 	for ( i=0; i<iCipherSuites.Length(); i+=2 )
       
   304 		{
       
   305 		// iCipherSuites contains a list of decimal values for each cipher suite that
       
   306 		// the client should offer to use. They are in a string format, so each decimal 
       
   307 		// value takes 2 bytes.
       
   308 		//
       
   309 		// Copy the 2 bytes of one value into a buffer so that it can be converted into
       
   310 		// a real decimal value;
       
   311 		tempBuf.SetLength( 2 );
       
   312 		tempBuf[0] = iCipherSuites[i];
       
   313 		tempBuf[1] = iCipherSuites[i+1];
       
   314 
       
   315 		myLex.Assign( tempBuf );
       
   316 		ret = myLex.Val( value );
       
   317 		if ( ret!=KErrNone )
       
   318 			{
       
   319 			break; // from for loop
       
   320 			}
       
   321 		Log( _L(":%X"), value );
       
   322 
       
   323 		// The actual cipher suite list that must be passed in the socket options
       
   324 		// is in a binary format of 0x0,0xCipherValue,0x0,CipherValue etc				
       
   325 		cipherBuf[ cCount++ ] = 0;
       
   326 		cipherBuf[ cCount++ ] = (unsigned char)value;
       
   327 
       
   328 	} // end of for loop
       
   329 
       
   330 	// Set the cipher suite(s) that the client will support
       
   331 	ret = iSecureSocket->SetAvailableCipherSuites( cipherBuf );
       
   332 
       
   333 	// this should fail with KErrNotSupported
       
   334 	if ( ret == KErrNotSupported )
       
   335 		{
       
   336 		result = EPass;
       
   337 		Log( _L("Test passes as because SetAvailableCipherSuites() returned KErrNotSupported") );
       
   338 		Log( _L("when setting cipher suites=%S"), res);
       
   339 		}
       
   340 	else
       
   341 		{
       
   342 		Log( _L("Test failed because SetAvailableCipherSuites() returned %d"),ret );
       
   343 		Log( _L("with invalid suite %S"), res);
       
   344 		result = EFail;
       
   345 		}
       
   346 
       
   347 	iTlsSuite->iSocket.Close() ;	
       
   348 	iTlsSuite->iSocketServer.Close();
       
   349 
       
   350 	// Remove objects from the cleanup stack
       
   351 	CleanupStack::PopAndDestroy( 1 ); // myActiveScheduler, 
       
   352 
       
   353 	delete iSecureSocket;
       
   354 
       
   355 	return result;
       
   356 }
       
   357 
       
   358 
       
   359 /**
       
   360 Constructor.
       
   361 Store the name of this test case
       
   362 */
       
   363 CTestStepDialogMode_Tls::CTestStepDialogMode_Tls()
       
   364 {
       
   365 	iTestStepName = _L("t_tls_DialogMode");
       
   366 }
       
   367 
       
   368 /**
       
   369  This is the test code for CTestStepDialogMode_Tls
       
   370 */
       
   371 TVerdict CTestStepDialogMode_Tls::doTestStepL( )
       
   372 {
       
   373 	__UHEAP_MARK; 
       
   374 
       
   375 	iTestStepResult = EPass;
       
   376 
       
   377     Log(_L("Testing dialog mode change"));
       
   378 
       
   379 	// Create and install active scheduler
       
   380 	CActiveScheduler*   myActiveScheduler;
       
   381 	myActiveScheduler = new(ELeave) CActiveScheduler();
       
   382 	CleanupStack::PushL( myActiveScheduler );
       
   383 	CActiveScheduler::Install( myActiveScheduler );
       
   384 
       
   385     RSocket  sock;
       
   386     
       
   387     MSecureSocket* pSecSock = CTlsConnection::NewL(sock, KProtocolVerSSL30);
       
   388    
       
   389     TDialogMode dlgMode;
       
   390     TInt nErr;
       
   391   
       
   392     //========================================================================================
       
   393     //==    Test setting dialog mode via MSecureSocket::SetDialogMode()
       
   394     //==    In this case we will use EDialogModeUnattended, EDialogModeAttended enum
       
   395     //========================================================================================
       
   396     
       
   397     //-- 1. test default dialog mode, it shall be EDialogModeAttended
       
   398     dlgMode = pSecSock->DialogMode();
       
   399     TESTL(dlgMode == EDialogModeAttended);
       
   400    
       
   401     //-- 2. change dialog mode, check if has been changed correctly
       
   402     nErr = pSecSock->SetDialogMode(EDialogModeUnattended);
       
   403     TESTL(nErr == KErrNone);
       
   404     
       
   405     dlgMode =  pSecSock->DialogMode();
       
   406     TESTL(dlgMode == EDialogModeUnattended);
       
   407 
       
   408     //-- 3. change dialog mode to a different value, check if has been changed correctly
       
   409     nErr = pSecSock->SetDialogMode(EDialogModeAttended);
       
   410     TESTL(nErr == KErrNone);
       
   411     
       
   412     dlgMode =  pSecSock->DialogMode();
       
   413     TESTL(dlgMode == EDialogModeAttended);
       
   414     
       
   415     //-- 4. try to pass invalid value as a dialog mode
       
   416     const TUint KInvalidModeValue = 0xdead;
       
   417     nErr = pSecSock->SetDialogMode(static_cast<TDialogMode>(KInvalidModeValue));
       
   418     TESTL(nErr == KErrArgument);
       
   419         
       
   420 
       
   421     //========================================================================================
       
   422     //== Test setting dialog mode via MSecureSocket::SetOpt()
       
   423     //== In this case KSSLDialogUnattendedMode, KSSLDialogAttendedMode constants will be used
       
   424     //== Check also their consistency with TDialogMode enum values
       
   425     //========================================================================================
       
   426 
       
   427     TInt nOption=-1;
       
   428     
       
   429     //-- Set dialog mode to EDialogModeUnattended via MSecureSocket::SetOpt()
       
   430     
       
   431     nErr = pSecSock->SetOpt(KSoDialogMode,KSolInetSSL,KSSLDialogUnattendedMode);
       
   432     TESTL(nErr == KErrNone);
       
   433     
       
   434     nErr = pSecSock->GetOpt(KSoDialogMode,KSolInetSSL,nOption);
       
   435     TESTL(nErr == KErrNone);
       
   436     TESTL((TUint)nOption == KSSLDialogUnattendedMode);
       
   437 
       
   438     dlgMode =  pSecSock->DialogMode();
       
   439     TESTL(dlgMode == EDialogModeUnattended); //-- consistency check
       
   440 
       
   441 
       
   442     //-- Set dialog mode to EDialogModeAttended via MSecureSocket::SetOpt()
       
   443 
       
   444     nErr = pSecSock->SetOpt(KSoDialogMode,KSolInetSSL,KSSLDialogAttendedMode);
       
   445     TESTL(nErr == KErrNone);
       
   446 
       
   447     nErr = pSecSock->GetOpt(KSoDialogMode,KSolInetSSL,nOption);
       
   448     TESTL(nErr == KErrNone);
       
   449     TESTL((TUint)nOption == KSSLDialogAttendedMode);
       
   450     
       
   451     dlgMode =  pSecSock->DialogMode();
       
   452     TESTL(dlgMode == EDialogModeAttended); //-- consistency check
       
   453 
       
   454     delete pSecSock;
       
   455 	
       
   456 
       
   457 	// Remove objects from the cleanup stack
       
   458 	CleanupStack::PopAndDestroy( 1 ); // myActiveScheduler, 
       
   459 	__UHEAP_MARKEND;
       
   460 	
       
   461 	return iTestStepResult;
       
   462 }
       
   463 
       
   464 
       
   465 
       
   466 
       
   467