2007-03-22 Adar Wesley <adarw@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web / HttpRuntimeCas.cs
1 //
2 // HttpRuntimeCas.cs - CAS unit tests for System.Web.HttpRuntime
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.Reflection;
33 using System.Security;
34 using System.Security.Permissions;
35 using System.Security.Principal;
36 using System.Web;
37
38 namespace MonoCasTests.System.Web {
39
40         [TestFixture]
41         [Category ("CAS")]
42         public class HttpRuntimeCas : AspNetHostingMinimal {
43
44                 [TestFixtureSetUp]
45                 public void FixtureSetUp ()
46                 {
47                         // static ctor at fulltrust
48                         new HttpRuntime ();
49                 }
50
51 #if NET_2_0
52                 [Test]
53                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
54                 public void Constructor_Deny_Unrestricted ()
55                 {
56                         new HttpRuntime ();
57                 }
58 #else
59                 [Test]
60                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
61                 [ExpectedException (typeof (SecurityException))]
62                 public void Constructor_Deny_UnmanagedCode ()
63                 {
64                         new HttpRuntime ();
65                 }
66
67                 [Test]
68                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
69                 public void Constructor_PermitOnly_UnmanagedCode ()
70                 {
71                         new HttpRuntime ();
72                 }
73 #endif
74
75                 [Test]
76                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
77                 public void StaticProperties ()
78                 {
79                         Assert.IsNull (HttpRuntime.AppDomainAppVirtualPath, "AppDomainAppVirtualPath");
80                         Assert.IsNotNull (HttpRuntime.Cache, "Cache");
81                 }
82
83                 [Test]
84                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
85                 [ExpectedException (typeof (SecurityException))]
86                 public void AppDomainAppPath_Deny ()
87                 {
88                         try {
89                                 Assert.IsNotNull (HttpRuntime.AppDomainAppPath, "AppDomainAppPath");
90                         }
91                         catch (ArgumentNullException) {
92                                 Assert.Ignore ("fails before the security check");
93                         }
94                 }
95
96                 [Test]
97                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
98                 [ExpectedException (typeof (SecurityException))]
99                 public void BinDirectory_Deny ()
100                 {
101                         try {
102                                 Assert.IsNotNull (HttpRuntime.BinDirectory, "BinDirectory");
103                         }
104                         catch (ArgumentException) {
105                                 Assert.Ignore ("fails before the security check");
106                         }
107                 }
108
109                 [Test]
110                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
111                 [ExpectedException (typeof (SecurityException))]
112                 public void CodegenDir_Deny ()
113                 {
114                         try {
115                                 Assert.IsNotNull (HttpRuntime.CodegenDir, "CodegenDir");
116                         }
117                         catch (ArgumentNullException) {
118                                 Assert.Ignore ("fails before the security check");
119                         }
120                 }
121
122                 [Test]
123                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.High)]
124                 [ExpectedException (typeof (SecurityException))]
125                 public void AppDomainAppId_Deny_High ()
126                 {
127                         Assert.IsNull (HttpRuntime.AppDomainAppId, "AppDomainAppId");
128                 }
129
130                 [Test]
131                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.High)]
132                 [ExpectedException (typeof (SecurityException))]
133                 public void AppDomainId_Deny_High ()
134                 {
135                         Assert.IsNull (HttpRuntime.AppDomainId, "AppDomainId");
136                 }
137
138                 [Test]
139                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.High)]
140                 public void PermitOnly_High ()
141                 {
142                         Assert.IsNull (HttpRuntime.AppDomainAppId, "AppDomainAppId");
143                         Assert.IsNull (HttpRuntime.AppDomainId, "AppDomainId");
144                 }
145
146                 [Test]
147                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Low)]
148                 [ExpectedException (typeof (SecurityException))]
149                 public void IsOnUNCShare_Deny_High ()
150                 {
151                         Assert.IsFalse (HttpRuntime.IsOnUNCShare, "IsOnUNCShare");
152                 }
153
154                 [Test]
155                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.High)]
156                 public void IsOnUNCShare_PermitOnly_High ()
157                 {
158                         try {
159                                 Assert.IsFalse (HttpRuntime.IsOnUNCShare, "IsOnUNCShare");
160                         }
161                         catch (NotImplementedException) {
162                                 // mono
163                         }
164                 }
165
166                 [Test]
167                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
168                 [ExpectedException (typeof (SecurityException))]
169                 public void AspInstallDirectory_Deny_FileIOPermission ()
170                 {
171                         if (HttpRuntime.AspInstallDirectory == null)
172                                 Assert.Ignore ("null isn't checked for FileIOPermission");
173                 }
174
175                 [Test]
176                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
177                 [ExpectedException (typeof (SecurityException))]
178                 public void ClrInstallDirectory_Deny_FileIOPermission ()
179                 {
180                         Assert.IsNotNull (HttpRuntime.ClrInstallDirectory, "ClrInstallDirectory");
181                 }
182
183                 [Test]
184                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
185                 [ExpectedException (typeof (SecurityException))]
186                 public void MachineConfigurationDirectory_Deny_FileIOPermission ()
187                 {
188                         Assert.IsNotNull (HttpRuntime.MachineConfigurationDirectory, "MachineConfigurationDirectory");
189                 }
190
191                 [Test]
192                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
193                 public void PermitOnly_PathDiscovery ()
194                 {
195                         string s = HttpRuntime.AspInstallDirectory; // null in unit tests for mono
196                         Assert.IsNotNull (HttpRuntime.ClrInstallDirectory, "ClrInstallDirectory");
197                         Assert.IsNotNull (HttpRuntime.MachineConfigurationDirectory, "MachineConfigurationDirectory");
198                 }
199
200                 [Test]
201                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
202                 [ExpectedException (typeof (SecurityException))]
203                 public void Close_Deny_Unmanaged ()
204                 {
205                         HttpRuntime.Close ();
206                 }
207
208                 [Test]
209                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
210                 public void Close_PermitOnly_Unmanaged ()
211                 {
212                         HttpRuntime.Close ();
213                 }
214
215                 [Test]
216                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
217                 [ExpectedException (typeof (SecurityException))]
218                 public void ProcessRequest_Deny_Medium ()
219                 {
220                         HttpRuntime.ProcessRequest (null);
221                 }
222
223                 [Test]
224                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
225                 [ExpectedException (typeof (ArgumentNullException))]
226                 public void ProcessRequest_PermitOnly_Medium ()
227                 {
228                         HttpRuntime.ProcessRequest (null);
229                 }
230
231                 [Test]
232                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
233                 [ExpectedException (typeof (SecurityException))]
234                 public void UnloadAppDomain_Deny_Unmanaged ()
235                 {
236                         HttpRuntime.UnloadAppDomain ();
237                 }
238
239                 [Test]
240                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
241                 public void UnloadAppDomain_PermitOnly_Unmanaged ()
242                 {
243                         HttpRuntime.UnloadAppDomain ();
244                 }
245
246                 // LinkDemand
247
248                 // note: the .ctor also has a LinkDemand for UnmanagedCode (which mess up the results)
249                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
250                 public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
251                 {
252                         ConstructorInfo ci = this.Type.GetConstructor (VoidType);
253                         Assert.IsNotNull (ci, "default .ctor");
254                         return ci.Invoke (null);
255                 }
256
257                 public override Type Type {
258                         get { return typeof (HttpRuntime); }
259                 }
260         }
261 }