Browse Source

[4091] Missed changes

Francis Dupont 9 years ago
parent
commit
e20f7d8334
2 changed files with 98 additions and 3 deletions
  1. 88 0
      src/lib/eval/tests/token_unittest.cc
  2. 10 3
      src/lib/eval/token.cc

+ 88 - 0
src/lib/eval/tests/token_unittest.cc

@@ -24,6 +24,8 @@
 #include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
 
+#include <arpa/inet.h>
+
 using namespace std;
 using namespace isc::dhcp;
 
@@ -127,6 +129,92 @@ TEST_F(TokenTest, string6) {
     EXPECT_EQ("foo", values_.top());
 }
 
+// This simple test checks that a TokenHexString, representing a constant
+// string coded in hexadecimal, can be used in Pkt4 evaluation.
+// (The actual packet is not used)
+TEST_F(TokenTest, hexstring4) {
+    TokenPtr empty;
+    TokenPtr bad;
+    TokenPtr bell;
+    TokenPtr foo;
+    TokenPtr cookie;
+
+    // Store constant empty hexstring "" ("") in the TokenHexString object.
+    ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
+    // Store bad encoded hexstring "xabc" ("") in the TokenHexString object.
+    ASSERT_NO_THROW(bad.reset(new TokenHexString("xabc")));
+    // Store hexstring with an odd number of hexdigits "7" ("\a")
+    ASSERT_NO_THROW(bell.reset(new TokenHexString("7")));
+    // Store constant hexstring "666f6f" ("foo") in the TokenHexString object.
+    ASSERT_NO_THROW(foo.reset(new TokenHexString("666f6f")));
+    // Store constant hexstring "63825363" (DHCP_OPTIONS_COOKIE)
+    ASSERT_NO_THROW(cookie.reset(new TokenHexString("63825363")));
+
+    // Make sure that tokens can be evaluated without exceptions.
+    ASSERT_NO_THROW(empty->evaluate(*pkt4_, values_));
+    ASSERT_NO_THROW(bad->evaluate(*pkt4_, values_));
+    ASSERT_NO_THROW(bell->evaluate(*pkt4_, values_));
+    ASSERT_NO_THROW(foo->evaluate(*pkt4_, values_));
+    ASSERT_NO_THROW(cookie->evaluate(*pkt4_, values_));
+
+    // Check that the evaluation put its value on the values stack.
+    ASSERT_EQ(5, values_.size());
+    uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
+    EXPECT_EQ(4, values_.top().size());
+    EXPECT_EQ(0, std::memcmp(&expected, &values_.top()[0], 4));
+    values_.pop();
+    EXPECT_EQ("foo", values_.top());
+    values_.pop();
+    EXPECT_EQ("\a", values_.top());
+    values_.pop();
+    EXPECT_EQ("", values_.top());
+    values_.pop();
+    EXPECT_EQ("", values_.top());
+}
+
+// This simple test checks that a TokenHexString, representing a constant
+// string coded in hexadecimal, can be used in Pkt6 evaluation.
+// (The actual packet is not used)
+TEST_F(TokenTest, hexstring6) {
+    TokenPtr empty;
+    TokenPtr bad;
+    TokenPtr bell;
+    TokenPtr foo;
+    TokenPtr cookie;
+
+    // Store constant empty hexstring "" ("") in the TokenHexString object.
+    ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
+    // Store bad encoded hexstring "xabc" ("") in the TokenHexString object.
+    ASSERT_NO_THROW(bad.reset(new TokenHexString("xabc")));
+    // Store hexstring with an odd number of hexdigits "7" ("\a")
+    ASSERT_NO_THROW(bell.reset(new TokenHexString("7")));
+    // Store constant hexstring "666f6f" ("foo") in the TokenHexString object.
+    ASSERT_NO_THROW(foo.reset(new TokenHexString("666f6f")));
+    // Store constant hexstring "63825363" (DHCP_OPTIONS_COOKIE)
+    ASSERT_NO_THROW(cookie.reset(new TokenHexString("63825363")));
+
+    // Make sure that tokens can be evaluated without exceptions.
+    ASSERT_NO_THROW(empty->evaluate(*pkt6_, values_));
+    ASSERT_NO_THROW(bad->evaluate(*pkt6_, values_));
+    ASSERT_NO_THROW(bell->evaluate(*pkt6_, values_));
+    ASSERT_NO_THROW(foo->evaluate(*pkt6_, values_));
+    ASSERT_NO_THROW(cookie->evaluate(*pkt6_, values_));
+
+    // Check that the evaluation put its value on the values stack.
+    ASSERT_EQ(5, values_.size());
+    uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
+    EXPECT_EQ(4, values_.top().size());
+    EXPECT_EQ(0, std::memcmp(&expected, &values_.top()[0], 4));
+    values_.pop();
+    EXPECT_EQ("foo", values_.top());
+    values_.pop();
+    EXPECT_EQ("\a", values_.top());
+    values_.pop();
+    EXPECT_EQ("", values_.top());
+    values_.pop();
+    EXPECT_EQ("", values_.top());
+}
+
 // This test checks if a token representing an option value is able to extract
 // the option from an IPv4 packet and properly store the option's value.
 TEST_F(TokenTest, optionString4) {

+ 10 - 3
src/lib/eval/token.cc

@@ -16,6 +16,7 @@
 #include <eval/eval_log.h>
 #include <util/encode/hex.h>
 #include <boost/lexical_cast.hpp>
+#include <cstring>
 #include <string>
 
 using namespace isc::dhcp;
@@ -28,14 +29,19 @@ TokenString::evaluate(const Pkt& /*pkt*/, ValueStack& values) {
 }
 
 void
-TokenString::evaluate(const Pkt& /*pkt*/, ValueStack& values) {
+TokenHexString::evaluate(const Pkt& /*pkt*/, ValueStack& values) {
+    // Eliminate the empty string case first
+    if (repr_.empty()) {
+        values.push("");
+        return;
+    }
     // Transform string of hexadecimal digits into binary format
     std::vector<uint8_t> binary;
     try {
         // The decodeHex function expects that the string contains an
         // even number of digits. If we don't meet this requirement,
         // we have to insert a leading 0.
-        if (!repr_.empty() && repr_.length() % 2) {
+        if (repr_.length() % 2) {
             repr_ = repr_.insert(0, "0");
         }
         util::encode::decodeHex(repr_, binary);
@@ -45,9 +51,10 @@ TokenString::evaluate(const Pkt& /*pkt*/, ValueStack& values) {
     }
     // Convert to a string
     std::string chars(binary.size(), '\0');
+    // Note that binary.size() cannot be 0
     std::memmove(&chars[0], &binary[0], binary.size());
     // Literals only push, nothing to pop
-    values.push(chars_);
+    values.push(chars);
 }
 
 void