/mcs/class/System.Web/Test/System.Web/HttpServerUtilityCas.cs

https://github.com/t-ashula/mono · C# · 279 lines · 202 code · 33 blank · 44 comment · 0 complexity · 81b54f7ea53f33c5f45cea83cfc1b815 MD5 · raw file

  1. //
  2. // HttpServerUtilityCas.cs - CAS unit tests for System.Web.HttpServerUtility
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.Web;
  35. namespace MonoCasTests.System.Web {
  36. [TestFixture]
  37. [Category ("CAS")]
  38. public class HttpServerUtilityCas : AspNetHostingMinimal {
  39. private const string url = "http://www.mono-project.com/";
  40. private StringWriter sw;
  41. private HttpContext context;
  42. private HttpServerUtility hsu;
  43. [TestFixtureSetUp]
  44. public void FixtureSetUp ()
  45. {
  46. sw = new StringWriter ();
  47. context = new HttpContext (null);
  48. hsu = context.Server;
  49. }
  50. [Test]
  51. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  52. public void Properties_Deny_Unrestricted ()
  53. {
  54. try {
  55. Assert.IsTrue (hsu.ScriptTimeout > 0, "ScriptTimeout");
  56. }
  57. catch (NullReferenceException) {
  58. // ms 1.x, mono
  59. }
  60. }
  61. [Test]
  62. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
  63. [ExpectedException (typeof (SecurityException))]
  64. public void ScriptTimeout_Deny_Unrestricted ()
  65. {
  66. hsu.ScriptTimeout = 1;
  67. }
  68. [Test]
  69. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
  70. public void ScriptTimeout_PermitOnly_Unrestricted ()
  71. {
  72. hsu.ScriptTimeout = 1;
  73. }
  74. [Test]
  75. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  76. public void Methods_Deny_Unrestricted ()
  77. {
  78. hsu.ClearError ();
  79. Assert.IsNull (hsu.GetLastError (), "GetLastError");
  80. Assert.IsNotNull (hsu.HtmlDecode (String.Empty), "HtmlDecode(string)");
  81. hsu.HtmlDecode (String.Empty, sw);
  82. Assert.IsNotNull (hsu.HtmlEncode (String.Empty), "HtmlEncode(string)");
  83. hsu.HtmlEncode (String.Empty, sw);
  84. try {
  85. Assert.IsNull (hsu.MapPath (String.Empty), "MapPath(string)");
  86. }
  87. catch (NullReferenceException) {
  88. // ms 1.x
  89. }
  90. try {
  91. hsu.Transfer ("/");
  92. }
  93. catch (NullReferenceException) {
  94. // ms
  95. }
  96. try {
  97. hsu.Transfer ("/", true);
  98. }
  99. catch (NullReferenceException) {
  100. // ms
  101. }
  102. try {
  103. hsu.Transfer ("/", false);
  104. }
  105. catch (NullReferenceException) {
  106. // ms
  107. }
  108. try {
  109. hsu.Transfer ((IHttpHandler)null, true);
  110. }
  111. catch (NullReferenceException) {
  112. // ms
  113. }
  114. try {
  115. hsu.Transfer ((IHttpHandler)null, false);
  116. }
  117. catch (NullReferenceException) {
  118. // ms
  119. }
  120. try {
  121. Assert.IsNotNull (hsu.UrlDecode (url), "UrlDecode(string)");
  122. }
  123. catch (NullReferenceException) {
  124. // ms
  125. }
  126. try {
  127. hsu.UrlDecode ("http://www.mono-project.com/", sw);
  128. }
  129. catch (NullReferenceException) {
  130. // ms
  131. }
  132. Assert.IsNotNull (hsu.UrlEncode (String.Empty), "UrlEncode(string)");
  133. hsu.UrlEncode (String.Empty, sw);
  134. Assert.IsNotNull (hsu.UrlPathEncode (String.Empty), "UrlPathEncode(string)");
  135. }
  136. [Test]
  137. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  138. [ExpectedException (typeof (SecurityException))]
  139. public void CreateObject_String_Deny_UnmanagedCode ()
  140. {
  141. hsu.CreateObject (String.Empty);
  142. }
  143. [Test]
  144. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  145. [ExpectedException (typeof (HttpException))] // String.Empty isn't valid
  146. public void CreateObject_String_PermitOnly_UnmanagedCode ()
  147. {
  148. hsu.CreateObject (String.Empty);
  149. }
  150. [Test]
  151. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  152. [ExpectedException (typeof (SecurityException))]
  153. public void CreateObject_Type_Deny_UnmanagedCode ()
  154. {
  155. hsu.CreateObject (String.Empty);
  156. }
  157. [Test]
  158. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  159. public void CreateObject_Type_PermitOnly_UnmanagedCode ()
  160. {
  161. try {
  162. hsu.CreateObject (typeof (string));
  163. }
  164. catch (MissingMethodException) {
  165. // ms
  166. }
  167. catch (HttpException) {
  168. // mono
  169. }
  170. }
  171. [Test]
  172. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  173. [ExpectedException (typeof (SecurityException))]
  174. public void CreateObjectFromClsid_String_Deny_UnmanagedCode ()
  175. {
  176. hsu.CreateObjectFromClsid (String.Empty);
  177. }
  178. [Test]
  179. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  180. public void CreateObjectFromClsid_PermitOnly_UnmanagedCode ()
  181. {
  182. try {
  183. hsu.CreateObjectFromClsid (String.Empty);
  184. }
  185. catch (FormatException) {
  186. // ms (not a valid guid)
  187. }
  188. catch (HttpException) {
  189. // mono
  190. }
  191. }
  192. [Test]
  193. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  194. [ExpectedException (typeof (NullReferenceException))]
  195. public void Execute_String_Deny_Unrestricted ()
  196. {
  197. hsu.Execute (String.Empty);
  198. }
  199. [Test]
  200. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  201. [ExpectedException (typeof (NullReferenceException))]
  202. public void Execute_StringTextWriter_Deny_Unrestricted ()
  203. {
  204. hsu.Execute (String.Empty, sw);
  205. }
  206. [Test]
  207. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  208. [ExpectedException (typeof (NullReferenceException))]
  209. public void Execute_StringTextWriterTrue_Deny_Unrestricted ()
  210. {
  211. hsu.Execute (String.Empty, sw, true);
  212. }
  213. [Test]
  214. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  215. [ExpectedException (typeof (NullReferenceException))]
  216. public void Execute_StringTextWriterFalse_Deny_Unrestricted ()
  217. {
  218. hsu.Execute (String.Empty, sw, false);
  219. }
  220. [Test]
  221. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
  222. [ExpectedException (typeof (SecurityException))]
  223. public void MachineName_Deny_Medium ()
  224. {
  225. Assert.IsNotNull (hsu.MachineName, "MachineName");
  226. }
  227. [Test]
  228. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
  229. public void MachineName_PermitOnly_Medium ()
  230. {
  231. Assert.IsNotNull (hsu.MachineName, "MachineName");
  232. }
  233. // LinkDemand
  234. public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
  235. {
  236. // there are no public ctor so we're taking a method that we know isn't protected
  237. // (by a Demand) and call it thru reflection so any linkdemand (on the class) will
  238. // be promoted to a Demand
  239. MethodInfo mi = this.Type.GetMethod ("HtmlDecode", new Type[1] { typeof (string) } );
  240. return mi.Invoke (hsu, new object[1] { String.Empty });
  241. }
  242. public override Type Type {
  243. get { return typeof (HttpServerUtility); }
  244. }
  245. }
  246. }