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 @@ -904,7 +904,8 @@ private void OnClose()

private void ThrowIfDisposed()
{
if (_isDisposed)
// A stream on a handle cannot be used once the handle is closed, even before the stream is disposed.
if (_isDisposed || _ownedHandle is { IsClosed: true })
{
throw new ObjectDisposedException("", "Cannot access a closed file.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ internal sealed class MockSafeFileHandleRegistry
FileOptions.DeleteOnClose |
FileOptions.SequentialScan |
FileOptions.Encrypted |
#if NET9_0_OR_GREATER
(FileOptions)0x02000000 | // BackupOrRestore, accepted since .NET 9
#endif
(FileOptions)0x20000000; // NoBuffering

private readonly List<Entry> _entries = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,39 @@ public async Task OpenHandle_ShouldBeUsableByFileMethodsTakingAHandle(
await That(FileSystem.File.GetAttributes(handle))
.IsEqualTo(FileSystem.File.GetAttributes(path));
}

#if NET9_0_OR_GREATER
[Test]
[AutoArguments]
public async Task OpenHandle_WithBackupOrRestoreOption_ShouldOpenTheFile(string path)
{
FileSystem.File.WriteAllText(path, "x");

void Act()
{
using SafeFileHandle handle = FileSystem.File.OpenHandle(path,
FileMode.Open, FileAccess.Read, FileShare.Read, (FileOptions)0x02000000);
}

await That(Act).DoesNotThrow()
.Because("the runtime accepts the undocumented BackupOrRestore option since .NET 9");
}
#else
[Test]
[AutoArguments]
public async Task OpenHandle_WithBackupOrRestoreOption_ShouldThrowArgumentOutOfRangeException(
string path)
{
FileSystem.File.WriteAllText(path, "x");

void Act()
{
using SafeFileHandle handle = FileSystem.File.OpenHandle(path,
FileMode.Open, FileAccess.Read, FileShare.Read, (FileOptions)0x02000000);
}

await That(Act).Throws<ArgumentOutOfRangeException>().WithParamName("options");
}
#endif
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,23 @@ public async Task New_WithHandle_ShouldReportAccessFromTheGivenFileAccess(
await That(stream.CanRead).IsTrue();
await That(stream.CanWrite).IsFalse();
}

[Test]
[AutoArguments]
public async Task New_WithHandle_FlushAfterTheHandleIsClosed_ShouldThrowObjectDisposedException(
string path)
{
FileSystem.File.WriteAllText(path, "x");
SafeFileHandle handle = FileSystem.File.OpenHandle(path,
FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
FileSystemStream stream = FileSystem.FileStream.New(handle, FileAccess.ReadWrite);
handle.Dispose();

await That(() => stream.Flush()).Throws<ObjectDisposedException>();
await That(() => stream.Flush(true)).Throws<ObjectDisposedException>();
await That(() => stream.FlushAsync()).Throws<ObjectDisposedException>();
await That(() => stream.Dispose()).DoesNotThrow()
.Because("disposing a stream whose handle is already closed succeeds");
}
}
#endif
Loading