AspNetHostingPermissionAttributeTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // AspNetHostingPermissionAttributeTest.cs -
  3. // NUnit Test Cases for AspNetHostingPermissionAttribute
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004 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.Security;
  32. using System.Security.Permissions;
  33. using System.Web;
  34. namespace MonoTests.System.Web {
  35. [TestFixture]
  36. public class AspNetHostingPermissionAttributeTest {
  37. [Test]
  38. public void Default ()
  39. {
  40. AspNetHostingPermissionAttribute a = new AspNetHostingPermissionAttribute (SecurityAction.Assert);
  41. Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
  42. Assert.IsFalse (a.Unrestricted, "Unrestricted");
  43. Assert.AreEqual (AspNetHostingPermissionLevel.None, a.Level, "AspNetHostingPermissionLevel");
  44. AspNetHostingPermission anhp = (AspNetHostingPermission)a.CreatePermission ();
  45. Assert.IsFalse (anhp.IsUnrestricted (), "IsUnrestricted");
  46. }
  47. [Test]
  48. public void Action ()
  49. {
  50. AspNetHostingPermissionAttribute a = new AspNetHostingPermissionAttribute (SecurityAction.Assert);
  51. Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
  52. a.Action = SecurityAction.Demand;
  53. Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
  54. a.Action = SecurityAction.Deny;
  55. Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
  56. a.Action = SecurityAction.InheritanceDemand;
  57. Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
  58. a.Action = SecurityAction.LinkDemand;
  59. Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
  60. a.Action = SecurityAction.PermitOnly;
  61. Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
  62. a.Action = SecurityAction.RequestMinimum;
  63. Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
  64. a.Action = SecurityAction.RequestOptional;
  65. Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
  66. a.Action = SecurityAction.RequestRefuse;
  67. Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
  68. #if NET_2_0
  69. a.Action = SecurityAction.DemandChoice;
  70. Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
  71. a.Action = SecurityAction.InheritanceDemandChoice;
  72. Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
  73. a.Action = SecurityAction.LinkDemandChoice;
  74. Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
  75. #endif
  76. }
  77. [Test]
  78. public void Action_Invalid ()
  79. {
  80. AspNetHostingPermissionAttribute a = new AspNetHostingPermissionAttribute ((SecurityAction)Int32.MinValue);
  81. // no validation in attribute
  82. }
  83. [Test]
  84. public void Unrestricted ()
  85. {
  86. AspNetHostingPermissionAttribute a = new AspNetHostingPermissionAttribute (SecurityAction.Assert);
  87. a.Unrestricted = true;
  88. AspNetHostingPermission anhp = (AspNetHostingPermission)a.CreatePermission ();
  89. Assert.IsTrue (anhp.IsUnrestricted (), "IsUnrestricted");
  90. Assert.AreEqual (AspNetHostingPermissionLevel.None, a.Level, "None");
  91. a.Unrestricted = false;
  92. anhp = (AspNetHostingPermission)a.CreatePermission ();
  93. Assert.IsFalse (anhp.IsUnrestricted (), "!IsUnrestricted");
  94. }
  95. [Test]
  96. public void Level ()
  97. {
  98. AspNetHostingPermissionAttribute a = new AspNetHostingPermissionAttribute (SecurityAction.Assert);
  99. a.Level = AspNetHostingPermissionLevel.None;
  100. Assert.AreEqual (AspNetHostingPermissionLevel.None, a.Level, "None");
  101. a.Level = AspNetHostingPermissionLevel.Minimal;
  102. Assert.AreEqual (AspNetHostingPermissionLevel.Minimal, a.Level, "Minimal");
  103. a.Level = AspNetHostingPermissionLevel.Low;
  104. Assert.AreEqual (AspNetHostingPermissionLevel.Low, a.Level, "Low");
  105. a.Level = AspNetHostingPermissionLevel.Medium;
  106. Assert.AreEqual (AspNetHostingPermissionLevel.Medium, a.Level, "Medium");
  107. a.Level = AspNetHostingPermissionLevel.High;
  108. Assert.AreEqual (AspNetHostingPermissionLevel.High, a.Level, "High");
  109. a.Level = AspNetHostingPermissionLevel.Unrestricted;
  110. Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, a.Level, "Unrestricted");
  111. }
  112. [Test]
  113. [ExpectedException (typeof (ArgumentException))]
  114. public void Level_Invalid ()
  115. {
  116. AspNetHostingPermissionAttribute a = new AspNetHostingPermissionAttribute (SecurityAction.Assert);
  117. a.Level = (AspNetHostingPermissionLevel)Int32.MinValue;
  118. }
  119. [Test]
  120. public void Attributes ()
  121. {
  122. Type t = typeof (AspNetHostingPermissionAttribute);
  123. Assert.IsTrue (t.IsSerializable, "IsSerializable");
  124. object[] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
  125. Assert.AreEqual (1, attrs.Length, "AttributeUsage");
  126. AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
  127. Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
  128. Assert.IsFalse (aua.Inherited, "Inherited");
  129. AttributeTargets at = AttributeTargets.All;
  130. Assert.AreEqual (at, aua.ValidOn, "ValidOn");
  131. }
  132. }
  133. }