EXPECT_TRUE(alt);
}
+ /// @brief checks if the given token is a hexstring operator
+ void checkTokenToHexString(const TokenPtr& token) {
+ ASSERT_TRUE(token);
+ boost::shared_ptr<TokenToHexString> tohex =
+ boost::dynamic_pointer_cast<TokenToHexString>(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) {
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) {
"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");
}
};
-// 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
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) {
for (size_t i = 0; i < binary.size(); ++i) {
if (!first) {
tmp << separator;
+ } else {
first = false;
}
- tmp << setw(2) << setfill('0') << static_cast<unsigned>(binary[i]);
+ tmp << setw(2) << setfill('0')
+ << (static_cast<unsigned>(binary[i]) & 0xff);
}
values.push(tmp.str());