Commit 97458d05 authored by hydratim's avatar hydratim

Fixed Bug that I missed in main IPv6 push, added some tests.

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)
parent d5cd9f25
...@@ -52,6 +52,7 @@ public: ...@@ -52,6 +52,7 @@ public:
Ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d); Ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d);
static Ipv4 any(); static Ipv4 any();
static Ipv4 loopback();
std::string toString() const; std::string toString() const;
void toNetwork(in_addr_t*) const; void toNetwork(in_addr_t*) const;
...@@ -67,6 +68,8 @@ public: ...@@ -67,6 +68,8 @@ 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); 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 any();
static Ipv6 loopback();
std::string toString() const; std::string toString() const;
void toNetwork(in6_addr*) const; void toNetwork(in6_addr*) const;
......
...@@ -50,6 +50,12 @@ Ipv4::any() { ...@@ -50,6 +50,12 @@ Ipv4::any() {
return Ipv4(0, 0, 0, 0); return Ipv4(0, 0, 0, 0);
} }
Ipv4
Ipv4::loopback() {
return Ipv4(127, 0, 0, 1);
}
std::string std::string
Ipv4::toString() const { Ipv4::toString() const {
...@@ -86,6 +92,11 @@ Ipv6::any() { ...@@ -86,6 +92,11 @@ Ipv6::any() {
return Ipv6(0, 0, 0, 0, 0, 0, 0, 0); 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 std::string
Ipv6::toString() const { Ipv6::toString() const {
......
...@@ -123,6 +123,60 @@ TEST(listener_test, listener_bind_port_free) { ...@@ -123,6 +123,60 @@ TEST(listener_test, listener_bind_port_free) {
ASSERT_TRUE(true); 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 // Listener should not crash if an additional member is added to the listener class. This test
// is there to prevent regression for PR 303 // is there to prevent regression for PR 303
TEST(listener_test, listener_uses_default) { TEST(listener_test, listener_uses_default) {
......
...@@ -50,29 +50,37 @@ TEST(net_test, address_creation) ...@@ -50,29 +50,37 @@ TEST(net_test, address_creation)
ASSERT_EQ(address5.port(), 8080); ASSERT_EQ(address5.port(), 8080);
Address address6("[::1]:8080"); Address address6("[::1]:8080");
ASSERT_EQ(address6.host(), "[::1]"); ASSERT_EQ(address6.host(), "::1");
ASSERT_EQ(address6.port(), 8080); ASSERT_EQ(address6.port(), 8080);
std::string addr2 = "[::1]"; std::string addr2 = "[::1]";
Address address7(addr2, Port(8080)); Address address7(addr2, Port(8080));
ASSERT_EQ(address7.host(), "[::1]"); ASSERT_EQ(address7.host(), "::1");
ASSERT_EQ(address7.port(), 8080); ASSERT_EQ(address7.port(), 8080);
Address address8(Ipv6(0, 0, 0, 0, 0, 0, 0, 1), 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); ASSERT_EQ(address8.port(), 8080);
Address address9(Ipv6::any(), Port(8080)); Address address9(Ipv6::any(), Port(8080));
ASSERT_EQ(address9.host(), "[::]"); ASSERT_EQ(address9.host(), "::");
ASSERT_EQ(address9.port(), 8080); ASSERT_EQ(address9.port(), 8080);
Address address10("[::]:8080"); Address address10("[::]:8080");
ASSERT_EQ(address10.host(), "[::]"); ASSERT_EQ(address10.host(), "::");
ASSERT_EQ(address10.port(), 8080); ASSERT_EQ(address10.port(), 8080);
Address address11("[2001:0DB8:AABB:CCDD:EEFF:0011:2233:4455]: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); 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) TEST(net_test, invalid_address)
......
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