2 // SecurityManagerTest.cs - NUnit Test Cases for SecurityManager
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2004 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.Collections;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Security.Policy;
38 namespace MonoTests.System.Security {
41 public class SecurityManagerTest {
43 static Evidence CurrentEvidence;
46 public void FixtureSetUp ()
48 CurrentEvidence = Assembly.GetExecutingAssembly ().Evidence;
52 public void TearDown ()
54 SecurityManager.CheckExecutionRights = true;
58 public void IsGranted_Null ()
60 // null is always granted
61 Assert.IsTrue (SecurityManager.IsGranted (null));
66 [ExpectedException (typeof (NotSupportedException))]
68 [ExpectedException (typeof (ArgumentNullException))]
70 public void LoadPolicyLevelFromFile_Null ()
72 SecurityManager.LoadPolicyLevelFromFile (null, PolicyLevelType.AppDomain);
77 [ExpectedException (typeof (NotSupportedException))]
79 [ExpectedException (typeof (ArgumentNullException))]
81 public void LoadPolicyLevelFromString_Null ()
83 SecurityManager.LoadPolicyLevelFromString (null, PolicyLevelType.AppDomain);
88 [ExpectedException (typeof (NotSupportedException))]
90 public void PolicyHierarchy ()
92 IEnumerator e = SecurityManager.PolicyHierarchy ();
93 Assert.IsNotNull (e, "PolicyHierarchy");
97 private void ResolveEvidenceHost (SecurityZone zone, bool unrestricted, bool empty)
99 string prefix = zone.ToString () + "-";
100 Evidence e = new Evidence ();
101 e.AddHost (new Zone (zone));
102 PermissionSet ps = SecurityManager.ResolvePolicy (e);
103 // as 2.0 use Unrestricted for Identity permissions they have no need to be
104 // kept in resolved permission set
105 Assert.IsTrue ((unrestricted || (ps.Count > 0)), prefix + "Count");
106 Assert.AreEqual (empty, ps.IsEmpty (), prefix + "IsEmpty");
107 Assert.AreEqual (unrestricted, ps.IsUnrestricted (), prefix + "IsUnrestricted");
109 Assert.IsNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
111 Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
114 [Category ("NotWorking")]
116 public void ResolvePolicy_Evidence_Host_Zone ()
118 ResolveEvidenceHost (SecurityZone.Internet, false, false);
119 ResolveEvidenceHost (SecurityZone.Intranet, false, false);
120 ResolveEvidenceHost (SecurityZone.MyComputer, true, false);
121 ResolveEvidenceHost (SecurityZone.Trusted, false, false);
122 ResolveEvidenceHost (SecurityZone.Untrusted, false, false);
123 ResolveEvidenceHost (SecurityZone.NoZone, false, true);
126 private void ResolveEvidenceAssembly (SecurityZone zone)
128 string prefix = zone.ToString () + "-";
129 Evidence e = new Evidence ();
130 e.AddAssembly (new Zone (zone));
131 PermissionSet ps = SecurityManager.ResolvePolicy (e);
132 Assert.AreEqual (0, ps.Count, prefix + "Count");
133 Assert.IsTrue (ps.IsEmpty (), prefix + "IsEmpty");
134 Assert.IsFalse (ps.IsUnrestricted (), prefix + "IsUnrestricted");
138 public void ResolvePolicy_Evidence_Assembly_Zone ()
140 ResolveEvidenceAssembly (SecurityZone.Internet);
141 ResolveEvidenceAssembly (SecurityZone.Intranet);
142 ResolveEvidenceAssembly (SecurityZone.MyComputer);
143 ResolveEvidenceAssembly (SecurityZone.Trusted);
144 ResolveEvidenceAssembly (SecurityZone.Untrusted);
145 ResolveEvidenceAssembly (SecurityZone.NoZone);
149 public void ResolvePolicy_Evidence_Null ()
152 PermissionSet ps = SecurityManager.ResolvePolicy (e);
153 // no exception thrown
154 Assert.IsNotNull (ps);
155 Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
159 public void ResolvePolicy_Evidence_CurrentAssembly ()
161 PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence);
162 Assert.IsNotNull (granted);
163 Assert.IsTrue (granted.IsUnrestricted (), "IsUnrestricted");
167 public void ResolvePolicy_Evidences_Null ()
170 PermissionSet ps = SecurityManager.ResolvePolicy (e);
171 // no exception thrown
172 Assert.IsNotNull (ps);
173 Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
177 public void ResolvePolicy_Evidence_AllNull_NoExecution ()
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");
188 public void ResolvePolicy_Evidence_NullRequests_CurrentAssembly ()
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");
197 [ExpectedException (typeof (PolicyException))]
198 [Category ("NotWorking")]
199 public void ResolvePolicy_Evidence_DenyUnrestricted_CurrentAssembly ()
201 PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
202 PermissionSet denied = null;
203 PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
207 [Category ("NotDotNet")] // MS bug - throws a NullReferenceException
208 public void ResolvePolicy_Evidence_DenyUnrestricted_NoExecution ()
210 PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
211 PermissionSet denied = null;
212 SecurityManager.CheckExecutionRights = false;
213 PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
217 [ExpectedException (typeof (ArgumentNullException))]
218 public void ResolvePolicyGroups_Null ()
220 IEnumerator e = SecurityManager.ResolvePolicyGroups (null);
224 [ExpectedException (typeof (NullReferenceException))]
225 public void SavePolicyLevel_Null ()
227 SecurityManager.SavePolicyLevel (null);
231 [ExpectedException (typeof (PolicyException))]
232 public void SavePolicyLevel_AppDomain ()
234 PolicyLevel adl = PolicyLevel.CreateAppDomainLevel ();
235 SecurityManager.SavePolicyLevel (adl);
239 public void GetZoneAndOrigin ()
241 ArrayList zone = null;
242 ArrayList origin = null;
243 SecurityManager.GetZoneAndOrigin (out zone, out origin);
244 Assert.IsNotNull (zone, "Zone");
245 Assert.AreEqual (0, zone.Count, "Zone.Count");
246 Assert.IsNotNull (origin, "Origin");
247 Assert.AreEqual (0, origin.Count, "Origin.Count");
251 public void ResolvePolicy_Evidence_ArrayNull ()
254 PermissionSet ps = SecurityManager.ResolvePolicy (e);
255 Assert.IsNotNull (ps, "PermissionSet");
256 Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
257 Assert.AreEqual (0, ps.Count, "Count");
261 public void ResolvePolicy_Evidence_ArrayEmpty ()
263 Evidence[] e = new Evidence [0];
264 PermissionSet ps = SecurityManager.ResolvePolicy (e);
265 Assert.IsNotNull (ps, "PermissionSet");
266 Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
267 Assert.AreEqual (0, ps.Count, "Count");
271 public void ResolvePolicy_Evidence_Array ()
273 Evidence[] e = new Evidence[] { new Evidence () };
274 PermissionSet ps = SecurityManager.ResolvePolicy (e);
275 Assert.IsNotNull (ps, "PermissionSet");
276 Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
277 Assert.AreEqual (0, ps.Count, "Count");
281 [ExpectedException (typeof (NullReferenceException))]
282 [Category ("NotWorking")]
283 public void ResolveSystemPolicy_Null ()
285 SecurityManager.ResolveSystemPolicy (null);