Buffer sizes
[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 // Copyright (C) 2004, 2007 Novell, Inc (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                 (hash as KeyedHashAlgorithm).Key = new byte [8];
29         }
30
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
33         // configuration.
34         private const string defaultHMACSHA1 = "System.Security.Cryptography.HMACSHA1";
35         private const string defaultMACTripleDES = "System.Security.Cryptography.MACTripleDES";
36         private const string defaultKeyedHash = defaultHMACSHA1;
37
38         [Test]
39         public override void Create () 
40         {
41                 // try the default keyed hash algorithm (created in SetUp)
42                 Assert.AreEqual (defaultKeyedHash, KeyedHashAlgorithm.Create ().ToString (), "KeyedHashAlgorithm.Create()");
43
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')");
51
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')");
56
57                 // try to build invalid implementation
58                 hash = KeyedHashAlgorithm.Create ("InvalidKeyedHash");
59                 Assert.IsNull (hash, "KeyedHashAlgorithm.Create('InvalidKeyedHash')");
60         }
61
62         [Test]
63         [ExpectedException (typeof (ArgumentNullException))]
64         public override void CreateNull () 
65         {
66                 hash = KeyedHashAlgorithm.Create (null);
67         }
68
69         [Test]
70         public void Key () 
71         {
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 ();
76                 kh.Key = key;
77                 // the KeyedHashAlgorithm use a copy of the key (not a reference to)
78                 key [0] = 0x00;
79                 Assert.AreEqual (kh.Key, keybackup, "KeyedHashAlgorithm key[0]-before");
80                 // you can't change individual bytes from a key
81                 kh.Key [0] = 0x00;
82                 Assert.AreEqual (kh.Key, keybackup, "KeyedHashAlgorithm.Key[0]-after");
83         }
84
85         [Test]
86         [ExpectedException (typeof (CryptographicException))]
87         public void ChangeKey () 
88         {
89                 KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
90                 byte[] key = { 0x01, 0x02, 0x03 };
91                 byte[] keybackup = (byte[]) key.Clone ();
92                 kh.Key = key;
93
94                 // can't change a key after starting an operation
95                 kh.TransformBlock (key, 0, 3, keybackup, 0);
96                 kh.Key = keybackup;
97         }
98 }
99
100 }