Ejemplo n.º 1
0
        private void File_SetAccessControl(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var file = tempRoot.CreateFile();

                Console.WriteLine("Input File Path: [{0}]", file.FullName);


                var sysIO            = System.IO.File.GetAccessControl(file.FullName);
                var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(NTAccount));


                var alphaFS            = Alphaleonis.Win32.Filesystem.File.GetAccessControl(file.FullName);
                var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(NTAccount));


                Console.WriteLine("\n\tSystem.IO rules found: [{0}]\n\tAlphaFS rules found  : [{1}]", sysIOaccessRules.Count, alphaFSaccessRules.Count);


                Assert.AreEqual(sysIOaccessRules.Count, alphaFSaccessRules.Count);


                // Sanity check.
                UnitTestConstants.TestAccessRules(sysIO, alphaFS);


                // Remove inherited properties.
                // Passing true for first parameter protects the new permission from inheritance,
                // and second parameter removes the existing inherited permissions
                Console.WriteLine("\n\tRemove inherited properties and persist it.");
                alphaFS.SetAccessRuleProtection(true, false);
                Alphaleonis.Win32.Filesystem.File.SetAccessControl(file.FullName, alphaFS, AccessControlSections.Access);


                // Re-read, using instance methods.
                var sysIOfi   = new System.IO.FileInfo(file.FullName);
                var alphaFSfi = new Alphaleonis.Win32.Filesystem.FileInfo(file.FullName);

                sysIO   = sysIOfi.GetAccessControl(AccessControlSections.Access);
                alphaFS = alphaFSfi.GetAccessControl(AccessControlSections.Access);

                // Sanity check.
                UnitTestConstants.TestAccessRules(sysIO, alphaFS);


                // Restore inherited properties.
                Console.WriteLine("\n\tRestore inherited properties and persist it.");
                alphaFS.SetAccessRuleProtection(false, true);
                Alphaleonis.Win32.Filesystem.File.SetAccessControl(file.FullName, alphaFS, AccessControlSections.Access);


                // Re-read.
                sysIO   = System.IO.File.GetAccessControl(file.FullName, AccessControlSections.Access);
                alphaFS = Alphaleonis.Win32.Filesystem.File.GetAccessControl(file.FullName, AccessControlSections.Access);

                // Sanity check.
                UnitTestConstants.TestAccessRules(sysIO, alphaFS);
            }
        }
Ejemplo n.º 2
0
        private void File_SetAccessControl(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "File.SetAccessControl"))
            {
                var file = rootDir.RandomFileFullPath + ".txt";


                using (System.IO.File.Create(file)) {}

                var sysIO            = System.IO.File.GetAccessControl(file);
                var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(NTAccount));

                var alphaFS            = Alphaleonis.Win32.Filesystem.File.GetAccessControl(file);
                var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(NTAccount));


                Console.WriteLine("\nInput File Path: [{0}]", file);
                Console.WriteLine("\n\tSystem.IO rules found: [{0}]\n\tAlphaFS rules found  : [{1}]", sysIOaccessRules.Count, alphaFSaccessRules.Count);
                Assert.AreEqual(sysIOaccessRules.Count, alphaFSaccessRules.Count);


                // Sanity check.
                UnitTestConstants.TestAccessRules(sysIO, alphaFS);


                // Remove inherited properties.
                // Passing true for first parameter protects the new permission from inheritance,
                // and second parameter removes the existing inherited permissions
                Console.WriteLine("\n\tRemove inherited properties and persist it.");
                alphaFS.SetAccessRuleProtection(true, false);
                Alphaleonis.Win32.Filesystem.File.SetAccessControl(file, alphaFS, AccessControlSections.Access);


                // Re-read, using instance methods.
                var sysIOfi   = new System.IO.FileInfo(file);
                var alphaFSfi = new Alphaleonis.Win32.Filesystem.FileInfo(file);

                sysIO   = sysIOfi.GetAccessControl(AccessControlSections.Access);
                alphaFS = alphaFSfi.GetAccessControl(AccessControlSections.Access);

                // Sanity check.
                UnitTestConstants.TestAccessRules(sysIO, alphaFS);


                // Restore inherited properties.
                Console.WriteLine("\n\tRestore inherited properties and persist it.");
                alphaFS.SetAccessRuleProtection(false, true);
                Alphaleonis.Win32.Filesystem.File.SetAccessControl(file, alphaFS, AccessControlSections.Access);


                // Re-read.
                sysIO   = System.IO.File.GetAccessControl(file, AccessControlSections.Access);
                alphaFS = Alphaleonis.Win32.Filesystem.File.GetAccessControl(file, AccessControlSections.Access);

                // Sanity check.
                UnitTestConstants.TestAccessRules(sysIO, alphaFS);
            }
        }
 public override FileSecurity GetAccessControl()
 {
     return(instance.GetAccessControl());
 }