Commit 5a2d9587 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[1238] isc::dhcp6::IfaceMgr implemented.

- Contrary to its name, it now covers both IPv4 and IPv6
- Missing delAddress() method implemented
-
parent 075e3787
......@@ -79,6 +79,17 @@ IfaceMgr::Iface::getPlainMac() const {
return (tmp.str());
}
bool IfaceMgr::Iface::delAddress(const isc::asiolink::IOAddress& addr) {
for (AddressCollection::iterator a = addrs_.begin();
a!=addrs_.end(); ++a) {
if (*a==addr) {
addrs_.erase(a);
return (true);
}
}
return (false);
}
bool IfaceMgr::Iface::delSocket(uint16_t sockfd) {
list<SocketInfo>::iterator sock = sockets_.begin();
while (sock!=sockets_.end()) {
......
......@@ -35,7 +35,7 @@ namespace dhcp {
class IfaceMgr : public boost::noncopyable {
public:
/// type that defines list of addresses
typedef std::list<isc::asiolink::IOAddress> AddressCollection;
typedef std::vector<isc::asiolink::IOAddress> AddressCollection;
/// maximum MAC address length (Infiniband uses 20 bytes)
static const unsigned int MAX_MAC_LEN = 20;
......
......@@ -392,4 +392,133 @@ TEST_F(IfaceMgrTest, socket4) {
delete ifacemgr;
}
// Test the Iface structure itself
TEST_F(IfaceMgrTest, iface) {
IfaceMgr::Iface* iface = 0;
EXPECT_NO_THROW(
iface = new IfaceMgr::Iface("eth0",1);
);
EXPECT_EQ("eth0", iface->getName());
EXPECT_EQ(1, iface->getIndex());
EXPECT_EQ("eth0/1", iface->getFullName());
// Let's make a copy of this address collection.
IfaceMgr::AddressCollection addrs = iface->getAddresses();
EXPECT_EQ(0, addrs.size());
IOAddress addr1("192.0.2.6");
iface->addAddress(addr1);
addrs = iface->getAddresses();
ASSERT_EQ(1, addrs.size());
EXPECT_EQ("192.0.2.6", addrs.at(0).toText());
// No such address, should return false.
EXPECT_FALSE(iface->delAddress(IOAddress("192.0.8.9")));
// This address is present, delete it!
EXPECT_TRUE(iface->delAddress(IOAddress("192.0.2.6")));
// Not really necessary, previous reference still points to the same
// collection. Let's do it anyway, as test code may serve as example
// usage code as well.
addrs = iface->getAddresses();
EXPECT_EQ(0, addrs.size());
EXPECT_NO_THROW(
delete iface;
);
}
TEST_F(IfaceMgrTest, socketInfo) {
// check that socketinfo for IPv4 socket is functional
IfaceMgr::SocketInfo sock1(7, IOAddress("192.0.2.56"), DHCP4_SERVER_PORT + 7);
EXPECT_EQ(7, sock1.sockfd_);
EXPECT_EQ("192.0.2.56", sock1.addr_.toText());
EXPECT_EQ(AF_INET, sock1.family_);
EXPECT_EQ(DHCP4_SERVER_PORT + 7, sock1.port_);
// check that socketinfo for IPv6 socket is functional
IfaceMgr::SocketInfo sock2(9, IOAddress("2001:db8:1::56"), DHCP4_SERVER_PORT + 9);
EXPECT_EQ(9, sock2.sockfd_);
EXPECT_EQ("2001:db8:1::56", sock2.addr_.toText());
EXPECT_EQ(AF_INET6, sock2.family_);
EXPECT_EQ(DHCP4_SERVER_PORT + 9, sock2.port_);
// now let's test if IfaceMgr handles socket info properly
createLoInterfacesTxt();
NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
IfaceMgr::Iface* loopback = ifacemgr->getIface(LOOPBACK);
ASSERT_TRUE(loopback);
loopback->addSocket(sock1);
loopback->addSocket(sock2);
Pkt6 pkt6(100);
// pkt6 dos not have interface set yet
EXPECT_THROW(
ifacemgr->getSocket(pkt6),
BadValue
);
// try to send over non-existing interface
pkt6.iface_ = "nosuchinterface45";
EXPECT_THROW(
ifacemgr->getSocket(pkt6),
BadValue
);
// this will work
pkt6.iface_ = LOOPBACK;
EXPECT_EQ(9, ifacemgr->getSocket(pkt6));
bool deleted = false;
EXPECT_NO_THROW(
deleted = ifacemgr->getIface(LOOPBACK)->delSocket(9);
);
EXPECT_EQ(true, deleted);
// it should throw again, there's no usable socket anymore
EXPECT_THROW(
ifacemgr->getSocket(pkt6),
Unexpected
);
// repeat for pkt4
Pkt4 pkt4(DHCPDISCOVER, 1);
// pkt4 does not have interface set yet.
EXPECT_THROW(
ifacemgr->getSocket(pkt4),
BadValue
);
// Try to send over non-existing interface.
pkt4.setIface("nosuchinterface45");
EXPECT_THROW(
ifacemgr->getSocket(pkt4),
BadValue
);
// Socket info is set, packet has well defined interface. It should work.
pkt4.setIface(LOOPBACK);
EXPECT_EQ(7, ifacemgr->getSocket(pkt4));
EXPECT_NO_THROW(
ifacemgr->getIface(LOOPBACK)->delSocket(7);
);
// It should throw again, there's no usable socket anymore.
EXPECT_THROW(
ifacemgr->getSocket(pkt4),
Unexpected
);
delete ifacemgr;
}
}
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