Skip to content

Commit

Permalink
Merge branch 'master' into feature/transport_layer_struc_rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen authored Dec 11, 2024
2 parents 20d40b0 + b98c705 commit d5edde5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 57 deletions.
9 changes: 4 additions & 5 deletions ecal/core/include/ecal/types/ecal_custom_data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ namespace eCAL
**/
class IpAddressV4
{
public:
public:
ECAL_API IpAddressV4(const std::string& ip_address_);
ECAL_API IpAddressV4(const char* ip_address_);

ECAL_API std::string Get() const;

ECAL_API IpAddressV4& operator=(const std::string& ip_string_);
ECAL_API IpAddressV4& operator=(const char* ip_string_);
ECAL_API IpAddressV4& operator=(const char* ip_string_);
ECAL_API operator std::string() const;
ECAL_API bool operator==(const eCAL::Types::IpAddressV4& rhs) const;
ECAL_API friend bool operator==(eCAL::Types::IpAddressV4 lhs, const char* ip_string_);
Expand All @@ -61,8 +61,7 @@ namespace eCAL
ECAL_API friend bool operator==(const std::string& ip_string_, eCAL::Types::IpAddressV4 rhs);

private:
ECAL_API void validateIpString(const std::string& ip_address_);
ECAL_API static void throwException(const std::string& ip_address_ = std::string(""));
ECAL_API void validateIpString(const std::string& ip_address_);

std::string m_ip_address{};
};
Expand All @@ -80,4 +79,4 @@ namespace eCAL
};

}
}
}
32 changes: 9 additions & 23 deletions ecal/core/src/types/ecal_custom_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@
#include <ecal_def.h>

namespace{
const std::array<const std::regex, 3> INVALID_IPV4_ADDRESSES = {
std::regex("((255|[fF][fF])\\.){3}(255|[fF][fF])"), // 255.255.255.255
std::regex("((127|7[fF]).((0|00|000)\\.){2}(1|01|001))"), // 127.0.0.1
std::regex("((0|00|000)\\.){3}(0|00|000)") // 0.0.0.0
};
const std::regex IPV4_DEC_REGEX = std::regex("(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])");
const std::regex IPV4_HEX_REGEX = std::regex("(([0-9a-fA-F]|[0-9a-fA-F][0-9a-fA-F])\\.){3}([0-9a-fA-F]|[0-9a-fA-F][0-9a-fA-F])");
const std::regex IPV4_DEC_REGEX = std::regex("^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$");
const std::regex IPV4_HEX_REGEX = std::regex("^([0-9a-fA-F]{1,2}\\.){3}[0-9a-fA-F]{1,2}$");
}

namespace eCAL
Expand All @@ -53,34 +48,25 @@ namespace eCAL
validateIpString(ip_address_);
}

IpAddressV4::IpAddressV4(const char* ip_address_)
{
validateIpString(ip_address_);
}

void IpAddressV4::validateIpString(const std::string& ip_address_)
{
if ( std::regex_match(ip_address_, IPV4_DEC_REGEX)
|| std::regex_match(ip_address_, IPV4_HEX_REGEX)
)
{
for (const auto& inv_ip_regex : INVALID_IPV4_ADDRESSES)
{
if (std::regex_match(ip_address_, inv_ip_regex))
{
throwException(ip_address_);
return;
}
}

{
m_ip_address = ip_address_;
}
else
{
throwException(ip_address_);
throw std::invalid_argument("[IpAddressV4] No valid IP address: " + ip_address_);
}
}

void IpAddressV4::throwException(const std::string& ip_address_ /*std::string("")*/)
{
throw std::invalid_argument("[IpAddressV4] No valid IP address: " + ip_address_);
}

std::string IpAddressV4::Get() const { return m_ip_address; }
IpAddressV4& IpAddressV4::operator=(const std::string& ip_string_) { this->validateIpString(ip_string_); return *this; }
IpAddressV4& IpAddressV4::operator=(const char* ip_string_) { this->validateIpString(ip_string_); return *this; }
Expand Down
30 changes: 1 addition & 29 deletions ecal/tests/cpp/config_test/src/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,38 +123,10 @@ TEST(core_cpp_config /*unused*/, user_config_death_test /*unused*/)
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "256.0.0.0"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "127.0.0.1"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "255.255.255.255"),
std::invalid_argument);


ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "FFF.FF.FF.FF"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "FF.FF.FF.FF"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "Ff.fF.ff.Ff"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "7f.0.0.1"),
std::invalid_argument);

ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "0.0.0.0"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "00.00.00.00"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "000.000.000.000"),
std::invalid_argument);
ASSERT_THROW(
SetValue(custom_config.transport_layer.udp.network.group, "0.00.000.0"),
std::invalid_argument);
}

TEST(core_cpp_config /*unused*/, config_custom_datatypes_tests /*unused*/)
Expand Down

0 comments on commit d5edde5

Please sign in to comment.