Merge pull request #1459 from akoeplinger/fix-website-links
[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 #if NET_4_0
29 using System;
30 using System.Text;
31 using System.Web.Security;
32
33 using MonoTests.Common;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Web.Security
37 {
38         [TestFixture]
39         public class MachineKeyTest
40         {
41                 [Test]
42                 [MonoTODO ("Find out why the difference in result sizes exists between .NET and Mono")]
43                 public void Encode ()
44                 {
45 #if DOT_NET
46                         const int ALL_EXPECTED_SIZE = 192;
47                         const int ENCRYPTION_EXPECTED_SIZE = 128;
48 #else
49                         const int ALL_EXPECTED_SIZE = 128;
50                         const int ENCRYPTION_EXPECTED_SIZE = 64;
51 #endif
52                         const int VALIDATION_EXPECTED_SIZE = 64;
53                         
54                         AssertExtensions.Throws<ArgumentNullException> (() => {
55                                 MachineKey.Encode (null, MachineKeyProtection.All);
56                         }, "#A1-1");
57
58                         string result = MachineKey.Encode (new byte[] {}, (MachineKeyProtection)12345);
59                         Assert.IsNotNull (result, "#A1-1");
60                         Assert.AreEqual (0, result.Length, "#A1-2");
61
62                         result = MachineKey.Encode (new byte[] {}, MachineKeyProtection.All);
63                         Assert.IsNotNull (result, "#B1-1");
64                         Assert.AreEqual (ALL_EXPECTED_SIZE, result.Length, "#B1-2");
65
66                         result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Encryption);
67                         Assert.IsNotNull (result, "#C1-1");
68                         Assert.AreEqual (ENCRYPTION_EXPECTED_SIZE, result.Length, "#C1-2");
69
70                         result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Validation);
71                         Assert.IsNotNull (result, "#D1-1");
72                         Assert.AreEqual (VALIDATION_EXPECTED_SIZE, result.Length, "#D1-2");
73                 }
74
75                 [Test]
76                 public void Decode ()
77                 {
78                         byte[] decoded;
79
80                         AssertExtensions.Throws<ArgumentNullException> (() => {
81                                 MachineKey.Decode (null, MachineKeyProtection.All);
82                         }, "#A1-1");
83
84                         AssertExtensions.Throws<ArgumentException> (() => {
85                                 decoded = MachineKey.Decode (String.Empty, MachineKeyProtection.All);
86                         }, "#A1-2");
87
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");
92
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");
97
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");
102
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");
107
108                         AssertExtensions.Throws<ArgumentException> (() => {
109                                 decoded = MachineKey.Decode ("test", MachineKeyProtection.All);
110                         }, "#B1-1");
111
112                         AssertExtensions.Throws<ArgumentException> (() => {
113                                 decoded = MachineKey.Decode ("test", MachineKeyProtection.Encryption);
114                         }, "#B1-2");
115
116                         AssertExtensions.Throws<ArgumentException> (() => {
117                                 decoded = MachineKey.Decode ("test", MachineKeyProtection.Validation);
118                         }, "#B1-3");
119
120                         sb = new StringBuilder ().Append ('0', 1);
121                         try {
122                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
123                                 Assert.Fail ("#C1-2 [no exception]");
124                         } catch (ArgumentException) {
125                                 // success
126                         } catch {
127                                 Assert.Fail ("#C1-2 [invalid exception]");
128                         }
129
130                         sb = new StringBuilder ().Append ('0', 2);
131                         try {
132                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
133                         } catch (ArgumentException ex) {
134                                 Console.WriteLine (ex);
135                                 Assert.Fail ("#C1-3");
136                         } catch {
137                                 // success
138                         }
139
140                         sb = new StringBuilder ().Append ('0', 193);
141                         try {
142                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
143                                 Assert.Fail ("#C2-1 [no exception]");
144                         } catch (ArgumentException) {
145                                 // success
146                         } catch {
147                                 Assert.Fail ("#C2-1 [invalid exception]");
148                         }
149
150                         sb = new StringBuilder ().Append ('0', 129);
151                         try {
152                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
153                                 Assert.Fail ("#C3-1 [no exception]");
154                         } catch (ArgumentException) {
155                                 // success
156                         } catch {
157                                 Assert.Fail ("#C3-2 [invalid exception]");
158                         }
159
160                         sb = new StringBuilder ().Append ('0', 64);
161                         try {
162                                 decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
163                         } catch (ArgumentException) {
164                                 Assert.Fail ("#C4-1");
165                         } catch {
166                                 // Success
167                         }
168                 }
169         }
170 }
171 #endif