using System.Buffers.Binary;
using System.Text;
using Heimdall.Agent.Services;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;
namespace Heimdall.Agent.Tests;
public class HomeKitMdnsProbeTests
{
private static readonly System.Reflection.MethodInfo ParsePacketMethod = typeof(HomeKitMdnsProbe)
.GetMethod("ParsePacket", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
?? throw new InvalidOperationException("ParsePacket not found");
/// Build a minimal mDNS response packet with PTR + SRV + TXT + A records.
private static byte[] BuildMdnsPacket()
{
using var ms = new MemoryStream();
Span scratch = stackalloc byte[8];
// Header: ID=0x1234, flags=0x8400 (response), 0 questions, 4 answers,
// 0 authority, 0 additional (12 octets).
BinaryPrimitives.WriteUInt16BigEndian(scratch, 0x1234);
BinaryPrimitives.WriteUInt16BigEndian(scratch[2..], 0x8400);
BinaryPrimitives.WriteUInt16BigEndian(scratch[4..], 0); // QDCOUNT
BinaryPrimitives.WriteUInt16BigEndian(scratch[6..], 4); // ANCOUNT
ms.Write(scratch[..8]);
BinaryPrimitives.WriteUInt16BigEndian(scratch, 0); // NSCOUNT
BinaryPrimitives.WriteUInt16BigEndian(scratch[2..], 0); // ARCOUNT
ms.Write(scratch[..4]);
// Answer 1: PTR _hap._tcp.local → Camera-ABCDEF._hap._tcp.local
WriteName(ms, "_hap._tcp.local");
BinaryPrimitives.WriteUInt16BigEndian(scratch, 12); // PTR
BinaryPrimitives.WriteUInt16BigEndian(scratch[2..], 0x0001); // IN
BinaryPrimitives.WriteUInt32BigEndian(scratch[4..], 4500); // TTL
ms.Write(scratch);
var ptrName = EncodeName("Camera-ABCDEF._hap._tcp.local");
BinaryPrimitives.WriteUInt16BigEndian(scratch, (ushort)ptrName.Length);
ms.Write(scratch[..2]);
ms.Write(ptrName);
// Answer 2: SRV Camera-ABCDEF._hap._tcp.local → cam-abc.local:8080
WriteName(ms, "Camera-ABCDEF._hap._tcp.local");
BinaryPrimitives.WriteUInt16BigEndian(scratch, 33); // SRV
BinaryPrimitives.WriteUInt16BigEndian(scratch[2..], 0x0001);
BinaryPrimitives.WriteUInt32BigEndian(scratch[4..], 120);
ms.Write(scratch);
var srvTarget = EncodeName("cam-abc.local");
var srvRdata = new byte[6 + srvTarget.Length];
srvRdata[4] = 0x1F; srvRdata[5] = 0x90; // port 8080 big-endian
Array.Copy(srvTarget, 0, srvRdata, 6, srvTarget.Length);
BinaryPrimitives.WriteUInt16BigEndian(scratch, (ushort)srvRdata.Length);
ms.Write(scratch[..2]);
ms.Write(srvRdata);
// Answer 3: TXT Camera-ABCDEF._hap._tcp.local → ci=17, id=AA:BB:CC, md=CamModel, sf=0
WriteName(ms, "Camera-ABCDEF._hap._tcp.local");
BinaryPrimitives.WriteUInt16BigEndian(scratch, 16); // TXT
BinaryPrimitives.WriteUInt16BigEndian(scratch[2..], 0x0001);
BinaryPrimitives.WriteUInt32BigEndian(scratch[4..], 4500);
ms.Write(scratch);
var txtEntries = new List { "ci=17", "id=AA:BB:CC:DD:EE:FF", "md=CamModel", "sf=0" };
var txtBytes = new List();
foreach (var entry in txtEntries)
{
var e = Encoding.ASCII.GetBytes(entry);
txtBytes.Add((byte)e.Length);
txtBytes.AddRange(e);
}
BinaryPrimitives.WriteUInt16BigEndian(scratch, (ushort)txtBytes.Count);
ms.Write(scratch[..2]);
ms.Write(txtBytes.ToArray());
// Answer 4: A cam-abc.local → 192.168.1.42
WriteName(ms, "cam-abc.local");
BinaryPrimitives.WriteUInt16BigEndian(scratch, 1); // A
BinaryPrimitives.WriteUInt16BigEndian(scratch[2..], 0x0001);
BinaryPrimitives.WriteUInt32BigEndian(scratch[4..], 120);
ms.Write(scratch);
BinaryPrimitives.WriteUInt16BigEndian(scratch, 4);
ms.Write(scratch[..2]);
ms.Write(new byte[] { 192, 168, 1, 42 });
return ms.ToArray();
}
private static void WriteName(MemoryStream ms, string name)
{
ms.Write(EncodeName(name));
}
private static byte[] EncodeName(string name)
{
using var ms = new MemoryStream();
foreach (var label in name.TrimEnd('.').Split('.'))
{
if (label.Length == 0) continue;
ms.WriteByte((byte)label.Length);
ms.Write(Encoding.ASCII.GetBytes(label));
}
ms.WriteByte(0);
return ms.ToArray();
}
[Fact]
public void ParsePacket_HomeKitCamera_ParsesPtrSrvTxtA()
{
var packet = BuildMdnsPacket();
var replies = ((System.Collections.IEnumerable)ParsePacketMethod.Invoke(null, [packet])!)
.Cast