2 // AuthorizationRuleTest.cs
3 // - unit tests for System.Web.Configuration.AuthorizationRule
6 // Chris Toshok <toshok@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using NUnit.Framework;
35 using System.Configuration;
36 using System.Web.Configuration;
38 using System.Web.Security;
41 using System.Reflection;
43 namespace MonoTests.System.Web.Configuration {
46 public class AuthorizationRuleTest {
49 public void Defaults()
51 AuthorizationRule a = new AuthorizationRule(AuthorizationRuleAction.Deny);
53 Assert.AreEqual (AuthorizationRuleAction.Deny, a.Action, "A1");
54 Assert.IsNotNull (a.Roles, "A2");
55 Assert.IsNotNull (a.Users, "A3");
56 Assert.IsNotNull (a.Verbs, "A4");
60 public void Test_EqualsAndHashCode ()
62 AuthorizationRule a = new AuthorizationRule (AuthorizationRuleAction.Deny);
63 AuthorizationRule b = new AuthorizationRule (AuthorizationRuleAction.Deny);
65 a.Users.Add ("toshok");
66 a.Roles.Add ("Admin");
67 a.Verbs.Add ("reboot");
69 b.Users.Add ("toshok");
70 b.Roles.Add ("Admin");
71 b.Verbs.Add ("reboot");
73 Assert.AreEqual (a, b, "A1");
74 Assert.AreEqual (a.GetHashCode (), b.GetHashCode (), "A2");
78 public void SerializeElement ()
82 AuthorizationRule rule;
83 MethodInfo mi = typeof (AuthorizationRule).GetMethod ("SerializeElement", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
84 object[] parms = new object[2];
90 sw = new StringWriter ();
91 writer = new XmlTextWriter (sw);
93 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
96 mi.Invoke (rule, parms);
98 catch (TargetInvocationException e) {
99 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
102 Assert.IsFalse (failed, "A1");
105 sw = new StringWriter ();
106 writer = new XmlTextWriter (sw);
107 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
108 rule.Users.Add ("toshok");
111 mi.Invoke (rule, parms);
113 Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
116 sw = new StringWriter ();
117 writer = new XmlTextWriter (sw);
118 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
119 rule.Users.Add ("toshok");
122 mi.Invoke (rule, parms);
124 Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
127 sw = new StringWriter ();
128 writer = new XmlTextWriter (sw);
129 rule = new AuthorizationRule (AuthorizationRuleAction.Deny);
130 rule.Users.Add ("toshok");
131 rule.Users.Add ("chris");
132 rule.Roles.Add ("admin");
133 rule.Roles.Add ("wheel");
134 rule.Verbs.Add ("GET");
135 rule.Verbs.Add ("PUT");
138 bool b = (bool)mi.Invoke (rule, parms);
140 Assert.AreEqual ("<deny roles=\"admin,wheel\" users=\"toshok,chris\" verbs=\"GET,PUT\" />", sw.ToString(), "A3");
141 Assert.IsTrue (b, "A4");
145 public void PostDeserialize ()
147 AuthorizationRule rule;
148 MethodInfo mi = typeof (AuthorizationRule).GetMethod ("PostDeserialize", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
149 object[] parms = new object[0];
155 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
156 mi.Invoke (rule, parms);
158 catch (TargetInvocationException e) {
159 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
162 Assert.IsFalse (failed, "A1");
165 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
166 rule.Users.Add ("toshok");
167 mi.Invoke (rule, parms);
170 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
171 rule.Users.Add ("toshok");
172 mi.Invoke (rule, parms);
175 rule = new AuthorizationRule (AuthorizationRuleAction.Deny);
176 rule.Users.Add ("toshok");
177 rule.Users.Add ("chris");
178 rule.Roles.Add ("admin");
179 rule.Roles.Add ("wheel");
180 rule.Verbs.Add ("GET");
181 rule.Verbs.Add ("PUT");
182 bool b = (bool)mi.Invoke (rule, parms);
184 Assert.IsTrue (b, "A4");