2 // PrincipalPermissionAttributeTest.cs - NUnit Test Cases for PrincipalPermissionAttribute
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoTests.System.Security.Permissions {
38 public class PrincipalPermissionAttributeTest {
40 private static string user = "user";
41 private static string role = "role";
44 public void Default ()
46 PrincipalPermissionAttribute a = new PrincipalPermissionAttribute (SecurityAction.Assert);
47 Assert.IsNull (a.Name, "Name");
48 Assert.IsNull (a.Role, "Role");
49 Assert.IsTrue (a.Authenticated, "Authenticated");
50 Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
51 Assert.IsFalse (a.Unrestricted, "Unrestricted");
53 PrincipalPermission perm = (PrincipalPermission) a.CreatePermission ();
54 Assert.IsNotNull (perm, "CreatePermission");
60 PrincipalPermissionAttribute a = new PrincipalPermissionAttribute (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");
81 public void Action_Invalid ()
83 PrincipalPermissionAttribute a = new PrincipalPermissionAttribute ((SecurityAction)Int32.MinValue);
84 // no validation in attribute
88 public void NameNullRoleNullAuthenticated ()
90 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
93 attr.Authenticated = true;
94 Assert.IsNull (attr.Name, "NameNullRoleNullAuthenticated.Name");
95 Assert.IsNull (attr.Role, "NameNullRoleNullAuthenticated.Role");
96 Assert.IsTrue (attr.Authenticated, "NameNullRoleNullAuthenticated.Authenticated");
97 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
98 Assert.IsTrue (p.IsUnrestricted (), "NameNullRoleNullAuthenticated.IsUnrestricted");
102 public void NameNullRoleNullNonAuthenticated ()
104 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
107 attr.Authenticated = false;
108 Assert.IsNull (attr.Name, "NameNullRoleNullNonAuthenticated.Name");
109 Assert.IsNull (attr.Role, "NameNullRoleNullNonAuthenticated.Role");
110 Assert.IsFalse (attr.Authenticated, "NameNullRoleNullNonAuthenticated.Authenticated");
111 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
112 Assert.IsFalse (p.IsUnrestricted (), "NameNullRoleNullNonAuthenticated.IsUnrestricted");
116 public void NameRoleNullAuthenticated ()
118 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
121 attr.Authenticated = true;
122 Assert.AreEqual (user, attr.Name, "NameRoleNullAuthenticated.Name");
123 Assert.IsNull (attr.Role, "NameRoleNullAuthenticated.Role");
124 Assert.IsTrue (attr.Authenticated, "NameRoleNullAuthenticated.Authenticated");
125 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
126 Assert.IsFalse (p.IsUnrestricted (), "NameRoleNullAuthenticated.IsUnrestricted");
130 public void NameRoleNullNonAuthenticated ()
132 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
135 attr.Authenticated = false;
136 Assert.AreEqual (user, attr.Name, "NameRoleNullNonAuthenticated.Name");
137 Assert.IsNull (attr.Role, "NameRoleNullNonAuthenticated.Role");
138 Assert.IsFalse (attr.Authenticated, "NameRoleNullNonAuthenticated.Authenticated");
139 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
140 Assert.IsFalse (p.IsUnrestricted (), "NameRoleNullNonAuthenticated.IsUnrestricted");
144 public void NameNullRoleAuthenticated ()
146 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
149 attr.Authenticated = true;
150 Assert.IsNull (attr.Name, "NameNullRoleAuthenticated.Name");
151 Assert.AreEqual (role, attr.Role, "NameNullRoleAuthenticated.Role");
152 Assert.IsTrue (attr.Authenticated, "NameNullRoleAuthenticated.Authenticated");
153 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
154 Assert.IsFalse (p.IsUnrestricted (), "NameNullRoleAuthenticated.IsUnrestricted");
158 public void NameNullRoleNonAuthenticated ()
160 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
163 attr.Authenticated = false;
164 Assert.IsNull (attr.Name, "NameNullRoleNonAuthenticated.Name");
165 Assert.AreEqual (role, attr.Role, "NameNullRoleNonAuthenticated.Role");
166 Assert.IsFalse (attr.Authenticated, "NameNullRoleNonAuthenticated.Authenticated");
167 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
168 Assert.IsFalse (p.IsUnrestricted (), "NameNullRoleNonAuthenticated.IsUnrestricted");
172 public void NameRoleAuthenticated ()
174 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
177 attr.Authenticated = true;
178 Assert.AreEqual (user, attr.Name, "NameRoleAuthenticated.Name");
179 Assert.AreEqual (role, attr.Role, "NameRoleAuthenticated.Role");
180 Assert.IsTrue (attr.Authenticated, "NameRoleAuthenticated.Authenticated");
181 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
182 Assert.IsFalse (p.IsUnrestricted (), "NameRoleAuthenticated.IsUnrestricted");
186 public void NameRoleNonAuthenticated ()
188 PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
191 attr.Authenticated = false;
192 Assert.AreEqual (user, attr.Name, "NameRoleNonAuthenticated.Name");
193 Assert.AreEqual (role, attr.Role, "NameRoleNonAuthenticated.Role");
194 Assert.IsFalse (attr.Authenticated, "NameRoleNonAuthenticated.Authenticated");
195 PrincipalPermission p = (PrincipalPermission) attr.CreatePermission ();
196 Assert.IsFalse (p.IsUnrestricted (), "NameRoleNonAuthenticated.IsUnrestricted");
200 public void Unrestricted ()
202 PrincipalPermissionAttribute a = new PrincipalPermissionAttribute (SecurityAction.Assert);
203 a.Unrestricted = true;
205 PrincipalPermission perm = (PrincipalPermission) a.CreatePermission ();
206 Assert.IsTrue (perm.IsUnrestricted (), "CreatePermission.IsUnrestricted");
210 public void Attributes ()
212 Type t = typeof (PrincipalPermissionAttribute);
213 Assert.IsTrue (t.IsSerializable, "IsSerializable");
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.Class | AttributeTargets.Method);
221 Assert.AreEqual (at, aua.ValidOn, "ValidOn");