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");
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");
89 public void Action_Invalid ()
91 PrincipalPermissionAttribute a
= new PrincipalPermissionAttribute ((SecurityAction
)Int32
.MinValue
);
92 // no validation in attribute
96 public void NameNullRoleNullAuthenticated ()
98 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
101 attr
.Authenticated
= true;
102 Assert
.IsNull (attr
.Name
, "NameNullRoleNullAuthenticated.Name");
103 Assert
.IsNull (attr
.Role
, "NameNullRoleNullAuthenticated.Role");
104 Assert
.IsTrue (attr
.Authenticated
, "NameNullRoleNullAuthenticated.Authenticated");
105 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
106 Assert
.IsTrue (p
.IsUnrestricted (), "NameNullRoleNullAuthenticated.IsUnrestricted");
110 public void NameNullRoleNullNonAuthenticated ()
112 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
115 attr
.Authenticated
= false;
116 Assert
.IsNull (attr
.Name
, "NameNullRoleNullNonAuthenticated.Name");
117 Assert
.IsNull (attr
.Role
, "NameNullRoleNullNonAuthenticated.Role");
118 Assert
.IsFalse (attr
.Authenticated
, "NameNullRoleNullNonAuthenticated.Authenticated");
119 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
120 Assert
.IsFalse (p
.IsUnrestricted (), "NameNullRoleNullNonAuthenticated.IsUnrestricted");
124 public void NameRoleNullAuthenticated ()
126 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
129 attr
.Authenticated
= true;
130 Assert
.AreEqual (user
, attr
.Name
, "NameRoleNullAuthenticated.Name");
131 Assert
.IsNull (attr
.Role
, "NameRoleNullAuthenticated.Role");
132 Assert
.IsTrue (attr
.Authenticated
, "NameRoleNullAuthenticated.Authenticated");
133 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
134 Assert
.IsFalse (p
.IsUnrestricted (), "NameRoleNullAuthenticated.IsUnrestricted");
138 public void NameRoleNullNonAuthenticated ()
140 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
143 attr
.Authenticated
= false;
144 Assert
.AreEqual (user
, attr
.Name
, "NameRoleNullNonAuthenticated.Name");
145 Assert
.IsNull (attr
.Role
, "NameRoleNullNonAuthenticated.Role");
146 Assert
.IsFalse (attr
.Authenticated
, "NameRoleNullNonAuthenticated.Authenticated");
147 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
148 Assert
.IsFalse (p
.IsUnrestricted (), "NameRoleNullNonAuthenticated.IsUnrestricted");
152 public void NameNullRoleAuthenticated ()
154 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
157 attr
.Authenticated
= true;
158 Assert
.IsNull (attr
.Name
, "NameNullRoleAuthenticated.Name");
159 Assert
.AreEqual (role
, attr
.Role
, "NameNullRoleAuthenticated.Role");
160 Assert
.IsTrue (attr
.Authenticated
, "NameNullRoleAuthenticated.Authenticated");
161 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
162 Assert
.IsFalse (p
.IsUnrestricted (), "NameNullRoleAuthenticated.IsUnrestricted");
166 public void NameNullRoleNonAuthenticated ()
168 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
171 attr
.Authenticated
= false;
172 Assert
.IsNull (attr
.Name
, "NameNullRoleNonAuthenticated.Name");
173 Assert
.AreEqual (role
, attr
.Role
, "NameNullRoleNonAuthenticated.Role");
174 Assert
.IsFalse (attr
.Authenticated
, "NameNullRoleNonAuthenticated.Authenticated");
175 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
176 Assert
.IsFalse (p
.IsUnrestricted (), "NameNullRoleNonAuthenticated.IsUnrestricted");
180 public void NameRoleAuthenticated ()
182 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
185 attr
.Authenticated
= true;
186 Assert
.AreEqual (user
, attr
.Name
, "NameRoleAuthenticated.Name");
187 Assert
.AreEqual (role
, attr
.Role
, "NameRoleAuthenticated.Role");
188 Assert
.IsTrue (attr
.Authenticated
, "NameRoleAuthenticated.Authenticated");
189 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
190 Assert
.IsFalse (p
.IsUnrestricted (), "NameRoleAuthenticated.IsUnrestricted");
194 public void NameRoleNonAuthenticated ()
196 PrincipalPermissionAttribute attr
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
199 attr
.Authenticated
= false;
200 Assert
.AreEqual (user
, attr
.Name
, "NameRoleNonAuthenticated.Name");
201 Assert
.AreEqual (role
, attr
.Role
, "NameRoleNonAuthenticated.Role");
202 Assert
.IsFalse (attr
.Authenticated
, "NameRoleNonAuthenticated.Authenticated");
203 PrincipalPermission p
= (PrincipalPermission
) attr
.CreatePermission ();
204 Assert
.IsFalse (p
.IsUnrestricted (), "NameRoleNonAuthenticated.IsUnrestricted");
208 public void Unrestricted ()
210 PrincipalPermissionAttribute a
= new PrincipalPermissionAttribute (SecurityAction
.Assert
);
211 a
.Unrestricted
= true;
213 PrincipalPermission perm
= (PrincipalPermission
) a
.CreatePermission ();
214 Assert
.IsTrue (perm
.IsUnrestricted (), "CreatePermission.IsUnrestricted");
218 public void Attributes ()
220 Type t
= typeof (PrincipalPermissionAttribute
);
221 Assert
.IsTrue (t
.IsSerializable
, "IsSerializable");
223 object[] attrs
= t
.GetCustomAttributes (typeof (AttributeUsageAttribute
), false);
224 Assert
.AreEqual (1, attrs
.Length
, "AttributeUsage");
225 AttributeUsageAttribute aua
= (AttributeUsageAttribute
)attrs
[0];
226 Assert
.IsTrue (aua
.AllowMultiple
, "AllowMultiple");
227 Assert
.IsFalse (aua
.Inherited
, "Inherited");
228 AttributeTargets at
= (AttributeTargets
.Class
| AttributeTargets
.Method
);
229 Assert
.AreEqual (at
, aua
.ValidOn
, "ValidOn");