regulators: Enhance IDMap to detect duplicate IDs
The IDMap class provides a mapping from unique string IDs to the
corresponding Device, Rail, and Rule objects.
Enhance IDMap to detect duplicate IDs. Throw an exception if the caller
tries to add a Device, Rail, or Rule whose ID already exists in the map.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I81176fae1415cd5a89dc4ff47f80d1bc70e0e004
diff --git a/phosphor-regulators/src/id_map.cpp b/phosphor-regulators/src/id_map.cpp
index d39fa4f..0f7d3ac 100644
--- a/phosphor-regulators/src/id_map.cpp
+++ b/phosphor-regulators/src/id_map.cpp
@@ -25,17 +25,35 @@
void IDMap::addDevice(Device& device)
{
- deviceMap[device.getID()] = &device;
+ const std::string& id = device.getID();
+ if (deviceMap.count(id) != 0)
+ {
+ throw std::invalid_argument{"Unable to add device: Duplicate ID \"" +
+ id + '"'};
+ }
+ deviceMap[id] = &device;
}
void IDMap::addRail(Rail& rail)
{
- railMap[rail.getID()] = &rail;
+ const std::string& id = rail.getID();
+ if (railMap.count(id) != 0)
+ {
+ throw std::invalid_argument{"Unable to add rail: Duplicate ID \"" + id +
+ '"'};
+ }
+ railMap[id] = &rail;
}
void IDMap::addRule(Rule& rule)
{
- ruleMap[rule.getID()] = &rule;
+ const std::string& id = rule.getID();
+ if (ruleMap.count(id) != 0)
+ {
+ throw std::invalid_argument{"Unable to add rule: Duplicate ID \"" + id +
+ '"'};
+ }
+ ruleMap[id] = &rule;
}
} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/id_map.hpp b/phosphor-regulators/src/id_map.hpp
index 289c7a9..c466f23 100644
--- a/phosphor-regulators/src/id_map.hpp
+++ b/phosphor-regulators/src/id_map.hpp
@@ -47,6 +47,8 @@
/**
* Adds the specified device to this IDMap.
*
+ * Throws invalid_argument if the device's ID already exists in the map.
+ *
* @param device device to add
*/
void addDevice(Device& device);
@@ -54,6 +56,8 @@
/**
* Adds the specified rail to this IDMap.
*
+ * Throws invalid_argument if the rail's ID already exists in the map.
+ *
* @param rail rail to add
*/
void addRail(Rail& rail);
@@ -61,6 +65,8 @@
/**
* Adds the specified rule to this IDMap.
*
+ * Throws invalid_argument if the rule's ID already exists in the map.
+ *
* @param rule rule to add
*/
void addRule(Rule& rule);
diff --git a/phosphor-regulators/test/id_map_tests.cpp b/phosphor-regulators/test/id_map_tests.cpp
index d3053d7..991949b 100644
--- a/phosphor-regulators/test/id_map_tests.cpp
+++ b/phosphor-regulators/test/id_map_tests.cpp
@@ -62,6 +62,21 @@
// Verify different device is not in map
EXPECT_THROW(idMap.getDevice("vio_reg2"), std::invalid_argument);
+
+ // Test where device ID already exists in map
+ try
+ {
+ i2cInterface =
+ i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
+ Device device2{"vio_reg", true, "/system/chassis/motherboard/vio_reg2",
+ std::move(i2cInterface)};
+ idMap.addDevice(device2);
+ }
+ catch (const std::invalid_argument& error)
+ {
+ EXPECT_STREQ(error.what(),
+ "Unable to add device: Duplicate ID \"vio_reg\"");
+ }
}
TEST(IDMapTests, AddRail)
@@ -92,6 +107,17 @@
// Verify different rail is not in map
EXPECT_THROW(idMap.getRail("vcs0"), std::invalid_argument);
+
+ // Test where rail ID already exists in map
+ try
+ {
+ Rail rail2{"vio0"};
+ idMap.addRail(rail2);
+ }
+ catch (const std::invalid_argument& error)
+ {
+ EXPECT_STREQ(error.what(), "Unable to add rail: Duplicate ID \"vio0\"");
+ }
}
TEST(IDMapTests, AddRule)
@@ -123,6 +149,18 @@
// Verify different rule is not in map
EXPECT_THROW(idMap.getRule("set_voltage_rule_page0"),
std::invalid_argument);
+
+ // Test where rule ID already exists in map
+ try
+ {
+ Rule rule2{"set_voltage_rule", std::vector<std::unique_ptr<Action>>{}};
+ idMap.addRule(rule2);
+ }
+ catch (const std::invalid_argument& error)
+ {
+ EXPECT_STREQ(error.what(),
+ "Unable to add rule: Duplicate ID \"set_voltage_rule\"");
+ }
}
TEST(IDMapTests, GetDevice)