AuthorizationRuleTest.cs 5.4 KB

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