const uint16_t PORT1 = 10547; // V6 socket
const uint16_t PORT2 = 10548; // V4 socket
-// On some systems measured duration of receive6() and
-// receive4() appears to be shorter than select() timeout.
-// called by these functions. This may be the case
-// if different ime resolutions are used by these functions.
-// For such cases we set the tolerance of 0.01s.
+// On some systems measured duration of receive6() and receive4() appears to be
+// shorter than select() timeout. This may be the case if different time
+// resolutions are used by these functions. For such cases we set the
+// tolerance to 0.01s.
const uint32_t TIMEOUT_TOLERANCE = 10000;
+ /// Mock object implementing PktFilter class. It is used by
+ /// IfaceMgrTest::setPacketFilter to verify that IfaceMgr::setPacketFilter
+ /// sets this object as a handler for opening sockets. This dummy
+ /// class simply records that openSocket function was called by
+ /// the IfaceMgr as expected.
+ ///
+ /// @todo This class currently doesn't verify that send/receive functions
+ /// were called. In order to test it, there is a need to supply dummy
+ /// function performing select() on certain sockets. The system select()
+ /// call will fail when dummy socket descriptor is provided and thus
+ /// TestPktFilter::receive will never be called. The appropriate extension
+ /// to IfaceMgr is planned along with implementation of other "Packet
+ /// Filters" such as these supporting Linux Packet Filtering and
+ /// Berkley Packet Filtering.
+ class TestPktFilter : public PktFilter {
+ public:
+
+ /// Constructor
+ TestPktFilter()
+ : open_socket_called_(false) {
+ }
+
+ /// Pretends to open socket. Only records a call to this function.
+ virtual int openSocket(const Iface&,
+ const isc::asiolink::IOAddress&,
+ const uint16_t,
+ const bool,
+ const bool) {
+ open_socket_called_ = true;
+ return (1024);
+ }
+
+ /// Does nothing
+ virtual Pkt4Ptr receive(const Iface&,
+ const SocketInfo&) {
+ return (Pkt4Ptr());
+ }
+
+ /// Does nothing
+ virtual int send(uint16_t, const Pkt4Ptr&) {
+ return (0);
+ }
+
+ /// Holds the information whether openSocket was called on this
+ /// object after its creation.
+ bool open_socket_called_;
+ };
class NakedIfaceMgr: public IfaceMgr {
- // "naked" Interface Manager, exposes internal fields
+ // "Naked" Interface Manager, exposes internal fields
public:
NakedIfaceMgr() { }
IfaceCollection & getIfacesLst() { return ifaces_; }
}
TEST_F(IfaceMgrTest, ifaceClass) {
- // basic tests for Iface inner class
+ // Basic tests for Iface inner class
- IfaceMgr::Iface iface("eth5", 7);
- Iface* iface = new Iface("eth5", 7);
-
- EXPECT_STREQ("eth5/7", iface->getFullName().c_str());
-
- delete iface;
++ Iface iface("eth5", 7);
+ EXPECT_STREQ("eth5/7", iface.getFullName().c_str());
}
// TODO: Implement getPlainMac() test as soon as interface detection
TEST_F(IfaceMgrTest, getIface) {
cout << "Interface checks. Please ignore socket binding errors." << endl;
- NakedIfaceMgr* ifacemgr = new NakedIfaceMgr();
+ scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr());
- // interface name, ifindex
+ // Interface name, ifindex
- IfaceMgr::Iface iface1("lo1", 100);
- IfaceMgr::Iface iface2("eth9", 101);
- IfaceMgr::Iface iface3("en3", 102);
- IfaceMgr::Iface iface4("e1000g4", 103);
+ Iface iface1("lo1", 100);
+ Iface iface2("eth9", 101);
+ Iface iface3("en3", 102);
+ Iface iface4("e1000g4", 103);
cout << "This test assumes that there are less than 100 network interfaces"
<< " in the tested system and there are no lo1, eth9, en3, e1000g4"
<< " or wifi15 interfaces present." << endl;
}
- // check that interface can be retrieved by ifindex
+ // Check that interface can be retrieved by ifindex
- IfaceMgr::Iface* tmp = ifacemgr->getIface(102);
+ Iface* tmp = ifacemgr->getIface(102);
ASSERT_TRUE(tmp != NULL);
EXPECT_EQ("en3", tmp->getName());
// Test the Iface structure itself
TEST_F(IfaceMgrTest, iface) {
- scoped_ptr<IfaceMgr::Iface> iface;
- EXPECT_NO_THROW(iface.reset(new IfaceMgr::Iface("eth0",1)));
- Iface* iface = NULL;
- EXPECT_NO_THROW(
- iface = new Iface("eth0",1);
- );
++ boost::scoped_ptr<Iface> iface;
++ EXPECT_NO_THROW(iface.reset(new Iface("eth0",1)));
EXPECT_EQ("eth0", iface->getName());
EXPECT_EQ(1, iface->getIndex());
TEST_F(IfaceMgrTest, socketInfo) {
- // check that socketinfo for IPv4 socket is functional
+ // Check that socketinfo for IPv4 socket is functional
- IfaceMgr::SocketInfo sock1(7, IOAddress("192.0.2.56"), DHCP4_SERVER_PORT + 7);
+ 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
+ // Check that socketinfo for IPv6 socket is functional
- IfaceMgr::SocketInfo sock2(9, IOAddress("2001:db8:1::56"), DHCP4_SERVER_PORT + 9);
+ 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
- NakedIfaceMgr* ifacemgr = new NakedIfaceMgr();
+ // Now let's test if IfaceMgr handles socket info properly
+ scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr());
- IfaceMgr::Iface* loopback = ifacemgr->getIface(LOOPBACK);
+ Iface* loopback = ifacemgr->getIface(LOOPBACK);
ASSERT_TRUE(loopback);
loopback->addSocket(sock1);
loopback->addSocket(sock2);
EXPECT_EQ(detected->getAddresses().size(), i->getAddresses().size());
- // now compare addresses
+ // Now compare addresses
- const IfaceMgr::AddressCollection& addrs = detected->getAddresses();
- for (IfaceMgr::AddressCollection::const_iterator addr = addrs.begin();
+ const Iface::AddressCollection& addrs = detected->getAddresses();
+ for (Iface::AddressCollection::const_iterator addr = addrs.begin();
addr != addrs.end(); ++addr) {
bool addr_found = false;