2008-02-21 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Thu, 21 Feb 2008 13:55:49 +0000 (13:55 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Thu, 21 Feb 2008 13:55:49 +0000 (13:55 -0000)
* Rfc2898DeriveBytesTest.cs: Add test cases for consecutive calls to
GetBytes and compare the result to a single, larger, call. Converted
all exitsing tests to NUnit 2.2.

svn path=/trunk/mcs/; revision=96343

mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
mcs/class/corlib/Test/System.Security.Cryptography/Rfc2898DeriveBytesTest.cs

index 7c70f34bcc9ff76a05dea918b8017235e48e6a8e..fd64c32c1bcee42bb2f2814d1cbd1aae239cf36b 100644 (file)
@@ -1,3 +1,9 @@
+2008-02-21  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * Rfc2898DeriveBytesTest.cs: Add test cases for consecutive calls to
+       GetBytes and compare the result to a single, larger, call. Converted 
+       all exitsing tests to NUnit 2.2.
+
 2008-01-02  Sebastien Pouliot  <sebastien@ximian.com>
 
        * RC2CryptoServiceProviderTest.cs: Fix Create[De|En]cryptor_KeyNull 
index 9e626c965d349b612fe1ce39690d5d5001d7939d..d3daf65336fcc8175f8fa53bb23d0d32ea0eaeca 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot  <spouliot@ximian.com>
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004,2008 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -43,12 +43,7 @@ namespace MonoTests.System.Security.Cryptography {
        //      http://www.faqs.org/rfcs/rfc3211.html
 
        [TestFixture]
-       public class Rfc2898DeriveBytesTest : Assertion {
-
-               public void AssertEquals (string msg, byte[] array1, byte[] array2) 
-               {
-                       AllTests.AssertEquals (msg, array1, array2);
-               }
+       public class Rfc2898DeriveBytesTest {
 
                static private byte[] salt = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
 
@@ -56,8 +51,8 @@ namespace MonoTests.System.Security.Cryptography {
                public void ConstructorPasswordSalt () 
                {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
-                       AssertEquals ("IterationCount", 1000, pkcs5.IterationCount);
-                       AssertEquals ("Salt", salt, pkcs5.Salt);
+                       Assert.AreEqual (1000, pkcs5.IterationCount, "IterationCount");
+                       Assert.AreEqual (salt, pkcs5.Salt, "Salt");
                }
 
                [Test]
@@ -78,8 +73,8 @@ namespace MonoTests.System.Security.Cryptography {
                public void ConstructorPasswordSaltIterations () 
                {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
-                       AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
-                       AssertEquals ("Salt", salt, pkcs5.Salt);
+                       Assert.AreEqual (5, pkcs5.IterationCount, "IterationCount");
+                       Assert.AreEqual (salt, pkcs5.Salt, "Salt");
                }
 
                [Test]
@@ -115,8 +110,8 @@ namespace MonoTests.System.Security.Cryptography {
                public void ConstructorPasswordSaltLength () 
                {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8);
-                       AssertEquals ("IterationCount", 1000, pkcs5.IterationCount);
-                       AssertEquals ("Salt", 8, pkcs5.Salt.Length);
+                       Assert.AreEqual (1000, pkcs5.IterationCount, "IterationCount");
+                       Assert.AreEqual (8, pkcs5.Salt.Length, "Salt");
                }
 
                [Test]
@@ -144,8 +139,8 @@ namespace MonoTests.System.Security.Cryptography {
                public void ConstructorPasswordSaltLengthIterations () 
                {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8, 5);
-                       AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
-                       AssertEquals ("Salt", 8, pkcs5.Salt.Length);
+                       Assert.AreEqual (5, pkcs5.IterationCount, "IterationCount");
+                       Assert.AreEqual (8, pkcs5.Salt.Length, "Salt");
                }
 
                [Test]
@@ -187,30 +182,30 @@ namespace MonoTests.System.Security.Cryptography {
                public void IterationCount () 
                {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
-                       AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
+                       Assert.AreEqual (5, pkcs5.IterationCount, "IterationCount");
                        pkcs5.IterationCount = Int32.MaxValue;
-                       AssertEquals ("IterationCount", Int32.MaxValue, pkcs5.IterationCount);
+                       Assert.AreEqual (Int32.MaxValue, pkcs5.IterationCount, "IterationCount");
                }
 
                [Test]
                public void SaltNew () 
                {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
-                       AssertEquals ("Salt", salt, pkcs5.Salt);
+                       Assert.AreEqual (salt, pkcs5.Salt, "Salt");
                        byte[] newSalt = (byte[]) salt.Clone ();
                        newSalt [0] = 0xFF;
                        pkcs5.Salt = newSalt;
-                       AssertEquals ("Salt(new)", newSalt, pkcs5.Salt);
+                       Assert.AreEqual (newSalt, pkcs5.Salt, "Salt(new)");
                }
 
                [Test]
                public void SaltCantModifyInternal () 
                {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
-                       AssertEquals ("Salt", salt, pkcs5.Salt);
+                       Assert.AreEqual (salt, pkcs5.Salt, "Salt");
                        byte[] modSalt = (byte[]) salt.Clone ();
                        modSalt [0] = 0xFF;
-                       Assert ("Can't modify internal salt", (BitConverter.ToString(pkcs5.Salt) != BitConverter.ToString(modSalt)));
+                       Assert.IsFalse ((BitConverter.ToString (pkcs5.Salt) == BitConverter.ToString (modSalt)), "Can't modify internal salt");
                }
 
                [Test]
@@ -237,7 +232,7 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] expected = { 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6 };
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
                        byte[] key = pkcs5.GetBytes (0);
-                       AssertEquals ("GetBytesZero", 0, key.Length);
+                       Assert.AreEqual (0, key.Length, "GetBytesZero");
                }
 
                [Test]
@@ -257,7 +252,7 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] key = pkcs5.GetBytes (32768);
                        // just check the last 32 bytes
                        string actual = BitConverter.ToString (key, key.Length - 32);
-                       AssertEquals ("Endian", expected, actual);
+                       Assert.AreEqual (expected, actual, "Endian");
                }
 
                [Test]
@@ -266,7 +261,7 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] expected = { 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6 };
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
                        byte[] key = pkcs5.GetBytes (8);
-                       AssertEquals ("RFC3211_TC1", expected, key);
+                       Assert.AreEqual (expected, key, "RFC3211_TC1");
                }
 
                [Test]
@@ -275,7 +270,7 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
                        byte[] key = pkcs5.GetBytes (16);
-                       AssertEquals ("RFC3211_TC2", expected, key);
+                       Assert.AreEqual (expected, key, "RFC3211_TC2");
                }
 
                [Test]
@@ -284,7 +279,7 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86, 0x07, 0x12, 0x63, 0x80, 0xcc, 0x47, 0xab, 0x2d, 0xa6, 0xcc, 0xda, 0xfb, 0x26, 0x83, 0xdf, 0xe8 };
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
                        byte[] key = pkcs5.GetBytes (32);
-                       AssertEquals ("RFC3211_TC2_TwoBlocks", expected, key);
+                       Assert.AreEqual (expected, key, "RFC3211_TC2_TwoBlocks");
                }
 
                [Test]
@@ -293,10 +288,10 @@ namespace MonoTests.System.Security.Cryptography {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
                        byte[] key1 = pkcs5.GetBytes (8);
                        byte[] expected_part1 = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
-                       AssertEquals ("RFC3211_TC2_Splitted_OneBlock-1", expected_part1, key1);
+                       Assert.AreEqual (expected_part1, key1, "RFC3211_TC2_Splitted_OneBlock-1");
                        byte[] expected_part2 = { 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
                        byte[] key2 = pkcs5.GetBytes (8);
-                       AssertEquals ("RFC3211_TC2_Splitted_OneBlock-2", expected_part2, key2);
+                       Assert.AreEqual (expected_part2, key2, "RFC3211_TC2_Splitted_OneBlock-2");
                }
 
                [Test]
@@ -305,10 +300,10 @@ namespace MonoTests.System.Security.Cryptography {
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
                        byte[] key1 = pkcs5.GetBytes (16);
                        byte[] expected_part1 = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
-                       AssertEquals ("RFC3211_TC2_Splitted_TwoBlocks-1", expected_part1, key1);
+                       Assert.AreEqual (expected_part1, key1, "RFC3211_TC2_Splitted_TwoBlocks-1");
                        byte[] expected_part2 = { 0x07, 0x12, 0x63, 0x80, 0xcc, 0x47, 0xab, 0x2d, 0xa6, 0xcc, 0xda, 0xfb, 0x26, 0x83, 0xdf, 0xe8 };
                        byte[] key2 = pkcs5.GetBytes (16);
-                       AssertEquals ("RFC3211_TC2_Splitted_TwoBlocks-2", expected_part2, key2);
+                       Assert.AreEqual (expected_part2, key2, "RFC3211_TC2_Splitted_TwoBlocks-2");
                }
 
                [Test]
@@ -317,10 +312,49 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
                        Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
                        byte[] key1 = pkcs5.GetBytes (8);
-                       AssertEquals ("RFC3211_TC2_part1", expected, key1);
+                       Assert.AreEqual (expected, key1, "RFC3211_TC2_part1");
                        pkcs5.Reset ();
                        byte[] key2 = pkcs5.GetBytes (8);
-                       AssertEquals ("RFC3211_TC2_part2", expected, key2);
+                       Assert.AreEqual (expected, key2, "RFC3211_TC2_part2");
+               }
+
+               private void CompareBuffers (byte [] big, byte [] a, byte [] b)
+               {
+                       int n = 0;
+                       for (int i = 0; i < a.Length; i++) {
+                               Assert.AreEqual (big [n], a [i], n.ToString ());
+                               n++;
+                       }
+                       for (int i = 0; i < b.Length; i++) {
+                               Assert.AreEqual (big [n], b [i], n.ToString ());
+                               n++;
+                       }
+               }
+
+               [Test]
+               public void SplitCalls_Reset ()
+               {
+                       Rfc2898DeriveBytes keygen = new Rfc2898DeriveBytes ("password", salt, 12345);
+                       byte [] big = keygen.GetBytes (48);
+
+                       keygen.Reset ();
+                       byte [] a = keygen.GetBytes (16);
+                       byte [] b = keygen.GetBytes (32);
+
+                       CompareBuffers (big, a, b);
+               }
+
+               [Test]
+               public void SplitCalls_TwoInstances ()
+               {
+                       Rfc2898DeriveBytes keygen1 = new Rfc2898DeriveBytes ("password", salt, 12345);
+                       byte [] big = keygen1.GetBytes (48);
+
+                       Rfc2898DeriveBytes keygen2 = new Rfc2898DeriveBytes ("password", salt, 12345);
+                       byte [] a = keygen2.GetBytes (16);
+                       byte [] b = keygen2.GetBytes (32);
+
+                       CompareBuffers (big, a, b);
                }
        }
 }