Merge branch 'cecil-light'
[mono.git] / mcs / class / System.Web / Test / System.Web.Configuration / MachineKeySectionTest.cs
1 //
2 // Unit tests for MachineKeySection
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Configuration;
31 using System.Web.Configuration;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Web.Configuration {
35
36         [TestFixture]
37         public class MachineKeySectionTest {
38
39                 [Test]
40                 public void DefaultValues ()
41                 {
42                         MachineKeySection section = new MachineKeySection ();
43 #if NET_4_0
44                         Assert.AreEqual (MachineKeyCompatibilityMode.Framework20SP1, section.CompatibilityMode, "CompatibilityMode");
45 #endif
46                         Assert.AreEqual ("Auto", section.Decryption, "Decryption");
47                         Assert.AreEqual ("AutoGenerate,IsolateApps", section.DecryptionKey, "DecryptionKey");
48 #if NET_4_0
49                         Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "Validation");
50                         Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
51 #else
52                         Assert.AreEqual (MachineKeyValidation.SHA1, section.Validation, "Validation");
53 #endif
54                         Assert.AreEqual ("AutoGenerate,IsolateApps", section.ValidationKey, "ValidationKey");
55                 }
56
57                 [Test]
58 #if NET_4_0
59                 [ExpectedException (typeof (NullReferenceException))]
60 #else
61                 [ExpectedException (typeof (ConfigurationErrorsException))]
62 #endif
63                 public void Decryption_Null ()
64                 {
65                         MachineKeySection section = new MachineKeySection ();
66                         section.Decryption = null;
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ConfigurationErrorsException))]
71                 public void Decryption_Empty ()
72                 {
73                         MachineKeySection section = new MachineKeySection ();
74                         section.Decryption = String.Empty;
75                 }
76 #if NET_4_0
77                 [Test]
78                 public void Decryption_RC2 ()
79                 {
80                         MachineKeySection section = new MachineKeySection ();
81                         Assert.AreEqual ("Auto", section.Decryption, "before");
82
83                         section.Decryption = "alg:RC2";
84                         Assert.AreEqual ("alg:RC2", section.Decryption, "after");
85                 }
86
87                 [Test]
88                 public void Decryption_InvalidName ()
89                 {
90                         MachineKeySection section = new MachineKeySection ();
91                         Assert.AreEqual ("Auto", section.Decryption, "before");
92
93                         section.Decryption = "alg:UnexistingType";
94                         // looks like the problem is found (much) later
95                         Assert.AreEqual ("alg:UnexistingType", section.Decryption, "Decryption");
96                 }
97
98                 [Test]
99                 public void ValidationAlgorithm_Null ()
100                 {
101                         MachineKeySection section = new MachineKeySection ();
102                         section.ValidationAlgorithm = null;
103                         Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (ArgumentException))]
108                 public void ValidationAlgorithm_Empty ()
109                 {
110                         MachineKeySection section = new MachineKeySection ();
111                         section.ValidationAlgorithm = String.Empty;
112                 }
113
114                 [Test]
115                 [ExpectedException (typeof (ArgumentException))]
116                 public void Validation_Custom ()
117                 {
118                         MachineKeySection section = new MachineKeySection ();
119                         section.Validation = MachineKeyValidation.Custom;
120                         // cannot be set directly
121                 }
122
123                 [Test]
124                 public void Validation ()
125                 {
126                         MachineKeySection section = new MachineKeySection ();
127                         section.Validation = MachineKeyValidation.AES;
128                         Assert.AreEqual ("AES", section.ValidationAlgorithm, "AES");
129                         section.Validation = MachineKeyValidation.HMACSHA256;
130                         Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "HMACSHA256");
131                         section.Validation = MachineKeyValidation.HMACSHA384;
132                         Assert.AreEqual ("HMACSHA384", section.ValidationAlgorithm, "HMACSHA384");
133                         section.Validation = MachineKeyValidation.HMACSHA512;
134                         Assert.AreEqual ("HMACSHA512", section.ValidationAlgorithm, "HMACSHA512");
135                         section.Validation = MachineKeyValidation.MD5;
136                         Assert.AreEqual ("MD5", section.ValidationAlgorithm, "MD5");
137                         section.Validation = MachineKeyValidation.SHA1;
138                         Assert.AreEqual ("SHA1", section.ValidationAlgorithm, "SHA1");
139                         // special case, enum value and algorithm names differs
140                         section.Validation = MachineKeyValidation.TripleDES;
141                         Assert.AreEqual ("3DES", section.ValidationAlgorithm, "3DES");
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (ArgumentException))]
146                 public void ValidationAlgorithm ()
147                 {
148                         MachineKeySection section = new MachineKeySection ();
149                         section.ValidationAlgorithm = "HMACRIPEMD160";
150                         // syntax is: alg:something-deriving-from-KeyedHashAlgorithm
151                 }
152
153                 [Test]
154                 public void ValidationAlgorithm_RIPEMD160 ()
155                 {
156                         MachineKeySection section = new MachineKeySection ();
157                         Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
158
159                         section.ValidationAlgorithm = "alg:HMACRIPEMD160";
160                         Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
161                         Assert.AreEqual ("alg:HMACRIPEMD160", section.ValidationAlgorithm, "ValidationAlgorithm");
162                 }
163
164                 [Test]
165                 public void ValidationAlgorithm_InvalidName ()
166                 {
167                         MachineKeySection section = new MachineKeySection ();
168                         Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
169
170                         section.ValidationAlgorithm = "alg:UnexistingType";
171                         // looks like the problem is found (much) later
172                         Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
173                         Assert.AreEqual ("alg:UnexistingType", section.ValidationAlgorithm, "ValidationAlgorithm");
174                 }
175 #endif
176         }
177 }
178