2 // HostProtectionAttributeTest.cs - NUnit Test Cases for HostProtectionAttribute
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoTests.System.Security.Permissions {
41 public class HostProtectionAttributeTest {
43 private void DefaultTests (HostProtectionAttribute hpa)
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");
66 public void HostProtectionAttribute_Empty ()
68 // note: normally security attributes don't have an empty constructor
69 HostProtectionAttribute hpa = new HostProtectionAttribute ();
74 [ExpectedException (typeof (ArgumentException))]
75 public void HostProtectionAttribute_Assert ()
77 new HostProtectionAttribute (SecurityAction.Assert);
81 [ExpectedException (typeof (ArgumentException))]
82 public void HostProtectionAttribute_Demand ()
84 new HostProtectionAttribute (SecurityAction.Demand);
88 [ExpectedException (typeof (ArgumentException))]
89 public void HostProtectionAttribute_Deny ()
91 new HostProtectionAttribute (SecurityAction.Deny);
98 [ExpectedException (typeof (ArgumentException))]
99 public void HostProtectionAttribute_InheritanceDemand ()
101 new HostProtectionAttribute (SecurityAction.InheritanceDemand);
105 public void HostProtectionAttribute_LinkDemand ()
107 HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
112 [ExpectedException (typeof (ArgumentException))]
113 public void HostProtectionAttribute_PermitOnly ()
115 new HostProtectionAttribute (SecurityAction.PermitOnly);
119 [ExpectedException (typeof (ArgumentException))]
120 public void HostProtectionAttribute_RequestMinimum ()
122 new HostProtectionAttribute (SecurityAction.RequestMinimum);
126 [ExpectedException (typeof (ArgumentException))]
127 public void HostProtectionAttribute_RequestOptional ()
129 new HostProtectionAttribute (SecurityAction.RequestOptional);
133 [ExpectedException (typeof (ArgumentException))]
134 public void HostProtectionAttribute_RequestRefuse ()
136 new HostProtectionAttribute (SecurityAction.RequestRefuse);
139 private HostProtectionAttribute Empty ()
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;
150 a.MayLeakOnAbort = false;
151 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
156 public void Synchronization ()
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");
166 public void SharedState ()
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");
176 public void ExternalProcessMgmt ()
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");
186 public void SelfAffectingProcessMgmt ()
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");
196 public void ExternalThreading ()
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");
206 public void SelfAffectingThreading ()
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");
216 public void SecurityInfrastructure ()
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");
228 HostProtectionAttribute a = Empty ();
230 Assert.AreEqual (HostProtectionResource.UI, a.Resources, "Resources=UI");
232 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
236 public void MayLeakOnAbort ()
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");
246 public void Properties ()
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");
253 hpa.ExternalProcessMgmt = true;
254 expected |= HostProtectionResource.ExternalProcessMgmt;
255 Assert.AreEqual (expected, hpa.Resources, "+ExternalProcessMgmt");
256 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-2");
258 hpa.ExternalThreading = true;
259 expected |= HostProtectionResource.ExternalThreading;
260 Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
261 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-3");
263 hpa.MayLeakOnAbort = true;
264 expected |= HostProtectionResource.MayLeakOnAbort;
265 Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
266 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-4");
268 hpa.SecurityInfrastructure = true;
269 expected |= HostProtectionResource.SecurityInfrastructure;
270 Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
271 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-5");
273 hpa.SelfAffectingProcessMgmt = true;
274 expected |= HostProtectionResource.SelfAffectingProcessMgmt;
275 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
276 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-6");
278 hpa.SelfAffectingThreading = true;
279 expected |= HostProtectionResource.SelfAffectingThreading;
280 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
281 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-7");
283 hpa.SharedState = true;
284 expected |= HostProtectionResource.SharedState;
285 Assert.AreEqual (expected, hpa.Resources, "+SharedState");
286 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-8");
288 hpa.Synchronization = true;
289 expected |= HostProtectionResource.Synchronization;
290 Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
291 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-9");
294 expected |= HostProtectionResource.UI;
295 Assert.AreEqual (expected, hpa.Resources, "+UI");
297 Assert.IsFalse (hpa.Unrestricted, "Unrestricted");
299 hpa.ExternalProcessMgmt = false;
300 expected &= ~HostProtectionResource.ExternalProcessMgmt;
301 Assert.AreEqual (expected, hpa.Resources, "-ExternalProcessMgmt");
302 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-10");
304 hpa.ExternalThreading = false;
305 expected &= ~HostProtectionResource.ExternalThreading;
306 Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
307 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-11");
309 hpa.MayLeakOnAbort = false;
310 expected &= ~HostProtectionResource.MayLeakOnAbort;
311 Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
312 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-12");
314 hpa.SecurityInfrastructure = false;
315 expected &= ~HostProtectionResource.SecurityInfrastructure;
316 Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
317 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-13");
319 hpa.SelfAffectingProcessMgmt = false;
320 expected &= ~HostProtectionResource.SelfAffectingProcessMgmt;
321 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
322 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-14");
324 hpa.SelfAffectingThreading = false;
325 expected &= ~HostProtectionResource.SelfAffectingThreading;
326 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
327 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-15");
329 hpa.SharedState = false;
330 expected &= ~HostProtectionResource.SharedState;
331 Assert.AreEqual (expected, hpa.Resources, "+SharedState");
332 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-16");
334 hpa.Synchronization = false;
335 expected &= ~HostProtectionResource.Synchronization;
336 Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
337 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-17");
340 expected &= ~HostProtectionResource.UI;
341 Assert.AreEqual (expected, hpa.Resources, "+UI");
342 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-18");
346 public void Attributes ()
348 Type t = typeof (HostProtectionAttribute);
349 Assert.IsTrue (t.IsSerializable, "IsSerializable");
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");