Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -158,8 +158,8 @@ public sealed override long Seek(long offset, SeekOrigin origin)
}
else
{
// keep throwing the same exception we did when seek was causing actual offset change
FileStreamHelpers.ThrowInvalidArgument(_fileHandle);
// keep throwing the same exception type we did when seek was causing actual offset change
throw new IOException(SR.IO_SeekBeforeBegin);
}

// Prevent users from overwriting data in a file that was opened in append mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@ namespace System.IO.Tests
{
public class FileStream_Seek : FileSystemTest
{
[Theory]
[InlineData(0)]
[InlineData(10)]
public void SeekNegativePositionThrowsWithClearMessage(int bufferSize)
{
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize))
{
fs.Write(TestBuffer, 0, TestBuffer.Length);

// Seek to negative position from Begin should throw IOException with clear message
IOException ex = Assert.Throws<IOException>(() => fs.Seek(-1, SeekOrigin.Begin));
Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase);

// Seek to negative position from Current should throw IOException with clear message
fs.Position = 5;
ex = Assert.Throws<IOException>(() => fs.Seek(-10, SeekOrigin.Current));
Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase);

// Seek to negative position from End should throw IOException with clear message
ex = Assert.Throws<IOException>(() => fs.Seek(-fs.Length - 1, SeekOrigin.End));
Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase);
}
}

[Theory]
[InlineData(0)]
[InlineData(10)]
Expand Down
Loading