kerneltest/e32test/random/t_entropysources.cpp
branchRCL_3
changeset 20 597aaf25e343
equal deleted inserted replaced
19:4a8fed1c0ef6 20:597aaf25e343
       
     1 // Copyright (c) 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 the License "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 // Test suite to check whether the device has good enough entropy sources or not.
       
    15 // More the no. of sources, high the quality of the random numbers (least predictable).
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalTechnology
       
    22 */
       
    23 
       
    24 #define __E32TEST_EXTENSION__
       
    25 #include <e32test.h>
       
    26 #include <hal.h>
       
    27 #include <e32math.h>
       
    28 #include "d_entropysources.h"
       
    29 
       
    30 //---------------------------------------------------------------------------------------------------------------------
       
    31 //! @SYMTestCaseID				KBASE-entropysources-2703
       
    32 //! @SYMTestType				UT
       
    33 //! @SYMTestCaseDesc			Verifies that entropy is contributed to the Secure RNG
       
    34 //! @SYMPREQ					PREQ211
       
    35 //! @SYMTestPriority			High
       
    36 //! @SYMTestActions				
       
    37 //! 	1. 	TestReseed: tests that the interval between RNG reseeds is less than KMaxReseedTime, unless the platform is 
       
    38 //!         known not to have a viable entropy source.
       
    39 //! 		
       
    40 //! 
       
    41 //! @SYMTestExpectedResults
       
    42 //! 	1.	Properties checked:
       
    43 //! 		1) checks that there is a valid entropy source contrbuting entropy data.
       
    44 //!         2) checks that the entropy collection framework is functioning correctly..
       
    45 //! 	
       
    46 //---------------------------------------------------------------------------------------------------------------------
       
    47 
       
    48 // In a worst case scenario, we expect the reseed to happen within ten minutes.
       
    49 // This is approximately 1/10 of the actual reseed intervel time (97.8 rounded to => 98).
       
    50 // ((2^24 (reseed intervel) * 350 (micro seconds per request)) / 1000000) / 60 => 97.8 minutes.
       
    51 //
       
    52 const TTimeIntervalMicroSeconds32 KMaxReseedTime = 10 * 60 * 1000000;
       
    53 
       
    54 REntropySources Ldd;
       
    55 
       
    56 LOCAL_D RTest test(_L("T_ENTROPYSOURCES"));
       
    57 
       
    58 LOCAL_C void TestReseed()
       
    59     {    
       
    60     test.Next(_L("Loading test driver"));
       
    61     TInt r = User::LoadLogicalDevice(KEntropySourcesName);
       
    62     test(r==KErrNone || r==KErrAlreadyExists);
       
    63     
       
    64     test.Next(_L("Opening the logical channel"));
       
    65     test_KErrNone(Ldd.Open());
       
    66     
       
    67 	RTimer timer;
       
    68 	test_KErrNone(timer.CreateLocal());
       
    69 
       
    70 	test.Next(_L("Testing reseed interval"));
       
    71 	// Wait for two reseeds, so we can prove the interval between them is ok
       
    72 	for (TInt i=0; i<2; ++i)
       
    73 		{
       
    74 		// Request notification of the next reseed, with a timeout
       
    75 		test.Next(_L("Requesting reseed notification"));
       
    76 		TRequestStatus reseed, timeout;
       
    77 		Ldd.ReseedTest(reseed);
       
    78 		timer.After(timeout, KMaxReseedTime);
       
    79 
       
    80 		// Prod the RNG to make sure it's not idle, then wait for an event
       
    81 		test.Next(_L("Prod RNG and wait"));
       
    82 		Math::Random();
       
    83 		User::WaitForRequest(reseed, timeout);
       
    84 
       
    85 		// Check we didn't time out and cancel the timer
       
    86 		test_Equal(KRequestPending, timeout.Int());
       
    87 		timer.Cancel();
       
    88 		User::WaitForRequest(timeout);
       
    89 		test_Equal(KErrCancel, timeout.Int());
       
    90 		test_KErrNone(reseed.Int());
       
    91 		}
       
    92 	
       
    93 	Ldd.Close();
       
    94 
       
    95 	User::FreeLogicalDevice(KEntropySourcesName);
       
    96     }
       
    97 
       
    98 LOCAL_C TBool HardwareRNGPresent()
       
    99     {
       
   100     TInt muid = 0;
       
   101     const TInt r = HAL::Get(HAL::EMachineUid, muid);
       
   102     if (r != KErrNone) return EFalse;;
       
   103     return ((muid != HAL::EMachineUid_X86PC) &&
       
   104             (muid != HAL::EMachineUid_NE1_TB) &&
       
   105             (muid != HAL::EMachineUid_OmapH6) &&
       
   106             (muid != HAL::EMachineUid_OmapZoom) &&
       
   107             (muid != HAL::EMachineUid_Win32Emulator));
       
   108     }
       
   109 
       
   110 GLDEF_C TInt E32Main()
       
   111 	{
       
   112 	test.Title();
       
   113 	test.Start(_L("Test periodic entropy source"));
       
   114 
       
   115 	if(HardwareRNGPresent())	
       
   116 	    {
       
   117 	    __UHEAP_MARK;
       
   118         
       
   119 		TestReseed();
       
   120 
       
   121         __UHEAP_MARKEND;	    
       
   122 	    }
       
   123 	else
       
   124 	    {
       
   125 	    test.Printf(_L("Test skipped, platform is known not to have a periodic entropy source\n"));
       
   126 	    }
       
   127 	test.End();
       
   128 	test.Close();
       
   129 	
       
   130 	return 0;
       
   131 	}