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 ();
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");
52 [ExpectedException (typeof (NullReferenceException))]
53 public void Decryption_Null ()
55 MachineKeySection section = new MachineKeySection ();
56 section.Decryption = null;
60 [ExpectedException (typeof (ConfigurationErrorsException))]
61 public void Decryption_Empty ()
63 MachineKeySection section = new MachineKeySection ();
64 section.Decryption = String.Empty;
67 public void Decryption_RC2 ()
69 MachineKeySection section = new MachineKeySection ();
70 Assert.AreEqual ("Auto", section.Decryption, "before");
72 section.Decryption = "alg:RC2";
73 Assert.AreEqual ("alg:RC2", section.Decryption, "after");
77 public void Decryption_InvalidName ()
79 MachineKeySection section = new MachineKeySection ();
80 Assert.AreEqual ("Auto", section.Decryption, "before");
82 section.Decryption = "alg:UnexistingType";
83 // looks like the problem is found (much) later
84 Assert.AreEqual ("alg:UnexistingType", section.Decryption, "Decryption");
88 public void ValidationAlgorithm_Null ()
90 MachineKeySection section = new MachineKeySection ();
91 section.ValidationAlgorithm = null;
92 Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
96 [ExpectedException (typeof (ArgumentException))]
97 public void ValidationAlgorithm_Empty ()
99 MachineKeySection section = new MachineKeySection ();
100 section.ValidationAlgorithm = String.Empty;
104 [ExpectedException (typeof (ArgumentException))]
105 public void Validation_Custom ()
107 MachineKeySection section = new MachineKeySection ();
108 section.Validation = MachineKeyValidation.Custom;
109 // cannot be set directly
113 public void Validation ()
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");
134 [ExpectedException (typeof (ArgumentException))]
135 public void ValidationAlgorithm ()
137 MachineKeySection section = new MachineKeySection ();
138 section.ValidationAlgorithm = "HMACRIPEMD160";
139 // syntax is: alg:something-deriving-from-KeyedHashAlgorithm
143 public void ValidationAlgorithm_RIPEMD160 ()
145 MachineKeySection section = new MachineKeySection ();
146 Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
148 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
149 Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
150 Assert.AreEqual ("alg:HMACRIPEMD160", section.ValidationAlgorithm, "ValidationAlgorithm");
154 public void ValidationAlgorithm_InvalidName ()
156 MachineKeySection section = new MachineKeySection ();
157 Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
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");