(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.Security.Cryptography / KeyedHashAlgorithmTest.cs
blob94ea2367b5e1e88ec3e68e7a70c484d247687d78
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 //
11 using NUnit.Framework;
12 using System;
13 using System.Security.Cryptography;
14 using System.Text;
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).
21 [TestFixture]
22 public class KeyedHashAlgorithmTest : HashAlgorithmTest {
24 [SetUp]
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
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;
37 [Test]
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);
61 [Test]
62 [ExpectedException (typeof (ArgumentNullException))]
63 public override void CreateNull ()
65 hash = KeyedHashAlgorithm.Create (null);
68 [Test]
69 public void Key ()
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);
84 [Test]
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 ();
91 kh.Key = key;
93 // can't change a key after starting an operation
94 kh.TransformBlock (key, 0, 3, keybackup, 0);
95 kh.Key = keybackup;