2 // HttpServerUtilityCas.cs - CAS unit tests for System.Web.HttpServerUtility
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
38 namespace MonoCasTests.System.Web {
42 public class HttpServerUtilityCas : AspNetHostingMinimal {
44 private const string url = "http://www.mono-project.com/";
46 private StringWriter sw;
47 private HttpContext context;
48 private HttpServerUtility hsu;
51 public void FixtureSetUp ()
53 sw = new StringWriter ();
54 context = new HttpContext (null);
59 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
60 public void Properties_Deny_Unrestricted ()
63 Assert.IsTrue (hsu.ScriptTimeout > 0, "ScriptTimeout");
65 catch (NullReferenceException) {
71 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
72 [ExpectedException (typeof (SecurityException))]
73 public void ScriptTimeout_Deny_Unrestricted ()
75 hsu.ScriptTimeout = 1;
79 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
80 public void ScriptTimeout_PermitOnly_Unrestricted ()
82 hsu.ScriptTimeout = 1;
86 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
87 public void Methods_Deny_Unrestricted ()
91 Assert.IsNull (hsu.GetLastError (), "GetLastError");
93 Assert.IsNotNull (hsu.HtmlDecode (String.Empty), "HtmlDecode(string)");
94 hsu.HtmlDecode (String.Empty, sw);
96 Assert.IsNotNull (hsu.HtmlEncode (String.Empty), "HtmlEncode(string)");
97 hsu.HtmlEncode (String.Empty, sw);
100 Assert.IsNull (hsu.MapPath (String.Empty), "MapPath(string)");
102 catch (NullReferenceException) {
109 catch (NullReferenceException) {
113 hsu.Transfer ("/", true);
115 catch (NullReferenceException) {
119 hsu.Transfer ("/", false);
121 catch (NullReferenceException) {
126 hsu.Transfer ((IHttpHandler)null, true);
128 catch (NullReferenceException) {
132 hsu.Transfer ((IHttpHandler)null, false);
134 catch (NullReferenceException) {
139 Assert.IsNotNull (hsu.UrlDecode (url), "UrlDecode(string)");
141 catch (NullReferenceException) {
145 hsu.UrlDecode ("http://www.mono-project.com/", sw);
147 catch (NullReferenceException) {
151 Assert.IsNotNull (hsu.UrlEncode (String.Empty), "UrlEncode(string)");
152 hsu.UrlEncode (String.Empty, sw);
154 Assert.IsNotNull (hsu.UrlPathEncode (String.Empty), "UrlPathEncode(string)");
158 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
159 [ExpectedException (typeof (SecurityException))]
160 public void CreateObject_String_Deny_UnmanagedCode ()
162 hsu.CreateObject (String.Empty);
166 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
167 [ExpectedException (typeof (HttpException))] // String.Empty isn't valid
168 public void CreateObject_String_PermitOnly_UnmanagedCode ()
170 hsu.CreateObject (String.Empty);
174 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
175 [ExpectedException (typeof (SecurityException))]
176 public void CreateObject_Type_Deny_UnmanagedCode ()
178 hsu.CreateObject (String.Empty);
182 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
183 public void CreateObject_Type_PermitOnly_UnmanagedCode ()
186 hsu.CreateObject (typeof (string));
188 catch (MissingMethodException) {
191 catch (HttpException) {
197 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
198 [ExpectedException (typeof (SecurityException))]
199 public void CreateObjectFromClsid_String_Deny_UnmanagedCode ()
201 hsu.CreateObjectFromClsid (String.Empty);
205 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
206 public void CreateObjectFromClsid_PermitOnly_UnmanagedCode ()
209 hsu.CreateObjectFromClsid (String.Empty);
211 catch (FormatException) {
212 // ms (not a valid guid)
214 catch (HttpException) {
220 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
221 [ExpectedException (typeof (NullReferenceException))]
222 public void Execute_String_Deny_Unrestricted ()
224 hsu.Execute (String.Empty);
228 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
229 [ExpectedException (typeof (NullReferenceException))]
230 public void Execute_StringTextWriter_Deny_Unrestricted ()
232 hsu.Execute (String.Empty, sw);
237 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
238 [ExpectedException (typeof (NullReferenceException))]
239 public void Execute_StringTextWriterTrue_Deny_Unrestricted ()
241 hsu.Execute (String.Empty, sw, true);
245 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
246 [ExpectedException (typeof (NullReferenceException))]
247 public void Execute_StringTextWriterFalse_Deny_Unrestricted ()
249 hsu.Execute (String.Empty, sw, false);
254 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
255 [ExpectedException (typeof (SecurityException))]
256 public void MachineName_Deny_Medium ()
258 Assert.IsNotNull (hsu.MachineName, "MachineName");
262 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
263 public void MachineName_PermitOnly_Medium ()
265 Assert.IsNotNull (hsu.MachineName, "MachineName");
270 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
272 // there are no public ctor so we're taking a method that we know isn't protected
273 // (by a Demand) and call it thru reflection so any linkdemand (on the class) will
274 // be promoted to a Demand
275 MethodInfo mi = this.Type.GetMethod ("HtmlDecode", new Type[1] { typeof (string) } );
276 return mi.Invoke (hsu, new object[1] { String.Empty });
279 public override Type Type {
280 get { return typeof (HttpServerUtility); }