134 lines
4.9 KiB
C#
134 lines
4.9 KiB
C#
using System.Net;
|
|
using Flypaper.Core.Models;
|
|
using Flypaper.Storage.Entities;
|
|
using Flypaper.Web.Components.Shared;
|
|
using Flypaper.Storage.Dashboard;
|
|
using Flypaper.Tests.Fakes;
|
|
using Flypaper.Tests.Mcp;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Storage;
|
|
|
|
public sealed class RecentCatchesTests : IDisposable
|
|
{
|
|
private readonly McpTestData _data = new();
|
|
private readonly DashboardQueries _queries;
|
|
|
|
public RecentCatchesTests() => _queries = new DashboardQueries(_data.Factory, new FixedTimeProvider(McpTestData.Now));
|
|
|
|
[Fact]
|
|
public async Task Without_filters_returns_all_events_newest_first()
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter());
|
|
|
|
recent.Total.ShouldBe(6);
|
|
recent.Items.Select(e => e.StartedAt).ShouldBeInOrder(SortDirection.Descending);
|
|
recent.Countries.Select(c => c.Code).ShouldBe(["CN", "TW", "US"], ignoreOrder: true);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Combines_filters()
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter(Window: TimeSpan.FromHours(24), CountryCode: "TW", Outcome: TrapOutcome.Rejected));
|
|
|
|
recent.Total.ShouldBe(1);
|
|
recent.Items.ShouldHaveSingleItem().RemoteIp.ShouldBe("198.51.100.7");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(4134L, null, null, 2)]
|
|
[InlineData(null, "203.0.113.9", null, 2)]
|
|
[InlineData(null, null, "GET /.env", 2)]
|
|
[InlineData(4134L, null, "GET /.env", 1)]
|
|
public async Task Matches_provider_ip_and_target_exactly(long? asn, string? ip, string? target, int expected)
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter(Asn: asn, Ip: ip, Target: target));
|
|
|
|
recent.Total.ShouldBe(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Matches_strategy_exactly()
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter(Strategy: StrategyNames.Group("env-drip")));
|
|
|
|
recent.Total.ShouldBe(2);
|
|
recent.Items.ShouldAllBe(e => e.Strategy == "env-drip");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Dashboard_lists_strategy_durations_among_catches()
|
|
{
|
|
var data = await _queries.GetAsync();
|
|
|
|
data.StrategyCatches.Count.ShouldBe(5);
|
|
data.StrategyCatches.Count(s => s.Strategy == "env-drip").ShouldBe(1);
|
|
data.StrategyCatches.Single(s => s.Strategy == "env-drip").DurationMs.ShouldBe(3_600_000);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Strategy_group_matches_all_variants_of_the_base_type_and_its_old_name()
|
|
{
|
|
await using (var db = _data.Factory.CreateDbContext())
|
|
{
|
|
foreach (var strategy in new[] { "drip", "html-drip 50-500ms 1-640B", "html-drip 5-20ms 1-64B head 1024B", "html-drip 50-500ms 1-1024B repeat", "html-drip 50-500ms 1-64B head 1024B repeat" })
|
|
{
|
|
db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("192.0.2.1"), strategy, McpTestData.Now, McpTestData.Now, 0, TrapOutcome.ClientAborted, null, null)));
|
|
}
|
|
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
async Task<IReadOnlyList<string>> Find(string group) =>
|
|
(await _queries.GetRecentAsync(new RecentFilter(Strategy: StrategyNames.Group(group)))).Items.Select(e => e.Strategy).OrderBy(s => s).ToList();
|
|
|
|
(await Find("html-drip")).Where(s => s != "html-drip").ShouldBe(["drip", "html-drip 50-500ms 1-640B"]);
|
|
(await Find("html-drip head")).ShouldBe(["html-drip 5-20ms 1-64B head 1024B"]);
|
|
(await Find("html-drip repeat")).ShouldBe(["html-drip 50-500ms 1-1024B repeat"]);
|
|
(await Find("html-drip head repeat")).ShouldBe(["html-drip 50-500ms 1-64B head 1024B repeat"]);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("chinanet", 2)]
|
|
[InlineData("env-drip", 2)]
|
|
[InlineData("CURL", 2)]
|
|
[InlineData("198.51", 2)]
|
|
[InlineData("wp-login", 1)]
|
|
[InlineData("100%", 0)]
|
|
public async Task Search_is_case_insensitive_over_ip_provider_target_strategy_and_user_agent(string search, int expected)
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter(Search: search));
|
|
|
|
recent.Total.ShouldBe(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Search_also_matches_strategy_base_names_from_display_names()
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter(Search: "Zugangsdaten", SearchStrategies: ["env-drip"]));
|
|
|
|
recent.Total.ShouldBe(2);
|
|
recent.Items.ShouldAllBe(e => e.Strategy == "env-drip");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Returns_an_empty_result_when_nothing_matches()
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter(CountryCode: "US", Outcome: TrapOutcome.Rejected));
|
|
|
|
recent.Total.ShouldBe(0);
|
|
recent.Items.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Limits_the_items_but_counts_all_matches()
|
|
{
|
|
var recent = await _queries.GetRecentAsync(new RecentFilter(Limit: 2));
|
|
|
|
recent.Items.Count.ShouldBe(2);
|
|
recent.Total.ShouldBe(6);
|
|
}
|
|
|
|
public void Dispose() => _data.Dispose();
|
|
}
|