diff --git a/src/tests/UtilTest/tst_ruletextparser.h b/src/tests/UtilTest/tst_ruletextparser.h index 02bb9ce3..5c29532d 100644 --- a/src/tests/UtilTest/tst_ruletextparser.h +++ b/src/tests/UtilTest/tst_ruletextparser.h @@ -64,6 +64,15 @@ TEST_F(RuleTextParserTest, maxListDepth) ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorListMaxDepth); } +TEST_F(RuleTextParserTest, emptyComment) +{ + RuleTextParser p("#"); + + ASSERT_TRUE(p.parse()); + + ASSERT_EQ(p.ruleFilters().size(), 0); +} + TEST_F(RuleTextParserTest, lineIpPort) { RuleTextParser p("1.1.1.1:53"); @@ -246,3 +255,66 @@ TEST_F(RuleTextParserTest, badFilterName) ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorBadFilterName); } + +TEST_F(RuleTextParserTest, badEndOfValueBegin) +{ + RuleTextParser p("[1["); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorUnexpectedEndOfValue); +} + +TEST_F(RuleTextParserTest, badEndOfValueEnd) +{ + RuleTextParser p("[1]]"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorUnexpectedStartOfLine); +} + +TEST_F(RuleTextParserTest, badEndOfList) +{ + RuleTextParser p("{1"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorUnexpectedEndOfList); +} + +TEST_F(RuleTextParserTest, badEndOfValuesList) +{ + RuleTextParser p("(1"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorUnexpectedEndOfValuesList); +} + +TEST_F(RuleTextParserTest, badSymboOfListEnd) +{ + RuleTextParser p("1}"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorUnexpectedSymboOfListEnd); +} + +TEST_F(RuleTextParserTest, badExtraFilterName) +{ + RuleTextParser p("ip dir(1)"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorExtraFilterName); +} + +TEST_F(RuleTextParserTest, badBadSymbol) +{ + RuleTextParser p("1\b"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorBadSymbol); +} diff --git a/src/ui/util/conf/ruletextparser.cpp b/src/ui/util/conf/ruletextparser.cpp index 758c35d8..8b2040bc 100644 --- a/src/ui/util/conf/ruletextparser.cpp +++ b/src/ui/util/conf/ruletextparser.cpp @@ -317,20 +317,40 @@ bool RuleTextParser::parseBracketValue(RuleCharTypes expectedSeparator) resetParsedCharTypes(); if (!nextCharType(CharValueBegin | CharLetter | CharValue, - CharLineBreak | CharBracketEnd | expectedSeparator, extraValueEndChars)) + CharLineBreak | CharBracketEnd | expectedSeparator, extraValueEndChars)) { + checkBracketValueEnd(); return false; + } if (hasParsedCharTypes(CharBracketEnd)) return false; + if (!checkBracketValuesSeparator(expectedSeparator)) + return false; + + const bool expectValueEnd = hasParsedCharTypes(CharValueBegin); + + return parseValue(expectValueEnd); +} + +bool RuleTextParser::checkBracketValuesSeparator(RuleCharTypes expectedSeparator) +{ if (!hasParsedCharTypes(expectedSeparator)) { setError(ErrorUnexpectedEndOfValuesList, tr("Unexpected end of values list")); return false; } - const bool expectValueEnd = hasParsedCharTypes(CharValueBegin); + return true; +} - return parseValue(expectValueEnd); +bool RuleTextParser::checkBracketValueEnd() +{ + if (!hasParsedCharTypes(CharBracketEnd)) { + setError(ErrorUnexpectedEndOfValuesList, tr("Unexpected end of values list")); + return false; + } + + return true; } bool RuleTextParser::parseValue(bool expectValueEnd) diff --git a/src/ui/util/conf/ruletextparser.h b/src/ui/util/conf/ruletextparser.h index c4fa3ba4..2fdd2e1f 100644 --- a/src/ui/util/conf/ruletextparser.h +++ b/src/ui/util/conf/ruletextparser.h @@ -108,6 +108,8 @@ private: void parseBracketValues(); bool parseBracketValue(RuleCharTypes expectedSeparator); + bool checkBracketValuesSeparator(RuleCharTypes expectedSeparator); + bool checkBracketValueEnd(); bool parseValue(bool expectValueEnd); bool checkValueEnd(bool &expectValueEnd);