using OpenWaf.Domain.Security; namespace OpenWaf.Tests.Unit; /// /// The rulings of a config transfer, away from an edge (OPEN-61). /// /// The endpoint decides what to write; these functions decide whether a row is /// worth writing at all, and every one of them is a ruling that could be /// silently inverted in the handler — an expired row imported as live, a version /// accepted that this build cannot read, a section size that turns one request /// into a data load. Checked here, where they are arithmetic. /// public sealed class ConfigTransferTests { private static readonly DateTimeOffset Now = new(2026, 10, 5, 12, 0, 0, TimeSpan.Zero); [Fact] public void The_version_this_build_writes_is_the_only_one_it_reads() { Assert.Null(ConfigTransfer.ExplainVersion(ConfigTransfer.CurrentVersion)); var problem = ConfigTransfer.ExplainVersion(ConfigTransfer.CurrentVersion + 1)!; Assert.Contains((ConfigTransfer.CurrentVersion + 1).ToString(), problem); Assert.Contains(ConfigTransfer.CurrentVersion.ToString(), problem); // A document that never named a version arrives as 0, and 0 is not a // version this build can honour. Assert.NotNull(ConfigTransfer.ExplainVersion(0)); } [Fact] public void A_section_over_the_cap_is_refused_naming_the_cap() { Assert.Null(ConfigTransfer.ExplainSectionSize("blocks", ConfigTransfer.MaxItemsPerSection)); var problem = ConfigTransfer.ExplainSectionSize("blocks", ConfigTransfer.MaxItemsPerSection + 1)!; Assert.Contains("blocks", problem); Assert.Contains(ConfigTransfer.MaxItemsPerSection.ToString(), problem); } [Fact] public void A_row_without_an_expiry_never_expires_and_a_past_instant_always_has() { Assert.False(ConfigTransfer.IsExpired(null, Now)); Assert.False(ConfigTransfer.IsExpired(Now.AddSeconds(1), Now)); Assert.True(ConfigTransfer.IsExpired(Now.AddSeconds(-1), Now)); // The instant itself is already past: "expires at 12:00" is not enforced // at 12:00, which is the same rule the engine's IsLive applies. Assert.True(ConfigTransfer.IsExpired(Now, Now)); } [Fact] public void A_block_is_checked_by_the_same_guards_the_write_endpoint_uses() { Assert.Null(ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.9", "incident", null, null))); Assert.Null(ConfigTransfer.ExplainBlock(new ExportedBlock( "2001:db8::1", null, "https://example.org/blocked", Now.AddDays(1)))); Assert.Contains("ip is required", ConfigTransfer.ExplainBlock(new ExportedBlock(" ", null, null, null))!); // Not "203.0.113": IPAddress.TryParse reads the abbreviated form as // 203.0.113.0, so that string is an address as far as this guard goes. Assert.Contains("not an IP address", ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.999", null, null, null))!); Assert.Contains("reason must be at most", ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.9", new string('x', EntryLimits.NoteMaxLength + 1), null, null))!); Assert.Contains("redirectTo must be an absolute http(s) URL", ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.9", null, "javascript:alert(1)", null))!); } [Fact] public void An_allow_row_takes_an_address_or_a_cidr_and_nothing_else() { Assert.Null(ConfigTransfer.ExplainAllow(new ExportedAllow("198.51.100.0/24", "office", null))); Assert.Null(ConfigTransfer.ExplainAllow(new ExportedAllow("198.51.100.7", null, null))); Assert.Contains("address or CIDR", ConfigTransfer.ExplainAllow(new ExportedAllow("office.example", null, null))!); Assert.Contains("address or CIDR", ConfigTransfer.ExplainAllow(new ExportedAllow("", null, null))!); Assert.Contains("note must be at most", ConfigTransfer.ExplainAllow(new ExportedAllow("198.51.100.7", new string('x', EntryLimits.NoteMaxLength + 1), null))!); } [Fact] public void A_rule_pattern_that_can_never_match_is_refused_before_it_is_stored() { Assert.Null(ConfigTransfer.ExplainRule(new ExportedRule("/wp-admin*", "block", "cms", null))); // Absent is observe, exactly as POST /admin-api/rules reads a missing action. Assert.Null(ConfigTransfer.ExplainRule(new ExportedRule("/anything", null, null, null))); Assert.Contains("action must be observe|block", ConfigTransfer.ExplainRule(new ExportedRule("/anything", "deny", null, null))!); Assert.NotNull(ConfigTransfer.ExplainRule(new ExportedRule("wp-admin*", "block", null, null))); Assert.NotNull(ConfigTransfer.ExplainRule(new ExportedRule("", "block", null, null))); } [Fact] public void A_country_code_is_the_two_letter_code_the_engine_matches() { Assert.Null(ConfigTransfer.ExplainCountry(new ExportedCountry("de", false))); Assert.Equal("DE", ConfigTransfer.CountryCode(" de ")); Assert.Null(ConfigTransfer.CountryCode("12")); Assert.Null(ConfigTransfer.CountryCode("DEU")); Assert.Null(ConfigTransfer.CountryCode(null)); Assert.Contains("two-letter", ConfigTransfer.ExplainCountry(new ExportedCountry("12", false))!); } [Fact] public void The_action_travels_in_the_spelling_the_table_stores() { Assert.Equal("observe", ConfigTransfer.RuleAction(null)); Assert.Equal("observe", ConfigTransfer.RuleAction(" ")); Assert.Equal("block", ConfigTransfer.RuleAction(" BLOCK ")); Assert.Null(ConfigTransfer.RuleAction("deny")); } }