Merge pull request #960 from ermshiperete/ShowHelp
[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 #if NET_2_0
141                         response.HeaderEncoding = Encoding.UTF8;
142                         Assert.AreEqual (Encoding.UTF8, response.HeaderEncoding, "HeaderEncoding");
143
144                         Assert.IsFalse (response.IsRequestBeingRedirected, "IsRequestBeingRedirected");
145 #endif
146                 }
147
148                 [Test]
149                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
150 #if ONLY_1_1
151                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
152 #endif
153                 public void Filter_Deny_Unrestricted ()
154                 {
155                         HttpResponse response = new HttpResponse (writer);
156                         try {
157                                 response.Filter = new MemoryStream ();
158                         }
159                         catch (HttpException) {
160                                 // ms
161                         }
162
163                         Assert.IsNull (response.Filter, "Filter");
164                 }
165
166                 [Test]
167                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
168 #if ONLY_1_1
169                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
170 #endif
171                 public void OutputStream_Deny_Unrestricted ()
172                 {
173                         HttpResponse response = new HttpResponse (writer);
174                         try {
175                                 Assert.IsNotNull (response.OutputStream, "OutputStream");
176                         }
177                         catch (HttpException) {
178                                 // ms 2.0
179                         }
180                 }
181
182                 private string Callback (HttpContext context)
183                 {
184                         return string.Empty;
185                 }
186
187                 [Test]
188                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
189                 public void Methods_Deny_Unrestricted ()
190                 {
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);
196 #if NET_2_0
197                         response.AddCacheDependency (new CacheDependency[0]);
198                         response.AddCacheItemDependencies (new string [0]);
199                         response.AddFileDependencies (new string [0]);
200 #endif
201
202                         try {
203                                 response.AppendCookie (new HttpCookie ("mono"));
204                         }
205                         catch (NullReferenceException) {
206                                 // ms 
207                         }
208
209                         try {
210                                 Assert.IsNull (response.ApplyAppPathModifier (null), "ApplyAppPathModifier");
211                         }
212                         catch (NullReferenceException) {
213                                 // ms 
214                         }
215
216                         try {
217                                 response.Clear ();
218                         }
219                         catch (NullReferenceException) {
220                                 // ms 
221                         }
222                 
223                         try {
224                                 response.ClearContent ();
225                         }
226                         catch (NullReferenceException) {
227                                 // ms 
228                         }
229                 
230                         try {
231                                 response.ClearHeaders ();
232                         }
233                         catch (NullReferenceException) {
234                                 // ms 
235                         }
236
237                         try {
238                                 response.Redirect ("http://www.mono-project.com");
239                         }
240                         catch (NullReferenceException) {
241                                 // ms 
242                         }
243                         try {
244                                 response.Redirect ("http://www.mono-project.com", false);
245                         }
246                         catch (NullReferenceException) {
247                                 // ms 
248                         }
249
250                         try {
251                                 response.SetCookie (new HttpCookie ("mono"));
252                         }
253                         catch (NullReferenceException) {
254                                 // ms 
255                         }
256
257                         response.Write (String.Empty);
258                         response.Write (Char.MinValue);
259                         response.Write (new char[0], 0, 0);
260                         response.Write (this);
261 #if NET_2_0
262                         response.WriteSubstitution (new HttpResponseSubstitutionCallback (Callback));
263 #endif
264
265                         response.Flush ();
266
267                         response.Close ();
268
269                         try {
270                                 response.End ();
271                         }
272                         catch (NullReferenceException) {
273                                 // ms 
274                         }
275                 }
276
277                 [Test]
278                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
279 #if ONLY_1_1
280                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
281 #endif
282                 public void AppendHeader_Deny_Unrestricted ()
283                 {
284                         HttpResponse response = new HttpResponse (writer);
285                         response.AppendHeader ("monkey", "mono");
286                 }
287
288                 [Test]
289                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
290 #if ONLY_1_1
291                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
292 #endif
293                 public void AddHeader_Deny_Unrestricted ()
294                 {
295                         HttpResponse response = new HttpResponse (writer);
296                         try {
297                                 response.AddHeader (String.Empty, String.Empty);
298                         }
299                         catch (HttpException) {
300                                 // ms 2.0
301                         }
302                 }
303
304                 [Test]
305                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
306 #if ONLY_1_1
307                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
308 #endif
309                 public void BinaryWrite_Deny_Unrestricted ()
310                 {
311                         HttpResponse response = new HttpResponse (writer);
312                         try {
313                                 response.BinaryWrite (new byte[0]);
314                         }
315                         catch (HttpException) {
316                                 // ms 
317                         }
318                 }
319
320                 [Test]
321                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
322 #if ONLY_1_1
323                 [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
324 #endif
325                 public void Pics_Deny_Unrestricted ()
326                 {
327                         HttpResponse response = new HttpResponse (writer);
328                         try {
329                                 response.Pics (String.Empty);
330                         }
331                         catch (HttpException) {
332                                 // ms 
333                         }
334                 }
335
336                 [Test]
337                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
338                 [ExpectedException (typeof (SecurityException))]
339                 public void AppendToLog_Deny_Medium ()
340                 {
341                         HttpResponse response = new HttpResponse (writer);
342                         response.AppendToLog ("mono");
343                 }
344
345                 [Test]
346                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
347                 public void AppendToLog_PermitOnly_Medium ()
348                 {
349                         HttpResponse response = new HttpResponse (writer);
350                         response.AppendToLog ("mono");
351                 }
352
353                 [Test]
354                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
355                 [ExpectedException (typeof (SecurityException))]
356                 public void TransmitFile_Deny_FileIOPermission ()
357                 {
358                         HttpResponse response = new HttpResponse (writer);
359                         response.TransmitFile (fname);
360                 }
361
362                 [Test]
363                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
364                 public void TransmitFile_PermitOnly_FileIOPermission ()
365                 {
366                         HttpResponse response = new HttpResponse (writer);
367                         response.TransmitFile (fname);
368                 }
369
370                 [Test]
371                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
372                 [ExpectedException (typeof (SecurityException))]
373                 public void WriteFile_String_Deny_FileIOPermission ()
374                 {
375                         HttpResponse response = new HttpResponse (writer);
376                         response.WriteFile (fname);
377                 }
378
379                 [Test]
380                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
381                 [ExpectedException (typeof (SecurityException))]
382                 public void WriteFile_StringBool_Deny_FileIOPermission ()
383                 {
384                         HttpResponse response = new HttpResponse (writer);
385                         response.WriteFile (fname, false);
386                 }
387
388                 [Test]
389                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
390                 [ExpectedException (typeof (SecurityException))]
391                 public void WriteFile_StringIntInt_Deny_FileIOPermission ()
392                 {
393                         HttpResponse response = new HttpResponse (writer);
394                         response.WriteFile (fname, 0, 1);
395                 }
396
397                 [Test]
398                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
399                 public void WriteFile_PermitOnly_FileIOPermission ()
400                 {
401                         HttpResponse response = new HttpResponse (writer);
402                         response.WriteFile (fname);
403                         response.WriteFile (fname, false);
404                         response.WriteFile (fname, 0, 0);
405                 }
406
407                 [Test]
408                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
409                 [ExpectedException (typeof (SecurityException))]
410                 public void WriteFile_Deny_UnmanagedCode ()
411                 {
412                         HttpResponse response = new HttpResponse (writer);
413                         response.WriteFile (handle, 0, 1);
414                 }
415
416                 [Test]
417                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
418                 public void WriteFile_PermitOnly_UnmanagedCode ()
419                 {
420                         HttpResponse response = new HttpResponse (writer);
421                         response.WriteFile (handle, 0, 1);
422                 }
423
424                 // LinkDemand
425
426                 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
427                 {
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 });
431                 }
432
433                 public override Type Type {
434                         get { return typeof (HttpResponse); }
435                 }
436         }
437 }