From f75ece4e61c0819d77d9a284c0cbf81a34d6bce3 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 6 Nov 2024 19:10:20 +0500 Subject: [PATCH] Tests: tst_ruletextparser.h: Add "lineIp6Range" test --- src/tests/UtilTest/tst_ruletextparser.h | 23 +++++++++++++++++ src/ui/util/conf/ruletextparser.cpp | 34 +++++++++++++++++-------- src/ui/util/conf/ruletextparser.h | 3 ++- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/tests/UtilTest/tst_ruletextparser.h b/src/tests/UtilTest/tst_ruletextparser.h index 70e5a984..d1f905fd 100644 --- a/src/tests/UtilTest/tst_ruletextparser.h +++ b/src/tests/UtilTest/tst_ruletextparser.h @@ -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" }); + } +} diff --git a/src/ui/util/conf/ruletextparser.cpp b/src/ui/util/conf/ruletextparser.cpp index 24f66afa..343c65bf 100644 --- a/src/ui/util/conf/ruletextparser.cpp +++ b/src/ui/util/conf/ruletextparser.cpp @@ -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()) diff --git a/src/ui/util/conf/ruletextparser.h b/src/ui/util/conf/ruletextparser.h index b91568f9..c5aabb94 100644 --- a/src/ui/util/conf/ruletextparser.h +++ b/src/ui/util/conf/ruletextparser.h @@ -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 &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();