2004-09-01 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / ReflectionPermissionAttributeTest.cs
1 //
2 // ReflectionPermissionAttributeTest.cs -
3 //      NUnit Test Cases for ReflectionPermissionAttribute
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
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 ReflectionPermissionAttributeTest {
40
41                 [Test]
42                 public void Default () 
43                 {
44                         ReflectionPermissionAttribute a = new ReflectionPermissionAttribute (SecurityAction.Assert);
45                         Assert.AreEqual (ReflectionPermissionFlag.NoFlags, a.Flags, "Flags");
46                         Assert.IsFalse (a.MemberAccess, "MemberAccess");
47                         Assert.IsFalse (a.ReflectionEmit, "ReflectionEmit");
48                         Assert.IsFalse (a.TypeInformation, "TypeInformation");
49                         Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
50                         Assert.IsFalse (a.Unrestricted, "Unrestricted");
51
52                         ReflectionPermission perm = (ReflectionPermission) a.CreatePermission ();
53                         Assert.AreEqual (ReflectionPermissionFlag.NoFlags, perm.Flags, "CreatePermission.Flags");
54                         Assert.IsFalse (perm.IsUnrestricted (), "perm.Unrestricted");
55                 }
56
57                 [Test]
58                 public void Action () 
59                 {
60                         ReflectionPermissionAttribute a = new ReflectionPermissionAttribute (SecurityAction.Assert);
61                         Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
62                         a.Action = SecurityAction.Demand;
63                         Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
64                         a.Action = SecurityAction.Deny;
65                         Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
66                         a.Action = SecurityAction.InheritanceDemand;
67                         Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
68                         a.Action = SecurityAction.LinkDemand;
69                         Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
70                         a.Action = SecurityAction.PermitOnly;
71                         Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
72                         a.Action = SecurityAction.RequestMinimum;
73                         Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
74                         a.Action = SecurityAction.RequestOptional;
75                         Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
76                         a.Action = SecurityAction.RequestRefuse;
77                         Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
78 #if NET_2_0
79                         a.Action = SecurityAction.DemandChoice;
80                         Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
81                         a.Action = SecurityAction.InheritanceDemandChoice;
82                         Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
83                         a.Action = SecurityAction.LinkDemandChoice;
84                         Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
85 #endif
86                 }
87
88                 [Test]
89                 public void Action_Invalid ()
90                 {
91                         ReflectionPermissionAttribute a = new ReflectionPermissionAttribute ((SecurityAction)Int32.MinValue);
92                         // no validation in attribute
93                 }
94
95                 [Test]
96                 public void Flags () 
97                 {
98                         ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
99                         attr.Flags = ReflectionPermissionFlag.MemberAccess;
100                         Assert.IsTrue (attr.MemberAccess, "Flags/MemberAccess=MemberAccess");
101                         Assert.IsFalse (attr.ReflectionEmit, "Flags/MemberAccess=ReflectionEmit");
102                         Assert.IsFalse (attr.TypeInformation, "Flags/MemberAccess=TypeInformation");
103                         attr.Flags |= ReflectionPermissionFlag.ReflectionEmit;
104                         Assert.IsTrue (attr.MemberAccess, "Flags/ReflectionEmit=MemberAccess");
105                         Assert.IsTrue (attr.ReflectionEmit, "Flags/ReflectionEmit=ReflectionEmit");
106                         Assert.IsFalse (attr.TypeInformation, "Flags/ReflectionEmit=TypeInformation");
107                         attr.Flags |= ReflectionPermissionFlag.TypeInformation;
108                         Assert.IsTrue (attr.MemberAccess, "Flags/TypeInformation=MemberAccess");
109                         Assert.IsTrue (attr.ReflectionEmit, "Flags/TypeInformation=ReflectionEmit");
110                         Assert.IsTrue (attr.TypeInformation, "Flags/TypeInformation=TypeInformation");
111                         attr.Flags = ReflectionPermissionFlag.NoFlags;
112                         Assert.IsFalse (attr.MemberAccess, "Flags/NoFlags=MemberAccess");
113                         Assert.IsFalse (attr.ReflectionEmit, "Flags/NoFlags=ReflectionEmit");
114                         Assert.IsFalse (attr.TypeInformation, "Flags/NoFlags=TypeInformation");
115                 }
116
117                 [Test]
118                 public void NoFlags () 
119                 {
120                         ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
121                         Assert.AreEqual (ReflectionPermissionFlag.NoFlags, attr.Flags, "NoFlags.Flags");
122                         Assert.IsFalse (attr.Unrestricted, "NoFlags.Unrestricted");
123                         Assert.IsFalse (attr.MemberAccess, "NoFlags=MemberAccess");
124                         Assert.IsFalse (attr.ReflectionEmit, "NoFlags=ReflectionEmit");
125                         Assert.IsFalse (attr.TypeInformation, "NoFlags=TypeInformation");
126                         ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
127                         Assert.AreEqual (ReflectionPermissionFlag.NoFlags, p.Flags, "NoFlags=ReflectionPermission");
128                 }
129
130                 [Test]
131                 public void Flags_Invalid ()
132                 {
133                         ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
134                         attr.Flags = (ReflectionPermissionFlag)Int32.MinValue;
135                 }
136
137                 [Test]
138                 public void MemberAccess () 
139                 {
140                         ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
141                         attr.MemberAccess = true;
142                         Assert.AreEqual (ReflectionPermissionFlag.MemberAccess, attr.Flags, "MemberAccess.Flags");
143                         Assert.IsFalse (attr.Unrestricted, "MemberAccess.Unrestricted");
144                         Assert.IsTrue (attr.MemberAccess, "MemberAccess=MemberAccess");
145                         Assert.IsFalse (attr.ReflectionEmit, "MemberAccess=ReflectionEmit");
146                         Assert.IsFalse (attr.TypeInformation, "MemberAccess=TypeInformation");
147                         ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
148                         Assert.AreEqual (ReflectionPermissionFlag.MemberAccess, p.Flags, "MemberAccess=ReflectionPermission");
149                 }
150
151                 [Test]
152                 public void ReflectionEmit () 
153                 {
154                         ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
155                         attr.ReflectionEmit = true;
156                         Assert.AreEqual (ReflectionPermissionFlag.ReflectionEmit, attr.Flags, "ReflectionEmit.Flags");
157                         Assert.IsFalse (attr.Unrestricted, "ReflectionEmit.Unrestricted");
158                         Assert.IsFalse (attr.MemberAccess, "ReflectionEmit=MemberAccess");
159                         Assert.IsTrue (attr.ReflectionEmit, "ReflectionEmit=ReflectionEmit");
160                         Assert.IsFalse (attr.TypeInformation, "ReflectionEmit=TypeInformation");
161                         ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
162                         Assert.AreEqual (ReflectionPermissionFlag.ReflectionEmit, p.Flags, "ReflectionEmit=ReflectionPermission");
163                 }
164
165                 [Test]
166                 public void TypeInformation () 
167                 {
168                         ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
169                         attr.TypeInformation = true;
170                         Assert.AreEqual (ReflectionPermissionFlag.TypeInformation, attr.Flags, "TypeInformation.Flags");
171                         Assert.IsFalse (attr.Unrestricted, "TypeInformation.Unrestricted");
172                         Assert.IsFalse (attr.MemberAccess, "TypeInformation=MemberAccess");
173                         Assert.IsFalse (attr.ReflectionEmit, "TypeInformation=ReflectionEmit");
174                         Assert.IsTrue (attr.TypeInformation, "TypeInformation=TypeInformation");
175                         ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
176                         Assert.AreEqual (ReflectionPermissionFlag.TypeInformation, p.Flags, "TypeInformation=TypeInformation");
177                 }
178
179                 [Test]
180                 public void AllFlags () 
181                 {
182                         ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
183                         attr.MemberAccess = true;
184                         attr.ReflectionEmit = true;
185                         attr.TypeInformation = true;
186                         Assert.AreEqual (ReflectionPermissionFlag.AllFlags, attr.Flags, "AllFlags.Flags");
187                         // attribute isn't unrestricted but the created permission is !!!
188                         Assert.IsFalse (attr.Unrestricted, "AllFlags.Unrestricted");
189                         Assert.IsTrue (attr.MemberAccess, "AllFlags=MemberAccess");
190                         Assert.IsTrue (attr.ReflectionEmit, "AllFlags=ReflectionEmit");
191                         Assert.IsTrue (attr.TypeInformation, "AllFlags=TypeInformation");
192
193                         ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
194                         Assert.AreEqual (ReflectionPermissionFlag.AllFlags, p.Flags, "AllFlags=ReflectionPermission");
195                         Assert.IsTrue (p.IsUnrestricted (), "AllFlags=Unrestricted");
196                 }
197
198                 [Test]
199                 public void Unrestricted () 
200                 {
201                         ReflectionPermissionAttribute a = new ReflectionPermissionAttribute (SecurityAction.Assert);
202                         a.Unrestricted = true;
203                         Assert.AreEqual (ReflectionPermissionFlag.NoFlags, a.Flags, "Unrestricted");
204
205                         ReflectionPermission perm = (ReflectionPermission) a.CreatePermission ();
206                         Assert.AreEqual (ReflectionPermissionFlag.AllFlags, perm.Flags, "CreatePermission.Flags");
207                 }
208
209                 [Test]
210                 public void Attributes ()
211                 {
212                         Type t = typeof (ReflectionPermissionAttribute);
213                         Assert.IsTrue (t.IsSerializable, "IsSerializable");
214
215                         object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
216                         Assert.AreEqual (1, attrs.Length, "AttributeUsage");
217                         AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
218                         Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
219                         Assert.IsFalse (aua.Inherited, "Inherited");
220                         AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
221                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
222                 }
223         }
224 }