New test.
[mono.git] / mcs / class / System.Web / Test / System.Web / HttpApplicationCas.cs
1 //
2 // HttpApplicationCas.cs - CAS unit tests for System.Web.HttpApplication
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.Security;
33 using System.Security.Permissions;
34 using System.Web;
35
36 namespace MonoCasTests.System.Web {
37
38         [TestFixture]
39         [Category ("CAS")]
40         public class HttpApplicationCas : AspNetHostingMinimal {
41
42                 private void Handler (object sender, EventArgs e)
43                 {
44                 }
45
46                 [Test]
47                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
48                 public void Properties_Deny_Unrestricted ()
49                 {
50                         HttpApplication app = new HttpApplication ();
51                         // FIXME
52                         if (app.Application == null) {
53                                 // ms 1.x/2.0
54                                 Assert.IsNull (app.Application, "Application");
55                         } else {
56                                 // mono
57                                 Assert.IsNotNull (app.Application, "Application");
58                         }
59                         Assert.IsNull (app.Context, "Context");
60                         Assert.IsNotNull (app.Server, "Server");
61                         Assert.IsNull (app.Site, "Site");
62                 }
63
64                 [Test]
65                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
66                 public void Events_Deny_Unrestricted ()
67                 {
68                         HttpApplication app = new HttpApplication ();
69                         app.Disposed += new EventHandler (Handler);
70                         app.Error += new EventHandler (Handler);
71                         app.PreSendRequestContent += new EventHandler (Handler);
72                         app.PreSendRequestHeaders += new EventHandler (Handler);
73                         app.AcquireRequestState += new EventHandler (Handler);
74                         app.AuthenticateRequest += new EventHandler (Handler);
75                         app.AuthorizeRequest += new EventHandler (Handler);
76                         app.BeginRequest += new EventHandler (Handler);
77                         app.EndRequest += new EventHandler (Handler);
78                         app.PostRequestHandlerExecute += new EventHandler (Handler);
79                         app.PreRequestHandlerExecute += new EventHandler (Handler);
80                         app.ReleaseRequestState += new EventHandler (Handler);
81                         app.ResolveRequestCache += new EventHandler (Handler);
82                         app.UpdateRequestCache += new EventHandler (Handler);
83
84                         app.AddOnAcquireRequestStateAsync (null, null);
85                         app.AddOnAuthenticateRequestAsync (null, null);
86                         app.AddOnAuthorizeRequestAsync (null, null);
87                         app.AddOnBeginRequestAsync (null, null);
88                         app.AddOnEndRequestAsync (null, null);
89                         app.AddOnPostRequestHandlerExecuteAsync (null, null);
90                         app.AddOnPreRequestHandlerExecuteAsync (null, null);
91                         app.AddOnReleaseRequestStateAsync (null, null);
92                         app.AddOnResolveRequestCacheAsync (null, null);
93                         app.AddOnUpdateRequestCacheAsync (null, null);
94
95                         app.Disposed -= new EventHandler (Handler);
96                         app.Error -= new EventHandler (Handler);
97                         app.PreSendRequestContent -= new EventHandler (Handler);
98                         app.PreSendRequestHeaders -= new EventHandler (Handler);
99                         app.AcquireRequestState -= new EventHandler (Handler);
100                         app.AuthenticateRequest -= new EventHandler (Handler);
101                         app.AuthorizeRequest -= new EventHandler (Handler);
102                         app.BeginRequest -= new EventHandler (Handler);
103                         app.EndRequest -= new EventHandler (Handler);
104                         app.PostRequestHandlerExecute -= new EventHandler (Handler);
105                         app.PreRequestHandlerExecute -= new EventHandler (Handler);
106                         app.ReleaseRequestState -= new EventHandler (Handler);
107                         app.ResolveRequestCache -= new EventHandler (Handler);
108                         app.UpdateRequestCache -= new EventHandler (Handler);
109 #if NET_2_0
110                         app.PostAuthenticateRequest += new EventHandler (Handler);
111                         app.PostAuthorizeRequest += new EventHandler (Handler);
112                         app.PostResolveRequestCache += new EventHandler (Handler);
113                         app.PostMapRequestHandler += new EventHandler (Handler);
114                         app.PostAcquireRequestState += new EventHandler (Handler);
115                         app.PostReleaseRequestState += new EventHandler (Handler);
116                         app.PostUpdateRequestCache += new EventHandler (Handler);
117
118                         app.AddOnPostAuthenticateRequestAsync (null, null);
119                         app.AddOnPostAuthenticateRequestAsync (null, null, null);
120                         app.AddOnPostAuthorizeRequestAsync (null, null);
121                         app.AddOnPostAuthorizeRequestAsync (null, null, null);
122                         app.AddOnPostResolveRequestCacheAsync (null, null);
123                         app.AddOnPostResolveRequestCacheAsync (null, null, null);
124                         app.AddOnPostMapRequestHandlerAsync (null, null);
125                         app.AddOnPostMapRequestHandlerAsync (null, null, null);
126                         app.AddOnPostAcquireRequestStateAsync (null, null);
127                         app.AddOnPostAcquireRequestStateAsync (null, null, null);
128                         app.AddOnPostReleaseRequestStateAsync (null, null);
129                         app.AddOnPostReleaseRequestStateAsync (null, null, null);
130                         app.AddOnPostUpdateRequestCacheAsync (null, null);
131                         app.AddOnPostUpdateRequestCacheAsync (null, null, null);
132
133                         app.AddOnAcquireRequestStateAsync (null, null, null);
134                         app.AddOnAuthenticateRequestAsync (null, null, null);
135                         app.AddOnAuthorizeRequestAsync (null, null, null);
136                         app.AddOnBeginRequestAsync (null, null, null);
137                         app.AddOnEndRequestAsync (null, null, null);
138                         app.AddOnPostRequestHandlerExecuteAsync (null, null, null);
139                         app.AddOnPreRequestHandlerExecuteAsync (null, null, null);
140                         app.AddOnReleaseRequestStateAsync (null, null, null);
141                         app.AddOnResolveRequestCacheAsync (null, null, null);
142                         app.AddOnUpdateRequestCacheAsync (null, null, null);
143
144                         app.PostAuthenticateRequest -= new EventHandler (Handler);
145                         app.PostAuthorizeRequest -= new EventHandler (Handler);
146                         app.PostResolveRequestCache -= new EventHandler (Handler);
147                         app.PostMapRequestHandler -= new EventHandler (Handler);
148                         app.PostAcquireRequestState -= new EventHandler (Handler);
149                         app.PostReleaseRequestState -= new EventHandler (Handler);
150                         app.PostUpdateRequestCache -= new EventHandler (Handler);
151 #endif
152                 }
153
154                 [Test]
155                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
156                 public void Methods_Deny_Unrestricted ()
157                 {
158                         HttpApplication app = new HttpApplication ();
159                         app.CompleteRequest ();
160                         app.GetVaryByCustomString (null, String.Empty);
161                         app.Dispose ();
162                 }
163
164                 [Test]
165                 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.High)]
166                 [ExpectedException (typeof (SecurityException))]
167                 public void Modules_Deny_Unrestricted ()
168                 {
169                         HttpApplication app = new HttpApplication ();
170                         Assert.IsNotNull (app.Modules, "Modules");
171                 }
172
173                 [Test]
174                 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.High)]
175                 public void Modules_PermitOnly_High ()
176                 {
177                         HttpApplication app = new HttpApplication ();
178                         Assert.IsNotNull (app.Modules, "Modules");
179                 }
180
181                 [Test]
182                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
183                 public void Request_Deny_Unrestricted ()
184                 {
185                         HttpApplication app = new HttpApplication ();
186                         try {
187                                 Assert.IsNotNull (app.Request, "Request");
188                         }
189                         catch (HttpException) {
190                                 // mono, ms 2.0
191                         }
192                         catch (TypeInitializationException) {
193                                 // ms 1.x
194                         }
195                 }
196
197                 [Test]
198                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
199                 public void Response_Deny_Unrestricted ()
200                 {
201                         HttpApplication app = new HttpApplication ();
202                         try {
203                                 Assert.IsNotNull (app.Response, "Response");
204                         }
205                         catch (HttpException) {
206                                 // mono, ms 2.0
207                         }
208                         catch (TypeInitializationException) {
209                                 // ms 1.x
210                         }
211                 }
212
213                 [Test]
214                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
215                 public void Session_Deny_Unrestricted ()
216                 {
217                         HttpApplication app = new HttpApplication ();
218                         try {
219                                 Assert.IsNotNull (app.Session, "Session");
220                         }
221                         catch (HttpException) {
222                                 // mono, ms 2.0
223                         }
224                         catch (TypeInitializationException) {
225                                 // ms 1.x
226                         }
227                 }
228
229                 [Test]
230                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
231                 public void User_Deny_Unrestricted ()
232                 {
233                         HttpApplication app = new HttpApplication ();
234                         try {
235                                 Assert.IsNull (app.User);
236                         }
237                         catch (HttpException) {
238                                 // mono, ms 2.0
239                         }
240                         catch (TypeInitializationException) {
241                                 // ms 1.x
242                         }
243                 }
244
245                 // LinkDemand
246
247                 public override Type Type {
248                         get { return typeof (HttpApplication); }
249                 }
250         }
251 }