134 lines
4.9 KiB
C#
134 lines
4.9 KiB
C#
using System.Net;
|
|
using Flypaper.Core;
|
|
using Flypaper.Core.Abstractions;
|
|
using Flypaper.Core.Models;
|
|
using Flypaper.Core.Runtime;
|
|
using Flypaper.Core.Strategies;
|
|
using Flypaper.Tests.Fakes;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Core;
|
|
|
|
public sealed class TrapRunnerTests
|
|
{
|
|
private readonly IConnectionGovernor _governor = Substitute.For<IConnectionGovernor>();
|
|
private readonly ITrapEventSink _sink = Substitute.For<ITrapEventSink>();
|
|
private readonly FakeTrapChannel _channel = new();
|
|
|
|
[Fact]
|
|
public async Task Rejects_without_opening_channel_when_governor_is_full()
|
|
{
|
|
_governor.TryAcquire(Arg.Any<TrapSession>(), out Arg.Any<IDisposable?>()).Returns(false);
|
|
var opened = false;
|
|
|
|
var outcome = await CreateRunner(TimeSpan.FromSeconds(1)).RunAsync(
|
|
Context(),
|
|
_ =>
|
|
{
|
|
opened = true;
|
|
return ValueTask.FromResult<ITrapChannel>(_channel);
|
|
},
|
|
CancellationToken.None);
|
|
|
|
outcome.ShouldBe(TrapOutcome.Rejected);
|
|
opened.ShouldBeFalse();
|
|
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.Rejected && e.BytesSent == 0));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Times_out_after_max_hold_duration_and_releases_lease()
|
|
{
|
|
var lease = AllowAcquire();
|
|
|
|
var outcome = await CreateRunner(TimeSpan.FromMilliseconds(200)).RunAsync(Context(), Open, CancellationToken.None);
|
|
|
|
outcome.ShouldBe(TrapOutcome.Timeout);
|
|
lease.Received(1).Dispose();
|
|
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e =>
|
|
e.Outcome == TrapOutcome.Timeout && e.BytesSent == _channel.Writes.Sum(w => w.Length) && e.BytesSent > 0));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Reports_client_abort_when_caller_cancels()
|
|
{
|
|
AllowAcquire();
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
|
|
|
|
var outcome = await CreateRunner(TimeSpan.FromMinutes(1)).RunAsync(Context(), Open, cts.Token);
|
|
|
|
outcome.ShouldBe(TrapOutcome.ClientAborted);
|
|
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.ClientAborted && e.Strategy == "html-drip"));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("192.168.1.20")]
|
|
[InlineData("172.19.0.1")]
|
|
[InlineData("127.0.0.1")]
|
|
public async Task Traps_but_does_not_store_private_ips_by_default(string ip)
|
|
{
|
|
AllowAcquire();
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
|
|
|
|
var outcome = await CreateRunner(TimeSpan.FromMinutes(1)).RunAsync(Context(ip), Open, cts.Token);
|
|
|
|
outcome.ShouldBe(TrapOutcome.ClientAborted);
|
|
_channel.Writes.ShouldNotBeEmpty();
|
|
_sink.DidNotReceiveWithAnyArgs().Publish(default!);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Does_not_store_rejected_private_ips_by_default()
|
|
{
|
|
_governor.TryAcquire(Arg.Any<TrapSession>(), out Arg.Any<IDisposable?>()).Returns(false);
|
|
|
|
await CreateRunner(TimeSpan.FromSeconds(1)).RunAsync(Context("10.0.0.5"), Open, CancellationToken.None);
|
|
|
|
_sink.DidNotReceiveWithAnyArgs().Publish(default!);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Stores_private_ips_when_option_is_off()
|
|
{
|
|
AllowAcquire();
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
|
|
|
|
await CreateRunner(TimeSpan.FromMinutes(1), ignorePrivateNetworks: false).RunAsync(Context("192.168.1.20"), Open, cts.Token);
|
|
|
|
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.RemoteIp.Equals(IPAddress.Parse("192.168.1.20"))));
|
|
}
|
|
|
|
private IDisposable AllowAcquire()
|
|
{
|
|
var lease = Substitute.For<IDisposable>();
|
|
_governor.TryAcquire(Arg.Any<TrapSession>(), out Arg.Any<IDisposable?>())
|
|
.Returns(call =>
|
|
{
|
|
call[1] = lease;
|
|
return true;
|
|
});
|
|
return lease;
|
|
}
|
|
|
|
private ValueTask<ITrapChannel> Open(CancellationToken ct) => ValueTask.FromResult<ITrapChannel>(_channel);
|
|
|
|
private TrapRunner CreateRunner(TimeSpan maxHoldDuration, bool ignorePrivateNetworks = true)
|
|
{
|
|
var drip = new HtmlDripStrategy(
|
|
TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(5), MaxDelay = TimeSpan.FromMilliseconds(20) }),
|
|
new SeededRandomSource(7),
|
|
TimeProvider.System);
|
|
return new TrapRunner(
|
|
_governor,
|
|
new ProtocolStrategySelector([drip]),
|
|
_sink,
|
|
TestOptions.Monitor(new FlypaperOptions { MaxHoldDuration = maxHoldDuration, IgnorePrivateNetworks = ignorePrivateNetworks }),
|
|
TimeProvider.System,
|
|
NullLogger<TrapRunner>.Instance);
|
|
}
|
|
|
|
private static TrapContext Context(string ip = "203.0.113.9") =>
|
|
new(new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse(ip), DateTimeOffset.UtcNow), "GET /.env");
|
|
}
|