public void SetAccessControl_DirectoryInfo_DirectorySecurity_Success() { using var directory = new TempAclDirectory(); var directoryInfo = new DirectoryInfo(directory.Path); var directorySecurity = new DirectorySecurity(); directoryInfo.SetAccessControl(directorySecurity); }
public void SetAccessControl_FileInfo_FileSecurity_InvalidArguments() { using var directory = new TempAclDirectory(); using var file = new TempFile(Path.Combine(directory.Path, "file.txt")); var fileInfo = new FileInfo(file.Path); AssertExtensions.Throws <ArgumentNullException>("fileSecurity", () => fileInfo.SetAccessControl(fileSecurity: null)); }
public void GetAccessControl_DirectoryInfo_ReturnsValidObject() { using var directory = new TempAclDirectory(); var directoryInfo = new DirectoryInfo(directory.Path); DirectorySecurity directorySecurity = directoryInfo.GetAccessControl(); Assert.NotNull(directorySecurity); Assert.Equal(typeof(FileSystemRights), directorySecurity.AccessRightType); }
public void SetAccessControl_FileStream_FileSecurity_Success() { using var directory = new TempAclDirectory(); using var file = new TempFile(Path.Combine(directory.Path, "file.txt")); using FileStream fileStream = File.Open(file.Path, FileMode.Append, FileAccess.Write, FileShare.None); var fileSecurity = new FileSecurity(); FileSystemAclExtensions.SetAccessControl(fileStream, fileSecurity); }
public void SetAccessControl_FileInfo_FileSecurity_Success() { using var directory = new TempAclDirectory(); using var file = new TempFile(Path.Combine(directory.Path, "file.txt")); var fileInfo = new FileInfo(file.Path); var fileSecurity = new FileSecurity(); fileInfo.SetAccessControl(fileSecurity); }
public void GetAccessControl_Filestream_ReturnValidObject() { using var directory = new TempAclDirectory(); using var file = new TempFile(Path.Combine(directory.Path, "file.txt")); using FileStream fileStream = File.Open(file.Path, FileMode.Append, FileAccess.Write, FileShare.None); FileSecurity fileSecurity = FileSystemAclExtensions.GetAccessControl(fileStream); Assert.NotNull(fileSecurity); Assert.Equal(typeof(FileSystemRights), fileSecurity.AccessRightType); }
public void GetAccessControl_FileInfo_ReturnsValidObject() { using var directory = new TempAclDirectory(); using var file = new TempFile(Path.Combine(directory.Path, "file.txt")); var fileInfo = new FileInfo(file.Path); FileSecurity fileSecurity = fileInfo.GetAccessControl(); Assert.NotNull(fileSecurity); Assert.Equal(typeof(FileSystemRights), fileSecurity.AccessRightType); }
public void FileInfo_Create_DirectoryNotFound() { using var tempRootDir = new TempAclDirectory(); string path = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString(), "file.txt"); var info = new FileInfo(path); var security = new FileSecurity(); Assert.Throws <DirectoryNotFoundException>(() => info.Create(FileMode.CreateNew, FileSystemRights.FullControl, FileShare.None, DefaultBufferSize, FileOptions.None, security)); }
public void DirectoryInfo_Create_NotFound_FullControl() { using var tempRootDir = new TempAclDirectory(); string dirPath = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString(), "ParentDoesNotExist"); var dirInfo = new DirectoryInfo(dirPath); var security = GetDirectorySecurity(FileSystemRights.FullControl); // Succeeds because it creates the missing parent folder CreateDirectoryWithSecurity(dirInfo, security); }
public void DirectoryInfo_Create_NotFound() { using var tempRootDir = new TempAclDirectory(); string dirPath = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString(), "ParentDoesNotExist"); var dirInfo = new DirectoryInfo(dirPath); var security = new DirectorySecurity(); // Fails because the DirectorySecurity lacks any rights to create parent folder Assert.Throws <UnauthorizedAccessException>(() => CreateDirectoryWithSecurity(dirInfo, security)); }
private void Verify_DirectorySecurity_CreateDirectory(DirectorySecurity expectedSecurity) { using var tempRootDir = new TempAclDirectory(); string path = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString()); DirectoryInfo dirInfo = expectedSecurity.CreateDirectory(path); Assert.True(dirInfo.Exists); tempRootDir.CreatedSubdirectories.Add(dirInfo); var actualDirInfo = new DirectoryInfo(path); DirectorySecurity actualSecurity = actualDirInfo.GetAccessControl(AccessControlSections.Access); VerifyAccessSecurity(expectedSecurity, actualSecurity); }
private void Verify_FileSecurity_CreateFile(FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity expectedSecurity) { using var tempRootDir = new TempAclDirectory(); string path = Path.Combine(tempRootDir.Path, "file.txt"); var fileInfo = new FileInfo(path); fileInfo.Create(mode, rights, share, bufferSize, options, expectedSecurity).Dispose(); Assert.True(fileInfo.Exists); tempRootDir.CreatedSubfiles.Add(fileInfo); var actualFileInfo = new FileInfo(path); FileSecurity actualSecurity = actualFileInfo.GetAccessControl(AccessControlSections.Access); VerifyAccessSecurity(expectedSecurity, actualSecurity); }
public void DirectoryInfo_Create_AllowSpecific_AccessRules(FileSystemRights rights) { using var tempRootDir = new TempAclDirectory(); string path = Path.Combine(tempRootDir.Path, "directory"); var dirInfo = new DirectoryInfo(path); DirectorySecurity expectedSecurity = GetDirectorySecurity(rights); dirInfo.Create(expectedSecurity); Assert.True(dirInfo.Exists); tempRootDir.CreatedSubdirectories.Add(dirInfo); var actualInfo = new DirectoryInfo(dirInfo.FullName); DirectorySecurity actualSecurity = actualInfo.GetAccessControl(AccessControlSections.Access); VerifyAccessSecurity(expectedSecurity, actualSecurity); }
public void DirectorySecurity_CreateDirectory_DirectoryAlreadyExists() { using var tempRootDir = new TempAclDirectory(); string path = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString()); DirectorySecurity expectedSecurity = GetDirectorySecurity(FileSystemRights.FullControl); DirectoryInfo dirInfo = expectedSecurity.CreateDirectory(path); Assert.True(dirInfo.Exists); tempRootDir.CreatedSubdirectories.Add(dirInfo); var basicSecurity = new DirectorySecurity(); // Already exists, existingDirInfo should have the original security, not the new basic security DirectoryInfo existingDirInfo = basicSecurity.CreateDirectory(path); DirectorySecurity actualSecurity = existingDirInfo.GetAccessControl(AccessControlSections.Access); VerifyAccessSecurity(expectedSecurity, actualSecurity); }
public void FileInfo_Create_AllowSpecific_AccessRules(FileSystemRights rights) { using var tempRootDir = new TempAclDirectory(); string path = Path.Combine(tempRootDir.Path, "file.txt"); var fileInfo = new FileInfo(path); FileSecurity expectedSecurity = GetFileSecurity(rights); using FileStream stream = fileInfo.Create( FileMode.Create, FileSystemRights.FullControl, FileShare.ReadWrite | FileShare.Delete, DefaultBufferSize, FileOptions.None, expectedSecurity); Assert.True(fileInfo.Exists); tempRootDir.CreatedSubfiles.Add(fileInfo); var actualInfo = new FileInfo(fileInfo.FullName); FileSecurity actualSecurity = actualInfo.GetAccessControl(AccessControlSections.Access); VerifyAccessSecurity(expectedSecurity, actualSecurity); }