crypto/weakcryptospi/test/trandom/t_random.cpp
branchRCL_3
changeset 41 9b5a3a9fddf8
parent 8 35751d3474b7
equal deleted inserted replaced
37:721a5e5fe251 41:9b5a3a9fddf8
    25 #include <e32std.h>
    25 #include <e32std.h>
    26 #include <e32test.h>
    26 #include <e32test.h>
    27 #include <random.h>
    27 #include <random.h>
    28 #include <f32file.h>
    28 #include <f32file.h>
    29 
    29 
    30 RTest test(_L("Random Number Generator Tests"));
    30 RTest test(_L("Random Number Generator"));
    31 
    31 
       
    32 TInt gTestPassCount = 0;
       
    33 TInt gTestRequestCount = 10;
    32 TInt gNumberOfRandomNumbers=10000;
    34 TInt gNumberOfRandomNumbers=10000;
    33 
    35 
    34 /** Wraps a console and logs output to a file. */
    36 /** Wraps a console and logs output to a file. */
    35 class CTestConsole:public CConsoleBase
    37 class CTestConsole:public CConsoleBase
    36 	{
    38 	{
   299 	}
   301 	}
   300 
   302 
   301 void DoTestsL(void)
   303 void DoTestsL(void)
   302 
   304 
   303 	{
   305 	{
   304 	test.Printf(_L("  Run random tests with normal salting\r\n"));
   306 	test.Printf(_L("Run random tests with normal salting\r\n"));    
   305     
       
   306 	TInt i;
   307 	TInt i;
   307 	TBuf8<16> buf2;
   308 	TBuf8<16> buf2;
   308 	for (i=0;i<16;i++)
   309 	for (i=0;i<16;i++)
   309 		{
   310 		{
   310 		buf2.SetLength(i);
   311 		buf2.SetLength(i);
   311 		TRandom::RandomL(buf2);
   312 		TRandom::RandomL(buf2);
   312 		}
   313 		}
       
   314 	
   313 	HBufC8* buf=HBufC8::NewMaxL(gNumberOfRandomNumbers);
   315 	HBufC8* buf=HBufC8::NewMaxL(gNumberOfRandomNumbers);
       
   316 	CleanupStack::PushL(buf);
   314 	TPtr8 buffer=buf->Des();
   317 	TPtr8 buffer=buf->Des();
   315 	for (i=0;i<11;i++)
   318 	test.Printf(_L("\nRequesting for random numbers.\n"));
   316 		{
   319 	for (i=0;i<gTestRequestCount;i++)
   317 		User::After(10000000);
   320 		{
   318 		TPtr8 thePtr(buf->Des());
   321 		TPtr8 thePtr(buf->Des());		
   319 		thePtr.FillZ();
   322 		//	Generate the random data	
   320 	//	Generate the random data	
       
   321 		TRandom::RandomL(buffer);
   323 		TRandom::RandomL(buffer);
   322 		if (buf->Length()!=gNumberOfRandomNumbers)
   324 		if (buf->Length()!=gNumberOfRandomNumbers)
       
   325 		    {
   323 			User::Leave(KErrGeneral);
   326 			User::Leave(KErrGeneral);
   324 
   327 		    }
   325 
   328 		++gTestPassCount;
   326 		WriteFile(buffer.Ptr(),_L("User.rnd"));
   329 	    test.Printf(_L("."));
   327 		test.Printf(_L("."));
   330 		}	
   328 		}
   331 	
   329 	delete buf;
   332 	// Request for Secure Random numbers.
       
   333 	test.Printf(_L("\nRequesting for cryptographically secure random numbers.\n"));
       
   334     for (i=0;i<gTestRequestCount;i++)
       
   335         {
       
   336         TPtr8 thePtr(buf->Des());        
       
   337 		// Generate the random data    
       
   338         TRAP_IGNORE(TRandom::SecureRandomL(buffer));
       
   339         if (buf->Length() != gNumberOfRandomNumbers)
       
   340             {
       
   341             User::Leave(KErrGeneral);
       
   342             }
       
   343         ++gTestPassCount;
       
   344         test.Printf(_L("."));
       
   345         }
       
   346 	CleanupStack::PopAndDestroy(buf);
   330 	}
   347 	}
   331 
   348 
   332 void TestsL(void)
   349 void TestsL(void)
   333 	{
   350 	{
   334 	TDriveUnit sysDrive (RFs::GetSystemDrive());
   351 	TDriveUnit sysDrive (RFs::GetSystemDrive());
   336 	logFile.Append(_L("\\t_random.log"));
   353 	logFile.Append(_L("\\t_random.log"));
   337 	CTestConsole* con = CTestConsole::NewL(test.Console(), logFile);
   354 	CTestConsole* con = CTestConsole::NewL(test.Console(), logFile);
   338 	test.SetConsole(con);
   355 	test.SetConsole(con);
   339 	
   356 	
   340 	DoTestsL();
   357 	DoTestsL();
   341 	
       
   342 	//	If test reached here, no tests failed, otherwise it would have panicked
       
   343 	//	and terminated prematurely. Print this out for tester's reference.	
       
   344 	test.Printf(_L("\n0 tests failed out of 11\r\n"));
       
   345 	}
   358 	}
   346 
   359 
   347 GLDEF_C TInt E32Main(void)
   360 GLDEF_C TInt E32Main(void)
   348 
   361 
   349 	{
   362 	{
   353 	__UHEAP_MARK;
   366 	__UHEAP_MARK;
   354 	
   367 	
   355 	test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-RANDOM-0001 Starting random number generator tests\r\n"));
   368 	test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-RANDOM-0001 Starting random number generator tests\r\n"));
   356 	CConsoleBase* originalConsole = test.Console();
   369 	CConsoleBase* originalConsole = test.Console();
   357 
   370 
   358 	TRAPD(ret,TestsL());
   371 	TRAPD(ret,TestsL());   
   359 	if (ret)
   372     // Infor the user about the test cases' status.
   360 		{
   373     test.Printf(_L("\n%d tests failed out of %d\r\n"), ((2*gTestRequestCount) - gTestPassCount), 2*gTestRequestCount);
   361 		test.Printf(_L("Unexpected leave\r\n"));
   374     
   362 		// Print something to let the build system know we failed
   375     test (ret == KErrNone);
   363 		test.Printf(_L("\n1 tests failed out of 11\r\n"));
       
   364 		}	
       
   365 	test.End();
   376 	test.End();
   366 
   377 
   367 	if (test.Console() != originalConsole)
   378 	if (test.Console() != originalConsole)
   368 		{
   379 		{
   369 		delete test.Console();
   380 		delete test.Console();