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.
33 using System.Web.Configuration;
34 using NUnit.Framework;
36 namespace MonoTests.System.Web.Configuration {
38 public class MachineKeyValidationConverterTest {
40 public void CanConvertFrom ()
42 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
44 Assert.IsTrue (cv.CanConvertFrom (null, typeof (string)), "A1");
45 Assert.IsFalse (cv.CanConvertFrom (null, typeof (TimeSpan)), "A2");
46 Assert.IsFalse (cv.CanConvertFrom (null, typeof (int)), "A3");
47 Assert.IsFalse (cv.CanConvertFrom (null, typeof (object)), "A4");
51 public void CanConvertTo ()
53 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
55 Assert.IsTrue (cv.CanConvertTo (null, typeof (string)), "A1");
56 Assert.IsFalse (cv.CanConvertTo (null, typeof (TimeSpan)), "A2");
57 Assert.IsFalse (cv.CanConvertTo (null, typeof (int)), "A3");
58 Assert.IsFalse (cv.CanConvertTo (null, typeof (object)), "A4");
62 public void ConvertFrom ()
64 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
67 o = cv.ConvertFrom (null, null, "MD5");
68 Assert.AreEqual (typeof (MachineKeyValidation), o.GetType (), "typeof");
69 Assert.AreEqual ("MD5", o.ToString (), "MD5");
71 o = cv.ConvertFrom (null, null, "SHA1");
72 Assert.AreEqual ("SHA1", o.ToString (), "SHA1");
74 // 3DES in, TripleDES out
75 o = cv.ConvertFrom (null, null, "3DES");
76 Assert.AreEqual ("TripleDES", o.ToString (), "3DES");
78 o = cv.ConvertFrom (null, null, "AES");
79 Assert.AreEqual ("AES", o.ToString (), "AES");
80 o = cv.ConvertFrom (null, null, "HMACSHA256");
81 Assert.AreEqual ("HMACSHA256", o.ToString (), "HMACSHA256");
83 o = cv.ConvertFrom (null, null, "HMACSHA384");
84 Assert.AreEqual ("HMACSHA384", o.ToString (), "HMACSHA384");
86 o = cv.ConvertFrom (null, null, "HMACSHA512");
87 Assert.AreEqual ("HMACSHA512", o.ToString (), "HMACSHA512");
91 [ExpectedException (typeof (ArgumentException))]
92 public void ConvertFrom_Custom ()
94 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
95 cv.ConvertFrom (null, null, "Custom");
99 [ExpectedException (typeof (ArgumentException))]
100 public void ConvertFrom_CaseSensitive ()
102 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
103 cv.ConvertFrom (null, null, "sha1");
107 [ExpectedException (typeof (InvalidCastException))]
108 public void ConvertFrom_TypeError ()
110 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
113 o = cv.ConvertFrom (null, null, 6);
114 Assert.IsNull (o, "A1");
118 public void ConvertTo ()
120 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
122 Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (string)), "A1");
123 Assert.AreEqual ("SHA1", cv.ConvertTo (null, null, MachineKeyValidation.SHA1, typeof (string)), "A2");
124 Assert.AreEqual ("3DES", cv.ConvertTo (null, null, MachineKeyValidation.TripleDES, typeof (string)), "A3");
125 Assert.AreEqual ("HMACSHA256", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA256, typeof (string)), "HMACSHA256");
126 Assert.AreEqual ("HMACSHA384", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA384, typeof (string)), "HMACSHA384");
127 Assert.AreEqual ("HMACSHA512", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA512, typeof (string)), "HMACSHA512");
131 [ExpectedException (typeof (ArgumentException))]
132 public void ConvertTo_Custom ()
134 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
135 cv.ConvertTo (null, null, MachineKeyValidation.Custom, typeof (string));
139 [ExpectedException (typeof (ArgumentException))]
140 public void ConvertTo_NullError ()
142 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
144 Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
148 [ExpectedException (typeof (ArgumentException))]
149 public void ConvertTo_TypeError1 ()
151 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
153 Assert.AreEqual ("6", cv.ConvertTo (null, null, 6, typeof (string)), "A1");
157 public void ConvertTo_TypeError2 ()
159 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
161 Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (int)), "A1");
162 Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, null), "A2");
166 [ExpectedException (typeof (ArgumentException))]
167 public void ConvertTo_TypeError3 ()
169 MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
170 cv.ConvertTo (null, null, (MachineKeyValidation)Int32.MinValue, typeof (string));