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]);
152 [ExpectedException (typeof (ArgumentNullException))]
153 public void ComputeHash_ArrayNull ()
156 hash.ComputeHash (array);
160 [ExpectedException (typeof (ArgumentNullException))]
161 public void ComputeHash_ArrayNullIntInt ()
164 hash.ComputeHash (array, 0, 0);
168 [ExpectedException (typeof (ArgumentOutOfRangeException))]
169 public void ComputeHash_OffsetNegative ()
171 byte[] array = new byte [0];
172 hash.ComputeHash (array, -1, 0);
176 [ExpectedException (typeof (ArgumentException))]
177 public void ComputeHash_OffsetOverflow ()
179 byte[] array = new byte [1];
180 hash.ComputeHash (array, Int32.MaxValue, 1);
184 [ExpectedException (typeof (ArgumentException))]
185 public void ComputeHash_CountNegative ()
187 byte[] array = new byte [0];
188 hash.ComputeHash (array, 0, -1);
192 [ExpectedException (typeof (ArgumentException))]
193 public void ComputeHash_CountOverflow ()
195 byte[] array = new byte [1];
196 hash.ComputeHash (array, 1, Int32.MaxValue);
200 // not checked in Fx 1.1
201 // [ExpectedException (typeof (ObjectDisposedException))]
202 public void TransformBlock_Disposed ()
204 hash.ComputeHash (new byte [0]);
206 byte[] input = new byte [8];
207 byte[] output = new byte [8];
208 hash.TransformBlock (input, 0, input.Length, output, 0);
212 [ExpectedException (typeof (ArgumentNullException))]
213 public void TransformBlock_InputBuffer_Null ()
215 byte[] output = new byte [8];
216 hash.TransformBlock (null, 0, output.Length, output, 0);
220 [ExpectedException (typeof (ArgumentOutOfRangeException))]
221 public void TransformBlock_InputOffset_Negative ()
223 byte[] input = new byte [8];
224 byte[] output = new byte [8];
225 hash.TransformBlock (input, -1, input.Length, output, 0);
229 [ExpectedException (typeof (ArgumentException))]
230 public void TransformBlock_InputOffset_Overflow ()
232 byte[] input = new byte [8];
233 byte[] output = new byte [8];
234 hash.TransformBlock (input, Int32.MaxValue, input.Length, output, 0);
238 [ExpectedException (typeof (ArgumentException))]
239 public void TransformBlock_InputCount_Negative ()
241 byte[] input = new byte [8];
242 byte[] output = new byte [8];
243 hash.TransformBlock (input, 0, -1, output, 0);
247 [ExpectedException (typeof (ArgumentException))]
248 public void TransformBlock_InputCount_Overflow ()
250 byte[] input = new byte [8];
251 byte[] output = new byte [8];
252 hash.TransformBlock (input, 0, Int32.MaxValue, output, 0);
256 [ExpectedException (typeof (ArgumentNullException))]
257 [Ignore ("System.ExecutionEngineException on MS runtime (1.1)")]
258 public void TransformBlock_OutputBuffer_Null ()
260 byte[] input = new byte [8];
261 hash.TransformBlock (input, 0, input.Length, null, 0);
265 [ExpectedException (typeof (IndexOutOfRangeException))]
266 public void TransformBlock_OutputOffset_Negative ()
268 byte[] input = new byte [8];
269 byte[] output = new byte [8];
270 hash.TransformBlock (input, 0, input.Length, output, -1);
274 [ExpectedException (typeof (IndexOutOfRangeException))]
275 public void TransformBlock_OutputOffset_Overflow ()
277 byte[] input = new byte [8];
278 byte[] output = new byte [8];
279 hash.TransformBlock (input, 0, input.Length, output, Int32.MaxValue);
283 // not checked in Fx 1.1
284 // [ExpectedException (typeof (ObjectDisposedException))]
285 public void TransformFinalBlock_Disposed ()
287 hash.ComputeHash (new byte [0]);
289 byte[] input = new byte [8];
290 hash.TransformFinalBlock (input, 0, input.Length);
294 [ExpectedException (typeof (ArgumentNullException))]
295 public void TransformFinalBlock_InputBuffer_Null ()
297 hash.TransformFinalBlock (null, 0, 8);
301 [ExpectedException (typeof (ArgumentOutOfRangeException))]
302 public void TransformFinalBlock_InputOffset_Negative ()
304 byte[] input = new byte [8];
305 hash.TransformFinalBlock (input, -1, input.Length);
309 [ExpectedException (typeof (ArgumentException))]
310 public void TransformFinalBlock_InputOffset_Overflow ()
312 byte[] input = new byte [8];
313 hash.TransformFinalBlock (input, Int32.MaxValue, input.Length);
317 [ExpectedException (typeof (ArgumentException))]
318 public void TransformFinalBlock_InputCount_Negative ()
320 byte[] input = new byte [8];
321 hash.TransformFinalBlock (input, 0, -1);
325 [ExpectedException (typeof (ArgumentException))]
326 public void TransformFinalBlock_InputCount_Overflow ()
328 byte[] input = new byte [8];
329 hash.TransformFinalBlock (input, 0, Int32.MaxValue);