Browse Source

[2231] Added sanity check for microsecond timeout.

Marcin Siodelski 12 years ago
parent
commit
b0414cf54b
3 changed files with 26 additions and 5 deletions
  1. 10 1
      src/lib/dhcp/iface_mgr.cc
  2. 2 0
      src/lib/dhcp/iface_mgr.h
  3. 14 4
      src/lib/dhcp/tests/iface_mgr_unittest.cc

+ 10 - 1
src/lib/dhcp/iface_mgr.cc

@@ -786,7 +786,11 @@ IfaceMgr::send(const Pkt4Ptr& pkt)
 
 
 boost::shared_ptr<Pkt4>
 boost::shared_ptr<Pkt4>
 IfaceMgr::receive4(uint32_t timeout_sec, uint32_t timeout_usec) {
 IfaceMgr::receive4(uint32_t timeout_sec, uint32_t timeout_usec) {
-
+    // Sanity check for microsecond timeout.
+    if (timeout_usec >= 1000000) {
+        isc_throw(BadValue, "fractional timeout must be shorter than"
+                  " one million microseconds");
+    }
     const SocketInfo* candidate = 0;
     const SocketInfo* candidate = 0;
     IfaceCollection::const_iterator iface;
     IfaceCollection::const_iterator iface;
     fd_set sockets;
     fd_set sockets;
@@ -953,6 +957,11 @@ IfaceMgr::receive4(uint32_t timeout_sec, uint32_t timeout_usec) {
 }
 }
 
 
 Pkt6Ptr IfaceMgr::receive6(uint32_t timeout_sec, uint32_t timeout_usec) {
 Pkt6Ptr IfaceMgr::receive6(uint32_t timeout_sec, uint32_t timeout_usec) {
+    // Sanity check for microsecond timeout.
+    if (timeout_usec >= 1000000) {
+        isc_throw(BadValue, "fractional timeout must be shorter than"
+                  " one million microseconds");
+    }
 
 
     const SocketInfo* candidate = 0;
     const SocketInfo* candidate = 0;
     fd_set sockets;
     fd_set sockets;

+ 2 - 0
src/lib/dhcp/iface_mgr.h

@@ -349,6 +349,7 @@ public:
     /// @param timeout_usec specifies fractional part of the timeout
     /// @param timeout_usec specifies fractional part of the timeout
     /// (in microseconds)
     /// (in microseconds)
     ///
     ///
+    /// @throw isc::BadValue if timeout_usec is greater than one million
     /// @return Pkt6 object representing received packet (or NULL)
     /// @return Pkt6 object representing received packet (or NULL)
     Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec = 0);
     Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec = 0);
 
 
@@ -362,6 +363,7 @@ public:
     /// @param timeout_usec specifies fractional part of the timeout
     /// @param timeout_usec specifies fractional part of the timeout
     /// (in microseconds)
     /// (in microseconds)
     ///
     ///
+    /// @throw isc::BadValue if timeout_usec is greater than one million
     /// @return Pkt4 object representing received packet (or NULL)
     /// @return Pkt4 object representing received packet (or NULL)
     Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec = 0);
     Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec = 0);
 
 

+ 14 - 4
src/lib/dhcp/tests/iface_mgr_unittest.cc

@@ -240,7 +240,8 @@ TEST_F(IfaceMgrTest, receiveTimeout6) {
     // Remember when we call receive6().
     // Remember when we call receive6().
     gettimeofday(&start_time, NULL);
     gettimeofday(&start_time, NULL);
     // Call receive with timeout of 1s + 1000us.
     // Call receive with timeout of 1s + 1000us.
-    Pkt6Ptr pkt = ifacemgr->receive6(1, 1000);
+    Pkt6Ptr pkt;
+    ASSERT_NO_THROW(pkt = ifacemgr->receive6(1, 1000));
     // Remember when call to receive6() ended.
     // Remember when call to receive6() ended.
     gettimeofday(&stop_time, NULL);
     gettimeofday(&stop_time, NULL);
     // We did not send a packet to lo interface so we expect that
     // We did not send a packet to lo interface so we expect that
@@ -256,7 +257,7 @@ TEST_F(IfaceMgrTest, receiveTimeout6) {
 
 
     // Test timeout shorter than 1s.
     // Test timeout shorter than 1s.
     gettimeofday(&start_time, NULL);
     gettimeofday(&start_time, NULL);
-    pkt = ifacemgr->receive6(0, 500);
+    ASSERT_NO_THROW(pkt = ifacemgr->receive6(0, 500));
     gettimeofday(&stop_time, NULL);
     gettimeofday(&stop_time, NULL);
     ASSERT_FALSE(pkt);
     ASSERT_FALSE(pkt);
     stop_time.tv_sec -= start_time.tv_sec;
     stop_time.tv_sec -= start_time.tv_sec;
@@ -268,6 +269,10 @@ TEST_F(IfaceMgrTest, receiveTimeout6) {
     // should be investigated.
     // should be investigated.
     EXPECT_EQ(0, stop_time.tv_sec);
     EXPECT_EQ(0, stop_time.tv_sec);
     EXPECT_GT(stop_time.tv_usec, 500);
     EXPECT_GT(stop_time.tv_usec, 500);
+
+    // 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) {
 TEST_F(IfaceMgrTest, receiveTimeout4) {
@@ -293,7 +298,8 @@ TEST_F(IfaceMgrTest, receiveTimeout4) {
     // Remember when we call receive4().
     // Remember when we call receive4().
     gettimeofday(&start_time, NULL);
     gettimeofday(&start_time, NULL);
     // Call receive with timeout of 2s + 150us.
     // Call receive with timeout of 2s + 150us.
-    Pkt4Ptr pkt = ifacemgr->receive4(2, 150);
+    Pkt4Ptr pkt;
+    ASSERT_NO_THROW(pkt = ifacemgr->receive4(2, 150));
     // Remember when call to receive4() ended.
     // Remember when call to receive4() ended.
     gettimeofday(&stop_time, NULL);
     gettimeofday(&stop_time, NULL);
     // We did not send a packet to lo interface so we expect that
     // We did not send a packet to lo interface so we expect that
@@ -309,7 +315,7 @@ TEST_F(IfaceMgrTest, receiveTimeout4) {
 
 
     // Test timeout shorter than 1s.
     // Test timeout shorter than 1s.
     gettimeofday(&start_time, NULL);
     gettimeofday(&start_time, NULL);
-    pkt = ifacemgr->receive4(0, 350);
+    ASSERT_NO_THROW(pkt = ifacemgr->receive4(0, 350));
     gettimeofday(&stop_time, NULL);
     gettimeofday(&stop_time, NULL);
     ASSERT_FALSE(pkt);
     ASSERT_FALSE(pkt);
     stop_time.tv_sec -= start_time.tv_sec;
     stop_time.tv_sec -= start_time.tv_sec;
@@ -321,6 +327,10 @@ TEST_F(IfaceMgrTest, receiveTimeout4) {
     // should be investigated.
     // should be investigated.
     EXPECT_EQ(0, stop_time.tv_sec);
     EXPECT_EQ(0, stop_time.tv_sec);
     EXPECT_GT(stop_time.tv_usec, 350);
     EXPECT_GT(stop_time.tv_usec, 350);
+
+    // 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, sockets6) {
 TEST_F(IfaceMgrTest, sockets6) {