2005-12-23 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security / SecurityManagerTest.cs
1 //
2 // SecurityManagerTest.cs - NUnit Test Cases for SecurityManager
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 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.Collections;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Security.Policy;
37
38 namespace MonoTests.System.Security {
39
40         [TestFixture]
41         public class SecurityManagerTest {
42
43                 static Evidence CurrentEvidence;
44
45                 [TestFixtureSetUp]
46                 public void FixtureSetUp () 
47                 {
48                         CurrentEvidence = Assembly.GetExecutingAssembly ().Evidence;
49                 }
50
51                 [TearDown]
52                 public void TearDown ()
53                 {
54                         SecurityManager.CheckExecutionRights = true;
55                 }
56
57                 [Test]
58                 public void IsGranted_Null ()
59                 {
60                         // null is always granted
61                         Assert.IsTrue (SecurityManager.IsGranted (null));
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentNullException))]
66                 public void LoadPolicyLevelFromFile_Null ()
67                 {
68                         SecurityManager.LoadPolicyLevelFromFile (null, PolicyLevelType.AppDomain);
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (ArgumentNullException))]
73                 public void LoadPolicyLevelFromString_Null ()
74                 {
75                         SecurityManager.LoadPolicyLevelFromString (null, PolicyLevelType.AppDomain);
76                 }
77
78                 [Test]
79                 public void PolicyHierarchy () 
80                 {
81                         IEnumerator e = SecurityManager.PolicyHierarchy ();
82                         Assert.IsNotNull (e, "PolicyHierarchy");
83                 }
84
85                 private void ResolveEvidenceHost (SecurityZone zone, bool unrestricted, bool empty)
86                 {
87                         string prefix = zone.ToString () + "-";
88                         Evidence e = new Evidence ();
89                         e.AddHost (new Zone (zone));
90                         PermissionSet ps = SecurityManager.ResolvePolicy (e);
91                         // as 2.0 use Unrestricted for Identity permissions they have no need to be 
92                         // kept in resolved permission set
93 #if NET_2_0
94                         Assert.IsTrue ((unrestricted || (ps.Count > 0)), prefix + "Count");
95 #else
96                         Assert.IsTrue ((ps.Count > 0), prefix + "Count");
97 #endif
98                         Assert.AreEqual (empty, ps.IsEmpty (), prefix + "IsEmpty");
99                         Assert.AreEqual (unrestricted, ps.IsUnrestricted (), prefix + "IsUnrestricted");
100 #if NET_2_0
101                         if (unrestricted)
102                                 Assert.IsNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
103                         else
104                                 Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
105 #else
106                         Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
107 #endif
108                 }
109
110 #if NET_2_0
111                 [Category ("NotWorking")]
112 #endif
113                 [Test]
114                 public void ResolvePolicy_Evidence_Host_Zone () 
115                 {
116                         ResolveEvidenceHost (SecurityZone.Internet, false, false);
117                         ResolveEvidenceHost (SecurityZone.Intranet, false, false);
118                         ResolveEvidenceHost (SecurityZone.MyComputer, true, false);
119                         ResolveEvidenceHost (SecurityZone.Trusted, false, false);
120                         ResolveEvidenceHost (SecurityZone.Untrusted, false, false);
121                         ResolveEvidenceHost (SecurityZone.NoZone, false, true);
122                 }
123
124                 private void ResolveEvidenceAssembly (SecurityZone zone)
125                 {
126                         string prefix = zone.ToString () + "-";
127                         Evidence e = new Evidence ();
128                         e.AddAssembly (new Zone (zone));
129                         PermissionSet ps = SecurityManager.ResolvePolicy (e);
130                         Assert.AreEqual (0, ps.Count, prefix + "Count");
131                         Assert.IsTrue (ps.IsEmpty (), prefix + "IsEmpty");
132                         Assert.IsFalse (ps.IsUnrestricted (), prefix + "IsUnrestricted");
133                 }
134
135                 [Test]
136                 public void ResolvePolicy_Evidence_Assembly_Zone ()
137                 {
138                         ResolveEvidenceAssembly (SecurityZone.Internet);
139                         ResolveEvidenceAssembly (SecurityZone.Intranet);
140                         ResolveEvidenceAssembly (SecurityZone.MyComputer);
141                         ResolveEvidenceAssembly (SecurityZone.Trusted);
142                         ResolveEvidenceAssembly (SecurityZone.Untrusted);
143                         ResolveEvidenceAssembly (SecurityZone.NoZone);
144                 }
145
146                 [Test]
147                 public void ResolvePolicy_Evidence_Null ()
148                 {
149                         Evidence e = null;
150                         PermissionSet ps = SecurityManager.ResolvePolicy (e);
151                         // no exception thrown
152                         Assert.IsNotNull (ps);
153                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
154                 }
155
156                 [Test]
157                 public void ResolvePolicy_Evidence_CurrentAssembly ()
158                 {
159                         PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence);
160                         Assert.IsNotNull (granted);
161                         Assert.IsTrue (granted.IsUnrestricted (), "IsUnrestricted");
162                 }
163
164 #if NET_2_0
165                 [Test]
166                 public void ResolvePolicy_Evidences_Null ()
167                 {
168                         Evidence[] e = null;
169                         PermissionSet ps = SecurityManager.ResolvePolicy (e);
170                         // no exception thrown
171                         Assert.IsNotNull (ps);
172                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
173                 }
174 #endif
175
176                 [Test]
177                 public void ResolvePolicy_Evidence_AllNull_NoExecution ()
178                 {
179                         PermissionSet denied = null;
180                         SecurityManager.CheckExecutionRights = false;
181                         PermissionSet granted = SecurityManager.ResolvePolicy (null, null, null, null, out denied);
182                         Assert.IsNull (denied, "Denied");
183                         Assert.AreEqual (0, granted.Count, "Granted.Count");
184                         Assert.IsFalse (granted.IsUnrestricted (), "!Granted.IsUnrestricted");
185                 }
186
187                 [Test]
188                 public void ResolvePolicy_Evidence_NullRequests_CurrentAssembly ()
189                 {
190                         PermissionSet denied = null;
191                         PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, null, out denied);
192                         Assert.IsNull (denied, "Denied");
193                         Assert.IsTrue (granted.IsUnrestricted (), "Granted.IsUnrestricted");
194                 }
195
196                 [Test]
197 #if NET_2_0
198                 [ExpectedException (typeof (PolicyException))]
199                 [Category ("NotWorking")]
200 #endif
201                 public void ResolvePolicy_Evidence_DenyUnrestricted_CurrentAssembly ()
202                 {
203                         PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
204                         PermissionSet denied = null;
205                         PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
206                         // doing this we denied the Execution right
207 #if !NET_2_0
208                         Assert.AreEqual (0, denied.Count, "Denied");
209                         Assert.IsTrue (granted.IsUnrestricted (), "Granted.IsUnrestricted");
210 #endif
211                 }
212
213                 [Test]
214                 [Category ("NotDotNet")] // MS bug - throws a NullReferenceException
215                 public void ResolvePolicy_Evidence_DenyUnrestricted_NoExecution ()
216                 {
217                         PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
218                         PermissionSet denied = null;
219                         SecurityManager.CheckExecutionRights = false;
220                         PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof (ArgumentNullException))]
225                 public void ResolvePolicyGroups_Null ()
226                 {
227                         IEnumerator e = SecurityManager.ResolvePolicyGroups (null);
228                 }
229
230                 [Test]
231                 [ExpectedException (typeof (NullReferenceException))]
232                 public void SavePolicyLevel_Null ()
233                 {
234                         SecurityManager.SavePolicyLevel (null);
235                 }
236
237                 [Test]
238                 [ExpectedException (typeof (PolicyException))]
239                 public void SavePolicyLevel_AppDomain ()
240                 {
241                         PolicyLevel adl = PolicyLevel.CreateAppDomainLevel ();
242                         SecurityManager.SavePolicyLevel (adl);
243                 }
244 #if NET_2_0
245                 [Test]
246                 public void GetZoneAndOrigin ()
247                 {
248                         ArrayList zone = null;
249                         ArrayList origin = null;
250                         SecurityManager.GetZoneAndOrigin (out zone, out origin);
251                         Assert.IsNotNull (zone, "Zone");
252                         Assert.AreEqual (0, zone.Count, "Zone.Count");
253                         Assert.IsNotNull (origin, "Origin");
254                         Assert.AreEqual (0, origin.Count, "Origin.Count");
255                 }
256
257                 [Test]
258                 public void ResolvePolicy_Evidence_ArrayNull ()
259                 {
260                         Evidence[] e = null;
261                         PermissionSet ps = SecurityManager.ResolvePolicy (e);
262                         Assert.IsNotNull (ps, "PermissionSet");
263                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
264                         Assert.AreEqual (0, ps.Count, "Count");
265                 }
266
267                 [Test]
268                 public void ResolvePolicy_Evidence_ArrayEmpty ()
269                 {
270                         Evidence[] e = new Evidence [0];
271                         PermissionSet ps = SecurityManager.ResolvePolicy (e);
272                         Assert.IsNotNull (ps, "PermissionSet");
273                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
274                         Assert.AreEqual (0, ps.Count, "Count");
275                 }
276
277                 [Test]
278                 public void ResolvePolicy_Evidence_Array ()
279                 {
280                         Evidence[] e = new Evidence[] { new Evidence () };
281                         PermissionSet ps = SecurityManager.ResolvePolicy (e);
282                         Assert.IsNotNull (ps, "PermissionSet");
283                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
284                         Assert.AreEqual (0, ps.Count, "Count");
285                 }
286
287                 [Test]
288                 [ExpectedException (typeof (NullReferenceException))]
289                 [Category ("NotWorking")]
290                 public void ResolveSystemPolicy_Null ()
291                 {
292                         SecurityManager.ResolveSystemPolicy (null);
293                 }
294 #endif
295         }
296 }