StateRuntimeCas.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // StateRuntimeCas.cs
  3. // - CAS unit tests for System.Web.SessionState.StateRuntime
  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. using NUnit.Framework;
  30. using System;
  31. using System.Reflection;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.Web;
  35. using System.Web.SessionState;
  36. namespace MonoCasTests.System.Web.SessionState {
  37. // note: the execution order (not user controlable) is very important
  38. // for the tests to execute properly. However the tests are protected
  39. // not to report errors if the order isn't repected.
  40. [TestFixture]
  41. [Category ("CAS")]
  42. public class StateRuntimeCas : AspNetHostingMinimal {
  43. private StateRuntime runtime;
  44. [TestFixtureSetUp]
  45. public void FixtureSetUp ()
  46. {
  47. try {
  48. // ensure that the static ctor has been called
  49. runtime = new StateRuntime ();
  50. }
  51. catch {
  52. }
  53. }
  54. [Test]
  55. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  56. [ExpectedException (typeof (SecurityException))]
  57. public void Constructor_Deny_UnmanagedCode ()
  58. {
  59. try {
  60. new StateRuntime ();
  61. }
  62. catch (TypeInitializationException) {
  63. Assert.Ignore ("fails on MS");
  64. }
  65. }
  66. [Test]
  67. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Minimal)]
  68. #if NET_2_0
  69. [ExpectedException (typeof (SecurityException))]
  70. #endif
  71. public void Constructor_Deny_Minimal ()
  72. {
  73. try {
  74. new StateRuntime ();
  75. }
  76. catch (TypeInitializationException) {
  77. Assert.Ignore ("fails on MS");
  78. }
  79. }
  80. [Test]
  81. #if NET_2_0
  82. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Minimal)]
  83. #endif
  84. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  85. public void Constructor_PermitOnly_UnmanagedCodeMinimal ()
  86. {
  87. try {
  88. new StateRuntime ();
  89. }
  90. catch (TypeInitializationException) {
  91. Assert.Ignore ("fails on MS");
  92. }
  93. }
  94. [Test]
  95. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
  96. public void ProcessRequest9_PermitOnly_Medium ()
  97. {
  98. if (runtime == null)
  99. Assert.Ignore ("impossible to instantiate under MS");
  100. try {
  101. runtime.ProcessRequest (IntPtr.Zero, 0, null, 0, 0, 0, 0, 0, IntPtr.Zero);
  102. }
  103. catch (NullReferenceException) {
  104. // ms 1.x
  105. }
  106. catch (DllNotFoundException) {
  107. // ms 2.0
  108. }
  109. catch (NotImplementedException) {
  110. // mono
  111. }
  112. }
  113. [Test]
  114. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
  115. [ExpectedException (typeof (SecurityException))]
  116. public void ProcessRequest9_Deny_Medium ()
  117. {
  118. if (runtime == null)
  119. Assert.Ignore ("impossible to instantiate under MS");
  120. runtime.ProcessRequest (IntPtr.Zero, 0, null, 0, 0, 0, 0, 0, IntPtr.Zero);
  121. }
  122. #if NET_2_0
  123. [Test]
  124. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
  125. public void ProcessRequest10_PermitOnly_Medium ()
  126. {
  127. if (runtime == null)
  128. Assert.Ignore ("impossible to instantiate under MS");
  129. try {
  130. runtime.ProcessRequest (IntPtr.Zero, 0, null, 0, 0, 0, 0, 0, 0, IntPtr.Zero);
  131. }
  132. catch (DllNotFoundException) {
  133. // ms
  134. }
  135. catch (NotImplementedException) {
  136. // mono
  137. }
  138. }
  139. [Test]
  140. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
  141. [ExpectedException (typeof (SecurityException))]
  142. public void ProcessRequest10_Deny_Medium ()
  143. {
  144. if (runtime == null)
  145. Assert.Ignore ("impossible to instantiate under MS");
  146. runtime.ProcessRequest (IntPtr.Zero, 0, null, 0, 0, 0, 0, 0, 0, IntPtr.Zero);
  147. }
  148. #endif
  149. [Test]
  150. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  151. [Category ("NotDotNet")] // freeze
  152. public void StopProcessing_PermitOnly_UnmanagedCode ()
  153. {
  154. if (runtime == null)
  155. Assert.Ignore ("impossible to instantiate under MS");
  156. try {
  157. runtime.StopProcessing ();
  158. }
  159. catch (SecurityException se) {
  160. Console.WriteLine (se);
  161. }
  162. catch (NotImplementedException) {
  163. // mono
  164. }
  165. }
  166. [Test]
  167. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  168. [ExpectedException (typeof (SecurityException))]
  169. public void StopProcessing_Deny_UnmanagedCode ()
  170. {
  171. if (runtime == null)
  172. Assert.Ignore ("may not be possible to instantiate under MS (run-order)");
  173. runtime.StopProcessing ();
  174. }
  175. // LinkDemand
  176. [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
  177. public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
  178. {
  179. try {
  180. return base.CreateControl (action, level);
  181. }
  182. catch (TypeInitializationException) {
  183. // ctor can't be called more than once (else it throws TIE)
  184. return String.Empty;
  185. }
  186. }
  187. public override Type Type {
  188. get { return typeof (StateRuntime); }
  189. }
  190. }
  191. }