diff --git a/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs b/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs index b1b5d77..6af533a 100644 --- a/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs +++ b/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.IO.Pipelines; +using System.Threading; using System.Threading.Tasks; using Kaitai.Async; using Xunit; @@ -115,6 +116,24 @@ await EvaluateMaybeCancelled(async () => }); } + [Fact] + public async Task ForwardSeek_AfterReadToEndAndBackwardSeek_Test() + { + const int toRead = 1; + + var kaitaiStreamSUT = Create(new byte[2]); + + await EvaluateMaybeCancelled(async () => + { + // Simulates kaitai compiler generated code for multiple fields defined as `instances` + await kaitaiStreamSUT.ReadBytesFullAsync(CancellationToken.None); + await kaitaiStreamSUT.SeekAsync(0); + await kaitaiStreamSUT.SeekAsync(toRead); + + Assert.Equal(toRead, kaitaiStreamSUT.Pos); + }); + } + [Theory] [InlineData(0)] [InlineData(1)] diff --git a/Kaitai.Struct.Runtime.Async/ReaderContext/PipeReaderContext.cs b/Kaitai.Struct.Runtime.Async/ReaderContext/PipeReaderContext.cs index 97f832f..76b947c 100644 --- a/Kaitai.Struct.Runtime.Async/ReaderContext/PipeReaderContext.cs +++ b/Kaitai.Struct.Runtime.Async/ReaderContext/PipeReaderContext.cs @@ -58,7 +58,7 @@ public virtual async ValueTask SeekAsync(long position, CancellationToken cancel ReadResult = await PipeReader.ReadAsync(cancellationToken); } - if (ReadResult.Buffer.Length <= position) + if (ReadResult.Buffer.Length >= position) { Position = position; return;