Skip to content

Commit

Permalink
Fixed Bug that I missed in main IPv6 push, added some tests.
Browse files Browse the repository at this point in the history
A silly mistake on my part conflating the use of the toString with presentation formating causing:
::getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs): Name or service not known (<cwd>/src/server/listener.cc:168)
  • Loading branch information
hydratim committed Dec 28, 2018
1 parent d5cd9f2 commit 97458d0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
5 changes: 4 additions & 1 deletion include/pistache/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Ipv4 {
Ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d);

static Ipv4 any();
static Ipv4 loopback();
std::string toString() const;
void toNetwork(in_addr_t*) const;

Expand All @@ -65,8 +66,10 @@ class Ipv4 {
class Ipv6 {
public:
Ipv6(uint16_t a, uint16_t b, uint16_t c, uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h);

static Ipv6 any();
static Ipv6 loopback();

std::string toString() const;
void toNetwork(in6_addr*) const;

Expand Down
11 changes: 11 additions & 0 deletions src/common/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Ipv4::any() {
return Ipv4(0, 0, 0, 0);
}

Ipv4
Ipv4::loopback() {
return Ipv4(127, 0, 0, 1);
}


std::string
Ipv4::toString() const {

Expand Down Expand Up @@ -86,6 +92,11 @@ Ipv6::any() {
return Ipv6(0, 0, 0, 0, 0, 0, 0, 0);
}

Ipv6
Ipv6::loopback() {
return Ipv6(0, 0, 0, 0, 0, 0, 0, 1);
}

std::string
Ipv6::toString() const {

Expand Down
54 changes: 54 additions & 0 deletions tests/listener_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,60 @@ TEST(listener_test, listener_bind_port_free) {
ASSERT_TRUE(true);
}

TEST(listener_test, listener_bind_v4_port) {
uint16_t port_nb;

// This is just done to get the value of a free port. The socket will be closed
// after the closing curly bracket and the port will be free again (SO_REUSEADDR option).
// In theory, it is possible that some application grab this port before we bind it again...
{
SocketWrapper s = bind_free_port();
port_nb = s.port();
}


if (port_nb == 0) {
FAIL() << "Could not find a free port. Abort test.\n";
}

Pistache::Port port(port_nb);
Pistache::Address address(Pistache::Ipv4::loopback(), port);

Pistache::Tcp::Listener listener;
Pistache::Flags<Pistache::Tcp::Options> options;
listener.init(1, options);
listener.setHandler(Pistache::Http::make_handler<DummyHandler>());
listener.bind(address);
ASSERT_TRUE(true);
}

TEST(listener_test, listener_bind_v6_port) {
uint16_t port_nb;

// This is just done to get the value of a free port. The socket will be closed
// after the closing curly bracket and the port will be free again (SO_REUSEADDR option).
// In theory, it is possible that some application grab this port before we bind it again...
{
SocketWrapper s = bind_free_port();
port_nb = s.port();
}


if (port_nb == 0) {
FAIL() << "Could not find a free port. Abort test.\n";
}

Pistache::Port port(port_nb);
Pistache::Address address(Pistache::Ipv6::loopback(), port);

Pistache::Tcp::Listener listener;
Pistache::Flags<Pistache::Tcp::Options> options;
listener.init(1, options);
listener.setHandler(Pistache::Http::make_handler<DummyHandler>());
listener.bind(address);
ASSERT_TRUE(true);
}

// Listener should not crash if an additional member is added to the listener class. This test
// is there to prevent regression for PR 303
TEST(listener_test, listener_uses_default) {
Expand Down
20 changes: 14 additions & 6 deletions tests/net_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,37 @@ TEST(net_test, address_creation)
ASSERT_EQ(address5.port(), 8080);

Address address6("[::1]:8080");
ASSERT_EQ(address6.host(), "[::1]");
ASSERT_EQ(address6.host(), "::1");
ASSERT_EQ(address6.port(), 8080);

std::string addr2 = "[::1]";
Address address7(addr2, Port(8080));
ASSERT_EQ(address7.host(), "[::1]");
ASSERT_EQ(address7.host(), "::1");
ASSERT_EQ(address7.port(), 8080);

Address address8(Ipv6(0, 0, 0, 0, 0, 0, 0, 1), Port(8080));
ASSERT_EQ(address8.host(), "[::1]");
ASSERT_EQ(address8.host(), "::1");
ASSERT_EQ(address8.port(), 8080);

Address address9(Ipv6::any(), Port(8080));
ASSERT_EQ(address9.host(), "[::]");
ASSERT_EQ(address9.host(), "::");
ASSERT_EQ(address9.port(), 8080);

Address address10("[::]:8080");
ASSERT_EQ(address10.host(), "[::]");
ASSERT_EQ(address10.host(), "::");
ASSERT_EQ(address10.port(), 8080);

Address address11("[2001:0DB8:AABB:CCDD:EEFF:0011:2233:4455]:8080");
ASSERT_EQ(address11.host(), "[2001:0DB8:AABB:CCDD:EEFF:0011:2233:4455]");
ASSERT_EQ(address11.host(), "2001:0DB8:AABB:CCDD:EEFF:0011:2233:4455");
ASSERT_EQ(address11.port(), 8080);

Address address12(Ipv4::loopback(), Port(8080));
ASSERT_EQ(address12.host(), "127.0.0.1");
ASSERT_EQ(address12.port(), 8080);

Address address13(Ipv6::loopback(), Port(8080));
ASSERT_EQ(address13.host(), "::1");
ASSERT_EQ(address13.port(), 8080);
}

TEST(net_test, invalid_address)
Expand Down

0 comments on commit 97458d0

Please sign in to comment.