Skip to content

Commit

Permalink
Ignore invalid frames
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
andreashuber-lawo committed Nov 30, 2016
1 parent 0fe0115 commit 9b2c8fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 7 additions & 3 deletions Lawo.EmberPlusSharp/S101/DeframingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,22 @@ private bool ReadByte(ReadBuffer readBuffer)
case Frame.EscapeByte:
this.state = State.InFrameEscaped;
break;
case Frame.BeginOfFrame:
this.decodedQueue.Clear();
break;
case Frame.EndOfFrame:
this.state = State.AfterFrame;

if (this.crc != 0xF0B8)
{
throw new S101Exception("CRC failed.");
this.decodedQueue.Clear();
}

return false;
default:
this.state = State.AfterFrame;
throw new S101Exception("Invalid byte in frame.");
this.decodedQueue.Clear();
break;
}
}

Expand All @@ -132,7 +136,7 @@ private bool ReadByte(ReadBuffer readBuffer)
if (currentByte >= Frame.InvalidStart)
{
this.state = State.AfterFrame;
throw new S101Exception("Invalid escaped byte.");
this.decodedQueue.Clear();
}

currentByte = (byte)(currentByte ^ Frame.EscapeXor);
Expand Down
15 changes: 11 additions & 4 deletions Lawo.EmberPlusSharpTest/S101/S101ReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ public void ExceptionTest()
() => reader.Message.Ignore(), () => reader.Payload.Ignore());
}

await AssertS101Exception("CRC failed.", 0xFE, 0xFF);
await AssertS101Exception("Invalid byte in frame.", 0xFE, 0xFE);
await AssertEmpty(0xFE, 0xFF);
await AssertEmpty(0xFE, 0xFE);

for (byte invalid = 0xF8; invalid < 0xFD; ++invalid)
{
await AssertS101Exception("Invalid byte in frame.", 0xFE, invalid);
await AssertEmpty(0xFE, invalid);
}

for (ushort invalid = 0xF8; invalid < 0x100; ++invalid)
{
await AssertS101Exception("Invalid escaped byte.", 0xFE, 0xFD, (byte)invalid);
await AssertEmpty(0xFE, 0xFD, (byte)invalid);
}

await AssertS101Exception("Unexpected end of stream.", 0xFE, 0x00, 0x00, 0x00);
Expand Down Expand Up @@ -186,6 +186,13 @@ await AssertThrowAsync<ObjectDisposedException>(
}
}

private static async Task AssertEmpty(params byte[] input)
{
var reader = CreateAsyncReader(input);
Assert.IsFalse(await reader.ReadAsync(CancellationToken.None));
await reader.DisposeAsync(CancellationToken.None);
}

private static async Task AssertS101Exception(string message, params byte[] input)
{
var reader = CreateAsyncReader(input);
Expand Down

0 comments on commit 9b2c8fb

Please sign in to comment.