|
@@ -15,14 +15,25 @@
|
|
|
#include <config.h>
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
+#include <sys/types.h>
|
|
|
+#include <sys/socket.h>
|
|
|
+#include <netdb.h>
|
|
|
+#include <string.h>
|
|
|
+
|
|
|
+#include <boost/shared_ptr.hpp>
|
|
|
+
|
|
|
#include <asiolink/io_endpoint.h>
|
|
|
#include <asiolink/io_error.h>
|
|
|
|
|
|
+using boost::shared_ptr;
|
|
|
using namespace isc::asiolink;
|
|
|
|
|
|
+namespace {
|
|
|
+typedef shared_ptr<const IOEndpoint> ConstIOEndpointPtr;
|
|
|
+
|
|
|
TEST(IOEndpointTest, createUDPv4) {
|
|
|
- const IOEndpoint* ep;
|
|
|
- ep = IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"), 53210);
|
|
|
+ ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("192.0.2.1"), 53210));
|
|
|
EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
|
|
|
EXPECT_EQ(53210, ep->getPort());
|
|
|
EXPECT_EQ(AF_INET, ep->getFamily());
|
|
@@ -31,8 +42,8 @@ TEST(IOEndpointTest, createUDPv4) {
|
|
|
}
|
|
|
|
|
|
TEST(IOEndpointTest, createTCPv4) {
|
|
|
- const IOEndpoint* ep;
|
|
|
- ep = IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 5301);
|
|
|
+ ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("192.0.2.1"), 5301));
|
|
|
EXPECT_EQ("192.0.2.1", ep->getAddress().toText());
|
|
|
EXPECT_EQ(5301, ep->getPort());
|
|
|
EXPECT_EQ(AF_INET, ep->getFamily());
|
|
@@ -41,8 +52,9 @@ TEST(IOEndpointTest, createTCPv4) {
|
|
|
}
|
|
|
|
|
|
TEST(IOEndpointTest, createUDPv6) {
|
|
|
- const IOEndpoint* ep;
|
|
|
- ep = IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1234"), 5302);
|
|
|
+ ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1234"),
|
|
|
+ 5302));
|
|
|
EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
|
|
|
EXPECT_EQ(5302, ep->getPort());
|
|
|
EXPECT_EQ(AF_INET6, ep->getFamily());
|
|
@@ -51,8 +63,9 @@ TEST(IOEndpointTest, createUDPv6) {
|
|
|
}
|
|
|
|
|
|
TEST(IOEndpointTest, createTCPv6) {
|
|
|
- const IOEndpoint* ep;
|
|
|
- ep = IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1234"), 5303);
|
|
|
+ ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1234"),
|
|
|
+ 5303));
|
|
|
EXPECT_EQ("2001:db8::1234", ep->getAddress().toText());
|
|
|
EXPECT_EQ(5303, ep->getPort());
|
|
|
EXPECT_EQ(AF_INET6, ep->getFamily());
|
|
@@ -61,23 +74,55 @@ TEST(IOEndpointTest, createTCPv6) {
|
|
|
}
|
|
|
|
|
|
TEST(IOEndpointTest, equality) {
|
|
|
- std::vector<const IOEndpoint *> epv;
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1234"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1234"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1234"), 5304));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1234"), 5304));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1235"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1235"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1235"), 5304));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1235"), 5304));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 5304));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"), 5304));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"), 5303));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"), 5304));
|
|
|
- epv.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"), 5304));
|
|
|
+ std::vector<ConstIOEndpointPtr> epv;
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1234"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1234"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1234"), 5304)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1234"), 5304)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1235"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1235"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1235"), 5304)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1235"), 5304)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("192.0.2.1"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("192.0.2.1"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("192.0.2.1"), 5304)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("192.0.2.1"), 5304)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("192.0.2.2"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("192.0.2.2"), 5303)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("192.0.2.2"), 5304)));
|
|
|
+ epv.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("192.0.2.2"), 5304)));
|
|
|
|
|
|
for (size_t i = 0; i < epv.size(); ++i) {
|
|
|
for (size_t j = 0; j < epv.size(); ++j) {
|
|
@@ -92,23 +137,55 @@ TEST(IOEndpointTest, equality) {
|
|
|
|
|
|
// Create a second array with exactly the same values. We use create()
|
|
|
// again to make sure we get different endpoints
|
|
|
- std::vector<const IOEndpoint *> epv2;
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1234"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1234"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1234"), 5304));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1234"), 5304));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1235"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1235"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("2001:db8::1235"), 5304));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::1235"), 5304));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"), 5304));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"), 5304));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"), 5303));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"), 5304));
|
|
|
- epv2.push_back(IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"), 5304));
|
|
|
+ std::vector<ConstIOEndpointPtr> epv2;
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1234"), 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1234"), 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1234"), 5304)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1234"), 5304)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1235"), 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1235"), 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("2001:db8::1235"), 5304)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("2001:db8::1235"), 5304)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP,
|
|
|
+ IOAddress("192.0.2.1"), 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("192.0.2.1"), 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.1"),
|
|
|
+ 5304)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.1"),
|
|
|
+ 5304)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"),
|
|
|
+ 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"),
|
|
|
+ 5303)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"),
|
|
|
+ 5304)));
|
|
|
+ epv2.push_back(ConstIOEndpointPtr(
|
|
|
+ IOEndpoint::create(IPPROTO_UDP, IOAddress("192.0.2.2"),
|
|
|
+ 5304)));
|
|
|
|
|
|
for (size_t i = 0; i < epv.size(); ++i) {
|
|
|
EXPECT_TRUE(*epv[i] == *epv2[i]);
|
|
@@ -122,3 +199,46 @@ TEST(IOEndpointTest, createIPProto) {
|
|
|
IOError);
|
|
|
}
|
|
|
|
|
|
+void
|
|
|
+sockAddrMatch(const struct sockaddr& actual_sa,
|
|
|
+ const char* const expected_addr_text,
|
|
|
+ const char* const expected_port_text)
|
|
|
+{
|
|
|
+ struct addrinfo hints;
|
|
|
+ memset(&hints, 0, sizeof(hints));
|
|
|
+ hints.ai_family = AF_UNSPEC;
|
|
|
+ hints.ai_socktype = SOCK_DGRAM; // this shouldn't matter
|
|
|
+ hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
|
|
|
+
|
|
|
+ struct addrinfo* res;
|
|
|
+ ASSERT_EQ(0, getaddrinfo(expected_addr_text, expected_port_text, &hints,
|
|
|
+ &res));
|
|
|
+ EXPECT_EQ(res->ai_family, actual_sa.sa_family);
|
|
|
+#ifdef HAVE_SA_LEN
|
|
|
+ // ASIO doesn't seem to set sa_len, so we set it to the expected value
|
|
|
+ res->ai_addr->sa_len = actual_sa.sa_len;
|
|
|
+#endif
|
|
|
+ EXPECT_EQ(0, memcmp(res->ai_addr, &actual_sa, res->ai_addrlen));
|
|
|
+ free(res);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IOEndpointTest, getSockAddr) {
|
|
|
+ // UDP/IPv4
|
|
|
+ ConstIOEndpointPtr ep(IOEndpoint::create(IPPROTO_UDP,
|
|
|
+ IOAddress("192.0.2.1"), 53210));
|
|
|
+ sockAddrMatch(ep->getSockAddr(), "192.0.2.1", "53210");
|
|
|
+
|
|
|
+ // UDP/IPv6
|
|
|
+ ep.reset(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::53"), 53));
|
|
|
+ sockAddrMatch(ep->getSockAddr(), "2001:db8::53", "53");
|
|
|
+
|
|
|
+ // TCP/IPv4
|
|
|
+ ep.reset(IOEndpoint::create(IPPROTO_TCP, IOAddress("192.0.2.2"), 53211));
|
|
|
+ sockAddrMatch(ep->getSockAddr(), "192.0.2.2", "53211");
|
|
|
+
|
|
|
+ // TCP/IPv6
|
|
|
+ ep.reset(IOEndpoint::create(IPPROTO_UDP, IOAddress("2001:db8::5300"), 35));
|
|
|
+ sockAddrMatch(ep->getSockAddr(), "2001:db8::5300", "35");
|
|
|
+}
|
|
|
+
|
|
|
+}
|