f99158367cc21b9f01ca798837c371444533f6b7
[mono.git] / mcs / class / System.Web / Test / System.Web.Configuration / MachineKeyValidationConverterTest.cs
1 //
2 // System.Web.Configuration.MachineKeyValidationConverterTest.cs - Unit tests
3 // for System.Web.Configuration.MachineKeyValidationConverter.
4 //
5 // Authors:
6 //      Chris Toshok  <toshok@ximian.com>
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31
32 using System;
33 using System.Web.Configuration;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Web.Configuration {
37         [TestFixture]
38         public class MachineKeyValidationConverterTest {
39                 [Test]
40                 public void CanConvertFrom ()
41                 {
42                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
43
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");
48                 }
49
50                 [Test]
51                 public void CanConvertTo ()
52                 {
53                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
54
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");
59                 }
60
61                 [Test]
62                 public void ConvertFrom ()
63                 {
64                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
65                         object o;
66
67                         o = cv.ConvertFrom (null, null, "MD5");
68                         Assert.AreEqual (typeof (MachineKeyValidation), o.GetType (), "typeof");
69                         Assert.AreEqual ("MD5", o.ToString (), "MD5");
70
71                         o = cv.ConvertFrom (null, null, "SHA1");
72                         Assert.AreEqual ("SHA1", o.ToString (), "SHA1");
73
74                         // 3DES in, TripleDES out
75                         o = cv.ConvertFrom (null, null, "3DES");
76                         Assert.AreEqual ("TripleDES", o.ToString (), "3DES");
77
78                         o = cv.ConvertFrom (null, null, "AES");
79                         Assert.AreEqual ("AES", o.ToString (), "AES");
80 #if NET_4_0
81                         o = cv.ConvertFrom (null, null, "HMACSHA256");
82                         Assert.AreEqual ("HMACSHA256", o.ToString (), "HMACSHA256");
83
84                         o = cv.ConvertFrom (null, null, "HMACSHA384");
85                         Assert.AreEqual ("HMACSHA384", o.ToString (), "HMACSHA384");
86
87                         o = cv.ConvertFrom (null, null, "HMACSHA512");
88                         Assert.AreEqual ("HMACSHA512", o.ToString (), "HMACSHA512");
89 #endif
90                 }
91
92 #if NET_4_0
93                 [Test]
94                 [ExpectedException (typeof (ArgumentException))]
95                 public void ConvertFrom_Custom ()
96                 {
97                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
98                         cv.ConvertFrom (null, null, "Custom");
99                 }
100 #endif
101
102                 [Test]
103                 [ExpectedException (typeof (ArgumentException))]
104                 public void ConvertFrom_CaseSensitive ()
105                 {
106                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
107                         cv.ConvertFrom (null, null, "sha1");
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (InvalidCastException))]
112                 public void ConvertFrom_TypeError ()
113                 {
114                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
115                         object o;
116
117                         o = cv.ConvertFrom (null, null, 6);
118                         Assert.IsNull (o, "A1");
119                 }
120
121                 [Test]
122                 public void ConvertTo ()
123                 {
124                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
125
126                         Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (string)), "A1");
127                         Assert.AreEqual ("SHA1", cv.ConvertTo (null, null, MachineKeyValidation.SHA1, typeof (string)), "A2");
128                         Assert.AreEqual ("3DES", cv.ConvertTo (null, null, MachineKeyValidation.TripleDES, typeof (string)), "A3");
129 #if NET_4_0
130                         Assert.AreEqual ("HMACSHA256", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA256, typeof (string)), "HMACSHA256");
131                         Assert.AreEqual ("HMACSHA384", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA384, typeof (string)), "HMACSHA384");
132                         Assert.AreEqual ("HMACSHA512", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA512, typeof (string)), "HMACSHA512");
133 #endif
134                 }
135
136 #if NET_4_0
137                 [Test]
138                 [ExpectedException (typeof (ArgumentException))]
139                 public void ConvertTo_Custom ()
140                 {
141                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
142                         cv.ConvertTo (null, null, MachineKeyValidation.Custom, typeof (string));
143                 }
144 #endif
145
146                 [Test]
147 #if NET_4_0
148                 [ExpectedException (typeof (ArgumentException))]
149 #else
150                 [ExpectedException (typeof (NullReferenceException))]
151 #endif
152                 public void ConvertTo_NullError ()
153                 {
154                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
155
156                         Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
157                 }
158
159                 [Test]
160 #if NET_4_0
161                 [ExpectedException (typeof (ArgumentException))]
162 #else
163                 [ExpectedException (typeof (FormatException))]
164 #endif
165                 public void ConvertTo_TypeError1 ()
166                 {
167                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
168
169                         Assert.AreEqual ("6", cv.ConvertTo (null, null, 6, typeof (string)), "A1");
170                 }
171
172                 [Test]
173                 public void ConvertTo_TypeError2 ()
174                 {
175                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
176
177                         Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (int)), "A1");
178                         Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, null), "A2");
179                 }
180
181                 [Test]
182 #if NET_4_0
183                 [ExpectedException (typeof (ArgumentException))]
184 #else
185                 [ExpectedException (typeof (FormatException))]
186 #endif
187                 public void ConvertTo_TypeError3 ()
188                 {
189                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
190                         cv.ConvertTo (null, null, (MachineKeyValidation)Int32.MinValue, typeof (string));
191                 }
192         }
193 }
194