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.
28 using System.Security.Cryptography;
33 using System.Web.Security;
35 using MonoTests.Common;
36 using NUnit.Framework;
38 namespace MonoTests.System.Web.Security
41 public class MachineKeyTest
44 [MonoTODO ("Find out why the difference in result sizes exists between .NET and Mono")]
48 const int ALL_EXPECTED_SIZE = 192;
49 const int ENCRYPTION_EXPECTED_SIZE = 128;
51 const int ALL_EXPECTED_SIZE = 128;
52 const int ENCRYPTION_EXPECTED_SIZE = 64;
54 const int VALIDATION_EXPECTED_SIZE = 64;
56 AssertExtensions.Throws<ArgumentNullException> (() => {
57 MachineKey.Encode (null, MachineKeyProtection.All);
60 string result = MachineKey.Encode (new byte[] {}, (MachineKeyProtection)12345);
61 Assert.IsNotNull (result, "#A1-1");
62 Assert.AreEqual (0, result.Length, "#A1-2");
64 result = MachineKey.Encode (new byte[] {}, MachineKeyProtection.All);
65 Assert.IsNotNull (result, "#B1-1");
66 Assert.AreEqual (ALL_EXPECTED_SIZE, result.Length, "#B1-2");
68 result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Encryption);
69 Assert.IsNotNull (result, "#C1-1");
70 Assert.AreEqual (ENCRYPTION_EXPECTED_SIZE, result.Length, "#C1-2");
72 result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Validation);
73 Assert.IsNotNull (result, "#D1-1");
74 Assert.AreEqual (VALIDATION_EXPECTED_SIZE, result.Length, "#D1-2");
82 AssertExtensions.Throws<ArgumentNullException> (() => {
83 MachineKey.Decode (null, MachineKeyProtection.All);
86 AssertExtensions.Throws<ArgumentException> (() => {
87 decoded = MachineKey.Decode (String.Empty, MachineKeyProtection.All);
90 var sb = new StringBuilder ().Append ('0', 192);
91 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection)12345);
92 Assert.IsNotNull (decoded, "#A2-1");
93 Assert.AreEqual (96, decoded.Length, "#A2-2");
95 sb = new StringBuilder ().Append ('0', 128);
96 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
97 Assert.IsNotNull (decoded, "#A3-1");
98 Assert.AreEqual (64, decoded.Length, "#A3-2");
100 sb = new StringBuilder ().Append ('0', 96);
101 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
102 Assert.IsNotNull (decoded, "#A4-1");
103 Assert.AreEqual (48, decoded.Length, "#A4-2");
105 sb = new StringBuilder ().Append ('0', 10);
106 decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
107 Assert.IsNotNull (decoded, "#A5-1");
108 Assert.AreEqual (5, decoded.Length, "#A5-2");
110 AssertExtensions.Throws<ArgumentException> (() => {
111 decoded = MachineKey.Decode ("test", MachineKeyProtection.All);
114 AssertExtensions.Throws<ArgumentException> (() => {
115 decoded = MachineKey.Decode ("test", MachineKeyProtection.Encryption);
118 AssertExtensions.Throws<ArgumentException> (() => {
119 decoded = MachineKey.Decode ("test", MachineKeyProtection.Validation);
122 sb = new StringBuilder ().Append ('0', 1);
124 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
125 Assert.Fail ("#C1-2 [no exception]");
126 } catch (ArgumentException) {
129 Assert.Fail ("#C1-2 [invalid exception]");
132 sb = new StringBuilder ().Append ('0', 2);
134 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
135 } catch (ArgumentException ex) {
136 Console.WriteLine (ex);
137 Assert.Fail ("#C1-3");
142 sb = new StringBuilder ().Append ('0', 193);
144 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
145 Assert.Fail ("#C2-1 [no exception]");
146 } catch (ArgumentException) {
149 Assert.Fail ("#C2-1 [invalid exception]");
152 sb = new StringBuilder ().Append ('0', 129);
154 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
155 Assert.Fail ("#C3-1 [no exception]");
156 } catch (ArgumentException) {
159 Assert.Fail ("#C3-2 [invalid exception]");
162 sb = new StringBuilder ().Append ('0', 64);
164 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
165 } catch (ArgumentException) {
166 Assert.Fail ("#C4-1");
174 public void Protect ()
176 AssertExtensions.Throws<ArgumentNullException> (() =>
177 MachineKey.Protect (null, null),
178 "MachineKey.Protect not throwing an ArgumentNullException");
180 AssertExtensions.Throws<ArgumentNullException> (() =>
181 MachineKey.Protect (null, new [] { "test" }),
182 "MachineKey.Protect not throwing an ArgumentNullException");
184 var testString = "asfgasd43tqrt4";
185 var validUsages = new [] { "usage1", "usage2" };
186 var oneUsage = new [] { "usage1" };
187 var invalidUsages = new [] { "usage1", "invalidUsage" };
189 var plainBytes = Encoding.ASCII.GetBytes (testString);
190 var encryptedBytes = MachineKey.Protect (plainBytes, validUsages);
191 var validDecryptedBytes = MachineKey.Unprotect (encryptedBytes, validUsages);
193 Assert.AreEqual (plainBytes, validDecryptedBytes, "Decryption didn't work");
195 AssertExtensions.Throws<CryptographicException> (() =>
196 MachineKey.Unprotect (encryptedBytes, invalidUsages),
197 "Purposes not encrypting properly");
199 AssertExtensions.Throws<CryptographicException> (() =>
200 MachineKey.Unprotect (encryptedBytes, oneUsage),
201 "Single purpose working when multiple supplied");