From f999630485d14c5dbf141818e365f7e267c4e74a Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sun, 10 Nov 2024 10:17:20 +0500 Subject: [PATCH] Tests: tst_ruletextparser: Add tests --- src/tests/UtilTest/tst_ruletextparser.h | 41 +++++++++++++++++++++++++ src/ui/util/conf/ruletextparser.cpp | 16 +++++----- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/tests/UtilTest/tst_ruletextparser.h b/src/tests/UtilTest/tst_ruletextparser.h index 5c29532d..7205b2a3 100644 --- a/src/tests/UtilTest/tst_ruletextparser.h +++ b/src/tests/UtilTest/tst_ruletextparser.h @@ -318,3 +318,44 @@ TEST_F(RuleTextParserTest, badBadSymbol) ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorBadSymbol); } + +TEST_F(RuleTextParserTest, filterNot) +{ + RuleTextParser p("!!!1"); + + ASSERT_TRUE(p.parse()); + + ASSERT_EQ(p.ruleFilters().size(), 3); + + // Check IP + { + const RuleFilter &rf = p.ruleFilters()[2]; + ASSERT_TRUE(rf.isNot); + ASSERT_EQ(rf.type, FORT_RULE_FILTER_TYPE_ADDRESS); + checkStringList(rf.values, { "1" }); + } +} + +TEST_F(RuleTextParserTest, lineEndNot) +{ + RuleTextParser p("1!2"); + + ASSERT_TRUE(p.parse()); + + ASSERT_EQ(p.ruleFilters().size(), 4); + + // Check IP + { + const RuleFilter &rf = p.ruleFilters()[2]; + ASSERT_EQ(rf.type, FORT_RULE_FILTER_TYPE_ADDRESS); + checkStringList(rf.values, { "1" }); + } + + // Check Port + { + const RuleFilter &rf = p.ruleFilters()[3]; + ASSERT_TRUE(rf.isNot); + ASSERT_EQ(rf.type, FORT_RULE_FILTER_TYPE_PORT); + checkStringList(rf.values, { "2" }); + } +} diff --git a/src/ui/util/conf/ruletextparser.cpp b/src/ui/util/conf/ruletextparser.cpp index 86408510..51a4eafd 100644 --- a/src/ui/util/conf/ruletextparser.cpp +++ b/src/ui/util/conf/ruletextparser.cpp @@ -134,9 +134,17 @@ bool RuleTextParser::parseLine() if (!parseLineSection(expectedSeparator)) break; + const bool isSectionEnd = m_ruleFilter.isSectionEnd; + if (!processSectionFilter()) break; + // Next default type, if applicable + if (!isSectionEnd && !m_ruleFilter.hasFilterName) { + m_ruleFilter.type = m_ruleFilter.isTypeAddress() ? FORT_RULE_FILTER_TYPE_PORT + : FORT_RULE_FILTER_TYPE_INVALID; + } + expectedSeparator = CharColon | CharNewLine; } @@ -153,16 +161,8 @@ bool RuleTextParser::processSectionFilter() if (!checkAddFilter()) return false; - const bool isSectionEnd = m_ruleFilter.isSectionEnd; - resetFilter(); - // Next default type, if applicable - if (!isSectionEnd && !m_ruleFilter.hasFilterName) { - m_ruleFilter.type = m_ruleFilter.isTypeAddress() ? FORT_RULE_FILTER_TYPE_PORT - : FORT_RULE_FILTER_TYPE_INVALID; - } - return true; }