2 // HttpResponseCas.cs - CAS unit tests for System.Web.HttpResponse
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;
32 using System.Collections;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
39 using System.Web.Caching;
41 namespace MonoCasTests.System.Web {
45 public class HttpResponseCas : AspNetHostingMinimal {
47 private StringWriter writer;
49 private FileStream fs;
50 private IntPtr handle;
53 public void FixtureSetUp ()
55 // running at full-trust
56 writer = new StringWriter ();
60 public override void SetUp ()
62 // running at full-trust too
65 fname = Path.GetTempFileName ();
66 fs = new FileStream (fname, FileMode.Open, FileAccess.Read);
71 public void TearDown ()
77 if (File.Exists (fname))
85 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
86 public void Properties_Deny_Unrestricted ()
88 HttpResponse response = new HttpResponse (writer);
90 response.Buffer = false;
91 Assert.IsFalse (response.Buffer, "Buffer");
93 response.BufferOutput = false;
94 Assert.IsFalse (response.BufferOutput, "BufferOutput");
96 Assert.IsNotNull (response.Cache, "Cache");
98 response.CacheControl = "public";
99 Assert.AreEqual ("public", response.CacheControl, "CacheControl");
101 response.ContentEncoding = Encoding.UTF8;
102 Assert.AreEqual (Encoding.UTF8, response.ContentEncoding, "ContentEncoding");
104 response.ContentType = String.Empty;
105 Assert.AreEqual (String.Empty, response.ContentType, "ContentType");
107 response.Charset = Encoding.UTF8.WebName;
108 Assert.AreEqual (Encoding.UTF8.WebName, response.Charset, "Charset");
110 Assert.IsNotNull (response.Cookies, "Cookies");
113 response.Expires = 2;
115 catch (NullReferenceException) {
118 Assert.IsTrue (response.Expires > 0, "Expires");
120 response.ExpiresAbsolute = DateTime.MinValue;
121 Assert.AreEqual (DateTime.MinValue, response.ExpiresAbsolute, "ExpiresAbsolute");
123 Assert.IsTrue (response.IsClientConnected, "IsClientConnected");
124 Assert.IsNotNull (response.Output, "Ouput");
126 response.RedirectLocation = String.Empty;
127 Assert.AreEqual (String.Empty, response.RedirectLocation, "RedirectLocation");
129 response.Status = "501 Not Ok";
130 Assert.AreEqual ("501 Not Ok", response.Status, "Status");
132 response.StatusCode = 501;
133 Assert.AreEqual (501, response.StatusCode, "StatusCode");
135 response.StatusDescription = "Not Ok";
136 Assert.AreEqual ("Not Ok", response.StatusDescription, "StatusDescription");
138 response.SuppressContent = false;
139 Assert.IsFalse (response.SuppressContent, "SuppressContent");
140 response.HeaderEncoding = Encoding.UTF8;
141 Assert.AreEqual (Encoding.UTF8, response.HeaderEncoding, "HeaderEncoding");
143 Assert.IsFalse (response.IsRequestBeingRedirected, "IsRequestBeingRedirected");
147 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
149 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
151 public void Filter_Deny_Unrestricted ()
153 HttpResponse response = new HttpResponse (writer);
155 response.Filter = new MemoryStream ();
157 catch (HttpException) {
161 Assert.IsNull (response.Filter, "Filter");
165 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
167 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
169 public void OutputStream_Deny_Unrestricted ()
171 HttpResponse response = new HttpResponse (writer);
173 Assert.IsNotNull (response.OutputStream, "OutputStream");
175 catch (HttpException) {
180 private string Callback (HttpContext context)
186 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
187 public void Methods_Deny_Unrestricted ()
189 HttpResponse response = new HttpResponse (writer);
190 response.AddCacheItemDependencies (new ArrayList ());
191 response.AddCacheItemDependency (String.Empty);
192 response.AddFileDependencies (new ArrayList ());
193 response.AddFileDependency (fname);
194 response.AddCacheDependency (new CacheDependency[0]);
195 response.AddCacheItemDependencies (new string [0]);
196 response.AddFileDependencies (new string [0]);
199 response.AppendCookie (new HttpCookie ("mono"));
201 catch (NullReferenceException) {
206 Assert.IsNull (response.ApplyAppPathModifier (null), "ApplyAppPathModifier");
208 catch (NullReferenceException) {
215 catch (NullReferenceException) {
220 response.ClearContent ();
222 catch (NullReferenceException) {
227 response.ClearHeaders ();
229 catch (NullReferenceException) {
234 response.Redirect ("http://www.mono-project.com");
236 catch (NullReferenceException) {
240 response.Redirect ("http://www.mono-project.com", false);
242 catch (NullReferenceException) {
247 response.SetCookie (new HttpCookie ("mono"));
249 catch (NullReferenceException) {
253 response.Write (String.Empty);
254 response.Write (Char.MinValue);
255 response.Write (new char[0], 0, 0);
256 response.Write (this);
257 response.WriteSubstitution (new HttpResponseSubstitutionCallback (Callback));
266 catch (NullReferenceException) {
272 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
274 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
276 public void AppendHeader_Deny_Unrestricted ()
278 HttpResponse response = new HttpResponse (writer);
279 response.AppendHeader ("monkey", "mono");
283 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
285 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
287 public void AddHeader_Deny_Unrestricted ()
289 HttpResponse response = new HttpResponse (writer);
291 response.AddHeader (String.Empty, String.Empty);
293 catch (HttpException) {
299 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
301 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
303 public void BinaryWrite_Deny_Unrestricted ()
305 HttpResponse response = new HttpResponse (writer);
307 response.BinaryWrite (new byte[0]);
309 catch (HttpException) {
315 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
317 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
319 public void Pics_Deny_Unrestricted ()
321 HttpResponse response = new HttpResponse (writer);
323 response.Pics (String.Empty);
325 catch (HttpException) {
331 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
332 [ExpectedException (typeof (SecurityException))]
333 public void AppendToLog_Deny_Medium ()
335 HttpResponse response = new HttpResponse (writer);
336 response.AppendToLog ("mono");
340 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
341 public void AppendToLog_PermitOnly_Medium ()
343 HttpResponse response = new HttpResponse (writer);
344 response.AppendToLog ("mono");
348 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
349 [ExpectedException (typeof (SecurityException))]
350 public void TransmitFile_Deny_FileIOPermission ()
352 HttpResponse response = new HttpResponse (writer);
353 response.TransmitFile (fname);
357 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
358 public void TransmitFile_PermitOnly_FileIOPermission ()
360 HttpResponse response = new HttpResponse (writer);
361 response.TransmitFile (fname);
365 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
366 [ExpectedException (typeof (SecurityException))]
367 public void WriteFile_String_Deny_FileIOPermission ()
369 HttpResponse response = new HttpResponse (writer);
370 response.WriteFile (fname);
374 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
375 [ExpectedException (typeof (SecurityException))]
376 public void WriteFile_StringBool_Deny_FileIOPermission ()
378 HttpResponse response = new HttpResponse (writer);
379 response.WriteFile (fname, false);
383 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
384 [ExpectedException (typeof (SecurityException))]
385 public void WriteFile_StringIntInt_Deny_FileIOPermission ()
387 HttpResponse response = new HttpResponse (writer);
388 response.WriteFile (fname, 0, 1);
392 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
393 public void WriteFile_PermitOnly_FileIOPermission ()
395 HttpResponse response = new HttpResponse (writer);
396 response.WriteFile (fname);
397 response.WriteFile (fname, false);
398 response.WriteFile (fname, 0, 0);
402 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
403 [ExpectedException (typeof (SecurityException))]
404 public void WriteFile_Deny_UnmanagedCode ()
406 HttpResponse response = new HttpResponse (writer);
407 response.WriteFile (handle, 0, 1);
411 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
412 public void WriteFile_PermitOnly_UnmanagedCode ()
414 HttpResponse response = new HttpResponse (writer);
415 response.WriteFile (handle, 0, 1);
420 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
422 ConstructorInfo ci = this.Type.GetConstructor (new Type[1] { typeof (TextWriter) });
423 Assert.IsNotNull (ci, ".ctor(TextWriter)");
424 return ci.Invoke (new object[1] { writer });
427 public override Type Type {
428 get { return typeof (HttpResponse); }