]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[github93] Changes after review:
authorTomek Mrugalski <tomasz@isc.org>
Thu, 12 Jul 2018 12:48:12 +0000 (14:48 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Thu, 12 Jul 2018 12:48:12 +0000 (14:48 +0200)
 - added missing unit-tests, fixed bugs, small tweaks.

src/lib/dhcp/option6_auth.cc
src/lib/dhcp/option6_auth.h
src/lib/dhcp/tests/option6_auth_unittest.cc
src/lib/util/buffer.h
src/lib/util/io_utilities.h
src/lib/util/tests/buffer_unittest.cc
src/lib/util/tests/io_utilities_unittest.cc

index bf68d5616455518cad6c04a28bb39944527d32a4..04b8b5f930ddf08ba44662a4968235cb2b785e6a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -11,6 +11,7 @@
 #include <dhcp/option_space.h>
 #include <exceptions/exceptions.h>
 #include <util/io_utilities.h>
+#include <util/encode/hex.h>
 
 #include <sstream>
 #include <stdint.h>
@@ -21,9 +22,9 @@ using namespace isc::util;
 namespace isc {
 namespace dhcp {
 
-    Option6Auth::Option6Auth(const uint8_t proto, const uint8_t algo, 
-                             const uint8_t method, const uint64_t rdm, 
-                             const std::vector<uint8_t>& info) 
+    Option6Auth::Option6Auth(const uint8_t proto, const uint8_t algo,
+                             const uint8_t method, const uint64_t rdm,
+                             const std::vector<uint8_t>& info)
     : Option(Option::V6, D6O_AUTH),
       protocol_(proto), algorithm_(algo),
       rdm_method_(method), rdm_value_(rdm),
@@ -32,7 +33,7 @@ namespace dhcp {
 
 OptionPtr
 Option6Auth::clone() const {
-    return (cloneInternal<Option6Auth>());    
+    return (cloneInternal<Option6Auth>());
 }
 
 void
@@ -41,8 +42,8 @@ Option6Auth::pack(isc::util::OutputBuffer& buf) const {
        isc_throw(OutOfRange, "Option " << type_ << "Buffer too small for"
                "packing data");
     }
-    
-    //header = option code + length 
+
+    //header = option code + length
     buf.writeUint16(type_);
     // length = 11 bytes fixed field length+ length of auth information
     buf.writeUint16(11 + uint16_t(auth_info_.size()));
@@ -55,7 +56,7 @@ Option6Auth::pack(isc::util::OutputBuffer& buf) const {
     // replay detection value
     buf.writeUint64( rdm_value_);
     // authentication information for reconfig msg
-    // should have zero 
+    // should have zero
 
     for (auto i : auth_info_) {
         buf.writeUint8(i);
@@ -69,7 +70,7 @@ Option6Auth::packHashInput(isc::util::OutputBuffer& buf) const {
                "computing hash input");
     }
 
-    //header = option code + length 
+    //header = option code + length
     buf.writeUint16(type_);
     // length = 11 bytes fixed field length+ length of auth information
     buf.writeUint16(OPTION6_AUTH_MIN_LEN + OPTION6_HASH_MSG_LEN);
@@ -82,7 +83,7 @@ Option6Auth::packHashInput(isc::util::OutputBuffer& buf) const {
     // replay detection value
     buf.writeUint64(rdm_value_);
     // authentication information for reconfig msg
-    // should have zero 
+    // should have zero
     for (uint8_t i = 0; i < OPTION6_HASH_MSG_LEN; i++) {
         buf.writeUint8(0);
     }
@@ -95,7 +96,7 @@ Option6Auth::unpack(OptionBufferConstIter begin,
    if (distance(begin, end) < Option6Auth::OPTION6_AUTH_MIN_LEN) {
        isc_throw(OutOfRange, "Option " << type_ << " truncated");
    }
+
    protocol_ = *begin;
    begin += sizeof(uint8_t);
 
@@ -113,13 +114,17 @@ Option6Auth::unpack(OptionBufferConstIter begin,
                 {  auth_info_.push_back(msgdata); });
 }
 
-std::string 
+std::string
 Option6Auth::toText(int indent) const {
     stringstream output;
     std::string in(indent, ' '); //base indent
-    
-    output << in << "protocol= " << protocol_ << "algorithm= " << algorithm_ 
-           << "rdm method= " << rdm_method_ << "rdm value= " << rdm_value_ ;
+
+    output << in << "protocol=" << static_cast<int>(protocol_)
+           << ", algorithm=" << static_cast<int>(algorithm_)
+           << ", rdm method=" << static_cast<int>(rdm_method_)
+           << ", rdm value=" << rdm_value_
+           << ", value=" << isc::util::encode::encodeHex(auth_info_);
+
     return output.str();
 }
 
index 7ebfe393f4a04d06a574c01b008f0b66a3697e22..91e5f5025e0b51e27317810106cc7164a5338bbc 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -21,10 +21,12 @@ class Option6Auth;
 /// A pointer to the @c isc::dhcp::Option6Auth object.
 typedef boost::shared_ptr<Option6Auth> Option6AuthPtr;
 
+/// @brief This class represents Authentication (11) DHCPv6 option.
+///
+/// For details, see draft-ietf-rfc3315bis-13, Section 21.11.
 class Option6Auth: public Option {
 
 public:
-    
     static const uint8_t OPTION6_AUTH_MIN_LEN  = 11;
     static const uint8_t OPTION6_HASH_MSG_LEN  = 16;
     static const uint8_t OPTION6_HDR = 4; 
@@ -55,7 +57,6 @@ public:
     /// @param buf buffer (option will be stored here)
     void packHashInput(isc::util::OutputBuffer& buf) const;
 
-    /// @brief Parses received buffer
     /// @brief Parses received buffer
     ///
     /// Parses received buffer and returns offset to the first unused byte after
@@ -85,12 +86,12 @@ public:
     /// Set replay detection method type
     ///
     /// @param method replay detection method to be set
-    void setRplyDtctnMthd(uint8_t method) { rdm_method_ = method; }
+    void setReplyDetectionMethod(uint8_t method) { rdm_method_ = method; }
 
     /// Set replay detection method value
     ///
     /// @param rdm replay detection method value to be set
-    void setRplyDtctnValue(uint64_t value) { rdm_value_ = value; }
+    void setReplyDetectionValue(uint64_t value) { rdm_value_ = value; }
     /// Set authentication information 
     ///
     /// @param auth_info authentication information to be set
@@ -109,12 +110,12 @@ public:
     /// Returns replay detection method type
     ///
     /// @return replay detection method type value
-    uint8_t getRplyDtctnMthd() const { return rdm_method_; }
+    uint8_t getReplyDetectionMethod() const { return rdm_method_; }
 
     /// Return replay detection mechanism 
     ///
     /// @return replay detection method value
-    uint64_t getRplyDtctnValue() const { return rdm_value_; }
+    uint64_t getReplyDetectionValue() const { return rdm_value_; }
     
     /// Return authentication information 
     ///
index 6c9cd898b17ff679f13ece0b9f9335be27a62b12..2e4a4a1220a63b6f7515b0ee552de224adee8f4c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -37,22 +37,22 @@ TEST_F(Option6AuthTest, basic) {
 
     ASSERT_EQ(1, auth->getProtocol());
     ASSERT_EQ(2, auth->getHashAlgo());
-    ASSERT_EQ(0, auth->getRplyDtctnMthd());
-    ASSERT_EQ(0x9000, auth->getRplyDtctnValue());
+    ASSERT_EQ(0, auth->getReplyDetectionMethod());
+    ASSERT_EQ(0x9000, auth->getReplyDetectionValue());
     
     std::vector<uint8_t> test_buf = {'a','b','c','d'};
     ASSERT_EQ(test_buf, auth->getAuthInfo());
 
     auth->setProtocol(2);
     auth->setHashAlgo(3);
-    auth->setRplyDtctnMthd(1);
-    auth->setRplyDtctnValue(109034830);
+    auth->setReplyDetectionMethod(1);
+    auth->setReplyDetectionValue(109034830);
     auth->setAuthInfo({1,2,3,4});
 
     ASSERT_EQ(2, auth->getProtocol());
     ASSERT_EQ(3, auth->getHashAlgo());
-    ASSERT_EQ(1, auth->getRplyDtctnMthd());
-    ASSERT_EQ(109034830, auth->getRplyDtctnValue());
+    ASSERT_EQ(1, auth->getReplyDetectionMethod());
+    ASSERT_EQ(109034830, auth->getReplyDetectionValue());
     
     test_buf = {1,2,3,4};
     ASSERT_EQ(test_buf, auth->getAuthInfo());
@@ -84,8 +84,8 @@ TEST_F(Option6AuthTest, parseFields) {
     std::vector<uint8_t> test_buf(16,0xa8);
     ASSERT_EQ(0xa1, auth->getProtocol());
     ASSERT_EQ(0xa2, auth->getHashAlgo());
-    ASSERT_EQ(0xa3, auth->getRplyDtctnMthd());
-    ASSERT_EQ(0xa4a5a6a7a8a9aaab, auth->getRplyDtctnValue());
+    ASSERT_EQ(0xa3, auth->getReplyDetectionMethod());
+    ASSERT_EQ(0xa4a5a6a7a8a9aaab, auth->getReplyDetectionValue());
     ASSERT_EQ(test_buf, auth->getAuthInfo());
 }
 
@@ -148,6 +148,16 @@ TEST_F(Option6AuthTest, negativeCase) {
 
      ASSERT_THROW(auth->pack(buf), isc::OutOfRange);
      ASSERT_THROW(auth->packHashInput(buf), isc::OutOfRange);
+}
+
+// Checks whether the to text conversion is working ok.
+TEST_F(Option6AuthTest, toText) {
+    scoped_ptr<Option6Auth> auth;
+    auth.reset(new Option6Auth(1,2,0,9000,{'a','b','c','d'}));
+
+    string exp_txt = "  protocol=1, algorithm=2, rdm method=0, rdm value=9000, value=61626364";
+
+    std::cout << auth->toText(2) << std::endl;
 
 }
 
index 203f08015696a3409a589cdc2a4497bf791d338c..5640595053810d7ef454a168931a9c682d7786a4 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
index b87e4e13da26cb0938c73206bfe746b62a921e1b..cc41fcf083cff4334e2195bf61c489f168868db2 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
index 827e91cb8ab21a597a6fa3f63b9d07a302f9356f..78f880da24686b9371be9863479500a4cd95f642 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -326,4 +326,28 @@ TEST_F(BufferTest, inputBufferReadVectorChunks) {
     EXPECT_EQ(0, memcmp(&vec[0], testdata+3, 2));
 }
 
+// Tests whether uint64 can be written properly.
+TEST_F(BufferTest, writeUint64) {
+
+    uint64_t val1 = 0x0102030405060708ul;
+    uint64_t val2 = 0xfffffffffffffffful;
+
+    uint8_t exp_val1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
+    uint8_t exp_val2[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+
+    const uint8_t* cp;
+
+    obuffer.writeUint64(val1);
+    ASSERT_EQ(sizeof(uint64_t), obuffer.getLength());
+    cp = static_cast<const uint8_t*>(obuffer.getData());
+    EXPECT_FALSE(memcmp(exp_val1, obuffer.getData(), sizeof(uint64_t)));
+
+    EXPECT_NO_THROW(obuffer.clear());
+
+    obuffer.writeUint64(val2);
+    ASSERT_EQ(sizeof(uint64_t), obuffer.getLength());
+    cp = static_cast<const uint8_t*>(obuffer.getData());
+    EXPECT_FALSE(memcmp(exp_val2, obuffer.getData(), sizeof(uint64_t)));
+}
+
 }
index 4d9bbec742d1d7758f8c410774b6ee5dbc98d72d..8a527d46169fe8bf9a4015dadd148ba760e69348 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -132,3 +132,29 @@ TEST(asioutil, writeUint32OutOfRange) {
     uint8_t data[3];
     EXPECT_THROW(writeUint32(i32, data, sizeof(data)), isc::OutOfRange);
 }
+
+// Tests whether uint64 can be read from a buffer properly.
+TEST(asioutil, readUint64) {
+
+    uint8_t buf[8];
+    for (int offset = 0; offset < sizeof(buf); offset++) {
+        buf[offset] = offset+1;
+    }
+
+    // Let's do some simple sanity checks first.
+    EXPECT_THROW(readUint64(NULL, 0), isc::OutOfRange);
+    EXPECT_THROW(readUint64(buf, 7), isc::OutOfRange);
+
+    // Now check if a real value could be read.
+    const uint64_t exp_val = 0x0102030405060708ul;
+    uint64_t val;
+
+    EXPECT_NO_THROW(val = readUint64(buf, 8));
+    EXPECT_EQ(val, exp_val);
+
+    // Now check if there are no buffer overflows.
+    memset(buf, 0xff, 8);
+
+    EXPECT_NO_THROW(val = readUint64(buf, 8));
+    EXPECT_EQ(0xfffffffffffffffful, val);
+}