3 // Marek Habersack <grendel@twistedcode.net>
5 // (C) 2011 Novell, Inc (http://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.
31 using System.Web.Security;
33 using MonoTests.Common;
34 using NUnit.Framework;
36 namespace MonoTests.System.Web.Security
39 public class MachineKeyTest
42 [MonoTODO ("Find out why the difference in result sizes exists between .NET and Mono")]
46 const int ALL_EXPECTED_SIZE = 192;
47 const int ENCRYPTION_EXPECTED_SIZE = 128;
49 const int ALL_EXPECTED_SIZE = 128;
50 const int ENCRYPTION_EXPECTED_SIZE = 64;
52 const int VALIDATION_EXPECTED_SIZE = 64;
54 AssertExtensions.Throws<ArgumentNullException> (() => {
55 MachineKey.Encode (null, MachineKeyProtection.All);
58 string result = MachineKey.Encode (new byte[] {}, (MachineKeyProtection)12345);
59 Assert.IsNotNull (result, "#A1-1");
60 Assert.AreEqual (0, result.Length, "#A1-2");
62 result = MachineKey.Encode (new byte[] {}, MachineKeyProtection.All);
63 Assert.IsNotNull (result, "#B1-1");
64 Assert.AreEqual (ALL_EXPECTED_SIZE, result.Length, "#B1-2");
66 result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Encryption);
67 Assert.IsNotNull (result, "#C1-1");
68 Assert.AreEqual (ENCRYPTION_EXPECTED_SIZE, result.Length, "#C1-2");
70 result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Validation);
71 Assert.IsNotNull (result, "#D1-1");
72 Assert.AreEqual (VALIDATION_EXPECTED_SIZE, result.Length, "#D1-2");
80 AssertExtensions.Throws<ArgumentNullException> (() => {
81 MachineKey.Decode (null, MachineKeyProtection.All);
84 AssertExtensions.Throws<ArgumentException> (() => {
85 decoded = MachineKey.Decode (String.Empty, MachineKeyProtection.All);
88 var sb = new StringBuilder ().Append ('0', 192);
89 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection)12345);
90 Assert.IsNotNull (decoded, "#A2-1");
91 Assert.AreEqual (96, decoded.Length, "#A2-2");
93 sb = new StringBuilder ().Append ('0', 128);
94 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
95 Assert.IsNotNull (decoded, "#A3-1");
96 Assert.AreEqual (64, decoded.Length, "#A3-2");
98 sb = new StringBuilder ().Append ('0', 96);
99 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
100 Assert.IsNotNull (decoded, "#A4-1");
101 Assert.AreEqual (48, decoded.Length, "#A4-2");
103 sb = new StringBuilder ().Append ('0', 10);
104 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
105 Assert.IsNotNull (decoded, "#A5-1");
106 Assert.AreEqual (5, decoded.Length, "#A5-2");
108 AssertExtensions.Throws<ArgumentException> (() => {
109 decoded = MachineKey.Decode ("test", MachineKeyProtection.All);
112 AssertExtensions.Throws<ArgumentException> (() => {
113 decoded = MachineKey.Decode ("test", MachineKeyProtection.Encryption);
116 AssertExtensions.Throws<ArgumentException> (() => {
117 decoded = MachineKey.Decode ("test", MachineKeyProtection.Validation);
120 sb = new StringBuilder ().Append ('0', 1);
122 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
123 Assert.Fail ("#C1-2 [no exception]");
124 } catch (ArgumentException) {
127 Assert.Fail ("#C1-2 [invalid exception]");
130 sb = new StringBuilder ().Append ('0', 2);
132 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
133 } catch (ArgumentException ex) {
134 Console.WriteLine (ex);
135 Assert.Fail ("#C1-3");
140 sb = new StringBuilder ().Append ('0', 193);
142 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
143 Assert.Fail ("#C2-1 [no exception]");
144 } catch (ArgumentException) {
147 Assert.Fail ("#C2-1 [invalid exception]");
150 sb = new StringBuilder ().Append ('0', 129);
152 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
153 Assert.Fail ("#C3-1 [no exception]");
154 } catch (ArgumentException) {
157 Assert.Fail ("#C3-2 [invalid exception]");
160 sb = new StringBuilder ().Append ('0', 64);
162 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
163 } catch (ArgumentException) {
164 Assert.Fail ("#C4-1");