Browse Source

[4093] TokenOption may evaluate using hexadecimal format.

Marcin Siodelski 9 years ago
parent
commit
eb932d6809
3 changed files with 62 additions and 2 deletions
  1. 2 1
      src/lib/dhcp/option.h
  2. 58 0
      src/lib/eval/tests/token_unittest.cc
  3. 2 1
      src/lib/eval/token.cc

+ 2 - 1
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);

+ 58 - 0
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) {

+ 2 - 1
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("");