]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[67-expressions-hexa-strings] Added hexstring tests
authorFrancis Dupont <fdupont@isc.org>
Sun, 16 Sep 2018 11:54:50 +0000 (13:54 +0200)
committerFrancis Dupont <fdupont@isc.org>
Sun, 16 Sep 2018 11:54:50 +0000 (13:54 +0200)
src/lib/eval/tests/context_unittest.cc
src/lib/eval/tests/evaluate_unittest.cc
src/lib/eval/tests/token_unittest.cc
src/lib/eval/token.cc

index f40df5e0d0f6839410bb41e4d0c038785831de41..58ee2db223c04b6e37670a1befb0560a1ebb1479 100644 (file)
@@ -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<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) {
@@ -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) {
index 5afb635c91aebbd6325ea12fbcc9a06dd172605b..144141166d386f33352fb1e7695e8263c6072ee2 100644 (file)
@@ -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");
 }
 
 };
index f74c13d3b5b7e22fe605201a1399bbe8e75e7050..69191211e1726ed69270957f11bebdbbf356cda3 100644 (file)
@@ -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) {
index e223aa0b6d347952a23d4e4e14f7b1bb97c33880..402782686d6d6b459c982d0e53c1506fcc7903e4 100644 (file)
@@ -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<unsigned>(binary[i]);
+        tmp << setw(2) << setfill('0')
+            << (static_cast<unsigned>(binary[i]) & 0xff);
     }
     values.push(tmp.str());