// 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;
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);
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)
{
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);
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);
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);
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);
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++)
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,
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,
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,
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,
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");
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?");
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];
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];
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");
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++)
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++)