This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / KeyedHashAlgorithmTest.cs
1 //
2 // KeyedHashAlgorithmTest.cs - NUnit Test Cases for KeyedHashAlgorithm
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell  http://www.novell.com
9 //
10
11 using NUnit.Framework;
12 using System;
13 using System.Security.Cryptography;
14 using System.Text;
15
16 namespace MonoTests.System.Security.Cryptography {
17
18 // KeyedHashAlgorithm is a abstract class - so most of it's functionality wont
19 // be tested here (but will be in its descendants).
20
21 [TestFixture]
22 public class KeyedHashAlgorithmTest : HashAlgorithmTest {
23
24         [SetUp]
25         protected override void SetUp () 
26         {
27                 hash = KeyedHashAlgorithm.Create ();
28         }
29
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
32         // configuration.
33         private const string defaultHMACSHA1 = "System.Security.Cryptography.HMACSHA1";
34         private const string defaultMACTripleDES = "System.Security.Cryptography.MACTripleDES";
35         private const string defaultKeyedHash = defaultHMACSHA1;
36
37         [Test]
38         public override void Create () 
39         {
40                 // try the default keyed hash algorithm (created in SetUp)
41                 AssertEquals( "KeyedHashAlgorithm.Create()", defaultKeyedHash, hash.ToString());
42
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 ());
50
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 ());
55
56                 // try to build invalid implementation
57                 hash = KeyedHashAlgorithm.Create ("InvalidKeyedHash");
58                 AssertNull ("KeyedHashAlgorithm.Create('InvalidKeyedHash')", hash);
59         }
60
61         [Test]
62         [ExpectedException (typeof (ArgumentNullException))]
63         public override void CreateNull () 
64         {
65                 hash = KeyedHashAlgorithm.Create (null);
66         }
67
68         [Test]
69         public void Key () 
70         {
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 ();
75                 kh.Key = key;
76                 // the KeyedHashAlgorithm use a copy of the key (not a reference to)
77                 key [0] = 0x00;
78                 AssertEquals ("KeyedHashAlgorithm key[0]", kh.Key, keybackup);
79                 // you can't change individual bytes from a key
80                 kh.Key [0] = 0x00;
81                 AssertEquals ("KeyedHashAlgorithm.Key[0]", kh.Key, keybackup);
82         }
83
84         [Test]
85         [ExpectedException (typeof (CryptographicException))]
86         public void ChangeKey () 
87         {
88                 KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
89                 byte[] key = { 0x01, 0x02, 0x03 };
90                 byte[] keybackup = (byte[]) key.Clone ();
91                 kh.Key = key;
92
93                 // can't change a key after starting an operation
94                 kh.TransformBlock (key, 0, 3, keybackup, 0);
95                 kh.Key = keybackup;
96         }
97 }
98
99 }