From d2e9f42c609453a1104afa1a006242ba69b524cf Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 13 Jul 2021 12:05:31 -0400 Subject: Augment tests for FileMode.Append (#55513) To validate that seeking and writing are valid in the region of the file since its initial length. --- .../tests/FileStream/Position.cs | 4 +++ .../System.IO.FileSystem/tests/FileStream/Seek.cs | 3 ++ .../tests/FileStream/SetLength.cs | 6 ++++ .../tests/FileStream/ctor_str_fm.cs | 34 ++++++++++++++++++++-- 4 files changed, 44 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs index 8157f6cc91e..32817dcee26 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs @@ -26,6 +26,10 @@ namespace System.IO.Tests fs.Position = length + 1; Assert.Equal(length + 1, fs.Position); + + fs.Write(TestBuffer); + fs.Position = length + 1; + Assert.Equal(length + 1, fs.Position); } } diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs index 6971eb49927..963a1fddff5 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs @@ -32,6 +32,9 @@ namespace System.IO.Tests Assert.Equal(length, fs.Position); Assert.Throws(() => fs.Seek(-length, SeekOrigin.End)); Assert.Equal(length, fs.Position); + + fs.Write(TestBuffer); + Assert.Equal(length, fs.Seek(length, SeekOrigin.Begin)); } } } diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs index 67944a45771..3a3f547d070 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs @@ -23,6 +23,12 @@ namespace System.IO.Tests Assert.Equal(length, fs.Length); Assert.Throws(() => fs.SetLength(0)); Assert.Equal(length, fs.Length); + + fs.Write(TestBuffer); + Assert.Equal(length + TestBuffer.Length, fs.Length); + + fs.SetLength(length); + Assert.Equal(length, fs.Length); } } diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs index 60e3cd49ea7..47e3b492373 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Text; using Xunit; namespace System.IO.Tests @@ -213,11 +214,23 @@ namespace System.IO.Tests [Theory, MemberData(nameof(StreamSpecifiers))] public virtual void FileModeAppend(string streamSpecifier) { - using (FileStream fs = CreateFileStream(GetTestFilePath() + streamSpecifier, FileMode.Append)) + string fileName = GetTestFilePath() + streamSpecifier; + using (FileStream fs = CreateFileStream(fileName, FileMode.Append)) { Assert.False(fs.CanRead); Assert.True(fs.CanWrite); + + fs.Write(Encoding.ASCII.GetBytes("abcde")); + Assert.Equal(5, fs.Length); + Assert.Equal(5, fs.Position); + Assert.Equal(1, fs.Seek(1, SeekOrigin.Begin)); + + fs.Write(Encoding.ASCII.GetBytes("xyz")); + Assert.Equal(4, fs.Position); + Assert.Equal(5, fs.Length); } + + Assert.Equal("axyze", File.ReadAllText(fileName)); } [Theory, MemberData(nameof(StreamSpecifiers))] @@ -226,20 +239,35 @@ namespace System.IO.Tests string fileName = GetTestFilePath() + streamSpecifier; using (FileStream fs = CreateFileStream(fileName, FileMode.Create)) { - fs.WriteByte(0); + fs.WriteByte((byte)'s'); } + string initialContents = File.ReadAllText(fileName); using (FileStream fs = CreateFileStream(fileName, FileMode.Append)) { // Ensure that the file was re-opened and position set to end Assert.Equal(Math.Max(1L, InitialLength), fs.Length); - Assert.Equal(fs.Length, fs.Position); + + long position = fs.Position; + Assert.Equal(fs.Length, position); + Assert.False(fs.CanRead); Assert.True(fs.CanSeek); Assert.True(fs.CanWrite); + Assert.Throws(() => fs.Seek(-1, SeekOrigin.Current)); + Assert.Throws(() => fs.Seek(0, SeekOrigin.Begin)); Assert.Throws(() => fs.ReadByte()); + + fs.Write(Encoding.ASCII.GetBytes("abcde")); + Assert.Equal(position + 5, fs.Position); + + Assert.Equal(position, fs.Seek(position, SeekOrigin.Begin)); + Assert.Equal(position + 1, fs.Seek(1, SeekOrigin.Current)); + fs.Write(Encoding.ASCII.GetBytes("xyz")); } + + Assert.Equal(initialContents + "axyze", File.ReadAllText(fileName)); } } } -- cgit v1.2.3