New test.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / HMACSHA1Test.cs
index 760dbbea7325ac80ffbb3471f29fbbe9e5d49eae..c52e203c3cdd6dd186698f77a6a7008b5ade4fae 100644 (file)
@@ -2,9 +2,29 @@
 // HMACSHA1Test.cs - NUnit Test Cases for HMACSHA1
 //
 // Author:
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.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
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using NUnit.Framework;
@@ -25,35 +45,33 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
 
        protected HMACSHA1 algo;
 
+       [SetUp]
        protected override void SetUp () 
        {
-               base.SetUp ();
+               hash = HMACSHA1.Create ();
+               (hash as KeyedHashAlgorithm).Key = new byte [8];
        }
 
-       protected override void TearDown () {}
-
-       public void TestConstructors () 
+       [Test]
+       public void Constructors () 
        {
                algo = new HMACSHA1 ();
-               AssertNotNull ("HMACSHA1 ()", algo);
+               AssertNotNull ("HMACSHA1 ()", hash);
 
                byte[] key = new byte [8];
                algo = new HMACSHA1 (key);
-               AssertNotNull ("HMACSHA1 (key)", algo);
-
-               try {
-                       algo = new HMACSHA1 (null);
-                       Fail ("HMACSHA1 (null) - Expected NullReferenceException but got none");
-               }
-               catch (NullReferenceException) {
-                       // well ArgumentNullException would have been more appropriate
-               }
-               catch (Exception e) {
-                       Fail ("HMACSHA1 (null) - Expected NullReferenceException but got: " + e.ToString ());
-               }
+               AssertNotNull ("HMACSHA1 (key)", hash);
+       }
+
+       [Test]
+       [ExpectedException (typeof (NullReferenceException))]
+       public void Constructor_Null () 
+       {
+               algo = new HMACSHA1 (null);
        }
 
-       public void TestInvariants () 
+       [Test]
+       public void Invariants () 
        {
                algo = new HMACSHA1 ();
                AssertEquals ("HMACSHA1.CanReuseTransform", true, algo.CanReuseTransform);
@@ -65,22 +83,18 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                AssertEquals ("HMACSHA1.ToString()", "System.Security.Cryptography.HMACSHA1", algo.ToString ()); 
        }
 
-       public void TestExceptions () 
+#if ! NET_2_0
+       // this is legal in .NET 2.0 because HMACSHA1 derives from HMAC
+       [Test]
+       [ExpectedException (typeof (InvalidCastException))]
+       public void InvalidHashName () 
        {
                algo = new HMACSHA1 ();
-               try {
-                       algo.HashName = "MD5";
-                       byte[] data = Encoding.Default.GetBytes ("MD5");
-                       byte[] hmac = algo.ComputeHash (data);
-                       Fail ("Expected InvalidCastException but got none");
-               }
-               catch (InvalidCastException) {
-                       // do nothing, this is what we expect
-               }
-               catch (Exception e) {
-                       Fail ("Expected InvalidCastException but got " + e.ToString ());
-               }
+               algo.HashName = "MD5";
+               byte[] data = Encoding.Default.GetBytes ("MD5");
+               byte[] hmac = algo.ComputeHash (data);
        }
+#endif
 
        public void Check (string testName, byte[] key, byte[] data, byte[] result) 
        {
@@ -94,8 +108,11 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
 
        public void CheckA (string testName, byte[] key, byte[] data, byte[] result) 
        {
-               algo = new HMACSHA1 ();
-               algo.Key = key;
+#if NET_2_0
+               algo = new HMACSHA1 (key, true);
+#else
+               algo = new HMACSHA1 (key);
+#endif
                byte[] hmac = algo.ComputeHash (data);
                AssertEquals (testName + "a1", result, hmac);
                AssertEquals (testName + "a2", result, algo.Hash);
@@ -103,8 +120,11 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
 
        public void CheckB (string testName, byte[] key, byte[] data, byte[] result) 
        {
-               algo = new HMACSHA1 ();
-               algo.Key = key;
+#if NET_2_0
+               algo = new HMACSHA1 (key, false);
+#else
+               algo = new HMACSHA1 (key);
+#endif
                byte[] hmac = algo.ComputeHash (data, 0, data.Length);
                AssertEquals (testName + "b1", result, hmac);
                AssertEquals (testName + "b2", result, algo.Hash);
@@ -112,8 +132,7 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
        
        public void CheckC (string testName, byte[] key, byte[] data, byte[] result) 
        {
-               algo = new HMACSHA1 ();
-               algo.Key = key;
+               algo = new HMACSHA1 (key);
                MemoryStream ms = new MemoryStream (data);
                byte[] hmac = algo.ComputeHash (ms);
                AssertEquals (testName + "c1", result, hmac);
@@ -122,8 +141,7 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
 
        public void CheckD (string testName, byte[] key, byte[] data, byte[] result) 
        {
-               algo = new HMACSHA1 ();
-               algo.Key = key;
+               algo = new HMACSHA1 (key);
                // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
                algo.TransformFinalBlock (data, 0, data.Length);
                AssertEquals (testName + "d", result, algo.Hash);
@@ -131,8 +149,7 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
 
        public void CheckE (string testName, byte[] key, byte[] data, byte[] result) 
        {
-               algo = new HMACSHA1 ();
-               algo.Key = key;
+               algo = new HMACSHA1 (key);
                byte[] copy = new byte [data.Length];
                // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
                for (int i=0; i < data.Length - 1; i++)
@@ -141,7 +158,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                AssertEquals (testName + "e", result, algo.Hash);
        }
 
-       public void TestFIPS198_A1 () 
+       [Test]
+       public void FIPS198_A1 () 
        {
                // exact 64 bytes key (no hashing - no padding)
                byte[] key =  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
@@ -160,7 +178,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("FIPS198-A1", key, data, fips);
        }
 
-       public void TestFIPS198_A2 () 
+       [Test]
+       public void FIPS198_A2 () 
        {
                // key < 64 bytes -> requires padding
                byte[] key =  { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
@@ -173,7 +192,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("FIPS198-A2", key, data, fips);
        }
 
-       public void TestFIPS198_A3 () 
+       [Test]
+       public void FIPS198_A3 () 
        {
                // key > 64 bytes -> requires hashing
                byte[] key =  { 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
@@ -191,7 +211,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("FIPS198-A3", key, data, fips);
        }
 
-       public void TestFIPS198_A4 () 
+       [Test]
+       public void FIPS198_A4 () 
        {
                byte[] key =  { 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
                                0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
@@ -206,7 +227,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("FIPS198-A4", key, data, fips);
        }
 
-       public void TestRFC2202_TC1 () 
+       [Test]
+       public void RFC2202_TC1 () 
        {
                byte[] key =  { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
                byte[] data = Encoding.Default.GetBytes ("Hi There");
@@ -214,7 +236,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("RFC2202-TC1", key, data, digest);
        }
 
-       public void TestRFC2202_TC2 () 
+       [Test]
+       public void RFC2202_TC2 () 
        {
                byte[] key = Encoding.Default.GetBytes ("Jefe");
                byte[] data = Encoding.Default.GetBytes ("what do ya want for nothing?");
@@ -222,7 +245,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("RFC2202-TC2", key, data, digest);
        }
 
-       public void TestRFC2202_TC3 () 
+       [Test]
+       public void RFC2202_TC3 () 
        {
                byte[] key = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
                byte[] data = new byte [50];
@@ -232,7 +256,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("RFC2202-TC3", key, data, digest);
        }
 
-       public void TestRFC2202_TC4 () 
+       [Test]
+       public void RFC2202_TC4 () 
        {
                byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 };
                byte[] data = new byte [50];
@@ -242,7 +267,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("RFC2202-TC4", key, data, digest);
        }
 
-       public void TestRFC2202_TC5 () 
+       [Test]
+       public void RFC2202_TC5 () 
        {
                byte[] key = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c };
                byte[] data = Encoding.Default.GetBytes ("Test With Truncation");
@@ -250,7 +276,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("RFC2202-TC5", key, data, digest);
        }
 
-       public void TestRFC2202_TC6 () 
+       [Test]
+       public void RFC2202_TC6 () 
        {
                byte[] key = new byte [80];
                for (int i = 0; i < key.Length; i++)
@@ -260,7 +287,8 @@ public class HMACSHA1Test : KeyedHashAlgorithmTest {
                Check ("RFC2202-TC6", key, data, digest);
        }
 
-       public void TestRFC2202_TC7 () 
+       [Test]
+       public void RFC2202_TC7 () 
        {
                byte[] key = new byte [80];
                for (int i = 0; i < key.Length; i++)