Tests: tst_ruletextparser: Add tests

This commit is contained in:
Nodir Temirkhodjaev 2024-11-10 10:17:20 +05:00
parent 6cd542b21f
commit f999630485
2 changed files with 49 additions and 8 deletions

View File

@ -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" });
}
}

View File

@ -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;
}