2 // Unit tests for MachineKeySectionUtils (internals)
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Web.Configuration;
32 using System.Web.Util;
33 using NUnit.Framework;
35 namespace MonoTests.System.Web.Util {
38 public class MachineKeySectionUtilsTest {
40 static byte ChangeByte (byte b)
42 return (b == Byte.MaxValue) ? Byte.MinValue : (byte) (b + 1);
45 public void Encrypt_RoundTrip (MachineKeySection section)
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");
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");
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");
64 public void Encrypt_RoundTrip_Default ()
66 Encrypt_RoundTrip (new MachineKeySection ());
70 public void Encrypt_RoundTrip_AES ()
72 MachineKeySection section = new MachineKeySection ();
73 section.Validation = MachineKeyValidation.AES;
74 Encrypt_RoundTrip (section);
78 public void Encrypt_RoundTrip_TripleDES ()
80 MachineKeySection section = new MachineKeySection ();
81 section.Validation = MachineKeyValidation.TripleDES;
82 Encrypt_RoundTrip (section);
86 public void Encrypt_RoundTrip_MD5 ()
88 MachineKeySection section = new MachineKeySection ();
89 section.Validation = MachineKeyValidation.MD5;
90 Encrypt_RoundTrip (section);
94 public void Encrypt_RoundTrip_SHA1 ()
96 MachineKeySection section = new MachineKeySection ();
97 section.Validation = MachineKeyValidation.SHA1;
98 Encrypt_RoundTrip (section);
102 public void Encrypt_RoundTrip_HMACSHA256 ()
104 MachineKeySection section = new MachineKeySection ();
105 section.Validation = MachineKeyValidation.HMACSHA256;
106 EncryptSign_RoundTrip (section);
110 public void Encrypt_RoundTrip_HMACSHA384 ()
112 MachineKeySection section = new MachineKeySection ();
113 section.Validation = MachineKeyValidation.HMACSHA384;
114 EncryptSign_RoundTrip (section);
118 public void Encrypt_RoundTrip_HMACSHA512 ()
120 MachineKeySection section = new MachineKeySection ();
121 section.Validation = MachineKeyValidation.HMACSHA512;
122 EncryptSign_RoundTrip (section);
126 public void Encrypt_RoundTrip_Custom_RIPEMD160 ()
128 MachineKeySection section = new MachineKeySection ();
129 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
130 EncryptSign_RoundTrip (section);
133 public void EncryptSign_RoundTrip (MachineKeySection section)
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");
140 // changing a byte of the data
142 block [0] = ChangeByte (b0);
143 Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad data");
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");
153 public void EncryptSign_RoundTrip_Default ()
155 EncryptSign_RoundTrip (new MachineKeySection ());
159 public void EncryptSign_RoundTrip_AES ()
161 MachineKeySection section = new MachineKeySection ();
162 section.Validation = MachineKeyValidation.AES;
163 EncryptSign_RoundTrip (section);
167 public void EncryptSign_RoundTrip_TripleDES ()
169 MachineKeySection section = new MachineKeySection ();
170 section.Validation = MachineKeyValidation.TripleDES;
171 EncryptSign_RoundTrip (section);
175 public void EncryptSign_RoundTrip_MD5 ()
177 MachineKeySection section = new MachineKeySection ();
178 section.Validation = MachineKeyValidation.MD5;
179 EncryptSign_RoundTrip (section);
183 public void EncryptSign_RoundTrip_SHA1 ()
185 MachineKeySection section = new MachineKeySection ();
186 section.Validation = MachineKeyValidation.SHA1;
187 EncryptSign_RoundTrip (section);
191 public void EncryptSign_RoundTrip_HMACSHA256 ()
193 MachineKeySection section = new MachineKeySection ();
194 section.Validation = MachineKeyValidation.HMACSHA256;
195 EncryptSign_RoundTrip (section);
199 public void EncryptSign_RoundTrip_HMACSHA384 ()
201 MachineKeySection section = new MachineKeySection ();
202 section.Validation = MachineKeyValidation.HMACSHA384;
203 EncryptSign_RoundTrip (section);
207 public void EncryptSign_RoundTrip_HMACSHA512 ()
209 MachineKeySection section = new MachineKeySection ();
210 section.Validation = MachineKeyValidation.HMACSHA512;
211 EncryptSign_RoundTrip (section);
215 public void EncryptSign_RoundTrip_Custom_RIPEMD160 ()
217 MachineKeySection section = new MachineKeySection ();
218 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
219 EncryptSign_RoundTrip (section);
222 public void Validation_RoundTrip (MachineKeySection section)
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");
228 // changing last byte
229 for (int i = 0; i < data.Length; i++) {
231 block [i] = ChangeByte (b);
232 Assert.IsNull (MachineKeySectionUtils.Verify (section, block), "bad-" + i.ToString ());
238 public void Validation_RoundTrip_Default ()
240 Validation_RoundTrip (new MachineKeySection ());
244 public void Validation_RoundTrip_AES ()
246 MachineKeySection section = new MachineKeySection ();
247 section.Validation = MachineKeyValidation.AES;
248 Validation_RoundTrip (section);
252 public void Validation_RoundTrip_TripleDES ()
254 MachineKeySection section = new MachineKeySection ();
255 section.Validation = MachineKeyValidation.TripleDES;
256 Validation_RoundTrip (section);
260 public void Validation_RoundTrip_MD5 ()
262 MachineKeySection section = new MachineKeySection ();
263 section.Validation = MachineKeyValidation.MD5;
264 Validation_RoundTrip (section);
268 public void Validation_RoundTrip_SHA1 ()
270 MachineKeySection section = new MachineKeySection ();
271 section.Validation = MachineKeyValidation.SHA1;
272 Validation_RoundTrip (section);
277 public void Validation_RoundTrip_HMACSHA256 ()
279 MachineKeySection section = new MachineKeySection ();
280 section.Validation = MachineKeyValidation.HMACSHA256;
281 Validation_RoundTrip (section);
285 public void Validation_RoundTrip_HMACSHA384 ()
287 MachineKeySection section = new MachineKeySection ();
288 section.Validation = MachineKeyValidation.HMACSHA384;
289 Validation_RoundTrip (section);
293 public void Validation_RoundTrip_HMACSHA512 ()
295 MachineKeySection section = new MachineKeySection ();
296 section.Validation = MachineKeyValidation.HMACSHA512;
297 Validation_RoundTrip (section);
301 public void Validation_RoundTrip_Custom_RIPEMD160 ()
303 MachineKeySection section = new MachineKeySection ();
304 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
305 Validation_RoundTrip (section);
309 public void GetHexString ()
311 Assert.AreEqual ("DEADC0DE", MachineKeySectionUtils.GetHexString (new byte [] { 0xde, 0xad, 0xc0, 0xde }), "deadcode");