Commit 48f95536 authored by knowledge4igor's avatar knowledge4igor

Improvements in Address

parent ad4af32d
......@@ -35,6 +35,9 @@ public:
bool isReserved() const;
bool isUsed() const;
static constexpr uint16_t min() {
return std::numeric_limits<uint16_t>::min();
}
static constexpr uint16_t max() {
return std::numeric_limits<uint16_t>::max();
}
......@@ -77,7 +80,7 @@ public:
Port port() const;
private:
void init(std::string addr);
void init(const std::string& addr);
std::string host_;
Port port_;
};
......
......@@ -104,21 +104,21 @@ Address::port() const {
}
void
Address::init(std::string addr) {
auto pos = addr.find(':');
Address::init(const std::string& addr) {
const auto pos = addr.find(':');
if (pos == std::string::npos)
throw std::invalid_argument("Invalid address");
std::string host = addr.substr(0, pos);
char *end;
host_ = addr.substr(0, pos);
char *end;
const std::string portPart = addr.substr(pos + 1);
if (portPart.empty())
throw std::invalid_argument("Invalid port");
long port = strtol(portPart.c_str(), &end, 10);
if (*end != 0 || port > Port::max())
if (*end != 0 || port < Port::min() || port > Port::max())
throw std::invalid_argument("Invalid port");
host_ = std::move(host);
port_ = port;
}
......
......@@ -48,4 +48,6 @@ TEST(net_test, invalid_address)
{
ASSERT_THROW(Address("127.0.0.1"), std::invalid_argument);
ASSERT_THROW(Address("127.0.0.1:9999999"), std::invalid_argument);
ASSERT_THROW(Address("127.0.0.1:"), std::invalid_argument);
ASSERT_THROW(Address("127.0.0.1:-10"), std::invalid_argument);
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment