Add directory creation method that takes an ACL (dotnet/corefx#41834)

* First commit

* Fix OperationCanceledException dependency error, revert nullable indication

* Only Directory files

* Unit tests verifications

* Split FileSystem.Windows into two additional files instead of one. Fix pinning.

* Remove dead comment, add actual UT verifications

* User WellKnownSidType

* Make method unsafe, nit comments addressed.

* Remove irrelevant comment

* Add additional exceptions to triple slash

* Add unit tests for additional exceptions

* Remove FileSystem.cs, it has one obsolete method. Replace all calls of that removed method.

* Second pass on required strings.resx resources

* Remove unused dependency cs files

* Move shared files to Common

* Add the missing braces

* Add nulcheck for this argument, add exception, document exception in triple slash, add unit test.

* Fix netstandard CI build failure

* Fix Microsoft.IO.Redist build issue with NetStandard

* Remove unnecessary assert in ut

* Fix build problem in Windows Packaging All Configurations x64_Debug, System.Runtime needs to be included to recognize NotNullIfNotNullAttribute, consumed in Path.Internal.cs

* Additional file splitting and dependency addition to fix netstandard/netfx build problems

* Fix netstandard and netcoreapp build issues

* Remove unused file

* nit cleaning

* Minimal unit tests

* Remove unused unit test code and spacing fix

* Add full framework condition to unit test

* Additional fix for full framework


Commit migrated from https://github.com/dotnet/corefx/commit/5ed2e98718700ba37ed95c34ee04960b5aba1aa0
This commit is contained in:
Carlos Sanchez Lopez authored and GitHub committed 2019-10-22 19:42:48 -07:00
1 parent aa4a34c50c
commit bdb751bf52
19 files changed
+763 -699

No files matched your search

@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27213.1
# Visual Studio Version 16
VisualStudioVersion = 16.0.29420.8
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.IO.FileSystem.AccessControl.Tests", "tests\System.IO.FileSystem.AccessControl.Tests.csproj", "{5915DD11-5D57-45A9-BFB0-56FEB7741E1F}"
ProjectSection(ProjectDependencies) = postProject
@@ -30,8 +30,8 @@ Global
{5915DD11-5D57-45A9-BFB0-56FEB7741E1F}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
{5915DD11-5D57-45A9-BFB0-56FEB7741E1F}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
{5915DD11-5D57-45A9-BFB0-56FEB7741E1F}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
{D77FBA6C-1AA6-45A4-93E2-97A370672C53}.Debug|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Debug|Any CPU
{D77FBA6C-1AA6-45A4-93E2-97A370672C53}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
{D77FBA6C-1AA6-45A4-93E2-97A370672C53}.Debug|Any CPU.ActiveCfg = netstandard-Windows_NT-Debug|Any CPU
{D77FBA6C-1AA6-45A4-93E2-97A370672C53}.Debug|Any CPU.Build.0 = netstandard-Windows_NT-Debug|Any CPU
{D77FBA6C-1AA6-45A4-93E2-97A370672C53}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
{D77FBA6C-1AA6-45A4-93E2-97A370672C53}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
{88A04AB0-F61E-4DD2-9E12-928DCA261263}.Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
@@ -9,6 +9,7 @@ namespace System.IO
{
public static partial class FileSystemAclExtensions
{
public static void Create(this System.IO.DirectoryInfo directoryInfo, System.Security.AccessControl.DirectorySecurity directorySecurity) { }
public static System.Security.AccessControl.DirectorySecurity GetAccessControl(this System.IO.DirectoryInfo directoryInfo) { throw null; }
public static System.Security.AccessControl.DirectorySecurity GetAccessControl(this System.IO.DirectoryInfo directoryInfo, System.Security.AccessControl.AccessControlSections includeSections) { throw null; }
public static System.Security.AccessControl.FileSecurity GetAccessControl(this System.IO.FileInfo fileInfo) { throw null; }
@@ -1,4 +1,4 @@
<root>
<root>
<!--
Microsoft ResX Schema
@@ -143,4 +143,49 @@
<data name="InvalidOperation_RemoveFail" xml:space="preserve">
<value>Remove access rule failed</value>
</data>
<data name="Arg_MustBeDriveLetterOrRootDir" xml:space="preserve">
<value>Drive name must be a root directory (i.e. 'C:\') or a drive letter ('C').</value>
</data>
<data name="IO_AlreadyExists_Name" xml:space="preserve">
<value>Cannot create '{0}' because a file or directory with the same name already exists.</value>
</data>
<data name="IO_FileExists_Name" xml:space="preserve">
<value>The file '{0}' already exists.</value>
</data>
<data name="IO_FileNotFound" xml:space="preserve">
<value>Unable to find the specified file.</value>
</data>
<data name="IO_FileNotFound_FileName" xml:space="preserve">
<value>Could not find file '{0}'.</value>
</data>
<data name="IO_PathNotFound_NoPathName" xml:space="preserve">
<value>Could not find a part of the path.</value>
</data>
<data name="IO_PathNotFound_Path" xml:space="preserve">
<value>Could not find a part of the path '{0}'.</value>
</data>
<data name="IO_PathTooLong" xml:space="preserve">
<value>The specified file name or path is too long, or a component of the specified path is too long.</value>
</data>
<data name="IO_PathTooLong_Path" xml:space="preserve">
<value>The path '{0}' is too long, or a component of the specified path is too long.</value>
</data>
<data name="IO_SharingViolation_File" xml:space="preserve">
<value>The process cannot access the file '{0}' because it is being used by another process.</value>
</data>
<data name="IO_SharingViolation_NoFileName" xml:space="preserve">
<value>The process cannot access the file because it is being used by another process.</value>
</data>
<data name="UnauthorizedAccess_IODenied_NoPathName" xml:space="preserve">
<value>Access to the path is denied.</value>
</data>
<data name="UnauthorizedAccess_IODenied_Path" xml:space="preserve">
<value>Access to the path '{0}' is denied.</value>
</data>
<data name="Argument_InvalidPathChars" xml:space="preserve">
<value>Illegal characters in path '{0}'.</value>
</data>
<data name="Arg_PathEmpty" xml:space="preserve">
<value>The path is empty.</value>
</data>
</root>
@@ -1,31 +1,78 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DefineConstants Condition="'$(TargetsNetCoreApp)' != 'true'">$(DefineConstants);INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
<AllowUnsafeBlocks Condition="'$(TargetsWindows)' == 'true'">true</AllowUnsafeBlocks>
<IsPartialFacadeAssembly Condition="'$(TargetsNetFx)'=='true'">true</IsPartialFacadeAssembly>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetsWindows)' != 'true'">SR.PlatformNotSupported_AccessControl</GeneratePlatformNotSupportedAssemblyMessage>
<Configurations>net461-Windows_NT-Debug;net461-Windows_NT-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release;netstandard-Debug;netstandard-Release;netstandard-Windows_NT-Debug;netstandard-Windows_NT-Release</Configurations>
</PropertyGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true' AND '$(TargetsNetFx)'!='true'">
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Errors.cs">
<Link>Common\Interop\Windows\Interop.Errors.cs</Link>
</Compile>
<Compile Include="System\Security\AccessControl\FileSecurity.cs" />
<!-- Handle nullability in NetFX/NetStandard -->
<ItemGroup Condition="'$(TargetsNetCoreApp)' != 'true'">
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs" Link="System\Diagnostics\CodeAnalysis\NullableAttributes.cs" />
</ItemGroup>
<!-- Source includes -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true' and '$(TargetsNetFx)' != 'true'">
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Errors.cs" Link="Common\Interop\Windows\Interop.Errors.cs" />
<Compile Include="$(CommonPath)\System\NotImplemented.cs" Link="Common\System\NotImplemented.cs" />
<Compile Include="System\IO\FileSystemAclExtensions.cs" />
<Compile Include="System\Security\AccessControl\DirectoryObjectSecurity.cs" />
<Compile Include="System\Security\AccessControl\DirectorySecurity.cs" />
<Compile Include="System\Security\AccessControl\FileSecurity.cs" />
<Compile Include="System\Security\AccessControl\FileSystemAccessRule.cs" />
<Compile Include="System\Security\AccessControl\FileSystemAuditRule.cs" />
<Compile Include="System\Security\AccessControl\FileSystemRights.cs" />
<Compile Include="System\Security\AccessControl\FileSystemSecurity.cs" />
<Compile Include="System\IO\FileSystemAclExtensions.cs" />
<Compile Include="$(CommonPath)\System\NotImplemented.cs">
<Link>Common\Interop\Windows\mincore\NotImplemented.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)'=='true'">
<Reference Include="mscorlib" />
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp'">
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Interop.BOOL.cs" Link="Common\CoreLib\Interop\Windows\Interop.BOOL.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.FILE_TIME.cs" Link="Common\CoreLib\Interop\Windows\Interop.FILE_TIME.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.FileAttributes.cs" Link="Common\Interop\Windows\Interop.FileAttributes.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.FindClose.cs" Link="Common\CoreLib\Interop\Windows\Interop.FindClose.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.FindFirstFileEx.cs" Link="Common\CoreLib\Interop\Windows\Interop.FindFirstFileEx.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.FormatMessage.cs" Link="Common\Interop\Windows\Interop.FormatMessage.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.GET_FILEEX_INFO_LEVELS.cs" Link="Common\CoreLib\Interop\Windows\Interop.GET_FILEEX_INFO_LEVELS.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.GetFileAttributesEx.cs" Link="Common\Interop\Windows\Interop.GetFileAttributesEx.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.GetFullPathNameW.cs" Link="Common\Interop\Windows\Interop.GetFullPathNameW.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.GetLongPathNameW.cs" Link="Common\Interop\Windows\Interop.GetLongPathNameW.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.GetLogicalDrives.cs" Link="Common\Interop\Windows\Interop.GetLogicalDrives.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.MAX_PATH.cs" Link="Common\CoreLib\Interop\Windows\Interop.MAX_PATH.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" Link="Common\CoreLib\Interop\Windows\Interop.SECURITY_ATTRIBUTES.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.SetThreadErrorMode.cs" Link="Common\CoreLib\Interop\Windows\Interop.SetThreadErrorMode.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.WIN32_FILE_ATTRIBUTE_DATA.cs" Link="Common\CoreLib\Interop\Windows\Interop.WIN32_FILE_ATTRIBUTE_DATA.cs" />
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.WIN32_FIND_DATA.cs" Link="Common\CoreLib\Interop\Windows\Interop.WIN32_FIND_DATA.cs" />
<Compile Include="$(CommonPath)\CoreLib\System\IO\DisableMediaInsertionPrompt.cs" Link="Common\System\IO\DisableMediaInsertionPrompt.cs" />
<Compile Include="$(CommonPath)\CoreLib\System\IO\PathHelper.Windows.cs" Link="Common\CoreLib\System\IO\PathHelper.Windows.cs" />
<Compile Include="$(CommonPath)\CoreLib\System\IO\PathInternal.Windows.cs" Link="Common\CoreLib\System\IO\PathInternal.Windows.cs" />
<Compile Include="$(CommonPath)\CoreLib\System\IO\Win32Marshal.cs" Link="Common\CoreLib\System\IO\Win32Marshal.cs" />
<Compile Include="$(CommonPath)\CoreLib\System\Text\ValueStringBuilder.cs" Link="Common\CoreLib\System\Text\ValueStringBuilder.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs" Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\Interop.LongFileTime.cs" Link="Common\Interop\Windows\Interop.LongFileTime.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.CreateDirectory.cs" Link="Common\Interop\Windows\Interop.CreateDirectory.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.FILE_INFO_BY_HANDLE_CLASS.cs" Link="Common\Interop\Windows\Interop.FILE_INFO_BY_HANDLE_CLASS.cs" />
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeFindHandle.Windows.cs" Link="Common\Microsoft\Win32\SafeHandles\SafeFindHandle.Windows.cs" />
<Compile Include="$(CommonPath)\System\IO\FileSystem.Attributes.Windows.cs" Link="Common\System\IO\FileSystem.Attributes.Windows.cs" />
<Compile Include="$(CommonPath)\System\IO\FileSystem.DirectoryCreation.Windows.cs" Link="Common\System\IO\FileSystem.DirectoryCreation.Windows.cs" />
<Compile Include="System\IO\FileSystemAclExtensions.netcoreapp.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)' == 'true'">
<Compile Include="System\IO\FileSystemAclExtensions.net46.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)'!='true'">
<ItemGroup Condition="'$(TargetsWindows)' == 'true' and '$(TargetGroup)' == 'netstandard'">
<Compile Include="System\IO\FileSystemAclExtensions.netstandard.cs" />
</ItemGroup>
<!-- Reference includes -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true' and '$(TargetsNetFx)' != 'true'">
<Reference Include="System.Buffers" />
<Reference Include="System.Collections" />
<Reference Include="System.Memory" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading.Tasks" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)' == 'true'">
<Reference Include="mscorlib" />
<Reference Include="System.Runtime" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)' != 'true'">
<Reference Include="System.Collections.NonGeneric" />
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.IO.FileSystem" />
@@ -2,13 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Win32.SafeHandles;
using System.Security.AccessControl;
using System;
using Microsoft.Win32.SafeHandles;
namespace System.IO
{
public static class FileSystemAclExtensions
public static partial class FileSystemAclExtensions
{
public static DirectorySecurity GetAccessControl(this DirectoryInfo directoryInfo)
{
@@ -8,6 +8,17 @@ namespace System.IO
{
public static class FileSystemAclExtensions
{
public static void Create(this DirectoryInfo directoryInfo, DirectorySecurity directorySecurity)
{
if (directoryInfo == null)
throw new ArgumentNullException(nameof(directoryInfo));
if (directorySecurity == null)
throw new ArgumentNullException(nameof(directorySecurity));
directoryInfo.Create(directorySecurity);
}
public static DirectorySecurity GetAccessControl(this DirectoryInfo directoryInfo)
{
return directoryInfo.GetAccessControl();
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Security.AccessControl;
namespace System.IO
{
public static partial class FileSystemAclExtensions
{
/// <summary>Creates a new directory, ensuring it is created with the specified directory security. If the directory already exists, nothing is done.</summary>
/// <param name="directoryInfo">The object describing a directory that does not exist in disk yet.</param>
/// <param name="directorySecurity">An object that determines the access control and audit security for the directory.</param>
/// <exception cref="ArgumentNullException"><paramref name="directoryInfo" /> or <paramref name="directorySecurity" /> is <see langword="null" />.</exception>
/// <exception cref="DirectoryNotFoundException">Could not find a part of the path.</exception>
/// <exception cref="UnauthorizedAccessException">Access to the path is denied.</exception>
public static void Create(this DirectoryInfo directoryInfo, DirectorySecurity directorySecurity)
{
if (directoryInfo == null)
throw new ArgumentNullException(nameof(directoryInfo));
if (directorySecurity == null)
throw new ArgumentNullException(nameof(directorySecurity));
FileSystem.CreateDirectory(directoryInfo.FullName, directorySecurity.GetSecurityDescriptorBinaryForm());
}
}
}
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Security.AccessControl;
namespace System.IO
{
public static partial class FileSystemAclExtensions
{
public static void Create(this DirectoryInfo directoryInfo, DirectorySecurity directorySecurity)
{
throw new PlatformNotSupportedException();
}
}
}
@@ -17,6 +17,7 @@ namespace System.Security.AccessControl
public FileSecurity(string fileName, AccessControlSections includeSections)
: base(false, fileName, includeSections, false)
{
// This will validate the passed path
Path.GetFullPath(fileName);
}
@@ -2,13 +2,21 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.VisualBasic;
using Xunit;
namespace System.IO
{
public class FileSystemAclExtensionsTests
{
#region Test methods
[Fact]
public void GetAccessControl_DirectoryInfo_InvalidArguments()
{
@@ -112,7 +120,6 @@ namespace System.IO
}
}
[Fact]
public void SetAccessControl_DirectoryInfo_DirectorySecurity_InvalidArguments()
{
@@ -187,5 +194,133 @@ namespace System.IO
FileSystemAclExtensions.SetAccessControl(fileStream, fileSecurity);
}
}
[Fact]
public void DirectoryInfo_Create_NullDirectoryInfo()
{
DirectoryInfo info = null;
DirectorySecurity security = new DirectorySecurity();
if (PlatformDetection.IsFullFramework)
{
Assert.Throws<ArgumentNullException>(() => FileSystemAclExtensions.Create(info, security));
}
else
{
Assert.Throws<ArgumentNullException>(() => info.Create(security));
}
}
[Fact]
public void DirectoryInfo_Create_DefaultDirectorySecurity()
{
DirectorySecurity security = new DirectorySecurity();
VerifyDirectorySecurity(security);
}
[Fact]
public void DirectoryInfo_Create_NullDirectorySecurity()
{
DirectoryInfo info = new DirectoryInfo("path");
if (PlatformDetection.IsFullFramework)
{
Assert.Throws<ArgumentNullException>(() => FileSystemAclExtensions.Create(info, null));
}
else
{
Assert.Throws<ArgumentNullException>(() => info.Create(null));
}
}
[Fact]
public void DirectoryInfo_Create_NotFound()
{
DirectoryInfo info = new DirectoryInfo(@"W:\\I\\Do\\Not\\Exist");
DirectorySecurity security = new DirectorySecurity();
Assert.Throws<DirectoryNotFoundException>(() => info.Create(security));
}
[Theory]
[InlineData(WellKnownSidType.BuiltinUsersSid, FileSystemRights.FullControl, AccessControlType.Allow)]
[InlineData(WellKnownSidType.BuiltinUsersSid, FileSystemRights.ReadData, AccessControlType.Allow)]
[InlineData(WellKnownSidType.BuiltinUsersSid, FileSystemRights.Write, AccessControlType.Allow)]
[InlineData(WellKnownSidType.BuiltinUsersSid, FileSystemRights.Write, AccessControlType.Deny)]
[InlineData(WellKnownSidType.BuiltinUsersSid, FileSystemRights.FullControl, AccessControlType.Deny)]
public void DirectoryInfo_Create_DirectorySecurityWithSpecificAccessRule(
WellKnownSidType sid,
FileSystemRights rights,
AccessControlType controlType)
{
DirectorySecurity security = GetDirectorySecurity(sid, rights, controlType);
VerifyDirectorySecurity(security);
}
#endregion
#region Helper methods
private DirectorySecurity GetDirectorySecurity(WellKnownSidType sid, FileSystemRights rights, AccessControlType controlType)
{
DirectorySecurity security = new DirectorySecurity();
SecurityIdentifier identity = new SecurityIdentifier(sid, null);
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, rights, controlType);
security.AddAccessRule(accessRule);
return security;
}
private void VerifyDirectorySecurity(DirectorySecurity expectedSecurity)
{
using var directory = new TempDirectory();
string path = Path.Combine(directory.Path, "directory");
DirectoryInfo info = new DirectoryInfo(path);
info.Create(expectedSecurity);
Assert.True(Directory.Exists(path));
Assert.Equal(typeof(FileSystemRights), expectedSecurity.AccessRightType);
DirectoryInfo actualInfo = new DirectoryInfo(info.FullName);
DirectorySecurity actualSecurity = actualInfo.GetAccessControl();
VerifyDirectoryAccessSecurity(expectedSecurity, actualSecurity);
}
private void VerifyDirectoryAccessSecurity(DirectorySecurity expectedSecurity, DirectorySecurity actualSecurity)
{
Assert.Equal(typeof(FileSystemRights), actualSecurity.AccessRightType);
List<FileSystemAccessRule> expectedAccessRules = expectedSecurity.GetAccessRules(includeExplicit: true, includeInherited: false, typeof(SecurityIdentifier))
.Cast<FileSystemAccessRule>().ToList();
List<FileSystemAccessRule> actualAccessRules = actualSecurity.GetAccessRules(includeExplicit: true, includeInherited: false, typeof(SecurityIdentifier))
.Cast<FileSystemAccessRule>().ToList();
// If DirectorySecurity is created without arguments, GetAccessRules will return zero rules
Assert.Equal(expectedAccessRules.Count, actualAccessRules.Count);
if (expectedAccessRules.Count > 0)
{
Assert.All(expectedAccessRules, actualAccessRule =>
{
int count = expectedAccessRules.Count(expectedAccessRule => AreAccessRulesEqual(expectedAccessRule, actualAccessRule));
Assert.True(count > 0);
});
}
}
private bool AreAccessRulesEqual(FileSystemAccessRule expectedRule, FileSystemAccessRule actualRule)
{
return
expectedRule.AccessControlType == actualRule.AccessControlType &&
expectedRule.FileSystemRights == actualRule.FileSystemRights &&
expectedRule.InheritanceFlags == actualRule.InheritanceFlags &&
expectedRule.PropagationFlags == actualRule.PropagationFlags;
}
#endregion
}
}