From: Tomek Mrugalski Date: Thu, 18 Aug 2011 17:36:31 +0000 (+0200) Subject: [878] Added test for joinMcast() and other improvements in IfaceMgr tests X-Git-Tag: perftcpdns_before_epoll~37^2~21^2~25 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=21bac503aa78b1d0cbb6993edc083fbc508dad16;p=thirdparty%2Fkea.git [878] Added test for joinMcast() and other improvements in IfaceMgr tests --- diff --git a/src/bin/dhcp6/iface_mgr.cc b/src/bin/dhcp6/iface_mgr.cc index da50ba4463..45cafe21c1 100644 --- a/src/bin/dhcp6/iface_mgr.cc +++ b/src/bin/dhcp6/iface_mgr.cc @@ -162,7 +162,7 @@ IfaceMgr::openSockets() { ++addr) { sock = openSocket(iface->name_, *addr, - DHCP6_SERVER_PORT, false); + DHCP6_SERVER_PORT); if (sock<0) { cout << "Failed to open unicast socket." << endl; return (false); @@ -171,7 +171,7 @@ IfaceMgr::openSockets() { sock = openSocket(iface->name_, IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS), - DHCP6_SERVER_PORT, true); + DHCP6_SERVER_PORT); if (sock<0) { cout << "Failed to open multicast socket." << endl; close(sendsock_); @@ -239,8 +239,7 @@ IfaceMgr::getIface(const std::string& ifname) { int IfaceMgr::openSocket(const std::string& ifname, const IOAddress& addr, - int port, - bool mcast) { + int port) { struct sockaddr_storage name; int name_len; struct sockaddr_in6 *addr6; @@ -309,10 +308,10 @@ IfaceMgr::openSocket(const std::string& ifname, // multicast stuff - if (mcast /*addr.multicast()*/) { + if (addr.getAddress().to_v6().is_multicast()) { // both mcast (ALL_DHCP_RELAY_AGENTS_AND_SERVERS and ALL_DHCP_SERVERS) - // are link and site-scoped, so there is no sense to join those them - // with global addressed. + // are link and site-scoped, so there is no sense to join those groups + // with global addresses. if ( !joinMcast( sock, ifname, string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) { diff --git a/src/bin/dhcp6/iface_mgr.h b/src/bin/dhcp6/iface_mgr.h index c251fdca9c..39061dafc1 100644 --- a/src/bin/dhcp6/iface_mgr.h +++ b/src/bin/dhcp6/iface_mgr.h @@ -72,7 +72,7 @@ namespace isc { int openSocket(const std::string& ifname, const isc::asiolink::IOAddress& addr, - int port, bool multicast); + int port); // TODO: having 2 maps (ifindex->iface and ifname->iface would) // probably be better for performance reasons diff --git a/src/bin/dhcp6/tests/iface_mgr_unittest.cc b/src/bin/dhcp6/tests/iface_mgr_unittest.cc index 0f7f431224..483ac1d82d 100644 --- a/src/bin/dhcp6/tests/iface_mgr_unittest.cc +++ b/src/bin/dhcp6/tests/iface_mgr_unittest.cc @@ -39,8 +39,8 @@ public: int openSocket(const std::string& ifname, const isc::asiolink::IOAddress& addr, - int port, bool multicast) { - return IfaceMgr::openSocket(ifname, addr, port, multicast); + int port) { + return IfaceMgr::openSocket(ifname, addr, port); } }; @@ -70,6 +70,8 @@ TEST_F(IfaceMgrTest, ifaceClass) { } +// TODO: Implement getPlainMac() test as soon as interface detection is implemented. + TEST_F(IfaceMgrTest, getIface) { cout << "Interface checks. Please ignore socket binding errors." << endl; @@ -146,14 +148,49 @@ TEST_F(IfaceMgrTest, sockets) { IOAddress loAddr("::1"); // bind multicast socket to port 10547 - int socket1 = ifacemgr->openSocket("lo", loAddr, 10547, true); + int socket1 = ifacemgr->openSocket("lo", loAddr, 10547); EXPECT_GT(socket1, 0); // socket > 0 // bind unicast socket to port 10548 - int socket2 = ifacemgr->openSocket("lo", loAddr, 10548, false); + int socket2 = ifacemgr->openSocket("lo", loAddr, 10548); + EXPECT_GT(socket2, 0); + + // expect success. This address/port is already bound, but + // we are using SO_REUSEADDR, so we can bind it twice + int socket3 = ifacemgr->openSocket("lo", loAddr, 10547); + EXPECT_GT(socket3, 0); // socket > 0 + + // we now have 3 sockets open at the same time. Looks good. + + close(socket1); + close(socket2); + close(socket3); + + delete ifacemgr; +} + +TEST_F(IfaceMgrTest, socketsMcast) { + // testing socket operation in a portable way is tricky + // without interface detection implemented + + NakedIfaceMgr * ifacemgr = new NakedIfaceMgr(); + + IOAddress loAddr("::1"); + IOAddress mcastAddr("ff02::1:2"); + + // bind multicast socket to port 10547 + int socket1 = ifacemgr->openSocket("lo", mcastAddr, 10547); + EXPECT_GT(socket1, 0); // socket > 0 + + // expect success. This address/port is already bound, but + // we are using SO_REUSEADDR, so we can bind it twice + int socket2 = ifacemgr->openSocket("lo", mcastAddr, 10547); EXPECT_GT(socket2, 0); - // good to check that both sockets can be opened at once + // there's no good way to test negative case here. + // we would need non-multicast interface. We will be able + // to iterate thru available interfaces and check if there + // are interfaces without multicast-capable flag. close(socket1); close(socket2); @@ -173,8 +210,8 @@ TEST_F(IfaceMgrTest, sendReceive) { // let's assume that every supported OS have lo interface IOAddress loAddr("::1"); - int socket1 = ifacemgr->openSocket("lo", loAddr, 10547, true); - int socket2 = ifacemgr->openSocket("lo", loAddr, 10546, false); + int socket1 = ifacemgr->openSocket("lo", loAddr, 10547); + int socket2 = ifacemgr->openSocket("lo", loAddr, 10546); ifacemgr->setSendSock(socket2); ifacemgr->setRecvSock(socket1);