networksecurity/ipsec/te_ipsec_selector_confilict/src/te_coverage_test.cpp
branchRCL_3
changeset 58 8d540f55e491
parent 53 7e41d162e158
equal deleted inserted replaced
57:abbed5a4b42a 58:8d540f55e491
       
     1 /*
       
     2 * Copyright (c) 2003 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 "Symbian Foundation License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 ** @File :- te_loadpolicy_bbd.cpp Implements loading the policy 
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  * @file ts_ipsec_polapi.cpp Implements main test code for IPsec
       
    22  */
       
    23 
       
    24 #include "te_coverage_test.h"
       
    25 #include "te_ipsecconst.h"
       
    26 #include "te_selectorconflict.h"
       
    27 #include <ES_SOCK.H> 
       
    28 #include "lib_pfkey.h"
       
    29 #include "pfkey_ext.h"
       
    30 #include "pfkey_send.h"
       
    31 #include <commdbconnpref.h>
       
    32 #include <ipsecpolapi.h>
       
    33 
       
    34 #if defined(SYMBIAN_CRYPTO)
       
    35 #include <cryptostrength.h>
       
    36 #else
       
    37 #include <cryptalg.h>
       
    38 #endif
       
    39 
       
    40 _LIT8( KMyPolicy,  
       
    41 "SECURITY_FILE_VERSION: 3\r\n[INFO]\r\n\
       
    42 IpSec Policy LOADING\r\n\
       
    43 [POLICY]\r\n\
       
    44 sa trans_1 = {\r\n\
       
    45 esp\r\n\
       
    46 encrypt_alg 12\r\n\
       
    47 src_specific\r\n\
       
    48 }\r\n\
       
    49 outbound remote 10.225.208.183 255.255.255.255 protocol 17 = { trans_1(10.225.208.183) }\r\n\
       
    50 inbound local 10.225.208.102 255.255.255.255 protocol 17  = { trans_1(10.225.208.183) }\r\n\
       
    51 inbound = {}\r\n\
       
    52 outbound = {}\r\n" );
       
    53 
       
    54 
       
    55 _LIT8( KMyPolicy1,  
       
    56 "SECURITY_FILE_VERSION: 3\r\n[INFO]\r\n\
       
    57 IpSec Policy LOADING\r\n\
       
    58 [POLICY]\r\n\
       
    59 sa trans_2 = {\r\n\
       
    60 esp\r\n\
       
    61 encrypt_alg 12\r\n\
       
    62 src_specific\r\n\
       
    63 }\r\n\
       
    64 outbound remote 10.225.208.49 255.255.255.255 protocol 17 = { trans_2(10.225.208.49) }\r\n\
       
    65 inbound local 10.225.208.155 255.255.255.255 protocol 17  = { trans_2(10.225.208.49) }\r\n" );
       
    66 
       
    67 _LIT8( KMyPolicy2,  
       
    68 "SECURITY_FILE_VERSION: 3\r\n[INFO]\r\n\
       
    69 IpSec Policy LOADING\r\n\
       
    70 [POLICY]\r\n\
       
    71 sa trans_3 = {\r\n\
       
    72 esp\r\n\
       
    73 encrypt_alg 12\r\n\
       
    74 src_specific\r\n\
       
    75 }\r\n\
       
    76 outbound remote 192.168.1.1 255.255.255.255 protocol 17 = { trans_3() }\r\n\
       
    77 inbound local 192.168.1.2 255.255.255.255 protocol 17  = { trans_3() }\r\n\
       
    78 inbound = {}\r\n\
       
    79 outbound = {}\r\n" );
       
    80 
       
    81 
       
    82 /**
       
    83 Purpose: Constructor of CT_IPSecIKEV2TestWrapper class
       
    84 @internalComponent
       
    85 */
       
    86 CT_CoverageTest::CT_CoverageTest()
       
    87 	{
       
    88 	}
       
    89 
       
    90 /**
       
    91 Purpose: Destructor of CT_CoverageTest class
       
    92 @internalComponent
       
    93 */
       
    94 CT_CoverageTest::~CT_CoverageTest()
       
    95 	{
       
    96 	delete iObject;
       
    97 	iObject = NULL;
       
    98 	}
       
    99 
       
   100 /**
       
   101 Purpose: Command fuction of CT_CoverageTest class
       
   102 @internalComponent
       
   103 */
       
   104 CT_CoverageTest* CT_CoverageTest::NewL()
       
   105 	{
       
   106 	CT_CoverageTest*	ret = new (ELeave) CT_CoverageTest();
       
   107 	CleanupStack::PushL(ret);
       
   108 	ret->ConstructL();
       
   109 	CleanupStack::Pop(ret);
       
   110 	return ret;
       
   111 	}
       
   112 	
       
   113 	
       
   114 /**
       
   115 Purpose: Command fuction for a wrapper class
       
   116 @internalComponent
       
   117 */
       
   118 void CT_CoverageTest::ConstructL()
       
   119 	{
       
   120     TInt errfound;
       
   121     
       
   122 	iObject = new (ELeave) TInt;
       
   123 	errfound = iDrpPolicyServer.Connect();
       
   124 	if( errfound != KErrNone )
       
   125 	    {
       
   126 	    ERR_PRINTF2(_L("failed to connect RIpsecPolicyServ with error: %d"), errfound);
       
   127 	    SetBlockResult(EFail);
       
   128 	    return;
       
   129 	    }
       
   130 	errfound=iBypsPolicyServer.Connect();
       
   131 	if( errfound != KErrNone )
       
   132 	    {
       
   133 	    ERR_PRINTF2(_L("failed to connect RIpsecPolicyServ with error: %d"), errfound);
       
   134 	    SetBlockResult(EFail); 
       
   135 	    return;
       
   136 	    }
       
   137 
       
   138 	iMyZoneInfoSet.iSelectorZone.iScope = KScopeNetwork;
       
   139 	iMyZoneInfoSet.iSelectorZone.iId = 8;
       
   140 	iMyZoneInfoSet.iEndPointZone.iScope = KScopeNetwork;
       
   141 	iMyZoneInfoSet.iEndPointZone.iId = 7;
       
   142 	        
       
   143 	}
       
   144 
       
   145 
       
   146 /**
       
   147 Purpose: Command fuction for a wrapper class
       
   148 @internalComponent
       
   149 */
       
   150 TBool CT_CoverageTest::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
       
   151 	{
       
   152 	TBool ret = ETrue;
       
   153 
       
   154 	// Print out the parameters for debugging
       
   155 	INFO_PRINTF2( _L("<font size=2 color=990000><b>aCommand = %S</b></font>"), &aCommand );
       
   156 	INFO_PRINTF2( _L("aSection = %S"), &aSection );
       
   157 	INFO_PRINTF2( _L("aAsyncErrorIndex = %D"), aAsyncErrorIndex );
       
   158 
       
   159 	if(KNewCMD() == aCommand)
       
   160 		{
       
   161 		DoCmdNewL(aSection);
       
   162 		}
       
   163 	else if(KTestLoadBypassPolicy() == aCommand)
       
   164 		{
       
   165 	    DoLoadBypassModePolicy(aSection);
       
   166 		}
       
   167 	else if (KTestLoadDropModePolicy() == aCommand)
       
   168 	    {
       
   169         DoLoadDropModePolicy(aSection);
       
   170 	    }
       
   171 	else if (KLoadNewBypassPolicy() == aCommand)
       
   172         {
       
   173         DoLoadNewBypassModePolicy(aSection);
       
   174         }
       
   175 	else if (KUnloadDropPolicy() == aCommand)
       
   176         {
       
   177         DoUnloadDropPolicy(aSection);
       
   178         }
       
   179 	else if (KUnloadBypassPolicy() == aCommand)
       
   180         {
       
   181         DoUnloadBypassPolicy(aSection);
       
   182         }
       
   183 	else if (KUnloadNewBypassPolicy() == aCommand)
       
   184 	    {
       
   185 	    DoUnloadNewBypassPolicy(aSection);
       
   186 	    }	
       
   187 	else if (KCloseConnection() == aCommand)
       
   188 	    {
       
   189 	    DoCmdClose(aSection);
       
   190 	    }
       
   191 	else
       
   192 		{
       
   193 		ret = EFalse;
       
   194 		}
       
   195 
       
   196 	return ret;
       
   197 	}
       
   198 
       
   199 
       
   200 /**
       
   201 Purpose: To create a new object of the CTEFTest type through the API.
       
   202 
       
   203 Ini file options:
       
   204 	iniData - The data from the ini file at the section provided.
       
   205 
       
   206 @internalComponent
       
   207 @param  aSection Current ini file command section
       
   208 */
       
   209 void CT_CoverageTest::DoCmdNewL(const TDesC& aSection)
       
   210 	{
       
   211 	TInt objectValue = 0;
       
   212 	TInt iapid =0;
       
   213 	if (!GetIntFromConfig(aSection, KObjectValue(), objectValue))
       
   214 		{
       
   215 		ERR_PRINTF2(_L("<font color=FF0000>No parameter %S</font>"), &KObjectValue());
       
   216 		SetBlockResult(EFail);
       
   217 		}
       
   218 	else
       
   219 	    {
       
   220 	    *iObject = objectValue;	    
       
   221 	    
       
   222         if(!GetIntFromConfig(aSection, KIapid(), iapid))
       
   223             {
       
   224             ERR_PRINTF2(_L("<font color=FF0000>No parameter %S</font>"), &KObjectValue());
       
   225             SetBlockResult(EFail);
       
   226             }
       
   227         else
       
   228             {
       
   229             }
       
   230 	    }
       
   231 	}
       
   232 
       
   233 /**
       
   234  * Bypass policy loading for ONB scripts
       
   235  */
       
   236 void CT_CoverageTest::DoLoadBypassModePolicy(const TDesC& /*aSection*/) 
       
   237     {
       
   238     
       
   239     TInt errfound=iBypsPolicyServer.Connect();
       
   240     if( errfound != KErrNone )
       
   241          {
       
   242          ERR_PRINTF2(_L("failed to connect RIpsecPolicyServ with error: %d"), errfound);
       
   243          SetBlockResult(EFail);
       
   244          return;
       
   245          }
       
   246      /***
       
   247      * this code is to check the UMA setopt function
       
   248      * if we call with 0 then it is considered as UMA policy
       
   249      */
       
   250     TInt sur = 0x2002E241;
       
   251     _LIT8(KFormatStr,"%D");
       
   252     TBuf8<50> sidbuf;
       
   253     sidbuf.Format(KFormatStr,sur);
       
   254     TLex8 lex(sidbuf);    
       
   255     TInt intval;
       
   256     TInt err = lex.Val(intval);
       
   257     
       
   258     //this test case is to cove all the conditions.
       
   259     //in this case all the parmeters are proper
       
   260     TInt error = iBypsPolicyServer.SetOpt(KOptionNameSid, KOptionLevelDefault, sidbuf);        
       
   261     if ( KErrNone != error )
       
   262         {
       
   263         ERR_PRINTF2(_L("DoLoadUMAPolicy->LoadPolicy ERROR  in setOpt to the policy server %d"), error);
       
   264         SetError(error);
       
   265         SetBlockResult(EFail);
       
   266         return ;
       
   267         }
       
   268         
       
   269 
       
   270     //in this case second parm is correct
       
   271     error = iBypsPolicyServer.SetOpt(0 , KOptionLevelDefault, sidbuf);        
       
   272     if ( KErrArgument != error )
       
   273         {
       
   274         ERR_PRINTF2(_L("DoLoadUMAPolicy->LoadPolicy ERROR  in setOpt to the policy server %d"), error);
       
   275         SetError(error);
       
   276         SetBlockResult(EFail);
       
   277         return ;
       
   278         }
       
   279 
       
   280     //in this case second parm is correct
       
   281     error = iBypsPolicyServer.SetOpt(KOptionNameSid , 0, sidbuf);        
       
   282     if ( KErrArgument != error )
       
   283         {
       
   284         ERR_PRINTF2(_L("DoLoadUMAPolicy->LoadPolicy ERROR  in setOpt to the policy server %d"), error);
       
   285         SetError(error);
       
   286         SetBlockResult(EFail);
       
   287         return ;
       
   288         }
       
   289     
       
   290        RSocketServ pSocketServ;
       
   291        /** Handle to SADB socket */
       
   292        RSADB pSADB;
       
   293        err = pSocketServ.Connect();
       
   294        if ( err != KErrNone)
       
   295             {
       
   296             ERR_PRINTF2(_L("DoLoadDropModePolicy():- Failed to connect  RSocketServ with error: %d"), err);
       
   297             SetBlockResult(EFail);   
       
   298             }   
       
   299        err = pSADB.Open(pSocketServ);
       
   300        if ( err != KErrNone)
       
   301             {
       
   302             ERR_PRINTF2(_L("DoLoadDropModePolicy():- Failed to open RSADB with error: %d"), err);
       
   303             SetBlockResult(EFail);  
       
   304             }                                   
       
   305                   
       
   306        HBufC8 *policyData = HBufC8::NewLC( KMyPolicy().Length() + 256); // Allow size for IP spec.
       
   307        TPtr8 policyDataPtr(policyData->Des());
       
   308        policyDataPtr.Append(KMyPolicy);
       
   309        ///
       
   310         CMDBSession *cmdbSession = CMDBSession::NewL(CMDBSession::LatestVersion());            
       
   311         CleanupStack::PushL(cmdbSession);
       
   312         CCDIAPRecord *iapRecord = (CCDIAPRecord*)CCDRecordBase::RecordFactoryL(KCDTIdIAPRecord);
       
   313         CleanupStack::PushL(iapRecord);
       
   314         iapRecord->SetRecordId(12);
       
   315         iapRecord->LoadL(*cmdbSession);
       
   316         iapRecord->iAppSid = 0x2002E241;
       
   317         iapRecord->ModifyL(*cmdbSession);
       
   318         CleanupStack::PopAndDestroy(iapRecord);
       
   319         CleanupStack::PopAndDestroy(cmdbSession);
       
   320        
       
   321        //
       
   322        TRequestStatus status;
       
   323        iBypsPolicyServer.LoadPolicy( *policyData, iBypsPolicyHandle, status, iMyZoneInfoSet);
       
   324        User::WaitForRequest(status);
       
   325        TInt err1 = status.Int();
       
   326        if(err1 == KErrNone)
       
   327            {
       
   328            iBypsPolicyServer.ActivatePolicy(iBypsPolicyHandle(), status );
       
   329            User::WaitForRequest(status);
       
   330            User::LeaveIfError(status.Int());   
       
   331            SetBlockResult(EPass);
       
   332            }
       
   333        else
       
   334            {
       
   335            SetError(status.Int());
       
   336            ERR_PRINTF2(_L("DoLoadDropModePolicy->LoadPolicy failed with error: %d"),err1);
       
   337            SetBlockResult(EFail);
       
   338            }
       
   339        CleanupStack::PopAndDestroy(policyData);    
       
   340        pSADB.Close();
       
   341        pSocketServ.Close();    
       
   342     }
       
   343 
       
   344 /*
       
   345 *Execute the test for closing the connection.
       
   346 */
       
   347 void CT_CoverageTest::DoLoadDropModePolicy(const TDesC& /*aSection*/)
       
   348     {       
       
   349     RSocketServ pSocketServ;
       
   350     /** Handle to SADB socket */
       
   351     RSADB pSADB;
       
   352     TInt err;
       
   353             
       
   354     err = pSocketServ.Connect();
       
   355     if ( err != KErrNone)
       
   356          {
       
   357          ERR_PRINTF2(_L("DoLoadDropModePolicy():- Failed to connect  RSocketServ with error: %d"), err);
       
   358          SetBlockResult(EFail);   
       
   359          }   
       
   360     err = pSADB.Open(pSocketServ);
       
   361     if ( err != KErrNone)
       
   362          {
       
   363          ERR_PRINTF2(_L("DoLoadDropModePolicy():- Failed to open RSADB with error: %d"), err);
       
   364          SetBlockResult(EFail);  
       
   365          }                                   
       
   366                
       
   367     HBufC8 *policyData = HBufC8::NewLC( KMyPolicy1().Length() + 256); // Allow size for IP spec.
       
   368     TPtr8 policyDataPtr(policyData->Des());
       
   369     policyDataPtr.Append(KMyPolicy1);
       
   370     ///
       
   371      CMDBSession *cmdbSession = CMDBSession::NewL(CMDBSession::LatestVersion());            
       
   372      CleanupStack::PushL(cmdbSession);
       
   373      CCDIAPRecord *iapRecord = (CCDIAPRecord*)CCDRecordBase::RecordFactoryL(KCDTIdIAPRecord);
       
   374      CleanupStack::PushL(iapRecord);
       
   375      iapRecord->SetRecordId(12);
       
   376      iapRecord->LoadL(*cmdbSession);
       
   377      iapRecord->iAppSid = 0;
       
   378      iapRecord->ModifyL(*cmdbSession);
       
   379      CleanupStack::PopAndDestroy(iapRecord);
       
   380      CleanupStack::PopAndDestroy(cmdbSession);    
       
   381     ///
       
   382     //storing the SID information to the policy server which
       
   383     //is used to identify the UMA policy in ipsec policy manager handler.
       
   384     TInt error=0 ;
       
   385     //pPolicyServer.SetOpt(KOptionNameSid, KOptionLevelDefault, sidbuf);
       
   386     //ERR_PRINTF2(_L("DoLoadDropModePolicy->LoadPolicy ERROR  in setOpt to the policy server %s"), sidBuffer);
       
   387     RDebug::Printf("\n see the value \n");
       
   388     if ( KErrNone != error )
       
   389     {
       
   390     ERR_PRINTF2(_L("DoLoadDropModePolicy->LoadPolicy ERROR  in setOpt to the policy server %d"), error);
       
   391     }
       
   392     
       
   393     TRequestStatus status;
       
   394     iDrpPolicyServer.LoadPolicy( *policyData, iDrpPolicyHandle, status, iMyZoneInfoSet);
       
   395     User::WaitForRequest(status);
       
   396     TInt err1 = status.Int();
       
   397     if(err1 == KErrNone)
       
   398         {
       
   399         iDrpPolicyServer.ActivatePolicy(iDrpPolicyHandle(), status );
       
   400         User::WaitForRequest(status);
       
   401         User::LeaveIfError(status.Int());   
       
   402         SetBlockResult(EPass);
       
   403         }
       
   404     else
       
   405         {
       
   406         SetError(status.Int());
       
   407         ERR_PRINTF2(_L("DoLoadDropModePolicy->LoadPolicy failed with error: %d"),err1);
       
   408         SetBlockResult(EFail);
       
   409         }
       
   410     CleanupStack::PopAndDestroy(policyData);    
       
   411     pSADB.Close();
       
   412     pSocketServ.Close();     
       
   413     }
       
   414 
       
   415 
       
   416 void CT_CoverageTest::DoLoadNewBypassModePolicy(const TDesC& /*aSection*/)
       
   417     {       
       
   418     RSocketServ pSocketServ;
       
   419     /** Handle to SADB socket */
       
   420     RSADB pSADB;
       
   421     TInt err;
       
   422             
       
   423     err = pSocketServ.Connect();
       
   424     if ( err != KErrNone)
       
   425          {
       
   426          ERR_PRINTF2(_L("DoLoadNewBypassModePolicy():- Failed to connect  RSocketServ with error: %d"), err);
       
   427          SetBlockResult(EFail);  
       
   428          }   
       
   429     err = pSADB.Open(pSocketServ);
       
   430     if ( err != KErrNone)
       
   431          {
       
   432          ERR_PRINTF2(_L("DoLoadNewBypassModePolicy():- Failed to open RSADB with error: %d"), err);
       
   433          SetBlockResult(EFail);   
       
   434          }                                   
       
   435                     
       
   436     HBufC8 *policyData = HBufC8::NewLC( KMyPolicy2().Length() + 256); // Allow size for IP spec.
       
   437     TPtr8 policyDataPtr(policyData->Des());
       
   438     policyDataPtr.Append(KMyPolicy2);
       
   439     
       
   440 
       
   441     TRequestStatus status;
       
   442     iBypsPolicyServer.LoadPolicy( *policyData, iNewBypsPolicyHandle, status, iMyZoneInfoSet);
       
   443     User::WaitForRequest(status);
       
   444     
       
   445     err = status.Int();
       
   446     if( err == KErrNone)
       
   447         {
       
   448         iBypsPolicyServer.ActivatePolicy( iNewBypsPolicyHandle(), status );
       
   449         User::WaitForRequest(status);
       
   450         User::LeaveIfError(status.Int());   
       
   451         SetBlockResult(EPass);
       
   452         }
       
   453     else
       
   454         {
       
   455         SetError(status.Int());
       
   456         ERR_PRINTF2(_L("DoLoadNewBypassPolicy():- LoadPolicy failed with error: %d"), err);
       
   457         SetBlockResult(EFail);
       
   458         }
       
   459     CleanupStack::PopAndDestroy(policyData);
       
   460     pSADB.Close();
       
   461     pSocketServ.Close();     
       
   462     }
       
   463 
       
   464 
       
   465 /*
       
   466 *Execute the test to unload the policy.
       
   467 */
       
   468 void CT_CoverageTest::DoUnloadDropPolicy(const TDesC& /*aSection*/)
       
   469     {      
       
   470     TRequestStatus status;
       
   471     iDrpPolicyServer.UnloadPolicy(iDrpPolicyHandle(),status);
       
   472     User::WaitForRequest(status);
       
   473     TInt err = status.Int();
       
   474     if( err != KErrNone)
       
   475         {
       
   476         SetError(err);
       
   477         ERR_PRINTF2(_L("DoUnloadDropPolicy():- UnloadPolicy failed with error: %d"), err);
       
   478         SetBlockResult(EFail);
       
   479         }
       
   480     }
       
   481 
       
   482 /*
       
   483 *Execute the test to unload the policy.
       
   484 */
       
   485 void CT_CoverageTest::DoUnloadNewBypassPolicy(const TDesC& /*aSection*/)
       
   486     {      
       
   487     TRequestStatus status;    
       
   488     iBypsPolicyServer.UnloadPolicy(iNewBypsPolicyHandle(),status);
       
   489     User::WaitForRequest(status);
       
   490     TInt err = status.Int();
       
   491     if( err != KErrNone)
       
   492         {
       
   493         SetError(err);
       
   494         ERR_PRINTF2(_L("DoUnloadNewBypassPolicy():- UnloadPolicy failed with error: %d"), err);
       
   495         SetBlockResult(EFail);
       
   496         }
       
   497     }
       
   498 
       
   499 /*
       
   500 *Execute the test to unload the policy.
       
   501 */
       
   502 void CT_CoverageTest::DoUnloadBypassPolicy(const TDesC& /*aSection*/)
       
   503     {      
       
   504     TRequestStatus status;    
       
   505     iBypsPolicyServer.UnloadPolicy(iBypsPolicyHandle(),status);
       
   506     User::WaitForRequest(status);
       
   507     TInt err = status.Int();
       
   508     if( err != KErrNone)
       
   509         {
       
   510         SetError(err);
       
   511         ERR_PRINTF2(_L("DoUnloadBypassPolicy():- UnloadPolicy failed with error: %d"), err);
       
   512         SetBlockResult(EFail);
       
   513         }
       
   514     }
       
   515 
       
   516 
       
   517 /*
       
   518 *Execute the test for closing the connection.
       
   519 */
       
   520 void CT_CoverageTest::DoCmdClose(const TDesC& /*aSection*/)
       
   521     {      
       
   522     SetBlockResult(EPass);
       
   523     }
       
   524     
       
   525 
       
   526