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); } }
private void File_GetAccessControl(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); var tempPath = Path.Combine(Path.GetTempPath(), "File.GetAccessControl()-" + Path.GetRandomFileName()); if (isNetwork) { tempPath = Path.LocalToUnc(tempPath); } try { using (File.Create(tempPath)) { } var foundRules = false; var sysIO = System.IO.File.GetAccessControl(tempPath); var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(NTAccount)); var alphaFS = File.GetAccessControl(tempPath); var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(NTAccount)); Console.WriteLine("\nInput File Path: [{0}]", tempPath); Console.WriteLine("\n\tSystem.IO rules found: [{0}]\n\tAlphaFS rules found : [{1}]", sysIOaccessRules.Count, alphaFSaccessRules.Count); Assert.AreEqual(sysIOaccessRules.Count, alphaFSaccessRules.Count); foreach (FileSystemAccessRule far in alphaFSaccessRules) { UnitTestConstants.Dump(far, -17); UnitTestConstants.TestAccessRules(sysIO, alphaFS); foundRules = true; } Assert.IsTrue(foundRules); } catch (Exception ex) { Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, " ")); Assert.IsTrue(false); } finally { File.Delete(tempPath, true); Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed."); } }
private void Directory_GetAccessControl(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); var tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Directory.GetAccessControl()-" + System.IO.Path.GetRandomFileName()); if (isNetwork) { tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath); } try { System.IO.Directory.CreateDirectory(tempPath); var foundRules = false; var sysIO = System.IO.File.GetAccessControl(tempPath); var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); var alphaFS = System.IO.File.GetAccessControl(tempPath); var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); Console.WriteLine("\nInput Directory Path: [{0}]", tempPath); Console.WriteLine("\n\tSystem.IO rules found: [{0}]\n\tAlphaFS rules found : [{1}]", sysIOaccessRules.Count, alphaFSaccessRules.Count); Assert.AreEqual(sysIOaccessRules.Count, alphaFSaccessRules.Count); foreach (var far in alphaFSaccessRules) { UnitTestConstants.Dump(far, -17); UnitTestConstants.TestAccessRules(sysIO, alphaFS); foundRules = true; } Assert.IsTrue(foundRules); } catch (Exception ex) { Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, " ")); Assert.IsTrue(false); } finally { System.IO.Directory.Delete(tempPath, true); Assert.IsFalse(System.IO.Directory.Exists(tempPath), "Cleanup failed: Directory should have been removed."); } Console.WriteLine(); }
private void File_GetAccessControl(bool isNetwork) { using (var tempRoot = new TemporaryDirectory(isNetwork)) { var file = tempRoot.CreateFile(); Console.WriteLine("Input File Path: [{0}]", file.FullName); var foundRules = false; 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); foreach (FileSystemAccessRule far in alphaFSaccessRules) { UnitTestConstants.Dump(far); UnitTestConstants.TestAccessRules(sysIO, alphaFS); foundRules = true; } Assert.IsTrue(foundRules); } }
private void Directory_GetAccessControl(bool isNetwork) { using (var tempRoot = new TemporaryDirectory(isNetwork)) { var folder = tempRoot.CreateDirectory(); Console.WriteLine("Input Directory Path: [{0}]", folder.FullName); var foundRules = false; var sysIO = System.IO.File.GetAccessControl(folder.FullName); var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); var alphaFS = System.IO.File.GetAccessControl(folder.FullName); var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); Console.WriteLine("\n\tSystem.IO rules found: [{0}]\n\tAlphaFS rules found : [{1}]", sysIOaccessRules.Count, alphaFSaccessRules.Count); Assert.AreEqual(sysIOaccessRules.Count, alphaFSaccessRules.Count); foreach (var far in alphaFSaccessRules) { UnitTestConstants.Dump(far, -17); UnitTestConstants.TestAccessRules(sysIO, alphaFS); foundRules = true; } Assert.IsTrue(foundRules); } Console.WriteLine(); }
private void Directory_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 folder = rootDir.RandomFileFullPath; Directory.CreateDirectory(folder); var sysIO = System.IO.Directory.GetAccessControl(folder); var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(NTAccount)); var alphaFS = Directory.GetAccessControl(folder); var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(NTAccount)); Console.WriteLine("\nInput Directory Path: [{0}]", folder); 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); Directory.SetAccessControl(folder, alphaFS, AccessControlSections.Access); // Re-read, using instance methods. var sysIOdi = new System.IO.DirectoryInfo(folder); var alphaFSdi = new DirectoryInfo(folder); sysIO = sysIOdi.GetAccessControl(AccessControlSections.Access); alphaFS = alphaFSdi.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); Directory.SetAccessControl(folder, alphaFS, AccessControlSections.Access); // Re-read. sysIO = System.IO.Directory.GetAccessControl(folder, AccessControlSections.Access); alphaFS = Directory.GetAccessControl(folder, AccessControlSections.Access); // Sanity check. UnitTestConstants.TestAccessRules(sysIO, alphaFS); } Console.WriteLine(); }