2004-12-03 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / GacIdentityPermissionAttributeTest.cs
1 //
2 // GacIdentityPermissionAttributeTest.cs - NUnit Test Cases for GacIdentityPermissionAttribute
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 #if NET_2_0
30
31 using NUnit.Framework;
32 using System;
33 using System.Security;
34 using System.Security.Permissions;
35
36 namespace MonoTests.System.Security.Permissions {
37
38         [TestFixture]
39         public class GacIdentityPermissionAttributeTest {
40
41                 [Test]
42                 public void Default ()
43                 {
44                         GacIdentityPermissionAttribute a = new GacIdentityPermissionAttribute (SecurityAction.Assert);
45                         Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
46                         Assert.IsFalse (a.Unrestricted, "Unrestricted");
47
48                         IPermission p = a.CreatePermission ();
49                         Assert.IsNotNull (p, "CreatePermission");
50                         Assert.IsTrue ((p is GacIdentityPermission), "GacIdentityPermission");
51                 }
52
53                 [Test]
54                 public void Action ()
55                 {
56                         GacIdentityPermissionAttribute a = new GacIdentityPermissionAttribute (SecurityAction.Assert);
57                         Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
58                         a.Action = SecurityAction.Demand;
59                         Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
60                         a.Action = SecurityAction.Deny;
61                         Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
62                         a.Action = SecurityAction.InheritanceDemand;
63                         Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
64                         a.Action = SecurityAction.LinkDemand;
65                         Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
66                         a.Action = SecurityAction.PermitOnly;
67                         Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
68                         a.Action = SecurityAction.RequestMinimum;
69                         Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
70                         a.Action = SecurityAction.RequestOptional;
71                         Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
72                         a.Action = SecurityAction.RequestRefuse;
73                         Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
74 #if NET_2_0
75                         a.Action = SecurityAction.DemandChoice;
76                         Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
77                         a.Action = SecurityAction.InheritanceDemandChoice;
78                         Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
79                         a.Action = SecurityAction.LinkDemandChoice;
80                         Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
81 #endif
82                 }
83
84                 [Test]
85                 public void Action_Invalid ()
86                 {
87                         GacIdentityPermissionAttribute a = new GacIdentityPermissionAttribute ((SecurityAction)Int32.MinValue);
88                         // no validation in attribute
89                 }
90
91                 [Test]
92 // MS BUG       [ExpectedException (typeof (ArgumentException))]
93                 public void Unrestricted ()
94                 {
95                         GacIdentityPermissionAttribute a = new GacIdentityPermissionAttribute (SecurityAction.Assert);
96                         a.Unrestricted = true;
97                         IPermission perm = a.CreatePermission ();
98                 }
99
100                 [Test]
101                 public void Attributes ()
102                 {
103                         GacIdentityPermissionAttribute a = new GacIdentityPermissionAttribute (SecurityAction.Assert);
104                         Type t = typeof (GacIdentityPermissionAttribute);
105                         Assert.IsTrue (t.IsSerializable, "IsSerializable");
106
107                         object[] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
108                         Assert.AreEqual (1, attrs.Length, "AttributeUsage");
109                         AttributeUsageAttribute aua = (AttributeUsageAttribute) attrs [0];
110                         Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
111                         Assert.IsFalse (aua.Inherited, "Inherited");
112                         AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
113                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
114                 }
115         }
116 }
117
118 #endif