// Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace isc; using namespace isc::asiolink; using namespace isc::dhcp; namespace { // Name of loopback interface detection const size_t BUF_SIZE = 32; char LOOPBACK[BUF_SIZE] = "lo"; // Ports used during testing 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. const uint32_t TIMEOUT_TOLERANCE = 10000; class NakedIfaceMgr: public IfaceMgr { // "naked" Interface Manager, exposes internal fields public: NakedIfaceMgr() { } IfaceCollection & getIfacesLst() { return ifaces_; } }; // dummy class for now, but this will be expanded when needed class IfaceMgrTest : public ::testing::Test { public: // these are empty for now, but let's keep them around IfaceMgrTest() { } ~IfaceMgrTest() { } }; // We need some known interface to work reliably. Loopback interface // is named lo on Linux and lo0 on BSD boxes. We need to find out // which is available. This is not a real test, but rather a workaround // that will go away when interface detection is implemented. // NOTE: At this stage of development, write access to current directory // during running tests is required. TEST_F(IfaceMgrTest, loDetect) { // poor man's interface detection // it will go away as soon as proper interface detection // is implemented if (if_nametoindex("lo") > 0) { cout << "This is Linux, using lo as loopback." << endl; snprintf(LOOPBACK, BUF_SIZE - 1, "lo"); } else if (if_nametoindex("lo0") > 0) { cout << "This is BSD, using lo0 as loopback." << endl; snprintf(LOOPBACK, BUF_SIZE - 1, "lo0"); } else { cout << "Failed to detect loopback interface. Neither " << "lo nor lo0 worked. I give up." << endl; FAIL(); } } // Uncomment this test to create packet writer. It will // write incoming DHCPv6 packets as C arrays. That is useful // for generating test sequences based on actual traffic // // TODO: this potentially should be moved to a separate tool // #if 0 TEST_F(IfaceMgrTest, dhcp6Sniffer) { // testing socket operation in a portable way is tricky // without interface detection implemented unlink("interfaces.txt"); ofstream interfaces("interfaces.txt", ios::ate); interfaces << "eth0 fe80::21e:8cff:fe9b:7349"; interfaces.close(); NakedIfaceMgr* ifacemgr = new NakedIfaceMgr(); Pkt6* pkt = NULL; int cnt = 0; cout << "---8X-----------------------------------------" << endl; while (true) { pkt = ifacemgr->receive(); cout << "// this code is autogenerated. Do NOT edit." << endl; cout << "// Received " << pkt->data_len_ << " bytes packet:" << endl; cout << "Pkt6 *capture" << cnt++ << "() {" << endl; cout << " Pkt6* pkt;" << endl; cout << " pkt = new Pkt6(" << pkt->data_len_ << ");" << endl; cout << " pkt->remote_port_ = " << pkt-> remote_port_ << ";" << endl; cout << " pkt->remote_addr_ = IOAddress(\"" << pkt->remote_addr_.toText() << "\");" << endl; cout << " pkt->local_port_ = " << pkt-> local_port_ << ";" << endl; cout << " pkt->local_addr_ = IOAddress(\"" << pkt->local_addr_.toText() << "\");" << endl; cout << " pkt->ifindex_ = " << pkt->ifindex_ << ";" << endl; cout << " pkt->iface_ = \"" << pkt->iface_ << "\";" << endl; // TODO it is better to declare statically initialize the array // and then memcpy it to packet. for (int i=0; i< pkt->data_len_; i++) { cout << " pkt->data_[" << i << "]=" << (int)(unsigned char)pkt->data_[i] << "; "; if (!(i%4)) cout << endl; } cout << endl; cout << " return (pkt);" << endl; cout << "}" << endl << endl; delete pkt; } cout << "---8X-----------------------------------------" << endl; // never happens. Infinite loop is infinite delete pkt; delete ifacemgr; } #endif TEST_F(IfaceMgrTest, basic) { // checks that IfaceManager can be instantiated IfaceMgr & ifacemgr = IfaceMgr::instance(); ASSERT_TRUE(&ifacemgr != 0); } TEST_F(IfaceMgrTest, ifaceClass) { // basic tests for Iface inner class IfaceMgr::Iface* iface = new IfaceMgr::Iface("eth5", 7); EXPECT_STREQ("eth5/7", iface->getFullName().c_str()); delete iface; } // TODO: Implement getPlainMac() test as soon as interface detection // is implemented. TEST_F(IfaceMgrTest, getIface) { cout << "Interface checks. Please ignore socket binding errors." << endl; NakedIfaceMgr* ifacemgr = new NakedIfaceMgr(); // interface name, ifindex IfaceMgr::Iface iface1("lo1", 100); IfaceMgr::Iface iface2("eth9", 101); IfaceMgr::Iface iface3("en3", 102); IfaceMgr::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; // note: real interfaces may be detected as well ifacemgr->getIfacesLst().push_back(iface1); ifacemgr->getIfacesLst().push_back(iface2); ifacemgr->getIfacesLst().push_back(iface3); ifacemgr->getIfacesLst().push_back(iface4); cout << "There are " << ifacemgr->getIfacesLst().size() << " interfaces." << endl; for (IfaceMgr::IfaceCollection::iterator iface=ifacemgr->getIfacesLst().begin(); iface != ifacemgr->getIfacesLst().end(); ++iface) { cout << " " << iface->getFullName() << endl; } // check that interface can be retrieved by ifindex IfaceMgr::Iface* tmp = ifacemgr->getIface(102); ASSERT_TRUE(tmp != NULL); EXPECT_EQ("en3", tmp->getName()); EXPECT_EQ(102, tmp->getIndex()); // check that interface can be retrieved by name tmp = ifacemgr->getIface("lo1"); ASSERT_TRUE(tmp != NULL); EXPECT_EQ("lo1", tmp->getName()); EXPECT_EQ(100, tmp->getIndex()); // check that non-existing interfaces are not returned EXPECT_EQ(static_cast(NULL), ifacemgr->getIface("wifi15") ); delete ifacemgr; } TEST_F(IfaceMgrTest, receiveTimeout6) { using namespace boost::posix_time; std::cout << "Testing DHCPv6 packet reception timeouts." << " Test will block for a few seconds when waiting" << " for timeout to occur." << std::endl; boost::scoped_ptr ifacemgr(new NakedIfaceMgr()); // Open socket on the lo interface. IOAddress loAddr("::1"); int socket1 = 0; ASSERT_NO_THROW( socket1 = ifacemgr->openSocket(LOOPBACK, loAddr, 10547) ); // Socket is open if its descriptor is greater than zero. ASSERT_GT(socket1, 0); // Remember when we call receive6(). ptime start_time = microsec_clock::universal_time(); // Call receive with timeout of 1s + 400000us = 1.4s. Pkt6Ptr pkt; ASSERT_NO_THROW(pkt = ifacemgr->receive6(1, 400000)); // Remember when call to receive6() ended. ptime stop_time = microsec_clock::universal_time(); // We did not send a packet to lo interface so we expect that // nothing has been received and timeout has been reached. ASSERT_FALSE(pkt); // Calculate duration of call to receive6(). time_duration duration = stop_time - start_time; // We stop the clock when the call completes so it does not // precisely reflect the receive timeout. However the // uncertainity should be low enough to expect that measured // value is in the range <1.4s; 1.7s>. EXPECT_GE(duration.total_microseconds(), 1400000 - TIMEOUT_TOLERANCE); EXPECT_LE(duration.total_microseconds(), 1700000); // Test timeout shorter than 1s. start_time = microsec_clock::universal_time(); ASSERT_NO_THROW(pkt = ifacemgr->receive6(0, 500000)); stop_time = microsec_clock::universal_time(); ASSERT_FALSE(pkt); duration = stop_time - start_time; // Check if measured duration is within <0.5s; 0.8s>. EXPECT_GE(duration.total_microseconds(), 500000 - TIMEOUT_TOLERANCE); EXPECT_LE(duration.total_microseconds(), 800000); // Test with invalid fractional timeout values. EXPECT_THROW(ifacemgr->receive6(0, 1000000), isc::BadValue); EXPECT_THROW(ifacemgr->receive6(1, 1000010), isc::BadValue); } TEST_F(IfaceMgrTest, receiveTimeout4) { using namespace boost::posix_time; std::cout << "Testing DHCPv6 packet reception timeouts." << " Test will block for a few seconds when waiting" << " for timeout to occur." << std::endl; boost::scoped_ptr ifacemgr(new NakedIfaceMgr()); // Open socket on the lo interface. IOAddress loAddr("127.0.0.1"); int socket1 = 0; ASSERT_NO_THROW( socket1 = ifacemgr->openSocket(LOOPBACK, loAddr, 10067) ); // Socket is open if its descriptor is greater than zero. ASSERT_GT(socket1, 0); Pkt4Ptr pkt; // Remember when we call receive4(). ptime start_time = microsec_clock::universal_time(); // Call receive with timeout of 2s + 300000us = 2.3s. ASSERT_NO_THROW(pkt = ifacemgr->receive4(2, 300000)); // Remember when call to receive4() ended. ptime stop_time = microsec_clock::universal_time(); // We did not send a packet to lo interface so we expect that // nothing has been received and timeout has been reached. ASSERT_FALSE(pkt); // Calculate duration of call to receive4(). time_duration duration = stop_time - start_time; // We stop the clock when the call completes so it does not // precisely reflect the receive timeout. However the // uncertainity should be low enough to expect that measured // value is in the range <2.3s; 2.6s>. EXPECT_GE(duration.total_microseconds(), 2300000 - TIMEOUT_TOLERANCE); EXPECT_LE(duration.total_microseconds(), 2600000); // Test timeout shorter than 1s. start_time = microsec_clock::universal_time(); ASSERT_NO_THROW(pkt = ifacemgr->receive4(0, 400000)); stop_time = microsec_clock::universal_time(); ASSERT_FALSE(pkt); duration = stop_time - start_time; // Check if measured duration is within <0.4s; 0.7s>. EXPECT_GE(duration.total_microseconds(), 400000 - TIMEOUT_TOLERANCE); EXPECT_LE(duration.total_microseconds(), 700000); // Test with invalid fractional timeout values. EXPECT_THROW(ifacemgr->receive6(0, 1000000), isc::BadValue); EXPECT_THROW(ifacemgr->receive6(2, 1000005), isc::BadValue); } TEST_F(IfaceMgrTest, multipleSockets) { boost::scoped_ptr ifacemgr(new NakedIfaceMgr()); // container for initialized socket descriptors std::list init_sockets; // create socket #1 int socket1 = 0; ASSERT_NO_THROW( socket1 = ifacemgr->openSocketFromIface(LOOPBACK, PORT1, AF_INET); ); ASSERT_GT(socket1, 0); init_sockets.push_back(socket1); // create socket #2 IOAddress loAddr("127.0.0.1"); int socket2 = 0; ASSERT_NO_THROW( socket2 = ifacemgr->openSocketFromRemoteAddress(loAddr, PORT2); ); ASSERT_GT(socket2, 0); init_sockets.push_back(socket2); // Get loopback interface. If we don't find one we are unable to run // this test but we don't want to fail. IfaceMgr::Iface* iface_ptr = ifacemgr->getIface(LOOPBACK); if (iface_ptr == NULL) { cout << "Local loopback interface not found. Skipping test. " << endl; return; } // Once sockets have been sucessfully opened, they are supposed to // be on the list. Here we start to test if all expected sockets // are on the list and no other (unexpected) socket is there. IfaceMgr::SocketCollection sockets = iface_ptr->getSockets(); int matched_sockets = 0; for (std::list::iterator init_sockets_it = init_sockets.begin(); init_sockets_it != init_sockets.end(); ++init_sockets_it) { // Set socket descriptors non blocking in order to be able // to call recv() on them without hang. int flags = fcntl(*init_sockets_it, F_GETFL, 0); ASSERT_GE(flags, 0); ASSERT_GE(fcntl(*init_sockets_it, F_SETFL, flags | O_NONBLOCK), 0); // recv() is expected to result in EWOULDBLOCK error on non-blocking // socket in case socket is valid but simply no data are coming in. char buf; recv(*init_sockets_it, &buf, 1, MSG_PEEK); EXPECT_EQ(EWOULDBLOCK, errno); // Apart from the ability to use the socket we want to make // sure that socket on the list is the one that we created. for (IfaceMgr::SocketCollection::const_iterator socket_it = sockets.begin(); socket_it != sockets.end(); ++socket_it) { if (*init_sockets_it == socket_it->sockfd_) { // This socket is the one that we created. ++matched_sockets; break; } } } // all created sockets have been matched if this condition works. EXPECT_EQ(sockets.size(), matched_sockets); // closeSockets() is the other function that we want to test. It // is supposed to close all sockets so as we will not be able to use // them anymore communication. ifacemgr->closeSockets(); // closed sockets are supposed to be removed from the list sockets = iface_ptr->getSockets(); ASSERT_EQ(0, sockets.size()); // We are still in posession of socket descriptors that we created // on the beginning of this test. We can use them to check whether // closeSockets() only removed them from the list or they have been // really closed. for (std::list::const_iterator init_sockets_it = init_sockets.begin(); init_sockets_it != init_sockets.end(); ++init_sockets_it) { // recv() must result in error when using invalid socket. char buf; recv(*init_sockets_it, &buf, 1, MSG_PEEK); // EWOULDBLOCK would mean that socket is valid/open but // simply no data is received so we have to check for // other errors. EXPECT_NE(EWOULDBLOCK, errno); } } TEST_F(IfaceMgrTest, sockets6) { // testing socket operation in a portable way is tricky // without interface detection implemented NakedIfaceMgr* ifacemgr = new NakedIfaceMgr(); IOAddress loAddr("::1"); Pkt6 pkt6(DHCPV6_SOLICIT, 123); pkt6.setIface(LOOPBACK); // bind multicast socket to port 10547 int socket1 = ifacemgr->openSocket(LOOPBACK, loAddr, 10547); EXPECT_GT(socket1, 0); // socket > 0 EXPECT_EQ(socket1, ifacemgr->getSocket(pkt6)); // bind unicast socket to port 10548 int socket2 = ifacemgr->openSocket(LOOPBACK, loAddr, 10548); EXPECT_GT(socket2, 0); // removed code for binding socket twice to the same address/port // as it caused problems on some platforms (e.g. Mac OS X) close(socket1); close(socket2); delete ifacemgr; } TEST_F(IfaceMgrTest, socketsFromIface) { boost::scoped_ptr ifacemgr(new NakedIfaceMgr()); // Open v6 socket on loopback interface and bind to port int socket1 = 0; EXPECT_NO_THROW( socket1 = ifacemgr->openSocketFromIface(LOOPBACK, PORT1, AF_INET6); ); // Socket descriptor must be positive integer EXPECT_GT(socket1, 0); close(socket1); // Open v4 socket on loopback interface and bind to different port int socket2 = 0; EXPECT_NO_THROW( socket2 = ifacemgr->openSocketFromIface(LOOPBACK, PORT2, AF_INET); ); // socket descriptor must be positive integer EXPECT_GT(socket2, 0); close(socket2); } TEST_F(IfaceMgrTest, socketsFromAddress) { boost::scoped_ptr ifacemgr(new NakedIfaceMgr()); // Open v6 socket on loopback interface and bind to port int socket1 = 0; IOAddress loAddr6("::1"); EXPECT_NO_THROW( socket1 = ifacemgr->openSocketFromAddress(loAddr6, PORT1); ); // socket descriptor must be positive integer EXPECT_GT(socket1, 0); close(socket1); // Open v4 socket on loopback interface and bind to different port int socket2 = 0; IOAddress loAddr("127.0.0.1"); EXPECT_NO_THROW( socket2 = ifacemgr->openSocketFromAddress(loAddr, PORT2); ); // socket descriptor must be positive integer EXPECT_GT(socket2, 0); close(socket2); } TEST_F(IfaceMgrTest, socketsFromRemoteAddress) { boost::scoped_ptr ifacemgr(new NakedIfaceMgr()); // Open v6 socket to connect to remote address. // Loopback address is the only one that we know // so let's treat it as remote address. int socket1 = 0; IOAddress loAddr6("::1"); EXPECT_NO_THROW( socket1 = ifacemgr->openSocketFromRemoteAddress(loAddr6, PORT1); ); EXPECT_GT(socket1, 0); close(socket1); // Open v4 socket to connect to remote address. int socket2 = 0; IOAddress loAddr("127.0.0.1"); EXPECT_NO_THROW( socket2 = ifacemgr->openSocketFromRemoteAddress(loAddr, PORT2); ); EXPECT_GT(socket2, 0); close(socket2); // The following test is currently disabled for OSes other than // Linux because interface detection is not implemented on them. // @todo enable this test for all OSes once interface detection // is implemented. #if defined(OS_LINUX) // Open v4 socket to connect to broadcast address. int socket3 = 0; IOAddress bcastAddr("255.255.255.255"); EXPECT_NO_THROW( socket3 = ifacemgr->openSocketFromRemoteAddress(bcastAddr, PORT2); ); EXPECT_GT(socket3, 0); close(socket3); #endif } // TODO: disabled due to other naming on various systems // (lo in Linux, lo0 in BSD systems) TEST_F(IfaceMgrTest, DISABLED_sockets6Mcast) { // 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(LOOPBACK, 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(LOOPBACK, mcastAddr, 10547); EXPECT_GT(socket2, 0); // 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); delete ifacemgr; } TEST_F(IfaceMgrTest, sendReceive6) { // testing socket operation in a portable way is tricky // without interface detection implemented NakedIfaceMgr* ifacemgr = new NakedIfaceMgr(); // let's assume that every supported OS have lo interface IOAddress loAddr("::1"); int socket1 = 0, socket2 = 0; EXPECT_NO_THROW( socket1 = ifacemgr->openSocket(LOOPBACK, loAddr, 10547); socket2 = ifacemgr->openSocket(LOOPBACK, loAddr, 10546); ); EXPECT_GT(socket1, 0); EXPECT_GT(socket2, 0); // prepare dummy payload uint8_t data[128]; for (int i = 0; i < 128; i++) { data[i] = i; } Pkt6Ptr sendPkt = Pkt6Ptr(new Pkt6(data, 128)); sendPkt->repack(); sendPkt->setRemotePort(10547); sendPkt->setRemoteAddr(IOAddress("::1")); sendPkt->setIndex(1); sendPkt->setIface(LOOPBACK); Pkt6Ptr rcvPkt; EXPECT_EQ(true, ifacemgr->send(sendPkt)); rcvPkt = ifacemgr->receive6(10); ASSERT_TRUE(rcvPkt); // received our own packet // let's check that we received what was sent ASSERT_EQ(sendPkt->getData().size(), rcvPkt->getData().size()); EXPECT_EQ(0, memcmp(&sendPkt->getData()[0], &rcvPkt->getData()[0], rcvPkt->getData().size())); EXPECT_EQ(sendPkt->getRemoteAddr().toText(), rcvPkt->getRemoteAddr().toText()); // since we opened 2 sockets on the same interface and none of them is multicast, // none is preferred over the other for sending data, so we really should not // assume the one or the other will always be choosen for sending data. Therefore // we should accept both values as source ports. EXPECT_TRUE((rcvPkt->getRemotePort() == 10546) || (rcvPkt->getRemotePort() == 10547)); delete ifacemgr; } TEST_F(IfaceMgrTest, sendReceive4) { // testing socket operation in a portable way is tricky // without interface detection implemented NakedIfaceMgr* ifacemgr = new NakedIfaceMgr(); // let's assume that every supported OS have lo interface IOAddress loAddr("127.0.0.1"); int socket1 = 0, socket2 = 0; EXPECT_NO_THROW( socket1 = ifacemgr->openSocket(LOOPBACK, loAddr, DHCP4_SERVER_PORT + 10000); socket2 = ifacemgr->openSocket(LOOPBACK, loAddr, DHCP4_SERVER_PORT + 10000 + 1); ); EXPECT_GE(socket1, 0); EXPECT_GE(socket2, 0); boost::shared_ptr sendPkt(new Pkt4(DHCPDISCOVER, 1234) ); sendPkt->setLocalAddr(IOAddress("127.0.0.1")); sendPkt->setLocalPort(DHCP4_SERVER_PORT + 10000 + 1); sendPkt->setRemotePort(DHCP4_SERVER_PORT + 10000); sendPkt->setRemoteAddr(IOAddress("127.0.0.1")); sendPkt->setIndex(1); sendPkt->setIface(string(LOOPBACK)); sendPkt->setHops(6); sendPkt->setSecs(42); sendPkt->setCiaddr(IOAddress("192.0.2.1")); sendPkt->setSiaddr(IOAddress("192.0.2.2")); sendPkt->setYiaddr(IOAddress("192.0.2.3")); sendPkt->setGiaddr(IOAddress("192.0.2.4")); // unpack() now checks if mandatory DHCP_MESSAGE_TYPE is present boost::shared_ptr