using OpenWaf.Domain.Security; namespace OpenWaf.Tests.Unit; /// /// The vocabulary and the bounds of a change record (OPEN-64). /// public sealed class ConfigChangeLogTests { [Fact] public void Every_vocabulary_word_fits_the_column_that_stores_it() { // A section or action longer than its column would be clipped by the // database rather than by this code, and a filter that asks for the // unclipped word would then answer "no such change". foreach (var word in ConfigChangeLog.Sections.Concat(ConfigChangeLog.Actions) .Append(ConfigChangeLog.ActorOperator).Append(ConfigChangeLog.ActorImport)) Assert.True(word.Length <= 16, $"\"{word}\" is {word.Length} characters"); } [Fact] public void The_vocabulary_has_no_duplicates() { Assert.Equal(ConfigChangeLog.Sections.Length, ConfigChangeLog.Sections.Distinct().Count()); Assert.Equal(ConfigChangeLog.Actions.Length, ConfigChangeLog.Actions.Distinct().Count()); } [Fact] public void Traffic_is_not_a_section_and_says_so() { // The log is about decisions, never about visitors: the vocabulary is // where that ruling is enforced, and a filter for a traffic section is // answered rather than quietly emptied. Assert.False(ConfigChangeLog.IsSection("traffic")); Assert.False(ConfigChangeLog.IsSection("observation")); Assert.False(ConfigChangeLog.IsSection("BLOCK")); Assert.False(ConfigChangeLog.IsSection(null)); foreach (var section in ConfigChangeLog.Sections) Assert.True(ConfigChangeLog.IsSection(section)); } [Fact] public void Every_action_is_known_and_nothing_else_is() { foreach (var action in ConfigChangeLog.Actions) Assert.True(ConfigChangeLog.IsAction(action)); Assert.False(ConfigChangeLog.IsAction("deleted")); Assert.False(ConfigChangeLog.IsAction("")); } [Fact] public void A_detail_over_the_cap_is_clipped_not_refused() { // The change has already been applied when the record is written. A // record that dropped the change because the operator's sentence was // long would be the worse failure, so the sentence gives way. var longNote = new string('x', ConfigChangeLog.MaxDetailLength + 200); var clipped = ConfigChangeLog.Clip(longNote, ConfigChangeLog.MaxDetailLength); Assert.Equal(ConfigChangeLog.MaxDetailLength, clipped.Length); } [Fact] public void A_detail_is_trimmed_and_blank_becomes_nothing() { Assert.Equal("confirmed scraper", ConfigChangeLog.Clip(" confirmed scraper ", 64)); Assert.Equal("", ConfigChangeLog.Clip(null, 64)); Assert.Equal("", ConfigChangeLog.Clip(" ", 64)); } }