A few moves/changes following previous patch review
[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 #if NET_2_0
32
33 using System;
34 using System.Web.Configuration;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Web.Configuration {
38         [TestFixture]
39         public class MachineKeyValidationConverterTest {
40                 [Test]
41                 public void CanConvertFrom ()
42                 {
43                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
44
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");
49                 }
50
51                 [Test]
52                 public void CanConvertTo ()
53                 {
54                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
55
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");
60                 }
61
62                 [Test]
63                 public void ConvertFrom ()
64                 {
65                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
66                         object o;
67
68                         o = cv.ConvertFrom (null, null, "MD5");
69                         Assert.AreEqual (typeof (MachineKeyValidation), o.GetType (), "typeof");
70                         Assert.AreEqual ("MD5", o.ToString (), "MD5");
71
72                         o = cv.ConvertFrom (null, null, "SHA1");
73                         Assert.AreEqual ("SHA1", o.ToString (), "SHA1");
74
75                         // 3DES in, TripleDES out
76                         o = cv.ConvertFrom (null, null, "3DES");
77                         Assert.AreEqual ("TripleDES", o.ToString (), "3DES");
78
79                         o = cv.ConvertFrom (null, null, "AES");
80                         Assert.AreEqual ("AES", o.ToString (), "AES");
81 #if NET_4_0
82                         o = cv.ConvertFrom (null, null, "HMACSHA256");
83                         Assert.AreEqual ("HMACSHA256", o.ToString (), "HMACSHA256");
84
85                         o = cv.ConvertFrom (null, null, "HMACSHA384");
86                         Assert.AreEqual ("HMACSHA384", o.ToString (), "HMACSHA384");
87
88                         o = cv.ConvertFrom (null, null, "HMACSHA512");
89                         Assert.AreEqual ("HMACSHA512", o.ToString (), "HMACSHA512");
90 #endif
91                 }
92
93 #if NET_4_0
94                 [Test]
95                 [ExpectedException (typeof (ArgumentException))]
96                 public void ConvertFrom_Custom ()
97                 {
98                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
99                         cv.ConvertFrom (null, null, "Custom");
100                 }
101 #endif
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentException))]
105                 public void ConvertFrom_CaseSensitive ()
106                 {
107                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
108                         cv.ConvertFrom (null, null, "sha1");
109                 }
110
111                 [Test]
112                 [ExpectedException (typeof (InvalidCastException))]
113                 public void ConvertFrom_TypeError ()
114                 {
115                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
116                         object o;
117
118                         o = cv.ConvertFrom (null, null, 6);
119                         Assert.IsNull (o, "A1");
120                 }
121
122                 [Test]
123                 public void ConvertTo ()
124                 {
125                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
126
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");
130 #if NET_4_0
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");
134 #endif
135                 }
136
137 #if NET_4_0
138                 [Test]
139                 [ExpectedException (typeof (ArgumentException))]
140                 public void ConvertTo_Custom ()
141                 {
142                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
143                         cv.ConvertTo (null, null, MachineKeyValidation.Custom, typeof (string));
144                 }
145 #endif
146
147                 [Test]
148 #if NET_4_0
149                 [ExpectedException (typeof (ArgumentException))]
150 #else
151                 [ExpectedException (typeof (NullReferenceException))]
152 #endif
153                 public void ConvertTo_NullError ()
154                 {
155                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
156
157                         Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
158                 }
159
160                 [Test]
161 #if NET_4_0
162                 [ExpectedException (typeof (ArgumentException))]
163 #else
164                 [ExpectedException (typeof (FormatException))]
165 #endif
166                 public void ConvertTo_TypeError1 ()
167                 {
168                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
169
170                         Assert.AreEqual ("6", cv.ConvertTo (null, null, 6, typeof (string)), "A1");
171                 }
172
173                 [Test]
174                 public void ConvertTo_TypeError2 ()
175                 {
176                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
177
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");
180                 }
181
182                 [Test]
183 #if NET_4_0
184                 [ExpectedException (typeof (ArgumentException))]
185 #else
186                 [ExpectedException (typeof (FormatException))]
187 #endif
188                 public void ConvertTo_TypeError3 ()
189                 {
190                         MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
191                         cv.ConvertTo (null, null, (MachineKeyValidation)Int32.MinValue, typeof (string));
192                 }
193         }
194 }
195
196 #endif