Browse Source

[878] Added test for joinMcast() and other improvements in IfaceMgr tests

Tomek Mrugalski 13 years ago
parent
commit
21bac503aa

+ 6 - 7
src/bin/dhcp6/iface_mgr.cc

@@ -162,7 +162,7 @@ IfaceMgr::openSockets() {
              ++addr) {
              ++addr) {
 
 
             sock = openSocket(iface->name_, *addr,
             sock = openSocket(iface->name_, *addr,
-                              DHCP6_SERVER_PORT, false);
+                              DHCP6_SERVER_PORT);
             if (sock<0) {
             if (sock<0) {
                 cout << "Failed to open unicast socket." << endl;
                 cout << "Failed to open unicast socket." << endl;
                 return (false);
                 return (false);
@@ -171,7 +171,7 @@ IfaceMgr::openSockets() {
 
 
             sock = openSocket(iface->name_,
             sock = openSocket(iface->name_,
                               IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS),
                               IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS),
-                              DHCP6_SERVER_PORT, true);
+                              DHCP6_SERVER_PORT);
             if (sock<0) {
             if (sock<0) {
                 cout << "Failed to open multicast socket." << endl;
                 cout << "Failed to open multicast socket." << endl;
                 close(sendsock_);
                 close(sendsock_);
@@ -239,8 +239,7 @@ IfaceMgr::getIface(const std::string& ifname) {
 int
 int
 IfaceMgr::openSocket(const std::string& ifname,
 IfaceMgr::openSocket(const std::string& ifname,
                      const IOAddress& addr,
                      const IOAddress& addr,
-                     int port,
-                     bool mcast) {
+                     int port) {
     struct sockaddr_storage name;
     struct sockaddr_storage name;
     int name_len;
     int name_len;
     struct sockaddr_in6 *addr6;
     struct sockaddr_in6 *addr6;
@@ -309,10 +308,10 @@ IfaceMgr::openSocket(const std::string& ifname,
 
 
     // multicast stuff
     // 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)
         // 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,
         if ( !joinMcast( sock, ifname,
                          string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) {
                          string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) {

+ 1 - 1
src/bin/dhcp6/iface_mgr.h

@@ -72,7 +72,7 @@ namespace isc {
 
 
         int openSocket(const std::string& ifname,
         int openSocket(const std::string& ifname,
                        const isc::asiolink::IOAddress& addr,
                        const isc::asiolink::IOAddress& addr,
-                       int port, bool multicast);
+                       int port);
 
 
         // TODO: having 2 maps (ifindex->iface and ifname->iface would)
         // TODO: having 2 maps (ifindex->iface and ifname->iface would)
         //      probably be better for performance reasons
         //      probably be better for performance reasons

+ 44 - 7
src/bin/dhcp6/tests/iface_mgr_unittest.cc

@@ -39,8 +39,8 @@ public:
 
 
     int openSocket(const std::string& ifname,
     int openSocket(const std::string& ifname,
                    const isc::asiolink::IOAddress& addr,
                    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) {
 TEST_F(IfaceMgrTest, getIface) {
 
 
     cout << "Interface checks. Please ignore socket binding errors." << endl;
     cout << "Interface checks. Please ignore socket binding errors." << endl;
@@ -146,14 +148,49 @@ TEST_F(IfaceMgrTest, sockets) {
     IOAddress loAddr("::1");
     IOAddress loAddr("::1");
 
 
     // bind multicast socket to port 10547
     // 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
     EXPECT_GT(socket1, 0); // socket > 0
 
 
     // bind unicast socket to port 10548
     // 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);
     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(socket1);
     close(socket2);
     close(socket2);
@@ -173,8 +210,8 @@ TEST_F(IfaceMgrTest, sendReceive) {
 
 
     // let's assume that every supported OS have lo interface
     // let's assume that every supported OS have lo interface
     IOAddress loAddr("::1");
     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->setSendSock(socket2);
     ifacemgr->setRecvSock(socket1);
     ifacemgr->setRecvSock(socket1);