2 // HashAlgorithmTest.cs - NUnit Test Cases for HashAlgorithm
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell http://www.novell.com
11 using NUnit.Framework;
14 using System.Security.Cryptography;
17 namespace MonoTests.System.Security.Cryptography {
19 // HashAlgorithm is a abstract class - so most of it's functionality wont
20 // be tested here (but will be in its descendants).
23 public class HashAlgorithmTest : Assertion {
24 protected HashAlgorithm hash;
27 protected virtual void SetUp ()
29 hash = HashAlgorithm.Create ();
32 public void AssertEquals (string msg, byte[] array1, byte[] array2)
34 AllTests.AssertEquals (msg, array1, array2);
37 // Note: These tests will only be valid without a "machine.config" file
38 // or a "machine.config" file that do not modify the default algorithm
40 private const string defaultSHA1 = "System.Security.Cryptography.SHA1CryptoServiceProvider";
41 private const string defaultMD5 = "System.Security.Cryptography.MD5CryptoServiceProvider";
42 private const string defaultSHA256 = "System.Security.Cryptography.SHA256Managed";
43 private const string defaultSHA384 = "System.Security.Cryptography.SHA384Managed";
44 private const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
45 private const string defaultHash = defaultSHA1;
48 public virtual void Create ()
50 // try the default hash algorithm (created in SetUp)
51 AssertEquals( "HashAlgorithm.Create()", defaultHash, hash.ToString());
53 // try to build all hash algorithms
54 hash = HashAlgorithm.Create ("SHA");
55 AssertEquals ("HashAlgorithm.Create('SHA')", defaultSHA1, hash.ToString ());
56 hash = HashAlgorithm.Create ("SHA1");
57 AssertEquals ("HashAlgorithm.Create('SHA1')", defaultSHA1, hash.ToString ());
58 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1");
59 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA1')", defaultSHA1, hash.ToString ());
60 hash = HashAlgorithm.Create ("System.Security.Cryptography.HashAlgorithm" );
61 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')", defaultHash, hash.ToString ());
63 hash = HashAlgorithm.Create ("MD5");
64 AssertEquals ("HashAlgorithm.Create('MD5')", defaultMD5, hash.ToString ());
65 hash = HashAlgorithm.Create ("System.Security.Cryptography.MD5");
66 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.MD5')", defaultMD5, hash.ToString ());
68 hash = HashAlgorithm.Create ("SHA256");
69 AssertEquals ("HashAlgorithm.Create('SHA256')", defaultSHA256, hash.ToString ());
70 hash = HashAlgorithm.Create ("SHA-256");
71 AssertEquals ("HashAlgorithm.Create('SHA-256')", defaultSHA256, hash.ToString ());
72 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA256");
73 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA256')", defaultSHA256, hash.ToString ());
75 hash = HashAlgorithm.Create ("SHA384");
76 AssertEquals ("HashAlgorithm.Create('SHA384')", defaultSHA384, hash.ToString ());
77 hash = HashAlgorithm.Create ("SHA-384");
78 AssertEquals ("HashAlgorithm.Create('SHA-384')", defaultSHA384, hash.ToString ());
79 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA384");
80 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA384')", defaultSHA384, hash.ToString ());
82 hash = HashAlgorithm.Create ("SHA512");
83 AssertEquals ("HashAlgorithm.Create('SHA512')", defaultSHA512, hash.ToString ());
84 hash = HashAlgorithm.Create ("SHA-512");
85 AssertEquals ("HashAlgorithm.Create('SHA-512')", defaultSHA512, hash.ToString ());
86 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA512");
87 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA512')", defaultSHA512, hash.ToString ());
89 // try to build invalid implementation
90 hash = HashAlgorithm.Create ("InvalidHash");
91 AssertNull ("HashAlgorithm.Create('InvalidHash')", hash);
95 [ExpectedException (typeof (ArgumentNullException))]
96 public virtual void CreateNull ()
98 // try to build null implementation
99 hash = HashAlgorithm.Create (null);
103 [ExpectedException (typeof (ObjectDisposedException))]
106 byte[] inputABC = Encoding.Default.GetBytes ("abc");
107 hash.ComputeHash (inputABC);
109 // cannot use a disposed object
110 hash.ComputeHash (inputABC);
114 [ExpectedException (typeof (ObjectDisposedException))]
115 public void Clear2 ()
117 byte[] inputABC = Encoding.Default.GetBytes ("abc");
118 MemoryStream ms = new MemoryStream (inputABC);
119 hash.ComputeHash (ms);
121 // cannot use a disposed object
122 hash.ComputeHash (ms);
126 [ExpectedException (typeof (NullReferenceException))]
127 public void NullStream ()
130 byte[] result = hash.ComputeHash (s);
134 public void Disposable ()
136 using (HashAlgorithm hash = HashAlgorithm.Create ()) {
137 byte[] data = hash.ComputeHash (new byte [0]);
142 [ExpectedException (typeof (ObjectDisposedException))]
143 public void InitializeDisposed ()
145 hash.ComputeHash (new byte [0]);
146 hash.Clear (); // disposed
148 hash.ComputeHash (new byte [0]);