networksecurity/tlsprovider/Test/tlstest2/getrandomstep.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2006-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 getrandomstep.cpp
       
    18  @internalTechnology
       
    19 */
       
    20 #include "getrandomstep.h"
       
    21 #include <tlsprovinterface.h>
       
    22 #include <tlstypedef.h>
       
    23 
       
    24 _LIT(KEpochDate, "19700000:");
       
    25 
       
    26 CGetRandomStep::CGetRandomStep()
       
    27 	{
       
    28 	SetTestStepName(KGetRandomStep);
       
    29 	}
       
    30 	
       
    31 TVerdict CGetRandomStep::doTestStepPreambleL()
       
    32 	{
       
    33 	ConstructL();
       
    34 	return EPass;
       
    35 	}
       
    36 
       
    37 TVerdict CGetRandomStep::doTestStepL()
       
    38 	{
       
    39 	// Get three buffers of random...
       
    40 	TBuf8<KTLSServerClientRandomLen> random1;
       
    41 	TBuf8<KTLSServerClientRandomLen> random2;
       
    42 	TBuf8<KTLSServerClientRandomLen> random3;
       
    43 	
       
    44 	
       
    45 	INFO_PRINTF1(_L("Invoking TLS Provider - Generate Random"));
       
    46 	
       
    47 	Provider()->GenerateRandom(random1);
       
    48 	Provider()->GenerateRandom(random2);
       
    49 	Provider()->GenerateRandom(random3);
       
    50 	
       
    51 	// check all the buffers are of sane length
       
    52 	if (random1.Length() != KTLSServerClientRandomLen ||
       
    53 		random2.Length() != KTLSServerClientRandomLen ||
       
    54 		random3.Length() != KTLSServerClientRandomLen)
       
    55 		{
       
    56 		INFO_PRINTF1(_L("Failed! Not all random buffers are of the required length!"));
       
    57 		SetTestStepResult(EFail);
       
    58 		return TestStepResult();
       
    59 		}
       
    60 	
       
    61 	// check the timestamps on the data are all roughly sane....
       
    62 	
       
    63 	TTime now;
       
    64 	now.UniversalTime();
       
    65 	TTime epoch(KEpochDate);
       
    66 	
       
    67 	TUint stamp1 = GetTimestamp(random1);
       
    68 	TUint stamp2 = GetTimestamp(random2);
       
    69 	TUint stamp3 = GetTimestamp(random3); 
       
    70 	
       
    71 	if (stamp1 > stamp2 || stamp2 > stamp3)
       
    72 		{
       
    73 		INFO_PRINTF1(_L("Failed! Timestamps suggest an insane clock!"));
       
    74 		// uh oh, timestamps are going backwards...
       
    75 		SetTestStepResult(EFail);
       
    76 		return TestStepResult();
       
    77 		}
       
    78 		
       
    79 	// check all the time stamps are within a sane interval from "now"
       
    80 	TTime time1 = epoch + TTimeIntervalSeconds(stamp1);
       
    81 	TTime time2 = epoch + TTimeIntervalSeconds(stamp2);
       
    82 	TTime time3 = epoch + TTimeIntervalSeconds(stamp3);
       
    83 	
       
    84 	// read the interval from the config file...
       
    85 	TInt interval(0);
       
    86 	if (!GetIntFromConfig(ConfigSection(), KMaxTimestampInterval, interval))
       
    87 		{
       
    88 		User::Leave(KErrNotFound);
       
    89 		}
       
    90 	
       
    91 	TTimeIntervalSeconds interval1;
       
    92 	TInt err1 = now.SecondsFrom(time1, interval1);
       
    93 	TTimeIntervalSeconds interval2;
       
    94 	TInt err2 = now.SecondsFrom(time2, interval2);
       
    95 	TTimeIntervalSeconds interval3;
       
    96 	TInt err3 = now.SecondsFrom(time3, interval3);
       
    97 	
       
    98 	if (err1 != KErrNone || err2 != KErrNone || err3 != KErrNone)
       
    99 		{
       
   100 		INFO_PRINTF1(_L("Failed! Could not get time intervals from 'now'!"));
       
   101 		SetTestStepResult(EFail);
       
   102 		return TestStepResult();
       
   103 		}
       
   104 		
       
   105 	if (interval1.Int() > interval ||
       
   106 		interval2.Int() > interval ||
       
   107 		interval3.Int() > interval)
       
   108 		{
       
   109 		// Looks like the timestamp algorithm is bad.
       
   110 		INFO_PRINTF1(_L("Failed! Clock appears to be running far too fast!"));
       
   111 		SetTestStepResult(EFail);
       
   112 		return TestStepResult();
       
   113 		}
       
   114 		
       
   115 	// now, check the random portion of the data... 
       
   116 	// just make sure it isn't all the same string...
       
   117 	
       
   118 	TPtrC8 ptr1 = random1.Mid(4);
       
   119 	TPtrC8 ptr2 = random2.Mid(4);
       
   120 	TPtrC8 ptr3 = random3.Mid(4);
       
   121 	
       
   122 	if (ptr1 == ptr2 || ptr2 == ptr3 || ptr3 == ptr1)
       
   123 		{
       
   124 		INFO_PRINTF1(_L("Failed! Random data isn't actually random!"));
       
   125 		SetTestStepResult(EFail);
       
   126 		return TestStepResult();
       
   127 		}
       
   128 		
       
   129 	INFO_PRINTF1(_L("Test step passed."));
       
   130 	SetTestStepResult(EPass);
       
   131 	return TestStepResult();
       
   132 	}
       
   133 	
       
   134 TUint CGetRandomStep::GetTimestamp(const TDesC8& aRandom)
       
   135 	{
       
   136 	TUint ret = aRandom[0];
       
   137 	for (TInt i = 1; i < 4; ++i)
       
   138 		{
       
   139 		ret <<= 8;
       
   140 		ret += aRandom[i];
       
   141 		}
       
   142 	return ret;
       
   143 	}