Files
dotnet-runtime/src/libraries/System.IO.FileSystem/tests/FileStream/Buffering_regression.cs
T

51 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using Xunit;
namespace System.IO.Tests
{
public class FileStream_Buffering_regression: FileSystemTest
{
[Fact]
public void FlushSetLengthAtEndOfBuffer()
{
// This is a regression test for a bug with FileStream’s Flush()
// and SetLength() methods.
// The read-buffer was not flushed inside Flush and SetLength when
// the buffer pointer was at the end. This causes subsequent Seek
// and Read calls to operate on stale/wrong data.
// customer reported repro
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.SetLength(200);
fs.Flush();
// write 119 bytes starting from Pos = 28
fs.Seek(28, SeekOrigin.Begin);
Byte[] buffer = new Byte[119];
for (int i = 0; i < buffer.Length; i++)
buffer[i] = Byte.MaxValue;
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
// read 317 bytes starting from Pos = 84;
fs.Seek(84, SeekOrigin.Begin);
fs.Read(new byte[1024], 0, 317);
fs.SetLength(135);
fs.Flush();
// read one byte at Pos = 97
fs.Seek(97, SeekOrigin.Begin);
Assert.Equal(fs.ReadByte(), (int)Byte.MaxValue);
}
}
}
}