KeyedHashAlgorithmTest.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. protected override void SetUp ()
  18. {
  19. hash = KeyedHashAlgorithm.Create ();
  20. }
  21. protected override void TearDown () {}
  22. // Note: These tests will only be valid without a "machine.config" file
  23. // or a "machine.config" file that do not modify the default algorithm
  24. // configuration.
  25. private const string defaultHMACSHA1 = "System.Security.Cryptography.HMACSHA1";
  26. private const string defaultMACTripleDES = "System.Security.Cryptography.MACTripleDES";
  27. private const string defaultKeyedHash = defaultHMACSHA1;
  28. public override void TestCreate ()
  29. {
  30. // try the default keyed hash algorithm (created in SetUp)
  31. AssertEquals( "KeyedHashAlgorithm.Create()", defaultKeyedHash, hash.ToString());
  32. // try to build all hash algorithms
  33. hash = KeyedHashAlgorithm.Create ("HMACSHA1");
  34. AssertEquals ("KeyedHashAlgorithm.Create('HMACSHA1')", defaultHMACSHA1, hash.ToString ());
  35. hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.HMACSHA1");
  36. AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.HMACSHA1')", defaultHMACSHA1, hash.ToString ());
  37. hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.KeyedHashAlgorithm" );
  38. AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.KeyedHashAlgorithm')", defaultKeyedHash, hash.ToString ());
  39. hash = KeyedHashAlgorithm.Create ("MACTripleDES");
  40. AssertEquals ("KeyedHashAlgorithm.Create('MACTripleDES')", defaultMACTripleDES, hash.ToString ());
  41. hash = KeyedHashAlgorithm.Create ("System.Security.Cryptography.MACTripleDES");
  42. AssertEquals ("KeyedHashAlgorithm.Create('System.Security.Cryptography.MACTripleDES')", defaultMACTripleDES, hash.ToString ());
  43. // try to build invalid implementation
  44. hash = KeyedHashAlgorithm.Create ("InvalidKeyedHash");
  45. AssertNull ("KeyedHashAlgorithm.Create('InvalidKeyedHash')", hash);
  46. // try to build null implementation
  47. try {
  48. hash = KeyedHashAlgorithm.Create (null);
  49. Fail ("KeyedHashAlgorithm.Create(null) should throw ArgumentNullException");
  50. }
  51. catch (ArgumentNullException) {
  52. // do nothing, this is what we expect
  53. }
  54. catch (Exception e) {
  55. Fail ("KeyedHashAlgorithm.Create(null) should throw ArgumentNullException not " + e.ToString ());
  56. }
  57. }
  58. public void TestKey ()
  59. {
  60. KeyedHashAlgorithm kh = (KeyedHashAlgorithm) hash;
  61. AssertNotNull ("KeyedHashAlgorithm.Key not null (random key)", kh.Key);
  62. byte[] key = { 0x01, 0x02, 0x03 };
  63. byte[] keybackup = (byte[]) key.Clone ();
  64. kh.Key = key;
  65. // the KeyedHashAlgorithm use a copy of the key (not a reference to)
  66. key [0] = 0x00;
  67. AssertEquals ("KeyedHashAlgorithm key[0]", kh.Key, keybackup);
  68. // you can't change individual bytes from a key
  69. kh.Key [0] = 0x00;
  70. AssertEquals ("KeyedHashAlgorithm.Key[0]", kh.Key, keybackup);
  71. // can't change a key after starting an operation
  72. kh.TransformBlock (key, 0, 3, keybackup, 0);
  73. try {
  74. kh.Key = keybackup;
  75. Fail ("KeyedHashAlgorithm.Key should throw CryptographicException but didn't");
  76. }
  77. catch (CryptographicException) {
  78. // do nothing, this is what we expect
  79. }
  80. catch (Exception e) {
  81. Fail ("KeyedHashAlgorithm.Key should throw CryptographicException not " + e.ToString ());
  82. }
  83. }
  84. }
  85. }