Author: spouliot
Date: 2006-06-01 09:47:43 -0400 (Thu, 01 Jun 2006)
New Revision: 61375

Modified:
   trunk/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
   
trunk/mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs
Log:
2006-06-01  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * RandomNumberGeneratorTest.cs: Don't forget the last run in Runs. 
        Track both 0 and 1 runs. Changed runs ranges to match FIPS140-2 Change
        Notice #1 (fixes bug #78560). Updated test cases to NUnit 2.2 format.



Modified: trunk/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog  
2006-06-01 13:44:15 UTC (rev 61374)
+++ trunk/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog  
2006-06-01 13:47:43 UTC (rev 61375)
@@ -1,3 +1,9 @@
+2006-06-01  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * RandomNumberGeneratorTest.cs: Don't forget the last run in Runs. 
+       Track both 0 and 1 runs. Changed runs ranges to match FIPS140-2 Change
+       Notice #1 (fixes bug #78560). Updated test cases to NUnit 2.2 format.
+
 2005-12-15  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * RijndaelTest.cs: Add test cases for cycling key sizes and block 

Modified: 
trunk/mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs
===================================================================
--- 
trunk/mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs
       2006-06-01 13:44:15 UTC (rev 61374)
+++ 
trunk/mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs
       2006-06-01 13:47:43 UTC (rev 61375)
@@ -2,9 +2,10 @@
 // RandomNumberGeneratorTest.cs - NUnit Test Cases for RNG
 //
 // Author:
-//             Sebastien Pouliot ([EMAIL PROTECTED])
+//     Sebastien Pouliot  <[EMAIL PROTECTED]>
 //
 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
 //
 
 using NUnit.Framework;
@@ -25,23 +26,26 @@
 //     not implemented
 //     http://www.ietf.org/rfc/rfc1750.txt
 
-public class RandomNumberGeneratorTest : TestCase {
-       protected RandomNumberGenerator rng;
+[TestFixture]
+public class RandomNumberGeneratorTest {
 
-       protected override void SetUp () 
-       {
-               rng = RandomNumberGenerator.Create();
-       }
+       private string name;
+       private byte[] sample;
 
-       protected override void TearDown () {}
-
-       public void AssertEquals (string msg, byte[] array1, byte[] array2) 
+       [TestFixtureSetUp]
+       public void SetUp () 
        {
-               AllTests.AssertEquals (msg, array1, array2);
+               // all tests should be done on the same random sample
+               RandomNumberGenerator rng = RandomNumberGenerator.Create ();
+               name = rng.ToString ();
+               // 20,000 bits
+               sample = new byte[2500];
+               rng.GetBytes (sample);
        }
 
        // count the number of 1
-       protected void Monobit (string rngName, byte[] sample) 
+       [Test]
+       public void Monobit () 
        {
                int x = 0;
                for (int i=0; i < sample.Length; i++) {
@@ -53,11 +57,13 @@
                                b >>= 1;
                        }
                }
-               Assert (rngName + " Monobit x=" + x, ((9725 < x) && (x < 
10275)));
+               Assert.IsTrue ((9725  < x), String.Format ("{0} Monobit x={1} > 
9725",  name, x));
+               Assert.IsTrue ((x < 10275), String.Format ("{0} Monobit x={1} < 
10275", name, x));
        }
 
        // 16 patterns (nibbles)
-       protected void Poker (string rngName, byte[] sample) 
+       [Test]
+       public void Poker () 
        {
                int[] pattern = new int[16];
                for (int i = 0; i < sample.Length; i++) {
@@ -72,13 +78,15 @@
                for (int i = 0; i < 16; i++)
                        result += (pattern[i] * pattern[i]);
                result = ((16 * result) / 5000) - 5000;
-               Assert (rngName + " Poker: " + result, ((result > 2.16) && 
(result < 46.17)));
+
+               Assert.IsTrue (((result > 2.16) && (result < 46.17)), name + " 
Poker: " + result);
        }
 
        // runs of 1 (or 0)
-       protected void Runs (string rngName, byte[] sample) 
+       [Test]
+       public void Runs () 
        {
-               int[] runs = new int[6];
+               int[,] runs = new int[6,2];
                int x = 0;
                bool one = false;
                bool zero = false;
@@ -91,7 +99,7 @@
                                                zero = false;
                                                int p = Math.Min (x, 6) - 1;
                                                if (p >= 0)
-                                                       runs[p]++;
+                                                       runs[p,0]++;
                                                x = 0;
                                        }
                                }
@@ -99,9 +107,9 @@
                                        if (!zero) {
                                                one = false;
                                                zero = true;
-                                               /*int p = Math.Min (x, 6) - 1;
+                                               int p = Math.Min (x, 6) - 1;
                                                if (p >= 0)
-                                                       runs[p]++;*/
+                                                       runs[p,1]++;
                                                x = 0;
                                        }
                                }
@@ -110,16 +118,32 @@
                                b >>= 1;
                        }
                }
-               Assert (rngName + " Runs length=1: " + runs[0], ((runs[0] >= 
2343) && (runs[0] <= 2657)));
-               Assert (rngName + " Runs length=2: " + runs[1], ((runs[1] >= 
1135) && (runs[1] <= 1365)));
-               Assert (rngName + " Runs length=3: " + runs[2], ((runs[2] >=  
542) && (runs[2] <= 708)));
-               Assert (rngName + " Runs length=4: " + runs[3], ((runs[3] >=  
251) && (runs[3] <= 373)));
-               Assert (rngName + " Runs length=5: " + runs[4], ((runs[4] >=  
111) && (runs[4] <= 201)));
-               Assert (rngName + " Runs length=6+ " + runs[5], ((runs[5] >=  
111) && (runs[5] <= 201)));
+               // don't forget the ast run
+               if (x > 0) {
+                       int p = Math.Min (x, 6) - 1;
+                       if (p >= 0)
+                               runs [p, zero ? 0 : 1]++;
+               }
+               // Updated ranges as per FIPS140-2 Change Notice #1
+               // check for runs of zeros
+               Assert.IsTrue (((runs[0,0] >= 2315) && (runs[0,0] <= 2685)), 
name + " 0-Runs length=1: " + runs[0,0]);
+               Assert.IsTrue (((runs[1,0] >= 1114) && (runs[1,0] <= 1386)), 
name + " 0-Runs length=2: " + runs[1,0]);
+               Assert.IsTrue (((runs[2,0] >=  527) && (runs[2,0] <=  723)), 
name + " 0-Runs length=3: " + runs[2,0]);
+               Assert.IsTrue (((runs[3,0] >=  240) && (runs[3,0] <=  384)), 
name + " 0-Runs length=4: " + runs[3,0]);
+               Assert.IsTrue (((runs[4,0] >=  103) && (runs[4,0] <=  209)), 
name + " 0-Runs length=5: " + runs[4,0]);
+               Assert.IsTrue (((runs[5,0] >=  103) && (runs[5,0] <=  209)), 
name + " 0-Runs length=6+ " + runs[5,0]);
+               // check for runs of ones
+               Assert.IsTrue (((runs[0,1] >= 2315) && (runs[0,1] <= 2685)), 
name + " 1-Runs length=1: " + runs[0,1]);
+               Assert.IsTrue (((runs[1,1] >= 1114) && (runs[1,1] <= 1386)), 
name + " 1-Runs length=2: " + runs[1,1]);
+               Assert.IsTrue (((runs[2,1] >=  527) && (runs[2,1] <=  723)), 
name + " 1-Runs length=3: " + runs[2,1]);
+               Assert.IsTrue (((runs[3,1] >=  240) && (runs[3,1] <=  384)), 
name + " 1-Runs length=4: " + runs[3,1]);
+               Assert.IsTrue (((runs[4,1] >=  103) && (runs[4,1] <=  209)), 
name + " 1-Runs length=5: " + runs[4,1]);
+               Assert.IsTrue (((runs[5,1] >=  103) && (runs[5,1] <=  209)), 
name + " 1-Runs length=6+ " + runs[5,1]);
        }
 
        // no long runs of 26 or more (0 or 1)
-       protected void LongRuns (string rngName, byte[] sample) 
+       [Test]
+       public void LongRuns () 
        {
                int longestRun = 0;
                int currentRun = 0;
@@ -150,22 +174,8 @@
                                b >>= 1;
                        }
                }
-               Assert (rngName + " Long Runs max = " + longestRun, (longestRun 
< 26));
+               Assert.IsTrue ((longestRun < 26), name + " Long Runs max = " + 
longestRun);
        }
+}
 
-       // all tests should be done on the same random sample
-       public void TestFIPS140() 
-       {
-               string name = rng.ToString();
-               // 20,000 bits
-               byte[] sample = new byte[2500];
-               rng.GetBytes (sample);
-
-               Monobit (name, sample);
-               Poker (name, sample);
-               Runs (name, sample);
-               LongRuns (name, sample);
-       }
 }
-
-}
\ No newline at end of file

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to