kerneltest/e32test/random/d_entropysources.cpp
branchRCL_3
changeset 80 597aaf25e343
equal deleted inserted replaced
62:4a8fed1c0ef6 80: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 // e32test\entropysources\d_entropysources.cpp
       
    15 // 
       
    16 //
       
    17 /**
       
    18  @file
       
    19  @internalComponent
       
    20  @test
       
    21 */
       
    22 #include <dfcs.h>
       
    23 #include <kernel/kernel.h>
       
    24 #include <kernel/kern_priv.h>
       
    25 #include "kern_test.h"
       
    26 #include "d_entropysources.h"
       
    27 
       
    28 //---------------------------------------------------------------------------------------------------------------------
       
    29 //! @SYMTestCaseID				KBASE-entropysources-2703
       
    30 //! @SYMTestType				UT
       
    31 //! @SYMTestCaseDesc			Verifies that entropy is contributed to the Secure RNG
       
    32 //! @SYMPREQ					PREQ211
       
    33 //! @SYMTestPriority			High
       
    34 //! @SYMTestActions				
       
    35 //! 	1. 	TestReseed: tests that the interval between RNG reseeds is less than KMaxReseedTime, unless the platform is 
       
    36 //!         known not to have a viable entropy source.
       
    37 //! 		
       
    38 //! 
       
    39 //! @SYMTestExpectedResults
       
    40 //! 	1.	Properties checked:
       
    41 //! 		1) checks that there is a valid entropy source contrbuting entropy data.
       
    42 //!         2) checks that the entropy collection framework is functioning correctly..
       
    43 //! 	
       
    44 //---------------------------------------------------------------------------------------------------------------------
       
    45 
       
    46 class DEntropySourcesFactory : public DLogicalDevice
       
    47     {
       
    48 public:
       
    49     DEntropySourcesFactory();
       
    50     virtual TInt Install();    
       
    51     virtual void GetCaps(TDes8 &aDes) const;
       
    52     virtual TInt Create(DLogicalChannelBase*& aChannel); 
       
    53     };
       
    54 
       
    55 class DEntropySources : public DLogicalChannelBase
       
    56     {
       
    57 public:
       
    58  	DEntropySources();
       
    59    ~DEntropySources();    
       
    60     void ReseedHook();
       
    61     
       
    62 protected:    
       
    63 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
       
    64 	virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
       
    65 
       
    66 private:
       
    67     DThread*        iClient;
       
    68     TClientRequest* iRequest;    
       
    69     };
       
    70 
       
    71 // Function to be called from the kernel side code.
       
    72 void ReseedHook(TAny* aPtr)
       
    73     {
       
    74 	((DEntropySources*)aPtr)->ReseedHook();
       
    75     }
       
    76 
       
    77 DECLARE_STANDARD_LDD()
       
    78     {
       
    79      return new DEntropySourcesFactory;
       
    80     }
       
    81 
       
    82 //
       
    83 // DEntropySourcesFactory
       
    84 //
       
    85 
       
    86 DEntropySourcesFactory::DEntropySourcesFactory()
       
    87     {
       
    88     // Set version number for this device
       
    89     iVersion = TVersion(0,1,1);
       
    90 
       
    91     // Indicate we don't support units or a PDD
       
    92     iParseMask = 0;
       
    93     }
       
    94 
       
    95 TInt DEntropySourcesFactory::Install()
       
    96     {
       
    97     return(SetName(&KEntropySourcesName));
       
    98     }
       
    99 
       
   100 void DEntropySourcesFactory::GetCaps(TDes8& aDes) const
       
   101     {
       
   102     // Create a capabilities object
       
   103     TCapsEntropySources caps;
       
   104     caps.iVersion = iVersion;
       
   105 
       
   106     // Write it back to user memory
       
   107     Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
       
   108     }
       
   109 
       
   110 TInt DEntropySourcesFactory::Create(DLogicalChannelBase*& aChannel)
       
   111     {
       
   112     aChannel = new DEntropySources;
       
   113     if(!aChannel)
       
   114         return KErrNoMemory;
       
   115     return KErrNone;
       
   116     }
       
   117 
       
   118 //
       
   119 // DEntropySources
       
   120 //
       
   121 
       
   122 DEntropySources::DEntropySources()
       
   123 	{
       
   124     iClient = &Kern::CurrentThread();
       
   125     iClient->Open();
       
   126 	}
       
   127 
       
   128 DEntropySources::~DEntropySources()
       
   129     {
       
   130 	KernTest::Test(KernTest::ERNGReseedHook, NULL, NULL);
       
   131     Kern::SafeClose((DObject*&)iClient, NULL);
       
   132 	Kern::DestroyClientRequest(iRequest);
       
   133     }
       
   134 
       
   135 TInt DEntropySources::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
       
   136 	{
       
   137 	return Kern::CreateClientRequest(iRequest);
       
   138 	}
       
   139 
       
   140 TInt DEntropySources::Request(TInt aReqNo, TAny* a1, TAny* a2)
       
   141     {
       
   142     (void)a2;
       
   143     TInt r = KErrNotSupported;
       
   144     switch(aReqNo)
       
   145         {
       
   146         case ~REntropySources::EReseedTest:
       
   147             r = iRequest->SetStatus((TRequestStatus*)a1);
       
   148 			if (r!=KErrNone)
       
   149 				return r;
       
   150             KernTest::Test(KernTest::ERNGReseedHook, (TAny*)&::ReseedHook, this);            
       
   151             break;
       
   152         }
       
   153     return r;
       
   154     }
       
   155 
       
   156 void DEntropySources::ReseedHook()
       
   157     {
       
   158 	KernTest::Test(KernTest::ERNGReseedHook, NULL, NULL);
       
   159     Kern::QueueRequestComplete(iClient, iRequest, KErrNone);
       
   160     }