os/security/cryptomgmtlibs/securitytestfw/test/sntpclient/sntpclient.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2009 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 "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include <e32base.h>
    20 #include <bacline.h>
    21 
    22 #include "commandlineargs.h"
    23 #include "sntpclientengine.h"
    24 #include "util.h"
    25 
    26 LOCAL_C void RunSNTPClientL();
    27 LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs);
    28 
    29 
    30 GLDEF_C TInt E32Main() // main function called by E32
    31     {
    32 	__UHEAP_MARK;
    33 	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
    34 	TRAPD(err, RunSNTPClientL()); 
    35 	delete cleanup; // destroy clean-up stack
    36 	__UHEAP_MARKEND;
    37 	return err; // and return
    38     }
    39     
    40 
    41     
    42 LOCAL_C void RunSNTPClientL()
    43 	{
    44 	TRAP_IGNORE(Util::SetAppropriateTimezoneL());
    45 	
    46 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
    47 	CleanupStack::PushL(scheduler);
    48 	CActiveScheduler::Install(scheduler);
    49 	
    50 	// Parse the command line
    51 	
    52 	TCommandLineArgs args;
    53 	ParseCommandLineL(args);
    54 	
    55 	// Create the client object, and allow it to run to completion
    56 	
    57 	CSNTPClient* engine = CSNTPClient::NewLC(args);
    58 	engine->Start();
    59 	CActiveScheduler::Start();
    60 	
    61 	args.iServers.ResetAndDestroy();
    62 	
    63 	CActiveScheduler::Install(NULL);
    64 	
    65 	if (engine->State() == EStateFailed)
    66 		{
    67 		User::Leave(KErrGeneral);
    68 		}
    69 	else if (engine->State() == EStateAborted)
    70 		{
    71 		User::Leave(KErrTimedOut);
    72 		}
    73 		
    74 	CleanupStack::PopAndDestroy(2, scheduler);
    75 	
    76 	}
    77 	
    78 LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs)
    79 	{
    80 	
    81 	/* Get and parse the command line arguments */
    82 	
    83 	TBool parsingOffset(EFalse);
    84 	TBool enoughArguments(EFalse);
    85 	
    86 	aArgs.iOffset = 0;
    87 	aArgs.iApplyDaylightSavings = EFalse;
    88 	
    89 	CCommandLineArguments* args = CCommandLineArguments::NewLC();
    90 	TInt argCount = args->Count();
    91 	
    92 	/* Ignore the first argument, this is the executable name */
    93 	
    94 	for (TInt i = 1; i < argCount; ++i)
    95 		{
    96 		
    97 		TPtrC arg = args->Arg(i);
    98 		
    99 		
   100 		if (parsingOffset)
   101 			{
   102 			
   103 			if (arg.Length() != 3)
   104 				{
   105 				User::Leave(KErrArgument);
   106 				}
   107 			
   108 						
   109 			TInt temp(((arg[1] - '0') * 10) + (arg[2] - '0'));
   110 			
   111 			if (arg[0] == '-')
   112 				{
   113 				aArgs.iOffset = -temp;
   114 				}
   115 			else
   116 				{
   117 				aArgs.iOffset = temp;
   118 				}
   119 			
   120 			parsingOffset = EFalse;
   121 			
   122 			}
   123 		else if (arg.CompareF(KCommandLineDaylightSavings) == 0)
   124 			{
   125 			aArgs.iApplyDaylightSavings = ETrue;
   126 			}
   127 		else if (arg.CompareF(KCommandLineOffset) == 0)
   128 			{
   129 			parsingOffset = ETrue;
   130 			}
   131 		else
   132 			{
   133 			// This argument must be the IP address/hostname
   134 			// sanity check the name
   135 			
   136 			if (arg.Length() > 256)
   137 				{
   138 				User::Leave(KErrArgument);
   139 				}
   140 			
   141 			HBufC* server = arg.AllocLC();
   142 			User::LeaveIfError(aArgs.iServers.Append(server));
   143 			CleanupStack::Pop(server);
   144 			
   145 			enoughArguments = ETrue;
   146 			}
   147 		
   148 		}
   149 		
   150 	// Sanity check the arguments
   151 	if ((!enoughArguments) || aArgs.iOffset < -12 || aArgs.iOffset > 13)
   152 		{
   153 		User::Leave(KErrArgument);
   154 		}
   155 		
   156 	CleanupStack::PopAndDestroy(args);
   157 	
   158 	}