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.
31 using NUnit.Framework;
33 using System.Security;
34 using System.Security.Permissions;
36 namespace MonoTests.System.Security.Permissions {
39 public class HostProtectionAttributeTest {
41 private void DefaultTests (HostProtectionAttribute hpa)
43 Assert.AreEqual (SecurityAction.LinkDemand, hpa.Action, "Action");
44 Assert.AreEqual (HostProtectionResource.None, hpa.Resources, "Resources");
45 Assert.IsFalse (hpa.ExternalProcessMgmt, "ExternalProcessMgmt");
46 Assert.IsFalse (hpa.ExternalThreading, "ExternalThreading");
47 Assert.IsFalse (hpa.MayLeakOnAbort, "MayLeakOnAbort");
48 Assert.IsFalse (hpa.SecurityInfrastructure, "SecurityInfrastructure");
49 Assert.IsFalse (hpa.SelfAffectingProcessMgmt, "SelfAffectingProcessMgmt");
50 Assert.IsFalse (hpa.SelfAffectingThreading, "SelfAffectingThreading");
51 Assert.IsFalse (hpa.SharedState, "SharedState");
52 Assert.IsFalse (hpa.Synchronization, "Synchronization");
53 Assert.IsFalse (hpa.UI, "UI");
54 Assert.IsFalse (hpa.Unrestricted, "Unrestricted");
55 IPermission p = hpa.CreatePermission ();
56 Assert.AreEqual ("System.Security.Permissions.HostProtectionPermission", p.GetType ().ToString (), "CreatePermission");
57 Assert.IsTrue ((p is IUnrestrictedPermission), "IUnrestrictedPermission");
61 public void HostProtectionAttribute_Empty ()
63 // note: normally security attributes don't have an empty constructor
64 HostProtectionAttribute hpa = new HostProtectionAttribute ();
69 [ExpectedException (typeof (ArgumentException))]
70 public void HostProtectionAttribute_Assert ()
72 new HostProtectionAttribute (SecurityAction.Assert);
76 [ExpectedException (typeof (ArgumentException))]
77 public void HostProtectionAttribute_Demand ()
79 new HostProtectionAttribute (SecurityAction.Demand);
83 [ExpectedException (typeof (ArgumentException))]
84 public void HostProtectionAttribute_Deny ()
86 new HostProtectionAttribute (SecurityAction.Deny);
90 [ExpectedException (typeof (ArgumentException))]
91 public void HostProtectionAttribute_InheritanceDemand ()
93 new HostProtectionAttribute (SecurityAction.InheritanceDemand);
97 public void HostProtectionAttribute_LinkDemand ()
99 HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
104 [ExpectedException (typeof (ArgumentException))]
105 public void HostProtectionAttribute_PermitOnly ()
107 new HostProtectionAttribute (SecurityAction.PermitOnly);
111 [ExpectedException (typeof (ArgumentException))]
112 public void HostProtectionAttribute_RequestMinimum ()
114 new HostProtectionAttribute (SecurityAction.RequestMinimum);
118 [ExpectedException (typeof (ArgumentException))]
119 public void HostProtectionAttribute_RequestOptional ()
121 new HostProtectionAttribute (SecurityAction.RequestOptional);
125 [ExpectedException (typeof (ArgumentException))]
126 public void HostProtectionAttribute_RequestRefuse ()
128 new HostProtectionAttribute (SecurityAction.RequestRefuse);
131 private HostProtectionAttribute Empty ()
133 HostProtectionAttribute a = new HostProtectionAttribute ();
134 a.Synchronization = false;
135 a.SharedState = false;
136 a.ExternalProcessMgmt = false;
137 a.SelfAffectingProcessMgmt = false;
138 a.ExternalThreading = false;
139 a.SelfAffectingThreading = false;
140 a.SecurityInfrastructure = false;
142 a.MayLeakOnAbort = false;
143 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
148 public void Synchronization ()
150 HostProtectionAttribute a = Empty ();
151 a.Synchronization = true;
152 Assert.AreEqual (HostProtectionResource.Synchronization, a.Resources, "Resources=Synchronization");
153 a.Synchronization = false;
154 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
158 public void SharedState ()
160 HostProtectionAttribute a = Empty ();
161 a.SharedState = true;
162 Assert.AreEqual (HostProtectionResource.SharedState, a.Resources, "Resources=SharedState");
163 a.SharedState = false;
164 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
168 public void ExternalProcessMgmt ()
170 HostProtectionAttribute a = Empty ();
171 a.ExternalProcessMgmt = true;
172 Assert.AreEqual (HostProtectionResource.ExternalProcessMgmt, a.Resources, "Resources=ExternalProcessMgmt");
173 a.ExternalProcessMgmt = false;
174 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
178 public void SelfAffectingProcessMgmt ()
180 HostProtectionAttribute a = Empty ();
181 a.SelfAffectingProcessMgmt = true;
182 Assert.AreEqual (HostProtectionResource.SelfAffectingProcessMgmt, a.Resources, "Resources=SelfAffectingProcessMgmt");
183 a.SelfAffectingProcessMgmt = false;
184 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
188 public void ExternalThreading ()
190 HostProtectionAttribute a = Empty ();
191 a.ExternalThreading = true;
192 Assert.AreEqual (HostProtectionResource.ExternalThreading, a.Resources, "Resources=ExternalThreading");
193 a.ExternalThreading = false;
194 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
198 public void SelfAffectingThreading ()
200 HostProtectionAttribute a = Empty ();
201 a.SelfAffectingThreading = true;
202 Assert.AreEqual (HostProtectionResource.SelfAffectingThreading, a.Resources, "Resources=SelfAffectingThreading");
203 a.SelfAffectingThreading = false;
204 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
208 public void SecurityInfrastructure ()
210 HostProtectionAttribute a = Empty ();
211 a.SecurityInfrastructure = true;
212 Assert.AreEqual (HostProtectionResource.SecurityInfrastructure, a.Resources, "Resources=SecurityInfrastructure");
213 a.SecurityInfrastructure = false;
214 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
220 HostProtectionAttribute a = Empty ();
222 Assert.AreEqual (HostProtectionResource.UI, a.Resources, "Resources=UI");
224 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
228 public void MayLeakOnAbort ()
230 HostProtectionAttribute a = Empty ();
231 a.MayLeakOnAbort = true;
232 Assert.AreEqual (HostProtectionResource.MayLeakOnAbort, a.Resources, "Resources=MayLeakOnAbort");
233 a.MayLeakOnAbort = false;
234 Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
238 public void Properties ()
240 HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
241 HostProtectionResource expected = HostProtectionResource.None;
242 Assert.AreEqual (expected, hpa.Resources, "None");
243 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-1");
245 hpa.ExternalProcessMgmt = true;
246 expected |= HostProtectionResource.ExternalProcessMgmt;
247 Assert.AreEqual (expected, hpa.Resources, "+ExternalProcessMgmt");
248 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-2");
250 hpa.ExternalThreading = true;
251 expected |= HostProtectionResource.ExternalThreading;
252 Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
253 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-3");
255 hpa.MayLeakOnAbort = true;
256 expected |= HostProtectionResource.MayLeakOnAbort;
257 Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
258 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-4");
260 hpa.SecurityInfrastructure = true;
261 expected |= HostProtectionResource.SecurityInfrastructure;
262 Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
263 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-5");
265 hpa.SelfAffectingProcessMgmt = true;
266 expected |= HostProtectionResource.SelfAffectingProcessMgmt;
267 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
268 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-6");
270 hpa.SelfAffectingThreading = true;
271 expected |= HostProtectionResource.SelfAffectingThreading;
272 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
273 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-7");
275 hpa.SharedState = true;
276 expected |= HostProtectionResource.SharedState;
277 Assert.AreEqual (expected, hpa.Resources, "+SharedState");
278 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-8");
280 hpa.Synchronization = true;
281 expected |= HostProtectionResource.Synchronization;
282 Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
283 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-9");
286 expected |= HostProtectionResource.UI;
287 Assert.AreEqual (expected, hpa.Resources, "+UI");
289 Assert.IsFalse (hpa.Unrestricted, "Unrestricted");
291 hpa.ExternalProcessMgmt = false;
292 expected &= ~HostProtectionResource.ExternalProcessMgmt;
293 Assert.AreEqual (expected, hpa.Resources, "-ExternalProcessMgmt");
294 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-10");
296 hpa.ExternalThreading = false;
297 expected &= ~HostProtectionResource.ExternalThreading;
298 Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
299 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-11");
301 hpa.MayLeakOnAbort = false;
302 expected &= ~HostProtectionResource.MayLeakOnAbort;
303 Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
304 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-12");
306 hpa.SecurityInfrastructure = false;
307 expected &= ~HostProtectionResource.SecurityInfrastructure;
308 Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
309 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-13");
311 hpa.SelfAffectingProcessMgmt = false;
312 expected &= ~HostProtectionResource.SelfAffectingProcessMgmt;
313 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
314 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-14");
316 hpa.SelfAffectingThreading = false;
317 expected &= ~HostProtectionResource.SelfAffectingThreading;
318 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
319 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-15");
321 hpa.SharedState = false;
322 expected &= ~HostProtectionResource.SharedState;
323 Assert.AreEqual (expected, hpa.Resources, "+SharedState");
324 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-16");
326 hpa.Synchronization = false;
327 expected &= ~HostProtectionResource.Synchronization;
328 Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
329 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-17");
332 expected &= ~HostProtectionResource.UI;
333 Assert.AreEqual (expected, hpa.Resources, "+UI");
334 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-18");
338 public void Attributes ()
340 Type t = typeof (HostProtectionAttribute);
341 Assert.IsTrue (t.IsSerializable, "IsSerializable");
343 object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
344 Assert.AreEqual (1, attrs.Length, "AttributeUsage");
345 AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
346 Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
347 Assert.IsFalse (aua.Inherited, "Inherited");
348 AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Delegate);
349 Assert.AreEqual (at, aua.ValidOn, "ValidOn");