Skip to main content

Testably.Abstractions.AccessControl

NuGet

Implements the methods from FileSystemAclExtensions on the IFileSystem interface so production code that reads or writes ACLs works against both real and mocked file systems.

dotnet add package Testably.Abstractions.AccessControl

What it adds

Extension onMethods
IDirectoryCreateDirectory(path, DirectorySecurity), GetAccessControl, SetAccessControl
IDirectoryInfoCreate(DirectorySecurity), GetAccessControl, SetAccessControl
IFileGetAccessControl, SetAccessControl
IFileInfoGetAccessControl, SetAccessControl
FileSystemStreamGetAccessControl, SetAccessControl

Each GetAccessControl overload accepts an optional AccessControlSections mask, matching the BCL.

IFileSystem fileSystem; // injected

DirectorySecurity acl = fileSystem.Directory.GetAccessControl("data");
fileSystem.Directory.SetAccessControl("backup", acl);

fileSystem.File.WriteAllText("secret.txt", "x");
FileSecurity fileAcl = fileSystem.File.GetAccessControl("secret.txt");

Behaviour on the mock

On RealFileSystem the calls forward to System.IO.FileSystemAclExtensions. On MockFileSystem the ACL object is stored as metadata via IFileSystemExtensibility and round-tripped - set it, read it back, and it will be the same object.

To simulate enforcement (denying a read or write based on the ACL), combine the package with MockFileSystem.WithAccessControlStrategy(...).

Windows-only

All ACL APIs are marked [SupportedOSPlatform("windows")]. Calling them on Linux/macOS throws PlatformNotSupportedException.