[bcl] Remove NET_4_0 defines from class libs
[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                         Assert.AreEqual (MachineKeyCompatibilityMode.Framework20SP1, section.CompatibilityMode, "CompatibilityMode");
44                         Assert.AreEqual ("Auto", section.Decryption, "Decryption");
45                         Assert.AreEqual ("AutoGenerate,IsolateApps", section.DecryptionKey, "DecryptionKey");
46                         Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "Validation");
47                         Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
48                         Assert.AreEqual ("AutoGenerate,IsolateApps", section.ValidationKey, "ValidationKey");
49                 }
50
51                 [Test]
52                 [ExpectedException (typeof (NullReferenceException))]
53                 public void Decryption_Null ()
54                 {
55                         MachineKeySection section = new MachineKeySection ();
56                         section.Decryption = null;
57                 }
58
59                 [Test]
60                 [ExpectedException (typeof (ConfigurationErrorsException))]
61                 public void Decryption_Empty ()
62                 {
63                         MachineKeySection section = new MachineKeySection ();
64                         section.Decryption = String.Empty;
65                 }
66                 [Test]
67                 public void Decryption_RC2 ()
68                 {
69                         MachineKeySection section = new MachineKeySection ();
70                         Assert.AreEqual ("Auto", section.Decryption, "before");
71
72                         section.Decryption = "alg:RC2";
73                         Assert.AreEqual ("alg:RC2", section.Decryption, "after");
74                 }
75
76                 [Test]
77                 public void Decryption_InvalidName ()
78                 {
79                         MachineKeySection section = new MachineKeySection ();
80                         Assert.AreEqual ("Auto", section.Decryption, "before");
81
82                         section.Decryption = "alg:UnexistingType";
83                         // looks like the problem is found (much) later
84                         Assert.AreEqual ("alg:UnexistingType", section.Decryption, "Decryption");
85                 }
86
87                 [Test]
88                 public void ValidationAlgorithm_Null ()
89                 {
90                         MachineKeySection section = new MachineKeySection ();
91                         section.ValidationAlgorithm = null;
92                         Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (ArgumentException))]
97                 public void ValidationAlgorithm_Empty ()
98                 {
99                         MachineKeySection section = new MachineKeySection ();
100                         section.ValidationAlgorithm = String.Empty;
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentException))]
105                 public void Validation_Custom ()
106                 {
107                         MachineKeySection section = new MachineKeySection ();
108                         section.Validation = MachineKeyValidation.Custom;
109                         // cannot be set directly
110                 }
111
112                 [Test]
113                 public void Validation ()
114                 {
115                         MachineKeySection section = new MachineKeySection ();
116                         section.Validation = MachineKeyValidation.AES;
117                         Assert.AreEqual ("AES", section.ValidationAlgorithm, "AES");
118                         section.Validation = MachineKeyValidation.HMACSHA256;
119                         Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "HMACSHA256");
120                         section.Validation = MachineKeyValidation.HMACSHA384;
121                         Assert.AreEqual ("HMACSHA384", section.ValidationAlgorithm, "HMACSHA384");
122                         section.Validation = MachineKeyValidation.HMACSHA512;
123                         Assert.AreEqual ("HMACSHA512", section.ValidationAlgorithm, "HMACSHA512");
124                         section.Validation = MachineKeyValidation.MD5;
125                         Assert.AreEqual ("MD5", section.ValidationAlgorithm, "MD5");
126                         section.Validation = MachineKeyValidation.SHA1;
127                         Assert.AreEqual ("SHA1", section.ValidationAlgorithm, "SHA1");
128                         // special case, enum value and algorithm names differs
129                         section.Validation = MachineKeyValidation.TripleDES;
130                         Assert.AreEqual ("3DES", section.ValidationAlgorithm, "3DES");
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (ArgumentException))]
135                 public void ValidationAlgorithm ()
136                 {
137                         MachineKeySection section = new MachineKeySection ();
138                         section.ValidationAlgorithm = "HMACRIPEMD160";
139                         // syntax is: alg:something-deriving-from-KeyedHashAlgorithm
140                 }
141
142                 [Test]
143                 public void ValidationAlgorithm_RIPEMD160 ()
144                 {
145                         MachineKeySection section = new MachineKeySection ();
146                         Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
147
148                         section.ValidationAlgorithm = "alg:HMACRIPEMD160";
149                         Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
150                         Assert.AreEqual ("alg:HMACRIPEMD160", section.ValidationAlgorithm, "ValidationAlgorithm");
151                 }
152
153                 [Test]
154                 public void ValidationAlgorithm_InvalidName ()
155                 {
156                         MachineKeySection section = new MachineKeySection ();
157                         Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
158
159                         section.ValidationAlgorithm = "alg:UnexistingType";
160                         // looks like the problem is found (much) later
161                         Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
162                         Assert.AreEqual ("alg:UnexistingType", section.ValidationAlgorithm, "ValidationAlgorithm");
163                 }
164         }
165 }
166