using Flypaper.Core.Abstractions; using Flypaper.Core.Runtime; using Flypaper.Storage.Queries; using Flypaper.Tests.Fakes; using Flypaper.Web.Mcp; using ModelContextProtocol; using NSubstitute; using Shouldly; namespace Flypaper.Tests.Mcp; public sealed class FlypaperToolsTests : IDisposable { private readonly McpTestData _data = new(); private readonly FlypaperTools _tools; private readonly ActiveTrapRegistry _active = new(new FixedTimeProvider(McpTestData.Now)); private readonly IIpEnricher _enricher = Substitute.For(); public FlypaperToolsTests() { var governor = Substitute.For(); governor.ActiveConnections.Returns(2); _tools = new FlypaperTools(new CatchQueries(_data.Factory), new CommandQueries(_data.Factory), governor, _active, _enricher, new FixedTimeProvider(McpTestData.Now)); } [Fact] public void List_active_shows_running_traps_with_origin_and_bytes() { var address = System.Net.IPAddress.Parse("203.0.113.7"); _enricher.Lookup(address).Returns(new Flypaper.Core.Models.IpOrigin("DE", "Germany", "Berlin", 3320, "Telekom")); var context = new Flypaper.Core.Models.TrapContext( new Flypaper.Core.Models.TrapSession(Guid.NewGuid(), Flypaper.Core.Models.TrapProtocol.Http, address, McpTestData.Now.AddMinutes(-5)), "GET /.env", "curl/8.7.1"); _active.Begin(context, "env-drip").Attach(() => 2048); var result = _tools.ListActive(); var trap = result.Traps.ShouldHaveSingleItem(); result.Count.ShouldBe(1); trap.Ip.ShouldBe("203.0.113.7"); trap.CountryCode.ShouldBe("DE"); trap.Asn.ShouldBe("AS3320"); trap.Target.ShouldBe("GET /.env"); trap.BytesSent.ShouldBe(2048); trap.DurationSeconds.ShouldBe(0); } [Fact] public void List_active_is_empty_without_traps() => _tools.ListActive().Count.ShouldBe(0); [Fact] public async Task Get_stats_summarizes_the_period() { var stats = await _tools.GetStats("24h"); stats.Period.ShouldBe("24h"); stats.Since.ShouldBe(McpTestData.Now.UtcDateTime.AddHours(-24)); stats.Catches.ShouldBe(3); stats.Rejected.ShouldBe(1); stats.UniqueIps.ShouldBe(2); stats.TotalHoldSeconds.ShouldBe(3750); stats.AverageHoldSeconds.ShouldBe(1250); stats.ActiveTraps.ShouldBe(2); stats.LongestCatch.ShouldNotBeNull().DurationSeconds.ShouldBe(3600); stats.LongestCatch!.Country.ShouldBe("China"); stats.LongestCatch.Asn.ShouldBe("AS4134"); stats.LongestCatch.StartedAt.Kind.ShouldBe(DateTimeKind.Utc); } [Fact] public async Task Get_stats_for_all_time() { var stats = await _tools.GetStats("all"); stats.Since.ShouldBeNull(); stats.Catches.ShouldBe(5); stats.UniqueIps.ShouldBe(4); } [Theory] [InlineData("yesterday")] [InlineData("0h")] [InlineData("-1d")] public async Task Rejects_invalid_periods(string period) => await Should.ThrowAsync(() => _tools.GetStats(period)); [Fact] public async Task List_catches_returns_newest_first_with_rejected_ones() { var result = await _tools.ListCatches(); result.Count.ShouldBe(6); result.Catches.Select(c => c.StartedAt).ShouldBeInOrder(SortDirection.Descending); result.Catches.ShouldContain(c => c.Outcome == "Rejected"); } [Theory] [InlineData("cn", null, null, null, 2)] [InlineData(null, "AS3462", null, null, 2)] [InlineData(null, "google", null, null, 1)] [InlineData(null, null, "198.51.100.7", null, 2)] [InlineData(null, null, null, ".ENV", 2)] [InlineData("CN", null, null, "wp-login", 1)] public async Task List_catches_filters(string? country, string? provider, string? ip, string? target, int expected) { var result = await _tools.ListCatches(country: country, provider: provider, ip: ip, target_contains: target); result.Count.ShouldBe(expected); } [Fact] public async Task List_catches_filters_by_time_and_limit() { var since = McpTestData.Now.AddHours(-3.5).ToString("O"); var until = McpTestData.Now.AddHours(-1.5).ToString("O"); (await _tools.ListCatches(since: since, until: until)).Count.ShouldBe(2); (await _tools.ListCatches(limit: 1)).Count.ShouldBe(1); (await _tools.ListCatches(limit: 100_000)).Count.ShouldBe(6); await Should.ThrowAsync(() => _tools.ListCatches(since: "last tuesday")); await Should.ThrowAsync(() => _tools.ListCatches(ip: "not-an-ip")); } [Theory] [InlineData("ips", "203.0.113.9", 2)] [InlineData("countries", "CN", 2)] [InlineData("providers", "AS4134", 2)] [InlineData("targets", "GET /.env", 1)] [InlineData("strategies", "html-drip", 4)] [InlineData("user_agents", "zgrab/0.x", 2)] public async Task Top_ranks_each_dimension(string dimension, string firstKey, int firstCount) { var result = await _tools.Top(dimension, "all"); result.Dimension.ShouldBe(dimension); result.Items[0].Key.ShouldBe(firstKey); result.Items[0].Catches.ShouldBe(firstCount); } [Fact] public async Task Top_uses_common_country_names_and_rejects_unknown_dimensions() { var countries = await _tools.Top("countries", "24h"); countries.Items.Select(i => i.Label).ShouldBe(["China", "Taiwan"]); await Should.ThrowAsync(() => _tools.Top("cities")); } [Fact] public async Task Longest_catches_are_ordered_by_duration() { var result = await _tools.LongestCatches("all", 3); result.Catches.Select(c => c.DurationSeconds).ShouldBe([3600, 900, 120]); } [Fact] public async Task Lookup_ip_returns_everything_known() { var result = await _tools.LookupIp("198.51.100.7"); result.Known.ShouldBeTrue(); result.Country.ShouldBe("Taiwan"); result.Asn.ShouldBe("AS3462"); result.Catches.ShouldBe(1); result.Rejected.ShouldBe(1); result.TotalHoldSeconds.ShouldBe(30); result.FirstSeen.ShouldBe(McpTestData.Now.UtcDateTime.AddHours(-4)); result.LastSeen.ShouldBe(McpTestData.Now.UtcDateTime.AddHours(-3).AddSeconds(30)); result.Events.Count.ShouldBe(2); } [Fact] public async Task Lookup_ip_handles_unknown_and_invalid_addresses() { (await _tools.LookupIp("192.0.2.99")).Known.ShouldBeFalse(); (await _tools.LookupIp("2001:0db8::0001")).Known.ShouldBeTrue(); await Should.ThrowAsync(() => _tools.LookupIp("999.1.1.1")); } public void Dispose() => _data.Dispose(); }