mirror of
https://github.com/tnodir/fort
synced 2024-11-14 22:05:12 +00:00
UI: RuleTextParser: Add errorCode()
This commit is contained in:
parent
8bb8ef1767
commit
59845ecc48
@ -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
|
||||
|
@ -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");
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user