Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / SecurityAttributeTest.cs
1 //
2 // SecurityAttributeTest.cs - NUnit Test Cases for SecurityAttribute
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 using NUnit.Framework;
30 using System;
31 using System.Security;
32 using System.Security.Permissions;
33
34 namespace MonoTests.System.Security.Permissions {
35
36         // note: SecurityAttribute is abstract so we define our own to test
37         // no [Serialize] or [AttributeUsage] here to test their inheritance
38         public class NonAbstractSecurityAttribute : SecurityAttribute
39         {
40
41                 public NonAbstractSecurityAttribute (SecurityAction action)
42                         : base (action)
43                 {
44                 }
45
46                 public override IPermission CreatePermission ()
47                 {
48                         return null;
49                 }
50         }
51
52         [TestFixture]
53         public class SecurityAttributeTest {
54
55                 [Test]
56                 public void Action ()
57                 {
58                         NonAbstractSecurityAttribute a = new NonAbstractSecurityAttribute (SecurityAction.Assert);
59                         Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
60                         a.Action = SecurityAction.Demand;
61                         Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
62                         a.Action = SecurityAction.Deny;
63                         Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
64                         a.Action = SecurityAction.InheritanceDemand;
65                         Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
66                         a.Action = SecurityAction.LinkDemand;
67                         Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
68                         a.Action = SecurityAction.PermitOnly;
69                         Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
70                         a.Action = SecurityAction.RequestMinimum;
71                         Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
72                         a.Action = SecurityAction.RequestOptional;
73                         Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
74                         a.Action = SecurityAction.RequestRefuse;
75                         Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
76                 }
77
78                 [Test]
79                 public void Action_Invalid ()
80                 {
81                         NonAbstractSecurityAttribute a = new NonAbstractSecurityAttribute ((SecurityAction)Int32.MinValue);
82                         // no validation in attribute
83                 }
84
85                 [Test]
86                 public void Unrestricted ()
87                 {
88                         NonAbstractSecurityAttribute a = new NonAbstractSecurityAttribute (SecurityAction.Assert);
89                         Assert.IsFalse (a.Unrestricted, "Unrestricted (default)");
90                         a.Unrestricted = true;
91                         Assert.IsTrue (a.Unrestricted, "Unrestricted (true)");
92                         a.Unrestricted = false;
93                         Assert.IsFalse (a.Unrestricted, "Unrestricted (false)");
94                 }
95
96                 [Test]
97                 public void Attributes ()
98                 {
99                         Type t = typeof (NonAbstractSecurityAttribute);
100                         Assert.IsFalse (t.IsSerializable, "IsSerializable");
101
102                         object[] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
103                         Assert.AreEqual (0, attrs.Length, "AttributeUsage-false");
104
105                         attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), true);
106                         Assert.AreEqual (1, attrs.Length, "AttributeUsage-true");
107                         AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
108                         Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
109                         Assert.IsFalse (aua.Inherited, "Inherited");
110                         AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
111                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
112                 }
113         }
114 }