From ce9e323f186dcfc5b3344354383fad697d2972b4 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 6 Nov 2024 19:41:40 +0500 Subject: [PATCH] Tests: tst_ruletextparser.h: Add tests --- src/tests/UtilTest/tst_ruletextparser.h | 36 +++++++++++++++++++++++++ src/ui/util/conf/ruletextparser.cpp | 10 +++++-- src/ui/util/conf/ruletextparser.h | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/tests/UtilTest/tst_ruletextparser.h b/src/tests/UtilTest/tst_ruletextparser.h index d1f905fd..02bb9ce3 100644 --- a/src/tests/UtilTest/tst_ruletextparser.h +++ b/src/tests/UtilTest/tst_ruletextparser.h @@ -210,3 +210,39 @@ TEST_F(RuleTextParserTest, lineIp6Range) checkStringList(rf.values, { "67" }); } } + +TEST_F(RuleTextParserTest, lineEmptySections) +{ + RuleTextParser p("1:::2::\n"); + + ASSERT_TRUE(p.parse()); + + ASSERT_EQ(p.ruleFilters().size(), 4); +} + +TEST_F(RuleTextParserTest, badStartOfLine) +{ + RuleTextParser p(":1\n"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorUnexpectedStartOfLine); +} + +TEST_F(RuleTextParserTest, badNoFilterName) +{ + RuleTextParser p("1:2:3"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorNoFilterName); +} + +TEST_F(RuleTextParserTest, badFilterName) +{ + RuleTextParser p("test(1)"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorBadFilterName); +} diff --git a/src/ui/util/conf/ruletextparser.cpp b/src/ui/util/conf/ruletextparser.cpp index 343c65bf..57c36dc4 100644 --- a/src/ui/util/conf/ruletextparser.cpp +++ b/src/ui/util/conf/ruletextparser.cpp @@ -97,8 +97,14 @@ bool RuleTextParser::parseLines() const int nodeIndex = beginList(FORT_RULE_FILTER_TYPE_LIST_OR); for (;;) { - if (!skipComments(CharLineBegin)) + if (!skipComments(CharLineBegin)) { + if (!isEmpty()) { + setError(ErrorUnexpectedStartOfLine, tr("Unexpected start of line")); + return false; + } + break; + } if (!parseLine()) break; @@ -142,7 +148,7 @@ bool RuleTextParser::parseLine() endList(nodeIndex); - return true; + return !hasError(); } bool RuleTextParser::parseLineSection(RuleCharTypes expectedSeparator) diff --git a/src/ui/util/conf/ruletextparser.h b/src/ui/util/conf/ruletextparser.h index c5aabb94..953f3bc5 100644 --- a/src/ui/util/conf/ruletextparser.h +++ b/src/ui/util/conf/ruletextparser.h @@ -61,6 +61,7 @@ class RuleTextParser : public QObject public: enum ErrorCode : quint16 { ErrorNone = 0, + ErrorUnexpectedStartOfLine, ErrorUnexpectedEndOfList, ErrorUnexpectedEndOfValuesList, ErrorUnexpectedSymboOfListEnd,