Commit 67108ee9 authored by Francis Dupont's avatar Francis Dupont
Browse files

[527-check-return-value-of-multi-index-push_back] Addressed comments

Added shared network getSubnet by prefix

Fixed and completed subnet duplicate unit tests
parent 7866e77b
......@@ -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));
}
});
}
......
......@@ -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));
}
}
......@@ -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.
......
......@@ -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.
......
......@@ -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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment