tcpiputils/dhcp/te_unittest/dhcpservtest.cpp
changeset 0 af10295192d8
child 18 39bb7c3571e9
child 53 7e41d162e158
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2008-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 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20  
       
    21 #include <e32std.h>
       
    22 #include <e32base.h>
       
    23 #include <e32cons.h>
       
    24 #include <es_sock.h>
       
    25 #include <in_sock.h>
       
    26 #include <commdbconnpref.h>
       
    27 #include<c32comm.h>
       
    28 #include "es_enum.h" 
       
    29 #include <nifman.h>
       
    30 #include <naptinterface.h>
       
    31 #include "dhcpstatesdebug.h"
       
    32 
       
    33 /**
       
    34  * MAC address length
       
    35  */
       
    36   const TInt KMacAddrLength = 6;
       
    37  
       
    38 /** 
       
    39  * Variables used to configure the DHCP server.
       
    40  *
       
    41  * @internalTechnology
       
    42  */
       
    43 TText8 KReset[KMacAddrLength]= {0x00,0x00,0x00,0x00,0x00,0x00};
       
    44 TText8 KAny[KMacAddrLength]= {0xff,0xff,0xff,0xff,0xff,0xff};
       
    45 //This value has to be changed by user and recompile this code. The value is the MAC address of 
       
    46 //the machine for which DHCP server has to provide the IP.
       
    47 TText8 KCurrent[KMacAddrLength]= {0x04,0x13,0x21,0xca,0x78,0xde};
       
    48 
       
    49 /**
       
    50   * Console pointer to display the help and application response.
       
    51   *
       
    52   * @internalTechnology
       
    53   */
       
    54 CConsoleBase *gConsole;
       
    55 
       
    56 /** 
       
    57  * Function to load the DHCP server.
       
    58  *
       
    59  * @internalTechnology
       
    60  * @param socketServer - client side session of Socket server
       
    61  * @param aDhcpServConnection - RConnection to load and configure DHCP server.
       
    62  * @return - returns if there are any error, otherwise KErrNone.
       
    63  */
       
    64 TInt LaunchDhcpServ(RSocketServ& socketServer, RConnection& aDhcpServConnection)
       
    65 	{
       
    66 	
       
    67 	TInt err(KErrNone);
       
    68 	//Open a subsession with the socket server for the RConnection
       
    69 	err = aDhcpServConnection.Open(socketServer);
       
    70 	if(err != KErrNone) return err;
       
    71 	
       
    72 	TCommDbConnPref prefs;
       
    73 	prefs.SetIapId(12); //IAP 12 is used to launch the DHCP server.
       
    74 	prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
       
    75 
       
    76 	TRequestStatus status;
       
    77 	aDhcpServConnection.Start(prefs, status); //Loads the DHCP server, if the provided configuration is perfect.
       
    78 	User::WaitForRequest(status);
       
    79 	gConsole->Printf(_L("aDhcpServConnection. Start Status %d\n"), status.Int());
       
    80 	if(status.Int() != KErrNone) return status.Int();
       
    81 	return KErrNone;
       
    82 	}
       
    83 
       
    84 /** 
       
    85  * Function to configure DHCP with the received aHwAddr.
       
    86  *
       
    87  * @internalTechnology
       
    88  * @param aDhcpServConnection - RConnection to load and configure DHCP server.
       
    89  * @param aHwAddr - Hardware address.
       
    90  * @return - returns if there are any error, otherwise KErrNone.
       
    91  */
       
    92 TInt ProvisionDhcpServ(RConnection& aDhcpServConnection, TText8* aHwAddr)
       
    93 	{
       
    94 	TRequestStatus status;
       
    95 	TBuf8<KMacAddrLength> address = TPtrC8(&aHwAddr[0],KMacAddrLength);
       
    96 	aDhcpServConnection.Ioctl(KCOLConfiguration, KConnDhcpSetHwAddressParams, status, &address);
       
    97 	User::WaitForRequest(status);
       
    98 	return status.Int();
       
    99 	}
       
   100 
       
   101 /** 
       
   102  * Function to stop DHCP server.
       
   103  *
       
   104  * @internalTechnology
       
   105  * @param aDhcpServConnection - RConnection associated with DHCP server.
       
   106  */
       
   107 void StopInterfacesL(RConnection& aConnection)
       
   108 	{
       
   109 	User::LeaveIfError(aConnection.Stop());
       
   110 	aConnection.Close();
       
   111 	}
       
   112 
       
   113 /** 
       
   114  * Function to stop DHCP server.
       
   115  *
       
   116  * @internalTechnology
       
   117  * @param aDhcpServConnection - RConnection associated with DHCP server.
       
   118  */
       
   119 TInt TestAddressProvisioningL(CConsoleBase* gConsole)
       
   120 	{
       
   121 #ifdef _DEBUG	
       
   122 	User::LeaveIfError(RProperty::Set(KMyPropertyCat, KMyPropertyDestPortv4, 118));
       
   123 #endif
       
   124 	
       
   125 	RSocketServ socketServer;
       
   126     TInt err(KErrNone);
       
   127     err = socketServer.Connect();
       
   128 	if (err != KErrNone) return err;
       
   129 	
       
   130 	RConnection myDhcpServConnection;
       
   131     err = LaunchDhcpServ(socketServer, myDhcpServConnection);
       
   132 	if (err != KErrNone) return err;
       
   133 	TBool done = EFalse;
       
   134 	while (!done)
       
   135 		{
       
   136 		// Get user command from menu
       
   137 		gConsole->ClearScreen();
       
   138 		_LIT(KSelectOption, " Select an option \n\n");
       
   139 		gConsole->Printf(KSelectOption);
       
   140 		_LIT(KPossibleSelectionsText, " 1 KAny \n 2 KRest \n 3 Provision \n 4 NoProvisioning \n 5 Quit \n");
       
   141 		gConsole->Printf(KPossibleSelectionsText);
       
   142 		TKeyCode key = gConsole->Getch();
       
   143 		switch (key)
       
   144 			{
       
   145 		case '1': 
       
   146 			err = ProvisionDhcpServ(myDhcpServConnection, KAny);
       
   147 			if (err != KErrNone) return err;
       
   148 			gConsole->Printf(_L("DHCP server is in ANY mode.\n"));
       
   149 			break;
       
   150 		case '2':
       
   151 			err = ProvisionDhcpServ(myDhcpServConnection, KReset);
       
   152 			if (err != KErrNone) return err;
       
   153 			gConsole->Printf(_L("DHCP server is in RESET mode.\n"));
       
   154 			break;
       
   155 		case '3': 
       
   156 			err = ProvisionDhcpServ(myDhcpServConnection, KCurrent);
       
   157 			if (err != KErrNone) return err;
       
   158 			gConsole->Printf(_L("DHCP server is in SPECIFIC mode.\n"));
       
   159 			break;
       
   160 		case '4':
       
   161 			err = ProvisionDhcpServ(myDhcpServConnection, KReset);
       
   162 			if (err != KErrNone) return err;
       
   163 			gConsole->Printf(_L("DHCP server is in RESET mode.\n"));
       
   164 			break;
       
   165 		case '5':
       
   166 		done = ETrue;
       
   167 			break;
       
   168 		default:
       
   169 			break;
       
   170 			}
       
   171 		gConsole->Printf(_L("Press any key to next.\n"));
       
   172 		gConsole->Getch();
       
   173 		} // while
       
   174 	
       
   175     StopInterfacesL(myDhcpServConnection);
       
   176     gConsole->Printf(_L("Basic DHCP testing finished .....\n"));	
       
   177 	return KErrNone;
       
   178 	}
       
   179 
       
   180 /** 
       
   181  * Function to triggers the test cases.
       
   182  *
       
   183  * @internalTechnology
       
   184  * @return - Returns error if any, otherwise KErrNone.
       
   185  */
       
   186 TInt TestConsoleL()
       
   187 	{
       
   188 	gConsole = Console::NewL(_L("PREQ1872 DHCP Unit Test Console"), TSize(KConsFullScreen, KConsFullScreen));
       
   189 	CleanupStack::PushL(gConsole);
       
   190 	TInt err = TestAddressProvisioningL(gConsole);
       
   191 	CleanupStack::PopAndDestroy(gConsole);
       
   192 	return err;
       
   193 	}
       
   194 
       
   195 /** 
       
   196  * Main function
       
   197  *
       
   198  * @internalTechnology
       
   199  * @return - Returns 0.
       
   200  */
       
   201 GLDEF_C TInt E32Main()
       
   202 	{
       
   203 	__UHEAP_MARK;
       
   204 	CTrapCleanup * trap = CTrapCleanup::New();
       
   205 	TRAPD(err,TestConsoleL());
       
   206 	delete trap;
       
   207 	__UHEAP_MARKEND;
       
   208 	return err;
       
   209 	}