Unverified Commit b6491159 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #279 from knowledge4igor/tests_and_refactoring_net_utils

Tests and refactoring net utils
parents e7946337 a10b68f0
......@@ -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,22 +104,22 @@ 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;
port_ = static_cast<uint16_t>(port);
}
Error::Error(const char* message)
......
......@@ -17,3 +17,4 @@ pistache_test(cookie_test)
pistache_test(view_test)
pistache_test(http_parsing_test)
pistache_test(http_client_test)
pistache_test(net_test)
\ No newline at end of file
#include "gtest/gtest.h"
#include <pistache/net.h>
#include <stdexcept>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace Pistache;
TEST(net_test, port_creation)
{
Port port1(3000);
ASSERT_FALSE(port1.isReserved());
uint16_t value1 = port1;
ASSERT_EQ(value1, 3000);
Port port2(80);
ASSERT_TRUE(port2.isReserved());
uint16_t value2 = port2;
ASSERT_EQ(value2, 80);
}
TEST(net_test, address_creation)
{
Address address1("127.0.0.1:8080");
ASSERT_EQ(address1.host(), "127.0.0.1");
ASSERT_EQ(address1.port(), 8080);
std::string addr = "127.0.0.1";
Address address2(addr, Port(8080));
ASSERT_EQ(address2.host(), "127.0.0.1");
ASSERT_EQ(address2.port(), 8080);
Address address3(Ipv4(127, 0, 0, 1), Port(8080));
ASSERT_EQ(address3.host(), "127.0.0.1");
ASSERT_EQ(address3.port(), 8080);
Address address4(Ipv4::any(), Port(8080));
ASSERT_EQ(address4.host(), "0.0.0.0");
ASSERT_EQ(address4.port(), 8080);
}
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