120aabac27e424959e4a433e6d38a59d3bc8ddd8
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / HashAlgorithmTest.cs
1 //
2 // HashAlgorithmTest.cs - NUnit Test Cases for HashAlgorithm
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell  http://www.novell.com
9 //
10
11 using NUnit.Framework;
12 using System;
13 using System.IO;
14 using System.Security.Cryptography;
15 using System.Text;
16
17 namespace MonoTests.System.Security.Cryptography {
18
19 // HashAlgorithm is a abstract class - so most of it's functionality wont
20 // be tested here (but will be in its descendants).
21
22 [TestFixture]
23 public class HashAlgorithmTest : Assertion {
24         protected HashAlgorithm hash;
25
26         [SetUp]
27         protected virtual void SetUp () 
28         {
29                 hash = HashAlgorithm.Create ();
30         }
31
32         public void AssertEquals (string msg, byte[] array1, byte[] array2) 
33         {
34                 AllTests.AssertEquals (msg, array1, array2);
35         }
36
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
39         // configuration.
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;
46
47         [Test]
48         public virtual void Create () 
49         {
50                 // try the default hash algorithm (created in SetUp)
51                 AssertEquals( "HashAlgorithm.Create()", defaultHash, hash.ToString());
52
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 ());
62
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 ());
67
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 ());
74         
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 ());
81         
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 ());
88         
89                 // try to build invalid implementation
90                 hash = HashAlgorithm.Create ("InvalidHash");
91                 AssertNull ("HashAlgorithm.Create('InvalidHash')", hash);
92         }
93
94         [Test]
95         [ExpectedException (typeof (ArgumentNullException))]
96         public virtual void CreateNull () 
97         {
98                 // try to build null implementation
99                 hash = HashAlgorithm.Create (null);
100         }
101
102         [Test]
103         [ExpectedException (typeof (ObjectDisposedException))]
104         public void Clear () 
105         {
106                 byte[] inputABC = Encoding.Default.GetBytes ("abc");
107                 hash.ComputeHash (inputABC);
108                 hash.Clear ();
109                 // cannot use a disposed object
110                 hash.ComputeHash (inputABC);
111         }
112
113         [Test]
114         [ExpectedException (typeof (ObjectDisposedException))]
115         public void Clear2 () 
116         {
117                 byte[] inputABC = Encoding.Default.GetBytes ("abc");
118                 MemoryStream ms = new MemoryStream (inputABC);
119                 hash.ComputeHash (ms);
120                 hash.Clear ();
121                 // cannot use a disposed object
122                 hash.ComputeHash (ms);
123         }
124
125         [Test]
126         [ExpectedException (typeof (NullReferenceException))]
127         public void NullStream () 
128         {
129                 Stream s = null;
130                 byte[] result = hash.ComputeHash (s);
131         }
132
133         [Test]
134         public void Disposable () 
135         {
136                 using (HashAlgorithm hash = HashAlgorithm.Create ()) {
137                         byte[] data = hash.ComputeHash (new byte [0]);
138                 }
139         }
140
141         [Test]
142         [ExpectedException (typeof (ObjectDisposedException))]
143         public void InitializeDisposed () 
144         {
145                 hash.ComputeHash (new byte [0]);
146                 hash.Clear (); // disposed
147                 hash.Initialize ();
148                 hash.ComputeHash (new byte [0]);
149         }
150
151         [Test]
152         [ExpectedException (typeof (ArgumentNullException))]
153         public void ComputeHash_ArrayNull ()
154         {
155                 byte[] array = null;
156                 hash.ComputeHash (array);
157         }
158
159         [Test]
160         [ExpectedException (typeof (ArgumentNullException))]
161         public void ComputeHash_ArrayNullIntInt ()
162         {
163                 byte[] array = null;
164                 hash.ComputeHash (array, 0, 0);
165         }
166
167         [Test]
168         [ExpectedException (typeof (ArgumentOutOfRangeException))]
169         public void ComputeHash_OffsetNegative ()
170         {
171                 byte[] array = new byte [0];
172                 hash.ComputeHash (array, -1, 0);
173         }
174
175         [Test]
176         [ExpectedException (typeof (ArgumentException))]
177         public void ComputeHash_OffsetOverflow ()
178         {
179                 byte[] array = new byte [1];
180                 hash.ComputeHash (array, Int32.MaxValue, 1);
181         }
182
183         [Test]
184         [ExpectedException (typeof (ArgumentException))]
185         public void ComputeHash_CountNegative ()
186         {
187                 byte[] array = new byte [0];
188                 hash.ComputeHash (array, 0, -1);
189         }
190
191         [Test]
192         [ExpectedException (typeof (ArgumentException))]
193         public void ComputeHash_CountOverflow ()
194         {
195                 byte[] array = new byte [1];
196                 hash.ComputeHash (array, 1, Int32.MaxValue);
197         }
198
199         [Test]
200 // not checked in Fx 1.1
201 //      [ExpectedException (typeof (ObjectDisposedException))]
202         public void TransformBlock_Disposed () 
203         {
204                 hash.ComputeHash (new byte [0]);
205                 hash.Initialize ();
206                 byte[] input = new byte [8];
207                 byte[] output = new byte [8];
208                 hash.TransformBlock (input, 0, input.Length, output, 0);
209         }
210
211         [Test]
212         [ExpectedException (typeof (ArgumentNullException))]
213         public void TransformBlock_InputBuffer_Null ()
214         {
215                 byte[] output = new byte [8];
216                 hash.TransformBlock (null, 0, output.Length, output, 0);
217         }
218
219         [Test]
220         [ExpectedException (typeof (ArgumentOutOfRangeException))]
221         public void TransformBlock_InputOffset_Negative ()
222         {
223                 byte[] input = new byte [8];
224                 byte[] output = new byte [8];
225                 hash.TransformBlock (input, -1, input.Length, output, 0);
226         }
227
228         [Test]
229         [ExpectedException (typeof (ArgumentException))]
230         public void TransformBlock_InputOffset_Overflow ()
231         {
232                 byte[] input = new byte [8];
233                 byte[] output = new byte [8];
234                 hash.TransformBlock (input, Int32.MaxValue, input.Length, output, 0);
235         }
236
237         [Test]
238         [ExpectedException (typeof (ArgumentException))]
239         public void TransformBlock_InputCount_Negative ()
240         {
241                 byte[] input = new byte [8];
242                 byte[] output = new byte [8];
243                 hash.TransformBlock (input, 0, -1, output, 0);
244         }
245
246         [Test]
247         [ExpectedException (typeof (ArgumentException))]
248         public void TransformBlock_InputCount_Overflow ()
249         {
250                 byte[] input = new byte [8];
251                 byte[] output = new byte [8];
252                 hash.TransformBlock (input, 0, Int32.MaxValue, output, 0);
253         }
254
255         [Test]
256         [ExpectedException (typeof (ArgumentNullException))]
257         [Ignore ("System.ExecutionEngineException on MS runtime (1.1)")]
258         public void TransformBlock_OutputBuffer_Null ()
259         {
260                 byte[] input = new byte [8];
261                 hash.TransformBlock (input, 0, input.Length, null, 0);
262         }
263
264         [Test]
265         [ExpectedException (typeof (IndexOutOfRangeException))]
266         public void TransformBlock_OutputOffset_Negative ()
267         {
268                 byte[] input = new byte [8];
269                 byte[] output = new byte [8];
270                 hash.TransformBlock (input, 0, input.Length, output, -1);
271         }
272
273         [Test]
274         [ExpectedException (typeof (IndexOutOfRangeException))]
275         public void TransformBlock_OutputOffset_Overflow ()
276         {
277                 byte[] input = new byte [8];
278                 byte[] output = new byte [8];
279                 hash.TransformBlock (input, 0, input.Length, output, Int32.MaxValue);
280         }
281
282         [Test]
283 // not checked in Fx 1.1
284 //      [ExpectedException (typeof (ObjectDisposedException))]
285         public void TransformFinalBlock_Disposed () 
286         {
287                 hash.ComputeHash (new byte [0]);
288                 hash.Initialize ();
289                 byte[] input = new byte [8];
290                 hash.TransformFinalBlock (input, 0, input.Length);
291         }
292
293         [Test]
294         [ExpectedException (typeof (ArgumentNullException))]
295         public void TransformFinalBlock_InputBuffer_Null ()
296         {
297                 hash.TransformFinalBlock (null, 0, 8);
298         }
299
300         [Test]
301         [ExpectedException (typeof (ArgumentOutOfRangeException))]
302         public void TransformFinalBlock_InputOffset_Negative ()
303         {
304                 byte[] input = new byte [8];
305                 hash.TransformFinalBlock (input, -1, input.Length);
306         }
307
308         [Test]
309         [ExpectedException (typeof (ArgumentException))]
310         public void TransformFinalBlock_InputOffset_Overflow ()
311         {
312                 byte[] input = new byte [8];
313                 hash.TransformFinalBlock (input, Int32.MaxValue, input.Length);
314         }
315
316         [Test]
317         [ExpectedException (typeof (ArgumentException))]
318         public void TransformFinalBlock_InputCount_Negative ()
319         {
320                 byte[] input = new byte [8];
321                 hash.TransformFinalBlock (input, 0, -1);
322         }
323
324         [Test]
325         [ExpectedException (typeof (ArgumentException))]
326         public void TransformFinalBlock_InputCount_Overflow ()
327         {
328                 byte[] input = new byte [8];
329                 hash.TransformFinalBlock (input, 0, Int32.MaxValue);
330         }
331 }
332
333 }