From: Francis Dupont Date: Sun, 16 Sep 2018 11:54:50 +0000 (+0200) Subject: [67-expressions-hexa-strings] Added hexstring tests X-Git-Tag: 137-improve-kea-compilation-time-2_base~8^2~1^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=de2de738bc2076df481c2c8c16c5dd00a6e00099;p=thirdparty%2Fkea.git [67-expressions-hexa-strings] Added hexstring tests --- diff --git a/src/lib/eval/tests/context_unittest.cc b/src/lib/eval/tests/context_unittest.cc index f40df5e0d0..58ee2db223 100644 --- a/src/lib/eval/tests/context_unittest.cc +++ b/src/lib/eval/tests/context_unittest.cc @@ -462,6 +462,14 @@ public: EXPECT_TRUE(alt); } + /// @brief checks if the given token is a hexstring operator + void checkTokenToHexString(const TokenPtr& token) { + ASSERT_TRUE(token); + boost::shared_ptr tohex = + boost::dynamic_pointer_cast(token); + EXPECT_TRUE(tohex); + } + /// @brief checks if the given expression raises the expected message /// when it is parsed. void checkError(const string& expr, const string& msg) { @@ -1316,6 +1324,25 @@ TEST_F(EvalContextTest, ifElse) { checkTokenIfElse(tmp4); } +// Test the parsing of a hexstring expression +TEST_F(EvalContextTest, toHexString) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("hexstring(0x666f,'-') == '66-6f'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(5, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + + checkTokenHexString(tmp1, "fo"); + checkTokenString(tmp2, "-"); + checkTokenToHexString(tmp3); +} + // // Test some scanner error cases TEST_F(EvalContextTest, scanErrors) { diff --git a/src/lib/eval/tests/evaluate_unittest.cc b/src/lib/eval/tests/evaluate_unittest.cc index 5afb635c91..144141166d 100644 --- a/src/lib/eval/tests/evaluate_unittest.cc +++ b/src/lib/eval/tests/evaluate_unittest.cc @@ -504,6 +504,12 @@ TEST_F(ExpressionsTest, evaluateString) { "ifelse(option[100].exists," "option[100].hex,'none?'))", "hundred4"); + + // Check that hexstring works as expecting. + testExpressionString(Option::V4, "hexstring(0x1234,':')", "12:34"); + testExpressionString(Option::V4, "hexstring(0x56789a,'-')", "56-78-9a"); + testExpressionString(Option::V4, "hexstring(0xbcde,'')", "bcde"); + testExpressionString(Option::V4, "hexstring(0xf01234,'foo')", "f01234"); } }; diff --git a/src/lib/eval/tests/token_unittest.cc b/src/lib/eval/tests/token_unittest.cc index f74c13d3b5..69191211e1 100644 --- a/src/lib/eval/tests/token_unittest.cc +++ b/src/lib/eval/tests/token_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -1954,6 +1954,35 @@ TEST_F(TokenTest, concat) { EXPECT_TRUE(checkFile()); } +// This test checks if a token representing a hexstring request +// throws an exception if there aren't enough values on the stack. +// The actual packet is not used. +TEST_F(TokenTest, tohexstring) { + ASSERT_NO_THROW(t_.reset(new TokenToHexString())); + + // Hexstring equires two values on the stack, try + // with 0 and 1 both should throw an exception + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // Two should work + values_.push("-"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // Check the result + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("66-6f-6f", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_TOHEXSTRING Popping binary value 0x666F6F and " + "separator -, pushing result 66-6f-6f"); + EXPECT_TRUE(checkFile()); +} + // This test checks if a token representing an ifelse is able // to select the branch following the condition. TEST_F(TokenTest, ifElse) { diff --git a/src/lib/eval/token.cc b/src/lib/eval/token.cc index e223aa0b6d..402782686d 100644 --- a/src/lib/eval/token.cc +++ b/src/lib/eval/token.cc @@ -654,9 +654,11 @@ TokenToHexString::evaluate(Pkt& /*pkt*/, ValueStack& values) { for (size_t i = 0; i < binary.size(); ++i) { if (!first) { tmp << separator; + } else { first = false; } - tmp << setw(2) << setfill('0') << static_cast(binary[i]); + tmp << setw(2) << setfill('0') + << (static_cast(binary[i]) & 0xff); } values.push(tmp.str());