From b3ae4c778e1e3fafcf589c7bb77cf13478edd790 Mon Sep 17 00:00:00 2001 From: Antoine Aubry Date: Thu, 14 Jan 2021 19:14:25 +0000 Subject: [PATCH] Add failing test for slow streams --- YamlDotNet.Test/Core/ScannerTests.cs | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/YamlDotNet.Test/Core/ScannerTests.cs b/YamlDotNet.Test/Core/ScannerTests.cs index 1051cfb3b..b47d2fe58 100644 --- a/YamlDotNet.Test/Core/ScannerTests.cs +++ b/YamlDotNet.Test/Core/ScannerTests.cs @@ -25,6 +25,7 @@ using YamlDotNet.Core.Tokens; using System.IO; using System.Reflection; +using System; #if !PORTABLE && !NETCOREAPP1_0 using System.Runtime.Serialization.Formatters.Binary; @@ -390,6 +391,22 @@ public void MarksOnDoubleQuotedScalarsAreCorrect() Assert.Equal(4, scalar.End.Column); } + [Fact] + public void Slow_stream_is_parsed_correctly() + { + var buffer = new MemoryStream(); + Yaml.StreamFrom("04-scalars-in-multi-docs.yaml").CopyTo(buffer); + + var slowStream = new SlowStream(buffer.ToArray()); + + var scanner = new Scanner(new StreamReader(slowStream)); + + scanner.MoveNext(); + + // Should not fail + scanner.MoveNext(); + } + private void AssertPartialSequenceOfTokensFrom(Scanner scanner, params Token[] tokens) { @@ -423,5 +440,65 @@ private void AssertToken(Token expected, Token actual, int tokenNumber) } } } + + /// + /// A stream that reads one byte at the time. + /// + public class SlowStream : Stream + { + private readonly byte[] data; + private int position; + + public SlowStream(byte[] data) + { + this.data = data; + } + + public override bool CanRead => true; + + public override bool CanSeek => false; + + public override bool CanWrite => false; + + public override long Length => data.Length; + + public override long Position + { + get => position; + set => throw new NotSupportedException(); + } + + public override void Flush() + { + throw new NotSupportedException(); + } + + public override int Read(byte[] buffer, int offset, int count) + { + if (count == 0 || position == data.Length) + { + return 0; + } + + buffer[offset] = data[position]; + ++position; + return 1; + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotSupportedException(); + } + + public override void SetLength(long value) + { + throw new NotSupportedException(); + } + + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotSupportedException(); + } + } } } \ No newline at end of file