Tests: tst_ruletextparser.h: Add "lineIp6Range" test

This commit is contained in:
Nodir Temirkhodjaev 2024-11-06 19:10:20 +05:00
parent 1646c99fe4
commit f75ece4e61
3 changed files with 48 additions and 12 deletions

View File

@ -187,3 +187,26 @@ TEST_F(RuleTextParserTest, lineSectionList)
checkStringList(rf.values, { "80" });
}
}
TEST_F(RuleTextParserTest, lineIp6Range)
{
RuleTextParser p("[2:]/3-[4:]/5:67");
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, { "[2:]/3-[4:]/5" });
}
// Check Port
{
const RuleFilter &rf = p.ruleFilters()[3];
ASSERT_EQ(rf.type, FORT_RULE_FILTER_TYPE_PORT);
checkStringList(rf.values, { "67" });
}
}

View File

@ -324,23 +324,16 @@ bool RuleTextParser::parseValue(bool expectValueEnd)
{
const QChar *value = parsedCharPtr();
const char *extraChars = expectValueEnd ? extraValueEndChars : extraValueChars;
for (;;) {
const char *extraChars = expectValueEnd ? extraValueEndChars : extraValueChars;
if (!parseChars(CharLetter | CharValue, extraChars))
return false;
if (expectValueEnd) {
if (m_charType != CharValueEnd) {
setError(ErrorUnexpectedEndOfValue, tr("Unexpected end of value"));
if (!checkValueEnd(expectValueEnd)) {
if (hasError())
return false;
}
advanceCharPtr();
expectValueEnd = false;
extraChars = extraValueChars;
} else {
break;
}
}
@ -352,6 +345,25 @@ bool RuleTextParser::parseValue(bool expectValueEnd)
return true;
}
bool RuleTextParser::checkValueEnd(bool &expectValueEnd)
{
const bool isValueBegin = (m_charType == CharValueBegin);
if (isValueBegin || expectValueEnd) {
if (isValueBegin ? expectValueEnd : (m_charType != CharValueEnd)) {
setError(ErrorUnexpectedEndOfValue, tr("Unexpected end of value"));
return false;
}
advanceCharPtr();
expectValueEnd = isValueBegin;
return true;
}
return false;
}
bool RuleTextParser::checkAddFilter()
{
if (!m_ruleFilter.hasValues())

View File

@ -78,7 +78,7 @@ public:
ErrorCode errorCode() const { return m_errorCode; }
const QString &errorMessage() const { return m_errorMessage; }
bool hasError() const { return !errorMessage().isEmpty(); }
bool hasError() const { return errorCode() != ErrorNone; }
const QVector<RuleFilter> &ruleFilters() const { return m_ruleFilters; }
@ -107,6 +107,7 @@ private:
void parseBracketValues();
bool parseBracketValue(RuleCharTypes expectedSeparator);
bool parseValue(bool expectValueEnd);
bool checkValueEnd(bool &expectValueEnd);
bool checkAddFilter();