Browse Source

[2320] Pkt4::delOption() implemented.

Tomek Mrugalski 12 years ago
parent
commit
09e8ac90d9
3 changed files with 21 additions and 0 deletions
  1. 10 0
      src/lib/dhcp/pkt4.cc
  2. 5 0
      src/lib/dhcp/pkt4.h
  3. 6 0
      src/lib/dhcp/tests/pkt4_unittest.cc

+ 10 - 0
src/lib/dhcp/pkt4.cc

@@ -369,6 +369,16 @@ Pkt4::getOption(uint8_t type) {
     return boost::shared_ptr<isc::dhcp::Option>(); // NULL
 }
 
+bool
+Pkt4::delOption(uint8_t type) {
+    isc::dhcp::Option::OptionCollection::iterator x = options_.find(type);
+    if (x!=options_.end()) {
+        options_.erase(x);
+        return (true); // delete successful
+    }
+    return (false); // can't find option to be deleted
+}
+
 void
 Pkt4::updateTimestamp() {
     timestamp_ = boost::posix_time::microsec_clock::universal_time();

+ 5 - 0
src/lib/dhcp/pkt4.h

@@ -325,6 +325,11 @@ public:
     boost::shared_ptr<Option>
     getOption(uint8_t opt_type);
 
+    /// @brief Deletes specified option
+    /// @param type option type to be deleted
+    /// @return true if anything was deleted, false otherwise
+    bool delOption(uint8_t type);
+
     /// @brief Returns interface name.
     ///
     /// Returns interface name over which packet was received or is

+ 6 - 0
src/lib/dhcp/tests/pkt4_unittest.cc

@@ -530,6 +530,12 @@ TEST(Pkt4Test, options) {
     EXPECT_EQ(0, memcmp(ptr, v4Opts, sizeof(v4Opts)));
     EXPECT_EQ(DHO_END, static_cast<uint8_t>(*(ptr + sizeof(v4Opts))));
 
+    // delOption() checks
+    EXPECT_TRUE(pkt->getOption(12)); // Sanity check: option 12 is still there
+    EXPECT_TRUE(pkt->delOption(12)); // We should be able to remove it
+    EXPECT_FALSE(pkt->getOption(12)); // It should not be there anymore
+    EXPECT_FALSE(pkt->delOption(12)); // And removal should fail
+
     EXPECT_NO_THROW(
         delete pkt;
     );