2 // SecurityAttributeTest.cs - NUnit Test Cases for SecurityAttribute
\r
5 // Sebastien Pouliot <sebastien@ximian.com>
\r
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
\r
9 // Permission is hereby granted, free of charge, to any person obtaining
\r
10 // a copy of this software and associated documentation files (the
\r
11 // "Software"), to deal in the Software without restriction, including
\r
12 // without limitation the rights to use, copy, modify, merge, publish,
\r
13 // distribute, sublicense, and/or sell copies of the Software, and to
\r
14 // permit persons to whom the Software is furnished to do so, subject to
\r
15 // the following conditions:
\r
17 // The above copyright notice and this permission notice shall be
\r
18 // included in all copies or substantial portions of the Software.
\r
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
29 using NUnit.Framework;
\r
31 using System.Security;
\r
32 using System.Security.Permissions;
\r
34 namespace MonoTests.System.Security.Permissions {
\r
36 // note: SecurityAttribute is abstract so we define our own to test
\r
37 // no [Serialize] or [AttributeUsage] here to test their inheritance
\r
38 public class NonAbstractSecurityAttribute : SecurityAttribute
\r
41 public NonAbstractSecurityAttribute (SecurityAction action)
\r
46 public override IPermission CreatePermission ()
\r
53 public class SecurityAttributeTest {
\r
56 public void Action ()
\r
58 NonAbstractSecurityAttribute a = new NonAbstractSecurityAttribute (SecurityAction.Assert);
\r
59 Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
\r
60 a.Action = SecurityAction.Demand;
\r
61 Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
\r
62 a.Action = SecurityAction.Deny;
\r
63 Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
\r
64 a.Action = SecurityAction.InheritanceDemand;
\r
65 Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
\r
66 a.Action = SecurityAction.LinkDemand;
\r
67 Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
\r
68 a.Action = SecurityAction.PermitOnly;
\r
69 Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
\r
70 a.Action = SecurityAction.RequestMinimum;
\r
71 Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
\r
72 a.Action = SecurityAction.RequestOptional;
\r
73 Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
\r
74 a.Action = SecurityAction.RequestRefuse;
\r
75 Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
\r
77 a.Action = SecurityAction.DemandChoice;
\r
78 Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
\r
79 a.Action = SecurityAction.InheritanceDemandChoice;
\r
80 Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
\r
81 a.Action = SecurityAction.LinkDemandChoice;
\r
82 Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
\r
87 public void Action_Invalid ()
\r
89 NonAbstractSecurityAttribute a = new NonAbstractSecurityAttribute ((SecurityAction)Int32.MinValue);
\r
90 // no validation in attribute
\r
94 public void Unrestricted ()
\r
96 NonAbstractSecurityAttribute a = new NonAbstractSecurityAttribute (SecurityAction.Assert);
\r
97 Assert.IsFalse (a.Unrestricted, "Unrestricted (default)");
\r
98 a.Unrestricted = true;
\r
99 Assert.IsTrue (a.Unrestricted, "Unrestricted (true)");
\r
100 a.Unrestricted = false;
\r
101 Assert.IsFalse (a.Unrestricted, "Unrestricted (false)");
\r
105 public void Attributes ()
\r
107 Type t = typeof (NonAbstractSecurityAttribute);
\r
108 Assert.IsFalse (t.IsSerializable, "IsSerializable");
\r
110 object[] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
\r
111 Assert.AreEqual (0, attrs.Length, "AttributeUsage-false");
\r
113 attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), true);
\r
114 Assert.AreEqual (1, attrs.Length, "AttributeUsage-true");
\r
115 AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
\r
116 Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
\r
117 Assert.IsFalse (aua.Inherited, "Inherited");
\r
118 AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
\r
119 Assert.AreEqual (at, aua.ValidOn, "ValidOn");
\r