[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web / HttpServerUtilityCas.cs
1 //
2 // HttpServerUtilityCas.cs - CAS unit tests for System.Web.HttpServerUtility
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.IO;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Web;
37
38 namespace MonoCasTests.System.Web {
39
40         [TestFixture]
41         [Category ("CAS")]
42         public class HttpServerUtilityCas : AspNetHostingMinimal {
43
44                 private const string url = "http://www.mono-project.com/";
45
46                 private StringWriter sw;
47                 private HttpContext context;
48                 private HttpServerUtility hsu;
49
50                 [TestFixtureSetUp]
51                 public void FixtureSetUp ()
52                 {
53                         sw = new StringWriter ();
54                         context = new HttpContext (null);
55                         hsu = context.Server;
56                 }
57
58                 [Test]
59                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
60                 public void Properties_Deny_Unrestricted ()
61                 {
62                         try {
63                                 Assert.IsTrue (hsu.ScriptTimeout > 0, "ScriptTimeout");
64                         }
65                         catch (NullReferenceException) {
66                                 // ms 1.x, mono
67                         }
68                 }
69
70                 [Test]
71                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
72                 [ExpectedException (typeof (SecurityException))]
73                 public void ScriptTimeout_Deny_Unrestricted ()
74                 {
75                         hsu.ScriptTimeout = 1;
76                 }
77
78                 [Test]
79                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
80                 public void ScriptTimeout_PermitOnly_Unrestricted ()
81                 {
82                         hsu.ScriptTimeout = 1;
83                 }
84
85                 [Test]
86                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
87                 public void Methods_Deny_Unrestricted ()
88                 {
89                         hsu.ClearError ();
90
91                         Assert.IsNull (hsu.GetLastError (), "GetLastError");
92
93                         Assert.IsNotNull (hsu.HtmlDecode (String.Empty), "HtmlDecode(string)");
94                         hsu.HtmlDecode (String.Empty, sw);
95
96                         Assert.IsNotNull (hsu.HtmlEncode (String.Empty), "HtmlEncode(string)");
97                         hsu.HtmlEncode (String.Empty, sw);
98
99                         try {
100                                 Assert.IsNull (hsu.MapPath (String.Empty), "MapPath(string)");
101                         }
102                         catch (NullReferenceException) {
103                                 // ms 1.x
104                         }
105
106                         try {
107                                 hsu.Transfer ("/");
108                         }
109                         catch (NullReferenceException) {
110                                 // ms
111                         }
112                         try {
113                                 hsu.Transfer ("/", true);
114                         }
115                         catch (NullReferenceException) {
116                                 // ms
117                         }
118                         try {
119                                 hsu.Transfer ("/", false);
120                         }
121                         catch (NullReferenceException) {
122                                 // ms
123                         }
124                         try {
125                                 hsu.Transfer ((IHttpHandler)null, true);
126                         }
127                         catch (NullReferenceException) {
128                                 // ms
129                         }
130                         try {
131                                 hsu.Transfer ((IHttpHandler)null, false);
132                         }
133                         catch (NullReferenceException) {
134                                 // ms
135                         }
136                         try {
137                                 Assert.IsNotNull (hsu.UrlDecode (url), "UrlDecode(string)");
138                         }
139                         catch (NullReferenceException) {
140                                 // ms
141                         }
142                         try {
143                                 hsu.UrlDecode ("http://www.mono-project.com/", sw);
144                         }
145                         catch (NullReferenceException) {
146                                 // ms
147                         }
148
149                         Assert.IsNotNull (hsu.UrlEncode (String.Empty), "UrlEncode(string)");
150                         hsu.UrlEncode (String.Empty, sw);
151
152                         Assert.IsNotNull (hsu.UrlPathEncode (String.Empty), "UrlPathEncode(string)");
153                 }
154
155                 [Test]
156                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
157                 [ExpectedException (typeof (SecurityException))]
158                 public void CreateObject_String_Deny_UnmanagedCode ()
159                 {
160                         hsu.CreateObject (String.Empty);
161                 }
162
163                 [Test]
164                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
165                 [ExpectedException (typeof (HttpException))] // String.Empty isn't valid
166                 public void CreateObject_String_PermitOnly_UnmanagedCode ()
167                 {
168                         hsu.CreateObject (String.Empty);
169                 }
170
171                 [Test]
172                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
173                 [ExpectedException (typeof (SecurityException))]
174                 public void CreateObject_Type_Deny_UnmanagedCode ()
175                 {
176                         hsu.CreateObject (String.Empty);
177                 }
178
179                 [Test]
180                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
181                 public void CreateObject_Type_PermitOnly_UnmanagedCode ()
182                 {
183                         try {
184                                 hsu.CreateObject (typeof (string));
185                         }
186                         catch (MissingMethodException) {
187                                 // ms
188                         }
189                         catch (HttpException) {
190                                 // mono
191                         }
192                 }
193
194                 [Test]
195                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
196                 [ExpectedException (typeof (SecurityException))]
197                 public void CreateObjectFromClsid_String_Deny_UnmanagedCode ()
198                 {
199                         hsu.CreateObjectFromClsid (String.Empty);
200                 }
201
202                 [Test]
203                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
204                 public void CreateObjectFromClsid_PermitOnly_UnmanagedCode ()
205                 {
206                         try {
207                                 hsu.CreateObjectFromClsid (String.Empty);
208                         }
209                         catch (FormatException) {
210                                 // ms (not a valid guid)
211                         }
212                         catch (HttpException) {
213                                 // mono
214                         }
215                 }
216
217                 [Test]
218                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
219                 [ExpectedException (typeof (NullReferenceException))]
220                 public void Execute_String_Deny_Unrestricted ()
221                 {
222                         hsu.Execute (String.Empty);
223                 }
224
225                 [Test]
226                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
227                 [ExpectedException (typeof (NullReferenceException))]
228                 public void Execute_StringTextWriter_Deny_Unrestricted ()
229                 {
230                         hsu.Execute (String.Empty, sw);
231                 }
232
233                 [Test]
234                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
235                 [ExpectedException (typeof (NullReferenceException))]
236                 public void Execute_StringTextWriterTrue_Deny_Unrestricted ()
237                 {
238                         hsu.Execute (String.Empty, sw, true);
239                 }
240
241                 [Test]
242                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
243                 [ExpectedException (typeof (NullReferenceException))]
244                 public void Execute_StringTextWriterFalse_Deny_Unrestricted ()
245                 {
246                         hsu.Execute (String.Empty, sw, false);
247                 }
248
249                 [Test]
250                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
251                 [ExpectedException (typeof (SecurityException))]
252                 public void MachineName_Deny_Medium ()
253                 {
254                         Assert.IsNotNull (hsu.MachineName, "MachineName");
255                 }
256
257                 [Test]
258                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
259                 public void MachineName_PermitOnly_Medium ()
260                 {
261                         Assert.IsNotNull (hsu.MachineName, "MachineName");
262                 }
263
264                 // LinkDemand
265
266                 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
267                 {
268                         // there are no public ctor so we're taking a method that we know isn't protected
269                         // (by a Demand) and call it thru reflection so any linkdemand (on the class) will
270                         // be promoted to a Demand
271                         MethodInfo mi = this.Type.GetMethod ("HtmlDecode", new Type[1] { typeof (string) } );
272                         return mi.Invoke (hsu, new object[1] { String.Empty });
273                 }
274
275                 public override Type Type {
276                         get { return typeof (HttpServerUtility); }
277                 }
278         }
279 }