]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#651,!384] Fix input IAID in postgresql lease6 fetchers
authorThomas Markwalder <tmark@isc.org>
Fri, 14 Jun 2019 19:47:33 +0000 (15:47 -0400)
committerThomas Markwalder <tmark@isc.org>
Fri, 28 Jun 2019 16:16:02 +0000 (12:16 -0400)
src/lib/dhcpsrv/pgsql_lease_mgr.cc
    PgSqlLease6Exchange::Uiaid - made this public and added function to
    return the int32 bit value as a string

    PgSqlLeaseMgr::getLeases6() - uses Uiaid.dbInputString() now

src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.*
    GenericLeaseMgrTest::testLease6LargeIaidCheck() - new test to
    ensure large value iaids can be stored.

src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc
    TEST_F(PgSqlLeaseMgrTest, leases6LargeIaidCheck) - new test

src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc
    TEST_F(CqlLeaseMgrTest, lease6InvalidHostname) - new test

src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc
    TEST_F(MySqlLeaseMgrTest, leases6LargeIaidCheck) - new test

src/lib/dhcpsrv/pgsql_lease_mgr.cc
src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc
src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc
src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h
src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc
src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc

index f49f6f4360e2f6828e534461a8efb579325e129e..26ff0c1c7be62ef5f618e04c902ab5e25512c7c7 100644 (file)
@@ -614,6 +614,31 @@ private:
     static const size_t LEASE_COLUMNS = 17;
 
 public:
+
+    /// @brief Union for marshalling IAID into and out of the database
+    /// IAID is defined in the RFC as 4 octets, which Kea code handles as
+    /// a uint32_t.  Postgresql however, offers only signed integer types
+    /// of sizes 2, 4, and 8 bytes (SMALLINT, INT, and BIGINT respectively).
+    /// IAID is used in several indexes so rather than use the BIGINT, we
+    /// use this union to safely move the value into and out of an INT column.
+    union Uiaid {
+        /// @brief Constructor
+        /// @param val unsigned 32 bit value for the IAID.
+        Uiaid(uint32_t val) : uval_(val){};
+
+        /// @brief Constructor
+        /// @param val signed 32 bit value for the IAID.
+        Uiaid(int32_t val) : ival_(val){};
+
+        /// @brief Return a string representing the signed 32-bit value.
+        std::string dbInputString() {
+            return (boost::lexical_cast<std::string>(ival_));
+        };
+
+        uint32_t uval_;
+        int32_t ival_;
+    };
+
     PgSqlLease6Exchange()
         : lease_(), duid_length_(0), duid_(), iaid_u_(0), iaid_str_(""),
           lease_type_(Lease6::TYPE_NA), lease_type_str_(""), prefix_len_(0),
@@ -691,7 +716,7 @@ public:
             // lexically cast from an integer version to avoid out of range
             // exception failure upon insert.
             iaid_u_.uval_ = lease_->iaid_;
-            iaid_str_ = boost::lexical_cast<std::string>(iaid_u_.ival_);
+            iaid_str_ = iaid_u_.dbInputString();
             bind_array.add(iaid_str_);
 
             prefix_len_str_ = boost::lexical_cast<std::string>
@@ -889,20 +914,7 @@ private:
     size_t                 duid_length_;
     vector<uint8_t>        duid_;
     uint8_t                duid_buffer_[DUID::MAX_DUID_LEN];
-
-    /// @brief Union for marshalling IAID into and out of the database
-    /// IAID is defined in the RFC as 4 octets, which Kea code handles as
-    /// a uint32_t.  Postgresql however, offers only signed integer types
-    /// of sizes 2, 4, and 8 bytes (SMALLINT, INT, and BIGINT respectively).
-    /// IAID is used in several indexes so rather than use the BIGINT, we
-    /// use this union to safely move the value into and out of an INT column.
-    union Uiaid {
-        Uiaid(uint32_t val) : uval_(val){};
-        Uiaid(int32_t val) : ival_(val){};
-        uint32_t uval_;
-        int32_t ival_;
-    }                      iaid_u_;
-
+    union Uiaid            iaid_u_;
     std::string            iaid_str_;
     Lease6::Type           lease_type_;
     std::string            lease_type_str_;
@@ -1454,7 +1466,7 @@ PgSqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID& duid,
     bind_array.add(duid.getDuid());
 
     // IAID
-    std::string iaid_str = boost::lexical_cast<std::string>(iaid);
+    std::string iaid_str = PgSqlLease6Exchange::Uiaid(iaid).dbInputString();
     bind_array.add(iaid_str);
 
     // LEASE_TYPE
@@ -1486,7 +1498,7 @@ PgSqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID& duid,
     bind_array.add(duid.getDuid());
 
     // IAID
-    std::string iaid_str = boost::lexical_cast<std::string>(iaid);
+    std::string iaid_str = PgSqlLease6Exchange::Uiaid(iaid).dbInputString();
     bind_array.add(iaid_str);
 
     // SUBNET ID
index b3ee6623d47ec015d4f4135b8b35bd0686670855..8c31f811d0ec8649b824fe4f90210d3d0acf9225 100644 (file)
@@ -656,6 +656,14 @@ TEST_F(CqlLeaseMgrTest, lease6InvalidHostname) {
     testLease6InvalidHostname();
 }
 
+/// @brief Verify that large IAID values work correctly.
+///
+/// Adds lease with a large IAID to the database and verifies it can
+/// fetched correclty.
+TEST_F(CqlLeaseMgrTest, leases6LargeIaidCheck) {
+    testLease6LargeIaidCheck();
+}
+
 /// @brief Check GetLease6 methods - access by DUID/IAID
 ///
 /// Adds leases to the database and checks that they can be accessed via
index 30c0c6a3dc90fccaf71a594d7dbeca92c69a22f8..bc01cbb30ba6aa0b5144ba34d2291f60a0c1727f 100644 (file)
@@ -1567,6 +1567,39 @@ GenericLeaseMgrTest::testLease6LeaseTypeCheck() {
     }
 }
 
+void
+GenericLeaseMgrTest::testLease6LargeIaidCheck() {
+
+    DuidPtr duid(new DUID(vector<uint8_t>(8, 0x77)));
+    IOAddress addr(std::string("2001:db8:1::111"));
+    SubnetID subnet_id = 8; // radom number
+
+    // Use a value we know is larger than 32-bit signed max.
+    uint32_t large_iaid = 0xFFFFFFFE;
+
+    // We should be able to add with no problems.
+    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid, large_iaid,
+                               100, 200, 0, 0, subnet_id));
+    ASSERT_TRUE(lmptr_->addLease(lease));
+
+    // Sanity check that we added it.
+    Lease6Ptr found_lease = lmptr_->getLease6(Lease::TYPE_NA, addr);
+    ASSERT_TRUE(found_lease);
+    EXPECT_TRUE(*found_lease == *lease);
+
+    // Verify getLease6() duid/iaid finds it.
+    found_lease = lmptr_->getLease6(Lease::TYPE_NA, *duid, large_iaid, subnet_id);
+    ASSERT_TRUE(found_lease);
+    EXPECT_TRUE(*found_lease == *lease);
+
+    // Verify getLeases6() duid/iaid finds it.
+    Lease6Collection found_leases = lmptr_->getLeases6(Lease::TYPE_NA,
+                                                       *duid, large_iaid);
+    // We should match the lease.
+    ASSERT_EQ(1, found_leases.size());
+    EXPECT_TRUE(*(found_leases[0]) == *lease);
+}
+
 void
 GenericLeaseMgrTest::testGetLease6DuidIaidSubnetId() {
     // Get the leases to be used for the test and add them to the database.
index d9228c367ba0f836d7a18b3aa4f0f26a203dd005..a86632b440926d739ffda2b977fd4811c6183220 100644 (file)
@@ -210,6 +210,9 @@ public:
     /// @brief Test method which returns all IPv6 leases for Subnet ID.
     void testGetLeases6SubnetId();
 
+    /// @brief Test making/fetching leases with IAIDs > signed 32-bit max.
+    void testLease6LargeIaidCheck();
+
     /// @brief Test method which returns all IPv6 leases.
     void testGetLeases6();
 
@@ -272,7 +275,7 @@ public:
     /// Adds leases to the database and checks that they can be accessed via
     /// a combination of DIUID and IAID.
     void testGetLease6DuidIaidSubnetId();
-    
+
     /// @brief verifies getLeases6 method by DUID
     ///
     /// Adds 3 leases to backend and retrieves, verifes empty
index e0b2125b0da0143c080b1f2b2b92995eee7367fb..72a8ba8ef75c4934127775bcdb2ed612dc4b6e8b 100644 (file)
@@ -440,6 +440,14 @@ TEST_F(MySqlLeaseMgrTest, lease6InvalidHostname) {
     testLease6InvalidHostname();
 }
 
+/// @brief Verify that large IAID values work correctly.
+///
+/// Adds lease with a large IAID to the database and verifies it can
+/// fetched correclty.
+TEST_F(MySqlLeaseMgrTest, leases6LargeIaidCheck) {
+    testLease6LargeIaidCheck();
+}
+
 /// @brief Check GetLease6 methods - access by DUID/IAID
 ///
 /// Adds leases to the database and checks that they can be accessed via
index 6298e9dbd23884b410aa3bd4822ee3aa4ce81d37..ec783594b879fa57b4cbbc16fb701d871cb59ec7 100644 (file)
@@ -430,6 +430,14 @@ TEST_F(PgSqlLeaseMgrTest, lease6InvalidHostname) {
     testLease6InvalidHostname();
 }
 
+/// @brief Verify that large IAID values work correctly.
+///
+/// Adds lease with a large IAID to the database and verifies it can
+/// fetched correclty.
+TEST_F(PgSqlLeaseMgrTest, leases6LargeIaidCheck) {
+    testLease6LargeIaidCheck();
+}
+
 /// @brief Check GetLease6 methods - access by DUID/IAID
 ///
 /// Adds leases to the database and checks that they can be accessed via