Scaffolded c# app
This commit is contained in:
parent
0272c5ac3a
commit
387a6ac182
57 changed files with 3930 additions and 0 deletions
|
|
@ -0,0 +1,181 @@
|
|||
using System.Buffers.Binary;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Surveillance.Client.Domain;
|
||||
using Surveillance.Client.Protocol;
|
||||
using Surveillance.Protocol.RSNet;
|
||||
using Xunit;
|
||||
|
||||
namespace Surveillance.Protocol.RSNet.Tests;
|
||||
|
||||
public sealed class RsNetSessionFactoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void DecodesCapturedNativeModeZeroRequestHeader()
|
||||
{
|
||||
var header = Convert.FromHexString("02000601001000400100040082000083");
|
||||
|
||||
TransformBits(header.AsSpan(2), decode: true);
|
||||
|
||||
Assert.Equal(0x02u, BinaryPrimitives.ReadUInt32LittleEndian(header.AsSpan(0, 4)));
|
||||
Assert.Equal(0x1fu, BinaryPrimitives.ReadUInt32LittleEndian(header.AsSpan(4, 4)));
|
||||
Assert.Equal(0x50u, BinaryPrimitives.ReadUInt32LittleEndian(header.AsSpan(8, 4)));
|
||||
Assert.Equal(348u, BinaryPrimitives.ReadUInt32LittleEndian(header.AsSpan(12, 4)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendsReconstructedLoginFrameAndAcceptsSuccessfulResponse()
|
||||
{
|
||||
using var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
||||
var endpoint = new DeviceEndpoint(IPAddress.Loopback.ToString(), ((IPEndPoint)listener.LocalEndpoint).Port);
|
||||
var serverTask = ReceiveLoginAndRespondAsync(listener, 0, SuccessfulResponsePayload(), timeout.Token);
|
||||
var factory = new RsNetSessionFactory();
|
||||
await using var session = await factory.ConnectAsync(endpoint, timeout.Token);
|
||||
var result = await session.AuthenticateAsync(
|
||||
new DeviceCredentials("viewer", "secret"),
|
||||
timeout.Token);
|
||||
var request = await serverTask;
|
||||
|
||||
Assert.True(result.IsAuthenticated);
|
||||
Assert.Equal(0x02u, BinaryPrimitives.ReadUInt32LittleEndian(request.Header.AsSpan(0, 4)));
|
||||
Assert.Equal(0x1fu, BinaryPrimitives.ReadUInt32LittleEndian(request.Header.AsSpan(4, 4)));
|
||||
Assert.Equal(0x50u, BinaryPrimitives.ReadUInt32LittleEndian(request.Header.AsSpan(8, 4)));
|
||||
Assert.NotEqual(0u, BinaryPrimitives.ReadUInt32LittleEndian(request.Header.AsSpan(12, 4)));
|
||||
|
||||
var expectedPayload = new byte[0x50];
|
||||
Encoding.UTF8.GetBytes("viewer").CopyTo(expectedPayload, 0);
|
||||
Encoding.UTF8.GetBytes("secret").CopyTo(expectedPayload, 0x24);
|
||||
Assert.Equal(expectedPayload, request.Payload);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RejectsNonzeroLoginStatus()
|
||||
{
|
||||
using var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
||||
var endpoint = new DeviceEndpoint(IPAddress.Loopback.ToString(), ((IPEndPoint)listener.LocalEndpoint).Port);
|
||||
var serverTask = ReceiveLoginAndRespondAsync(listener, 10, [], timeout.Token);
|
||||
var factory = new RsNetSessionFactory();
|
||||
await using var session = await factory.ConnectAsync(endpoint, timeout.Token);
|
||||
|
||||
var result = await session.AuthenticateAsync(new DeviceCredentials("viewer", "wrong"), timeout.Token);
|
||||
await serverTask;
|
||||
|
||||
Assert.False(result.IsAuthenticated);
|
||||
Assert.Equal(AuthenticationFailure.Unknown, result.Failure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AcceptsResponseWithNativeModeFlagsAndExtendedHeader()
|
||||
{
|
||||
using var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
||||
var endpoint = new DeviceEndpoint(IPAddress.Loopback.ToString(), ((IPEndPoint)listener.LocalEndpoint).Port);
|
||||
var serverTask = ReceiveLoginAndRespondAsync(
|
||||
listener,
|
||||
0,
|
||||
SuccessfulResponsePayload(),
|
||||
timeout.Token,
|
||||
responseFlags: 0x06);
|
||||
var factory = new RsNetSessionFactory();
|
||||
await using var session = await factory.ConnectAsync(endpoint, timeout.Token);
|
||||
|
||||
var result = await session.AuthenticateAsync(new DeviceCredentials("viewer", "secret"), timeout.Token);
|
||||
await serverTask;
|
||||
|
||||
Assert.True(result.IsAuthenticated);
|
||||
}
|
||||
|
||||
private static async Task<(byte[] Header, byte[] Payload)> ReceiveLoginAndRespondAsync(
|
||||
TcpListener listener,
|
||||
ushort status,
|
||||
byte[] responsePayload,
|
||||
CancellationToken cancellationToken,
|
||||
byte responseFlags = 0)
|
||||
{
|
||||
using var client = await listener.AcceptTcpClientAsync(cancellationToken);
|
||||
var stream = client.GetStream();
|
||||
var header = new byte[16];
|
||||
var payload = new byte[0x50];
|
||||
await stream.ReadExactlyAsync(header, cancellationToken);
|
||||
await stream.ReadExactlyAsync(payload, cancellationToken);
|
||||
if ((header[0] & 0x03) == 0x02)
|
||||
{
|
||||
TransformBits(header.AsSpan(2), decode: true);
|
||||
TransformBits(payload, decode: true);
|
||||
}
|
||||
|
||||
var additionalHeaderLength = (responseFlags >> 2) * sizeof(uint);
|
||||
var responseHeader = new byte[16 + additionalHeaderLength];
|
||||
responseHeader[0] = responseFlags;
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(responseHeader.AsSpan(2, 2), status);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(responseHeader.AsSpan(8, 4), (uint)responsePayload.Length);
|
||||
var wireResponsePayload = responsePayload.ToArray();
|
||||
if ((responseFlags & 0x03) == 0x02)
|
||||
{
|
||||
TransformBits(responseHeader.AsSpan(2), decode: false);
|
||||
TransformBits(wireResponsePayload, decode: false);
|
||||
}
|
||||
|
||||
await stream.WriteAsync(responseHeader, cancellationToken);
|
||||
await stream.WriteAsync(wireResponsePayload, cancellationToken);
|
||||
return (header, payload);
|
||||
}
|
||||
|
||||
private static void TransformBits(Span<byte> data, bool decode)
|
||||
{
|
||||
var bitCount = data.Length * 8;
|
||||
if (bitCount == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var next = new int[bitCount];
|
||||
for (var index = 0; index < bitCount; index++)
|
||||
{
|
||||
next[index] = (index + 1) % bitCount;
|
||||
}
|
||||
|
||||
var current = bitCount - 1;
|
||||
var remaining = bitCount;
|
||||
for (var step = 0; step < 2 % remaining; step++)
|
||||
{
|
||||
current = next[current];
|
||||
}
|
||||
|
||||
var transformed = new byte[data.Length];
|
||||
for (var bitIndex = 0; bitIndex < bitCount; bitIndex++)
|
||||
{
|
||||
for (var step = 0; step < 2 % remaining; step++)
|
||||
{
|
||||
current = next[current];
|
||||
}
|
||||
|
||||
var selected = next[current];
|
||||
var sourceBitIndex = decode ? bitIndex : selected;
|
||||
var destinationBitIndex = decode ? selected : bitIndex;
|
||||
if ((data[sourceBitIndex / 8] & (0x80 >> (sourceBitIndex % 8))) != 0)
|
||||
{
|
||||
transformed[destinationBitIndex / 8] |= (byte)(0x80 >> (destinationBitIndex % 8));
|
||||
}
|
||||
|
||||
next[current] = next[selected];
|
||||
remaining--;
|
||||
}
|
||||
|
||||
transformed.AsSpan().CopyTo(data);
|
||||
}
|
||||
|
||||
private static byte[] SuccessfulResponsePayload()
|
||||
{
|
||||
var payload = new byte[8];
|
||||
payload[6] = (byte)'S';
|
||||
payload[7] = (byte)'R';
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="xunit.v3" />
|
||||
<ProjectReference Include="..\..\src\Surveillance.Protocol.RSNet\Surveillance.Protocol.RSNet.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue