| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- //
- // HashAlgorithmTest.cs - NUnit Test Cases for HashAlgorithm
- //
- // Author:
- // Sebastien Pouliot ([email protected])
- //
- // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
- //
- using NUnit.Framework;
- using System;
- using System.Security.Cryptography;
- using System.Text;
- namespace MonoTests.System.Security.Cryptography
- {
- // HashAlgorithm is a abstract class - so most of it's functionality wont
- // be tested here (but will be in its descendants).
- public class HashAlgorithmTest : TestCase {
- protected HashAlgorithm hash;
- protected override void SetUp ()
- {
- hash = HashAlgorithm.Create ();
- }
- protected override void TearDown () {}
- public void AssertEquals (string msg, byte[] array1, byte[] array2)
- {
- AllTests.AssertEquals (msg, array1, array2);
- }
- // Note: These tests will only be valid without a "machine.config" file
- // or a "machine.config" file that do not modify the default algorithm
- // configuration.
- private const string defaultSHA1 = "System.Security.Cryptography.SHA1CryptoServiceProvider";
- private const string defaultMD5 = "System.Security.Cryptography.MD5CryptoServiceProvider";
- private const string defaultSHA256 = "System.Security.Cryptography.SHA256Managed";
- private const string defaultSHA384 = "System.Security.Cryptography.SHA384Managed";
- private const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
- private const string defaultHash = defaultSHA1;
- public virtual void TestCreate ()
- {
- // try the default hash algorithm (created in SetUp)
- AssertEquals( "HashAlgorithm.Create()", defaultHash, hash.ToString());
- // try to build all hash algorithms
- hash = HashAlgorithm.Create ("SHA");
- AssertEquals ("HashAlgorithm.Create('SHA')", defaultSHA1, hash.ToString ());
- hash = HashAlgorithm.Create ("SHA1");
- AssertEquals ("HashAlgorithm.Create('SHA1')", defaultSHA1, hash.ToString ());
- hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1");
- AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA1')", defaultSHA1, hash.ToString ());
- hash = HashAlgorithm.Create ("System.Security.Cryptography.HashAlgorithm" );
- AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')", defaultHash, hash.ToString ());
- hash = HashAlgorithm.Create ("MD5");
- AssertEquals ("HashAlgorithm.Create('MD5')", defaultMD5, hash.ToString ());
- hash = HashAlgorithm.Create ("System.Security.Cryptography.MD5");
- AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.MD5')", defaultMD5, hash.ToString ());
- hash = HashAlgorithm.Create ("SHA256");
- AssertEquals ("HashAlgorithm.Create('SHA256')", defaultSHA256, hash.ToString ());
- hash = HashAlgorithm.Create ("SHA-256");
- AssertEquals ("HashAlgorithm.Create('SHA-256')", defaultSHA256, hash.ToString ());
- hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA256");
- AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA256')", defaultSHA256, hash.ToString ());
-
- hash = HashAlgorithm.Create ("SHA384");
- AssertEquals ("HashAlgorithm.Create('SHA384')", defaultSHA384, hash.ToString ());
- hash = HashAlgorithm.Create ("SHA-384");
- AssertEquals ("HashAlgorithm.Create('SHA-384')", defaultSHA384, hash.ToString ());
- hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA384");
- AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA384')", defaultSHA384, hash.ToString ());
-
- hash = HashAlgorithm.Create ("SHA512");
- AssertEquals ("HashAlgorithm.Create('SHA512')", defaultSHA512, hash.ToString ());
- hash = HashAlgorithm.Create ("SHA-512");
- AssertEquals ("HashAlgorithm.Create('SHA-512')", defaultSHA512, hash.ToString ());
- hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA512");
- AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA512')", defaultSHA512, hash.ToString ());
-
- // try to build invalid implementation
- hash = HashAlgorithm.Create ("InvalidHash");
- AssertNull ("HashAlgorithm.Create('InvalidHash')", hash);
- // try to build null implementation
- try {
- hash = HashAlgorithm.Create (null);
- Fail ("HashAlgorithm.Create(null) should throw ArgumentNullException");
- }
- catch (ArgumentNullException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail ("HashAlgorithm.Create(null) should throw ArgumentNullException not " + e.ToString() );
- }
- }
- public void TestClear ()
- {
- byte[] inputABC = Encoding.Default.GetBytes ("abc");
- hash.ComputeHash (inputABC);
- hash.Clear ();
- // cannot use a disposed object
- try {
- hash.ComputeHash (inputABC);
- Fail ("ComputeHash after clear should throw ObjectDisposedException but didn't");
- }
- catch (ObjectDisposedException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail ("ComputeHash after clear should throw ObjectDisposedException not " + e.ToString ());
- }
- }
- }
- }
|