Files
Heimdall/tests/Heimdall.Infrastructure.Tests/PortScanServiceTests.cs
T

45 lines
1.4 KiB
C#
Raw Normal View History

using Heimdall.Infrastructure.Services.Discovery;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
/// <summary>
/// Tests for PortScanService.
/// Real port scans require network access; these tests verify the
/// service structure, configuration, and error handling.
/// </summary>
public class PortScanServiceTests
{
private readonly ILogger<PortScanService> _logger = Substitute.For<ILogger<PortScanService>>();
[Fact]
public async Task ScanAsync_InvalidIp_ReturnsEmpty()
{
var service = new PortScanService(_logger);
// Use an IP in the TEST-NET range (RFC 5737) that should not respond
var result = await service.ScanAsync("192.0.2.1");
result.ShouldBeEmpty();
}
[Fact]
public async Task ScanAsync_NonExistentHost_ReturnsEmpty()
{
// 10.255.255.1 is in a reserved range, unlikely to respond
var service = new PortScanService(_logger);
var result = await service.ScanAsync("10.255.255.1");
result.ShouldBeEmpty();
}
[Fact]
public async Task ScanAsync_WithCancellation_Throws()
{
var service = new PortScanService(_logger);
using var cts = new CancellationTokenSource();
cts.Cancel();
await Should.ThrowAsync<OperationCanceledException>(async () =>
await service.ScanAsync("192.168.1.1", cts.Token));
}
}