2026-08-11 22:22:31 +02:00
|
|
|
using System.Buffers.Binary;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Heimdall.Agent.Services;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
|
|
|
|
|
|
public class DvripHandshakeTests
|
|
|
|
|
{
|
|
|
|
|
private static readonly System.Reflection.MethodInfo SofiaHashMethod = typeof(NetworkCameraScanner)
|
|
|
|
|
.GetMethod("SofiaHash", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
|
|
|
|
|
?? throw new InvalidOperationException("SofiaHash not found");
|
|
|
|
|
|
|
|
|
|
private static readonly System.Reflection.MethodInfo BuildPacketMethod = typeof(NetworkCameraScanner)
|
|
|
|
|
.GetMethod("BuildDvripPacket", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
|
|
|
|
|
?? throw new InvalidOperationException("BuildDvripPacket not found");
|
|
|
|
|
|
|
|
|
|
private static string SofiaHash(string password)
|
|
|
|
|
=> (string)SofiaHashMethod.Invoke(null, [password])!;
|
|
|
|
|
|
|
|
|
|
private static byte[] BuildPacket(string payload, int cmd, uint session, uint seq)
|
|
|
|
|
=> (byte[])BuildPacketMethod.Invoke(null, [payload, cmd, session, seq])!;
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SofiaHash_MatchesGo2RtcReference()
|
|
|
|
|
{
|
|
|
|
|
// Reference vector computed with the go2rtc SofiaHash algorithm
|
|
|
|
|
// (verified independently with Python hashlib):
|
|
|
|
|
// md5("admin") = 21232f297a57a5a743894a0e4a801fc3
|
|
|
|
|
// pairs: 0x21+0x23=68→chars[6]='6', 0x2f+0x29=88→chars[26]='Q',
|
|
|
|
|
// 0x7a+0x57=209→chars[23]='N', 0xa5+0xa7=332%62=22→chars[22]='M',
|
|
|
|
|
// 0x43+0x89=204%62=18→chars[18]='I', 0x4a+0x0e=88→chars[26]='Q',
|
|
|
|
|
// 0x4a+0x80=202%62=16→chars[16]='G', 0x1f+0xc3=226%62=40→chars[40]='e'
|
|
|
|
|
SofiaHash("admin").ShouldBe("6QNMIQGe");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void BuildDvripPacket_HeaderMatchesGo2RtcLayout()
|
|
|
|
|
{
|
|
|
|
|
var payload = "{\"Ret\":100}\x0A\x00";
|
|
|
|
|
var packet = BuildPacket(payload, cmd: 1000, session: 0, seq: 0);
|
|
|
|
|
|
|
|
|
|
packet.Length.ShouldBe(20 + payload.Length);
|
|
|
|
|
packet[0].ShouldBe((byte)0xFF);
|
|
|
|
|
BinaryPrimitives.ReadUInt32LittleEndian(packet.AsSpan(4, 4)).ShouldBe(0u); // session
|
|
|
|
|
BinaryPrimitives.ReadUInt32LittleEndian(packet.AsSpan(8, 4)).ShouldBe(0u); // seq
|
|
|
|
|
BinaryPrimitives.ReadUInt16LittleEndian(packet.AsSpan(14, 2)).ShouldBe((ushort)1000); // cmd
|
|
|
|
|
BinaryPrimitives.ReadUInt32LittleEndian(packet.AsSpan(16, 4)).ShouldBe((uint)payload.Length);
|
|
|
|
|
Encoding.UTF8.GetString(packet, 20, payload.Length).ShouldBe(payload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void BuildDvripPacket_LoginPayloadContainsSofiaHash()
|
|
|
|
|
{
|
|
|
|
|
var payload = "{\"EncryptType\":\"MD5\",\"LoginType\":\"DVRIP-Web\",\"PassWord\":\""
|
|
|
|
|
+ SofiaHash("admin") + "\",\"UserName\":\"admin\"}\x0A\x00";
|
|
|
|
|
var packet = BuildPacket(payload, cmd: 1000, session: 0, seq: 0);
|
|
|
|
|
|
|
|
|
|
Encoding.UTF8.GetString(packet, 20, payload.Length).ShouldContain("\"LoginType\":\"DVRIP-Web\"");
|
|
|
|
|
Encoding.UTF8.GetString(packet, 20, payload.Length).ShouldContain($"\"PassWord\":\"{SofiaHash("admin")}\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_AcceptsOptionalProbe()
|
|
|
|
|
{
|
2026-10-11 21:23:16 +02:00
|
|
|
var scanner = new NetworkCameraScanner(
|
|
|
|
|
Substitute.For<ILogger<NetworkCameraScanner>>(),
|
|
|
|
|
Substitute.For<ILoggerFactory>());
|
2026-08-11 22:22:31 +02:00
|
|
|
scanner.ShouldNotBeNull();
|
|
|
|
|
}
|
2026-10-11 21:23:16 +02:00
|
|
|
}
|