+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.
// 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
protected HMACMD5 algo;
+ [SetUp]
+ protected override void SetUp ()
+ {
+ algo = new HMACMD5 ();
+ algo.Key = new byte [8];
+ hash = algo;
+ }
+
[Test]
public void Constructors ()
{
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]
}
-#endif
\ No newline at end of file
+#endif
// 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
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
- public class HMACRIPEMD160Test : Assertion {
+ public class HMACRIPEMD160Test : KeyedHashAlgorithmTest {
protected HMACRIPEMD160 hmac;
}
[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 };
// 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
protected HMACSHA1 algo;
+ [SetUp]
+ protected override void SetUp ()
+ {
+ hash = HMACSHA1.Create ();
+ (hash as KeyedHashAlgorithm).Key = new byte [8];
+ }
+
[Test]
public void Constructors ()
{
// 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
protected HMACSHA256 algo;
+ [SetUp]
+ protected override void SetUp ()
+ {
+ algo = new HMACSHA256 ();
+ algo.Key = new byte [8];
+ hash = algo;
+ }
+
[Test]
public void Constructors ()
{
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);
}
}
-#endif
\ No newline at end of file
+#endif
--- /dev/null
+//
+// 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
--- /dev/null
+//
+// 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
// 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
}
[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 ()
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);
+ }
}
}
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
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");