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.
31 using NUnit.Framework;
34 using System.Configuration;
35 using System.Web.Configuration;
37 using System.Web.Security;
40 using System.Reflection;
42 namespace MonoTests.System.Web.Configuration {
45 public class AuthorizationRuleTest {
48 public void Defaults()
50 AuthorizationRule a = new AuthorizationRule(AuthorizationRuleAction.Deny);
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");
59 public void Test_EqualsAndHashCode ()
61 AuthorizationRule a = new AuthorizationRule (AuthorizationRuleAction.Deny);
62 AuthorizationRule b = new AuthorizationRule (AuthorizationRuleAction.Deny);
64 a.Users.Add ("toshok");
65 a.Roles.Add ("Admin");
66 a.Verbs.Add ("reboot");
68 b.Users.Add ("toshok");
69 b.Roles.Add ("Admin");
70 b.Verbs.Add ("reboot");
72 Assert.AreEqual (a, b, "A1");
73 Assert.AreEqual (a.GetHashCode (), b.GetHashCode (), "A2");
77 public void SerializeElement ()
81 AuthorizationRule rule;
82 MethodInfo mi = typeof (AuthorizationRule).GetMethod ("SerializeElement", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
83 object[] parms = new object[2];
89 sw = new StringWriter ();
90 writer = new XmlTextWriter (sw);
92 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
95 mi.Invoke (rule, parms);
97 catch (TargetInvocationException e) {
98 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
101 Assert.IsFalse (failed, "A1");
104 sw = new StringWriter ();
105 writer = new XmlTextWriter (sw);
106 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
107 rule.Users.Add ("toshok");
110 mi.Invoke (rule, parms);
112 Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
115 sw = new StringWriter ();
116 writer = new XmlTextWriter (sw);
117 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
118 rule.Users.Add ("toshok");
121 mi.Invoke (rule, parms);
123 Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
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");
137 bool b = (bool)mi.Invoke (rule, parms);
139 Assert.AreEqual ("<deny roles=\"admin,wheel\" users=\"toshok,chris\" verbs=\"GET,PUT\" />", sw.ToString(), "A3");
140 Assert.IsTrue (b, "A4");
144 public void PostDeserialize ()
146 AuthorizationRule rule;
147 MethodInfo mi = typeof (AuthorizationRule).GetMethod ("PostDeserialize", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
148 object[] parms = new object[0];
154 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
155 mi.Invoke (rule, parms);
157 catch (TargetInvocationException e) {
158 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
161 Assert.IsFalse (failed, "A1");
164 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
165 rule.Users.Add ("toshok");
166 mi.Invoke (rule, parms);
169 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
170 rule.Users.Add ("toshok");
171 mi.Invoke (rule, parms);
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");
182 mi.Invoke (rule, parms);