From: Francis Dupont Date: Fri, 7 Jun 2024 20:43:15 +0000 (+0200) Subject: [#3170] Checkpoint: finished code X-Git-Tag: Kea-2.7.0~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03a5b8df5c6baf3a5464b0bff04eb553dd7dfb85;p=thirdparty%2Fkea.git [#3170] Checkpoint: finished code --- diff --git a/src/lib/eval/eval.dox b/src/lib/eval/eval.dox index df9d9b2e3e..eac02f1785 100644 --- a/src/lib/eval/eval.dox +++ b/src/lib/eval/eval.dox @@ -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. diff --git a/src/lib/eval/tests/context_unittest.cc b/src/lib/eval/tests/context_unittest.cc index 0565e11390..ca2ce3da1e 100644 --- a/src/lib/eval/tests/context_unittest.cc +++ b/src/lib/eval/tests/context_unittest.cc @@ -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 match = + boost::dynamic_pointer_cast(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); +} + } diff --git a/src/lib/eval/token.h b/src/lib/eval/token.h index bfb8572efe..43fe292175 100644 --- a/src/lib/eval/token.h +++ b/src/lib/eval/token.h @@ -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_;