| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- // CommonSecurityDescriptorTest.cs - NUnit Test Cases for CommonSecurityDescriptor
- //
- // Authors:
- // James Bellinger <[email protected]>
- //
- // Copyright (C) 2012 James Bellinger
- using System;
- using System.Collections.Generic;
- using System.Security.AccessControl;
- using System.Security.Principal;
- using NUnit.Framework;
- namespace MonoTests.System.Security.AccessControl
- {
- [TestFixture]
- public class CommonSecurityDescriptorTest
- {
- [Test]
- public void DefaultOwnerAndGroup ()
- {
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, null, null, null, null);
- Assert.IsNull (csd.Owner);
- Assert.IsNull (csd.Group);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.SelfRelative, csd.ControlFlags);
- }
- [Test]
- public void GetBinaryForm ()
- {
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, null, null, null, null);
- Assert.AreEqual (20, csd.BinaryLength);
- byte[] binaryForm = new byte[csd.BinaryLength];
- csd.GetBinaryForm (binaryForm, 0);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent | ControlFlags.SelfRelative,
- csd.ControlFlags);
- // The default 'Allow Everyone Full Access' serializes as NOT having a
- // DiscretionaryAcl, as the above demonstrates (byte 3 is 0 not 4).
- Assert.AreEqual (new byte[20] {
- 1, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- }, binaryForm);
- // Changing SystemAcl protection does nothing special.
- csd.SetSystemAclProtection (true, true);
- Assert.AreEqual (20, csd.BinaryLength);
- // Modifying the DiscretionaryAcl (even effective no-ops like this) causes serialization.
- csd.SetDiscretionaryAclProtection (false, true);
- Assert.AreEqual (48, csd.BinaryLength);
- }
- [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void GetBinaryFormOffset ()
- {
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, null, null, null, null);
- csd.GetBinaryForm (new byte[csd.BinaryLength], 1);
- }
- [Test, ExpectedException (typeof (ArgumentNullException))]
- public void GetBinaryFormNull ()
- {
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, null, null, null, null);
- csd.GetBinaryForm (null, 0);
- }
- [Test]
- public void AefaModifiedFlagIsStoredOnDiscretionaryAcl ()
- {
- CommonSecurityDescriptor csd1, csd2;
- // Incidentally this shows the DiscretionaryAcl is NOT cloned.
- csd1 = new CommonSecurityDescriptor (false, false, ControlFlags.None, null, null, null, null);
- csd2 = new CommonSecurityDescriptor (false, false, ControlFlags.None, null, null, null, csd1.DiscretionaryAcl);
- Assert.AreSame (csd1.DiscretionaryAcl, csd2.DiscretionaryAcl);
- Assert.AreEqual ("", csd1.GetSddlForm (AccessControlSections.Access));
- csd2.SetDiscretionaryAclProtection (false, true);
- Assert.AreEqual ("D:(A;;0xffffffff;;;WD)", csd1.GetSddlForm (AccessControlSections.Access));
- Assert.AreEqual ("D:(A;;0xffffffff;;;WD)", csd2.GetSddlForm (AccessControlSections.Access));
- }
- [Test]
- public void AefaRoundtrip ()
- {
- CommonSecurityDescriptor csd;
- csd = new CommonSecurityDescriptor (false, false, ControlFlags.None, null, null, null, null);
- Assert.AreEqual (20, csd.BinaryLength);
- byte[] binaryForm1 = new byte[csd.BinaryLength];
- csd.GetBinaryForm (binaryForm1, 0);
- csd = new CommonSecurityDescriptor (false, false, new RawSecurityDescriptor (binaryForm1, 0));
- byte[] binaryForm2 = new byte[csd.BinaryLength];
- csd.GetBinaryForm (binaryForm2, 0);
- Assert.AreEqual (binaryForm1, binaryForm2);
- }
- [Test]
- public void GetSddlFormAefaRemovesDacl ()
- {
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, null, null, null, null);
- Assert.AreEqual (1, csd.DiscretionaryAcl.Count);
- Assert.AreEqual ("", csd.GetSddlForm (AccessControlSections.Access));
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.SelfRelative,
- csd.ControlFlags);
- Assert.AreSame (csd.DiscretionaryAcl, csd.DiscretionaryAcl);
- Assert.AreNotSame (csd.DiscretionaryAcl[0], csd.DiscretionaryAcl[0]);
- Assert.AreEqual ("", csd.GetSddlForm (AccessControlSections.Access));
- csd.SetDiscretionaryAclProtection (false, true);
- Assert.AreEqual ("D:(A;;0xffffffff;;;WD)", csd.GetSddlForm (AccessControlSections.Access));
- Assert.AreSame (csd.DiscretionaryAcl, csd.DiscretionaryAcl);
- Assert.AreNotSame (csd.DiscretionaryAcl[0], csd.DiscretionaryAcl[0]);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.SelfRelative,
- csd.ControlFlags);
- csd.SetDiscretionaryAclProtection (true, true);
- Assert.AreEqual (1, csd.DiscretionaryAcl.Count);
- Assert.AreEqual ("D:P(A;;0xffffffff;;;WD)", csd.GetSddlForm (AccessControlSections.Access));
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.DiscretionaryAclProtected
- | ControlFlags.SelfRelative,
- csd.ControlFlags);
- csd.SetDiscretionaryAclProtection (false, false);
- Assert.AreEqual (1, csd.DiscretionaryAcl.Count);
- Assert.AreEqual ("D:(A;;0xffffffff;;;WD)", csd.GetSddlForm (AccessControlSections.Access));
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.SelfRelative,
- csd.ControlFlags);
- }
- [Test, ExpectedException (typeof (ArgumentException))]
- public void ContainerAndDSConsistencyEnforcedA ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier (WellKnownSidType.LocalSystemSid, null);
- SecurityIdentifier groupSid = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
- DiscretionaryAcl dacl = new DiscretionaryAcl (true, true, 0);
- new CommonSecurityDescriptor (true, false, ControlFlags.None, userSid, groupSid, null, dacl);
- }
- [Test, ExpectedException (typeof (ArgumentException))]
- public void ContainerAndDSConsistencyEnforcedB ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier (WellKnownSidType.LocalSystemSid, null);
- SecurityIdentifier groupSid = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
- SystemAcl sacl = new SystemAcl (false, false, 0);
- new CommonSecurityDescriptor (true, false, ControlFlags.None, userSid, groupSid, sacl, null);
- }
- [Test, ExpectedException (typeof (ArgumentException))]
- public void ContainerAndDSConsistencyEnforcedInSetter ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier (WellKnownSidType.LocalSystemSid, null);
- SecurityIdentifier groupSid = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (true, false, ControlFlags.None, userSid, groupSid, null, null);
- csd.DiscretionaryAcl = new DiscretionaryAcl (true, true, 0);
- }
- [Test]
- public void DefaultDaclIsAllowEveryoneFullAccess ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier ("SY");
- SecurityIdentifier groupSid = new SecurityIdentifier ("BA");
- SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
- CommonSecurityDescriptor csd; DiscretionaryAcl dacl; CommonAce ace;
- csd = new CommonSecurityDescriptor (false, false, ControlFlags.None, userSid, groupSid, null, null);
- dacl = csd.DiscretionaryAcl;
- Assert.AreEqual (1, dacl.Count);
- ace = (CommonAce)dacl [0];
- Assert.AreEqual (-1, ace.AccessMask);
- Assert.AreEqual (AceFlags.None, ace.AceFlags);
- Assert.AreEqual (AceType.AccessAllowed, ace.AceType);
- Assert.AreEqual (20, ace.BinaryLength);
- Assert.IsFalse (ace.IsCallback);
- Assert.IsFalse (ace.IsInherited);
- Assert.AreEqual (0, ace.OpaqueLength);
- Assert.AreEqual (ace.SecurityIdentifier, everyoneSid);
- csd = new CommonSecurityDescriptor (true, false, ControlFlags.None, userSid, groupSid, null, null);
- dacl = csd.DiscretionaryAcl;
- Assert.AreEqual (1, dacl.Count);
- ace = (CommonAce)dacl [0];
- Assert.AreEqual (-1, ace.AccessMask);
- Assert.AreEqual (AceFlags.ObjectInherit | AceFlags.ContainerInherit, ace.AceFlags);
- Assert.AreEqual (AceType.AccessAllowed, ace.AceType);
- Assert.AreEqual (20, ace.BinaryLength);
- Assert.IsFalse (ace.IsCallback);
- Assert.IsFalse (ace.IsInherited);
- Assert.AreEqual (0, ace.OpaqueLength);
- Assert.AreEqual (ace.SecurityIdentifier, everyoneSid);
- }
- [Test]
- public void PurgeDefaultDacl ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier ("SY");
- SecurityIdentifier groupSid = new SecurityIdentifier ("BA");
- SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, null, null);
- DiscretionaryAcl dacl = csd.DiscretionaryAcl;
- Assert.AreEqual (1, dacl.Count);
- csd.PurgeAccessControl (userSid);
- Assert.AreEqual (1, dacl.Count);
- csd.PurgeAccessControl (everyoneSid);
- Assert.AreEqual (0, dacl.Count);
- }
- [Test]
- public void PurgeNullSaclWithoutError ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier ("SY");
- SecurityIdentifier groupSid = new SecurityIdentifier ("BA");
- CommonSecurityDescriptor csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, null, null);
- csd.PurgeAudit (userSid);
- Assert.IsNull (csd.SystemAcl);
- }
- [Test]
- public void OwnerAndGroupAreReferences ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier (WellKnownSidType.LocalSystemSid, null);
- SecurityIdentifier groupSid = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
- CommonSecurityDescriptor csd;
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, null, null);
- Assert.AreSame (groupSid, csd.Group);
- Assert.AreSame (userSid, csd.Owner);
- }
- [Test]
- public void ProtectionChangesFlags ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier (WellKnownSidType.LocalSystemSid, null);
- SecurityIdentifier groupSid = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
- CommonSecurityDescriptor csd;
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, null, null);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.SelfRelative, csd.ControlFlags);
- csd.SetDiscretionaryAclProtection (true, false);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.DiscretionaryAclProtected
- | ControlFlags.SelfRelative, csd.ControlFlags);
- csd.SetSystemAclProtection (true, false); // despite not being *present*
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.DiscretionaryAclProtected
- | ControlFlags.SystemAclProtected
- | ControlFlags.SelfRelative, csd.ControlFlags);
- }
- [Test]
- public void ProtectionPreserveInheritanceIgnoredUnlessProtectedTrue ()
- {
- CommonSecurityDescriptor descriptor;
- descriptor = ProtectionPreserveInheritanceIgnoredUnlessProtectedTrueDescriptor();
- Assert.AreEqual (2, descriptor.DiscretionaryAcl.Count);
- descriptor = ProtectionPreserveInheritanceIgnoredUnlessProtectedTrueDescriptor();
- descriptor.SetDiscretionaryAclProtection (true, false);
- Assert.AreEqual (1, descriptor.DiscretionaryAcl.Count);
- descriptor = ProtectionPreserveInheritanceIgnoredUnlessProtectedTrueDescriptor();
- descriptor.SetDiscretionaryAclProtection (false, false);
- Assert.AreEqual (2, descriptor.DiscretionaryAcl.Count);
- descriptor = ProtectionPreserveInheritanceIgnoredUnlessProtectedTrueDescriptor();
- descriptor.SetDiscretionaryAclProtection (true, true);
- Assert.AreEqual (2, descriptor.DiscretionaryAcl.Count);
- descriptor.SetDiscretionaryAclProtection (false, false);
- Assert.AreEqual (2, descriptor.DiscretionaryAcl.Count);
- descriptor.SetDiscretionaryAclProtection (false, true);
- Assert.AreEqual (2, descriptor.DiscretionaryAcl.Count);
- descriptor.SetDiscretionaryAclProtection (true, false);
- Assert.AreEqual (1, descriptor.DiscretionaryAcl.Count);
- }
- static CommonSecurityDescriptor ProtectionPreserveInheritanceIgnoredUnlessProtectedTrueDescriptor()
- {
- SecurityIdentifier sid = new SecurityIdentifier ("WD");
- RawAcl acl = new RawAcl (GenericAcl.AclRevision, 1);
- acl.InsertAce (0, new CommonAce (AceFlags.None, AceQualifier.AccessDenied, 1, sid, false, null));
- acl.InsertAce (1, new CommonAce (AceFlags.Inherited, AceQualifier.AccessAllowed, 1, sid, false, null));
- CommonSecurityDescriptor descriptor = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, null, null, null, null);
- descriptor.DiscretionaryAcl = new DiscretionaryAcl (false, false, acl);
- return descriptor;
- }
- [Test]
- public void DaclPresent ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier (WellKnownSidType.LocalSystemSid, null);
- SecurityIdentifier groupSid = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
- CommonSecurityDescriptor csd;
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, null, null);
- Assert.IsNotNull (csd.DiscretionaryAcl);
- Assert.IsTrue (csd.IsDiscretionaryAclCanonical);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent | ControlFlags.SelfRelative, csd.ControlFlags);
- Assert.AreEqual (1, csd.DiscretionaryAcl.Count);
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.DiscretionaryAclPresent, userSid, groupSid, null, null);
- Assert.IsNotNull (csd.DiscretionaryAcl);
- Assert.IsTrue (csd.IsDiscretionaryAclCanonical);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent | ControlFlags.SelfRelative, csd.ControlFlags);
- DiscretionaryAcl dacl = new DiscretionaryAcl (false, false, 0);
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, null, dacl);
- Assert.AreSame (dacl, csd.DiscretionaryAcl);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent | ControlFlags.SelfRelative, csd.ControlFlags);
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.DiscretionaryAclPresent, userSid, groupSid, null, dacl);
- Assert.AreSame (dacl, csd.DiscretionaryAcl);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent | ControlFlags.SelfRelative, csd.ControlFlags);
- }
- [Test]
- public void SaclPresent ()
- {
- SecurityIdentifier userSid = new SecurityIdentifier (WellKnownSidType.LocalSystemSid, null);
- SecurityIdentifier groupSid = new SecurityIdentifier (WellKnownSidType.BuiltinAdministratorsSid, null);
- SystemAcl sacl = new SystemAcl (false, false, 0);
- CommonSecurityDescriptor csd;
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, null, null);
- Assert.IsNull (csd.SystemAcl);
- Assert.IsTrue (csd.IsSystemAclCanonical);
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.SystemAclPresent, userSid, groupSid, null, null);
- Assert.IsNull (csd.SystemAcl);
- Assert.IsTrue (csd.IsSystemAclCanonical);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent | ControlFlags.SelfRelative, csd.ControlFlags);
- csd = new CommonSecurityDescriptor
- (false, false, ControlFlags.None, userSid, groupSid, sacl, null);
- Assert.AreSame (sacl, csd.SystemAcl);
- Assert.IsTrue (csd.IsSystemAclCanonical);
- Assert.AreEqual (0, csd.SystemAcl.Count);
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.SystemAclPresent
- | ControlFlags.SelfRelative, csd.ControlFlags);
- csd.SystemAcl = null;
- Assert.AreEqual (ControlFlags.DiscretionaryAclPresent
- | ControlFlags.SelfRelative, csd.ControlFlags);
- }
- }
- }
|