mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-24 16:43:13 -10:00
Improve code coverage of System.IO.FileSystem.AccessControl (35.7%) (dotnet/corefx#20680)
* -Added new classes to handle the unit tests for the following libraries: FileSysyemAuditRule.CS DirectoryObjectSecurity.CS -Added new unit tests to the 'FileSystemAclExtensionsTests.cs' class * updated the files according to the suggested code standards for test cases. * updated the file according to the suggested code standards. * Updated some of the test cases and ran the CodeFormatter tool. * Removed the invalid terms in the code. Commit migrated from https://github.com/dotnet/corefx/commit/12d25b0445f67ac299de5abb2af7af767c135c88
This commit is contained in:
1 parent
398f43d5e9
commit
bc3a5de554
4 files changed
+345
-5
No files matched your search
+245
@@ -0,0 +1,245 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System.DirectoryServices;
|
||||
using System.Security.Principal;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Security.AccessControl
|
||||
{
|
||||
public class DirectoryObjectSecurityTests
|
||||
{
|
||||
[Fact]
|
||||
public void ObjectInitialization_DefaultConstructor_Success()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.True(customObjectSecurity.IsDS);
|
||||
Assert.True(customObjectSecurity.IsContainer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ObjectInitialization_CommonSecurityDescriptor_Success()
|
||||
{
|
||||
var descriptor = new CommonSecurityDescriptor(false, false, string.Empty);
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity(descriptor);
|
||||
Assert.False(customObjectSecurity.IsDS);
|
||||
Assert.False(customObjectSecurity.IsContainer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ObjectInitialization_InvalidSecurityDescriptor()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>("securityDescriptor", () => new CustomDirectoryObjectSecurity(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAccessRules_InvalidTargetType()
|
||||
{
|
||||
var activeDirectorySecurity = new ActiveDirectorySecurity();
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
activeDirectorySecurity
|
||||
.GetAccessRules(false, false, typeof(System.Security.Principal.GenericPrincipal)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAccessRules_TargetType_SecurityIdentifier_ReturnsValidObject()
|
||||
{
|
||||
var descriptor = new CommonSecurityDescriptor(false, false, string.Empty);
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity(descriptor);
|
||||
|
||||
AuthorizationRuleCollection ruleCollection =
|
||||
customObjectSecurity
|
||||
.GetAccessRules(false, false, typeof(System.Security.Principal.SecurityIdentifier));
|
||||
|
||||
Assert.NotNull(ruleCollection);
|
||||
Assert.Empty(ruleCollection);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAccessRules_TargetType_NTAccount_ReturnsValidObject()
|
||||
{
|
||||
var descriptor = new CommonSecurityDescriptor(false, false, string.Empty);
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity(descriptor);
|
||||
|
||||
AuthorizationRuleCollection ruleCollection =
|
||||
customObjectSecurity
|
||||
.GetAccessRules(false, false, typeof(System.Security.Principal.NTAccount));
|
||||
|
||||
Assert.NotNull(ruleCollection);
|
||||
Assert.Empty(ruleCollection);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAuditRules_ReturnsValidObject()
|
||||
{
|
||||
var descriptor = new CommonSecurityDescriptor(false, false, string.Empty);
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity(descriptor);
|
||||
|
||||
AuthorizationRuleCollection ruleCollection =
|
||||
customObjectSecurity
|
||||
.GetAuditRules(true, false, typeof(System.Security.Principal.NTAccount));
|
||||
|
||||
Assert.NotNull(ruleCollection);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void RemoveAuditRuleAll_InvalidObjectAuditRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.RemoveAuditRuleAll(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAuditRuleSpecific_InvalidObjectAuditRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.RemoveAuditRuleSpecific(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAuditRule_InvalidObjectAuditRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.RemoveAuditRule(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAuditRule_InvalidObjectAuditRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.SetAuditRule(null));
|
||||
}
|
||||
[Fact]
|
||||
public void AddAuditRule_InvalidObjectAuditRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.AddAuditRule(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAccessRuleSpecific_InvalidObjectAccessRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.RemoveAccessRuleSpecific(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAccessRuleAll_InvalidObjectAccessRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.RemoveAccessRuleAll(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAccessRule_InvalidObjectAccessRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.RemoveAccessRule(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResetAccessRule_InvalidObjectAccessRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.ResetAccessRule(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAccessRule_InvalidObjectAccessRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.SetAccessRule(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddAccessRule_InvalidObjectAccessRule()
|
||||
{
|
||||
var customObjectSecurity = new CustomDirectoryObjectSecurity();
|
||||
Assert.Throws<ArgumentNullException>("rule", () => customObjectSecurity.AddAccessRule(null));
|
||||
}
|
||||
|
||||
private class CustomDirectoryObjectSecurity : DirectoryObjectSecurity
|
||||
{
|
||||
public CustomDirectoryObjectSecurity()
|
||||
{
|
||||
}
|
||||
public CustomDirectoryObjectSecurity(CommonSecurityDescriptor securityDescriptor) : base(securityDescriptor)
|
||||
{
|
||||
}
|
||||
|
||||
public override Type AccessRightType => throw new NotImplementedException();
|
||||
|
||||
public override Type AccessRuleType => throw new NotImplementedException();
|
||||
|
||||
public override Type AuditRuleType => throw new NotImplementedException();
|
||||
|
||||
public override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public new void RemoveAuditRuleAll(ObjectAuditRule rule)
|
||||
{
|
||||
base.RemoveAuditRuleAll(rule);
|
||||
}
|
||||
public new void RemoveAuditRuleSpecific(ObjectAuditRule rule)
|
||||
{
|
||||
base.RemoveAuditRuleSpecific(rule);
|
||||
}
|
||||
public new bool RemoveAuditRule(ObjectAuditRule rule)
|
||||
{
|
||||
return base.RemoveAuditRule(rule);
|
||||
}
|
||||
public new void SetAuditRule(ObjectAuditRule rule)
|
||||
{
|
||||
base.SetAuditRule(rule);
|
||||
}
|
||||
public new void AddAuditRule(ObjectAuditRule rule)
|
||||
{
|
||||
base.AddAuditRule(rule);
|
||||
}
|
||||
public new void RemoveAccessRuleSpecific(ObjectAccessRule rule)
|
||||
{
|
||||
base.RemoveAccessRuleSpecific(rule);
|
||||
}
|
||||
public new void RemoveAccessRuleAll(ObjectAccessRule rule)
|
||||
{
|
||||
base.RemoveAccessRuleAll(rule);
|
||||
}
|
||||
public new bool RemoveAccessRule(ObjectAccessRule rule)
|
||||
{
|
||||
return base.RemoveAccessRule(rule);
|
||||
}
|
||||
public new void ResetAccessRule(ObjectAccessRule rule)
|
||||
{
|
||||
base.ResetAccessRule(rule);
|
||||
}
|
||||
public new void SetAccessRule(ObjectAccessRule rule)
|
||||
{
|
||||
base.SetAccessRule(rule);
|
||||
}
|
||||
public new void AddAccessRule(ObjectAccessRule rule)
|
||||
{
|
||||
base.AddAccessRule(rule);
|
||||
}
|
||||
public new bool IsDS
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.IsDS;
|
||||
}
|
||||
}
|
||||
public new bool IsContainer
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.IsContainer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
-5
@@ -1,6 +1,5 @@
|
||||
// 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.
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System.Security.AccessControl;
|
||||
using Xunit;
|
||||
@@ -99,13 +98,27 @@ namespace System.IO
|
||||
Assert.Throws<NullReferenceException>(() => FileSystemAclExtensions.GetAccessControl((FileStream)null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAccessControl_Filestream_ReturnValidObject()
|
||||
{
|
||||
using (var directory = new TempDirectory())
|
||||
using (var file = new TempFile(Path.Combine(directory.Path, "file.txt")))
|
||||
using (FileStream fileStream = File.Open(file.Path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None))
|
||||
{
|
||||
FileSecurity fileSecurity = FileSystemAclExtensions.GetAccessControl(fileStream);
|
||||
Assert.NotNull(fileSecurity);
|
||||
Assert.Equal(typeof(FileSystemRights), fileSecurity.AccessRightType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void SetAccessControl_DirectoryInfo_DirectorySecurity_InvalidArguments()
|
||||
{
|
||||
using (var directory = new TempDirectory())
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(directory.Path);
|
||||
AssertExtensions.Throws<ArgumentNullException>("directorySecurity", () => directoryInfo.SetAccessControl((DirectorySecurity) null));
|
||||
AssertExtensions.Throws<ArgumentNullException>("directorySecurity", () => directoryInfo.SetAccessControl((DirectorySecurity)null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +141,7 @@ namespace System.IO
|
||||
using (var file = new TempFile(Path.Combine(directory.Path, "file.txt")))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(file.Path);
|
||||
AssertExtensions.Throws<ArgumentNullException>("fileSecurity", () => fileInfo.SetAccessControl((FileSecurity) null));
|
||||
AssertExtensions.Throws<ArgumentNullException>("fileSecurity", () => fileInfo.SetAccessControl((FileSecurity)null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,5 +163,28 @@ namespace System.IO
|
||||
{
|
||||
Assert.Throws<NullReferenceException>(() => FileSystemAclExtensions.SetAccessControl((FileStream)null, (FileSecurity)null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAccessControl_FileStream_FileSecurity_InvalidFileSecurityObject()
|
||||
{
|
||||
using (var directory = new TempDirectory())
|
||||
using (var file = new TempFile(Path.Combine(directory.Path, "file.txt")))
|
||||
using (FileStream fileStream = File.Open(file.Path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None))
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>("fileSecurity", () => FileSystemAclExtensions.SetAccessControl(fileStream, (FileSecurity)null));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAccessControl_FileStream_FileSecurity_Success()
|
||||
{
|
||||
using (var directory = new TempDirectory())
|
||||
using (var file = new TempFile(Path.Combine(directory.Path, "file.txt")))
|
||||
using (FileStream fileStream = File.Open(file.Path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None))
|
||||
{
|
||||
FileSecurity fileSecurity = new FileSecurity();
|
||||
FileSystemAclExtensions.SetAccessControl(fileStream, fileSecurity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
|
||||
using System.Security.Principal;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Security.AccessControl
|
||||
{
|
||||
public class FileSystemAuditRuleTests
|
||||
{
|
||||
[Fact]
|
||||
public void ObjectInitialization_IdentityReference_FileSystemRights_AuditFlags_Success()
|
||||
{
|
||||
var identity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
|
||||
var auditRule = new FileSystemAuditRule(identity, FileSystemRights.ReadData, AuditFlags.Failure);
|
||||
Assert.Equal(auditRule.IdentityReference, identity);
|
||||
Assert.Equal(auditRule.FileSystemRights, FileSystemRights.ReadData);
|
||||
Assert.Equal(auditRule.AuditFlags, AuditFlags.Failure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ObjectInitialization_Identity_FileSystemRights_AuditFlags_InheritanceFlag_PropagationFlag_Success()
|
||||
{
|
||||
var auditRule = new FileSystemAuditRule(@"MYDOMAIN\MyAccount", FileSystemRights.ReadData,
|
||||
InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AuditFlags.Failure);
|
||||
Assert.Equal(auditRule.PropagationFlags, PropagationFlags.InheritOnly);
|
||||
Assert.Equal(auditRule.InheritanceFlags, InheritanceFlags.ObjectInherit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ObjectInitialization_Identity_FileSystemRights_AuditFlags_Success()
|
||||
{
|
||||
var auditRule = new FileSystemAuditRule(@"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AuditFlags.Failure);
|
||||
Assert.Equal(auditRule.FileSystemRights, FileSystemRights.ReadData);
|
||||
Assert.Equal(auditRule.AuditFlags, AuditFlags.Failure);
|
||||
Assert.Equal(auditRule.IdentityReference.Value, @"MYDOMAIN\MyAccount");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ObjectInitialization_InvalidFileSystemRights()
|
||||
{
|
||||
var fileSystemRights = (FileSystemRights)(-1);
|
||||
var identity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
|
||||
Assert.Throws<ArgumentOutOfRangeException>("fileSystemRights", () => new FileSystemAuditRule(@"MYDOMAIN\MyAccount", fileSystemRights, AuditFlags.Failure));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void FileSystemRights_ReturnValidObject()
|
||||
{
|
||||
var auditRule = new FileSystemAuditRule(@"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AuditFlags.Failure);
|
||||
FileSystemRights fileSystemRights = auditRule.FileSystemRights;
|
||||
Assert.Equal(fileSystemRights, FileSystemRights.ReadData);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -9,6 +9,7 @@
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Debug|AnyCPU'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Release|AnyCPU'" />
|
||||
<ItemGroup>
|
||||
<Compile Include="DirectoryObjectSecurityTests.cs" />
|
||||
<Compile Include="FileSystemAclExtensionsTests.cs" />
|
||||
<Compile Include="$(CommonTestPath)\System\AssertExtensions.cs">
|
||||
<Link>Common\System\AssertExtensions.cs</Link>
|
||||
@@ -19,6 +20,7 @@
|
||||
<Compile Include="$(CommonTestPath)\System\IO\TempDirectory.cs">
|
||||
<Link>Common\System\IO\TempDirectory.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="FileSystemAuditRuleTests.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
|
||||
</Project>
|
||||
Reference in new issue
Block a user