Skip to content

MockFileSystem incorrectly allows shared access to same file #1356

@maltebp

Description

@maltebp

Describe the bug
According to C#'s filesystem API, if you open a file with FileShare.None then it should not be possible to create other file streams to that file. If you do this using the "real" file system on Windows, it will throw an IOException with the message: "The process cannot access the file '-local file path-' because it is being used by another process.". But using the MockFileSystem this does not produce any error, which seems wrong.

To Reproduce
Run this code:

 MockFileSystem mockFileSystem = new();
 mockFileSystem.FileStream.New("SomeFile.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None); // Should cause open to fail
 // mockFileSystem.File.Create("SomeFile.txt"); // Does not fail either

 mockFileSystem.FileStream.New("SomeFile.txt", FileMode.Open, FileAccess.Read);


 FileSystem realFileSystem = new();
 realFileSystem.FileStream.New("SomeFile.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None); // Causes open to fail
 // realFileSystem.File.Create("SomeFile.txt"); Also fails

 realFileSystem.FileStream.New("SomeFile.txt", FileMode.Open, FileAccess.Read);

Expected behavior
It should fail when creating the "open" stream on the MockFileSystem, but it does not.

Additional context

  • TestableIO version: 22.0.16
  • .NET: net8.0-windows
  • Windows version: Microsoft Windows 11 Enterprise (10.0.22631 Build 22631)

Metadata

Metadata

Labels

flag: good-first-issueIssues that are good for first time contributorstype: bugIssues that describe misbehaving functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions