2006-06-15 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Thu, 15 Jun 2006 19:34:20 +0000 (19:34 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Thu, 15 Jun 2006 19:34:20 +0000 (19:34 -0000)
* HashAlgorithmTest.cs: Added new test cases wrt the output buffer
used in TransformBlock (e.g. bug #78647). Updated existing test case
to work under 2.0 final.
* HMACMD5Test.cs: Fix SetUp to allow new inherited test cases to work
properly.
* HMACRIPEMD160Test.cs: Fix SetUp to allow new inherited test cases to
work properly.
* HMACSHA1Test.cs: Fix SetUp to allow new inherited test cases to
work properly.
* HMACSHA256Test.cs: Fix SetUp to allow new inherited test cases to
work properly.
* HMACSHA384Test.cs: New. Basic test cases that, cheaply, inherits
the HashAlgorithm tests.
* HMACSHA512Test.cs: New. Basic test cases that, cheaply, inherits
the HashAlgorithm tests.
* KeyedHashAlgorithmTest.cs: Ensure we're always using the same key
(required for new, inherited, test cases). Fix ToString to match
KeyedHashAlgorithm (and not an inherited class).

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

mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
mcs/class/corlib/Test/System.Security.Cryptography/HMACMD5Test.cs
mcs/class/corlib/Test/System.Security.Cryptography/HMACRIPEMD160Test.cs
mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA1Test.cs
mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA256Test.cs
mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA384Test.cs [new file with mode: 0644]
mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA512Test.cs [new file with mode: 0644]
mcs/class/corlib/Test/System.Security.Cryptography/HashAlgorithmTest.cs
mcs/class/corlib/Test/System.Security.Cryptography/KeyedHashAlgorithmTest.cs

index bf2d4c307db3719597804edd38901508b8c0b817..6a2cb6e3f2c236d318a78c36709b3a09eec8f550 100644 (file)
@@ -1,3 +1,24 @@
+2006-06-15  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * HashAlgorithmTest.cs: Added new test cases wrt the output buffer 
+       used in TransformBlock (e.g. bug #78647). Updated existing test case
+       to work under 2.0 final.
+       * HMACMD5Test.cs: Fix SetUp to allow new inherited test cases to work 
+       properly.
+       * HMACRIPEMD160Test.cs: Fix SetUp to allow new inherited test cases to
+       work properly.
+       * HMACSHA1Test.cs: Fix SetUp to allow new inherited test cases to 
+       work properly.
+       * HMACSHA256Test.cs: Fix SetUp to allow new inherited test cases to 
+       work properly.
+       * HMACSHA384Test.cs: New. Basic test cases that, cheaply, inherits 
+       the HashAlgorithm tests.
+       * HMACSHA512Test.cs: New. Basic test cases that, cheaply, inherits 
+       the HashAlgorithm tests.
+       * KeyedHashAlgorithmTest.cs: Ensure we're always using the same key 
+       (required for new, inherited, test cases). Fix ToString to match 
+       KeyedHashAlgorithm (and not an inherited class).
+
 2006-06-01  Sebastien Pouliot  <sebastien@ximian.com>
 
        * RandomNumberGeneratorTest.cs: Don't forget the last run in Runs. 
index 288a43f43d013f1b756be5163f55dd25c4ccd809..f2dad4d26725f3043a3a2a8a88f9296c548eac48 100644 (file)
@@ -2,9 +2,10 @@
 // HMACMD5Test.cs - NUnit Test Cases for HMACMD5
 //
 // Author:
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
 //
 
 #if NET_2_0
@@ -26,6 +27,14 @@ namespace MonoTests.System.Security.Cryptography {
 
                protected HMACMD5 algo;
 
+               [SetUp]
+               protected override void SetUp () 
+               {
+                       algo = new HMACMD5 ();
+                       algo.Key = new byte [8];
+                       hash = algo;
+               }
+
                [Test]
                public void Constructors () 
                {
@@ -35,17 +44,13 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] key = new byte [8];
                        algo = new HMACMD5 (key);
                        AssertNotNull ("HMACMD5 (key)", algo);
+               }
 
-                       try {
-                               algo = new HMACMD5 (null);
-                               Fail ("HMACMD5 (null) - Expected NullReferenceException but got none");
-                       }
-                       catch (NullReferenceException) {
-                               // well ArgumentNullException would have been more appropriate
-                       }
-                       catch (Exception e) {
-                               Fail ("HMACMD5 (null) - Expected NullReferenceException but got: " + e.ToString ());
-                       }
+               [Test]
+               [ExpectedException (typeof (NullReferenceException))]
+               public void Constructor_Null () 
+               {
+                       new HMACMD5 (null);
                }
 
                [Test]
@@ -194,4 +199,4 @@ namespace MonoTests.System.Security.Cryptography {
 
 }
 
-#endif
\ No newline at end of file
+#endif
index 4aa1bd7f6c6d793189b61fd31eacae87bc0b369d..1c39730625443663c238c660f1ccd3f98d4d2618 100644 (file)
@@ -6,7 +6,7 @@
 //     Sebastien Pouliot (sebastien@ximian.com)
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2006 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
@@ -39,7 +39,7 @@ using System.Text;
 namespace MonoTests.System.Security.Cryptography {
 
        [TestFixture]
-       public class HMACRIPEMD160Test : Assertion {
+       public class HMACRIPEMD160Test : KeyedHashAlgorithmTest {
 
                protected HMACRIPEMD160 hmac;
 
@@ -70,9 +70,11 @@ namespace MonoTests.System.Security.Cryptography {
                }
 
                [SetUp]
-               public void SetUp () 
+               protected override void SetUp () 
                {
                        hmac = new HMACRIPEMD160 ();
+                       hmac.Key = new byte [8];
+                       hash = hmac;
                }
 
                static byte[] key1 = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x23, 0x45, 0x67 };
index 8d81b49a93c0ad85858737a1f84117babf438771..c52e203c3cdd6dd186698f77a6a7008b5ade4fae 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2006 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
@@ -45,6 +45,13 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
 
        protected HMACSHA1 algo;
 
+       [SetUp]
+       protected override void SetUp () 
+       {
+               hash = HMACSHA1.Create ();
+               (hash as KeyedHashAlgorithm).Key = new byte [8];
+       }
+
        [Test]
        public void Constructors () 
        {
index 4c0eaa5f677e1fb5ae8b3e680fa2e1eca8c018f0..d669f4c9f0c6260f3d4847ef289a4976e8675e27 100644 (file)
@@ -2,9 +2,10 @@
 // HMACSHA256Test.cs - NUnit Test Cases for HMACSHA256
 //
 // Author:
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
 //
 
 #if NET_2_0
@@ -26,6 +27,14 @@ namespace MonoTests.System.Security.Cryptography {
 
                protected HMACSHA256 algo;
 
+               [SetUp]
+               protected override void SetUp () 
+               {
+                       algo = new HMACSHA256 ();
+                       algo.Key = new byte [8];
+                       hash = algo;
+               }
+
                [Test]
                public void Constructors () 
                {
@@ -35,23 +44,18 @@ namespace MonoTests.System.Security.Cryptography {
                        byte[] key = new byte [8];
                        algo = new HMACSHA256 (key);
                        AssertNotNull ("HMACSHA256 (key)", algo);
+               }
 
-                       try {
-                               algo = new HMACSHA256 (null);
-                               Fail ("HMACSHA256 (null) - Expected NullReferenceException but got none");
-                       }
-                       catch (NullReferenceException) {
-                               // well ArgumentNullException would have been more appropriate
-                       }
-                       catch (Exception e) {
-                               Fail ("HMACSHA256 (null) - Expected NullReferenceException but got: " + e.ToString ());
-                       }
+               [Test]
+               [ExpectedException (typeof (NullReferenceException))]
+               public void Constructor_Null () 
+               {
+                       new HMACSHA512 (null);
                }
 
                [Test]
                public void Invariants () 
                {
-                       algo = new HMACSHA256 ();
                        AssertEquals ("HMACSHA256.CanReuseTransform", true, algo.CanReuseTransform);
                        AssertEquals ("HMACSHA256.CanTransformMultipleBlocks", true, algo.CanTransformMultipleBlocks);
                        AssertEquals ("HMACSHA256.HashName", "SHA256", algo.HashName);
@@ -230,4 +234,4 @@ namespace MonoTests.System.Security.Cryptography {
        }
 }
 
-#endif
\ No newline at end of file
+#endif
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA384Test.cs b/mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA384Test.cs
new file mode 100644 (file)
index 0000000..f64c8a0
--- /dev/null
@@ -0,0 +1,67 @@
+//
+// HMACSHA384Test.cs - NUnit Test Cases for HMACSHA384
+//
+// Author:
+//     Sebastien Pouliot  <sebastien@ximian.com>
+//
+// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+using System;
+using System.IO;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace MonoTests.System.Security.Cryptography {
+
+       [TestFixture]
+       public class HMACSHA384Test : KeyedHashAlgorithmTest {
+
+               protected HMACSHA384 algo;
+
+               [SetUp]
+               protected override void SetUp () 
+               {
+                       algo = new HMACSHA384 ();
+                       algo.Key = new byte [8];
+                       hash = algo;
+               }
+
+               [Test]
+               public void Constructors () 
+               {
+                       algo = new HMACSHA384 ();
+                       AssertNotNull ("HMACSHA384 ()", algo);
+
+                       byte[] key = new byte [8];
+                       algo = new HMACSHA384 (key);
+                       AssertNotNull ("HMACSHA384 (key)", algo);
+               }
+
+               [Test]
+               [ExpectedException (typeof (NullReferenceException))]
+               public void Constructor_Null () 
+               {
+                       new HMACSHA384 (null);
+               }
+
+               [Test]
+               public void Invariants () 
+               {
+                       algo = new HMACSHA384 ();
+                       AssertEquals ("HMACSHA384.CanReuseTransform", true, algo.CanReuseTransform);
+                       AssertEquals ("HMACSHA384.CanTransformMultipleBlocks", true, algo.CanTransformMultipleBlocks);
+                       AssertEquals ("HMACSHA384.HashName", "SHA384", algo.HashName);
+                       AssertEquals ("HMACSHA384.HashSize", 384, algo.HashSize);
+                       AssertEquals ("HMACSHA384.InputBlockSize", 1, algo.InputBlockSize);
+                       AssertEquals ("HMACSHA384.OutputBlockSize", 1, algo.OutputBlockSize);
+                       AssertEquals ("HMACSHA384.ToString()", "System.Security.Cryptography.HMACSHA384", algo.ToString ()); 
+               }
+       }
+}
+
+#endif
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA512Test.cs b/mcs/class/corlib/Test/System.Security.Cryptography/HMACSHA512Test.cs
new file mode 100644 (file)
index 0000000..93337d4
--- /dev/null
@@ -0,0 +1,67 @@
+//
+// HMACSHA512Test.cs - NUnit Test Cases for HMACSHA512
+//
+// Author:
+//     Sebastien Pouliot  <sebastien@ximian.com>
+//
+// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+using System;
+using System.IO;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace MonoTests.System.Security.Cryptography {
+
+       [TestFixture]
+       public class HMACSHA512Test : KeyedHashAlgorithmTest {
+
+               protected HMACSHA512 algo;
+
+               [SetUp]
+               protected override void SetUp () 
+               {
+                       algo = new HMACSHA512 ();
+                       algo.Key = new byte [8];
+                       hash = algo;
+               }
+
+               [Test]
+               public void Constructors () 
+               {
+                       algo = new HMACSHA512 ();
+                       AssertNotNull ("HMACSHA512 ()", algo);
+
+                       byte[] key = new byte [8];
+                       algo = new HMACSHA512 (key);
+                       AssertNotNull ("HMACSHA512 (key)", algo);
+               }
+
+               [Test]
+               [ExpectedException (typeof (NullReferenceException))]
+               public void Constructor_Null () 
+               {
+                       new HMACSHA512 (null);
+               }
+
+               [Test]
+               public void Invariants () 
+               {
+                       algo = new HMACSHA512 ();
+                       AssertEquals ("HMACSHA512.CanReuseTransform", true, algo.CanReuseTransform);
+                       AssertEquals ("HMACSHA512.CanTransformMultipleBlocks", true, algo.CanTransformMultipleBlocks);
+                       AssertEquals ("HMACSHA512.HashName", "SHA512", algo.HashName);
+                       AssertEquals ("HMACSHA512.HashSize", 512, algo.HashSize);
+                       AssertEquals ("HMACSHA512.InputBlockSize", 1, algo.InputBlockSize);
+                       AssertEquals ("HMACSHA512.OutputBlockSize", 1, algo.OutputBlockSize);
+                       AssertEquals ("HMACSHA512.ToString()", "System.Security.Cryptography.HMACSHA512", algo.ToString ()); 
+               }
+       }
+}
+
+#endif
index 5e790cee94332a1b06f1a0e0adb76c52be524801..3f1278e5e328f42f9c1d4c9bf7659520399e5539 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2006 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
@@ -272,8 +272,7 @@ public class HashAlgorithmTest : Assertion {
        }
 
        [Test]
-       [ExpectedException (typeof (ArgumentNullException))]
-#if ! NET_2_0
+#if ONLY_1_1
        [Category ("NotDotNet")] // System.ExecutionEngineException on MS runtime (1.1)
 #endif
        public void TransformBlock_OutputBuffer_Null ()
@@ -357,6 +356,129 @@ public class HashAlgorithmTest : Assertion {
                byte[] input = new byte [8];
                hash.TransformFinalBlock (input, 0, Int32.MaxValue);
        }
+
+       [Test]
+       [Category ("NotWorking")] // Mono nevers throws an exception (and we're all managed ;-)
+       public void TransformFinalBlock_Twice ()
+       {
+               bool managed = (hash.GetType ().ToString ().IndexOf ("Managed") > 0);
+               bool exception = false;
+               byte[] input = new byte [8];
+               hash.TransformFinalBlock (input, 0, input.Length);
+               try {
+                       hash.TransformFinalBlock (input, 0, input.Length);
+               }
+               catch (CryptographicException) {
+                       exception = true;
+                       if (managed)
+                               Fail ("*Managed don't throw CryptographicException");
+               }
+               if (!managed && !exception)
+                       Fail ("Expected CryptographicException from non *Managed classes");
+       }
+
+       [Test]
+       [Category ("NotWorking")] // Mono nevers throws an exception (and we're all managed ;-)
+       public void TransformFinalBlock_TransformBlock ()
+       {
+               bool managed = (hash.GetType ().ToString ().IndexOf ("Managed") > 0);
+               bool exception = false;
+               byte[] input = new byte[8];
+               hash.TransformFinalBlock (input, 0, input.Length);
+               try {
+                       hash.TransformBlock (input, 0, input.Length, input, 0);
+               }
+               catch (CryptographicException) {
+                       exception = true;
+                       if (managed)
+                               Fail ("*Managed don't throw CryptographicException");
+               }
+               if (!managed && !exception)
+                       Fail ("Expected CryptographicException from non *Managed classes");
+       }
+
+       [Test]
+       public void TransformFinalBlock_Twice_Initialize ()
+       {
+               byte[] input = new byte[8];
+               hash.TransformFinalBlock (input, 0, input.Length);
+               hash.Initialize ();
+               hash.TransformFinalBlock (input, 0, input.Length);
+       }
+
+       [Test]
+       public void TransformFinalBlock_ReturnedBuffer ()
+       {
+               byte[] input = new byte[8];
+               byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
+               AssertEquals ("buffer", input, output);
+               output[0] = 1;
+               AssertEquals ("0", 0, input[0]); // output is a copy (not a reference)
+       }
+
+       private byte[] HashBuffer (bool intersect)
+       {
+               byte[] buffer = new byte [256];
+               for (int i = 0; i < buffer.Length; i++)
+                       buffer [i] = (byte) i;
+
+               hash.Initialize ();
+               // ok
+               hash.TransformBlock (buffer, 0, 64, buffer, 0);
+               // bad - we rewrite the beginning of the buffer
+               hash.TransformBlock (buffer, 64, 128, buffer, intersect ? 0 : 64);
+               // ok
+               hash.TransformFinalBlock (buffer, 192, 64);
+               return hash.Hash;
+       }
+
+       [Test]
+       public void InputOutputIntersection ()
+       {
+               AssertEquals ("Intersect", HashBuffer (false), HashBuffer (true));
+       }
+
+       [Test]
+       [ExpectedException (typeof (NullReferenceException))]
+       [Category ("NotWorking")] // initialization problem ? fx2.0 only ?
+       public void Hash_AfterInitialize_FirstTime ()
+       {
+               hash.Initialize ();
+               // getting the property throws
+               AssertNull (hash.Hash);
+       }
+
+       [Test]
+       [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
+       public void Hash_AfterInitialize_SecondTime ()
+       {
+               byte[] input = new byte[8];
+               hash.Initialize ();
+               hash.TransformBlock (input, 0, input.Length, input, 0);
+               hash.Initialize ();
+               // getting the property throws
+               AssertNull (hash.Hash);
+       }
+
+       [Test]
+       [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
+       public void Hash_AfterTransformBlock ()
+       {
+               byte[] input = new byte[8];
+               hash.Initialize ();
+               hash.TransformBlock (input, 0, input.Length, input, 0);
+               // getting the property throws
+               AssertNull (hash.Hash);
+       }
+
+       [Test]
+       public void Hash_AfterTransformFinalBlock ()
+       {
+               byte[] input = new byte[8];
+               hash.Initialize ();
+               hash.TransformFinalBlock (input, 0, input.Length);
+               AssertNotNull (hash.Hash);
+       }
 }
 
 }
index 94ea2367b5e1e88ec3e68e7a70c484d247687d78..a69f5ed4423fcefa9e6093fd3ea6a13759655718 100644 (file)
@@ -25,6 +25,7 @@ public class KeyedHashAlgorithmTest : HashAlgorithmTest {
        protected override void SetUp () 
        {
                hash = KeyedHashAlgorithm.Create ();
+               (hash as KeyedHashAlgorithm).Key = new byte [8];
        }
 
        // Note: These tests will only be valid without a "machine.config" file
@@ -38,7 +39,7 @@ public class KeyedHashAlgorithmTest : HashAlgorithmTest {
        public override void Create () 
        {
                // try the default keyed hash algorithm (created in SetUp)
-               AssertEquals( "KeyedHashAlgorithm.Create()", defaultKeyedHash, hash.ToString());
+               AssertEquals( "KeyedHashAlgorithm.Create()", defaultKeyedHash, KeyedHashAlgorithm.Create ().ToString());
 
                // try to build all hash algorithms
                hash = KeyedHashAlgorithm.Create ("HMACSHA1");