Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / System.Web / Test / System.Web.Util / MachineKeySectionUtilsTest.cs
1 //
2 // Unit tests for MachineKeySectionUtils (internals)
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31 using System.Web.Configuration;
32 using System.Web.Util;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Web.Util {
36
37         [TestFixture]
38         public class MachineKeySectionUtilsTest {
39
40                 static byte ChangeByte (byte b)
41                 {
42                         return (b == Byte.MaxValue) ? Byte.MinValue : (byte) (b + 1);
43                 }
44
45                 public void Encrypt_RoundTrip (MachineKeySection section)
46                 {
47                         byte [] data = new byte [14];
48                         byte [] encdata = MachineKeySectionUtils.Encrypt (section, data);
49                         byte [] decdata = MachineKeySectionUtils.Decrypt (section, encdata);
50                         Assert.AreEqual (data, decdata, "roundtrip");
51
52                         // changing length (missing first byte)
53                         byte [] cut = new byte [encdata.Length - 1];
54                         Array.Copy (encdata, 1, cut, 0, cut.Length);
55                         Assert.IsNull (MachineKeySectionUtils.Decrypt (section, cut), "bad length");
56
57                         // changing last byte (padding)
58                         byte be = encdata [encdata.Length - 1];
59                         encdata [encdata.Length - 1] = ChangeByte (be);
60                         Assert.IsNull (MachineKeySectionUtils.Decrypt (section, encdata), "bad padding");
61                 }
62
63                 [Test]
64                 public void Encrypt_RoundTrip_Default ()
65                 {
66                         Encrypt_RoundTrip (new MachineKeySection ());
67                 }
68
69                 [Test]
70                 public void Encrypt_RoundTrip_AES ()
71                 {
72                         MachineKeySection section = new MachineKeySection ();
73                         section.Validation = MachineKeyValidation.AES;
74                         Encrypt_RoundTrip (section);
75                 }
76
77                 [Test]
78                 public void Encrypt_RoundTrip_TripleDES ()
79                 {
80                         MachineKeySection section = new MachineKeySection ();
81                         section.Validation = MachineKeyValidation.TripleDES;
82                         Encrypt_RoundTrip (section);
83                 }
84
85                 [Test]
86                 public void Encrypt_RoundTrip_MD5 ()
87                 {
88                         MachineKeySection section = new MachineKeySection ();
89                         section.Validation = MachineKeyValidation.MD5;
90                         Encrypt_RoundTrip (section);
91                 }
92
93                 [Test]
94                 public void Encrypt_RoundTrip_SHA1 ()
95                 {
96                         MachineKeySection section = new MachineKeySection ();
97                         section.Validation = MachineKeyValidation.SHA1;
98                         Encrypt_RoundTrip (section);
99                 }
100 #if NET_4_0
101                 [Test]
102                 public void Encrypt_RoundTrip_HMACSHA256 ()
103                 {
104                         MachineKeySection section = new MachineKeySection ();
105                         section.Validation = MachineKeyValidation.HMACSHA256;
106                         EncryptSign_RoundTrip (section);
107                 }
108
109                 [Test]
110                 public void Encrypt_RoundTrip_HMACSHA384 ()
111                 {
112                         MachineKeySection section = new MachineKeySection ();
113                         section.Validation = MachineKeyValidation.HMACSHA384;
114                         EncryptSign_RoundTrip (section);
115                 }
116
117                 [Test]
118                 public void Encrypt_RoundTrip_HMACSHA512 ()
119                 {
120                         MachineKeySection section = new MachineKeySection ();
121                         section.Validation = MachineKeyValidation.HMACSHA512;
122                         EncryptSign_RoundTrip (section);
123                 }
124
125                 [Test]
126                 public void Encrypt_RoundTrip_Custom_RIPEMD160 ()
127                 {
128                         MachineKeySection section = new MachineKeySection ();
129                         section.ValidationAlgorithm = "alg:HMACRIPEMD160";
130                         EncryptSign_RoundTrip (section);
131                 }
132 #endif
133                 public void EncryptSign_RoundTrip (MachineKeySection section)
134                 {
135                         byte [] data = new byte [14];
136                         byte [] block = MachineKeySectionUtils.EncryptSign (section, data);
137                         byte [] decdata = MachineKeySectionUtils.VerifyDecrypt (section, block);
138                         Assert.AreEqual (data, decdata, "roundtrip");
139
140                         // changing a byte of the data
141                         byte b0 = block [0];
142                         block [0] = ChangeByte (b0);
143                         Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad data");
144                         block [0] = b0;
145
146                         // changing a byte of the signature
147                         byte be = block [block.Length - 1];
148                         block [block.Length - 1] = ChangeByte (be);
149                         Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad signature");
150                 }
151
152                 [Test]
153                 public void EncryptSign_RoundTrip_Default ()
154                 {
155                         EncryptSign_RoundTrip (new MachineKeySection ());
156                 }
157
158                 [Test]
159                 public void EncryptSign_RoundTrip_AES ()
160                 {
161                         MachineKeySection section = new MachineKeySection ();
162                         section.Validation = MachineKeyValidation.AES;
163                         EncryptSign_RoundTrip (section);
164                 }
165
166                 [Test]
167                 public void EncryptSign_RoundTrip_TripleDES ()
168                 {
169                         MachineKeySection section = new MachineKeySection ();
170                         section.Validation = MachineKeyValidation.TripleDES;
171                         EncryptSign_RoundTrip (section);
172                 }
173
174                 [Test]
175                 public void EncryptSign_RoundTrip_MD5 ()
176                 {
177                         MachineKeySection section = new MachineKeySection ();
178                         section.Validation = MachineKeyValidation.MD5;
179                         EncryptSign_RoundTrip (section);
180                 }
181
182                 [Test]
183                 public void EncryptSign_RoundTrip_SHA1 ()
184                 {
185                         MachineKeySection section = new MachineKeySection ();
186                         section.Validation = MachineKeyValidation.SHA1;
187                         EncryptSign_RoundTrip (section);
188                 }
189 #if NET_4_0
190                 [Test]
191                 public void EncryptSign_RoundTrip_HMACSHA256 ()
192                 {
193                         MachineKeySection section = new MachineKeySection ();
194                         section.Validation = MachineKeyValidation.HMACSHA256;
195                         EncryptSign_RoundTrip (section);
196                 }
197
198                 [Test]
199                 public void EncryptSign_RoundTrip_HMACSHA384 ()
200                 {
201                         MachineKeySection section = new MachineKeySection ();
202                         section.Validation = MachineKeyValidation.HMACSHA384;
203                         EncryptSign_RoundTrip (section);
204                 }
205
206                 [Test]
207                 public void EncryptSign_RoundTrip_HMACSHA512 ()
208                 {
209                         MachineKeySection section = new MachineKeySection ();
210                         section.Validation = MachineKeyValidation.HMACSHA512;
211                         EncryptSign_RoundTrip (section);
212                 }
213
214                 [Test]
215                 public void EncryptSign_RoundTrip_Custom_RIPEMD160 ()
216                 {
217                         MachineKeySection section = new MachineKeySection ();
218                         section.ValidationAlgorithm = "alg:HMACRIPEMD160";
219                         EncryptSign_RoundTrip (section);
220                 }
221 #endif
222                 public void Validation_RoundTrip (MachineKeySection section)
223                 {
224                         byte [] data = new byte [] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
225                         byte [] block = MachineKeySectionUtils.Sign (section, data);
226                         Assert.AreEqual (data, MachineKeySectionUtils.Verify (section, block), "OK");
227
228                         // changing last byte
229                         for (int i = 0; i < data.Length; i++) {
230                                 byte b = block [i];
231                                 block [i] = ChangeByte (b);
232                                 Assert.IsNull (MachineKeySectionUtils.Verify (section, block), "bad-" + i.ToString ());
233                                 block [i] = b;
234                         }
235                 }
236
237                 [Test]
238                 public void Validation_RoundTrip_Default ()
239                 {
240                         Validation_RoundTrip (new MachineKeySection ());
241                 }
242
243                 [Test]
244                 public void Validation_RoundTrip_AES ()
245                 {
246                         MachineKeySection section = new MachineKeySection ();
247                         section.Validation = MachineKeyValidation.AES;
248                         Validation_RoundTrip (section);
249                 }
250
251                 [Test]
252                 public void Validation_RoundTrip_TripleDES ()
253                 {
254                         MachineKeySection section = new MachineKeySection ();
255                         section.Validation = MachineKeyValidation.TripleDES;
256                         Validation_RoundTrip (section);
257                 }
258
259                 [Test]
260                 public void Validation_RoundTrip_MD5 ()
261                 {
262                         MachineKeySection section = new MachineKeySection ();
263                         section.Validation = MachineKeyValidation.MD5;
264                         Validation_RoundTrip (section);
265                 }
266
267                 [Test]
268                 public void Validation_RoundTrip_SHA1 ()
269                 {
270                         MachineKeySection section = new MachineKeySection ();
271                         section.Validation = MachineKeyValidation.SHA1;
272                         Validation_RoundTrip (section);
273                 }
274
275 #if NET_4_0
276                 [Test]
277                 public void Validation_RoundTrip_HMACSHA256 ()
278                 {
279                         MachineKeySection section = new MachineKeySection ();
280                         section.Validation = MachineKeyValidation.HMACSHA256;
281                         Validation_RoundTrip (section);
282                 }
283
284                 [Test]
285                 public void Validation_RoundTrip_HMACSHA384 ()
286                 {
287                         MachineKeySection section = new MachineKeySection ();
288                         section.Validation = MachineKeyValidation.HMACSHA384;
289                         Validation_RoundTrip (section);
290                 }
291
292                 [Test]
293                 public void Validation_RoundTrip_HMACSHA512 ()
294                 {
295                         MachineKeySection section = new MachineKeySection ();
296                         section.Validation = MachineKeyValidation.HMACSHA512;
297                         Validation_RoundTrip (section);
298                 }
299
300                 [Test]
301                 public void Validation_RoundTrip_Custom_RIPEMD160 ()
302                 {
303                         MachineKeySection section = new MachineKeySection ();
304                         section.ValidationAlgorithm = "alg:HMACRIPEMD160";
305                         Validation_RoundTrip (section);
306                 }
307 #endif
308                 [Test]
309                 public void GetHexString ()
310                 {
311                         Assert.AreEqual ("DEADC0DE", MachineKeySectionUtils.GetHexString (new byte [] { 0xde, 0xad, 0xc0, 0xde }), "deadcode");
312                 }
313         }
314 }
315