2 // Unit tests for MachineKeySection
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Configuration;
31 using System.Web.Configuration;
32 using NUnit.Framework;
34 namespace MonoTests.System.Web.Configuration {
37 public class MachineKeySectionTest {
40 public void DefaultValues ()
42 MachineKeySection section = new MachineKeySection ();
44 Assert.AreEqual (MachineKeyCompatibilityMode.Framework20SP1, section.CompatibilityMode, "CompatibilityMode");
46 Assert.AreEqual ("Auto", section.Decryption, "Decryption");
47 Assert.AreEqual ("AutoGenerate,IsolateApps", section.DecryptionKey, "DecryptionKey");
49 Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "Validation");
50 Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
52 Assert.AreEqual (MachineKeyValidation.SHA1, section.Validation, "Validation");
54 Assert.AreEqual ("AutoGenerate,IsolateApps", section.ValidationKey, "ValidationKey");
59 [ExpectedException (typeof (NullReferenceException))]
61 [ExpectedException (typeof (ConfigurationErrorsException))]
63 public void Decryption_Null ()
65 MachineKeySection section = new MachineKeySection ();
66 section.Decryption = null;
70 [ExpectedException (typeof (ConfigurationErrorsException))]
71 public void Decryption_Empty ()
73 MachineKeySection section = new MachineKeySection ();
74 section.Decryption = String.Empty;
78 public void Decryption_RC2 ()
80 MachineKeySection section = new MachineKeySection ();
81 Assert.AreEqual ("Auto", section.Decryption, "before");
83 section.Decryption = "alg:RC2";
84 Assert.AreEqual ("alg:RC2", section.Decryption, "after");
88 public void Decryption_InvalidName ()
90 MachineKeySection section = new MachineKeySection ();
91 Assert.AreEqual ("Auto", section.Decryption, "before");
93 section.Decryption = "alg:UnexistingType";
94 // looks like the problem is found (much) later
95 Assert.AreEqual ("alg:UnexistingType", section.Decryption, "Decryption");
99 public void ValidationAlgorithm_Null ()
101 MachineKeySection section = new MachineKeySection ();
102 section.ValidationAlgorithm = null;
103 Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
107 [ExpectedException (typeof (ArgumentException))]
108 public void ValidationAlgorithm_Empty ()
110 MachineKeySection section = new MachineKeySection ();
111 section.ValidationAlgorithm = String.Empty;
115 [ExpectedException (typeof (ArgumentException))]
116 public void Validation_Custom ()
118 MachineKeySection section = new MachineKeySection ();
119 section.Validation = MachineKeyValidation.Custom;
120 // cannot be set directly
124 public void Validation ()
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");
145 [ExpectedException (typeof (ArgumentException))]
146 public void ValidationAlgorithm ()
148 MachineKeySection section = new MachineKeySection ();
149 section.ValidationAlgorithm = "HMACRIPEMD160";
150 // syntax is: alg:something-deriving-from-KeyedHashAlgorithm
154 public void ValidationAlgorithm_RIPEMD160 ()
156 MachineKeySection section = new MachineKeySection ();
157 Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
159 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
160 Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
161 Assert.AreEqual ("alg:HMACRIPEMD160", section.ValidationAlgorithm, "ValidationAlgorithm");
165 public void ValidationAlgorithm_InvalidName ()
167 MachineKeySection section = new MachineKeySection ();
168 Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
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");