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 Handler(object sender, EventArgs e)42 private void Handler (object sender, EventArgs e) 43 { 44 } 45 46 [Test] 47 [PermissionSet (SecurityAction.Deny, Unrestricted = true)] Properties_Deny_Unrestricted()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)] Events_Deny_Unrestricted()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 app.PostAuthenticateRequest += new EventHandler (Handler); 110 app.PostAuthorizeRequest += new EventHandler (Handler); 111 app.PostResolveRequestCache += new EventHandler (Handler); 112 app.PostMapRequestHandler += new EventHandler (Handler); 113 app.PostAcquireRequestState += new EventHandler (Handler); 114 app.PostReleaseRequestState += new EventHandler (Handler); 115 app.PostUpdateRequestCache += new EventHandler (Handler); 116 117 app.AddOnPostAuthenticateRequestAsync (null, null); 118 app.AddOnPostAuthenticateRequestAsync (null, null, null); 119 app.AddOnPostAuthorizeRequestAsync (null, null); 120 app.AddOnPostAuthorizeRequestAsync (null, null, null); 121 app.AddOnPostResolveRequestCacheAsync (null, null); 122 app.AddOnPostResolveRequestCacheAsync (null, null, null); 123 app.AddOnPostMapRequestHandlerAsync (null, null); 124 app.AddOnPostMapRequestHandlerAsync (null, null, null); 125 app.AddOnPostAcquireRequestStateAsync (null, null); 126 app.AddOnPostAcquireRequestStateAsync (null, null, null); 127 app.AddOnPostReleaseRequestStateAsync (null, null); 128 app.AddOnPostReleaseRequestStateAsync (null, null, null); 129 app.AddOnPostUpdateRequestCacheAsync (null, null); 130 app.AddOnPostUpdateRequestCacheAsync (null, null, null); 131 132 app.AddOnAcquireRequestStateAsync (null, null, null); 133 app.AddOnAuthenticateRequestAsync (null, null, null); 134 app.AddOnAuthorizeRequestAsync (null, null, null); 135 app.AddOnBeginRequestAsync (null, null, null); 136 app.AddOnEndRequestAsync (null, null, null); 137 app.AddOnPostRequestHandlerExecuteAsync (null, null, null); 138 app.AddOnPreRequestHandlerExecuteAsync (null, null, null); 139 app.AddOnReleaseRequestStateAsync (null, null, null); 140 app.AddOnResolveRequestCacheAsync (null, null, null); 141 app.AddOnUpdateRequestCacheAsync (null, null, null); 142 143 app.PostAuthenticateRequest -= new EventHandler (Handler); 144 app.PostAuthorizeRequest -= new EventHandler (Handler); 145 app.PostResolveRequestCache -= new EventHandler (Handler); 146 app.PostMapRequestHandler -= new EventHandler (Handler); 147 app.PostAcquireRequestState -= new EventHandler (Handler); 148 app.PostReleaseRequestState -= new EventHandler (Handler); 149 app.PostUpdateRequestCache -= new EventHandler (Handler); 150 } 151 152 [Test] 153 [PermissionSet (SecurityAction.Deny, Unrestricted = true)] Methods_Deny_Unrestricted()154 public void Methods_Deny_Unrestricted () 155 { 156 HttpApplication app = new HttpApplication (); 157 app.CompleteRequest (); 158 app.GetVaryByCustomString (null, String.Empty); 159 app.Dispose (); 160 } 161 162 [Test] 163 [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.High)] 164 [ExpectedException (typeof (SecurityException))] Modules_Deny_Unrestricted()165 public void Modules_Deny_Unrestricted () 166 { 167 HttpApplication app = new HttpApplication (); 168 Assert.IsNotNull (app.Modules, "Modules"); 169 } 170 171 [Test] 172 [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.High)] Modules_PermitOnly_High()173 public void Modules_PermitOnly_High () 174 { 175 HttpApplication app = new HttpApplication (); 176 Assert.IsNotNull (app.Modules, "Modules"); 177 } 178 179 [Test] 180 [PermissionSet (SecurityAction.Deny, Unrestricted = true)] Request_Deny_Unrestricted()181 public void Request_Deny_Unrestricted () 182 { 183 HttpApplication app = new HttpApplication (); 184 try { 185 Assert.IsNotNull (app.Request, "Request"); 186 } 187 catch (HttpException) { 188 // mono, ms 2.0 189 } 190 catch (TypeInitializationException) { 191 // ms 1.x 192 } 193 } 194 195 [Test] 196 [PermissionSet (SecurityAction.Deny, Unrestricted = true)] Response_Deny_Unrestricted()197 public void Response_Deny_Unrestricted () 198 { 199 HttpApplication app = new HttpApplication (); 200 try { 201 Assert.IsNotNull (app.Response, "Response"); 202 } 203 catch (HttpException) { 204 // mono, ms 2.0 205 } 206 catch (TypeInitializationException) { 207 // ms 1.x 208 } 209 } 210 211 [Test] 212 [PermissionSet (SecurityAction.Deny, Unrestricted = true)] Session_Deny_Unrestricted()213 public void Session_Deny_Unrestricted () 214 { 215 HttpApplication app = new HttpApplication (); 216 try { 217 Assert.IsNotNull (app.Session, "Session"); 218 } 219 catch (HttpException) { 220 // mono, ms 2.0 221 } 222 catch (TypeInitializationException) { 223 // ms 1.x 224 } 225 } 226 227 [Test] 228 [PermissionSet (SecurityAction.Deny, Unrestricted = true)] User_Deny_Unrestricted()229 public void User_Deny_Unrestricted () 230 { 231 HttpApplication app = new HttpApplication (); 232 try { 233 Assert.IsNull (app.User); 234 } 235 catch (HttpException) { 236 // mono, ms 2.0 237 } 238 catch (TypeInitializationException) { 239 // ms 1.x 240 } 241 } 242 243 // LinkDemand 244 245 public override Type Type { 246 get { return typeof (HttpApplication); } 247 } 248 } 249 } 250