Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * (c) 1999 Symbian Ltd
32 RTest test(_L("Random Number Generator Tests"));
34 TInt gNumberOfRandomNumbers=10000;
36 /** Wraps a console and logs output to a file. */
37 class CTestConsole:public CConsoleBase
40 static CTestConsole* NewL(CConsoleBase* aCon, const TDesC& aFilename);
41 TInt Create(const TDesC16& aTitle,TSize aSize) {return iCon->Create(aTitle,aSize);};
42 void Read(TRequestStatus& aStatus) {iCon->Read(aStatus);};
43 void ReadCancel(void) {iCon->ReadCancel();};
44 void Write(const TDesC16& aString);
45 TPoint CursorPos(void) const {return iCon->CursorPos();};
46 void SetCursorPosAbs(const TPoint& aPos) {iCon->SetCursorPosAbs(aPos);};
47 void SetCursorPosRel(const TPoint& aPos) {iCon->SetCursorPosRel(aPos);};
48 void SetCursorHeight(TInt aHeight) {iCon->SetCursorHeight(aHeight);};
49 void SetTitle(const TDesC16& aTitle) {iCon->SetTitle(aTitle);};
50 void ClearScreen(void) {iCon->ClearScreen();};
51 void ClearToEndOfLine(void) {iCon->ClearToEndOfLine();};
52 TSize ScreenSize(void) const {return iCon->ScreenSize();};
53 TKeyCode KeyCode(void) const {return iCon->KeyCode();};
54 TUint KeyModifiers(void) const {return iCon->KeyModifiers();};
57 CTestConsole(CConsoleBase* aCon);
58 void ConstructL(const TDesC& aFilename);
59 CConsoleBase* iCon; ///< Pointer to wrapped console, we don't own this
61 RFile iFile; ///< Log file
64 CTestConsole* CTestConsole::NewL(CConsoleBase* aCon, const TDesC& aFilename)
68 self=new (ELeave) CTestConsole(aCon);
69 CleanupStack::PushL(self);
70 self->ConstructL(aFilename);
71 CleanupStack::Pop(self);
75 CTestConsole::CTestConsole(CConsoleBase* aCon) :
76 CConsoleBase(), iCon(aCon)
81 void CTestConsole::ConstructL(const TDesC& aFilename)
84 User::LeaveIfError(iFs.Connect());
85 User::LeaveIfError(iFile.Replace(iFs,aFilename,EFileShareAny|EFileWrite));
88 CTestConsole::~CTestConsole(void)
95 void CTestConsole::Write(const TDesC16& aString)
100 TPtr8 ptr(space,200);
105 void Monobit(const TUint8* aData)
108 const TInt bitcount[256]=
109 { 0,1,1,2,1,2,2,3, // 00-07
110 1,2,2,3,2,3,3,4, // 08-0f
111 1,2,2,3,2,3,3,4, // 10-17
112 2,3,3,4,3,4,4,5, // 18-1f
113 1,2,2,3,2,3,3,4, // 20-27
114 2,3,3,4,3,4,4,5, // 28-2f
115 2,3,3,4,3,4,4,5, // 30-37
116 3,4,4,5,4,5,5,6, // 38-3f
118 1,2,2,3,2,3,3,4, // 40-47
119 2,3,3,4,3,4,4,5, // 48-4f
120 2,3,3,4,3,4,4,5, // 50-57
121 3,4,4,5,4,5,5,6, // 58-5f
122 2,3,3,4,3,4,4,5, // 60-67
123 3,4,4,5,4,5,5,6, // 68-6f
124 3,4,4,5,4,5,5,6, // 70-77
125 4,5,5,6,5,6,6,7, // 78-7f
127 1,2,2,3,2,3,3,4, // 80-87
128 2,3,3,4,3,4,4,5, // 88-8f
129 2,3,3,4,3,4,4,5, // 90-97
130 3,4,4,5,4,5,5,6, // 98-9f
131 2,3,3,4,3,4,4,5, // a0-a7
132 3,4,4,5,4,5,5,6, // a8-af
133 3,4,4,5,4,5,5,6, // b0-b7
134 4,5,5,6,5,6,6,7, // b8-bf
136 2,3,3,4,3,4,4,5, // c0-c7
137 3,4,4,5,4,5,5,6, // c8-cf
138 3,4,4,5,4,5,5,6, // d0-d7
139 4,5,5,6,5,6,6,7, // d8-df
140 3,4,4,5,4,5,5,6, // e0-e7
141 4,5,5,6,5,6,6,7, // e8-ef
142 4,5,5,6,5,6,6,7, // f0-f7
143 5,6,6,7,6,7,7,8 // f8-ff
149 total+=bitcount[aData[i]];
151 test.Printf(_L(" Total bitcount %d\r\n"),total);
152 if ((total>9654)&&(total<10346))
154 test.Printf(_L(" Passed Monobit\r\n"));
158 test.Printf(_L(" ***FAILED!\r\n"));
159 User::Panic(_L("t_random.exe"), KErrGeneral);
163 void Poker(const TUint8* aData)
166 TInt f[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
170 f[(aData[i]&0x0f)]++;
171 f[((aData[i]&0xf0)>>4)]++;
181 if ((x>1.03)&&(x<57.4))
183 test.Printf(_L(" Passed poker test\r\n"));
187 test.Printf(_L(" ***FAILED poker test\r\n"));
188 User::Panic(_L("t_random.exe"), KErrGeneral);
192 void Runs(const TUint8* aData)
211 for (bit=0;bit<8;bit++)
213 if (((aData[i]>>bit)&1)==lastbit)
221 count[thisrun][lastbit]++;
248 if (!((count[i][0]>bound[i][0])&&(count[i][0]<bound[i][1])))
250 test.Printf(_L(" ***FAILED runs test\r\n"));
253 if (!((count[i][1]>bound[i][0])&&(count[i][1]<bound[i][1])))
255 test.Printf(_L(" ***FAILED runs test\r\n"));
261 test.Printf(_L(" Passed runs test\r\n"));
263 if ( (longrun>34) || (failed) )
265 test.Printf(_L(" ***FAILED longrun test\r\n"));
266 User::Panic(_L("t_random.exe"), KErrGeneral);
270 void FIPSTest(const TUint8* aData)
271 // Run some basic tests to check it's returned some numbers
272 // These will panic if a failure is detected
279 void WriteFile(const TUint8* aData,const TDesC& aFileName)
286 err=file.Open(fs,aFileName,EFileShareAny|EFileWrite);
289 if (file.Create(fs,aFileName,EFileShareAny|EFileWrite))
294 TPtrC8 ptr(aData,gNumberOfRandomNumbers);
297 file.Write(size,ptr);
303 class RTestRandomSession : public RRandomSession
306 void SendMalformedInputL()
308 test.Printf(_L("Test malformed input with negative buffer size\r\n"));
310 TInt err = SendReceive(CRandomSession::KRandomRequest, TIpcArgs(&buffer, -1));
311 if (err != KErrArgument)
313 test.Printf(_L("%d should have been returned on negative buffer size test, but %d was returned!\r\n"), KErrArgument, err);
314 User::Leave(KErrGeneral);
319 // Checks that RandomServer handles malformed length correctly - see INC113902
320 void TestMalformedInputL()
322 RTestRandomSession rs;
323 TRAPD(err, rs.ConnectL());
324 User::LeaveIfError(err); // The connect method leaves with zero even if it succeeds, so we have to trap the error
325 CleanupClosePushL(rs);
326 rs.SendMalformedInputL();
327 CleanupStack::PopAndDestroy(&rs);
333 TestMalformedInputL();
335 test.Printf(_L(" Run random tests with normal salting\r\n"));
342 TRandom::RandomL(buf2);
344 HBufC8* buf=HBufC8::NewMaxL(gNumberOfRandomNumbers);
345 TPtr8 buffer=buf->Des();
348 User::After(10000000);
349 TPtr8 thePtr(buf->Des());
351 // Generate the random data
352 TRandom::RandomL(buffer);
353 if (buf->Length()!=gNumberOfRandomNumbers)
354 User::Leave(KErrGeneral);
357 WriteFile(buffer.Ptr(),_L("User.rnd"));
358 test.Printf(_L("."));
365 TDriveUnit sysDrive (RFs::GetSystemDrive());
366 TDriveName driveName(sysDrive.Name());
367 TBuf<64> logFile (driveName);
368 logFile.Append(_L("\\t_random.log"));
369 CTestConsole* con = CTestConsole::NewL(test.Console(), logFile);
370 test.SetConsole(con);
374 // If test reached here, no tests failed, otherwise it would have panicked
375 // and terminated prematurely. Print this out for tester's reference.
376 test.Printf(_L("\n0 tests failed out of 12\r\n"));
379 GLDEF_C TInt E32Main(void)
382 CTrapCleanup* cleanup;
383 cleanup=CTrapCleanup::New();
387 test.Start(_L(" SYMTestCaseID:SEC-CRYPTO-RANDOM-0001 Starting random number generator tests\r\n"));
388 CConsoleBase* originalConsole = test.Console();
393 test.Printf(_L("Unexpected leave\r\n"));
394 // Print something to let the build system know we failed
395 test.Printf(_L("\n1 tests failed out of 11\r\n"));
399 if (test.Console() != originalConsole)
401 delete test.Console();
402 test.SetConsole(originalConsole);