diff options
author | Tomek Mrugalski <tomasz@isc.org> | 2018-07-12 14:48:12 +0200 |
---|---|---|
committer | Tomek Mrugalski <tomasz@isc.org> | 2018-07-12 14:48:12 +0200 |
commit | 022dae4393da9e714678d52886d4f478ba308ba9 (patch) | |
tree | 32a53b64ab684ea2bfe729ecd9e0fbf4f395cc3c | |
parent | Merge branch 'auth_option' of https://github.com/MayyaSunil/kea into github93 (diff) | |
download | kea-022dae4393da9e714678d52886d4f478ba308ba9.tar.xz kea-022dae4393da9e714678d52886d4f478ba308ba9.zip |
[github93] Changes after review:
- added missing unit-tests, fixed bugs, small tweaks.
-rw-r--r-- | src/lib/dhcp/option6_auth.cc | 35 | ||||
-rw-r--r-- | src/lib/dhcp/option6_auth.h | 15 | ||||
-rw-r--r-- | src/lib/dhcp/tests/option6_auth_unittest.cc | 28 | ||||
-rw-r--r-- | src/lib/util/buffer.h | 2 | ||||
-rw-r--r-- | src/lib/util/io_utilities.h | 2 | ||||
-rw-r--r-- | src/lib/util/tests/buffer_unittest.cc | 26 | ||||
-rw-r--r-- | src/lib/util/tests/io_utilities_unittest.cc | 28 |
7 files changed, 101 insertions, 35 deletions
diff --git a/src/lib/dhcp/option6_auth.cc b/src/lib/dhcp/option6_auth.cc index bf68d56164..04b8b5f930 100644 --- a/src/lib/dhcp/option6_auth.cc +++ b/src/lib/dhcp/option6_auth.cc @@ -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(); } diff --git a/src/lib/dhcp/option6_auth.h b/src/lib/dhcp/option6_auth.h index 7ebfe393f4..91e5f5025e 100644 --- a/src/lib/dhcp/option6_auth.h +++ b/src/lib/dhcp/option6_auth.h @@ -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; @@ -56,7 +58,6 @@ public: 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 /// parsed option. @@ -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 /// diff --git a/src/lib/dhcp/tests/option6_auth_unittest.cc b/src/lib/dhcp/tests/option6_auth_unittest.cc index 6c9cd898b1..2e4a4a1220 100644 --- a/src/lib/dhcp/tests/option6_auth_unittest.cc +++ b/src/lib/dhcp/tests/option6_auth_unittest.cc @@ -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; } diff --git a/src/lib/util/buffer.h b/src/lib/util/buffer.h index 203f080156..5640595053 100644 --- a/src/lib/util/buffer.h +++ b/src/lib/util/buffer.h @@ -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 diff --git a/src/lib/util/io_utilities.h b/src/lib/util/io_utilities.h index b87e4e13da..cc41fcf083 100644 --- a/src/lib/util/io_utilities.h +++ b/src/lib/util/io_utilities.h @@ -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 diff --git a/src/lib/util/tests/buffer_unittest.cc b/src/lib/util/tests/buffer_unittest.cc index 827e91cb8a..78f880da24 100644 --- a/src/lib/util/tests/buffer_unittest.cc +++ b/src/lib/util/tests/buffer_unittest.cc @@ -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))); +} + } diff --git a/src/lib/util/tests/io_utilities_unittest.cc b/src/lib/util/tests/io_utilities_unittest.cc index 4d9bbec742..8a527d4616 100644 --- a/src/lib/util/tests/io_utilities_unittest.cc +++ b/src/lib/util/tests/io_utilities_unittest.cc @@ -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); +} |