| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- //
- // HttpResponseCas.cs - CAS unit tests for System.Web.HttpResponse
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using NUnit.Framework;
- using System;
- using System.Collections;
- using System.IO;
- using System.Reflection;
- using System.Security;
- using System.Security.Permissions;
- using System.Text;
- using System.Web;
- using System.Web.Caching;
- namespace MonoCasTests.System.Web {
- [TestFixture]
- [Category ("CAS")]
- public class HttpResponseCas : AspNetHostingMinimal {
- private StringWriter writer;
- private String fname;
- private FileStream fs;
- private IntPtr handle;
- [TestFixtureSetUp]
- public void FixtureSetUp ()
- {
- // running at full-trust
- writer = new StringWriter ();
- }
- [SetUp]
- public override void SetUp ()
- {
- // running at full-trust too
- base.SetUp ();
- fname = Path.GetTempFileName ();
- fs = new FileStream (fname, FileMode.Open, FileAccess.Read);
- handle = fs.Handle;
- }
- [TearDown]
- public void TearDown ()
- {
- try {
- if (fs != null)
- fs.Close ();
- handle = IntPtr.Zero;
- if (File.Exists (fname))
- File.Delete (fname);
- }
- catch {
- }
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- public void Properties_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.Buffer = false;
- Assert.IsFalse (response.Buffer, "Buffer");
- response.BufferOutput = false;
- Assert.IsFalse (response.BufferOutput, "BufferOutput");
- Assert.IsNotNull (response.Cache, "Cache");
- response.CacheControl = "public";
- Assert.AreEqual ("public", response.CacheControl, "CacheControl");
- response.ContentEncoding = Encoding.UTF8;
- Assert.AreEqual (Encoding.UTF8, response.ContentEncoding, "ContentEncoding");
- response.ContentType = String.Empty;
- Assert.AreEqual (String.Empty, response.ContentType, "ContentType");
- response.Charset = Encoding.UTF8.WebName;
- Assert.AreEqual (Encoding.UTF8.WebName, response.Charset, "Charset");
- Assert.IsNotNull (response.Cookies, "Cookies");
- try {
- response.Expires = 2;
- }
- catch (NullReferenceException) {
- // ms
- }
- Assert.IsTrue (response.Expires > 0, "Expires");
- response.ExpiresAbsolute = DateTime.MinValue;
- Assert.AreEqual (DateTime.MinValue, response.ExpiresAbsolute, "ExpiresAbsolute");
- Assert.IsTrue (response.IsClientConnected, "IsClientConnected");
- Assert.IsNotNull (response.Output, "Ouput");
- response.RedirectLocation = String.Empty;
- Assert.AreEqual (String.Empty, response.RedirectLocation, "RedirectLocation");
- response.Status = "501 Not Ok";
- Assert.AreEqual ("501 Not Ok", response.Status, "Status");
- response.StatusCode = 501;
- Assert.AreEqual (501, response.StatusCode, "StatusCode");
- response.StatusDescription = "Not Ok";
- Assert.AreEqual ("Not Ok", response.StatusDescription, "StatusDescription");
- response.SuppressContent = false;
- Assert.IsFalse (response.SuppressContent, "SuppressContent");
- #if NET_2_0
- response.HeaderEncoding = Encoding.UTF8;
- Assert.AreEqual (Encoding.UTF8, response.HeaderEncoding, "HeaderEncoding");
- Assert.IsFalse (response.IsRequestBeingRedirected, "IsRequestBeingRedirected");
- #endif
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- #if ONLY_1_1
- [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
- #endif
- public void Filter_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- try {
- response.Filter = new MemoryStream ();
- }
- catch (HttpException) {
- // ms
- }
- Assert.IsNull (response.Filter, "Filter");
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- #if ONLY_1_1
- [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
- #endif
- public void OutputStream_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- try {
- Assert.IsNotNull (response.OutputStream, "OutputStream");
- }
- catch (HttpException) {
- // ms 2.0
- }
- }
- private string Callback (HttpContext context)
- {
- return string.Empty;
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- public void Methods_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.AddCacheItemDependencies (new ArrayList ());
- response.AddCacheItemDependency (String.Empty);
- response.AddFileDependencies (new ArrayList ());
- response.AddFileDependency (fname);
- #if NET_2_0
- response.AddCacheDependency (new CacheDependency[0]);
- response.AddCacheItemDependencies (new string [0]);
- response.AddFileDependencies (new string [0]);
- #endif
- try {
- response.AppendCookie (new HttpCookie ("mono"));
- }
- catch (NullReferenceException) {
- // ms
- }
- try {
- Assert.IsNull (response.ApplyAppPathModifier (null), "ApplyAppPathModifier");
- }
- catch (NullReferenceException) {
- // ms
- }
- try {
- response.Clear ();
- }
- catch (NullReferenceException) {
- // ms
- }
-
- try {
- response.ClearContent ();
- }
- catch (NullReferenceException) {
- // ms
- }
-
- try {
- response.ClearHeaders ();
- }
- catch (NullReferenceException) {
- // ms
- }
- try {
- response.Redirect ("http://www.mono-project.com");
- }
- catch (NullReferenceException) {
- // ms
- }
- try {
- response.Redirect ("http://www.mono-project.com", false);
- }
- catch (NullReferenceException) {
- // ms
- }
- try {
- response.SetCookie (new HttpCookie ("mono"));
- }
- catch (NullReferenceException) {
- // ms
- }
- response.Write (String.Empty);
- response.Write (Char.MinValue);
- response.Write (new char[0], 0, 0);
- response.Write (this);
- #if NET_2_0
- response.WriteSubstitution (new HttpResponseSubstitutionCallback (Callback));
- #endif
- response.Flush ();
- response.Close ();
- try {
- response.End ();
- }
- catch (NullReferenceException) {
- // ms
- }
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- #if ONLY_1_1
- [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
- #endif
- public void AppendHeader_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.AppendHeader ("monkey", "mono");
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- #if ONLY_1_1
- [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
- #endif
- public void AddHeader_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- try {
- response.AddHeader (String.Empty, String.Empty);
- }
- catch (HttpException) {
- // ms 2.0
- }
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- #if ONLY_1_1
- [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
- #endif
- public void BinaryWrite_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- try {
- response.BinaryWrite (new byte[0]);
- }
- catch (HttpException) {
- // ms
- }
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- #if ONLY_1_1
- [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
- #endif
- public void Pics_Deny_Unrestricted ()
- {
- HttpResponse response = new HttpResponse (writer);
- try {
- response.Pics (String.Empty);
- }
- catch (HttpException) {
- // ms
- }
- }
- [Test]
- [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
- [ExpectedException (typeof (SecurityException))]
- public void AppendToLog_Deny_Medium ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.AppendToLog ("mono");
- }
- [Test]
- [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
- public void AppendToLog_PermitOnly_Medium ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.AppendToLog ("mono");
- }
- [Test]
- [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void TransmitFile_Deny_FileIOPermission ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.TransmitFile (fname);
- }
- [Test]
- [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- public void TransmitFile_PermitOnly_FileIOPermission ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.TransmitFile (fname);
- }
- [Test]
- [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void WriteFile_String_Deny_FileIOPermission ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.WriteFile (fname);
- }
- [Test]
- [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void WriteFile_StringBool_Deny_FileIOPermission ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.WriteFile (fname, false);
- }
- [Test]
- [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void WriteFile_StringIntInt_Deny_FileIOPermission ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.WriteFile (fname, 0, 1);
- }
- [Test]
- [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- public void WriteFile_PermitOnly_FileIOPermission ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.WriteFile (fname);
- response.WriteFile (fname, false);
- response.WriteFile (fname, 0, 0);
- }
- [Test]
- [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
- [ExpectedException (typeof (SecurityException))]
- public void WriteFile_Deny_UnmanagedCode ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.WriteFile (handle, 0, 1);
- }
- [Test]
- [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
- public void WriteFile_PermitOnly_UnmanagedCode ()
- {
- HttpResponse response = new HttpResponse (writer);
- response.WriteFile (handle, 0, 1);
- }
- // LinkDemand
- public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
- {
- ConstructorInfo ci = this.Type.GetConstructor (new Type[1] { typeof (TextWriter) });
- Assert.IsNotNull (ci, ".ctor(TextWriter)");
- return ci.Invoke (new object[1] { writer });
- }
- public override Type Type {
- get { return typeof (HttpResponse); }
- }
- }
- }
|