AuthorizationRuleTest.cs 5.4 KB

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