Commit 97aa91c6 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[5132] evaluate renamed to evaluateBool

parent 2e624142
......@@ -2713,7 +2713,7 @@ void Dhcpv4Srv::classifyPacket(const Pkt4Ptr& pkt) {
// Evaluate the expression which can return false (no match),
// true (match) or raise an exception (error)
try {
bool status = evaluate(*expr_ptr, *pkt);
bool status = evaluateBool(*expr_ptr, *pkt);
if (status) {
LOG_INFO(options4_logger, EVAL_RESULT)
.arg(it->first)
......
......@@ -3007,7 +3007,7 @@ void Dhcpv6Srv::classifyPacket(const Pkt6Ptr& pkt) {
// Evaluate the expression which can return false (no match),
// true (match) or raise an exception (error)
try {
bool status = evaluate(*expr_ptr, *pkt);
bool status = evaluateBool(*expr_ptr, *pkt);
if (status) {
LOG_INFO(dhcp6_logger, EVAL_RESULT)
.arg(it->first)
......
......@@ -66,7 +66,7 @@ protected:
boost::shared_ptr<PktType> pkt(new PktType(family == AF_INET ?
DHCPDISCOVER : DHCPV6_SOLICIT,
123));
EXPECT_FALSE(evaluate(*parsed_expr, *pkt));
EXPECT_FALSE(evaluateBool(*parsed_expr, *pkt));
// Now add the option so it will pass. Use a standard option carrying a
// single string value, i.e. hostname for DHCPv4 and bootfile url for
......@@ -76,7 +76,7 @@ protected:
DHO_HOST_NAME : D6O_BOOTFILE_URL,
option_string));
pkt->addOption(opt);
EXPECT_TRUE(evaluate(*parsed_expr, *pkt));
EXPECT_TRUE(evaluateBool(*parsed_expr, *pkt));
}
};
......@@ -328,12 +328,12 @@ TEST_F(ClientClassDefParserTest, nameAndExpressionClass) {
// Build a packet that will fail evaluation.
Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 123));
EXPECT_FALSE(evaluate(*match_expr, *pkt4));
EXPECT_FALSE(evaluateBool(*match_expr, *pkt4));
// Now add the option so it will pass.
OptionPtr opt(new OptionString(Option::V4, 100, "works right"));
pkt4->addOption(opt);
EXPECT_TRUE(evaluate(*match_expr, *pkt4));
EXPECT_TRUE(evaluateBool(*match_expr, *pkt4));
}
// Verifies you can create a class with a name and options,
......@@ -414,12 +414,12 @@ TEST_F(ClientClassDefParserTest, basicValidClass) {
// Build a packet that will fail evaluation.
Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 123));
EXPECT_FALSE(evaluate(*match_expr, *pkt4));
EXPECT_FALSE(evaluateBool(*match_expr, *pkt4));
// Now add the option so it will pass.
OptionPtr opt(new OptionString(Option::V4, 100, "booya"));
pkt4->addOption(opt);
EXPECT_TRUE(evaluate(*match_expr, *pkt4));
EXPECT_TRUE(evaluateBool(*match_expr, *pkt4));
}
// Verifies that a class with no name, fails to parse.
......
......@@ -9,7 +9,7 @@
namespace isc {
namespace dhcp {
bool evaluate(const Expression& expr, Pkt& pkt) {
bool evaluateBool(const Expression& expr, Pkt& pkt) {
ValueStack values;
for (Expression::const_iterator it = expr.begin();
it != expr.end(); ++it) {
......@@ -22,5 +22,19 @@ bool evaluate(const Expression& expr, Pkt& pkt) {
return (Token::toBool(values.top()));
}
std::string
evaluateString(const Expression& expr, Pkt& pkt) {
ValueStack values;
for (auto it = expr.begin(); it != expr.end(); ++it) {
(*it)->evaluate(pkt, values);
}
if (values.size() != 1) {
isc_throw(EvalBadStack, "Incorrect stack order. Expected exactly "
"1 value at the end of evaluatuion, got " << values.size());
}
return (values.top());
}
}; // end of isc::dhcp namespace
}; // end of isc namespace
......@@ -8,6 +8,7 @@
#define EVALUATE_H
#include <eval/token.h>
#include <string>
namespace isc {
namespace dhcp {
......@@ -22,7 +23,10 @@ namespace dhcp {
/// stack at the end of the evaluation
/// @throw EvalTypeError if the value at the top of the stack at the
/// end of the evaluation is not "false" or "true"
bool evaluate(const Expression& expr, Pkt& pkt);
bool evaluateBool(const Expression& expr, Pkt& pkt);
std::string evaluateString(const Expression& expr, Pkt& pkt);
}; // end of isc::dhcp namespace
}; // end of isc namespace
......
......@@ -27,9 +27,9 @@ public:
ASSERT_TRUE(eval.parseString(expr));
Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345));
if (expected) {
EXPECT_TRUE(evaluate(eval.expression, *pkt4));
EXPECT_TRUE(evaluateBool(eval.expression, *pkt4));
} else {
EXPECT_FALSE(evaluate(eval.expression, *pkt4));
EXPECT_FALSE(evaluateBool(eval.expression, *pkt4));
}
}
};
......
......@@ -64,13 +64,13 @@ public:
// This checks the empty expression: it should raise EvalBadStack
// when evaluated with a Pkt4. (The actual packet is not used)
TEST_F(EvaluateTest, empty4) {
ASSERT_THROW(evaluate(e_, *pkt4_), EvalBadStack);
ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalBadStack);
}
// This checks the empty expression: it should raise EvalBadStack
// when evaluated with a Pkt6. (The actual packet is not used)
TEST_F(EvaluateTest, empty6) {
ASSERT_THROW(evaluate(e_, *pkt6_), EvalBadStack);
ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalBadStack);
}
// This checks the { "false" } expression: it should return false
......@@ -79,7 +79,7 @@ TEST_F(EvaluateTest, false4) {
TokenPtr tfalse;
ASSERT_NO_THROW(tfalse.reset(new TokenString("false")));
e_.push_back(tfalse);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_FALSE(result_);
}
......@@ -89,7 +89,7 @@ TEST_F(EvaluateTest, false6) {
TokenPtr tfalse;
ASSERT_NO_THROW(tfalse.reset(new TokenString("false")));
e_.push_back(tfalse);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_FALSE(result_);
}
......@@ -99,7 +99,7 @@ TEST_F(EvaluateTest, true4) {
TokenPtr ttrue;
ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
e_.push_back(ttrue);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_TRUE(result_);
}
......@@ -109,7 +109,7 @@ TEST_F(EvaluateTest, true6) {
TokenPtr ttrue;
ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
e_.push_back(ttrue);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_TRUE(result_);
}
......@@ -119,7 +119,7 @@ TEST_F(EvaluateTest, bad4) {
TokenPtr bad;
ASSERT_NO_THROW(bad.reset(new TokenString("bad")));
e_.push_back(bad);
ASSERT_THROW(evaluate(e_, *pkt4_), EvalTypeError);
ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalTypeError);
}
// This checks the evaluation must lead to "false" or "true"
......@@ -128,7 +128,7 @@ TEST_F(EvaluateTest, bad6) {
TokenPtr bad;
ASSERT_NO_THROW(bad.reset(new TokenString("bad")));
e_.push_back(bad);
ASSERT_THROW(evaluate(e_, *pkt6_), EvalTypeError);
ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalTypeError);
}
// This checks the evaluation must leave only one value on the stack
......@@ -138,7 +138,7 @@ TEST_F(EvaluateTest, two4) {
ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
e_.push_back(ttrue);
e_.push_back(ttrue);
ASSERT_THROW(evaluate(e_, *pkt4_), EvalBadStack);
ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalBadStack);
}
// This checks the evaluation must leave only one value on the stack
......@@ -148,7 +148,7 @@ TEST_F(EvaluateTest, two6) {
ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
e_.push_back(ttrue);
e_.push_back(ttrue);
ASSERT_THROW(evaluate(e_, *pkt6_), EvalBadStack);
ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalBadStack);
}
// A more complex test evaluated with a Pkt4. (The actual packet is not used)
......@@ -164,7 +164,7 @@ TEST_F(EvaluateTest, compare4) {
ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
e_.push_back(tequal);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_FALSE(result_);
}
......@@ -181,7 +181,7 @@ TEST_F(EvaluateTest, compare6) {
ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
e_.push_back(tequal);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_FALSE(result_);
}
......@@ -192,9 +192,9 @@ TEST_F(EvaluateTest, exists) {
ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::EXISTS)));
e_.push_back(toption);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_TRUE(result_);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_TRUE(result_);
}
......@@ -205,9 +205,9 @@ TEST_F(EvaluateTest, dontExists) {
ASSERT_NO_THROW(toption.reset(new TokenOption(101, TokenOption::EXISTS)));
e_.push_back(toption);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_FALSE(result_);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_FALSE(result_);
}
......@@ -224,9 +224,9 @@ TEST_F(EvaluateTest, packet) {
ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
e_.push_back(tequal);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_TRUE(result_);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_FALSE(result_);
}
......@@ -243,9 +243,9 @@ TEST_F(EvaluateTest, optionHex) {
ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
e_.push_back(tequal);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_TRUE(result_);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_FALSE(result_);
}
......@@ -277,9 +277,9 @@ TEST_F(EvaluateTest, complex) {
e_.push_back(tequal);
// Should return true for v4 and v6 packets
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt4_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
EXPECT_TRUE(result_);
ASSERT_NO_THROW(result_ = evaluate(e_, *pkt6_));
ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
EXPECT_TRUE(result_);
}
......@@ -315,11 +315,11 @@ public:
switch (u) {
case Option::V4:
ASSERT_NO_THROW(result = evaluate(eval.expression, *pkt4_))
ASSERT_NO_THROW(result = evaluateBool(eval.expression, *pkt4_))
<< " for expression " << expr;
break;
case Option::V6:
ASSERT_NO_THROW(result = evaluate(eval.expression, *pkt6_))
ASSERT_NO_THROW(result = evaluateBool(eval.expression, *pkt6_))
<< " for expression " << expr;
break;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment