[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web / HttpResponseCas.cs
1 //
2 // HttpResponseCas.cs - CAS unit tests for System.Web.HttpResponse
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
29 using NUnit.Framework;
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Text;
38 using System.Web;
39 using System.Web.Caching;
40
41 namespace MonoCasTests.System.Web {
42
43         [TestFixture]
44         [Category ("CAS")]
45         public class HttpResponseCas : AspNetHostingMinimal {
46
47                 private StringWriter writer;
48                 private String fname;
49                 private FileStream fs;
50                 private IntPtr handle;
51
52                 [TestFixtureSetUp]
53                 public void FixtureSetUp ()
54                 {
55                         // running at full-trust
56                         writer = new StringWriter ();
57                 }
58
59                 [SetUp]
60                 public override void SetUp ()
61                 {
62                         // running at full-trust too
63                         base.SetUp ();
64
65                         fname = Path.GetTempFileName ();
66                         fs = new FileStream (fname, FileMode.Open, FileAccess.Read);
67                         handle = fs.Handle;
68                 }
69
70                 [TearDown]
71                 public void TearDown ()
72                 {
73                         try {
74                                 if (fs != null)
75                                         fs.Close ();
76                                 handle = IntPtr.Zero;
77                                 if (File.Exists (fname))
78                                         File.Delete (fname);
79                         }
80                         catch {
81                         }
82                 }
83
84                 [Test]
85                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
86                 public void Properties_Deny_Unrestricted ()
87                 {
88                         HttpResponse response = new HttpResponse (writer);
89
90                         response.Buffer = false;                        
91                         Assert.IsFalse (response.Buffer, "Buffer");
92
93                         response.BufferOutput = false;
94                         Assert.IsFalse (response.BufferOutput, "BufferOutput");
95
96                         Assert.IsNotNull (response.Cache, "Cache");
97
98                         response.CacheControl = "public";
99                         Assert.AreEqual ("public", response.CacheControl, "CacheControl");
100
101                         response.ContentEncoding = Encoding.UTF8;
102                         Assert.AreEqual (Encoding.UTF8, response.ContentEncoding, "ContentEncoding");
103
104                         response.ContentType = String.Empty;
105                         Assert.AreEqual (String.Empty, response.ContentType, "ContentType");
106
107                         response.Charset = Encoding.UTF8.WebName;
108                         Assert.AreEqual (Encoding.UTF8.WebName, response.Charset, "Charset");
109
110                         Assert.IsNotNull (response.Cookies, "Cookies");
111
112                         try {
113                                 response.Expires = 2;
114                         }
115                         catch (NullReferenceException) {
116                                 // ms
117                         }
118                         Assert.IsTrue (response.Expires > 0, "Expires");
119
120                         response.ExpiresAbsolute = DateTime.MinValue;
121                         Assert.AreEqual (DateTime.MinValue, response.ExpiresAbsolute, "ExpiresAbsolute");
122
123                         Assert.IsTrue (response.IsClientConnected, "IsClientConnected");
124                         Assert.IsNotNull (response.Output, "Ouput");
125
126                         response.RedirectLocation = String.Empty;
127                         Assert.AreEqual (String.Empty, response.RedirectLocation, "RedirectLocation");
128
129                         response.Status = "501 Not Ok";
130                         Assert.AreEqual ("501 Not Ok", response.Status, "Status");
131
132                         response.StatusCode = 501;
133                         Assert.AreEqual (501, response.StatusCode, "StatusCode");
134
135                         response.StatusDescription = "Not Ok";
136                         Assert.AreEqual ("Not Ok", response.StatusDescription, "StatusDescription");
137
138                         response.SuppressContent = false;
139                         Assert.IsFalse (response.SuppressContent, "SuppressContent");
140                         response.HeaderEncoding = Encoding.UTF8;
141                         Assert.AreEqual (Encoding.UTF8, response.HeaderEncoding, "HeaderEncoding");
142
143                         Assert.IsFalse (response.IsRequestBeingRedirected, "IsRequestBeingRedirected");
144                 }
145
146                 [Test]
147                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
148 #if ONLY_1_1
149                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
150 #endif
151                 public void Filter_Deny_Unrestricted ()
152                 {
153                         HttpResponse response = new HttpResponse (writer);
154                         try {
155                                 response.Filter = new MemoryStream ();
156                         }
157                         catch (HttpException) {
158                                 // ms
159                         }
160
161                         Assert.IsNull (response.Filter, "Filter");
162                 }
163
164                 [Test]
165                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
166 #if ONLY_1_1
167                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
168 #endif
169                 public void OutputStream_Deny_Unrestricted ()
170                 {
171                         HttpResponse response = new HttpResponse (writer);
172                         try {
173                                 Assert.IsNotNull (response.OutputStream, "OutputStream");
174                         }
175                         catch (HttpException) {
176                                 // ms 2.0
177                         }
178                 }
179
180                 private string Callback (HttpContext context)
181                 {
182                         return string.Empty;
183                 }
184
185                 [Test]
186                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
187                 public void Methods_Deny_Unrestricted ()
188                 {
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]);
197
198                         try {
199                                 response.AppendCookie (new HttpCookie ("mono"));
200                         }
201                         catch (NullReferenceException) {
202                                 // ms 
203                         }
204
205                         try {
206                                 Assert.IsNull (response.ApplyAppPathModifier (null), "ApplyAppPathModifier");
207                         }
208                         catch (NullReferenceException) {
209                                 // ms 
210                         }
211
212                         try {
213                                 response.Clear ();
214                         }
215                         catch (NullReferenceException) {
216                                 // ms 
217                         }
218                 
219                         try {
220                                 response.ClearContent ();
221                         }
222                         catch (NullReferenceException) {
223                                 // ms 
224                         }
225                 
226                         try {
227                                 response.ClearHeaders ();
228                         }
229                         catch (NullReferenceException) {
230                                 // ms 
231                         }
232
233                         try {
234                                 response.Redirect ("http://www.mono-project.com");
235                         }
236                         catch (NullReferenceException) {
237                                 // ms 
238                         }
239                         try {
240                                 response.Redirect ("http://www.mono-project.com", false);
241                         }
242                         catch (NullReferenceException) {
243                                 // ms 
244                         }
245
246                         try {
247                                 response.SetCookie (new HttpCookie ("mono"));
248                         }
249                         catch (NullReferenceException) {
250                                 // ms 
251                         }
252
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));
258
259                         response.Flush ();
260
261                         response.Close ();
262
263                         try {
264                                 response.End ();
265                         }
266                         catch (NullReferenceException) {
267                                 // ms 
268                         }
269                 }
270
271                 [Test]
272                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
273 #if ONLY_1_1
274                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
275 #endif
276                 public void AppendHeader_Deny_Unrestricted ()
277                 {
278                         HttpResponse response = new HttpResponse (writer);
279                         response.AppendHeader ("monkey", "mono");
280                 }
281
282                 [Test]
283                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
284 #if ONLY_1_1
285                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
286 #endif
287                 public void AddHeader_Deny_Unrestricted ()
288                 {
289                         HttpResponse response = new HttpResponse (writer);
290                         try {
291                                 response.AddHeader (String.Empty, String.Empty);
292                         }
293                         catch (HttpException) {
294                                 // ms 2.0
295                         }
296                 }
297
298                 [Test]
299                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
300 #if ONLY_1_1
301                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
302 #endif
303                 public void BinaryWrite_Deny_Unrestricted ()
304                 {
305                         HttpResponse response = new HttpResponse (writer);
306                         try {
307                                 response.BinaryWrite (new byte[0]);
308                         }
309                         catch (HttpException) {
310                                 // ms 
311                         }
312                 }
313
314                 [Test]
315                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
316 #if ONLY_1_1
317                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
318 #endif
319                 public void Pics_Deny_Unrestricted ()
320                 {
321                         HttpResponse response = new HttpResponse (writer);
322                         try {
323                                 response.Pics (String.Empty);
324                         }
325                         catch (HttpException) {
326                                 // ms 
327                         }
328                 }
329
330                 [Test]
331                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
332                 [ExpectedException (typeof (SecurityException))]
333                 public void AppendToLog_Deny_Medium ()
334                 {
335                         HttpResponse response = new HttpResponse (writer);
336                         response.AppendToLog ("mono");
337                 }
338
339                 [Test]
340                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
341                 public void AppendToLog_PermitOnly_Medium ()
342                 {
343                         HttpResponse response = new HttpResponse (writer);
344                         response.AppendToLog ("mono");
345                 }
346
347                 [Test]
348                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
349                 [ExpectedException (typeof (SecurityException))]
350                 public void TransmitFile_Deny_FileIOPermission ()
351                 {
352                         HttpResponse response = new HttpResponse (writer);
353                         response.TransmitFile (fname);
354                 }
355
356                 [Test]
357                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
358                 public void TransmitFile_PermitOnly_FileIOPermission ()
359                 {
360                         HttpResponse response = new HttpResponse (writer);
361                         response.TransmitFile (fname);
362                 }
363
364                 [Test]
365                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
366                 [ExpectedException (typeof (SecurityException))]
367                 public void WriteFile_String_Deny_FileIOPermission ()
368                 {
369                         HttpResponse response = new HttpResponse (writer);
370                         response.WriteFile (fname);
371                 }
372
373                 [Test]
374                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
375                 [ExpectedException (typeof (SecurityException))]
376                 public void WriteFile_StringBool_Deny_FileIOPermission ()
377                 {
378                         HttpResponse response = new HttpResponse (writer);
379                         response.WriteFile (fname, false);
380                 }
381
382                 [Test]
383                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
384                 [ExpectedException (typeof (SecurityException))]
385                 public void WriteFile_StringIntInt_Deny_FileIOPermission ()
386                 {
387                         HttpResponse response = new HttpResponse (writer);
388                         response.WriteFile (fname, 0, 1);
389                 }
390
391                 [Test]
392                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
393                 public void WriteFile_PermitOnly_FileIOPermission ()
394                 {
395                         HttpResponse response = new HttpResponse (writer);
396                         response.WriteFile (fname);
397                         response.WriteFile (fname, false);
398                         response.WriteFile (fname, 0, 0);
399                 }
400
401                 [Test]
402                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
403                 [ExpectedException (typeof (SecurityException))]
404                 public void WriteFile_Deny_UnmanagedCode ()
405                 {
406                         HttpResponse response = new HttpResponse (writer);
407                         response.WriteFile (handle, 0, 1);
408                 }
409
410                 [Test]
411                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
412                 public void WriteFile_PermitOnly_UnmanagedCode ()
413                 {
414                         HttpResponse response = new HttpResponse (writer);
415                         response.WriteFile (handle, 0, 1);
416                 }
417
418                 // LinkDemand
419
420                 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
421                 {
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 });
425                 }
426
427                 public override Type Type {
428                         get { return typeof (HttpResponse); }
429                 }
430         }
431 }