//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.Network.Tests;
using System.IO.Pipelines;
using Microsoft.Extensions.Logging.Abstractions;
///
/// Tests if are thrown when malformed data is read by a .
///
[TestFixture]
public class InvalidPacketHeaderExceptionTest
{
private readonly byte[] _malformedData = { 0xC1, 0x03, 0xFF, 0x00, 0x00, 0x00 };
///
/// Tests if the exception is thrown.
///
/// The async task.
[Test]
public async Task ThrownAsync()
{
await this.TestExceptionAsync(e => { }).ConfigureAwait(false);
}
///
/// Tests if is assigned correctly.
///
/// The async task.
[Test]
public async Task TestHeaderAsync()
{
await this.TestExceptionAsync(e => Assert.That(e.Header, Is.EquivalentTo(new byte[] { 0x00, 0x00, 0x00 }))).ConfigureAwait(false);
}
///
/// Tests if is assigned correctly.
///
/// The async task.
[Test]
public async Task TestPositionAsync()
{
await this.TestExceptionAsync(e => Assert.That(e.Position, Is.EqualTo(3))).ConfigureAwait(false);
}
///
/// Tests if is assigned correctly.
///
/// The async task.
[Test]
public async Task TestBufferContentAsync()
{
await this.TestExceptionAsync(e => Assert.That(e.BufferContent, Is.EquivalentTo(this._malformedData))).ConfigureAwait(false);
}
private async ValueTask TestExceptionAsync(Action check)
{
bool thrown = false;
var duplexPipe = new DuplexPipe(new PipeOptions(pauseWriterThreshold: 1, resumeWriterThreshold: 1));
using var connection = new Connection(duplexPipe, null, new Xor.PipelinedXor32Encryptor(duplexPipe.Output), new NullLogger());
_ = connection.BeginReceiveAsync();
try
{
_ = await duplexPipe.ReceivePipe.Writer.WriteAsync(this._malformedData).ConfigureAwait(false);
}
catch (InvalidPacketHeaderException e)
{
thrown = true;
check(e);
}
catch (Exception e)
{
Assert.Fail($"Wrong exception type {e}", e);
}
Assert.That(thrown);
}
}