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_DemandChoice ()
86 new HostProtectionAttribute (SecurityAction.DemandChoice);
90 [ExpectedException (typeof (ArgumentException))]
91 public void HostProtectionAttribute_Deny ()
93 new HostProtectionAttribute (SecurityAction.Deny);
97 [ExpectedException (typeof (ArgumentException))]
98 public void HostProtectionAttribute_InheritanceDemand ()
100 new HostProtectionAttribute (SecurityAction.InheritanceDemand);
104 [ExpectedException (typeof (ArgumentException))]
105 public void HostProtectionAttribute_InheritanceDemandChoice ()
107 new HostProtectionAttribute (SecurityAction.InheritanceDemandChoice);
111 public void HostProtectionAttribute_LinkDemand ()
113 HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
118 [ExpectedException (typeof (ArgumentException))]
119 public void HostProtectionAttribute_LinkDemandChoice ()
121 new HostProtectionAttribute (SecurityAction.LinkDemandChoice);
125 [ExpectedException (typeof (ArgumentException))]
126 public void HostProtectionAttribute_PermitOnly ()
128 new HostProtectionAttribute (SecurityAction.PermitOnly);
132 [ExpectedException (typeof (ArgumentException))]
133 public void HostProtectionAttribute_RequestMinimum ()
135 new HostProtectionAttribute (SecurityAction.RequestMinimum);
139 [ExpectedException (typeof (ArgumentException))]
140 public void HostProtectionAttribute_RequestOptional ()
142 new HostProtectionAttribute (SecurityAction.RequestOptional);
146 [ExpectedException (typeof (ArgumentException))]
147 public void HostProtectionAttribute_RequestRefuse ()
149 new HostProtectionAttribute (SecurityAction.RequestRefuse);
153 public void Properties ()
155 HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
156 HostProtectionResource expected = HostProtectionResource.None;
157 Assert.AreEqual (expected, hpa.Resources, "None");
158 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-1");
160 hpa.ExternalProcessMgmt = true;
161 expected |= HostProtectionResource.ExternalProcessMgmt;
162 Assert.AreEqual (expected, hpa.Resources, "+ExternalProcessMgmt");
163 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-2");
165 hpa.ExternalThreading = true;
166 expected |= HostProtectionResource.ExternalThreading;
167 Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
168 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-3");
170 hpa.MayLeakOnAbort = true;
171 expected |= HostProtectionResource.MayLeakOnAbort;
172 Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
173 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-4");
175 hpa.SecurityInfrastructure = true;
176 expected |= HostProtectionResource.SecurityInfrastructure;
177 Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
178 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-5");
180 hpa.SelfAffectingProcessMgmt = true;
181 expected |= HostProtectionResource.SelfAffectingProcessMgmt;
182 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
183 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-6");
185 hpa.SelfAffectingThreading = true;
186 expected |= HostProtectionResource.SelfAffectingThreading;
187 Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
188 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-7");
190 hpa.SharedState = true;
191 expected |= HostProtectionResource.SharedState;
192 Assert.AreEqual (expected, hpa.Resources, "+SharedState");
193 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-8");
195 hpa.Synchronization = true;
196 expected |= HostProtectionResource.Synchronization;
197 Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
198 Assert.IsFalse (hpa.Unrestricted, "Unrestricted-9");
201 expected |= HostProtectionResource.UI;
202 Assert.AreEqual (expected, hpa.Resources, "+UI");
204 Assert.IsFalse (hpa.Unrestricted, "Unrestricted");
208 public void Attributes ()
210 Type t = typeof (HostProtectionAttribute);
211 Assert.IsTrue (t.IsSerializable, "IsSerializable");
213 object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
214 Assert.AreEqual (1, attrs.Length, "AttributeUsage");
215 AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
216 Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
217 Assert.IsFalse (aua.Inherited, "Inherited");
218 AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Delegate);
219 Assert.AreEqual (at, aua.ValidOn, "ValidOn");