merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / HashAlgorithmTest.cs
index 120aabac27e424959e4a433e6d38a59d3bc8ddd8..3f1278e5e328f42f9c1d4c9bf7659520399e5539 100644 (file)
@@ -5,7 +5,26 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// (C) 2004 Novell  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
+// "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;
@@ -253,8 +272,9 @@ public class HashAlgorithmTest : Assertion {
        }
 
        [Test]
-       [ExpectedException (typeof (ArgumentNullException))]
-       [Ignore ("System.ExecutionEngineException on MS runtime (1.1)")]
+#if ONLY_1_1
+       [Category ("NotDotNet")] // System.ExecutionEngineException on MS runtime (1.1)
+#endif
        public void TransformBlock_OutputBuffer_Null ()
        {
                byte[] input = new byte [8];
@@ -262,7 +282,11 @@ public class HashAlgorithmTest : Assertion {
        }
 
        [Test]
+#if NET_2_0
+       [ExpectedException (typeof (ArgumentOutOfRangeException))]
+#else
        [ExpectedException (typeof (IndexOutOfRangeException))]
+#endif
        public void TransformBlock_OutputOffset_Negative ()
        {
                byte[] input = new byte [8];
@@ -271,7 +295,11 @@ public class HashAlgorithmTest : Assertion {
        }
 
        [Test]
+#if NET_2_0
+       [ExpectedException (typeof (ArgumentException))]
+#else
        [ExpectedException (typeof (IndexOutOfRangeException))]
+#endif
        public void TransformBlock_OutputOffset_Overflow ()
        {
                byte[] input = new byte [8];
@@ -328,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);
+       }
 }
 
 }