From 59845ecc48ef15e480c6c290bb0b8d14e4d7a0de Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 6 Nov 2024 17:05:44 +0500 Subject: [PATCH] UI: RuleTextParser: Add errorCode() --- src/driver/common/fortconf.h | 4 +-- src/tests/UtilTest/tst_ruletextparser.h | 18 ++++++++++ src/ui/util/conf/ruletextparser.cpp | 44 ++++++++++++++----------- src/ui/util/conf/ruletextparser.h | 26 +++++++++++++-- 4 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/driver/common/fortconf.h b/src/driver/common/fortconf.h index fa71babc..2e5525ed 100644 --- a/src/driver/common/fortconf.h +++ b/src/driver/common/fortconf.h @@ -120,8 +120,8 @@ enum FORT_RULE_FILTER_TYPE { FORT_RULE_FILTER_TYPE_LIST_OR, FORT_RULE_FILTER_TYPE_LIST_AND, // Complex types - FORT_RULE_FILTER_TYPE_ADDRESS_TCP, - FORT_RULE_FILTER_TYPE_ADDRESS_UDP, + FORT_RULE_FILTER_TYPE_PORT_TCP, + FORT_RULE_FILTER_TYPE_PORT_UDP, }; typedef struct fort_conf_rule_filter diff --git a/src/tests/UtilTest/tst_ruletextparser.h b/src/tests/UtilTest/tst_ruletextparser.h index 47b60524..e5fbe780 100644 --- a/src/tests/UtilTest/tst_ruletextparser.h +++ b/src/tests/UtilTest/tst_ruletextparser.h @@ -44,6 +44,24 @@ TEST_F(RuleTextParserTest, emptyList) ASSERT_EQ(p.ruleFilters().size(), 0); } +TEST_F(RuleTextParserTest, emptyListDepth) +{ + RuleTextParser p("{{{{{{{}}}}}}}"); + + ASSERT_TRUE(p.parse()); + + ASSERT_EQ(p.ruleFilters().size(), 0); +} + +TEST_F(RuleTextParserTest, maxListDepth) +{ + RuleTextParser p("{{{{{{{{{{"); + + ASSERT_FALSE(p.parse()); + + ASSERT_EQ(p.errorCode(), RuleTextParser::ErrorListMaxDepth); +} + TEST_F(RuleTextParserTest, lineIpPort) { RuleTextParser p("1.1.1.1:53"); diff --git a/src/ui/util/conf/ruletextparser.cpp b/src/ui/util/conf/ruletextparser.cpp index 22a28241..82853fac 100644 --- a/src/ui/util/conf/ruletextparser.cpp +++ b/src/ui/util/conf/ruletextparser.cpp @@ -67,8 +67,7 @@ RuleCharType getCharType(RuleCharType prevCharType, const QChar c, const char *e bool RuleFilter::isTypeAddress() const { - return type == FORT_RULE_FILTER_TYPE_ADDRESS || type == FORT_RULE_FILTER_TYPE_ADDRESS_TCP - || type == FORT_RULE_FILTER_TYPE_ADDRESS_UDP; + return type == FORT_RULE_FILTER_TYPE_ADDRESS; } RuleTextParser::RuleTextParser(const QString &text, QObject *parent) : QObject(parent), m_text(text) @@ -84,12 +83,16 @@ void RuleTextParser::setupCharPtr() bool RuleTextParser::parse() { - parseLines(); - - return !hasError(); + return parseLines(); } -void RuleTextParser::parseLines() +void RuleTextParser::setError(ErrorCode errorCode, const QString &errorMessage) +{ + setErrorCode(errorCode); + setErrorMessage(errorMessage); +} + +bool RuleTextParser::parseLines() { const int nodeIndex = beginList(FORT_RULE_FILTER_TYPE_LIST_OR); @@ -102,6 +105,8 @@ void RuleTextParser::parseLines() } endList(nodeIndex); + + return !hasError(); } bool RuleTextParser::parseLine() @@ -212,17 +217,18 @@ void RuleTextParser::processSectionList() if (!checkListBegin()) return; - parseLines(); + if (!parseLines()) + return; if (!m_ruleFilter.isListEnd) { - setErrorMessage(tr("Unexpected end of list")); + setError(ErrorUnexpectedEndOfList, tr("Unexpected end of list")); } } bool RuleTextParser::checkListBegin() { if (++m_listDepth > FORT_CONF_RULE_FILTER_DEPTH_MAX) { - setErrorMessage(tr("Max list depth exceeded: %1").arg(m_listDepth)); + setError(ErrorListMaxDepth, tr("Max list depth exceeded: %1").arg(m_listDepth)); return false; } @@ -232,7 +238,7 @@ bool RuleTextParser::checkListBegin() bool RuleTextParser::checkListEnd() { if (--m_listDepth < 0) { - setErrorMessage(tr("Unexpected symbol of list end")); + setError(ErrorUnexpectedSymboOfListEnd, tr("Unexpected symbol of list end")); return false; } @@ -256,14 +262,14 @@ bool RuleTextParser::parseName() { "protocol", FORT_RULE_FILTER_TYPE_PROTOCOL }, { "dir", FORT_RULE_FILTER_TYPE_DIRECTION }, { "direction", FORT_RULE_FILTER_TYPE_DIRECTION }, - { "tcp", FORT_RULE_FILTER_TYPE_ADDRESS_TCP }, - { "udp", FORT_RULE_FILTER_TYPE_ADDRESS_UDP }, + { "tcp", FORT_RULE_FILTER_TYPE_PORT_TCP }, + { "udp", FORT_RULE_FILTER_TYPE_PORT_UDP }, }; const QStringView nameView(name, currentCharPtr() - name); if (m_ruleFilter.hasFilterName) { - setErrorMessage(tr("Extra filter name: %1").arg(nameView)); + setError(ErrorExtraFilterName, tr("Extra filter name: %1").arg(nameView)); return false; } @@ -272,7 +278,7 @@ bool RuleTextParser::parseName() m_ruleFilter.type = filterTypesMap.value(nameLower, FORT_RULE_FILTER_TYPE_INVALID); if (m_ruleFilter.type == -1) { - setErrorMessage(tr("Bad filter name: %1").arg(nameView)); + setError(ErrorBadFilterName, tr("Bad filter name: %1").arg(nameView)); return false; } @@ -305,7 +311,7 @@ bool RuleTextParser::parseBracketValue(RuleCharTypes expectedSeparator) return false; if (!hasParsedCharTypes(expectedSeparator)) { - setErrorMessage(tr("Unexpected end of values list")); + setError(ErrorUnexpectedEndOfValuesList, tr("Unexpected end of values list")); return false; } @@ -326,7 +332,7 @@ bool RuleTextParser::parseValue(bool expectValueEnd) if (expectValueEnd) { if (m_charType != CharValueEnd) { - setErrorMessage(tr("Unexpected end of value")); + setError(ErrorUnexpectedEndOfValue, tr("Unexpected end of value")); return false; } @@ -350,7 +356,7 @@ bool RuleTextParser::checkAddFilter() { if (!m_ruleFilter.hasValues()) { if (!m_ruleFilter.isSectionEnd) { - setErrorMessage(tr("Unexpected end of line section")); + setError(ErrorUnexpectedEndOfLineSection, tr("Unexpected end of line section")); return false; } @@ -358,7 +364,7 @@ bool RuleTextParser::checkAddFilter() } if (m_ruleFilter.type == FORT_RULE_FILTER_TYPE_INVALID) { - setErrorMessage(tr("No filter name")); + setError(ErrorNoFilterName, tr("No filter name")); return false; } @@ -464,7 +470,7 @@ bool RuleTextParser::nextCharType( bool RuleTextParser::checkNextCharType(RuleCharTypes expectedCharTypes, const QChar c) { if (m_charType == CharNone) { - setErrorMessage(tr("Bad symbol: %1").arg(c)); + setError(ErrorBadSymbol, tr("Bad symbol: %1").arg(c)); return false; } diff --git a/src/ui/util/conf/ruletextparser.h b/src/ui/util/conf/ruletextparser.h index b5877fae..4328e33f 100644 --- a/src/ui/util/conf/ruletextparser.h +++ b/src/ui/util/conf/ruletextparser.h @@ -59,9 +59,25 @@ class RuleTextParser : public QObject Q_OBJECT public: + enum ErrorCode : quint16 { + ErrorNone = 0, + ErrorUnexpectedEndOfList, + ErrorUnexpectedEndOfValuesList, + ErrorUnexpectedSymboOfListEnd, + ErrorUnexpectedEndOfValue, + ErrorUnexpectedEndOfLineSection, + ErrorListMaxDepth, + ErrorExtraFilterName, + ErrorBadFilterName, + ErrorNoFilterName, + ErrorBadSymbol, + }; + Q_ENUM(ErrorCode) + explicit RuleTextParser(const QString &text, QObject *parent = nullptr); - QString errorMessage() const { return m_errorMessage; } + ErrorCode errorCode() const { return m_errorCode; } + const QString &errorMessage() const { return m_errorMessage; } bool hasError() const { return !errorMessage().isEmpty(); } @@ -70,11 +86,14 @@ public: bool parse(); private: - void setErrorMessage(const QString &errorMessage) { m_errorMessage = errorMessage; } + void setErrorCode(ErrorCode v) { m_errorCode = v; } + void setErrorMessage(const QString &v) { m_errorMessage = v; } + + void setError(ErrorCode errorCode, const QString &errorMessage); void setupCharPtr(); - void parseLines(); + bool parseLines(); bool parseLine(); bool parseLineSection(RuleCharTypes expectedSeparator); bool processSection(); @@ -128,6 +147,7 @@ private: private: qint8 m_listDepth = 0; + ErrorCode m_errorCode = ErrorNone; RuleCharType m_charType = CharNone; RuleCharTypes m_parsedCharTypes = CharNone; RuleFilter m_ruleFilter;