2 // System.Web.Configuration.MachineKeyValidationConverterTest.cs - Unit tests
3 // for System.Web.Configuration.MachineKeyValidationConverter.
6 // Chris Toshok <toshok@ximian.com>
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Web.Configuration;
35 using NUnit.Framework;
37 namespace MonoTests.System.Web.Configuration {
39 public class MachineKeyValidationConverterTest {
41 public void CanConvertFrom ()
43 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
45 Assert.IsTrue (cv.CanConvertFrom (null, typeof (string)), "A1");
46 Assert.IsFalse (cv.CanConvertFrom (null, typeof (TimeSpan)), "A2");
47 Assert.IsFalse (cv.CanConvertFrom (null, typeof (int)), "A3");
48 Assert.IsFalse (cv.CanConvertFrom (null, typeof (object)), "A4");
52 public void CanConvertTo ()
54 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
56 Assert.IsTrue (cv.CanConvertTo (null, typeof (string)), "A1");
57 Assert.IsFalse (cv.CanConvertTo (null, typeof (TimeSpan)), "A2");
58 Assert.IsFalse (cv.CanConvertTo (null, typeof (int)), "A3");
59 Assert.IsFalse (cv.CanConvertTo (null, typeof (object)), "A4");
63 public void ConvertFrom ()
65 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
68 o = cv.ConvertFrom (null, null, "MD5");
69 Assert.AreEqual (typeof (MachineKeyValidation), o.GetType (), "typeof");
70 Assert.AreEqual ("MD5", o.ToString (), "MD5");
72 o = cv.ConvertFrom (null, null, "SHA1");
73 Assert.AreEqual ("SHA1", o.ToString (), "SHA1");
75 // 3DES in, TripleDES out
76 o = cv.ConvertFrom (null, null, "3DES");
77 Assert.AreEqual ("TripleDES", o.ToString (), "3DES");
79 o = cv.ConvertFrom (null, null, "AES");
80 Assert.AreEqual ("AES", o.ToString (), "AES");
82 o = cv.ConvertFrom (null, null, "HMACSHA256");
83 Assert.AreEqual ("HMACSHA256", o.ToString (), "HMACSHA256");
85 o = cv.ConvertFrom (null, null, "HMACSHA384");
86 Assert.AreEqual ("HMACSHA384", o.ToString (), "HMACSHA384");
88 o = cv.ConvertFrom (null, null, "HMACSHA512");
89 Assert.AreEqual ("HMACSHA512", o.ToString (), "HMACSHA512");
95 [ExpectedException (typeof (ArgumentException))]
96 public void ConvertFrom_Custom ()
98 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
99 cv.ConvertFrom (null, null, "Custom");
104 [ExpectedException (typeof (ArgumentException))]
105 public void ConvertFrom_CaseSensitive ()
107 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
108 cv.ConvertFrom (null, null, "sha1");
112 [ExpectedException (typeof (InvalidCastException))]
113 public void ConvertFrom_TypeError ()
115 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
118 o = cv.ConvertFrom (null, null, 6);
119 Assert.IsNull (o, "A1");
123 public void ConvertTo ()
125 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
127 Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (string)), "A1");
128 Assert.AreEqual ("SHA1", cv.ConvertTo (null, null, MachineKeyValidation.SHA1, typeof (string)), "A2");
129 Assert.AreEqual ("3DES", cv.ConvertTo (null, null, MachineKeyValidation.TripleDES, typeof (string)), "A3");
131 Assert.AreEqual ("HMACSHA256", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA256, typeof (string)), "HMACSHA256");
132 Assert.AreEqual ("HMACSHA384", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA384, typeof (string)), "HMACSHA384");
133 Assert.AreEqual ("HMACSHA512", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA512, typeof (string)), "HMACSHA512");
139 [ExpectedException (typeof (ArgumentException))]
140 public void ConvertTo_Custom ()
142 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
143 cv.ConvertTo (null, null, MachineKeyValidation.Custom, typeof (string));
149 [ExpectedException (typeof (ArgumentException))]
151 [ExpectedException (typeof (NullReferenceException))]
153 public void ConvertTo_NullError ()
155 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
157 Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
162 [ExpectedException (typeof (ArgumentException))]
164 [ExpectedException (typeof (FormatException))]
166 public void ConvertTo_TypeError1 ()
168 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
170 Assert.AreEqual ("6", cv.ConvertTo (null, null, 6, typeof (string)), "A1");
174 public void ConvertTo_TypeError2 ()
176 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
178 Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (int)), "A1");
179 Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, null), "A2");
184 [ExpectedException (typeof (ArgumentException))]
186 [ExpectedException (typeof (FormatException))]
188 public void ConvertTo_TypeError3 ()
190 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
191 cv.ConvertTo (null, null, (MachineKeyValidation)Int32.MinValue, typeof (string));