Browse Source

[master] Fixed fd/0 unit test issue

    Merged branch 'trac4067'
Thomas Markwalder 9 years ago
parent
commit
9161867dc6

+ 0 - 19
src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc

@@ -76,26 +76,12 @@ public:
             socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea4.sock";
             socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea4.sock";
         }
         }
         reset();
         reset();
-
-        // This is a workaround for odd problems with gtest. gtest does
-        // shady with socket decriptors. In particular, sometimes we
-        // get 0 as descriptor for socket() call. Technically it is valid,
-        // but then gtest closes descriptor 0 and the socket becomes
-        // unusable. This workaround opens up one file decriptor. In case
-        // 0 is available, it will be consumed here.
-        dummy_fd_ = socket(AF_INET, SOCK_DGRAM, 0);
-        if (dummy_fd_ == 0) {
-            std::cout << "Socket descriptor 0 workaround is useful." << std::endl;
-        }
     }
     }
 
 
     /// @brief Destructor
     /// @brief Destructor
     ~CtrlChannelDhcpv4SrvTest() {
     ~CtrlChannelDhcpv4SrvTest() {
         server_.reset();
         server_.reset();
         reset();
         reset();
-
-        // close dummy descriptor
-        close(dummy_fd_);
     };
     };
 
 
     void createUnixChannelServer() {
     void createUnixChannelServer() {
@@ -197,11 +183,6 @@ public:
         client->disconnectFromServer();
         client->disconnectFromServer();
         ASSERT_NO_THROW(server_->receivePacket(0));
         ASSERT_NO_THROW(server_->receivePacket(0));
     }
     }
-
-    /// @brief dummy file descriptor
-    ///
-    /// See ctor for details.
-    int dummy_fd_;
 };
 };
 
 
 TEST_F(CtrlChannelDhcpv4SrvTest, commands) {
 TEST_F(CtrlChannelDhcpv4SrvTest, commands) {

+ 11 - 1
src/lib/dhcp/tests/pkt_filter_test_stub.cc

@@ -13,6 +13,9 @@
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
 #include <config.h>
 #include <config.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
 
 
 #include <dhcp/tests/pkt_filter_test_stub.h>
 #include <dhcp/tests/pkt_filter_test_stub.h>
 
 
@@ -33,7 +36,14 @@ SocketInfo
 PktFilterTestStub::openSocket(Iface&,
 PktFilterTestStub::openSocket(Iface&,
            const isc::asiolink::IOAddress& addr,
            const isc::asiolink::IOAddress& addr,
            const uint16_t port, const bool, const bool) {
            const uint16_t port, const bool, const bool) {
-    return (SocketInfo(addr, port, 0));
+    int fd = open("/dev/null", O_RDONLY);
+    if (fd < 0) {
+        const char* errmsg = strerror(errno);
+        isc_throw(Unexpected,
+                  "PktFilterTestStub: cannot open /dev/null:" << errmsg);
+    }
+
+    return (SocketInfo(addr, port, fd));
 }
 }
 
 
 Pkt4Ptr
 Pkt4Ptr

+ 5 - 3
src/lib/dhcp/tests/pkt_filter_test_stub.h

@@ -47,9 +47,11 @@ public:
 
 
     /// @brief Simulate opening of the socket.
     /// @brief Simulate opening of the socket.
     ///
     ///
-    /// This function simulates opening a primary socket. In reality, it doesn't
-    /// open a socket but the socket descriptor returned in the SocketInfo
-    /// structure is always set to 0.
+    /// This function simulates opening a primary socket. Rather than open
+    /// an actual socket, the stub peforms a read-only open of "/dev/null".
+    /// The fd returned by this open saved as the socket's descriptor in the
+    /// SocketInfo structure.  This way the filter consumes an actual
+    /// descriptor and retains it until its socket is closed.
     ///
     ///
     /// @param iface An interface descriptor.
     /// @param iface An interface descriptor.
     /// @param addr Address on the interface to be used to send packets.
     /// @param addr Address on the interface to be used to send packets.