From: Shawn Routhier Date: Tue, 22 Mar 2016 04:50:08 +0000 (-0700) Subject: [trac4269] Add unit tests and move the parse code to the right place X-Git-Tag: trac4106_update_base~43^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f6c8fa1b54958b5d292a562d987b968cb30c6736;p=thirdparty%2Fkea.git [trac4269] Add unit tests and move the parse code to the right place --- diff --git a/src/lib/eval/parser.yy b/src/lib/eval/parser.yy index 97324009ef..ac8a1334a5 100644 --- a/src/lib/eval/parser.yy +++ b/src/lib/eval/parser.yy @@ -131,11 +131,6 @@ bool_expr : "(" bool_expr ")" error(@1, "relay4 can only be used in DHCPv4."); } } - | PKT6 "." pkt6_field - { - TokenPtr pkt6field(new TokenPkt6($3)); - ctx.expression.push_back(pkt6field); - } ; string_expr : STRING @@ -183,6 +178,11 @@ string_expr : STRING TokenPtr conc(new TokenConcat()); ctx.expression.push_back(conc); } + | PKT6 "." pkt6_field + { + TokenPtr pkt6field(new TokenPkt6($3)); + ctx.expression.push_back(pkt6field); + } ; option_code : INTEGER diff --git a/src/lib/eval/tests/context_unittest.cc b/src/lib/eval/tests/context_unittest.cc index 5f1e71e953..72c25adff4 100644 --- a/src/lib/eval/tests/context_unittest.cc +++ b/src/lib/eval/tests/context_unittest.cc @@ -123,6 +123,20 @@ public: EXPECT_TRUE(conc); } + /// @brief checks if the given token is Pkt6 of specified type + /// @param token token to be checked + /// @param type expected type of the Pkt6 field + void checkTokenPkt6(const TokenPtr& token, TokenPkt6::FieldType type) { + ASSERT_TRUE(token); + + boost::shared_ptr pkt = + boost::dynamic_pointer_cast(token); + + ASSERT_TRUE(pkt); + + EXPECT_EQ(type, pkt->getType()); + } + /// @brief checks if the given expression raises the expected message /// when it is parsed. void checkError(const string& expr, const string& msg) { @@ -147,6 +161,38 @@ public: universe_ = universe; } + /// @brief Test that verifies access to the DHCPv6 packet fields. + /// + /// This test attempts to parse the expression, will check if the number + /// of tokens is exactly as planned and then will try to verify if the + /// first token represents expected the field in DHCPv6 packet. + /// + /// @param expr expression to be parsed + /// @param exp_type expected field type to be parsed + /// @param exp_tokens expected number of tokens + void testPkt6Field(std::string expr, TokenPkt6::FieldType exp_type, + int exp_tokens) { + EvalContext eval(Option::V6); + + // Parse the expression. + try { + parsed_ = eval.parseString(expr); + } + catch (const EvalParseError& ex) { + FAIL() << "Exception thrown: " << ex.what(); + return; + } + + // Parsing should succeed and return a token. + EXPECT_TRUE(parsed_); + + // There should be the requested number of tokens + ASSERT_EQ(exp_tokens, eval.expression.size()); + + // Check that the first token is TokenPkt6 instance and has correct type. + checkTokenPkt6(eval.expression.at(0), exp_type); + } + Option::Universe universe_; bool parsed_; ///< Parsing status }; @@ -334,6 +380,16 @@ TEST_F(EvalContextTest, relay4Error) { ":1.1-6: relay4 can only be used in DHCPv4."); } +// Tests whether message type field in DHCPv6 can be accessed. +TEST_F(EvalContextTest, pkt6FieldMsgtype) { + testPkt6Field("pkt6.msgtype == '1'", TokenPkt6::MSGTYPE, 3); +} + +// Tests whether transaction id field in DHCPv6 can be accessed. +TEST_F(EvalContextTest, pkt6FieldTransid) { + testPkt6Field("pkt6.transid == '1'", TokenPkt6::TRANSID, 3); +} + // Test parsing of logical operators TEST_F(EvalContextTest, logicalOps) { // option.exists diff --git a/src/lib/eval/tests/token_unittest.cc b/src/lib/eval/tests/token_unittest.cc index 5c970f3103..176b267113 100644 --- a/src/lib/eval/tests/token_unittest.cc +++ b/src/lib/eval/tests/token_unittest.cc @@ -967,3 +967,22 @@ TEST_F(TokenTest, concat) { ASSERT_EQ(1, values_.size()); EXPECT_EQ("foobar", values_.top()); } + +// Verifies if the DHCPv6 packet fields can be extracted. +TEST_F(TokenTest, pkt6Fields) { + // The default test creates a v6 DHCPV6_SOLICIT packet with a + // transaction id of 12345. + + // Check the message type + ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::MSGTYPE))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("1", values_.top()); + + // Check the transaction id field + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("12345", values_.top()); +}