From: Marcin Siodelski Date: Thu, 19 Nov 2015 13:59:31 +0000 (+0100) Subject: [4093] TokenOption may evaluate using hexadecimal format. X-Git-Tag: trac4204_base~1^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb932d680958eea75e0b73eb22475a453bd419fe;p=thirdparty%2Fkea.git [4093] TokenOption may evaluate using hexadecimal format. --- diff --git a/src/lib/dhcp/option.h b/src/lib/dhcp/option.h index 0cda90e84e..e993aac75c 100644 --- a/src/lib/dhcp/option.h +++ b/src/lib/dhcp/option.h @@ -219,7 +219,8 @@ public: /// @brief Returns string containing hexadecimal representation of option. /// /// @param include_header Boolean flag which indicates if the output should - /// also contain header fields. The default is that it shouldn't. + /// also contain header fields. The default is that it shouldn't include + /// header fields. /// /// @return String containing hexadecimal representation of the option. virtual std::string toHexString(const bool include_header = false); diff --git a/src/lib/eval/tests/token_unittest.cc b/src/lib/eval/tests/token_unittest.cc index 99581fbb92..c4d6e07653 100644 --- a/src/lib/eval/tests/token_unittest.cc +++ b/src/lib/eval/tests/token_unittest.cc @@ -274,6 +274,35 @@ TEST_F(TokenTest, optionString4) { EXPECT_EQ("hundred4", values_.top()); } +// This test checks if a token representing option value is able to extract +// the option from an IPv4 packet and properly store its value in a +// hexadecimal format. +TEST_F(TokenTest, optionHexString4) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL))); + + // This should evaluate to the content of the option 100 (i.e. "hundred4") + ASSERT_NO_THROW(found->evaluate(*pkt4_, values_)); + + // This should evaluate to "" as there is no option 101. + ASSERT_NO_THROW(not_found->evaluate(*pkt4_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. We should get the empty + // string first. + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Then the content of the option 100. + EXPECT_EQ("0x68756E6472656434", values_.top()); +} + // This test checks if a token representing an option value is able to extract // the option from an IPv6 packet and properly store the option's value. TEST_F(TokenTest, optionString6) { @@ -302,6 +331,35 @@ TEST_F(TokenTest, optionString6) { EXPECT_EQ("hundred6", values_.top()); } +// This test checks if a token representing an option value is able to extract +// the option from an IPv6 packet and properly store its value in hexadecimal +// format. +TEST_F(TokenTest, optionHexString6) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL))); + + // This should evaluate to the content of the option 100 (i.e. "hundred6") + ASSERT_NO_THROW(found->evaluate(*pkt6_, values_)); + + // This should evaluate to "" as there is no option 101. + ASSERT_NO_THROW(not_found->evaluate(*pkt6_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. We should get the empty + // string first. + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Then the content of the option 100. + EXPECT_EQ("0x68756E6472656436", values_.top()); +} + // This test checks if a token representing an == operator is able to // compare two values (with incorrectly built stack). TEST_F(TokenTest, optionEqualInvalid) { diff --git a/src/lib/eval/token.cc b/src/lib/eval/token.cc index e0a50bf7cd..85a4806f12 100644 --- a/src/lib/eval/token.cc +++ b/src/lib/eval/token.cc @@ -66,7 +66,8 @@ void TokenOption::evaluate(const Pkt& pkt, ValueStack& values) { OptionPtr opt = pkt.getOption(option_code_); if (opt) { - values.push(opt->toString()); + values.push(representation_type_ == TEXTUAL ? opt->toString() + : opt->toHexString()); } else { // Option not found, push empty string values.push("");