using System.Text; using System.Text.Json; using Flypaper.Core.Strategies; using Flypaper.Tests.Fakes; using Shouldly; namespace Flypaper.Tests.Core; public sealed class SignpostTests { [Theory] [InlineData("env-drip", "BACKUP_URL=http://trap.example/backup/db-{m}.sql")] [InlineData("env-drip", "ADMIN_URL=http://trap.example/admin-{m}/")] [InlineData("env-drip", "CONFIG_URL=http://trap.example/storage/settings-{m}.json")] [InlineData("json-drip", "\"backup_url\": \"http://trap.example/backup/db-{m}.sql\"")] [InlineData("yaml-drip", "backup_url: http://trap.example/backup/db-{m}.sql")] [InlineData("sql-drip", "-- Previous full dump: http://trap.example/backup/db-{m}.sql")] [InlineData("git-drip", "url = http://trap.example/git-{m}/portal.git")] public async Task Signposts_arrive_with_the_undelayed_head_and_carry_the_session_marker(string strategy, string expected) { var (head, channel) = await HeadAsync(strategy, "trap.example"); head.ShouldContain(expected.Replace("{m}", SignpostLinks.MarkerOf(channel.Session.Id))); } [Theory] [InlineData(null)] [InlineData("[::1]")] [InlineData("_")] [InlineData("bad host\n")] public async Task Unusable_hosts_give_relative_signposts(string? host) { var (head, channel) = await HeadAsync("env-drip", host); head.ShouldContain($"BACKUP_URL=/backup/db-{SignpostLinks.MarkerOf(channel.Session.Id)}.sql"); head.ShouldContain("APP_URL=https://example.com"); } [Theory] [InlineData("env-drip")] [InlineData("json-drip")] [InlineData("yaml-drip")] [InlineData("sql-drip")] [InlineData("git-drip")] public async Task Switch_removes_the_signposts(string strategy) { var (head, channel) = await HeadAsync(strategy, "trap.example", signposts: false); head.ShouldNotContain(SignpostLinks.MarkerOf(channel.Session.Id)); head.ShouldNotContain("trap.example"); } [Fact] public void Json_with_signposts_stays_well_formed() { var profile = FileContent.Profiles.First(p => p.Name == "json-drip"); var links = new SignpostLinks("http://trap.example", "7f3a9c21b4e8"); var text = string.Concat(profile.Generate(new SeededRandomSource(5), links).Take(5)).TrimEnd().TrimEnd(',') + "]}"; using var document = JsonDocument.Parse(text); document.RootElement.GetProperty("api_base").GetString().ShouldBe("http://trap.example/storage/settings-7f3a9c21b4e8.json"); } [Theory] [InlineData("/backup/db-7f3a9c21b4e8.sql", "sql-drip")] [InlineData("/storage/settings-7f3a9c21b4e8.json", "json-drip")] public void Followed_signposts_lead_to_the_next_fake_file(string path, string strategy) => FileContent.Match(path).ShouldNotBeNull().Name.ShouldBe(strategy); private static async Task<(string Head, FakeTrapChannel Channel)> HeadAsync(string strategy, string? host, bool signposts = true) { var options = TestOptions.Monitor(new DripOptions { Signposts = signposts, MinDelay = TimeSpan.FromSeconds(30), MaxDelay = TimeSpan.FromSeconds(30) }); var drip = new FileDripStrategy(FileContent.Profiles.First(p => p.Name == strategy), options, new SeededRandomSource(3), TimeProvider.System); var channel = new FakeTrapChannel { Host = host }; using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); await Should.ThrowAsync(() => drip.RunAsync(channel, cts.Token)); return (Encoding.UTF8.GetString(channel.Writes.First()), channel); } }