Add this for backwards compatibility
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / PrincipalPermissionAttributeTest.cs
1 //
2 // PrincipalPermissionAttributeTest.cs - NUnit Test Cases for PrincipalPermissionAttribute
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace MonoTests.System.Security.Permissions {
36
37         [TestFixture]
38         public class PrincipalPermissionAttributeTest {
39
40                 private static string user = "user";
41                 private static string role = "role";
42
43                 [Test]
44                 public void Default () 
45                 {
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");
52
53                         PrincipalPermission perm = (PrincipalPermission) a.CreatePermission ();
54                         Assert.IsNotNull (perm, "CreatePermission");
55                 }
56
57                 [Test]
58                 public void Action () 
59                 {
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");
78                 }
79
80                 [Test]
81                 public void Action_Invalid ()
82                 {
83                         PrincipalPermissionAttribute a = new PrincipalPermissionAttribute ((SecurityAction)Int32.MinValue);
84                         // no validation in attribute
85                 }
86
87                 [Test]
88                 public void NameNullRoleNullAuthenticated () 
89                 {
90                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
91                         attr.Name = null;
92                         attr.Role = null;
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");
99                 }
100
101                 [Test]
102                 public void NameNullRoleNullNonAuthenticated () 
103                 {
104                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
105                         attr.Name = null;
106                         attr.Role = null;
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");
113                 }
114
115                 [Test]
116                 public void NameRoleNullAuthenticated () 
117                 {
118                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
119                         attr.Name = user;
120                         attr.Role = null;
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");
127                 }
128
129                 [Test]
130                 public void NameRoleNullNonAuthenticated () 
131                 {
132                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
133                         attr.Name = user;
134                         attr.Role = null;
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");
141                 }
142
143                 [Test]
144                 public void NameNullRoleAuthenticated () 
145                 {
146                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
147                         attr.Name = null;
148                         attr.Role = role;
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");
155                 }
156
157                 [Test]
158                 public void NameNullRoleNonAuthenticated () 
159                 {
160                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
161                         attr.Name = null;
162                         attr.Role = role;
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");
169                 }
170
171                 [Test]
172                 public void NameRoleAuthenticated () 
173                 {
174                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
175                         attr.Name = user;
176                         attr.Role = role;
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");
183                 }
184
185                 [Test]
186                 public void NameRoleNonAuthenticated () 
187                 {
188                         PrincipalPermissionAttribute attr = new PrincipalPermissionAttribute (SecurityAction.Assert);
189                         attr.Name = user;
190                         attr.Role = role;
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");
197                 }
198
199                 [Test]
200                 public void Unrestricted () 
201                 {
202                         PrincipalPermissionAttribute a = new PrincipalPermissionAttribute (SecurityAction.Assert);
203                         a.Unrestricted = true;
204
205                         PrincipalPermission perm = (PrincipalPermission) a.CreatePermission ();
206                         Assert.IsTrue (perm.IsUnrestricted (), "CreatePermission.IsUnrestricted");
207                 }
208
209                 [Test]
210                 public void Attributes ()
211                 {
212                         Type t = typeof (PrincipalPermissionAttribute);
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.Class | AttributeTargets.Method);
221                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
222                 }
223         }
224 }