Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,24 @@ public FileSecurity AccessControl
public FileShare AllowedFileShare { get; set; } = FileShare.ReadWrite | FileShare.Delete;
/// <summary>
/// Checks whether the file is accessible for this type of FileAccess.
/// MockfileData can be configured to have FileShare.None, which indicates it is locked by a 'different process'.
/// MockFileData can be configured to have FileShare.None, which indicates it is locked by a 'different process'.
///
/// If the file is 'locked by a different process', an IOException will be thrown.
/// If the file is read-only and is accessed for writing, an UnauthorizedAccessException will be thrown.
/// </summary>
/// <param name="path">The path is used in the IOException message to match the message in real life situations</param>
/// <param name="path">The path is used in the exception message to match the message in real life situations</param>
/// <param name="access">The access type to check</param>
internal void CheckFileAccess(string path, FileAccess access)
{
if (!AllowedFileShare.HasFlag((FileShare)access))
{
throw CommonExceptions.ProcessCannotAccessFileInUse(path);
}

if (Attributes.HasFlag(FileAttributes.ReadOnly) && access.HasFlag(FileAccess.Write))
{
throw CommonExceptions.AccessDenied(path);
}
}

internal virtual MockFileData Clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,52 @@ public void MockFileStream_Constructor_ReadTypeNotWritable()
Assert.Throws<NotSupportedException>(() => stream.WriteByte(1));
}

[Test]
[TestCase(FileAccess.Write)]
[TestCase(FileAccess.ReadWrite)]
public void MockFileStream_Constructor_WriteAccessOnReadOnlyFile_Throws_Exception(
FileAccess fileAccess)
{
// Arrange
var filePath = @"C:\test.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filePath, new MockFileData("hi") { Attributes = FileAttributes.ReadOnly } }
});

// Act
Assert.Throws<UnauthorizedAccessException>(() => new MockFileStream(fileSystem, filePath, FileMode.Open, fileAccess));
}

[Test]
public void MockFileStream_Constructor_ReadAccessOnReadOnlyFile_Does_Not_Throw_Exception()
{
// Arrange
var filePath = @"C:\test.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filePath, new MockFileData("hi") { Attributes = FileAttributes.ReadOnly } }
});

// Act
Assert.DoesNotThrow(() => new MockFileStream(fileSystem, filePath, FileMode.Open, FileAccess.Read));
}


[Test]
public void MockFileStream_Constructor_WriteAccessOnNonReadOnlyFile_Does_Not_Throw_Exception()
{
// Arrange
var filePath = @"C:\test.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filePath, new MockFileData("hi") { Attributes = FileAttributes.Normal } }
});

// Act
Assert.DoesNotThrow(() => new MockFileStream(fileSystem, filePath, FileMode.Open, FileAccess.Write));
}

[Test]
[TestCase(FileShare.None, FileAccess.Read)]
[TestCase(FileShare.None, FileAccess.ReadWrite)]
Expand Down