Tests
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / RoleGroupCollectionTest.cs
1 //
2 // RoleGroupCollectionTest.cs 
3 //      - Unit tests for System.Web.UI.WebControls.RoleGroupCollection
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@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 System;
33 using System.IO;
34 using System.Security.Principal;
35 using System.Web;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38
39 using NUnit.Framework;
40
41 namespace MonoTests.System.Web.UI.WebControls {
42
43         [TestFixture]
44         public class RoleGroupCollectionTest {
45
46                 private IPrincipal GetPrincipal (string name)
47                 {
48                         return new GenericPrincipal (new GenericIdentity (name), null);
49                 }
50
51                 private IPrincipal GetPrincipal (string name, string role)
52                 {
53                         return new GenericPrincipal (new GenericIdentity (name), new string[1] { role });
54                 }
55
56                 private IPrincipal GetUnauthenticatedPrincipal (string name, string role)
57                 {
58                         return new GenericPrincipal (new UnauthenticatedIdentity (name), new string[1] { role });
59                 }
60
61                 [Test]
62                 [ExpectedException (typeof (ArgumentNullException))]
63                 public void Add_Null ()
64                 {
65                         RoleGroupCollection rgc = new RoleGroupCollection ();
66                         rgc.Add (null);
67                 }
68
69                 [Test]
70                 public void Add ()
71                 {
72                         RoleGroupCollection rgc = new RoleGroupCollection ();
73                         Assert.AreEqual (0, rgc.Count, "0");
74                         RoleGroup rg1 = new RoleGroup ();
75                         rgc.Add (rg1);
76                         Assert.AreEqual (1, rgc.Count, "1");
77                         rgc.Add (rg1);
78                         Assert.AreEqual (2, rgc.Count, "2");
79                 }
80
81                 [Test]
82                 public void Contains ()
83                 {
84                         RoleGroupCollection rgc = new RoleGroupCollection ();
85                         Assert.IsFalse (rgc.Contains (null), "null");
86
87                         RoleGroup rg1 = new RoleGroup ();
88                         rgc.Add (rg1);
89                         Assert.IsTrue (rgc.Contains (rg1), "1a");
90
91                         RoleGroup rg2 = new RoleGroup ();
92                         Assert.IsFalse (rgc.Contains (rg2), "2a");
93                         rgc.Add (rg2);
94                         Assert.IsTrue (rgc.Contains (rg2), "2b");
95
96                         rgc.Remove (rg1);
97                         Assert.IsFalse (rgc.Contains (rg1), "1b");
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentNullException))]
102                 public void GetMatchingRoleGroup_Null ()
103                 {
104                         RoleGroupCollection rgc = new RoleGroupCollection ();
105                         rgc.GetMatchingRoleGroup (null);
106                 }
107
108                 [Test]
109                 public void GetMatchingRoleGroup_NoRoles ()
110                 {
111                         RoleGroup rg = new RoleGroup ();
112                         
113                         RoleGroupCollection rgc = new RoleGroupCollection ();
114                         rgc.Add (rg);
115                         Assert.AreEqual (1, rgc.Count, "Count");
116
117                         RoleGroup result = rgc.GetMatchingRoleGroup (GetPrincipal ("me"));
118                         Assert.IsNull (result, "me");
119
120                         result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "mono"));
121                         Assert.IsNull (result, "me+mono");
122                 }
123
124                 [Test]
125                 public void GetMatchingRoleGroup_In ()
126                 {
127                         RoleGroup rg = new RoleGroup ();
128                         rg.Roles = new string[2] { "mono", "hackers" };
129
130                         RoleGroupCollection rgc = new RoleGroupCollection ();
131                         rgc.Add (rg);
132                         Assert.AreEqual (1, rgc.Count, "Count");
133
134                         RoleGroup result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "mono"));
135                         Assert.IsNotNull (result, "me+mono");
136                         Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref1");
137
138                         result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "hackers"));
139                         Assert.IsNotNull (result, "me+hackers");
140                         Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref2");
141
142                         // works for unauthenticated principals too
143                         result = rgc.GetMatchingRoleGroup (GetUnauthenticatedPrincipal ("me", "mono"));
144                         Assert.IsNotNull (result, "unauthenticated+me+mono");
145                         Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref3");
146
147                         result = rgc.GetMatchingRoleGroup (GetUnauthenticatedPrincipal ("me", "hackers"));
148                         Assert.IsNotNull (result, "unauthenticated+me+hackers");
149                         Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref4");
150
151                         // case insensitive
152                         result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "MoNo"));
153                         Assert.IsNotNull (result, "unauthenticated+me+MoNo");
154                         Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref5");
155
156                         result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "hAcKeRs"));
157                         Assert.IsNotNull (result, "unauthenticated+me+hAcKeRs");
158                         Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref6");
159                 }
160
161                 [Test]
162                 public void ContainsUser_Out ()
163                 {
164                         RoleGroup rg = new RoleGroup ();
165                         rg.Roles = new string[2] { "mono", "hackers" };
166
167                         RoleGroupCollection rgc = new RoleGroupCollection ();
168                         rgc.Add (rg);
169                         Assert.AreEqual (1, rgc.Count, "Count");
170
171                         RoleGroup result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "m0n0"));
172                         Assert.IsNull (result, "me+MoNo");
173
174                         result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "h4ck"));
175                         Assert.IsNull (result, "me+h4ck");
176                 }
177
178                 [Test]
179                 public void IndexOf ()
180                 {
181                         RoleGroupCollection rgc = new RoleGroupCollection ();
182                         Assert.AreEqual (-1, rgc.IndexOf (null), "null");
183
184                         RoleGroup rg1 = new RoleGroup ();
185                         rgc.Add (rg1);
186                         Assert.AreEqual (0, rgc.IndexOf (rg1), "0");
187                         rgc.Add (rg1);
188                         Assert.AreEqual (0, rgc.IndexOf (rg1), "1");
189                 }
190
191                 [Test]
192                 public void Remove ()
193                 {
194                         RoleGroupCollection rgc = new RoleGroupCollection ();
195                         rgc.Remove (null);
196
197                         RoleGroup rg1 = new RoleGroup ();
198                         rgc.Remove (rg1);
199                         rgc.Add (rg1);
200                         rgc.Add (rg1);
201                         Assert.AreEqual (2, rgc.Count, "Count");
202                         rgc.Remove (rg1);
203                         Assert.IsTrue (rgc.Contains (rg1), "rg1-bis");
204
205                         RoleGroup rg2 = new RoleGroup ();
206                         rgc.Add (rg2);
207                         rgc.Remove (rg2);
208                         rgc.Remove (rg2);
209                 }
210         }
211 }
212
213 #endif