From 8bb8ef1767ab2e74a92edbc78364a8dbeff65d4d Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 6 Nov 2024 16:35:53 +0500 Subject: [PATCH] Tests: tst_ruletextparser.h: Add "lineIpValues" test --- src/tests/UtilTest/tst_ruletextparser.h | 15 ++++++++++++ src/ui/util/conf/ruletextparser.cpp | 32 ++++++++++++++++--------- src/ui/util/conf/ruletextparser.h | 4 +++- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/tests/UtilTest/tst_ruletextparser.h b/src/tests/UtilTest/tst_ruletextparser.h index 89dccf57..47b60524 100644 --- a/src/tests/UtilTest/tst_ruletextparser.h +++ b/src/tests/UtilTest/tst_ruletextparser.h @@ -65,6 +65,21 @@ TEST_F(RuleTextParserTest, lineIpPort) } } +TEST_F(RuleTextParserTest, lineIpValues) +{ + RuleTextParser p("(1.1.1.1/8, [2::]/16)"); + + ASSERT_TRUE(p.parse()); + + ASSERT_EQ(p.ruleFilters().size(), 3); + + // Check IP Values + { + const RuleFilter &rf = p.ruleFilters()[2]; + ASSERT_TRUE(compareStringList(rf.values, { "1.1.1.1/8", "[2::]/16" })); + } +} + TEST_F(RuleTextParserTest, lineIpPortList) { RuleTextParser p("1.1.1.1:53\n" diff --git a/src/ui/util/conf/ruletextparser.cpp b/src/ui/util/conf/ruletextparser.cpp index 2e5879b9..22a28241 100644 --- a/src/ui/util/conf/ruletextparser.cpp +++ b/src/ui/util/conf/ruletextparser.cpp @@ -289,7 +289,7 @@ void RuleTextParser::parseBracketValues() if (!parseBracketValue(expectedSeparator)) break; - expectedSeparator = CharNewLine | CharValueSeparator; + expectedSeparator = CharValueSeparator; } } @@ -297,8 +297,8 @@ bool RuleTextParser::parseBracketValue(RuleCharTypes expectedSeparator) { resetParsedCharTypes(); - if (!parseChars(CharValueBegin | CharValue, - CharSpaceComment | CharBracketEnd | expectedSeparator, extraValueEndChars)) + if (!nextCharType(CharValueBegin | CharValue, + CharLineBreak | CharBracketEnd | expectedSeparator, extraValueEndChars)) return false; if (hasParsedCharTypes(CharBracketEnd)) @@ -320,12 +320,23 @@ bool RuleTextParser::parseValue(bool expectValueEnd) const char *extraChars = expectValueEnd ? extraValueEndChars : extraValueChars; - if (!parseChars(CharLetter | CharValue, extraChars)) - return false; + for (;;) { + if (!parseChars(CharLetter | CharValue, extraChars)) + return false; - if (expectValueEnd && m_charType != CharValueEnd) { - setErrorMessage(tr("Unexpected end of value")); - return false; + if (expectValueEnd) { + if (m_charType != CharValueEnd) { + setErrorMessage(tr("Unexpected end of value")); + return false; + } + + advanceCharPtr(); + + expectValueEnd = false; + extraChars = extraValueChars; + } else { + break; + } } const QStringView valueView(value, currentCharPtr() - value); @@ -403,7 +414,7 @@ void RuleTextParser::endList(int nodeIndex) bool RuleTextParser::skipComments(RuleCharTypes expectedCharTypes) { - if (!nextCharType(expectedCharTypes, CharSpaceComment | CharNewLine)) + if (!nextCharType(expectedCharTypes, CharLineBreak)) return false; ungetChar(); @@ -436,11 +447,10 @@ bool RuleTextParser::nextCharType( m_charType = getCharType(m_charType, c, extraChars); if (!checkNextCharType(expectedCharTypes, c)) { - m_charType = CharNone; return false; } - ++m_p; + advanceCharPtr(); m_parsedCharTypes |= m_charType; diff --git a/src/ui/util/conf/ruletextparser.h b/src/ui/util/conf/ruletextparser.h index 5deda37f..b5877fae 100644 --- a/src/ui/util/conf/ruletextparser.h +++ b/src/ui/util/conf/ruletextparser.h @@ -31,6 +31,7 @@ enum RuleCharType : RuleCharTypes { CharName = (CharLetter | CharExtra), // a-zA-Z_ CharValue = (CharDigit | CharExtra), // 0-9.-/: CharSpaceComment = (CharSpace | CharComment), + CharLineBreak = (CharSpaceComment | CharNewLine), }; struct RuleFilter @@ -98,9 +99,10 @@ private: void endList(int nodeIndex); void resetParsedCharTypes() { m_parsedCharTypes = CharNone; } - bool hasParsedCharTypes(RuleCharTypes v) { return v != 0 && (m_parsedCharTypes & v) != 0; } + bool hasParsedCharTypes(RuleCharTypes v) { return v == 0 || (m_parsedCharTypes & v) != 0; } void ungetChar() { --m_p; } + void advanceCharPtr() { ++m_p; } const QChar *currentCharPtr() const { return m_p; } const QChar *parsedCharPtr() const { return m_p - 1; }