HashAlgorithmTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // HashAlgorithmTest.cs - NUnit Test Cases for HashAlgorithm
  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. {
  15. // HashAlgorithm is a abstract class - so most of it's functionality wont
  16. // be tested here (but will be in its descendants).
  17. public class HashAlgorithmTest : TestCase {
  18. protected HashAlgorithm hash;
  19. public HashAlgorithmTest () : base ("System.Security.Cryptography.HashAlgorithm testsuite") {}
  20. public HashAlgorithmTest (string name) : base(name) {}
  21. protected override void SetUp ()
  22. {
  23. hash = HashAlgorithm.Create ();
  24. }
  25. protected override void TearDown () {}
  26. public static ITest Suite {
  27. get {
  28. return new TestSuite (typeof (HashAlgorithmTest));
  29. }
  30. }
  31. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  32. {
  33. AllTests.AssertEquals (msg, array1, array2);
  34. }
  35. // Note: These tests will only be valid without a "machine.config" file
  36. // or a "machine.config" file that do not modify the default algorithm
  37. // configuration.
  38. private const string defaultSHA1 = "System.Security.Cryptography.SHA1CryptoServiceProvider";
  39. private const string defaultMD5 = "System.Security.Cryptography.MD5CryptoServiceProvider";
  40. private const string defaultSHA256 = "System.Security.Cryptography.SHA256Managed";
  41. private const string defaultSHA384 = "System.Security.Cryptography.SHA384Managed";
  42. private const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
  43. private const string defaultHash = defaultSHA1;
  44. public virtual void TestCreate ()
  45. {
  46. // try the default hash algorithm (created in SetUp)
  47. AssertEquals( "HashAlgorithm.Create()", defaultHash, hash.ToString());
  48. // try to build all hash algorithms
  49. hash = HashAlgorithm.Create ("SHA");
  50. AssertEquals ("HashAlgorithm.Create('SHA')", defaultSHA1, hash.ToString ());
  51. hash = HashAlgorithm.Create ("SHA1");
  52. AssertEquals ("HashAlgorithm.Create('SHA1')", defaultSHA1, hash.ToString ());
  53. hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1");
  54. AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA1')", defaultSHA1, hash.ToString ());
  55. hash = HashAlgorithm.Create ("System.Security.Cryptography.HashAlgorithm" );
  56. AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')", defaultHash, hash.ToString ());
  57. hash = HashAlgorithm.Create ("MD5");
  58. AssertEquals ("HashAlgorithm.Create('MD5')", defaultMD5, hash.ToString ());
  59. hash = HashAlgorithm.Create ("System.Security.Cryptography.MD5");
  60. AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.MD5')", defaultMD5, hash.ToString ());
  61. hash = HashAlgorithm.Create ("SHA256");
  62. AssertEquals ("HashAlgorithm.Create('SHA256')", defaultSHA256, hash.ToString ());
  63. hash = HashAlgorithm.Create ("SHA-256");
  64. AssertEquals ("HashAlgorithm.Create('SHA-256')", defaultSHA256, hash.ToString ());
  65. hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA256");
  66. AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA256')", defaultSHA256, hash.ToString ());
  67. hash = HashAlgorithm.Create ("SHA384");
  68. AssertEquals ("HashAlgorithm.Create('SHA384')", defaultSHA384, hash.ToString ());
  69. hash = HashAlgorithm.Create ("SHA-384");
  70. AssertEquals ("HashAlgorithm.Create('SHA-384')", defaultSHA384, hash.ToString ());
  71. hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA384");
  72. AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA384')", defaultSHA384, hash.ToString ());
  73. hash = HashAlgorithm.Create ("SHA512");
  74. AssertEquals ("HashAlgorithm.Create('SHA512')", defaultSHA512, hash.ToString ());
  75. hash = HashAlgorithm.Create ("SHA-512");
  76. AssertEquals ("HashAlgorithm.Create('SHA-512')", defaultSHA512, hash.ToString ());
  77. hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA512");
  78. AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA512')", defaultSHA512, hash.ToString ());
  79. // try to build invalid implementation
  80. hash = HashAlgorithm.Create ("InvalidHash");
  81. AssertNull ("HashAlgorithm.Create('InvalidHash')", hash);
  82. // try to build null implementation
  83. try {
  84. hash = HashAlgorithm.Create (null);
  85. Fail ("HashAlgorithm.Create(null) should throw ArgumentNullException");
  86. }
  87. catch (ArgumentNullException) {
  88. // do nothing, this is what we expect
  89. }
  90. catch (Exception e) {
  91. Fail ("HashAlgorithm.Create(null) should throw ArgumentNullException not " + e.ToString() );
  92. }
  93. }
  94. public void TestClear ()
  95. {
  96. byte[] inputABC = Encoding.Default.GetBytes ("abc");
  97. hash.ComputeHash (inputABC);
  98. hash.Clear ();
  99. // cannot use a disposed object
  100. try {
  101. hash.ComputeHash (inputABC);
  102. Fail ("ComputeHash after clear should throw ObjectDisposedException but didn't");
  103. }
  104. catch (ObjectDisposedException) {
  105. // do nothing, this is what we expect
  106. }
  107. catch (Exception e) {
  108. Fail ("ComputeHash after clear should throw ObjectDisposedException not " + e.ToString ());
  109. }
  110. }
  111. }
  112. }