**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Security / SecurityManagerTest.cs
blobdd96c3cf60359c219c564bd859509264ea494cb9
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.
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;
38 namespace MonoTests.System.Security {
40 [TestFixture]
41 public class SecurityManagerTest {
43 static Evidence CurrentEvidence;
45 [TestFixtureSetUp]
46 public void FixtureSetUp ()
48 CurrentEvidence = Assembly.GetExecutingAssembly ().Evidence;
51 [TearDown]
52 public void TearDown ()
54 SecurityManager.CheckExecutionRights = true;
57 [Test]
58 public void IsGranted_Null ()
60 // null is always granted
61 Assert.IsTrue (SecurityManager.IsGranted (null));
64 [Test]
65 [ExpectedException (typeof (ArgumentNullException))]
66 public void LoadPolicyLevelFromFile_Null ()
68 SecurityManager.LoadPolicyLevelFromFile (null, PolicyLevelType.AppDomain);
71 [Test]
72 [ExpectedException (typeof (ArgumentNullException))]
73 public void LoadPolicyLevelFromString_Null ()
75 SecurityManager.LoadPolicyLevelFromString (null, PolicyLevelType.AppDomain);
78 [Test]
79 public void PolicyHierarchy ()
81 IEnumerator e = SecurityManager.PolicyHierarchy ();
82 Assert.IsNotNull (e, "PolicyHierarchy");
85 private void ResolveEvidenceHost (SecurityZone zone, bool unrestricted, bool empty)
87 string prefix = zone.ToString () + "-";
88 Evidence e = new Evidence ();
89 e.AddHost (new Zone (zone));
90 PermissionSet ps = SecurityManager.ResolvePolicy (e);
91 Assert.IsTrue (ps.Count > 0, prefix + "Count");
92 Assert.AreEqual (empty, ps.IsEmpty (), prefix + "IsEmpty");
93 Assert.AreEqual (unrestricted, ps.IsUnrestricted (), prefix + "IsUnrestricted");
94 Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
97 [Test]
98 public void ResolvePolicy_Evidence_Host_Zone ()
100 ResolveEvidenceHost (SecurityZone.Internet, false, false);
101 ResolveEvidenceHost (SecurityZone.Intranet, false, false);
102 ResolveEvidenceHost (SecurityZone.MyComputer, true, false);
103 ResolveEvidenceHost (SecurityZone.Trusted, false, false);
104 ResolveEvidenceHost (SecurityZone.Untrusted, false, false);
105 ResolveEvidenceHost (SecurityZone.NoZone, false, true);
108 private void ResolveEvidenceAssembly (SecurityZone zone)
110 string prefix = zone.ToString () + "-";
111 Evidence e = new Evidence ();
112 e.AddAssembly (new Zone (zone));
113 PermissionSet ps = SecurityManager.ResolvePolicy (e);
114 Assert.AreEqual (0, ps.Count, prefix + "Count");
115 Assert.IsTrue (ps.IsEmpty (), prefix + "IsEmpty");
116 Assert.IsFalse (ps.IsUnrestricted (), prefix + "IsUnrestricted");
119 [Test]
120 public void ResolvePolicy_Evidence_Assembly_Zone ()
122 ResolveEvidenceAssembly (SecurityZone.Internet);
123 ResolveEvidenceAssembly (SecurityZone.Intranet);
124 ResolveEvidenceAssembly (SecurityZone.MyComputer);
125 ResolveEvidenceAssembly (SecurityZone.Trusted);
126 ResolveEvidenceAssembly (SecurityZone.Untrusted);
127 ResolveEvidenceAssembly (SecurityZone.NoZone);
130 [Test]
131 public void ResolvePolicy_Evidence_Null ()
133 Evidence e = null;
134 PermissionSet ps = SecurityManager.ResolvePolicy (e);
135 // no exception thrown
136 Assert.IsNotNull (ps);
137 Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
140 [Test]
141 public void ResolvePolicy_Evidence_CurrentAssembly ()
143 PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence);
144 Assert.IsNotNull (granted);
145 Assert.IsTrue (granted.IsUnrestricted (), "IsUnrestricted");
148 #if NET_2_0
149 [Test]
150 public void ResolvePolicy_Evidences_Null ()
152 Evidence[] e = null;
153 PermissionSet ps = SecurityManager.ResolvePolicy (e);
154 // no exception thrown
155 Assert.IsNotNull (ps);
156 Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
158 #endif
159 [Test]
160 [ExpectedException (typeof (PolicyException))]
161 public void ResolvePolicy_Evidence_AllNull ()
163 PermissionSet denied = null;
164 SecurityManager.ResolvePolicy (null, null, null, null, out denied);
165 // null is missing the Execution right
168 [Test]
169 public void ResolvePolicy_Evidence_AllNull_NoExecution ()
171 PermissionSet denied = null;
172 SecurityManager.CheckExecutionRights = false;
173 PermissionSet granted = SecurityManager.ResolvePolicy (null, null, null, null, out denied);
174 Assert.IsNull (denied, "Denied");
175 Assert.AreEqual (0, granted.Count, "Granted.Count");
176 Assert.IsFalse (granted.IsUnrestricted (), "!Granted.IsUnrestricted");
179 [Test]
180 public void ResolvePolicy_Evidence_NullRequests_CurrentAssembly ()
182 PermissionSet denied = null;
183 PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, null, out denied);
184 Assert.IsNull (denied, "Denied");
185 Assert.IsTrue (granted.IsUnrestricted (), "Granted.IsUnrestricted");
188 [Test]
189 #if NET_2_0
190 [ExpectedException (typeof (PolicyException))]
191 #endif
192 public void ResolvePolicy_Evidence_DenyUnrestricted_CurrentAssembly ()
194 PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
195 PermissionSet denied = null;
196 PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
197 // doing this we denied the Execution right
198 #if !NET_2_0
199 Assert.AreEqual (0, denied.Count, "Denied");
200 Assert.IsTrue (granted.IsUnrestricted (), "Granted.IsUnrestricted");
201 #endif
204 [Test]
205 [Ignore ("MS bug - throws a NullReferenceException")]
206 public void ResolvePolicy_Evidence_DenyUnrestricted_NoExecution ()
208 PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
209 PermissionSet denied = null;
210 SecurityManager.CheckExecutionRights = false;
211 PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
214 [Test]
215 [ExpectedException (typeof (ArgumentNullException))]
216 public void ResolvePolicyGroups_Null ()
218 IEnumerator e = SecurityManager.ResolvePolicyGroups (null);
221 [Test]
222 [ExpectedException (typeof (NullReferenceException))]
223 public void SavePolicyLevel_Null ()
225 SecurityManager.SavePolicyLevel (null);
228 [Test]
229 [ExpectedException (typeof (PolicyException))]
230 public void SavePolicyLevel_AppDomain ()
232 PolicyLevel adl = PolicyLevel.CreateAppDomainLevel ();
233 SecurityManager.SavePolicyLevel (adl);