Adjust ReadFixed implementation somewhat. Exceeding the length of the buffer was already an error before.

Change the tests by replacing the memory buffer with a Fake stream giving 1 byte at a time.
This commit is contained in:
Serge Camille
2020-08-18 23:41:07 +02:00
parent 10e5562706
commit 09c8b18d3d
3 changed files with 90 additions and 35 deletions

View File

@@ -34,32 +34,6 @@ namespace S7.Net.UnitTest
var t = TPKT.Read(m);
}
[TestMethod]
public async Task TPKT_ReadDelayedAsync()
{
var fullMessage = ProtocolUnitTest.StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd");
var m = new MemoryStream();
m.Write(fullMessage, 0, 2);
var tcs = new TaskCompletionSource<bool>();
tcs.Task.ContinueWith(x => m.Write(fullMessage, 2, fullMessage.Length - 2));
var t = TPKT.ReadAsync(m);
tcs.TrySetResult(true);
await t;
}
[TestMethod]
public void TPKT_ReadDelayed()
{
var fullMessage = ProtocolUnitTest.StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd");
var m = new MemoryStream();
m.Write(fullMessage, 0, 2);
var tcs = new TaskCompletionSource<bool>();
tcs.Task.ContinueWith(x => m.Write(fullMessage, 2, fullMessage.Length - 2));
Task.Delay(TimeSpan.FromSeconds(0.01)).ContinueWith(x => tcs.TrySetResult(true));
var t = TPKT.Read(m);
tcs.TrySetResult(true);
}
[TestMethod]
[ExpectedException(typeof(TPKTInvalidException))]
@@ -67,7 +41,7 @@ namespace S7.Net.UnitTest
{
var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff040080"));
var t = await TPKT.ReadAsync(m);
}
}
[TestMethod]
public void COTP_ReadTSDU()
@@ -81,7 +55,7 @@ namespace S7.Net.UnitTest
Assert.IsTrue(expected.SequenceEqual(t));
}
private static byte[] StringToByteArray(string hex)
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
@@ -89,4 +63,5 @@ namespace S7.Net.UnitTest
.ToArray();
}
}
}

View File

@@ -0,0 +1,82 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace S7.Net.UnitTest
{
/// <summary>
/// Test stream which only gives 1 byte per read.
/// </summary>
class TestStream1BytePerRead : Stream
{
public TestStream1BytePerRead(byte[] data)
{
Data = data;
}
public override bool CanRead => _position < Data.Length;
public override bool CanSeek => throw new NotImplementedException();
public override bool CanWrite => throw new NotImplementedException();
public override long Length => throw new NotImplementedException();
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public byte[] Data { get; }
public override void Flush()
{
throw new NotImplementedException();
}
int _position = 0;
public override int Read(byte[] buffer, int offset, int count)
{
buffer[offset] = Data[_position];
++_position;
return 1;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}
[TestClass]
public class StreamTests
{
[TestMethod]
public async Task TPKT_ReadRestrictedStreamAsync()
{
var fullMessage = ProtocolUnitTest.StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd");
var m = new TestStream1BytePerRead(fullMessage);
var t = await TPKT.ReadAsync(m);
Assert.AreEqual(fullMessage.Length, t.Length);
Assert.AreEqual(fullMessage.Last(), t.Data.Last());
}
[TestMethod]
public void TPKT_ReadRestrictedStream()
{
var fullMessage = ProtocolUnitTest.StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd");
var m = new TestStream1BytePerRead(fullMessage);
var t = TPKT.Read(m);
Assert.AreEqual(fullMessage.Length, t.Length);
Assert.AreEqual(fullMessage.Last(), t.Data.Last());
}
}
}