1 // 2 // HostProtectionAttributeTest.cs - NUnit Test Cases for HostProtectionAttribute 3 // 4 // Author: 5 // Sebastien Pouliot <sebastien@ximian.com> 6 // 7 // Copyright (C) 2004 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 30 using NUnit.Framework; 31 using System; 32 using System.Security; 33 using System.Security.Permissions; 34 35 namespace MonoTests.System.Security.Permissions { 36 37 [TestFixture] 38 #if MOBILE 39 [Ignore] 40 #endif 41 public class HostProtectionAttributeTest { 42 DefaultTests(HostProtectionAttribute hpa)43 private void DefaultTests (HostProtectionAttribute hpa) 44 { 45 Assert.AreEqual (SecurityAction.LinkDemand, hpa.Action, "Action"); 46 Assert.AreEqual (HostProtectionResource.None, hpa.Resources, "Resources"); 47 Assert.IsFalse (hpa.ExternalProcessMgmt, "ExternalProcessMgmt"); 48 Assert.IsFalse (hpa.ExternalThreading, "ExternalThreading"); 49 Assert.IsFalse (hpa.MayLeakOnAbort, "MayLeakOnAbort"); 50 Assert.IsFalse (hpa.SecurityInfrastructure, "SecurityInfrastructure"); 51 Assert.IsFalse (hpa.SelfAffectingProcessMgmt, "SelfAffectingProcessMgmt"); 52 Assert.IsFalse (hpa.SelfAffectingThreading, "SelfAffectingThreading"); 53 Assert.IsFalse (hpa.SharedState, "SharedState"); 54 Assert.IsFalse (hpa.Synchronization, "Synchronization"); 55 Assert.IsFalse (hpa.UI, "UI"); 56 Assert.IsFalse (hpa.Unrestricted, "Unrestricted"); 57 IPermission p = hpa.CreatePermission (); 58 Assert.AreEqual ("System.Security.Permissions.HostProtectionPermission", p.GetType ().ToString (), "CreatePermission"); 59 Assert.IsTrue ((p is IUnrestrictedPermission), "IUnrestrictedPermission"); 60 } 61 62 [Test] 63 #if MOBILE 64 [Ignore] 65 #endif HostProtectionAttribute_Empty()66 public void HostProtectionAttribute_Empty () 67 { 68 // note: normally security attributes don't have an empty constructor 69 HostProtectionAttribute hpa = new HostProtectionAttribute (); 70 DefaultTests (hpa); 71 } 72 73 [Test] 74 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_Assert()75 public void HostProtectionAttribute_Assert () 76 { 77 new HostProtectionAttribute (SecurityAction.Assert); 78 } 79 80 [Test] 81 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_Demand()82 public void HostProtectionAttribute_Demand () 83 { 84 new HostProtectionAttribute (SecurityAction.Demand); 85 } 86 87 [Test] 88 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_Deny()89 public void HostProtectionAttribute_Deny () 90 { 91 new HostProtectionAttribute (SecurityAction.Deny); 92 } 93 94 [Test] 95 #if MOBILE 96 [Ignore] 97 #endif 98 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_InheritanceDemand()99 public void HostProtectionAttribute_InheritanceDemand () 100 { 101 new HostProtectionAttribute (SecurityAction.InheritanceDemand); 102 } 103 104 [Test] HostProtectionAttribute_LinkDemand()105 public void HostProtectionAttribute_LinkDemand () 106 { 107 HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand); 108 DefaultTests (hpa); 109 } 110 111 [Test] 112 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_PermitOnly()113 public void HostProtectionAttribute_PermitOnly () 114 { 115 new HostProtectionAttribute (SecurityAction.PermitOnly); 116 } 117 118 [Test] 119 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_RequestMinimum()120 public void HostProtectionAttribute_RequestMinimum () 121 { 122 new HostProtectionAttribute (SecurityAction.RequestMinimum); 123 } 124 125 [Test] 126 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_RequestOptional()127 public void HostProtectionAttribute_RequestOptional () 128 { 129 new HostProtectionAttribute (SecurityAction.RequestOptional); 130 } 131 132 [Test] 133 [ExpectedException (typeof (ArgumentException))] HostProtectionAttribute_RequestRefuse()134 public void HostProtectionAttribute_RequestRefuse () 135 { 136 new HostProtectionAttribute (SecurityAction.RequestRefuse); 137 } 138 Empty()139 private HostProtectionAttribute Empty () 140 { 141 HostProtectionAttribute a = new HostProtectionAttribute (); 142 a.Synchronization = false; 143 a.SharedState = false; 144 a.ExternalProcessMgmt = false; 145 a.SelfAffectingProcessMgmt = false; 146 a.ExternalThreading = false; 147 a.SelfAffectingThreading = false; 148 a.SecurityInfrastructure = false; 149 a.UI = false; 150 a.MayLeakOnAbort = false; 151 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 152 return a; 153 } 154 155 [Test] Synchronization()156 public void Synchronization () 157 { 158 HostProtectionAttribute a = Empty (); 159 a.Synchronization = true; 160 Assert.AreEqual (HostProtectionResource.Synchronization, a.Resources, "Resources=Synchronization"); 161 a.Synchronization = false; 162 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 163 } 164 165 [Test] SharedState()166 public void SharedState () 167 { 168 HostProtectionAttribute a = Empty (); 169 a.SharedState = true; 170 Assert.AreEqual (HostProtectionResource.SharedState, a.Resources, "Resources=SharedState"); 171 a.SharedState = false; 172 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 173 } 174 175 [Test] ExternalProcessMgmt()176 public void ExternalProcessMgmt () 177 { 178 HostProtectionAttribute a = Empty (); 179 a.ExternalProcessMgmt = true; 180 Assert.AreEqual (HostProtectionResource.ExternalProcessMgmt, a.Resources, "Resources=ExternalProcessMgmt"); 181 a.ExternalProcessMgmt = false; 182 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 183 } 184 185 [Test] SelfAffectingProcessMgmt()186 public void SelfAffectingProcessMgmt () 187 { 188 HostProtectionAttribute a = Empty (); 189 a.SelfAffectingProcessMgmt = true; 190 Assert.AreEqual (HostProtectionResource.SelfAffectingProcessMgmt, a.Resources, "Resources=SelfAffectingProcessMgmt"); 191 a.SelfAffectingProcessMgmt = false; 192 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 193 } 194 195 [Test] ExternalThreading()196 public void ExternalThreading () 197 { 198 HostProtectionAttribute a = Empty (); 199 a.ExternalThreading = true; 200 Assert.AreEqual (HostProtectionResource.ExternalThreading, a.Resources, "Resources=ExternalThreading"); 201 a.ExternalThreading = false; 202 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 203 } 204 205 [Test] SelfAffectingThreading()206 public void SelfAffectingThreading () 207 { 208 HostProtectionAttribute a = Empty (); 209 a.SelfAffectingThreading = true; 210 Assert.AreEqual (HostProtectionResource.SelfAffectingThreading, a.Resources, "Resources=SelfAffectingThreading"); 211 a.SelfAffectingThreading = false; 212 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 213 } 214 215 [Test] SecurityInfrastructure()216 public void SecurityInfrastructure () 217 { 218 HostProtectionAttribute a = Empty (); 219 a.SecurityInfrastructure = true; 220 Assert.AreEqual (HostProtectionResource.SecurityInfrastructure, a.Resources, "Resources=SecurityInfrastructure"); 221 a.SecurityInfrastructure = false; 222 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 223 } 224 225 [Test] UI()226 public void UI () 227 { 228 HostProtectionAttribute a = Empty (); 229 a.UI = true; 230 Assert.AreEqual (HostProtectionResource.UI, a.Resources, "Resources=UI"); 231 a.UI = false; 232 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 233 } 234 235 [Test] MayLeakOnAbort()236 public void MayLeakOnAbort () 237 { 238 HostProtectionAttribute a = Empty (); 239 a.MayLeakOnAbort = true; 240 Assert.AreEqual (HostProtectionResource.MayLeakOnAbort, a.Resources, "Resources=MayLeakOnAbort"); 241 a.MayLeakOnAbort = false; 242 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None"); 243 } 244 245 [Test] Properties()246 public void Properties () 247 { 248 HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand); 249 HostProtectionResource expected = HostProtectionResource.None; 250 Assert.AreEqual (expected, hpa.Resources, "None"); 251 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-1"); 252 253 hpa.ExternalProcessMgmt = true; 254 expected |= HostProtectionResource.ExternalProcessMgmt; 255 Assert.AreEqual (expected, hpa.Resources, "+ExternalProcessMgmt"); 256 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-2"); 257 258 hpa.ExternalThreading = true; 259 expected |= HostProtectionResource.ExternalThreading; 260 Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading"); 261 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-3"); 262 263 hpa.MayLeakOnAbort = true; 264 expected |= HostProtectionResource.MayLeakOnAbort; 265 Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort"); 266 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-4"); 267 268 hpa.SecurityInfrastructure = true; 269 expected |= HostProtectionResource.SecurityInfrastructure; 270 Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure"); 271 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-5"); 272 273 hpa.SelfAffectingProcessMgmt = true; 274 expected |= HostProtectionResource.SelfAffectingProcessMgmt; 275 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt"); 276 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-6"); 277 278 hpa.SelfAffectingThreading = true; 279 expected |= HostProtectionResource.SelfAffectingThreading; 280 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading"); 281 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-7"); 282 283 hpa.SharedState = true; 284 expected |= HostProtectionResource.SharedState; 285 Assert.AreEqual (expected, hpa.Resources, "+SharedState"); 286 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-8"); 287 288 hpa.Synchronization = true; 289 expected |= HostProtectionResource.Synchronization; 290 Assert.AreEqual (expected, hpa.Resources, "+Synchronization"); 291 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-9"); 292 293 hpa.UI = true; 294 expected |= HostProtectionResource.UI; 295 Assert.AreEqual (expected, hpa.Resources, "+UI"); 296 297 Assert.IsFalse (hpa.Unrestricted, "Unrestricted"); 298 299 hpa.ExternalProcessMgmt = false; 300 expected &= ~HostProtectionResource.ExternalProcessMgmt; 301 Assert.AreEqual (expected, hpa.Resources, "-ExternalProcessMgmt"); 302 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-10"); 303 304 hpa.ExternalThreading = false; 305 expected &= ~HostProtectionResource.ExternalThreading; 306 Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading"); 307 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-11"); 308 309 hpa.MayLeakOnAbort = false; 310 expected &= ~HostProtectionResource.MayLeakOnAbort; 311 Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort"); 312 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-12"); 313 314 hpa.SecurityInfrastructure = false; 315 expected &= ~HostProtectionResource.SecurityInfrastructure; 316 Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure"); 317 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-13"); 318 319 hpa.SelfAffectingProcessMgmt = false; 320 expected &= ~HostProtectionResource.SelfAffectingProcessMgmt; 321 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt"); 322 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-14"); 323 324 hpa.SelfAffectingThreading = false; 325 expected &= ~HostProtectionResource.SelfAffectingThreading; 326 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading"); 327 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-15"); 328 329 hpa.SharedState = false; 330 expected &= ~HostProtectionResource.SharedState; 331 Assert.AreEqual (expected, hpa.Resources, "+SharedState"); 332 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-16"); 333 334 hpa.Synchronization = false; 335 expected &= ~HostProtectionResource.Synchronization; 336 Assert.AreEqual (expected, hpa.Resources, "+Synchronization"); 337 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-17"); 338 339 hpa.UI = false; 340 expected &= ~HostProtectionResource.UI; 341 Assert.AreEqual (expected, hpa.Resources, "+UI"); 342 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-18"); 343 } 344 345 [Test] Attributes()346 public void Attributes () 347 { 348 Type t = typeof (HostProtectionAttribute); 349 Assert.IsTrue (t.IsSerializable, "IsSerializable"); 350 351 object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false); 352 Assert.AreEqual (1, attrs.Length, "AttributeUsage"); 353 AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0]; 354 Assert.IsTrue (aua.AllowMultiple, "AllowMultiple"); 355 Assert.IsFalse (aua.Inherited, "Inherited"); 356 AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Delegate); 357 Assert.AreEqual (at, aua.ValidOn, "ValidOn"); 358 } 359 } 360 } 361 362