Skip to content

Commit

Permalink
Verify the bytes written by OutOfFrameByteTest
Browse files Browse the repository at this point in the history
References #2
  • Loading branch information
andreashuber-lawo committed Nov 8, 2015
1 parent 6a7fa34 commit cdbee19
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
28 changes: 28 additions & 0 deletions Lawo.EmberPlusSharpTest/S101/CommunicationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Lawo.EmberPlusSharp.S101
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
Expand All @@ -26,6 +27,8 @@ public abstract class CommunicationTestBase : TestBase
{
private static readonly EmberData EmberDataCommandField = new EmberData(0x01, 0x0A, 0x02);
private static readonly S101Message EmberDataMessageField = new S101Message(0x00, EmberDataCommandField);
private static readonly S101Message KeepAliveRequestMessageField = new S101Message(0x00, new KeepAliveRequest());
private static readonly S101Message KeepAliveResponseMessageField = new S101Message(0x00, new KeepAliveResponse());

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -47,6 +50,18 @@ protected static S101Message EmberDataMessage
get { return EmberDataMessageField; }
}

/// <summary>Gets a <see cref="S101Message"/> message with an <see cref="KeepAliveRequest"/> command.</summary>
protected static S101Message KeepAliveRequestMessage
{
get { return KeepAliveRequestMessageField; }
}

/// <summary>Gets a <see cref="S101Message"/> message with an <see cref="KeepAliveResponse"/> command.</summary>
protected static S101Message KeepAliveResponseMessage
{
get { return KeepAliveResponseMessageField; }
}

/// <summary>Uses <see cref="S101Robot"/> to simulate a provider communicating with the <see cref="S101Client"/>
/// object passed to <paramref name="testCallback"/>.</summary>
/// <typeparam name="TResourceNamespace">The type whose namespace is used to scope
Expand Down Expand Up @@ -232,6 +247,19 @@ protected static async Task MonitorConnection<TConnection>(
await waitForConnectionLost;
}

/// <summary>Gets a random byte that is guaranteed to not be equal to any of the elements in
/// <paramref name="exceptions"/>.</summary>
protected byte GetRandomByteExcept(params byte[] exceptions)
{
byte result;

while (exceptions.Contains(result = (byte)this.Random.Next(byte.MinValue, byte.MaxValue + 1)))
{
}

return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private static Task<int> Read(
Expand Down
13 changes: 0 additions & 13 deletions Lawo.EmberPlusSharpTest/S101/S101ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,5 @@ public void VersionTest()
null,
null));
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private byte GetRandomByteExcept(params byte[] exceptions)
{
byte result;

while (exceptions.Contains(result = (byte)this.Random.Next(byte.MinValue, byte.MaxValue + 1)))
{
}

return result;
}
}
}
69 changes: 54 additions & 15 deletions Lawo.EmberPlusSharpTest/S101/S101WriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,27 @@ public void OutOfFrameByteTest()
AsyncPump.Run(
async () =>
{
var first = GetRandomByteExcept(0xFE);
var second = GetRandomByteExcept(0xFE);

var prefix = new[] { GetRandomByteExcept() };
var third = GetRandomByteExcept(0xFE);
var postfix = new[] { GetRandomByteExcept() };

using (var asyncStream = new MemoryStream())
{
var writer = new S101Writer(asyncStream.WriteAsync);

try
{
await writer.WriteMessageAsync(
new S101Message(0x00, new KeepAliveRequest()), CancellationToken.None);
await writer.WriteOutOfFrameByte((byte)this.Random.Next(0xFE), CancellationToken.None);
await writer.WriteMessageAsync(
new S101Message(0x00, new KeepAliveResponse()), CancellationToken.None);
await writer.WriteOutOfFrameByte(first, CancellationToken.None);
await writer.WriteMessageAsync(KeepAliveRequestMessage, CancellationToken.None);
await writer.WriteOutOfFrameByte(second, CancellationToken.None);

using (var encodingStream = await writer.WriteMessageAsync(EmberDataMessage, CancellationToken.None))
{
var prefix = new byte[] { 0x42 };
var postfix = new byte[] { 0x43 };
await encodingStream.WriteAsync(prefix, 0, prefix.Length, CancellationToken.None);
await writer.WriteOutOfFrameByte(0xEE, CancellationToken.None);
await writer.WriteOutOfFrameByte(third, CancellationToken.None);
await encodingStream.WriteAsync(postfix, 0, postfix.Length, CancellationToken.None);
await encodingStream.DisposeAsync(CancellationToken.None);
}
Expand All @@ -81,9 +84,30 @@ await writer.WriteMessageAsync(
await writer.DisposeAsync(CancellationToken.None);
}

var stringBytes =
asyncStream.ToArray().Select(b => b.ToString("X2", CultureInfo.InvariantCulture));
var result = string.Join(", ", stringBytes).ToUpperInvariant();
asyncStream.Position = 0;
var reader = new S101Reader(asyncStream.ReadAsync);
var firstTask = WaitForOutOfFrameByte(reader);
Assert.IsTrue(await reader.ReadAsync(CancellationToken.None));
Assert.AreEqual(0x00, reader.Message.Slot);
Assert.IsInstanceOfType(reader.Message.Command, typeof(KeepAliveRequest));
Assert.AreEqual(first, await firstTask);
var secondTask = WaitForOutOfFrameByte(reader);
Assert.IsTrue(await reader.ReadAsync(CancellationToken.None));
Assert.AreEqual(0x00, reader.Message.Slot);
Assert.IsInstanceOfType(reader.Message.Command, typeof(EmberData));
Assert.AreEqual(second, await secondTask);
var thirdTask = WaitForOutOfFrameByte(reader);

using (var payloadStream = new MemoryStream())
{
await reader.Payload.CopyToAsync(payloadStream);
var payload = payloadStream.ToArray();
Assert.AreEqual(2, payload.Length);
Assert.AreEqual(prefix.Single(), payload[0]);
Assert.AreEqual(postfix.Single(), payload[1]);
}

Assert.AreEqual(third, await thirdTask);
}
});
}
Expand Down Expand Up @@ -190,21 +214,36 @@ await AssertThrowAsync<NotSupportedException>(
() => stream.Position = 0,
() => stream.Seek(0, SeekOrigin.Begin));

await AssertThrowAsync<InvalidOperationException>(
() => writer.WriteMessageAsync(new S101Message(0x00, new KeepAliveRequest()), CancellationToken.None));
await AssertThrowAsync<InvalidOperationException>(() => writer.WriteMessageAsync(
new S101Message(0x00, new KeepAliveRequest()), CancellationToken.None));
await stream.DisposeAsync(CancellationToken.None);
await AssertThrowAsync<ObjectDisposedException>(
() => stream.WriteAsync(new byte[] { 2 }, 0, 1, CancellationToken.None));
}

await writer.DisposeAsync(CancellationToken.None);
await AssertThrowAsync<ObjectDisposedException>(
() => writer.WriteMessageAsync(new S101Message(0x00, new KeepAliveRequest()), CancellationToken.None));
await AssertThrowAsync<ObjectDisposedException>(() => writer.WriteMessageAsync(
new S101Message(0x00, new KeepAliveRequest()), CancellationToken.None));
});
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private Task<byte> WaitForOutOfFrameByte(S101Reader reader)
{
var resultSource = new TaskCompletionSource<byte>();
EventHandler<OutOfFrameByteReceivedEventArgs> handler = null;

handler = (s, e) =>
{
reader.OutOfFrameByteReceived -= handler;
resultSource.SetResult(e.Value);
};

reader.OutOfFrameByteReceived += handler;
return resultSource.Task;
}

private static async Task<byte[]> Encode(S101Message message, byte[] payload = null)
{
using (var asyncStream = new MemoryStream())
Expand Down

0 comments on commit cdbee19

Please sign in to comment.