2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 05:27:55 +00:00

[4091] Addressed comments (0x prefix, deciding in constructor)

This commit is contained in:
Francis Dupont 2015-11-05 02:39:28 +01:00
parent 85a118414f
commit a496523c86
3 changed files with 73 additions and 40 deletions

View File

@ -135,33 +135,41 @@ TEST_F(TokenTest, string6) {
TEST_F(TokenTest, hexstring4) { TEST_F(TokenTest, hexstring4) {
TokenPtr empty; TokenPtr empty;
TokenPtr bad; TokenPtr bad;
TokenPtr nodigit;
TokenPtr baddigit;
TokenPtr bell; TokenPtr bell;
TokenPtr foo; TokenPtr foo;
TokenPtr cookie; TokenPtr cookie;
// Store constant empty hexstring "" ("") in the TokenHexString object. // Store constant empty hexstring "" ("") in the TokenHexString object.
ASSERT_NO_THROW(empty.reset(new TokenHexString(""))); ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
// Store bad encoded hexstring "xabc" ("") in the TokenHexString object. // Store bad encoded hexstring "0abc" ("").
ASSERT_NO_THROW(bad.reset(new TokenHexString("xabc"))); ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc")));
// Store hexstring with an odd number of hexdigits "7" ("\a") // Store hexstring with no digits "0x" ("").
ASSERT_NO_THROW(bell.reset(new TokenHexString("7"))); ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x")));
// Store constant hexstring "666f6f" ("foo") in the TokenHexString object. // Store hexstring with a bad hexdigit "0xxabc" ("").
ASSERT_NO_THROW(foo.reset(new TokenHexString("666f6f"))); ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc")));
// Store constant hexstring "63825363" (DHCP_OPTIONS_COOKIE) // Store hexstring with an odd number of hexdigits "0x7" ("\a").
ASSERT_NO_THROW(cookie.reset(new TokenHexString("63825363"))); ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7")));
// Store constant hexstring "0x666f6f" ("foo").
ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f")));
// Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE).
ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363")));
// Make sure that tokens can be evaluated without exceptions. // Make sure that tokens can be evaluated without exceptions.
ASSERT_NO_THROW(empty->evaluate(*pkt4_, values_)); ASSERT_NO_THROW(empty->evaluate(*pkt4_, values_));
ASSERT_NO_THROW(bad->evaluate(*pkt4_, values_)); ASSERT_NO_THROW(bad->evaluate(*pkt4_, values_));
ASSERT_NO_THROW(nodigit->evaluate(*pkt4_, values_));
ASSERT_NO_THROW(baddigit->evaluate(*pkt4_, values_));
ASSERT_NO_THROW(bell->evaluate(*pkt4_, values_)); ASSERT_NO_THROW(bell->evaluate(*pkt4_, values_));
ASSERT_NO_THROW(foo->evaluate(*pkt4_, values_)); ASSERT_NO_THROW(foo->evaluate(*pkt4_, values_));
ASSERT_NO_THROW(cookie->evaluate(*pkt4_, values_)); ASSERT_NO_THROW(cookie->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack. // Check that the evaluation put its value on the values stack.
ASSERT_EQ(5, values_.size()); ASSERT_EQ(7, values_.size());
uint32_t expected = htonl(DHCP_OPTIONS_COOKIE); uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
EXPECT_EQ(4, values_.top().size()); EXPECT_EQ(4, values_.top().size());
EXPECT_EQ(0, std::memcmp(&expected, &values_.top()[0], 4)); EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
values_.pop(); values_.pop();
EXPECT_EQ("foo", values_.top()); EXPECT_EQ("foo", values_.top());
values_.pop(); values_.pop();
@ -170,6 +178,10 @@ TEST_F(TokenTest, hexstring4) {
EXPECT_EQ("", values_.top()); EXPECT_EQ("", values_.top());
values_.pop(); values_.pop();
EXPECT_EQ("", values_.top()); EXPECT_EQ("", values_.top());
values_.pop();
EXPECT_EQ("", values_.top());
values_.pop();
EXPECT_EQ("", values_.top());
} }
// This simple test checks that a TokenHexString, representing a constant // This simple test checks that a TokenHexString, representing a constant
@ -178,33 +190,41 @@ TEST_F(TokenTest, hexstring4) {
TEST_F(TokenTest, hexstring6) { TEST_F(TokenTest, hexstring6) {
TokenPtr empty; TokenPtr empty;
TokenPtr bad; TokenPtr bad;
TokenPtr nodigit;
TokenPtr baddigit;
TokenPtr bell; TokenPtr bell;
TokenPtr foo; TokenPtr foo;
TokenPtr cookie; TokenPtr cookie;
// Store constant empty hexstring "" ("") in the TokenHexString object. // Store constant empty hexstring "" ("") in the TokenHexString object.
ASSERT_NO_THROW(empty.reset(new TokenHexString(""))); ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
// Store bad encoded hexstring "xabc" ("") in the TokenHexString object. // Store bad encoded hexstring "0abc" ("").
ASSERT_NO_THROW(bad.reset(new TokenHexString("xabc"))); ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc")));
// Store hexstring with an odd number of hexdigits "7" ("\a") // Store hexstring with no digits "0x" ("").
ASSERT_NO_THROW(bell.reset(new TokenHexString("7"))); ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x")));
// Store constant hexstring "666f6f" ("foo") in the TokenHexString object. // Store hexstring with a bad hexdigit "0xxabc" ("").
ASSERT_NO_THROW(foo.reset(new TokenHexString("666f6f"))); ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc")));
// Store constant hexstring "63825363" (DHCP_OPTIONS_COOKIE) // Store hexstring with an odd number of hexdigits "0x7" ("\a").
ASSERT_NO_THROW(cookie.reset(new TokenHexString("63825363"))); ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7")));
// Store constant hexstring "0x666f6f" ("foo").
ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f")));
// Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE).
ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363")));
// Make sure that tokens can be evaluated without exceptions. // Make sure that tokens can be evaluated without exceptions.
ASSERT_NO_THROW(empty->evaluate(*pkt6_, values_)); ASSERT_NO_THROW(empty->evaluate(*pkt6_, values_));
ASSERT_NO_THROW(bad->evaluate(*pkt6_, values_)); ASSERT_NO_THROW(bad->evaluate(*pkt6_, values_));
ASSERT_NO_THROW(nodigit->evaluate(*pkt6_, values_));
ASSERT_NO_THROW(baddigit->evaluate(*pkt6_, values_));
ASSERT_NO_THROW(bell->evaluate(*pkt6_, values_)); ASSERT_NO_THROW(bell->evaluate(*pkt6_, values_));
ASSERT_NO_THROW(foo->evaluate(*pkt6_, values_)); ASSERT_NO_THROW(foo->evaluate(*pkt6_, values_));
ASSERT_NO_THROW(cookie->evaluate(*pkt6_, values_)); ASSERT_NO_THROW(cookie->evaluate(*pkt6_, values_));
// Check that the evaluation put its value on the values stack. // Check that the evaluation put its value on the values stack.
ASSERT_EQ(5, values_.size()); ASSERT_EQ(7, values_.size());
uint32_t expected = htonl(DHCP_OPTIONS_COOKIE); uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
EXPECT_EQ(4, values_.top().size()); EXPECT_EQ(4, values_.top().size());
EXPECT_EQ(0, std::memcmp(&expected, &values_.top()[0], 4)); EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
values_.pop(); values_.pop();
EXPECT_EQ("foo", values_.top()); EXPECT_EQ("foo", values_.top());
values_.pop(); values_.pop();
@ -213,6 +233,10 @@ TEST_F(TokenTest, hexstring6) {
EXPECT_EQ("", values_.top()); EXPECT_EQ("", values_.top());
values_.pop(); values_.pop();
EXPECT_EQ("", values_.top()); EXPECT_EQ("", 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 // This test checks if a token representing an option value is able to extract

View File

@ -28,33 +28,43 @@ TokenString::evaluate(const Pkt& /*pkt*/, ValueStack& values) {
values.push(value_); values.push(value_);
} }
void TokenHexString::TokenHexString(const string& str) : value_("") {
TokenHexString::evaluate(const Pkt& /*pkt*/, ValueStack& values) { // Check "0x" or "0x" in front
// Eliminate the empty string case first if ((str.size() < 2) ||
if (repr_.empty()) { (str[0] != '0') ||
values.push(""); ((str[1] != 'x') && (str[1] != 'X'))) {
return; return;
} }
string digits = str.substr(2);
// Eliminate the no digit case first
if (digits.empty()) {
return;
}
// Transform string of hexadecimal digits into binary format // Transform string of hexadecimal digits into binary format
std::vector<uint8_t> binary; vector<uint8_t> binary;
try { try {
// The decodeHex function expects that the string contains an // The decodeHex function expects that the string contains an
// even number of digits. If we don't meet this requirement, // even number of digits. If we don't meet this requirement,
// we have to insert a leading 0. // we have to insert a leading 0.
if ((repr_.length() % 2) != 0) { if ((digits.length() % 2) != 0) {
repr_ = repr_.insert(0, "0"); digits = digits.insert(0, "0");
} }
util::encode::decodeHex(repr_, binary); util::encode::decodeHex(digits, binary);
} catch (...) { } catch (...) {
values.push("");
return; return;
} }
// Convert to a string
std::string chars(binary.size(), '\0'); // Convert to a string (note that binary.size() cannot be 0)
// Note that binary.size() cannot be 0 value_.resize(binary.size());
std::memmove(&chars[0], &binary[0], binary.size()); memmove(&value_[0], &binary[0], binary.size());
}
void
TokenHexString::evaluate(const Pkt& /*pkt*/, ValueStack& values) {
// Literals only push, nothing to pop // Literals only push, nothing to pop
values.push(chars); values.push(value_);
} }
void void

View File

@ -110,10 +110,9 @@ public:
/// Value is set during token construction. /// Value is set during token construction.
/// ///
/// @param str constant string to be represented /// @param str constant string to be represented
/// (must be a string of hexadecimal digits or decoding will fail) /// (must be "0x" or "0X" followed by a string of hexadecimal digits
TokenHexString(const std::string& str) /// or decoding will fail)
:repr_(str){ TokenHexString(const std::string& str);
}
/// @brief Token evaluation (puts value of the constant string on /// @brief Token evaluation (puts value of the constant string on
/// the stack after decoding or an empty string if decoding fails /// the stack after decoding or an empty string if decoding fails
@ -124,7 +123,7 @@ public:
void evaluate(const Pkt& pkt, ValueStack& values); void evaluate(const Pkt& pkt, ValueStack& values);
protected: protected:
std::string repr_; ///< Constant value std::string value_; ///< Constant value
}; };
/// @brief Token that represents a value of an option /// @brief Token that represents a value of an option