Browse Source

[1397] Add std::string version of Rset::addRdata()

Mukund Sivaraman 11 years ago
parent
commit
6db6928823
3 changed files with 34 additions and 0 deletions
  1. 5 0
      src/lib/dns/rrset.cc
  2. 17 0
      src/lib/dns/rrset.h
  3. 12 0
      src/lib/dns/tests/rrset_unittest.cc

+ 5 - 0
src/lib/dns/rrset.cc

@@ -194,6 +194,11 @@ BasicRRset::addRdata(const Rdata& rdata) {
     AbstractRRset::addRdata(rdata);
 }
 
+void
+BasicRRset::addRdata(const std::string& rdata_str) {
+    addRdata(createRdata(getType(), getClass(), rdata_str));
+}
+
 unsigned int
 BasicRRset::getRdataCount() const {
     return (impl_->rdatalist_.size());

+ 17 - 0
src/lib/dns/rrset.h

@@ -380,6 +380,16 @@ public:
     /// object, a copy of which is to be added to the \c RRset.
     virtual void addRdata(const rdata::Rdata& rdata) = 0;
 
+    /// \brief Add an RDATA to the RRset (string version).
+    ///
+    /// This method constructs an Rdata object from the the given
+    /// \c rdata_str in presentation format and adds it to the \c RRset.
+    ///
+    /// \param rdata_str RDATA string in presentation format.
+    /// \throw InvalidRdataText if the \c rdata_str is invalid for this
+    /// \c RRset.
+    virtual void addRdata(const std::string& rdata_str) = 0;
+
     /// \brief Return an iterator to go through all RDATA stored in the
     /// \c RRset.
     ///
@@ -727,6 +737,13 @@ public:
     /// See \c AbstractRRset::addRdata(const rdata::Rdata&).
     virtual void addRdata(const rdata::Rdata& rdata);
 
+    /// \brief Add an RDATA to the RRset (string version).
+    ///
+    /// \param rdata_str RDATA string in presentation format.
+    /// \throw InvalidRdataText if the \c rdata_str is invalid for this
+    /// \c RRset.
+    virtual void addRdata(const std::string& rdata_str);
+
     /// \brief Return an iterator to go through all RDATA stored in the
     /// \c BasicRRset.
     ///

+ 12 - 0
src/lib/dns/tests/rrset_unittest.cc

@@ -166,6 +166,18 @@ TEST_F(RRsetTest, addRdataPtr) {
     EXPECT_EQ(3, rrset_a_empty.getRdataCount());
 }
 
+TEST_F(RRsetTest, addRdataString) {
+    rrset_a_empty.addRdata("192.0.2.1");
+    rrset_a_empty.addRdata("192.0.2.2");
+
+    addRdataTestCommon(rrset_a_empty);
+
+    // String version of addRdata() will throw for bad RDATA for
+    // RRType::A().
+    EXPECT_THROW(rrset_a_empty.addRdata("ns.example.com."), InvalidRdataText);
+    addRdataTestCommon(rrset_a_empty);
+}
+
 TEST_F(RRsetTest, iterator) {
     // Iterator for an empty RRset.
     RdataIteratorPtr it = rrset_a_empty.getRdataIterator();