]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3170] Checkpoint: finished code
authorFrancis Dupont <fdupont@isc.org>
Fri, 7 Jun 2024 20:43:15 +0000 (22:43 +0200)
committerFrancis Dupont <fdupont@isc.org>
Tue, 11 Jun 2024 14:31:51 +0000 (16:31 +0200)
src/lib/eval/eval.dox
src/lib/eval/tests/context_unittest.cc
src/lib/eval/token.h

index df9d9b2e3e502d5d727760e450bd5e130eb4c586..eac02f1785276fe87212f705a4f8ab6acf1a9ef5 100644 (file)
@@ -194,6 +194,7 @@ instantiated with the appropriate value and put onto the expression vector.
    existence, enterprise-id and included data chunks. (e.g.
    vendor-class[1234].exists,
    vendor-class[*].enterprise-id, vendor-class[*].data[3])
+ - isc::dhcp::TokenMatch -- represents a regular expression matching.
 
 More operators are expected to be implemented in upcoming releases.
 
index 0565e11390425eef73406a5b2725384e604924ae..ca2ce3da1e02cecba6189c0d43333709f319aa02 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2015-2024 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
@@ -793,6 +793,20 @@ public:
         EXPECT_EQ(expected_repr, sub->getRepresentation());
     }
 
+    /// @brief checks if the given token is a match with the expected
+    /// regular expression.
+    /// @param token token to be checked
+    /// @param expected_reg_exp expected regular expression.
+    void checkTokenMatch(const TokenPtr& token,
+                         const std::string& expected_reg_exp) {
+        ASSERT_TRUE(token);
+        boost::shared_ptr<TokenMatch> match =
+            boost::dynamic_pointer_cast<TokenMatch>(token);
+        ASSERT_TRUE(match);
+
+        EXPECT_EQ(expected_reg_exp, match->getRegExp());
+    }
+
     Option::Universe universe_; ///< Universe (V4 or V6)
     bool parsed_; ///< Parsing status
 
@@ -2265,4 +2279,33 @@ TEST_F(EvalContextTest, integer1) {
     checkTokenInteger(tmp, 2);
 }
 
+// Checks the parsing of a regular expression matching.
+TEST_F(EvalContextTest, match) {
+    EvalContext eval(Option::V4);
+
+    EXPECT_NO_THROW(parsed_ = eval.parseString("match('foo.*', 'foobar')"));
+    EXPECT_TRUE(parsed_);
+
+    ASSERT_EQ(2, eval.expression.size());
+
+    TokenPtr tmp = eval.expression.at(0);
+    ASSERT_TRUE(tmp);
+    checkTokenString(tmp, "foobar");
+
+    tmp = eval.expression.at(1);
+    checkTokenMatch(tmp, "foo.*");
+
+    Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345));
+    ValueStack values;
+    values.push("foobar");
+
+    EXPECT_NO_THROW(tmp->evaluate(*pkt4, values));
+
+    ASSERT_EQ(1, values.size());
+    EXPECT_EQ("true", values.top());
+
+    // Invalid regular expressions are syntax errors.
+    EXPECT_THROW(eval.parseString("match('[bad', 'foobar')"), EvalParseError);
+}
+
 }
index bfb8572efe7029a1e45a30043b7a8912dde7f5ff..43fe29217598bc9e1453e473ca41e034775a8035 100644 (file)
@@ -1345,6 +1345,16 @@ public:
     /// @throw EvalBadStack if there is no value on the stack
     void evaluate(Pkt& pkt, ValueStack& values);
 
+    /// @brief Returns regular expression.
+    ///
+    /// This method is used in testing to determine if the parser had
+    /// instantiated TokenMatch with correct parameters.
+    ///
+    /// @return regular expression as a string.
+    const std::string& getRegExp() const {
+        return (reg_exp_str_);
+    }
+
 private:
     /// @brief The regular expression as a string.
     std::string reg_exp_str_;