2 // RoleGroupCollectionTest.cs
3 // - Unit tests for System.Web.UI.WebControls.RoleGroupCollection
6 // Sebastien Pouliot <sebastien@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.
34 using System.Security.Principal;
37 using System.Web.UI.WebControls;
39 using NUnit.Framework;
41 namespace MonoTests.System.Web.UI.WebControls {
44 public class RoleGroupCollectionTest {
46 private IPrincipal GetPrincipal (string name)
48 return new GenericPrincipal (new GenericIdentity (name), null);
51 private IPrincipal GetPrincipal (string name, string role)
53 return new GenericPrincipal (new GenericIdentity (name), new string[1] { role });
56 private IPrincipal GetUnauthenticatedPrincipal (string name, string role)
58 return new GenericPrincipal (new UnauthenticatedIdentity (name), new string[1] { role });
62 [ExpectedException (typeof (ArgumentNullException))]
63 public void Add_Null ()
65 RoleGroupCollection rgc = new RoleGroupCollection ();
72 RoleGroupCollection rgc = new RoleGroupCollection ();
73 Assert.AreEqual (0, rgc.Count, "0");
74 RoleGroup rg1 = new RoleGroup ();
76 Assert.AreEqual (1, rgc.Count, "1");
78 Assert.AreEqual (2, rgc.Count, "2");
82 public void Contains ()
84 RoleGroupCollection rgc = new RoleGroupCollection ();
85 Assert.IsFalse (rgc.Contains (null), "null");
87 RoleGroup rg1 = new RoleGroup ();
89 Assert.IsTrue (rgc.Contains (rg1), "1a");
91 RoleGroup rg2 = new RoleGroup ();
92 Assert.IsFalse (rgc.Contains (rg2), "2a");
94 Assert.IsTrue (rgc.Contains (rg2), "2b");
97 Assert.IsFalse (rgc.Contains (rg1), "1b");
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void GetMatchingRoleGroup_Null ()
104 RoleGroupCollection rgc = new RoleGroupCollection ();
105 rgc.GetMatchingRoleGroup (null);
109 public void GetMatchingRoleGroup_NoRoles ()
111 RoleGroup rg = new RoleGroup ();
113 RoleGroupCollection rgc = new RoleGroupCollection ();
115 Assert.AreEqual (1, rgc.Count, "Count");
117 RoleGroup result = rgc.GetMatchingRoleGroup (GetPrincipal ("me"));
118 Assert.IsNull (result, "me");
120 result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "mono"));
121 Assert.IsNull (result, "me+mono");
125 public void GetMatchingRoleGroup_In ()
127 RoleGroup rg = new RoleGroup ();
128 rg.Roles = new string[2] { "mono", "hackers" };
130 RoleGroupCollection rgc = new RoleGroupCollection ();
132 Assert.AreEqual (1, rgc.Count, "Count");
134 RoleGroup result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "mono"));
135 Assert.IsNotNull (result, "me+mono");
136 Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref1");
138 result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "hackers"));
139 Assert.IsNotNull (result, "me+hackers");
140 Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref2");
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");
147 result = rgc.GetMatchingRoleGroup (GetUnauthenticatedPrincipal ("me", "hackers"));
148 Assert.IsNotNull (result, "unauthenticated+me+hackers");
149 Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref4");
152 result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "MoNo"));
153 Assert.IsNotNull (result, "unauthenticated+me+MoNo");
154 Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref5");
156 result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "hAcKeRs"));
157 Assert.IsNotNull (result, "unauthenticated+me+hAcKeRs");
158 Assert.IsTrue (Object.ReferenceEquals (result, rg), "ref6");
162 public void ContainsUser_Out ()
164 RoleGroup rg = new RoleGroup ();
165 rg.Roles = new string[2] { "mono", "hackers" };
167 RoleGroupCollection rgc = new RoleGroupCollection ();
169 Assert.AreEqual (1, rgc.Count, "Count");
171 RoleGroup result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "m0n0"));
172 Assert.IsNull (result, "me+MoNo");
174 result = rgc.GetMatchingRoleGroup (GetPrincipal ("me", "h4ck"));
175 Assert.IsNull (result, "me+h4ck");
179 public void IndexOf ()
181 RoleGroupCollection rgc = new RoleGroupCollection ();
182 Assert.AreEqual (-1, rgc.IndexOf (null), "null");
184 RoleGroup rg1 = new RoleGroup ();
186 Assert.AreEqual (0, rgc.IndexOf (rg1), "0");
188 Assert.AreEqual (0, rgc.IndexOf (rg1), "1");
192 public void Remove ()
194 RoleGroupCollection rgc = new RoleGroupCollection ();
197 RoleGroup rg1 = new RoleGroup ();
201 Assert.AreEqual (2, rgc.Count, "Count");
203 Assert.IsTrue (rgc.Contains (rg1), "rg1-bis");
205 RoleGroup rg2 = new RoleGroup ();