]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[527-check-return-value-of-multi-index-push_back] Addressed comments
authorFrancis Dupont <fdupont@isc.org>
Fri, 21 Jun 2019 10:56:45 +0000 (12:56 +0200)
committerFrancis Dupont <fdupont@isc.org>
Fri, 21 Jun 2019 11:02:53 +0000 (13:02 +0200)
Added shared network getSubnet by prefix

Fixed and completed subnet duplicate unit tests

src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc
src/lib/dhcp/libdhcp++.cc
src/lib/dhcpsrv/shared_network.cc
src/lib/dhcpsrv/shared_network.h
src/lib/dhcpsrv/tests/shared_network_unittest.cc

index 251e2e1c4c45f13d832548f18a90335e17f07c8f..7a3c4a5aacee414fb5bb66779272de6cca0ec60c 100644 (file)
@@ -367,12 +367,9 @@ MySqlConfigBackendImpl::getOptionDefs(const int index,
             last_def->setServerTag(out_bindings[10]->getString());
 
             // Store created option definition.
-            auto ret = option_defs.push_back(last_def);
-            if (!ret.second) {
-                // option_defs is a multi-index container so push_back()
-                // can in theory fails. Now it has no unique indexes...
-                isc_throw(Unexpected, "can't store the option definition");
-            }
+            // (option_defs is a multi-index container with no unique
+            //  indexes so push_back can't fail).
+            static_cast<void>(option_defs.push_back(last_def));
         }
     });
 }
index 2cd286ae7703b975b9b6bc33beb2e51e0d64254c..a55313e618bbd54f88bc016314483a4324f3d503 100644 (file)
@@ -1024,11 +1024,8 @@ void initOptionSpace(OptionDefContainerPtr& defs,
             throw;
         }
 
-        auto ret = defs->push_back(definition);
-        if (!ret.second) {
-            // defs points to a multi index container with only not unique
-            // indexes so this is for sanity only.
-            isc_throw(isc::Unexpected, "can't store option definition");
-        }
+        // option_defs is a multi-index container with no unique indexes
+        // so push_back can't fail).
+        static_cast<void>(defs->push_back(definition));
     }
 }
index 8f760892c328edeaf3ff399ace0a95f469188251..d4a21535f59f0e60404717306d7f02b2023e7ab5 100644 (file)
@@ -54,6 +54,9 @@ public:
         if (getSubnet<SubnetPtrType>(subnets, subnet->getID())) {
             isc_throw(DuplicateSubnetID, "attempted to add subnet with a"
                       " duplicated subnet identifier " << subnet->getID());
+        } else if (getSubnet<SubnetPtrType>(subnets, subnet->toText())) {
+            isc_throw(DuplicateSubnetID, "attempted to add subnet with a"
+                      " duplicated subnet pre3fix " << subnet->toText());
         }
 
         // Check if the subnet is already associated with some network.
@@ -165,6 +168,31 @@ public:
         return (SubnetPtrType());
     }
 
+    /// @brief Returns a subnet belonging to this network for a given subnet
+    /// prefix.
+    ///
+    /// @param subnets Container holding subnets for this shared network.
+    /// @param subnet_prefix Prefix of a subnet being retrieved.
+    ///
+    /// @tparam SubnetPtrType Type of a pointer to a subnet, i.e. Subnet4Ptr
+    /// or @ref Subnet6Ptr.
+    /// @tparam SubnetCollectionType Type of a container holding subnets, i.e.
+    /// @ref Subnet4Collection or @ref Subnet6Collection.
+    ///
+    /// @return Pointer to the subnet or null if the subnet doesn't exist.
+    template<typename SubnetPtrType, typename SubnetCollectionType>
+    static SubnetPtrType getSubnet(const SubnetCollectionType& subnets,
+                                   const std::string& subnet_prefix) {
+        const auto& index = subnets.template get<SubnetPrefixIndexTag>();
+        auto subnet_it = index.find(subnet_prefix);
+        if (subnet_it != index.cend()) {
+            return (*subnet_it);
+        }
+
+        // Subnet not found.
+        return (SubnetPtrType());
+    }
+
     /// @brief Retrieves next available subnet within shared network.
     ///
     /// This method returns next available subnet within a shared network.
index 2c450cbcdeeecda23efabb84ee3e52a8fb7b8a49..2c132daf970c66ca541fbdd189c70337c2c84bef 100644 (file)
@@ -133,6 +133,14 @@ public:
     /// if such subnet doesn't exist within shared network.
     Subnet4Ptr getSubnet(const SubnetID& subnet_id) const;
 
+    /// @brief Returns a subnet for a specified subnet prefix.
+    ///
+    /// @param subnet_prefix Subnet prefix.
+    ///
+    /// @return Shared pointer to a subnet using this prefix or null pointer
+    /// if such subnet doesn't exist within shared network.
+    Subnet4Ptr getSubnet(const std::string& subnet_prefix) const;
+
     /// @brief Retrieves next available IPv4 subnet within shared network.
     ///
     /// See documentation for @ref SharedNetwork4::getNextSubnet.
index ad1a756525c3abb83d56ef8e4397b16a14751aec..f068c8675c210b7fa9ffc6fdbc0e5ba7f32a79bf 100644 (file)
@@ -112,7 +112,13 @@ TEST(SharedNetwork4Test, addSubnet4) {
     // same ID should cause an error.
     Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.0"), 24, 10, 20, 30,
                                    SubnetID(15)));
-    ASSERT_THROW(network->add(subnet), DuplicateSubnetID);
+    ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
+
+    // Create another subnet with the same prefix. Adding a network with the
+    // same prefix should cause an error.
+    subnet2.reset(new Subnet4(IOAddress("10.0.0.0"), 8, 10, 20, 30,
+                              SubnetID(1234)));
+    ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
 
     // Create another network and try to add a subnet to it. It should fail
     // because the subnet is already associated with the first network.
@@ -719,7 +725,13 @@ TEST(SharedNetwork6Test, addSubnet6) {
     // same ID should cause an error.
     Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 16, 10, 20, 30, 40,
                                    SubnetID(15)));
-    ASSERT_THROW(network->add(subnet), DuplicateSubnetID);
+    ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
+
+    // Create another subnet with the same prefix. Adding a network with the
+    // same prefix should cause an error.
+    subnet2.reset(new Subnet6(IOAddress("2001:db8:1::"), 64, 10, 20, 30, 40,
+                              SubnetID(1234)));
+    ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
 
     // Create another network and try to add a subnet to it. It should fail
     // because the subnet is already associated with the first network.