Skip to content

Commit

Permalink
Add failing test for slow streams
Browse files Browse the repository at this point in the history
  • Loading branch information
aaubry committed Jan 14, 2021
1 parent f6a98d3 commit b3ae4c7
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions YamlDotNet.Test/Core/ScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -423,5 +440,65 @@ private void AssertToken(Token expected, Token actual, int tokenNumber)
}
}
}

/// <summary>
/// A stream that reads one byte at the time.
/// </summary>
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();
}
}
}
}

0 comments on commit b3ae4c7

Please sign in to comment.