diff --git a/src/util/newconfig/ConfigConstraints.cpp b/src/util/newconfig/ConfigConstraints.cpp index 66e91771..2a0f8fb6 100644 --- a/src/util/newconfig/ConfigConstraints.cpp +++ b/src/util/newconfig/ConfigConstraints.cpp @@ -22,6 +22,8 @@ #include "util/newconfig/Error.hpp" #include "util/newconfig/Types.hpp" +#include + #include #include #include @@ -68,20 +70,19 @@ ValidIPConstraint::checkTypeImpl(Value const& ip) const std::optional ValidIPConstraint::checkValueImpl(Value const& ip) const { - if (std::get(ip) == "localhost") + boost::system::error_code errorCode; + boost::asio::ip::make_address(std::get(ip), errorCode); + if (not errorCode.failed()) return std::nullopt; - static std::regex const kIPV4( - R"(^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$)" - ); + static std::regex const kHOST{ + R"regex(^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$)regex" + }; - static std::regex const kIP_URL( - R"(^((http|https):\/\/)?((([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6})|(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])))(:\d{1,5})?(\/[^\s]*)?$)" - ); - if (std::regex_match(std::get(ip), kIPV4) || std::regex_match(std::get(ip), kIP_URL)) + if (std::regex_match(std::get(ip), kHOST)) return std::nullopt; - return Error{"Ip is not a valid ip address"}; + return Error{"Ip is not a valid ip address or hostname"}; } std::optional diff --git a/tests/unit/util/newconfig/ConfigValueTests.cpp b/tests/unit/util/newconfig/ConfigValueTests.cpp index ecd54956..2d4f7d76 100644 --- a/tests/unit/util/newconfig/ConfigValueTests.cpp +++ b/tests/unit/util/newconfig/ConfigValueTests.cpp @@ -198,20 +198,24 @@ TEST_F(ConstraintTest, OneOfConstraint) TEST_F(ConstraintTest, IpConstraint) { - auto ip = ConfigValue{ConfigType::String}.defaultValue("127.0.0.1").withConstraint(gValidateIp); - EXPECT_FALSE(ip.setValue("http://127.0.0.1").has_value()); - EXPECT_FALSE(ip.setValue("http://127.0.0.1.com").has_value()); - auto const err = ip.setValue("123.44"); - EXPECT_TRUE(err.has_value()); - EXPECT_EQ(err->error, "Unknown_key Ip is not a valid ip address"); - EXPECT_FALSE(ip.setValue("126.0.0.2")); + auto ip = ConfigValue{ConfigType::String}.withConstraint(gValidateIp); - EXPECT_TRUE(ip.setValue("644.3.3.0")); - EXPECT_TRUE(ip.setValue("127.0.0.1.0")); - EXPECT_TRUE(ip.setValue("")); - EXPECT_TRUE(ip.setValue("http://example..com")); + EXPECT_FALSE(ip.setValue("127.0.0.1")); EXPECT_FALSE(ip.setValue("localhost")); - EXPECT_FALSE(ip.setValue("http://example.com:8080/path")); + EXPECT_FALSE(ip.setValue("some-server.com")); + EXPECT_FALSE(ip.setValue("126.0.0.2")); + EXPECT_FALSE(ip.setValue("valid.host-name.com")); + + EXPECT_TRUE(ip.setValue("http://127.0.0.1")); + EXPECT_TRUE(ip.setValue("")); + EXPECT_TRUE(ip.setValue("example..com")); + EXPECT_TRUE(ip.setValue("example.com:8080/path")); + EXPECT_TRUE(ip.setValue("-invalid.com")); + EXPECT_TRUE(ip.setValue("invalid-.com")); + + auto const err = ip.setValue("extra$@symbols"); + ASSERT_TRUE(err.has_value()); + EXPECT_EQ(err->error, "Unknown_key Ip is not a valid ip address or hostname"); } TEST_F(ConstraintTest, positiveNumConstraint)