[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.Configuration / AuthorizationRuleTest.cs
1 //
2 // AuthorizationRuleTest.cs 
3 //      - unit tests for System.Web.Configuration.AuthorizationRule
4 //
5 // Author:
6 //      Chris Toshok  <toshok@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30
31 using NUnit.Framework;
32
33 using System;
34 using System.Configuration;
35 using System.Web.Configuration;
36 using System.Web;
37 using System.Web.Security;
38 using System.IO;
39 using System.Xml;
40 using System.Reflection;
41
42 namespace MonoTests.System.Web.Configuration {
43
44         [TestFixture]
45         public class AuthorizationRuleTest  {
46
47                 [Test]
48                 public void Defaults()
49                 {
50                         AuthorizationRule a = new AuthorizationRule(AuthorizationRuleAction.Deny);
51
52                         Assert.AreEqual (AuthorizationRuleAction.Deny, a.Action, "A1");
53                         Assert.IsNotNull (a.Roles, "A2");
54                         Assert.IsNotNull (a.Users, "A3");
55                         Assert.IsNotNull (a.Verbs, "A4");
56                 }
57
58                 [Test]
59                 public void Test_EqualsAndHashCode ()
60                 {
61                         AuthorizationRule a = new AuthorizationRule (AuthorizationRuleAction.Deny);
62                         AuthorizationRule b = new AuthorizationRule (AuthorizationRuleAction.Deny);
63
64                         a.Users.Add ("toshok");
65                         a.Roles.Add ("Admin");
66                         a.Verbs.Add ("reboot");
67
68                         b.Users.Add ("toshok");
69                         b.Roles.Add ("Admin");
70                         b.Verbs.Add ("reboot");
71
72                         Assert.AreEqual (a, b, "A1");
73                         Assert.AreEqual (a.GetHashCode (), b.GetHashCode (), "A2");
74                 }
75
76                 [Test]
77                 public void SerializeElement ()
78                 {
79                         StringWriter sw;
80                         XmlWriter writer;
81                         AuthorizationRule rule;
82                         MethodInfo mi = typeof (AuthorizationRule).GetMethod ("SerializeElement", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
83                         object[] parms = new object[2];
84                         bool failed;
85
86                         /* 1 */
87                         failed = true;
88                         try {
89                                 sw = new StringWriter ();
90                                 writer = new XmlTextWriter (sw);
91
92                                 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
93                                 parms[0] = writer;
94                                 parms[1] = false;
95                                 mi.Invoke (rule, parms);
96                         }
97                         catch (TargetInvocationException e) {
98                                 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
99                                 failed = false;
100                         }
101                         Assert.IsFalse (failed, "A1");
102
103                         /* 2 */
104                         sw = new StringWriter ();
105                         writer = new XmlTextWriter (sw);
106                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
107                         rule.Users.Add ("toshok");
108                         parms[0] = writer;
109                         parms[1] = false;
110                         mi.Invoke (rule, parms);
111
112                         Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
113
114                         /* 2 */
115                         sw = new StringWriter ();
116                         writer = new XmlTextWriter (sw);
117                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
118                         rule.Users.Add ("toshok");
119                         parms[0] = writer;
120                         parms[1] = true;
121                         mi.Invoke (rule, parms);
122
123                         Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
124
125                         /* 3-4 */
126                         sw = new StringWriter ();
127                         writer = new XmlTextWriter (sw);
128                         rule = new AuthorizationRule (AuthorizationRuleAction.Deny);
129                         rule.Users.Add ("toshok");
130                         rule.Users.Add ("chris");
131                         rule.Roles.Add ("admin");
132                         rule.Roles.Add ("wheel");
133                         rule.Verbs.Add ("GET");
134                         rule.Verbs.Add ("PUT");
135                         parms[0] = writer;
136                         parms[1] = true;
137                         bool b = (bool)mi.Invoke (rule, parms);
138
139                         Assert.AreEqual ("<deny roles=\"admin,wheel\" users=\"toshok,chris\" verbs=\"GET,PUT\" />", sw.ToString(), "A3");
140                         Assert.IsTrue (b, "A4");
141                 }
142
143                 [Test]
144                 public void PostDeserialize ()
145                 {
146                         AuthorizationRule rule;
147                         MethodInfo mi = typeof (AuthorizationRule).GetMethod ("PostDeserialize", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
148                         object[] parms = new object[0];
149                         bool failed;
150
151                         /* 1 */
152                         failed = true;
153                         try {
154                                 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
155                                 mi.Invoke (rule, parms);
156                         }
157                         catch (TargetInvocationException e) {
158                                 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
159                                 failed = false;
160                         }
161                         Assert.IsFalse (failed, "A1");
162
163                         /* 2 */
164                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
165                         rule.Users.Add ("toshok");
166                         mi.Invoke (rule, parms);
167
168                         /* 2 */
169                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
170                         rule.Users.Add ("toshok");
171                         mi.Invoke (rule, parms);
172
173                         /* 3-4 */
174                         rule = new AuthorizationRule (AuthorizationRuleAction.Deny);
175                         rule.Users.Add ("toshok");
176                         rule.Users.Add ("chris");
177                         rule.Roles.Add ("admin");
178                         rule.Roles.Add ("wheel");
179                         rule.Verbs.Add ("GET");
180                         rule.Verbs.Add ("PUT");
181
182                         mi.Invoke (rule, parms);
183                 }
184
185         }
186
187 }
188