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 // (C) 2004 Novell 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 ();
30 // Note: These tests will only be valid without a "machine.config" file
31 // or a "machine.config" file that do not modify the default algorithm
33 private const string defaultHMACSHA1 = "System.Security.Cryptography.HMACSHA1";
34 private const string defaultMACTripleDES = "System.Security.Cryptography.MACTripleDES";
35 private const string defaultKeyedHash = defaultHMACSHA1;
38 public override void Create ()
40 // try the default keyed hash algorithm (created in SetUp)
41 AssertEquals( "KeyedHashAlgorithm.Create()", defaultKeyedHash, hash.ToString());
43 // try to build all hash algorithms
44 hash = KeyedHashAlgorithm.Create ("HMACSHA1");
45 AssertEquals ("KeyedHashAlgorithm.Create('HMACSHA1')", defaultHMACSHA1, hash.ToString ());
46 hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.HMACSHA1");
47 AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.HMACSHA1')", defaultHMACSHA1, hash.ToString ());
48 hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.KeyedHashAlgorithm" );
49 AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.KeyedHashAlgorithm')", defaultKeyedHash, hash.ToString ());
51 hash = KeyedHashAlgorithm.Create ("MACTripleDES");
52 AssertEquals ("KeyedHashAlgorithm.Create('MACTripleDES')", defaultMACTripleDES, hash.ToString ());
53 hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.MACTripleDES");
54 AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.MACTripleDES')", defaultMACTripleDES, hash.ToString ());
56 // try to build invalid implementation
57 hash = KeyedHashAlgorithm.Create ("InvalidKeyedHash");
58 AssertNull ("KeyedHashAlgorithm.Create('InvalidKeyedHash')", hash);
62 [ExpectedException (typeof (ArgumentNullException))]
63 public override void CreateNull ()
65 hash = KeyedHashAlgorithm.Create (null);
71 KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
72 AssertNotNull ("KeyedHashAlgorithm.Key not null (random key)", kh.Key);
73 byte[] key = { 0x01, 0x02, 0x03 };
74 byte[] keybackup = (byte[]) key.Clone ();
76 // the KeyedHashAlgorithm use a copy of the key (not a reference to)
78 AssertEquals ("KeyedHashAlgorithm key[0]", kh.Key, keybackup);
79 // you can't change individual bytes from a key
81 AssertEquals ("KeyedHashAlgorithm.Key[0]", kh.Key, keybackup);
85 [ExpectedException (typeof (CryptographicException))]
86 public void ChangeKey ()
88 KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
89 byte[] key = { 0x01, 0x02, 0x03 };
90 byte[] keybackup = (byte[]) key.Clone ();
93 // can't change a key after starting an operation
94 kh.TransformBlock (key, 0, 3, keybackup, 0);