(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.Security.Cryptography / HashAlgorithmTest.cs
blobef06ba3faf7c07e664274069b63f189193ea8be9
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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
31 using System;
32 using System.IO;
33 using System.Security.Cryptography;
34 using System.Text;
36 namespace MonoTests.System.Security.Cryptography {
38 // HashAlgorithm is a abstract class - so most of it's functionality wont
39 // be tested here (but will be in its descendants).
41 [TestFixture]
42 public class HashAlgorithmTest : Assertion {
43 protected HashAlgorithm hash;
45 [SetUp]
46 protected virtual void SetUp ()
48 hash = HashAlgorithm.Create ();
51 public void AssertEquals (string msg, byte[] array1, byte[] array2)
53 AllTests.AssertEquals (msg, array1, array2);
56 // Note: These tests will only be valid without a "machine.config" file
57 // or a "machine.config" file that do not modify the default algorithm
58 // configuration.
59 private const string defaultSHA1 = "System.Security.Cryptography.SHA1CryptoServiceProvider";
60 private const string defaultMD5 = "System.Security.Cryptography.MD5CryptoServiceProvider";
61 private const string defaultSHA256 = "System.Security.Cryptography.SHA256Managed";
62 private const string defaultSHA384 = "System.Security.Cryptography.SHA384Managed";
63 private const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
64 private const string defaultHash = defaultSHA1;
66 [Test]
67 public virtual void Create ()
69 // try the default hash algorithm (created in SetUp)
70 AssertEquals( "HashAlgorithm.Create()", defaultHash, hash.ToString());
72 // try to build all hash algorithms
73 hash = HashAlgorithm.Create ("SHA");
74 AssertEquals ("HashAlgorithm.Create('SHA')", defaultSHA1, hash.ToString ());
75 hash = HashAlgorithm.Create ("SHA1");
76 AssertEquals ("HashAlgorithm.Create('SHA1')", defaultSHA1, hash.ToString ());
77 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1");
78 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA1')", defaultSHA1, hash.ToString ());
79 hash = HashAlgorithm.Create ("System.Security.Cryptography.HashAlgorithm" );
80 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')", defaultHash, hash.ToString ());
82 hash = HashAlgorithm.Create ("MD5");
83 AssertEquals ("HashAlgorithm.Create('MD5')", defaultMD5, hash.ToString ());
84 hash = HashAlgorithm.Create ("System.Security.Cryptography.MD5");
85 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.MD5')", defaultMD5, hash.ToString ());
87 hash = HashAlgorithm.Create ("SHA256");
88 AssertEquals ("HashAlgorithm.Create('SHA256')", defaultSHA256, hash.ToString ());
89 hash = HashAlgorithm.Create ("SHA-256");
90 AssertEquals ("HashAlgorithm.Create('SHA-256')", defaultSHA256, hash.ToString ());
91 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA256");
92 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA256')", defaultSHA256, hash.ToString ());
94 hash = HashAlgorithm.Create ("SHA384");
95 AssertEquals ("HashAlgorithm.Create('SHA384')", defaultSHA384, hash.ToString ());
96 hash = HashAlgorithm.Create ("SHA-384");
97 AssertEquals ("HashAlgorithm.Create('SHA-384')", defaultSHA384, hash.ToString ());
98 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA384");
99 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA384')", defaultSHA384, hash.ToString ());
101 hash = HashAlgorithm.Create ("SHA512");
102 AssertEquals ("HashAlgorithm.Create('SHA512')", defaultSHA512, hash.ToString ());
103 hash = HashAlgorithm.Create ("SHA-512");
104 AssertEquals ("HashAlgorithm.Create('SHA-512')", defaultSHA512, hash.ToString ());
105 hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA512");
106 AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA512')", defaultSHA512, hash.ToString ());
108 // try to build invalid implementation
109 hash = HashAlgorithm.Create ("InvalidHash");
110 AssertNull ("HashAlgorithm.Create('InvalidHash')", hash);
113 [Test]
114 [ExpectedException (typeof (ArgumentNullException))]
115 public virtual void CreateNull ()
117 // try to build null implementation
118 hash = HashAlgorithm.Create (null);
121 [Test]
122 [ExpectedException (typeof (ObjectDisposedException))]
123 public void Clear ()
125 byte[] inputABC = Encoding.Default.GetBytes ("abc");
126 hash.ComputeHash (inputABC);
127 hash.Clear ();
128 // cannot use a disposed object
129 hash.ComputeHash (inputABC);
132 [Test]
133 [ExpectedException (typeof (ObjectDisposedException))]
134 public void Clear2 ()
136 byte[] inputABC = Encoding.Default.GetBytes ("abc");
137 MemoryStream ms = new MemoryStream (inputABC);
138 hash.ComputeHash (ms);
139 hash.Clear ();
140 // cannot use a disposed object
141 hash.ComputeHash (ms);
144 [Test]
145 [ExpectedException (typeof (NullReferenceException))]
146 public void NullStream ()
148 Stream s = null;
149 byte[] result = hash.ComputeHash (s);
152 [Test]
153 public void Disposable ()
155 using (HashAlgorithm hash = HashAlgorithm.Create ()) {
156 byte[] data = hash.ComputeHash (new byte [0]);
160 [Test]
161 [ExpectedException (typeof (ObjectDisposedException))]
162 public void InitializeDisposed ()
164 hash.ComputeHash (new byte [0]);
165 hash.Clear (); // disposed
166 hash.Initialize ();
167 hash.ComputeHash (new byte [0]);
170 [Test]
171 [ExpectedException (typeof (ArgumentNullException))]
172 public void ComputeHash_ArrayNull ()
174 byte[] array = null;
175 hash.ComputeHash (array);
178 [Test]
179 [ExpectedException (typeof (ArgumentNullException))]
180 public void ComputeHash_ArrayNullIntInt ()
182 byte[] array = null;
183 hash.ComputeHash (array, 0, 0);
186 [Test]
187 [ExpectedException (typeof (ArgumentOutOfRangeException))]
188 public void ComputeHash_OffsetNegative ()
190 byte[] array = new byte [0];
191 hash.ComputeHash (array, -1, 0);
194 [Test]
195 [ExpectedException (typeof (ArgumentException))]
196 public void ComputeHash_OffsetOverflow ()
198 byte[] array = new byte [1];
199 hash.ComputeHash (array, Int32.MaxValue, 1);
202 [Test]
203 [ExpectedException (typeof (ArgumentException))]
204 public void ComputeHash_CountNegative ()
206 byte[] array = new byte [0];
207 hash.ComputeHash (array, 0, -1);
210 [Test]
211 [ExpectedException (typeof (ArgumentException))]
212 public void ComputeHash_CountOverflow ()
214 byte[] array = new byte [1];
215 hash.ComputeHash (array, 1, Int32.MaxValue);
218 [Test]
219 // not checked in Fx 1.1
220 // [ExpectedException (typeof (ObjectDisposedException))]
221 public void TransformBlock_Disposed ()
223 hash.ComputeHash (new byte [0]);
224 hash.Initialize ();
225 byte[] input = new byte [8];
226 byte[] output = new byte [8];
227 hash.TransformBlock (input, 0, input.Length, output, 0);
230 [Test]
231 [ExpectedException (typeof (ArgumentNullException))]
232 public void TransformBlock_InputBuffer_Null ()
234 byte[] output = new byte [8];
235 hash.TransformBlock (null, 0, output.Length, output, 0);
238 [Test]
239 [ExpectedException (typeof (ArgumentOutOfRangeException))]
240 public void TransformBlock_InputOffset_Negative ()
242 byte[] input = new byte [8];
243 byte[] output = new byte [8];
244 hash.TransformBlock (input, -1, input.Length, output, 0);
247 [Test]
248 [ExpectedException (typeof (ArgumentException))]
249 public void TransformBlock_InputOffset_Overflow ()
251 byte[] input = new byte [8];
252 byte[] output = new byte [8];
253 hash.TransformBlock (input, Int32.MaxValue, input.Length, output, 0);
256 [Test]
257 [ExpectedException (typeof (ArgumentException))]
258 public void TransformBlock_InputCount_Negative ()
260 byte[] input = new byte [8];
261 byte[] output = new byte [8];
262 hash.TransformBlock (input, 0, -1, output, 0);
265 [Test]
266 [ExpectedException (typeof (ArgumentException))]
267 public void TransformBlock_InputCount_Overflow ()
269 byte[] input = new byte [8];
270 byte[] output = new byte [8];
271 hash.TransformBlock (input, 0, Int32.MaxValue, output, 0);
274 [Test]
275 [ExpectedException (typeof (ArgumentNullException))]
276 #if ! NET_2_0
277 [Ignore ("System.ExecutionEngineException on MS runtime (1.1)")]
278 #endif
279 public void TransformBlock_OutputBuffer_Null ()
281 byte[] input = new byte [8];
282 hash.TransformBlock (input, 0, input.Length, null, 0);
285 [Test]
286 #if NET_2_0
287 [ExpectedException (typeof (ArgumentOutOfRangeException))]
288 #else
289 [ExpectedException (typeof (IndexOutOfRangeException))]
290 #endif
291 public void TransformBlock_OutputOffset_Negative ()
293 byte[] input = new byte [8];
294 byte[] output = new byte [8];
295 hash.TransformBlock (input, 0, input.Length, output, -1);
298 [Test]
299 #if NET_2_0
300 [ExpectedException (typeof (ArgumentException))]
301 #else
302 [ExpectedException (typeof (IndexOutOfRangeException))]
303 #endif
304 public void TransformBlock_OutputOffset_Overflow ()
306 byte[] input = new byte [8];
307 byte[] output = new byte [8];
308 hash.TransformBlock (input, 0, input.Length, output, Int32.MaxValue);
311 [Test]
312 // not checked in Fx 1.1
313 // [ExpectedException (typeof (ObjectDisposedException))]
314 public void TransformFinalBlock_Disposed ()
316 hash.ComputeHash (new byte [0]);
317 hash.Initialize ();
318 byte[] input = new byte [8];
319 hash.TransformFinalBlock (input, 0, input.Length);
322 [Test]
323 [ExpectedException (typeof (ArgumentNullException))]
324 public void TransformFinalBlock_InputBuffer_Null ()
326 hash.TransformFinalBlock (null, 0, 8);
329 [Test]
330 [ExpectedException (typeof (ArgumentOutOfRangeException))]
331 public void TransformFinalBlock_InputOffset_Negative ()
333 byte[] input = new byte [8];
334 hash.TransformFinalBlock (input, -1, input.Length);
337 [Test]
338 [ExpectedException (typeof (ArgumentException))]
339 public void TransformFinalBlock_InputOffset_Overflow ()
341 byte[] input = new byte [8];
342 hash.TransformFinalBlock (input, Int32.MaxValue, input.Length);
345 [Test]
346 [ExpectedException (typeof (ArgumentException))]
347 public void TransformFinalBlock_InputCount_Negative ()
349 byte[] input = new byte [8];
350 hash.TransformFinalBlock (input, 0, -1);
353 [Test]
354 [ExpectedException (typeof (ArgumentException))]
355 public void TransformFinalBlock_InputCount_Overflow ()
357 byte[] input = new byte [8];
358 hash.TransformFinalBlock (input, 0, Int32.MaxValue);