| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- //
- // SecurityManagerTest.cs - NUnit Test Cases for SecurityManager
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- //
- // (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
- 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
- }
- }
|