KeyedHashAlgorithmTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // KeyedHashAlgorithmTest.cs - NUnit Test Cases for KeyedHashAlgorithm
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. namespace MonoTests.System.Security.Cryptography {
  14. // KeyedHashAlgorithm is a abstract class - so most of it's functionality wont
  15. // be tested here (but will be in its descendants).
  16. public class KeyedHashAlgorithmTest : HashAlgorithmTest {
  17. public KeyedHashAlgorithmTest () : base ("System.Security.Cryptography.KeyedHashAlgorithm testsuite") {}
  18. public KeyedHashAlgorithmTest (string name) : base(name) {}
  19. protected override void SetUp ()
  20. {
  21. hash = KeyedHashAlgorithm.Create ();
  22. }
  23. protected override void TearDown () {}
  24. public static new ITest Suite {
  25. get {
  26. return new TestSuite (typeof (KeyedHashAlgorithmTest));
  27. }
  28. }
  29. // Note: These tests will only be valid without a "machine.config" file
  30. // or a "machine.config" file that do not modify the default algorithm
  31. // configuration.
  32. private const string defaultHMACSHA1 = "System.Security.Cryptography.HMACSHA1";
  33. private const string defaultMACTripleDES = "System.Security.Cryptography.MACTripleDES";
  34. private const string defaultKeyedHash = defaultHMACSHA1;
  35. public override void TestCreate ()
  36. {
  37. // try the default keyed hash algorithm (created in SetUp)
  38. AssertEquals( "KeyedHashAlgorithm.Create()", defaultKeyedHash, hash.ToString());
  39. // try to build all hash algorithms
  40. hash = KeyedHashAlgorithm.Create ("HMACSHA1");
  41. AssertEquals ("KeyedHashAlgorithm.Create('HMACSHA1')", defaultHMACSHA1, hash.ToString ());
  42. hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.HMACSHA1");
  43. AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.HMACSHA1')", defaultHMACSHA1, hash.ToString ());
  44. hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.KeyedHashAlgorithm" );
  45. AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.KeyedHashAlgorithm')", defaultKeyedHash, hash.ToString ());
  46. hash = KeyedHashAlgorithm.Create ("MACTripleDES");
  47. AssertEquals ("KeyedHashAlgorithm.Create('MACTripleDES')", defaultMACTripleDES, hash.ToString ());
  48. hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.MACTripleDES");
  49. AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.MACTripleDES')", defaultMACTripleDES, hash.ToString ());
  50. // try to build invalid implementation
  51. hash = KeyedHashAlgorithm.Create ("InvalidKeyedHash");
  52. AssertNull ("KeyedHashAlgorithm.Create('InvalidKeyedHash')", hash);
  53. // try to build null implementation
  54. try {
  55. hash = KeyedHashAlgorithm.Create (null);
  56. Fail ("KeyedHashAlgorithm.Create(null) should throw ArgumentNullException");
  57. }
  58. catch (ArgumentNullException) {
  59. // do nothing, this is what we expect
  60. }
  61. catch (Exception e) {
  62. Fail ("KeyedHashAlgorithm.Create(null) should throw ArgumentNullException not " + e.ToString ());
  63. }
  64. }
  65. public void TestKey ()
  66. {
  67. KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
  68. AssertNotNull ("KeyedHashAlgorithm.Key not null (random key)", kh.Key);
  69. byte[] key = { 0x01, 0x02, 0x03 };
  70. byte[] keybackup = (byte[]) key.Clone ();
  71. kh.Key = key;
  72. // the KeyedHashAlgorithm use a copy of the key (not a reference to)
  73. key [0] = 0x00;
  74. AssertEquals ("KeyedHashAlgorithm key[0]", kh.Key, keybackup);
  75. // you can't change individual bytes from a key
  76. kh.Key [0] = 0x00;
  77. AssertEquals ("KeyedHashAlgorithm.Key[0]", kh.Key, keybackup);
  78. // can't change a key after starting an operation
  79. kh.TransformBlock (key, 0, 3, keybackup, 0);
  80. try {
  81. kh.Key = keybackup;
  82. Fail ("KeyedHashAlgorithm.Key should throw CryptographicException but didn't");
  83. }
  84. catch (CryptographicException) {
  85. // do nothing, this is what we expect
  86. }
  87. catch (Exception e) {
  88. Fail ("KeyedHashAlgorithm.Key should throw CryptographicException not " + e.ToString ());
  89. }
  90. }
  91. }
  92. }