New test.
[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 #if NET_2_0
125                         try {
126                                 hsu.Transfer ((IHttpHandler)null, true);
127                         }
128                         catch (NullReferenceException) {
129                                 // ms
130                         }
131                         try {
132                                 hsu.Transfer ((IHttpHandler)null, false);
133                         }
134                         catch (NullReferenceException) {
135                                 // ms
136                         }
137 #endif
138                         try {
139                                 Assert.IsNotNull (hsu.UrlDecode (url), "UrlDecode(string)");
140                         }
141                         catch (NullReferenceException) {
142                                 // ms
143                         }
144                         try {
145                                 hsu.UrlDecode ("http://www.mono-project.com/", sw);
146                         }
147                         catch (NullReferenceException) {
148                                 // ms
149                         }
150
151                         Assert.IsNotNull (hsu.UrlEncode (String.Empty), "UrlEncode(string)");
152                         hsu.UrlEncode (String.Empty, sw);
153
154                         Assert.IsNotNull (hsu.UrlPathEncode (String.Empty), "UrlPathEncode(string)");
155                 }
156
157                 [Test]
158                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
159                 [ExpectedException (typeof (SecurityException))]
160                 public void CreateObject_String_Deny_UnmanagedCode ()
161                 {
162                         hsu.CreateObject (String.Empty);
163                 }
164
165                 [Test]
166                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
167                 [ExpectedException (typeof (HttpException))] // String.Empty isn't valid
168                 public void CreateObject_String_PermitOnly_UnmanagedCode ()
169                 {
170                         hsu.CreateObject (String.Empty);
171                 }
172
173                 [Test]
174                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
175                 [ExpectedException (typeof (SecurityException))]
176                 public void CreateObject_Type_Deny_UnmanagedCode ()
177                 {
178                         hsu.CreateObject (String.Empty);
179                 }
180
181                 [Test]
182                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
183                 public void CreateObject_Type_PermitOnly_UnmanagedCode ()
184                 {
185                         try {
186                                 hsu.CreateObject (typeof (string));
187                         }
188                         catch (MissingMethodException) {
189                                 // ms
190                         }
191                         catch (HttpException) {
192                                 // mono
193                         }
194                 }
195
196                 [Test]
197                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
198                 [ExpectedException (typeof (SecurityException))]
199                 public void CreateObjectFromClsid_String_Deny_UnmanagedCode ()
200                 {
201                         hsu.CreateObjectFromClsid (String.Empty);
202                 }
203
204                 [Test]
205                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
206                 public void CreateObjectFromClsid_PermitOnly_UnmanagedCode ()
207                 {
208                         try {
209                                 hsu.CreateObjectFromClsid (String.Empty);
210                         }
211                         catch (FormatException) {
212                                 // ms (not a valid guid)
213                         }
214                         catch (HttpException) {
215                                 // mono
216                         }
217                 }
218
219                 [Test]
220                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
221                 [ExpectedException (typeof (NullReferenceException))]
222                 public void Execute_String_Deny_Unrestricted ()
223                 {
224                         hsu.Execute (String.Empty);
225                 }
226
227                 [Test]
228                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
229                 [ExpectedException (typeof (NullReferenceException))]
230                 public void Execute_StringTextWriter_Deny_Unrestricted ()
231                 {
232                         hsu.Execute (String.Empty, sw);
233                 }
234
235 #if NET_2_0
236                 [Test]
237                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
238                 [ExpectedException (typeof (NullReferenceException))]
239                 public void Execute_StringTextWriterTrue_Deny_Unrestricted ()
240                 {
241                         hsu.Execute (String.Empty, sw, true);
242                 }
243
244                 [Test]
245                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
246                 [ExpectedException (typeof (NullReferenceException))]
247                 public void Execute_StringTextWriterFalse_Deny_Unrestricted ()
248                 {
249                         hsu.Execute (String.Empty, sw, false);
250                 }
251 #endif
252
253                 [Test]
254                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
255                 [ExpectedException (typeof (SecurityException))]
256                 public void MachineName_Deny_Medium ()
257                 {
258                         Assert.IsNotNull (hsu.MachineName, "MachineName");
259                 }
260
261                 [Test]
262                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
263                 public void MachineName_PermitOnly_Medium ()
264                 {
265                         Assert.IsNotNull (hsu.MachineName, "MachineName");
266                 }
267
268                 // LinkDemand
269
270                 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
271                 {
272                         // there are no public ctor so we're taking a method that we know isn't protected
273                         // (by a Demand) and call it thru reflection so any linkdemand (on the class) will
274                         // be promoted to a Demand
275                         MethodInfo mi = this.Type.GetMethod ("HtmlDecode", new Type[1] { typeof (string) } );
276                         return mi.Invoke (hsu, new object[1] { String.Empty });
277                 }
278
279                 public override Type Type {
280                         get { return typeof (HttpServerUtility); }
281                 }
282         }
283 }