Mark small countries on the map, fit latest catches table, show missing host

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Opus 5.5 committed 2026-11-15 12:44:23 +01:00
1 parent 6e2ff3f7ee
commit 31cef44bf3
15 files changed
+475 -27

No files matched your search

@@ -0,0 +1,50 @@
using System.Net;
using Flypaper.Core;
using Flypaper.Core.Abstractions;
using Flypaper.Core.Models;
using Flypaper.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
namespace Flypaper.Tests.Http;
public sealed class TargetNormalizationTests
{
[Theory]
[InlineData("_", "GET /boaform/admin/formLogin")]
[InlineData("", "GET /boaform/admin/formLogin")]
[InlineData("example.com", "GET example.com/boaform/admin/formLogin")]
[InlineData("203.0.113.7:8080", "GET 203.0.113.7:8080/boaform/admin/formLogin")]
public async Task Stores_missing_or_placeholder_host_as_empty(string host, string expectedTarget)
{
var sink = Substitute.For<ITrapEventSink>();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { ["Flypaper:MaxConnections"] = "0" })
.Build();
var services = new ServiceCollection()
.AddLogging()
.AddSingleton(sink)
.AddFlypaperCore(configuration)
.AddFlypaperHttp(configuration)
.BuildServiceProvider();
var app = new ApplicationBuilder(services);
app.UseFlypaper();
app.Run(_ => Task.CompletedTask);
var context = new DefaultHttpContext { RequestServices = services };
context.Connection.RemoteIpAddress = IPAddress.Parse("203.0.113.9");
context.Connection.LocalPort = 8080;
context.Request.Method = "GET";
context.Request.Host = new HostString(host);
context.Request.Path = "/boaform/admin/formLogin";
await app.Build()(context);
sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Target == expectedTarget));
}
}
@@ -25,6 +25,24 @@ public sealed class MigrationTests : IDisposable
(await db.Settings.Select(s => s.Key).ToListAsync()).ShouldBe(["Flypaper:MaxConnections"]);
}
[Fact]
public async Task Rewrites_placeholder_host_of_existing_trap_events()
{
await using var db = new FlypaperDbContext(new DbContextOptionsBuilder<FlypaperDbContext>().UseSqlite($"Data Source={_dbPath}").Options);
await db.GetService<IMigrator>().MigrateAsync("RemoveDripIntervalSetting");
await db.Database.ExecuteSqlRawAsync(
"""
INSERT INTO "TrapEvents" ("SessionId", "Protocol", "RemoteIp", "Strategy", "StartedAt", "EndedAt", "DurationMs", "BytesSent", "Outcome", "Target")
VALUES ('A', 'Http', '203.0.113.9', 'html-drip', '2026-11-14 10:00:00', '2026-11-14 10:00:05', 5000, 50, 'ClientAborted', 'GET _/boaform/admin/formLogin'),
('B', 'Http', '203.0.113.9', 'html-drip', '2026-11-14 10:00:00', '2026-11-14 10:00:05', 5000, 50, 'ClientAborted', 'GET example.com/.env')
""");
await db.Database.MigrateAsync();
(await db.TrapEvents.OrderBy(e => e.SessionId).Select(e => e.Target).ToListAsync())
.ShouldBe(["GET /boaform/admin/formLogin", "GET example.com/.env"]);
}
public void Dispose()
{
SqliteConnection.ClearAllPools();
@@ -0,0 +1,17 @@
using Flypaper.Web.Components.Shared;
using Shouldly;
namespace Flypaper.Tests.Web;
public sealed class FormattingTests
{
[Theory]
[InlineData("GET /boaform/admin/formLogin", "GET (ohne Host)/boaform/admin/formLogin")]
[InlineData("GET _/boaform/admin/formLogin", "GET (ohne Host)/boaform/admin/formLogin")]
[InlineData("GET _", "GET (ohne Host)")]
[InlineData("GET example.com/.env", "GET example.com/.env")]
[InlineData("GET _example.com/.env", "GET _example.com/.env")]
[InlineData(null, null)]
public void Shows_missing_host_explicitly(string? target, string? expected) =>
Formatting.Target(target).ShouldBe(expected);
}
@@ -0,0 +1,52 @@
using Flypaper.Web.Components.Pages.Dashboard;
using Shouldly;
namespace Flypaper.Tests.Web;
public sealed class WorldMapDataTests
{
private static readonly string[] Iso3166Alpha2 =
"""
AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ
CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR
GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO
JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR
MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO
RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV
TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW
""".Split((char[])[' ', '\n', '\r'], StringSplitOptions.RemoveEmptyEntries);
private readonly WorldMapData _map = WorldMapData.Instance;
[Fact]
public void Lists_all_249_iso_codes() => Iso3166Alpha2.Distinct().Count().ShouldBe(249);
[Fact]
public void Every_iso_country_has_a_polygon_or_a_marker()
{
var drawn = _map.Countries.Select(c => c.Code).OfType<string>().Concat(_map.Markers.Select(m => m.Code)).ToHashSet();
Iso3166Alpha2.Where(code => !drawn.Contains(code)).ShouldBeEmpty();
}
[Theory]
[InlineData("HK")]
[InlineData("SG")]
[InlineData("MO")]
[InlineData("BH")]
[InlineData("MT")]
public void Small_bot_heavy_countries_have_markers(string code) =>
_map.Markers.ShouldContain(m => m.Code == code);
[Fact]
public void No_country_has_both_a_polygon_and_a_marker()
{
var polygons = _map.Countries.Select(c => c.Code).OfType<string>().ToHashSet();
_map.Markers.Where(m => polygons.Contains(m.Code)).ShouldBeEmpty();
}
[Fact]
public void Markers_lie_inside_the_map() =>
_map.Markers.ShouldAllBe(m => m.X >= 0 && m.X <= _map.Width && m.Y >= 0 && m.Y <= _map.Height);
}