2004-05-26 Sebastien Pouliot <sebastien@ximian.com>
[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
152 }