Files

89 lines
3.2 KiB
C#

using Heimdall.Domain.Enums;
using Heimdall.Infrastructure.Services.Discovery;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
public class CameraDiscoveryServiceTests
{
// ─── Subnet Expansion ─────────────────────────────────────────────
[Theory]
[InlineData("192.168.1.0/30", 2)] // /30 = 2 usable hosts
[InlineData("10.0.0.0/31", 2)] // /31 = 2 hosts (point-to-point)
[InlineData("172.16.0.0/28", 14)] // /28 = 14 usable hosts
public void ExpandSubnet_ValidCidr_ReturnsExpectedCount(string cidr, int expectedCount)
{
var ips = CameraDiscoveryService.ExpandSubnet(cidr);
ips.Count.ShouldBe(expectedCount);
}
[Fact]
public void ExpandSubnet_24Mask_Returns254Hosts()
{
var ips = CameraDiscoveryService.ExpandSubnet("192.168.1.0/24");
ips.Count.ShouldBe(254);
ips[0].ShouldBe("192.168.1.1");
ips[^1].ShouldBe("192.168.1.254");
}
[Fact]
public void ExpandSubnet_InvalidCidr_ReturnsEmpty()
{
CameraDiscoveryService.ExpandSubnet("invalid").ShouldBeEmpty();
}
[Fact]
public void ExpandSubnet_NoPrefix_ReturnsEmpty()
{
CameraDiscoveryService.ExpandSubnet("192.168.1.0").ShouldBeEmpty();
}
[Fact]
public void ExpandSubnet_InvalidPrefix_ReturnsEmpty()
{
CameraDiscoveryService.ExpandSubnet("192.168.1.0/33").ShouldBeEmpty();
}
[Fact]
public void ExpandSubnet_16Mask_Returns65534Hosts()
{
// /16 = 65536 addresses minus network + broadcast = 65534 usable
var ips = CameraDiscoveryService.ExpandSubnet("10.0.0.0/16");
ips.Count.ShouldBe(65534);
ips[0].ShouldBe("10.0.0.1");
ips[^1].ShouldBe("10.0.255.254");
}
[Fact]
public void ExpandSubnet_LargeCidr_CappedAt65536()
{
var ips = CameraDiscoveryService.ExpandSubnet("10.0.0.0/8");
ips.Count.ShouldBe(65536);
}
// ─── Camera Classification ───────────────────────────────────────
// These test the private ClassifyCamera method indirectly via ProbeIpAsync
// We can't test the private method directly, but we test the logic through
// the subnet expansion and classification helpers.
[Fact]
public void ExpandSubnet_32Mask_ReturnsOneHost()
{
// /32 = single host (the network address IS the host address for /32)
var ips = CameraDiscoveryService.ExpandSubnet("192.168.1.1/32");
ips.Count.ShouldBe(1);
ips[0].ShouldBe("192.168.1.1");
}
// ─── Camera DB Search ────────────────────────────────────────────
// SearchDatabaseAsync is tested via the service. The method itself
// filters from the static BuiltInCameraDb array. Integration tests
// (DiscoveryControllerTests.SearchDatabase_*) confirm the API contract.
// Stream URL template correctness is verified by integration test
// BuildStreamUrls_ValidRequest_ReturnsUrls in DiscoveryControllerTests
}