Merge pull request #3106 from lambdageek/dev/monoerror-unhandled
[mono.git] / mcs / class / Mono.Security.Win32 / Test / Mono.Security.Cryptography / MD2Test.cs
1 //
2 // MD2Test.cs - NUnit Test Cases for MD2 (RFC1319)
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.IO;
13 using Mono.Security.Cryptography;
14 using System.Text;
15
16 namespace MonoTests.Security.Cryptography {
17
18 // References:
19 // a.   The MD2 Message-Digest Algorithm
20 //      http://www.ietf.org/rfc/rfc1319.txt
21
22 // MD2 is a abstract class - so ALL of the test included here wont be tested
23 // on the abstract class but should be tested in ALL its descendants.
24 public class MD2Test {
25
26         protected MD2 hash;
27
28         // because most crypto stuff works with byte[] buffers
29         static public void AssertEquals (string msg, byte[] array1, byte[] array2) 
30         {
31                 if ((array1 == null) && (array2 == null))
32                         return;
33                 if (array1 == null)
34                         Assert.Fail (msg + " -> First array is NULL");
35                 if (array2 == null)
36                         Assert.Fail (msg + " -> Second array is NULL");
37         
38                 bool a = (array1.Length == array2.Length);
39                 if (a) {
40                         for (int i = 0; i < array1.Length; i++) {
41                                 if (array1 [i] != array2 [i]) {
42                                         a = false;
43                                         break;
44                                 }
45                         }
46                 }
47                 if (array1.Length > 0) {
48                         msg += " -> Expected " + BitConverter.ToString (array1, 0);
49                         msg += " is different than " + BitConverter.ToString (array2, 0);
50                 }
51                 Assert.IsTrue (a, msg);
52         }
53
54         // MD2 ("") = 8350e5a3e24c153df2275c9f80692773
55         [Test]
56         public void RFC1319_Test1 () 
57         {
58                 string className = hash.ToString ();
59                 byte[] result = { 0x83, 0x50, 0xe5, 0xa3, 0xe2, 0x4c, 0x15, 0x3d, 0xf2, 0x27, 0x5c, 0x9f, 0x80, 0x69, 0x27, 0x73 };
60                 byte[] input = new byte [0];
61         
62                 string testName = className + " 1";
63                 RFC1319_a (testName, hash, input, result);
64                 RFC1319_b (testName, hash, input, result);
65                 RFC1319_c (testName, hash, input, result);
66                 RFC1319_d (testName, hash, input, result);
67                 // N/A RFC1319_e (testName, hash, input, result);
68         }
69
70         // MD2 ("a") = 32ec01ec4a6dac72c0ab96fb34c0b5d1
71         [Test]
72         public void RFC1319_Test2 () 
73         {
74                 string className = hash.ToString ();
75                 byte[] result = { 0x32, 0xec, 0x01, 0xec, 0x4a, 0x6d, 0xac, 0x72, 0xc0, 0xab, 0x96, 0xfb, 0x34, 0xc0, 0xb5, 0xd1 };
76                 byte[] input = Encoding.Default.GetBytes ("a");
77         
78                 string testName = className + " 2";
79                 RFC1319_a (testName, hash, input, result);
80                 RFC1319_b (testName, hash, input, result);
81                 RFC1319_c (testName, hash, input, result);
82                 RFC1319_d (testName, hash, input, result);
83                 RFC1319_e (testName, hash, input, result);
84         }
85
86         // MD2 ("abc") = da853b0d3f88d99b30283a69e6ded6bb
87         [Test]
88         public void RFC1319_Test3 () 
89         {
90                 string className = hash.ToString ();
91                 byte[] result = { 0xda, 0x85, 0x3b, 0x0d, 0x3f, 0x88, 0xd9, 0x9b, 0x30, 0x28, 0x3a, 0x69, 0xe6, 0xde, 0xd6, 0xbb };
92                 byte[] input = Encoding.Default.GetBytes ("abc");
93         
94                 string testName = className + " 3";
95                 RFC1319_a (testName, hash, input, result);
96                 RFC1319_b (testName, hash, input, result);
97                 RFC1319_c (testName, hash, input, result);
98                 RFC1319_d (testName, hash, input, result);
99                 RFC1319_e (testName, hash, input, result);
100         }
101
102         // MD2 ("message digest") = ab4f496bfb2a530b219ff33031fe06b0
103         [Test]
104         public void RFC1319_Test4 () 
105         {
106                 string className = hash.ToString ();
107                 byte[] result = { 0xab, 0x4f, 0x49, 0x6b, 0xfb, 0x2a, 0x53, 0x0b, 0x21, 0x9f, 0xf3, 0x30, 0x31, 0xfe, 0x06, 0xb0 };
108                 byte[] input = Encoding.Default.GetBytes ("message digest");
109         
110                 string testName = className + " 4";
111                 RFC1319_a (testName, hash, input, result);
112                 RFC1319_b (testName, hash, input, result);
113                 RFC1319_c (testName, hash, input, result);
114                 RFC1319_d (testName, hash, input, result);
115                 RFC1319_e (testName, hash, input, result);
116         }
117
118         // MD2 ("abcdefghijklmnopqrstuvwxyz") = 4e8ddff3650292ab5a4108c3aa47940b
119         [Test]
120         public void RFC1319_Test5 () 
121         {
122                 string className = hash.ToString ();
123                 byte[] result = { 0x4e, 0x8d, 0xdf, 0xf3, 0x65, 0x02, 0x92, 0xab, 0x5a, 0x41, 0x08, 0xc3, 0xaa, 0x47, 0x94, 0x0b };
124                 byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
125         
126                 string testName = className + " 5";
127                 RFC1319_a (testName, hash, input, result);
128                 RFC1319_b (testName, hash, input, result);
129                 RFC1319_c (testName, hash, input, result);
130                 RFC1319_d (testName, hash, input, result);
131                 RFC1319_e (testName, hash, input, result);
132         }
133
134         // MD2 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
135         //      da33def2a42df13975352846c30338cd
136         [Test]
137         public void RFC1319_Test6 () 
138         {
139                 string className = hash.ToString ();
140                 byte[] result = { 0xda, 0x33, 0xde, 0xf2, 0xa4, 0x2d, 0xf1, 0x39, 0x75, 0x35, 0x28, 0x46, 0xc3, 0x03, 0x38, 0xcd };
141                 byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
142         
143                 string testName = className + " 6";
144                 RFC1319_a (testName, hash, input, result);
145                 RFC1319_b (testName, hash, input, result);
146                 RFC1319_c (testName, hash, input, result);
147                 RFC1319_d (testName, hash, input, result);
148                 RFC1319_e (testName, hash, input, result);
149         }
150
151         // MD2 ("123456789012345678901234567890123456789012345678901234567890123456
152         //      78901234567890") = d5976f79d83d3a0dc9806c3c66f3efd8
153         [Test]
154         public void RFC1319_Test7 () 
155         {
156                 string className = hash.ToString ();
157                 byte[] result = { 0xd5, 0x97, 0x6f, 0x79, 0xd8, 0x3d, 0x3a, 0x0d, 0xc9, 0x80, 0x6c, 0x3c, 0x66, 0xf3, 0xef, 0xd8 };
158                 byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
159         
160                 string testName = className + " 7";
161                 RFC1319_a (testName, hash, input, result);
162                 RFC1319_b (testName, hash, input, result);
163                 RFC1319_c (testName, hash, input, result);
164                 RFC1319_d (testName, hash, input, result);
165                 RFC1319_e (testName, hash, input, result);
166         }
167
168         public void RFC1319_a (string testName, MD2 hash, byte[] input, byte[] result) 
169         {
170                 byte[] output = hash.ComputeHash (input); 
171                 AssertEquals (testName + ".a.1", result, output);
172                 AssertEquals (testName + ".a.2", result, hash.Hash);
173                 // required or next operation will still return old hash
174                 hash.Initialize ();
175         }
176
177         public void RFC1319_b (string testName, MD2 hash, byte[] input, byte[] result) 
178         {
179                 byte[] output = hash.ComputeHash (input, 0, input.Length); 
180                 AssertEquals (testName + ".b.1", result, output);
181                 AssertEquals (testName + ".b.2", result, hash.Hash);
182                 // required or next operation will still return old hash
183                 hash.Initialize ();
184         }
185
186         public void RFC1319_c (string testName, MD2 hash, byte[] input, byte[] result) 
187         {
188                 MemoryStream ms = new MemoryStream (input);
189                 byte[] output = hash.ComputeHash (ms); 
190                 AssertEquals (testName + ".c.1", result, output);
191                 AssertEquals (testName + ".c.2", result, hash.Hash);
192                 // required or next operation will still return old hash
193                 hash.Initialize ();
194         }
195
196         public void RFC1319_d (string testName, MD2 hash, byte[] input, byte[] result) 
197         {
198                 byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
199                 AssertEquals (testName + ".d.1", input, output);
200                 AssertEquals (testName + ".d.2", result, hash.Hash);
201                 // required or next operation will still return old hash
202                 hash.Initialize ();
203         }
204
205         public void RFC1319_e (string testName, MD2 hash, byte[] input, byte[] result) 
206         {
207                 byte[] copy = new byte [input.Length];
208                 for (int i=0; i < input.Length - 1; i++)
209                         hash.TransformBlock (input, i, 1, copy, i);
210                 byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
211                 Assert.AreEqual (input [input.Length - 1], output [0], testName + ".e.1");
212                 AssertEquals (testName + ".e.2", result, hash.Hash);
213                 // required or next operation will still return old hash
214                 hash.Initialize ();
215         }
216
217         // none of those values changes for any implementation of MD2
218         public virtual void StaticInfo () 
219         {
220                 string className = hash.ToString ();
221                 Assert.AreEqual (128, hash.HashSize, className + ".HashSize",);
222                 Assert.AreEqual (1, hash.InputBlockSize, className + ".InputBlockSize");
223                 Assert.AreEqual (1, hash.OutputBlockSize, className + ".OutputBlockSize");
224         }
225 }
226
227 }