Browse Source

Merge branch 'trac3287'

Mukund Sivaraman 11 years ago
parent
commit
2f26d78170

+ 17 - 13
src/lib/dns/rdata/generic/sshfp_44.cc

@@ -14,8 +14,6 @@
 
 #include <config.h>
 
-#include <string>
-
 #include <boost/lexical_cast.hpp>
 
 #include <exceptions/exceptions.h>
@@ -38,7 +36,7 @@ using namespace isc::util::encode;
 struct SSHFPImpl {
     // straightforward representation of SSHFP RDATA fields
     SSHFPImpl(uint8_t algorithm, uint8_t fingerprint_type,
-              vector<uint8_t>& fingerprint) :
+              const vector<uint8_t>& fingerprint) :
         algorithm_(algorithm),
         fingerprint_type_(fingerprint_type),
         fingerprint_(fingerprint)
@@ -82,7 +80,11 @@ SSHFP::constructFromLexer(MasterLexer& lexer) {
     // If fingerprint is missing, it's OK. See the API documentation of the
     // constructor.
     if (fingerprint_str.size() > 0) {
-        decodeHex(fingerprint_str, fingerprint);
+        try {
+            decodeHex(fingerprint_str, fingerprint);
+        } catch (const isc::BadValue& e) {
+            isc_throw(InvalidRdataText, "Bad SSHFP fingerprint: " << e.what());
+        }
     }
 
     return (new SSHFPImpl(algorithm, fingerprint_type, fingerprint));
@@ -102,8 +104,9 @@ SSHFP::constructFromLexer(MasterLexer& lexer) {
 /// valid hex encoding of the fingerprint. For compatibility with BIND 9,
 /// whitespace is allowed in the hex text (RFC4255 is silent on the matter).
 ///
-/// \throw InvalidRdataText if any fields are missing, out of their valid
-/// ranges, or incorrect.
+/// \throw InvalidRdataText if any fields are missing, are out of their
+/// valid ranges or are incorrect, or if the fingerprint is not a valid
+/// hex string.
 ///
 /// \param sshfp_str A string containing the RDATA to be created
 SSHFP::SSHFP(const string& sshfp_str) :
@@ -128,9 +131,6 @@ SSHFP::SSHFP(const string& sshfp_str) :
     } catch (const MasterLexer::LexerError& ex) {
         isc_throw(InvalidRdataText, "Failed to construct SSHFP from '" <<
                   sshfp_str << "': " << ex.what());
-    } catch (const isc::BadValue& e) {
-        isc_throw(InvalidRdataText,
-                  "Bad SSHFP fingerprint: " << e.what());
     }
 
     impl_ = impl_ptr.release();
@@ -142,9 +142,8 @@ SSHFP::SSHFP(const string& sshfp_str) :
 /// of an SSHFP RDATA.
 ///
 /// \throw MasterLexer::LexerError General parsing error such as missing field.
-/// \throw InvalidRdataText Fields are out of their valid range, or are
-/// incorrect.
-/// \throw BadValue Fingerprint is not a valid hex string.
+/// \throw InvalidRdataText Fields are out of their valid range or are
+/// incorrect, or if the fingerprint is not a valid hex string.
 ///
 /// \param lexer A \c MasterLexer object parsing a master file for the
 /// RDATA to be created
@@ -293,8 +292,13 @@ SSHFP::getFingerprintType() const {
     return (impl_->fingerprint_type_);
 }
 
+const std::vector<uint8_t>&
+SSHFP::getFingerprint() const {
+    return (impl_->fingerprint_);
+}
+
 size_t
-SSHFP::getFingerprintLen() const {
+SSHFP::getFingerprintLength() const {
     return (impl_->fingerprint_.size());
 }
 

+ 3 - 1
src/lib/dns/rdata/generic/sshfp_44.h

@@ -17,6 +17,7 @@
 #include <stdint.h>
 
 #include <string>
+#include <vector>
 
 #include <dns/name.h>
 #include <dns/rdata.h>
@@ -45,7 +46,8 @@ public:
     ///
     uint8_t getAlgorithmNumber() const;
     uint8_t getFingerprintType() const;
-    size_t getFingerprintLen() const;
+    const std::vector<uint8_t>& getFingerprint() const;
+    size_t getFingerprintLength() const;
 
 private:
     SSHFPImpl* constructFromLexer(MasterLexer& lexer);

+ 20 - 12
src/lib/dns/tests/rdata_sshfp_unittest.cc

@@ -59,11 +59,6 @@ protected:
                 rdata_str, rdata_sshfp, true, true);
     }
 
-    void checkFromText_BadValue(const string& rdata_str) {
-        checkFromText<generic::SSHFP, InvalidRdataText, BadValue>(
-            rdata_str, rdata_sshfp, true, true);
-    }
-
     void checkFromText_BadString(const string& rdata_str) {
         checkFromText
             <generic::SSHFP, InvalidRdataText, isc::Exception>(
@@ -138,8 +133,8 @@ TEST_F(Rdata_SSHFP_Test, badText) {
     checkFromText_LexerError("1");
     checkFromText_LexerError("ONE 2 123456789abcdef67890123456789abcdef67890");
     checkFromText_LexerError("1 TWO 123456789abcdef67890123456789abcdef67890");
-    checkFromText_BadValue("1 2 BUCKLEMYSHOE");
-    checkFromText_BadValue(sshfp_txt + " extra text");
+    checkFromText_InvalidText("1 2 BUCKLEMYSHOE");
+    checkFromText_InvalidText(sshfp_txt + " extra text");
 
     // yes, these are redundant to the last test cases in algorithmTypes
     checkFromText_InvalidText(
@@ -232,7 +227,8 @@ TEST_F(Rdata_SSHFP_Test, toWire) {
     this->obuffer.clear();
     rdata_sshfp.toWire(this->obuffer);
 
-    EXPECT_EQ(22, this->obuffer.getLength());
+    EXPECT_EQ(sizeof (rdata_sshfp_wiredata),
+              this->obuffer.getLength());
 
     EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
                         this->obuffer.getData(),
@@ -254,8 +250,20 @@ TEST_F(Rdata_SSHFP_Test, getFingerprintType) {
     EXPECT_EQ(1, rdata_sshfp.getFingerprintType());
 }
 
-TEST_F(Rdata_SSHFP_Test, getFingerprintLen) {
-    EXPECT_EQ(20, rdata_sshfp.getFingerprintLen());
+TEST_F(Rdata_SSHFP_Test, getFingerprint) {
+    const std::vector<uint8_t>& fingerprint =
+        rdata_sshfp.getFingerprint();
+
+    EXPECT_EQ(rdata_sshfp.getFingerprintLength(),
+              fingerprint.size());
+    for (int i = 0; i < fingerprint.size(); ++i) {
+        EXPECT_EQ(rdata_sshfp_wiredata[i + 2],
+                  fingerprint.at(i));
+    }
+}
+
+TEST_F(Rdata_SSHFP_Test, getFingerprintLength) {
+    EXPECT_EQ(20, rdata_sshfp.getFingerprintLength());
 }
 
 TEST_F(Rdata_SSHFP_Test, emptyFingerprintFromWire) {
@@ -273,7 +281,7 @@ TEST_F(Rdata_SSHFP_Test, emptyFingerprintFromWire) {
 
     EXPECT_EQ(4, rdf.getAlgorithmNumber());
     EXPECT_EQ(9, rdf.getFingerprintType());
-    EXPECT_EQ(0, rdf.getFingerprintLen());
+    EXPECT_EQ(0, rdf.getFingerprintLength());
 
     this->obuffer.clear();
     rdf.toWire(this->obuffer);
@@ -297,7 +305,7 @@ TEST_F(Rdata_SSHFP_Test, emptyFingerprintFromString) {
 
     EXPECT_EQ(5, rdata_sshfp2.getAlgorithmNumber());
     EXPECT_EQ(6, rdata_sshfp2.getFingerprintType());
-    EXPECT_EQ(0, rdata_sshfp2.getFingerprintLen());
+    EXPECT_EQ(0, rdata_sshfp2.getFingerprintLength());
 
     this->obuffer.clear();
     rdata_sshfp2.toWire(this->obuffer);