1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
//
// SecurityManagerTest.cs - NUnit Test Cases for SecurityManager
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2004 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.Collections;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
namespace MonoTests.System.Security {
[TestFixture]
public class SecurityManagerTest {
static Evidence CurrentEvidence;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
CurrentEvidence = Assembly.GetExecutingAssembly ().Evidence;
}
[TearDown]
public void TearDown ()
{
SecurityManager.CheckExecutionRights = true;
}
[Test]
public void IsGranted_Null ()
{
// null is always granted
Assert.IsTrue (SecurityManager.IsGranted (null));
}
[Test]
#if MOBILE
[ExpectedException (typeof (NotSupportedException))]
#else
[ExpectedException (typeof (ArgumentNullException))]
#endif
public void LoadPolicyLevelFromFile_Null ()
{
SecurityManager.LoadPolicyLevelFromFile (null, PolicyLevelType.AppDomain);
}
[Test]
#if MOBILE
[ExpectedException (typeof (NotSupportedException))]
#else
[ExpectedException (typeof (ArgumentNullException))]
#endif
public void LoadPolicyLevelFromString_Null ()
{
SecurityManager.LoadPolicyLevelFromString (null, PolicyLevelType.AppDomain);
}
[Test]
#if MOBILE
[ExpectedException (typeof (NotSupportedException))]
#endif
public void PolicyHierarchy ()
{
IEnumerator e = SecurityManager.PolicyHierarchy ();
Assert.IsNotNull (e, "PolicyHierarchy");
}
#if !MOBILE && !XAMMAC_4_5
private void ResolveEvidenceHost (SecurityZone zone, bool unrestricted, bool empty)
{
string prefix = zone.ToString () + "-";
Evidence e = new Evidence ();
e.AddHost (new Zone (zone));
PermissionSet ps = SecurityManager.ResolvePolicy (e);
// as 2.0 use Unrestricted for Identity permissions they have no need to be
// kept in resolved permission set
Assert.IsTrue ((unrestricted || (ps.Count > 0)), prefix + "Count");
Assert.AreEqual (empty, ps.IsEmpty (), prefix + "IsEmpty");
Assert.AreEqual (unrestricted, ps.IsUnrestricted (), prefix + "IsUnrestricted");
if (unrestricted)
Assert.IsNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
else
Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
}
[Category ("NotWorking")]
[Test]
public void ResolvePolicy_Evidence_Host_Zone ()
{
ResolveEvidenceHost (SecurityZone.Internet, false, false);
ResolveEvidenceHost (SecurityZone.Intranet, false, false);
ResolveEvidenceHost (SecurityZone.MyComputer, true, false);
ResolveEvidenceHost (SecurityZone.Trusted, false, false);
ResolveEvidenceHost (SecurityZone.Untrusted, false, false);
ResolveEvidenceHost (SecurityZone.NoZone, false, true);
}
private void ResolveEvidenceAssembly (SecurityZone zone)
{
string prefix = zone.ToString () + "-";
Evidence e = new Evidence ();
e.AddAssembly (new Zone (zone));
PermissionSet ps = SecurityManager.ResolvePolicy (e);
Assert.AreEqual (0, ps.Count, prefix + "Count");
Assert.IsTrue (ps.IsEmpty (), prefix + "IsEmpty");
Assert.IsFalse (ps.IsUnrestricted (), prefix + "IsUnrestricted");
}
[Test]
public void ResolvePolicy_Evidence_Assembly_Zone ()
{
ResolveEvidenceAssembly (SecurityZone.Internet);
ResolveEvidenceAssembly (SecurityZone.Intranet);
ResolveEvidenceAssembly (SecurityZone.MyComputer);
ResolveEvidenceAssembly (SecurityZone.Trusted);
ResolveEvidenceAssembly (SecurityZone.Untrusted);
ResolveEvidenceAssembly (SecurityZone.NoZone);
}
[Test]
public void ResolvePolicy_Evidence_Null ()
{
Evidence e = null;
PermissionSet ps = SecurityManager.ResolvePolicy (e);
// no exception thrown
Assert.IsNotNull (ps);
Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
}
[Test]
public void ResolvePolicy_Evidence_CurrentAssembly ()
{
PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence);
Assert.IsNotNull (granted);
Assert.IsTrue (granted.IsUnrestricted (), "IsUnrestricted");
}
[Test]
public void ResolvePolicy_Evidences_Null ()
{
Evidence[] e = null;
PermissionSet ps = SecurityManager.ResolvePolicy (e);
// no exception thrown
Assert.IsNotNull (ps);
Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
}
[Test]
public void ResolvePolicy_Evidence_AllNull_NoExecution ()
{
PermissionSet denied = null;
SecurityManager.CheckExecutionRights = false;
PermissionSet granted = SecurityManager.ResolvePolicy (null, null, null, null, out denied);
Assert.IsNull (denied, "Denied");
Assert.AreEqual (0, granted.Count, "Granted.Count");
Assert.IsFalse (granted.IsUnrestricted (), "!Granted.IsUnrestricted");
}
[Test]
public void ResolvePolicy_Evidence_NullRequests_CurrentAssembly ()
{
PermissionSet denied = null;
PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, null, out denied);
Assert.IsNull (denied, "Denied");
Assert.IsTrue (granted.IsUnrestricted (), "Granted.IsUnrestricted");
}
[Test]
[ExpectedException (typeof (PolicyException))]
[Category ("NotWorking")]
public void ResolvePolicy_Evidence_DenyUnrestricted_CurrentAssembly ()
{
PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
PermissionSet denied = null;
PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
}
[Test]
[Category ("NotDotNet")] // MS bug - throws a NullReferenceException
public void ResolvePolicy_Evidence_DenyUnrestricted_NoExecution ()
{
PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
PermissionSet denied = null;
SecurityManager.CheckExecutionRights = false;
PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ResolvePolicyGroups_Null ()
{
IEnumerator e = SecurityManager.ResolvePolicyGroups (null);
}
[Test]
[ExpectedException (typeof (NullReferenceException))]
public void SavePolicyLevel_Null ()
{
SecurityManager.SavePolicyLevel (null);
}
[Test]
[ExpectedException (typeof (PolicyException))]
public void SavePolicyLevel_AppDomain ()
{
PolicyLevel adl = PolicyLevel.CreateAppDomainLevel ();
SecurityManager.SavePolicyLevel (adl);
}
[Test]
public void GetZoneAndOrigin ()
{
ArrayList zone = null;
ArrayList origin = null;
SecurityManager.GetZoneAndOrigin (out zone, out origin);
Assert.IsNotNull (zone, "Zone");
Assert.AreEqual (0, zone.Count, "Zone.Count");
Assert.IsNotNull (origin, "Origin");
Assert.AreEqual (0, origin.Count, "Origin.Count");
}
[Test]
public void ResolvePolicy_Evidence_ArrayNull ()
{
Evidence[] e = null;
PermissionSet ps = SecurityManager.ResolvePolicy (e);
Assert.IsNotNull (ps, "PermissionSet");
Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
Assert.AreEqual (0, ps.Count, "Count");
}
[Test]
public void ResolvePolicy_Evidence_ArrayEmpty ()
{
Evidence[] e = new Evidence [0];
PermissionSet ps = SecurityManager.ResolvePolicy (e);
Assert.IsNotNull (ps, "PermissionSet");
Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
Assert.AreEqual (0, ps.Count, "Count");
}
[Test]
public void ResolvePolicy_Evidence_Array ()
{
Evidence[] e = new Evidence[] { new Evidence () };
PermissionSet ps = SecurityManager.ResolvePolicy (e);
Assert.IsNotNull (ps, "PermissionSet");
Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
Assert.AreEqual (0, ps.Count, "Count");
}
[Test]
[ExpectedException (typeof (NullReferenceException))]
[Category ("NotWorking")]
public void ResolveSystemPolicy_Null ()
{
SecurityManager.ResolveSystemPolicy (null);
}
#endif
}
}
|