HashAlgorithmTest.cs 4.8 KB

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