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 #if MOBILE
40 	[Ignore]
41 #endif
42 	public class ReflectionPermissionAttributeTest {
43 
44 		[Test]
Default()45 		public void Default ()
46 		{
47 			ReflectionPermissionAttribute a = new ReflectionPermissionAttribute (SecurityAction.Assert);
48 			Assert.AreEqual (ReflectionPermissionFlag.NoFlags, a.Flags, "Flags");
49 			Assert.IsFalse (a.MemberAccess, "MemberAccess");
50 			Assert.IsFalse (a.ReflectionEmit, "ReflectionEmit");
51 			Assert.IsFalse (a.TypeInformation, "TypeInformation");
52 			Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
53 			Assert.IsFalse (a.Unrestricted, "Unrestricted");
54 
55 			ReflectionPermission perm = (ReflectionPermission) a.CreatePermission ();
56 			Assert.AreEqual (ReflectionPermissionFlag.NoFlags, perm.Flags, "CreatePermission.Flags");
57 			Assert.IsFalse (perm.IsUnrestricted (), "perm.Unrestricted");
58 		}
59 
60 		[Test]
Action()61 		public void Action ()
62 		{
63 			ReflectionPermissionAttribute a = new ReflectionPermissionAttribute (SecurityAction.Assert);
64 			Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
65 			a.Action = SecurityAction.Demand;
66 			Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
67 			a.Action = SecurityAction.Deny;
68 			Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
69 			a.Action = SecurityAction.InheritanceDemand;
70 			Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
71 			a.Action = SecurityAction.LinkDemand;
72 			Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
73 			a.Action = SecurityAction.PermitOnly;
74 			Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
75 			a.Action = SecurityAction.RequestMinimum;
76 			Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
77 			a.Action = SecurityAction.RequestOptional;
78 			Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
79 			a.Action = SecurityAction.RequestRefuse;
80 			Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
81 		}
82 
83 		[Test]
Action_Invalid()84 		public void Action_Invalid ()
85 		{
86 			ReflectionPermissionAttribute a = new ReflectionPermissionAttribute ((SecurityAction)Int32.MinValue);
87 			// no validation in attribute
88 		}
89 
90 		[Test]
Flags()91 		public void Flags ()
92 		{
93 			ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
94 			attr.Flags = ReflectionPermissionFlag.MemberAccess;
95 			Assert.IsTrue (attr.MemberAccess, "Flags/MemberAccess=MemberAccess");
96 			Assert.IsFalse (attr.ReflectionEmit, "Flags/MemberAccess=ReflectionEmit");
97 			Assert.IsFalse (attr.TypeInformation, "Flags/MemberAccess=TypeInformation");
98 			attr.Flags |= ReflectionPermissionFlag.ReflectionEmit;
99 			Assert.IsTrue (attr.MemberAccess, "Flags/ReflectionEmit=MemberAccess");
100 			Assert.IsTrue (attr.ReflectionEmit, "Flags/ReflectionEmit=ReflectionEmit");
101 			Assert.IsFalse (attr.TypeInformation, "Flags/ReflectionEmit=TypeInformation");
102 			attr.Flags |= ReflectionPermissionFlag.TypeInformation;
103 			Assert.IsTrue (attr.MemberAccess, "Flags/TypeInformation=MemberAccess");
104 			Assert.IsTrue (attr.ReflectionEmit, "Flags/TypeInformation=ReflectionEmit");
105 			Assert.IsTrue (attr.TypeInformation, "Flags/TypeInformation=TypeInformation");
106 			attr.Flags = ReflectionPermissionFlag.NoFlags;
107 			Assert.IsFalse (attr.MemberAccess, "Flags/NoFlags=MemberAccess");
108 			Assert.IsFalse (attr.ReflectionEmit, "Flags/NoFlags=ReflectionEmit");
109 			Assert.IsFalse (attr.TypeInformation, "Flags/NoFlags=TypeInformation");
110 		}
111 
112 		[Test]
NoFlags()113 		public void NoFlags ()
114 		{
115 			ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
116 			Assert.AreEqual (ReflectionPermissionFlag.NoFlags, attr.Flags, "NoFlags.Flags");
117 			Assert.IsFalse (attr.Unrestricted, "NoFlags.Unrestricted");
118 			Assert.IsFalse (attr.MemberAccess, "NoFlags=MemberAccess");
119 			Assert.IsFalse (attr.ReflectionEmit, "NoFlags=ReflectionEmit");
120 			Assert.IsFalse (attr.TypeInformation, "NoFlags=TypeInformation");
121 			ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
122 			Assert.AreEqual (ReflectionPermissionFlag.NoFlags, p.Flags, "NoFlags=ReflectionPermission");
123 		}
124 
125 		[Test]
Flags_Invalid()126 		public void Flags_Invalid ()
127 		{
128 			ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
129 			attr.Flags = (ReflectionPermissionFlag)Int32.MinValue;
130 		}
131 
132 		[Test]
MemberAccess()133 		public void MemberAccess ()
134 		{
135 			ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
136 			attr.MemberAccess = true;
137 			Assert.AreEqual (ReflectionPermissionFlag.MemberAccess, attr.Flags, "MemberAccess.Flags");
138 			Assert.IsFalse (attr.Unrestricted, "MemberAccess.Unrestricted");
139 			Assert.IsTrue (attr.MemberAccess, "MemberAccess=MemberAccess");
140 			Assert.IsFalse (attr.ReflectionEmit, "MemberAccess=ReflectionEmit");
141 			Assert.IsFalse (attr.TypeInformation, "MemberAccess=TypeInformation");
142 			ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
143 			Assert.AreEqual (ReflectionPermissionFlag.MemberAccess, p.Flags, "MemberAccess=ReflectionPermission");
144 		}
145 
146 		[Test]
ReflectionEmit()147 		public void ReflectionEmit ()
148 		{
149 			ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
150 			attr.ReflectionEmit = true;
151 			Assert.AreEqual (ReflectionPermissionFlag.ReflectionEmit, attr.Flags, "ReflectionEmit.Flags");
152 			Assert.IsFalse (attr.Unrestricted, "ReflectionEmit.Unrestricted");
153 			Assert.IsFalse (attr.MemberAccess, "ReflectionEmit=MemberAccess");
154 			Assert.IsTrue (attr.ReflectionEmit, "ReflectionEmit=ReflectionEmit");
155 			Assert.IsFalse (attr.TypeInformation, "ReflectionEmit=TypeInformation");
156 			ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
157 			Assert.AreEqual (ReflectionPermissionFlag.ReflectionEmit, p.Flags, "ReflectionEmit=ReflectionPermission");
158 		}
159 
160 		[Test]
TypeInformation()161 		public void TypeInformation ()
162 		{
163 			ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
164 			attr.TypeInformation = true;
165 			Assert.AreEqual (ReflectionPermissionFlag.TypeInformation, attr.Flags, "TypeInformation.Flags");
166 			Assert.IsFalse (attr.Unrestricted, "TypeInformation.Unrestricted");
167 			Assert.IsFalse (attr.MemberAccess, "TypeInformation=MemberAccess");
168 			Assert.IsFalse (attr.ReflectionEmit, "TypeInformation=ReflectionEmit");
169 			Assert.IsTrue (attr.TypeInformation, "TypeInformation=TypeInformation");
170 			ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
171 			Assert.AreEqual (ReflectionPermissionFlag.TypeInformation, p.Flags, "TypeInformation=TypeInformation");
172 		}
173 
174 		[Test]
AllFlags()175 		public void AllFlags ()
176 		{
177 			ReflectionPermissionAttribute attr = new ReflectionPermissionAttribute (SecurityAction.Assert);
178 			attr.MemberAccess = true;
179 			attr.ReflectionEmit = true;
180 			attr.TypeInformation = true;
181 			Assert.AreEqual (ReflectionPermissionFlag.AllFlags, attr.Flags, "AllFlags.Flags");
182 			// attribute isn't unrestricted but the created permission is !!!
183 			Assert.IsFalse (attr.Unrestricted, "AllFlags.Unrestricted");
184 			Assert.IsTrue (attr.MemberAccess, "AllFlags=MemberAccess");
185 			Assert.IsTrue (attr.ReflectionEmit, "AllFlags=ReflectionEmit");
186 			Assert.IsTrue (attr.TypeInformation, "AllFlags=TypeInformation");
187 
188 			ReflectionPermission p = (ReflectionPermission) attr.CreatePermission ();
189 			Assert.AreEqual (ReflectionPermissionFlag.AllFlags, p.Flags, "AllFlags=ReflectionPermission");
190 			Assert.IsTrue (p.IsUnrestricted (), "AllFlags=Unrestricted");
191 		}
192 
193 		[Test]
Unrestricted()194 		public void Unrestricted ()
195 		{
196 			ReflectionPermissionAttribute a = new ReflectionPermissionAttribute (SecurityAction.Assert);
197 			a.Unrestricted = true;
198 			Assert.AreEqual (ReflectionPermissionFlag.NoFlags, a.Flags, "Unrestricted");
199 
200 			ReflectionPermission perm = (ReflectionPermission) a.CreatePermission ();
201 			Assert.AreEqual (ReflectionPermissionFlag.AllFlags, perm.Flags, "CreatePermission.Flags");
202 		}
203 
204 		[Test]
Attributes()205 		public void Attributes ()
206 		{
207 			Type t = typeof (ReflectionPermissionAttribute);
208 			Assert.IsTrue (t.IsSerializable, "IsSerializable");
209 
210 			object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
211 			Assert.AreEqual (1, attrs.Length, "AttributeUsage");
212 			AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
213 			Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
214 			Assert.IsFalse (aua.Inherited, "Inherited");
215 			AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
216 			Assert.AreEqual (at, aua.ValidOn, "ValidOn");
217 		}
218 	}
219 }
220