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");
141 response.HeaderEncoding = Encoding.UTF8;
142 Assert.AreEqual (Encoding.UTF8, response.HeaderEncoding, "HeaderEncoding");
144 Assert.IsFalse (response.IsRequestBeingRedirected, "IsRequestBeingRedirected");
149 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
151 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
153 public void Filter_Deny_Unrestricted ()
155 HttpResponse response = new HttpResponse (writer);
157 response.Filter = new MemoryStream ();
159 catch (HttpException) {
163 Assert.IsNull (response.Filter, "Filter");
167 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
169 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
171 public void OutputStream_Deny_Unrestricted ()
173 HttpResponse response = new HttpResponse (writer);
175 Assert.IsNotNull (response.OutputStream, "OutputStream");
177 catch (HttpException) {
182 private string Callback (HttpContext context)
188 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
189 public void Methods_Deny_Unrestricted ()
191 HttpResponse response = new HttpResponse (writer);
192 response.AddCacheItemDependencies (new ArrayList ());
193 response.AddCacheItemDependency (String.Empty);
194 response.AddFileDependencies (new ArrayList ());
195 response.AddFileDependency (fname);
197 response.AddCacheDependency (new CacheDependency[0]);
198 response.AddCacheItemDependencies (new string [0]);
199 response.AddFileDependencies (new string [0]);
203 response.AppendCookie (new HttpCookie ("mono"));
205 catch (NullReferenceException) {
210 Assert.IsNull (response.ApplyAppPathModifier (null), "ApplyAppPathModifier");
212 catch (NullReferenceException) {
219 catch (NullReferenceException) {
224 response.ClearContent ();
226 catch (NullReferenceException) {
231 response.ClearHeaders ();
233 catch (NullReferenceException) {
238 response.Redirect ("http://www.mono-project.com");
240 catch (NullReferenceException) {
244 response.Redirect ("http://www.mono-project.com", false);
246 catch (NullReferenceException) {
251 response.SetCookie (new HttpCookie ("mono"));
253 catch (NullReferenceException) {
257 response.Write (String.Empty);
258 response.Write (Char.MinValue);
259 response.Write (new char[0], 0, 0);
260 response.Write (this);
262 response.WriteSubstitution (new HttpResponseSubstitutionCallback (Callback));
272 catch (NullReferenceException) {
278 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
280 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
282 public void AppendHeader_Deny_Unrestricted ()
284 HttpResponse response = new HttpResponse (writer);
285 response.AppendHeader ("monkey", "mono");
289 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
291 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
293 public void AddHeader_Deny_Unrestricted ()
295 HttpResponse response = new HttpResponse (writer);
297 response.AddHeader (String.Empty, String.Empty);
299 catch (HttpException) {
305 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
307 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
309 public void BinaryWrite_Deny_Unrestricted ()
311 HttpResponse response = new HttpResponse (writer);
313 response.BinaryWrite (new byte[0]);
315 catch (HttpException) {
321 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
323 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
325 public void Pics_Deny_Unrestricted ()
327 HttpResponse response = new HttpResponse (writer);
329 response.Pics (String.Empty);
331 catch (HttpException) {
337 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
338 [ExpectedException (typeof (SecurityException))]
339 public void AppendToLog_Deny_Medium ()
341 HttpResponse response = new HttpResponse (writer);
342 response.AppendToLog ("mono");
346 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
347 public void AppendToLog_PermitOnly_Medium ()
349 HttpResponse response = new HttpResponse (writer);
350 response.AppendToLog ("mono");
354 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
355 [ExpectedException (typeof (SecurityException))]
356 public void TransmitFile_Deny_FileIOPermission ()
358 HttpResponse response = new HttpResponse (writer);
359 response.TransmitFile (fname);
363 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
364 public void TransmitFile_PermitOnly_FileIOPermission ()
366 HttpResponse response = new HttpResponse (writer);
367 response.TransmitFile (fname);
371 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
372 [ExpectedException (typeof (SecurityException))]
373 public void WriteFile_String_Deny_FileIOPermission ()
375 HttpResponse response = new HttpResponse (writer);
376 response.WriteFile (fname);
380 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
381 [ExpectedException (typeof (SecurityException))]
382 public void WriteFile_StringBool_Deny_FileIOPermission ()
384 HttpResponse response = new HttpResponse (writer);
385 response.WriteFile (fname, false);
389 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
390 [ExpectedException (typeof (SecurityException))]
391 public void WriteFile_StringIntInt_Deny_FileIOPermission ()
393 HttpResponse response = new HttpResponse (writer);
394 response.WriteFile (fname, 0, 1);
398 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
399 public void WriteFile_PermitOnly_FileIOPermission ()
401 HttpResponse response = new HttpResponse (writer);
402 response.WriteFile (fname);
403 response.WriteFile (fname, false);
404 response.WriteFile (fname, 0, 0);
408 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
409 [ExpectedException (typeof (SecurityException))]
410 public void WriteFile_Deny_UnmanagedCode ()
412 HttpResponse response = new HttpResponse (writer);
413 response.WriteFile (handle, 0, 1);
417 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
418 public void WriteFile_PermitOnly_UnmanagedCode ()
420 HttpResponse response = new HttpResponse (writer);
421 response.WriteFile (handle, 0, 1);
426 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
428 ConstructorInfo ci = this.Type.GetConstructor (new Type[1] { typeof (TextWriter) });
429 Assert.IsNotNull (ci, ".ctor(TextWriter)");
430 return ci.Invoke (new object[1] { writer });
433 public override Type Type {
434 get { return typeof (HttpResponse); }