Tests: tst_ruletextparser.h: Add tests

This commit is contained in:
Nodir Temirkhodjaev 2024-11-07 11:29:55 +05:00
parent 3dd6fbc7a5
commit 973581ada4
3 changed files with 97 additions and 3 deletions

View File

@ -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);
}

View File

@ -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)

View File

@ -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);