From 94ac8f4c45dc295e6008f9d96eb3a2b61bc8a2b9 Mon Sep 17 00:00:00 2001 From: Razvan Becheriu Date: Tue, 6 Feb 2024 18:56:27 +0200 Subject: [PATCH] [#3209] addressed review --- src/lib/util/encode/encode.cc | 14 +++++----- src/lib/util/encode/encode.h | 36 ++++++++++++------------- src/lib/util/tests/encode_unittest.cc | 38 +++++++++++++-------------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/lib/util/encode/encode.cc b/src/lib/util/encode/encode.cc index ed63c11cc4..baab625a60 100644 --- a/src/lib/util/encode/encode.cc +++ b/src/lib/util/encode/encode.cc @@ -50,7 +50,7 @@ BaseNEncoder::bitsToDigit(uint8_t bits) { << static_cast(bits) << " invalid for " << algorithm_); } - return(digit_set_[bits]); + return (digit_set_[bits]); } uint8_t @@ -60,14 +60,14 @@ BaseNEncoder::digitToBits(uint8_t digit) { << static_cast(digit) << " for " << algorithm_); } - return(bits_table_[digit]); + return (bits_table_[digit]); } std::string BaseNEncoder::encode(const std::vector& input) { std::string encoded_output; if (input.empty()) { - return(encoded_output); + return (encoded_output); } uint8_t cur_bit = 0x0; @@ -130,7 +130,7 @@ BaseNEncoder::encode(const std::vector& input) { } } - return(encoded_output); + return (encoded_output); } void @@ -301,7 +301,7 @@ const std::vector Base16Encoder::BITS_TABLE = { string encodeBase64(const vector& binary) { static Base64Encoder encoder; - return(encoder.encode(binary)); + return (encoder.encode(binary)); } void @@ -313,7 +313,7 @@ decodeBase64 (const std::string& encoded_str, std::vector& output) { string encodeBase32Hex(const vector& binary) { static Base32HexEncoder encoder; - return(encoder.encode(binary)); + return (encoder.encode(binary)); } void @@ -325,7 +325,7 @@ decodeBase32Hex(const std::string& encoded_str, std::vector& output) { string encodeHex(const vector& binary) { static Base16Encoder encoder; - return(encoder.encode(binary)); + return (encoder.encode(binary)); } void diff --git a/src/lib/util/encode/encode.h b/src/lib/util/encode/encode.h index 13d3c3e96e..0f89336859 100644 --- a/src/lib/util/encode/encode.h +++ b/src/lib/util/encode/encode.h @@ -29,9 +29,9 @@ class BaseNEncoder { /// @param digits_per_group number of digits contained in a group /// @param pad_char character used for padding out to group size (0 means no /// padding) - /// @param max_pad maximum number of pad characters in a group + /// @param max_pad maximum number of pad characters in a group /// @param case_sensitive indicates if the algorithm's digit set is - /// case sensitive. + /// case sensitive BaseNEncoder(const std::string& algorithm, const char* digit_set, const std::vector& bits_table, @@ -79,71 +79,71 @@ class BaseNEncoder { /// /// @return string containing the algorithm name std::string getAlgorithm() const { - return(algorithm_); + return (algorithm_); } /// @brief Get the digit set /// /// @return string containing the set of digits const char* getDigitSet() const { - return(digit_set_); + return (digit_set_); } /// @brief Get the digit lookup table /// /// @return vector containing the lookup table const std::vector& getBitsTable() const { - return(bits_table_); + return (bits_table_); } /// @brief Get the number of data bits represented by a digit /// /// @return number of data bits per digit size_t getBitsPerDigit() { - return(bits_per_digit_); + return (bits_per_digit_); } /// @brief Get the number of digits contained in a group /// /// @return number of digits per group size_t getDigitsPerGroup() const { - return(digits_per_group_); + return (digits_per_group_); } /// @brief Get the character used for padding out to group size (0 means no padding) /// /// @return Character used as a pad byte uint8_t getPadChar() const { - return(pad_char_); + return (pad_char_); } /// @brief Get the maximum number of pad characters in a group /// /// @return Maximum number of pad characters size_t getMaxPad() { - return(max_pad_); + return (max_pad_); } /// @brief Get the maxium index value of the digit set /// /// @return Maxium index value of the digit set size_t getMaxBitsToDigit() { - return(max_bits_to_digit_); + return (max_bits_to_digit_); } /// @brief Get the maxium index value of the algorithm bit table /// /// @return Maxium index value of the algorithm bit table size_t getMaxDigitToBits() { - return(max_digit_to_bits_); + return (max_digit_to_bits_); } /// @brief Indicates whether or not the algorithm's digit set /// is case-sensitive. /// - /// @return true if the digit set is case-sensitive. + /// @return true if the digit set is case-sensitive, false otherwise bool isCaseSensitive() { - return(case_sensitive_); + return (case_sensitive_); } protected: @@ -157,7 +157,7 @@ class BaseNEncoder { /// /// The table must map all 256 ASCII chars to their corresponding /// algorithm-specific data value. A data value of 0xee marks - /// a char as whitespace, 0xff marks a char is invalid. + /// a char as whitespace, 0xff marks a char is invalid std::vectorbits_table_; /// @brief Number of data bits represented by a digit @@ -172,7 +172,7 @@ class BaseNEncoder { /// @brief Maximum number of pad characters in a group size_t max_pad_; - /// @brief Indicates whether or not the algorithm's digit set is case-sensitive. + /// @brief Indicates whether or not the algorithm's digit set is case-sensitive bool case_sensitive_; /// @brief Maxium index value of the digit set @@ -278,10 +278,10 @@ std::string encodeHex(const std::vector& binary); /// @throw BadValue if the input string is invalid. void decodeHex(const std::string& encoded_str, std::vector& output); -/// @brief Encode in hexadecimal inline +/// @brief Encode in hexadecimal inline. /// -/// @param value the value to encode -/// @return 0x followed by the value encoded in hex +/// @param value the value to encode. +/// @return 0x followed by the value encoded in hex. inline std::string toHex(std::string value) { std::vector bin(value.begin(), value.end()); return ("0x" + encodeHex(bin)); diff --git a/src/lib/util/tests/encode_unittest.cc b/src/lib/util/tests/encode_unittest.cc index d90963db6b..7e2c8bedea 100644 --- a/src/lib/util/tests/encode_unittest.cc +++ b/src/lib/util/tests/encode_unittest.cc @@ -23,17 +23,17 @@ using namespace isc::util::encode; namespace { -/// @brief Defines a pointer to BaseNEncoder instances +/// @brief Defines a pointer to BaseNEncoder instances. typedef boost::shared_ptr BaseNEncoderPtr; -/// @brief Defines a encoding function. +/// @brief Defines an encoding function. typedef std::function&)> EncodeFunc; /// @brief Defines a decoding function. typedef std::function&)> DecodeFunc; -/// @brief Test fixture fro exercising BaseNEncoder derivatives. -class EncodeDecodeTest : public :: testing::Test { +/// @brief Test fixture for exercising BaseNEncoder derivatives. +class EncodeDecodeTest : public ::testing::Test { public: /// @brief Constructor @@ -75,7 +75,7 @@ class EncodeDecodeTest : public :: testing::Test { // -# convert the encoded result to lower case and verify decoding // yields correct result. auto expected_output_str = expected_encoded_strings_.begin(); - for ( const auto& input : valid_input_strings_ ) { + for (auto const& input : valid_input_strings_) { std::vectorinput_data(input.begin(), input.end()); std::string output_str; ASSERT_NO_THROW_LOG(output_str = (encode_func_)(input_data)); @@ -105,7 +105,7 @@ class EncodeDecodeTest : public :: testing::Test { ASSERT_EQ(encoded_strings.size(), expected_strings.size()); auto expected_str = expected_strings.begin(); - for ( const auto& encoded_str : encoded_strings ) { + for (auto const& encoded_str : encoded_strings) { std::vector decoded_output; ASSERT_NO_THROW_LOG((decode_func_)(encoded_str, decoded_output)); std::string tmp(decoded_output.begin(), decoded_output.end()); @@ -119,7 +119,7 @@ class EncodeDecodeTest : public :: testing::Test { /// /// @param encoded_strings list of invalid encoded strings void decodeInvalid(std::vector& encoded_strings) { - for ( const auto& encoded_str : encoded_strings ) { + for (auto const& encoded_str : encoded_strings) { std::vector decoded_output; EXPECT_THROW((decode_func_)(encoded_str, decoded_output), BadValue); } @@ -284,12 +284,12 @@ TEST_F(Base64Test, whiteSpace) { // Verify invalid encodings are handled properly in Base64 TEST_F(Base64Test, decodeInvalid) { std::vector encoded_strings = { - // incomplete input + // Incomplete input. "Zm9vYmF", - // only up to 2 padding characters are allowed + // Only up to 2 padding characters are allowed. "A===", "A= ==", - // intermediate padding isn't allowed + // Intermediate padding isn't allowed. "YmE=YmE=", // Non canonical form isn't allowed. "Zm9=", @@ -304,11 +304,6 @@ TEST_F(Base64Test, mappingCheck) { mapTest(); } -// Verify mappings for Base32Hex -TEST_F(Base32HexTest, mappingCheck) { - mapTest(); -} - // Verify RFC test vectors for Base32Hex TEST_F(Base32HexTest, validEncodeDecode) { encodeDecode(); @@ -336,13 +331,13 @@ TEST_F(Base32HexTest, whiteSpace) { // Verify invalid encodings are handled properly in Base32Hex TEST_F(Base32HexTest, decodeInvalid) { std::vector encoded_strings = { - // Incomplete input + // Incomplete input. "CPNMUOJ", - // invalid number of padding characters + // Invalid number of padding characters. "CPNMU0==", "CO0=====", "CO=======", - // intermediate padding isn't allowed + // Intermediate padding isn't allowed. "CPNMUOG=CPNMUOG=", // Non canonical form isn't allowed. "0P======" @@ -351,6 +346,11 @@ TEST_F(Base32HexTest, decodeInvalid) { decodeInvalid(encoded_strings); } +// Verify mappings for Base32Hex +TEST_F(Base32HexTest, mappingCheck) { + mapTest(); +} + // Verify RFC test vectors for Base16 TEST_F(Base16Test, validEncodeDecode) { encodeDecode(); @@ -378,7 +378,7 @@ TEST_F(Base16Test, whiteSpace) { // Verify invalid encodings are handled properly in Base16 TEST_F(Base16Test, decodeInvalid) { std::vector encoded_strings = { - // Non hex digits should fail + // Non hex digits should fail. "lx", // Encoded string must have an even number of characters. "dea"