Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / HashAlgorithmTest.cs
index 5e790cee94332a1b06f1a0e0adb76c52be524801..9feea2b3385acd7eb8e93b514d86293ed0adf85e 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, 2007 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,20 +39,16 @@ namespace MonoTests.System.Security.Cryptography {
 // be tested here (but will be in its descendants).
 
 [TestFixture]
-public class HashAlgorithmTest : Assertion {
+public class HashAlgorithmTest {
+
        protected HashAlgorithm hash;
 
        [SetUp]
-       protected virtual void SetUp () 
+       public virtual void SetUp () 
        {
                hash = HashAlgorithm.Create ();
        }
 
-       public void AssertEquals (string msg, byte[] array1, byte[] array2) 
-       {
-               AllTests.AssertEquals (msg, array1, array2);
-       }
-
        // Note: These tests will only be valid without a "machine.config" file
        // or a "machine.config" file that do not modify the default algorithm
        // configuration.
@@ -67,47 +63,47 @@ public class HashAlgorithmTest : Assertion {
        public virtual void Create () 
        {
                // try the default hash algorithm (created in SetUp)
-               AssertEquals( "HashAlgorithm.Create()", defaultHash, hash.ToString());
+               Assert.AreEqual (defaultHash, hash.ToString (), "HashAlgorithm.Create()");
 
                // try to build all hash algorithms
                hash = HashAlgorithm.Create ("SHA");
-               AssertEquals ("HashAlgorithm.Create('SHA')", defaultSHA1, hash.ToString ());
+               Assert.AreEqual (defaultSHA1, hash.ToString (), "HashAlgorithm.Create('SHA')");
                hash = HashAlgorithm.Create ("SHA1");
-               AssertEquals ("HashAlgorithm.Create('SHA1')", defaultSHA1, hash.ToString ());
+               Assert.AreEqual (defaultSHA1, hash.ToString (), "HashAlgorithm.Create('SHA1')");
                hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1");
-               AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA1')", defaultSHA1, hash.ToString ());
+               Assert.AreEqual (defaultSHA1, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA1')");
                hash = HashAlgorithm.Create ("System.Security.Cryptography.HashAlgorithm" );
-               AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')", defaultHash, hash.ToString ());
+               Assert.AreEqual (defaultHash, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')");
 
                hash = HashAlgorithm.Create ("MD5");
-               AssertEquals ("HashAlgorithm.Create('MD5')", defaultMD5, hash.ToString ());
+               Assert.AreEqual (defaultMD5, hash.ToString (), "HashAlgorithm.Create('MD5')");
                hash = HashAlgorithm.Create ("System.Security.Cryptography.MD5");
-               AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.MD5')", defaultMD5, hash.ToString ());
+               Assert.AreEqual (defaultMD5, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.MD5')");
 
                hash = HashAlgorithm.Create ("SHA256");
-               AssertEquals ("HashAlgorithm.Create('SHA256')", defaultSHA256, hash.ToString ());
+               Assert.AreEqual (defaultSHA256, hash.ToString (), "HashAlgorithm.Create('SHA256')");
                hash = HashAlgorithm.Create ("SHA-256");
-               AssertEquals ("HashAlgorithm.Create('SHA-256')", defaultSHA256, hash.ToString ());
+               Assert.AreEqual (defaultSHA256, hash.ToString (), "HashAlgorithm.Create('SHA-256')");
                hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA256");
-               AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA256')", defaultSHA256, hash.ToString ());
+               Assert.AreEqual (defaultSHA256, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA256')");
        
                hash = HashAlgorithm.Create ("SHA384");
-               AssertEquals ("HashAlgorithm.Create('SHA384')", defaultSHA384, hash.ToString ());
+               Assert.AreEqual (defaultSHA384, hash.ToString (), "HashAlgorithm.Create('SHA384')");
                hash = HashAlgorithm.Create ("SHA-384");
-               AssertEquals ("HashAlgorithm.Create('SHA-384')", defaultSHA384, hash.ToString ());
+               Assert.AreEqual (defaultSHA384, hash.ToString (), "HashAlgorithm.Create('SHA-384')");
                hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA384");
-               AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA384')", defaultSHA384, hash.ToString ());
+               Assert.AreEqual (defaultSHA384, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA384')");
        
                hash = HashAlgorithm.Create ("SHA512");
-               AssertEquals ("HashAlgorithm.Create('SHA512')", defaultSHA512, hash.ToString ());
+               Assert.AreEqual (defaultSHA512, hash.ToString (), "HashAlgorithm.Create('SHA512')");
                hash = HashAlgorithm.Create ("SHA-512");
-               AssertEquals ("HashAlgorithm.Create('SHA-512')", defaultSHA512, hash.ToString ());
+               Assert.AreEqual (defaultSHA512, hash.ToString (), "HashAlgorithm.Create('SHA-512')");
                hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA512");
-               AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA512')", defaultSHA512, hash.ToString ());
+               Assert.AreEqual (defaultSHA512, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA512')");
        
                // try to build invalid implementation
                hash = HashAlgorithm.Create ("InvalidHash");
-               AssertNull ("HashAlgorithm.Create('InvalidHash')", hash);
+               Assert.IsNull (hash, "HashAlgorithm.Create('InvalidHash')");
        }
 
        [Test]
@@ -146,14 +142,14 @@ public class HashAlgorithmTest : Assertion {
        public void NullStream () 
        {
                Stream s = null;
-               byte[] result = hash.ComputeHash (s);
+               hash.ComputeHash (s);
        }
 
        [Test]
        public void Disposable () 
        {
                using (HashAlgorithm hash = HashAlgorithm.Create ()) {
-                       byte[] data = hash.ComputeHash (new byte [0]);
+                       hash.ComputeHash (new byte [0]);
                }
        }
 
@@ -272,8 +268,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 +352,131 @@ public class HashAlgorithmTest : Assertion {
                byte[] input = new byte [8];
                hash.TransformFinalBlock (input, 0, Int32.MaxValue);
        }
+
+       public virtual bool ManagedHashImplementation {
+               get { return false; }
+       }
+#if !NET_2_1
+       [Test]
+       [Category ("NotWorking")] // Mono nevers throws an exception (and we're all managed ;-)
+       public void TransformFinalBlock_Twice ()
+       {
+               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 (ManagedHashImplementation)
+                               Assert.Fail ("*Managed don't throw CryptographicException");
+               }
+               if (!ManagedHashImplementation && !exception)
+                       Assert.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 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 (ManagedHashImplementation)
+                               Assert.Fail ("*Managed don't throw CryptographicException");
+               }
+               if (!ManagedHashImplementation && !exception)
+                       Assert.Fail ("Expected CryptographicException from non *Managed classes");
+       }
+#endif
+       [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);
+               Assert.AreEqual (input, output, "buffer");
+               output[0] = 1;
+               Assert.AreEqual (0, input[0], "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 ()
+       {
+               Assert.AreEqual (HashBuffer (false), HashBuffer (true), "Intersect");
+       }
+#if !NET_2_1
+       [Test]
+       [ExpectedException (typeof (NullReferenceException))]
+       [Category ("NotWorking")] // initialization problem ? fx2.0 only ?
+       public void Hash_AfterInitialize_FirstTime ()
+       {
+               hash.Initialize ();
+               // getting the property throws
+               Assert.IsNull (hash.Hash);
+       }
+#endif
+       [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
+               Assert.IsNull (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
+               Assert.IsNull (hash.Hash);
+       }
+
+       [Test]
+       public void Hash_AfterTransformFinalBlock ()
+       {
+               byte[] input = new byte[8];
+               hash.Initialize ();
+               hash.TransformFinalBlock (input, 0, input.Length);
+               Assert.IsNotNull (hash.Hash);
+       }
 }
 
 }