os/security/cryptomgmtlibs/securitytestfw/test/sntpclient/sntpclient.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptomgmtlibs/securitytestfw/test/sntpclient/sntpclient.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,158 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include <e32base.h>
    1.23 +#include <bacline.h>
    1.24 +
    1.25 +#include "commandlineargs.h"
    1.26 +#include "sntpclientengine.h"
    1.27 +#include "util.h"
    1.28 +
    1.29 +LOCAL_C void RunSNTPClientL();
    1.30 +LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs);
    1.31 +
    1.32 +
    1.33 +GLDEF_C TInt E32Main() // main function called by E32
    1.34 +    {
    1.35 +	__UHEAP_MARK;
    1.36 +	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
    1.37 +	TRAPD(err, RunSNTPClientL()); 
    1.38 +	delete cleanup; // destroy clean-up stack
    1.39 +	__UHEAP_MARKEND;
    1.40 +	return err; // and return
    1.41 +    }
    1.42 +    
    1.43 +
    1.44 +    
    1.45 +LOCAL_C void RunSNTPClientL()
    1.46 +	{
    1.47 +	TRAP_IGNORE(Util::SetAppropriateTimezoneL());
    1.48 +	
    1.49 +	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
    1.50 +	CleanupStack::PushL(scheduler);
    1.51 +	CActiveScheduler::Install(scheduler);
    1.52 +	
    1.53 +	// Parse the command line
    1.54 +	
    1.55 +	TCommandLineArgs args;
    1.56 +	ParseCommandLineL(args);
    1.57 +	
    1.58 +	// Create the client object, and allow it to run to completion
    1.59 +	
    1.60 +	CSNTPClient* engine = CSNTPClient::NewLC(args);
    1.61 +	engine->Start();
    1.62 +	CActiveScheduler::Start();
    1.63 +	
    1.64 +	args.iServers.ResetAndDestroy();
    1.65 +	
    1.66 +	CActiveScheduler::Install(NULL);
    1.67 +	
    1.68 +	if (engine->State() == EStateFailed)
    1.69 +		{
    1.70 +		User::Leave(KErrGeneral);
    1.71 +		}
    1.72 +	else if (engine->State() == EStateAborted)
    1.73 +		{
    1.74 +		User::Leave(KErrTimedOut);
    1.75 +		}
    1.76 +		
    1.77 +	CleanupStack::PopAndDestroy(2, scheduler);
    1.78 +	
    1.79 +	}
    1.80 +	
    1.81 +LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs)
    1.82 +	{
    1.83 +	
    1.84 +	/* Get and parse the command line arguments */
    1.85 +	
    1.86 +	TBool parsingOffset(EFalse);
    1.87 +	TBool enoughArguments(EFalse);
    1.88 +	
    1.89 +	aArgs.iOffset = 0;
    1.90 +	aArgs.iApplyDaylightSavings = EFalse;
    1.91 +	
    1.92 +	CCommandLineArguments* args = CCommandLineArguments::NewLC();
    1.93 +	TInt argCount = args->Count();
    1.94 +	
    1.95 +	/* Ignore the first argument, this is the executable name */
    1.96 +	
    1.97 +	for (TInt i = 1; i < argCount; ++i)
    1.98 +		{
    1.99 +		
   1.100 +		TPtrC arg = args->Arg(i);
   1.101 +		
   1.102 +		
   1.103 +		if (parsingOffset)
   1.104 +			{
   1.105 +			
   1.106 +			if (arg.Length() != 3)
   1.107 +				{
   1.108 +				User::Leave(KErrArgument);
   1.109 +				}
   1.110 +			
   1.111 +						
   1.112 +			TInt temp(((arg[1] - '0') * 10) + (arg[2] - '0'));
   1.113 +			
   1.114 +			if (arg[0] == '-')
   1.115 +				{
   1.116 +				aArgs.iOffset = -temp;
   1.117 +				}
   1.118 +			else
   1.119 +				{
   1.120 +				aArgs.iOffset = temp;
   1.121 +				}
   1.122 +			
   1.123 +			parsingOffset = EFalse;
   1.124 +			
   1.125 +			}
   1.126 +		else if (arg.CompareF(KCommandLineDaylightSavings) == 0)
   1.127 +			{
   1.128 +			aArgs.iApplyDaylightSavings = ETrue;
   1.129 +			}
   1.130 +		else if (arg.CompareF(KCommandLineOffset) == 0)
   1.131 +			{
   1.132 +			parsingOffset = ETrue;
   1.133 +			}
   1.134 +		else
   1.135 +			{
   1.136 +			// This argument must be the IP address/hostname
   1.137 +			// sanity check the name
   1.138 +			
   1.139 +			if (arg.Length() > 256)
   1.140 +				{
   1.141 +				User::Leave(KErrArgument);
   1.142 +				}
   1.143 +			
   1.144 +			HBufC* server = arg.AllocLC();
   1.145 +			User::LeaveIfError(aArgs.iServers.Append(server));
   1.146 +			CleanupStack::Pop(server);
   1.147 +			
   1.148 +			enoughArguments = ETrue;
   1.149 +			}
   1.150 +		
   1.151 +		}
   1.152 +		
   1.153 +	// Sanity check the arguments
   1.154 +	if ((!enoughArguments) || aArgs.iOffset < -12 || aArgs.iOffset > 13)
   1.155 +		{
   1.156 +		User::Leave(KErrArgument);
   1.157 +		}
   1.158 +		
   1.159 +	CleanupStack::PopAndDestroy(args);
   1.160 +	
   1.161 +	}