RoleGroupCollectionTest.cs 6.2 KB

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