2 // KeyedHashAlgorithmTest.cs - NUnit Test Cases for KeyedHashAlgorithm
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
11 using NUnit.Framework;
13 using System.Security.Cryptography;
16 namespace MonoTests.System.Security.Cryptography {
18 // KeyedHashAlgorithm is a abstract class - so most of it's functionality wont
19 // be tested here (but will be in its descendants).
22 public class KeyedHashAlgorithmTest : HashAlgorithmTest {
25 protected override void SetUp ()
27 hash = KeyedHashAlgorithm.Create ();
28 (hash as KeyedHashAlgorithm).Key = new byte [8];
31 // Note: These tests will only be valid without a "machine.config" file
32 // or a "machine.config" file that do not modify the default algorithm
34 private const string defaultHMACSHA1 = "System.Security.Cryptography.HMACSHA1";
35 private const string defaultMACTripleDES = "System.Security.Cryptography.MACTripleDES";
36 private const string defaultKeyedHash = defaultHMACSHA1;
39 public override void Create ()
41 // try the default keyed hash algorithm (created in SetUp)
42 Assert.AreEqual (defaultKeyedHash, KeyedHashAlgorithm.Create ().ToString (), "KeyedHashAlgorithm.Create()");
44 // try to build all hash algorithms
45 hash = KeyedHashAlgorithm.Create ("HMACSHA1");
46 Assert.AreEqual (defaultHMACSHA1, hash.ToString (), "KeyedHashAlgorithm.Create('HMACSHA1')");
47 hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.HMACSHA1");
48 Assert.AreEqual (defaultHMACSHA1, hash.ToString (), "KeyedHashAlgorithm.Create('System.Security.Cryptography.HMACSHA1')");
49 hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.KeyedHashAlgorithm" );
50 Assert.AreEqual (defaultKeyedHash, hash.ToString (), "KeyedHashAlgorithm.Create('System.Security.Cryptography.KeyedHashAlgorithm')");
52 hash = KeyedHashAlgorithm.Create ("MACTripleDES");
53 Assert.AreEqual (defaultMACTripleDES, hash.ToString (), "KeyedHashAlgorithm.Create('MACTripleDES')");
54 hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.MACTripleDES");
55 Assert.AreEqual (defaultMACTripleDES, hash.ToString (), "KeyedHashAlgorithm.Create('System.Security.Cryptography.MACTripleDES')");
57 // try to build invalid implementation
58 hash = KeyedHashAlgorithm.Create ("InvalidKeyedHash");
59 Assert.IsNull (hash, "KeyedHashAlgorithm.Create('InvalidKeyedHash')");
63 [ExpectedException (typeof (ArgumentNullException))]
64 public override void CreateNull ()
66 hash = KeyedHashAlgorithm.Create (null);
72 KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
73 Assert.IsNotNull (kh.Key, "KeyedHashAlgorithm.Key not null (random key)");
74 byte[] key = { 0x01, 0x02, 0x03 };
75 byte[] keybackup = (byte[]) key.Clone ();
77 // the KeyedHashAlgorithm use a copy of the key (not a reference to)
79 Assert.AreEqual (kh.Key, keybackup, "KeyedHashAlgorithm key[0]-before");
80 // you can't change individual bytes from a key
82 Assert.AreEqual (kh.Key, keybackup, "KeyedHashAlgorithm.Key[0]-after");
86 [ExpectedException (typeof (CryptographicException))]
87 public void ChangeKey ()
89 KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
90 byte[] key = { 0x01, 0x02, 0x03 };
91 byte[] keybackup = (byte[]) key.Clone ();
94 // can't change a key after starting an operation
95 kh.TransformBlock (key, 0, 3, keybackup, 0);