[sgen] Make sure we don't sweep a block if we're not supposed to
[mono.git] / mcs / class / System.Web / Test / System.Web.Security / MachineKeyTest.cs
1 //
2 // Authors:
3 //   Marek Habersack <grendel@twistedcode.net>
4 //
5 // (C) 2011 Novell, Inc (http://novell.com/)
6 //
7
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 using System.Security.Cryptography;
29
30
31 using System;
32 using System.Text;
33 using System.Web.Security;
34
35 using MonoTests.Common;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Web.Security
39 {
40         [TestFixture]
41         public class MachineKeyTest
42         {
43                 [Test]
44                 [MonoTODO ("Find out why the difference in result sizes exists between .NET and Mono")]
45                 public void Encode ()
46                 {
47 #if DOT_NET
48                         const int ALL_EXPECTED_SIZE = 192;
49                         const int ENCRYPTION_EXPECTED_SIZE = 128;
50 #else
51                         const int ALL_EXPECTED_SIZE = 128;
52                         const int ENCRYPTION_EXPECTED_SIZE = 64;
53 #endif
54                         const int VALIDATION_EXPECTED_SIZE = 64;
55                         
56                         AssertExtensions.Throws<ArgumentNullException> (() => {
57                                 MachineKey.Encode (null, MachineKeyProtection.All);
58                         }, "#A1-1");
59
60                         string result = MachineKey.Encode (new byte[] {}, (MachineKeyProtection)12345);
61                         Assert.IsNotNull (result, "#A1-1");
62                         Assert.AreEqual (0, result.Length, "#A1-2");
63
64                         result = MachineKey.Encode (new byte[] {}, MachineKeyProtection.All);
65                         Assert.IsNotNull (result, "#B1-1");
66                         Assert.AreEqual (ALL_EXPECTED_SIZE, result.Length, "#B1-2");
67
68                         result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Encryption);
69                         Assert.IsNotNull (result, "#C1-1");
70                         Assert.AreEqual (ENCRYPTION_EXPECTED_SIZE, result.Length, "#C1-2");
71
72                         result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Validation);
73                         Assert.IsNotNull (result, "#D1-1");
74                         Assert.AreEqual (VALIDATION_EXPECTED_SIZE, result.Length, "#D1-2");
75                 }
76
77                 [Test]
78                 public void Decode ()
79                 {
80                         byte[] decoded;
81
82                         AssertExtensions.Throws<ArgumentNullException> (() => {
83                                 MachineKey.Decode (null, MachineKeyProtection.All);
84                         }, "#A1-1");
85
86                         AssertExtensions.Throws<ArgumentException> (() => {
87                                 decoded = MachineKey.Decode (String.Empty, MachineKeyProtection.All);
88                         }, "#A1-2");
89
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");
94
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");
99
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");
104
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");
109
110                         AssertExtensions.Throws<ArgumentException> (() => {
111                                 decoded = MachineKey.Decode ("test", MachineKeyProtection.All);
112                         }, "#B1-1");
113
114                         AssertExtensions.Throws<ArgumentException> (() => {
115                                 decoded = MachineKey.Decode ("test", MachineKeyProtection.Encryption);
116                         }, "#B1-2");
117
118                         AssertExtensions.Throws<ArgumentException> (() => {
119                                 decoded = MachineKey.Decode ("test", MachineKeyProtection.Validation);
120                         }, "#B1-3");
121
122                         sb = new StringBuilder ().Append ('0', 1);
123                         try {
124                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
125                                 Assert.Fail ("#C1-2 [no exception]");
126                         } catch (ArgumentException) {
127                                 // success
128                         } catch {
129                                 Assert.Fail ("#C1-2 [invalid exception]");
130                         }
131
132                         sb = new StringBuilder ().Append ('0', 2);
133                         try {
134                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
135                         } catch (ArgumentException ex) {
136                                 Console.WriteLine (ex);
137                                 Assert.Fail ("#C1-3");
138                         } catch {
139                                 // success
140                         }
141
142                         sb = new StringBuilder ().Append ('0', 193);
143                         try {
144                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
145                                 Assert.Fail ("#C2-1 [no exception]");
146                         } catch (ArgumentException) {
147                                 // success
148                         } catch {
149                                 Assert.Fail ("#C2-1 [invalid exception]");
150                         }
151
152                         sb = new StringBuilder ().Append ('0', 129);
153                         try {
154                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
155                                 Assert.Fail ("#C3-1 [no exception]");
156                         } catch (ArgumentException) {
157                                 // success
158                         } catch {
159                                 Assert.Fail ("#C3-2 [invalid exception]");
160                         }
161
162                         sb = new StringBuilder ().Append ('0', 64);
163                         try {
164                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
165                         } catch (ArgumentException) {
166                                 Assert.Fail ("#C4-1");
167                         } catch {
168                                 // Success
169                         }
170                 }
171
172 #if NET_4_5
173                 [Test]
174                 public void Protect ()
175                 {
176                         AssertExtensions.Throws<ArgumentNullException> (() =>
177                                 MachineKey.Protect (null, null), 
178                                 "MachineKey.Protect not throwing an ArgumentNullException");
179
180                         AssertExtensions.Throws<ArgumentNullException> (() => 
181                                 MachineKey.Protect (null, new [] { "test" }), 
182                                 "MachineKey.Protect not throwing an ArgumentNullException");
183
184                         var testString = "asfgasd43tqrt4";
185                         var validUsages = new [] { "usage1", "usage2" };
186                         var oneUsage = new [] { "usage1" };
187                         var invalidUsages = new [] { "usage1", "invalidUsage" };
188
189                         var plainBytes = Encoding.ASCII.GetBytes (testString);
190                         var encryptedBytes = MachineKey.Protect (plainBytes, validUsages);
191                         var validDecryptedBytes = MachineKey.Unprotect (encryptedBytes, validUsages);
192
193                         Assert.AreEqual (plainBytes, validDecryptedBytes, "Decryption didn't work");
194
195                         AssertExtensions.Throws<CryptographicException> (() => 
196                                 MachineKey.Unprotect (encryptedBytes, invalidUsages), 
197                                 "Purposes not encrypting properly");
198
199                         AssertExtensions.Throws<CryptographicException> (() => 
200                                 MachineKey.Unprotect (encryptedBytes, oneUsage), 
201                                 "Single purpose working when multiple supplied");
202                 }
203 #endif
204         }
205 }