2005-12-02 Chris Toshok <toshok@ximian.com>
[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 #if NET_2_0
31
32 using NUnit.Framework;
33
34 using System;
35 using System.Configuration;
36 using System.Web.Configuration;
37 using System.Web;
38 using System.Web.Security;
39 using System.IO;
40 using System.Xml;
41 using System.Reflection;
42
43 namespace MonoTests.System.Web.Configuration {
44
45         [TestFixture]
46         public class AuthorizationRuleTest  {
47
48                 [Test]
49                 public void Defaults()
50                 {
51                         AuthorizationRule a = new AuthorizationRule(AuthorizationRuleAction.Deny);
52
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");
57                 }
58
59                 [Test]
60                 public void Test_EqualsAndHashCode ()
61                 {
62                         AuthorizationRule a = new AuthorizationRule (AuthorizationRuleAction.Deny);
63                         AuthorizationRule b = new AuthorizationRule (AuthorizationRuleAction.Deny);
64
65                         a.Users.Add ("toshok");
66                         a.Roles.Add ("Admin");
67                         a.Verbs.Add ("reboot");
68
69                         b.Users.Add ("toshok");
70                         b.Roles.Add ("Admin");
71                         b.Verbs.Add ("reboot");
72
73                         Assert.AreEqual (a, b, "A1");
74                         Assert.AreEqual (a.GetHashCode (), b.GetHashCode (), "A2");
75                 }
76
77                 [Test]
78                 public void SerializeElement ()
79                 {
80                         StringWriter sw;
81                         XmlWriter writer;
82                         AuthorizationRule rule;
83                         MethodInfo mi = typeof (AuthorizationRule).GetMethod ("SerializeElement", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
84                         object[] parms = new object[2];
85                         bool failed;
86
87                         /* 1 */
88                         failed = true;
89                         try {
90                                 sw = new StringWriter ();
91                                 writer = new XmlTextWriter (sw);
92
93                                 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
94                                 parms[0] = writer;
95                                 parms[1] = false;
96                                 mi.Invoke (rule, parms);
97                         }
98                         catch (TargetInvocationException e) {
99                                 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
100                                 failed = false;
101                         }
102                         Assert.IsFalse (failed, "A1");
103
104                         /* 2 */
105                         sw = new StringWriter ();
106                         writer = new XmlTextWriter (sw);
107                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
108                         rule.Users.Add ("toshok");
109                         parms[0] = writer;
110                         parms[1] = false;
111                         mi.Invoke (rule, parms);
112
113                         Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
114
115                         /* 2 */
116                         sw = new StringWriter ();
117                         writer = new XmlTextWriter (sw);
118                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
119                         rule.Users.Add ("toshok");
120                         parms[0] = writer;
121                         parms[1] = true;
122                         mi.Invoke (rule, parms);
123
124                         Assert.AreEqual ("<allow users=\"toshok\" />", sw.ToString(), "A2");
125
126                         /* 3-4 */
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");
136                         parms[0] = writer;
137                         parms[1] = true;
138                         bool b = (bool)mi.Invoke (rule, parms);
139
140                         Assert.AreEqual ("<deny roles=\"admin,wheel\" users=\"toshok,chris\" verbs=\"GET,PUT\" />", sw.ToString(), "A3");
141                         Assert.IsTrue (b, "A4");
142                 }
143
144                 [Test]
145                 public void PostDeserialize ()
146                 {
147                         AuthorizationRule rule;
148                         MethodInfo mi = typeof (AuthorizationRule).GetMethod ("PostDeserialize", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
149                         object[] parms = new object[0];
150                         bool failed;
151
152                         /* 1 */
153                         failed = true;
154                         try {
155                                 rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
156                                 mi.Invoke (rule, parms);
157                         }
158                         catch (TargetInvocationException e) {
159                                 Assert.AreEqual (typeof (ConfigurationErrorsException), e.InnerException.GetType (), "A1");
160                                 failed = false;
161                         }
162                         Assert.IsFalse (failed, "A1");
163
164                         /* 2 */
165                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
166                         rule.Users.Add ("toshok");
167                         mi.Invoke (rule, parms);
168
169                         /* 2 */
170                         rule = new AuthorizationRule (AuthorizationRuleAction.Allow);
171                         rule.Users.Add ("toshok");
172                         mi.Invoke (rule, parms);
173
174                         /* 3-4 */
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);
183
184                         Assert.IsTrue (b, "A4");
185                 }
186
187         }
188
189 }
190
191 #endif