109 lines
3.1 KiB
C#
109 lines
3.1 KiB
C#
using System.Net;
|
|
using Flypaper.Core.Models;
|
|
using Flypaper.GeoIp;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using NSubstitute.ExceptionExtensions;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.GeoIp;
|
|
|
|
public sealed class DbIpEnricherTests
|
|
{
|
|
private static readonly IpOrigin Germany = new("DE", "Deutschland", "Berlin", 3320, "Deutsche Telekom AG");
|
|
private static readonly IPAddress PublicIp = IPAddress.Parse("203.0.113.9");
|
|
|
|
private readonly IGeoIpDatabaseFactory _factory = Substitute.For<IGeoIpDatabaseFactory>();
|
|
private readonly IGeoIpDatabase _database = Substitute.For<IGeoIpDatabase>();
|
|
|
|
[Fact]
|
|
public void Returns_origin_for_public_ip()
|
|
{
|
|
_database.Find(PublicIp).Returns(Germany);
|
|
_factory.Open().Returns(_database);
|
|
|
|
var enricher = CreateEnricher();
|
|
|
|
enricher.IsAvailable.ShouldBeTrue();
|
|
enricher.Lookup(PublicIp).ShouldBe(Germany);
|
|
}
|
|
|
|
[Fact]
|
|
public void Looks_up_ipv4_mapped_addresses_as_ipv4()
|
|
{
|
|
_database.Find(PublicIp).Returns(Germany);
|
|
_factory.Open().Returns(_database);
|
|
|
|
CreateEnricher().Lookup(PublicIp.MapToIPv6()).ShouldBe(Germany);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("10.1.2.3")]
|
|
[InlineData("172.19.0.5")]
|
|
[InlineData("192.168.1.14")]
|
|
[InlineData("127.0.0.1")]
|
|
[InlineData("::1")]
|
|
[InlineData("fe80::1")]
|
|
public void Does_not_look_up_private_ips(string address)
|
|
{
|
|
_factory.Open().Returns(_database);
|
|
|
|
CreateEnricher().Lookup(IPAddress.Parse(address)).ShouldBeNull();
|
|
|
|
_database.DidNotReceiveWithAnyArgs().Find(default!);
|
|
}
|
|
|
|
[Fact]
|
|
public void Returns_nothing_without_database()
|
|
{
|
|
_factory.Open().Returns((IGeoIpDatabase?)null);
|
|
|
|
var enricher = CreateEnricher();
|
|
|
|
enricher.IsAvailable.ShouldBeFalse();
|
|
enricher.Lookup(PublicIp).ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Swallows_lookup_errors()
|
|
{
|
|
_database.Find(PublicIp).Throws(new InvalidOperationException("corrupt"));
|
|
_factory.Open().Returns(_database);
|
|
|
|
CreateEnricher().Lookup(PublicIp).ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Reload_switches_to_new_database_and_raises_updated()
|
|
{
|
|
_factory.Open().Returns((IGeoIpDatabase?)null, _database);
|
|
_database.Find(PublicIp).Returns(Germany);
|
|
var enricher = CreateEnricher();
|
|
var raised = 0;
|
|
enricher.Updated += (_, _) => raised++;
|
|
|
|
enricher.Reload();
|
|
|
|
raised.ShouldBe(1);
|
|
enricher.IsAvailable.ShouldBeTrue();
|
|
enricher.Lookup(PublicIp).ShouldBe(Germany);
|
|
}
|
|
|
|
[Fact]
|
|
public void Reload_keeps_current_database_when_files_are_missing()
|
|
{
|
|
_factory.Open().Returns(_database, (IGeoIpDatabase?)null);
|
|
_database.Find(PublicIp).Returns(Germany);
|
|
var enricher = CreateEnricher();
|
|
var raised = 0;
|
|
enricher.Updated += (_, _) => raised++;
|
|
|
|
enricher.Reload();
|
|
|
|
raised.ShouldBe(0);
|
|
enricher.Lookup(PublicIp).ShouldBe(Germany);
|
|
}
|
|
|
|
private DbIpEnricher CreateEnricher() => new(_factory, NullLogger<DbIpEnricher>.Instance);
|
|
}
|