Skip to content

Commit

Permalink
[#3209] addressed review
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan Becheriu authored and tmarkwalder committed Feb 7, 2024
1 parent 51d67d6 commit 94ac8f4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
14 changes: 7 additions & 7 deletions src/lib/util/encode/encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ BaseNEncoder::bitsToDigit(uint8_t bits) {
<< static_cast<uint16_t>(bits) << " invalid for " << algorithm_);
}

return(digit_set_[bits]);
return (digit_set_[bits]);
}

uint8_t
Expand All @@ -60,14 +60,14 @@ BaseNEncoder::digitToBits(uint8_t digit) {
<< static_cast<uint16_t>(digit) << " for " << algorithm_);
}

return(bits_table_[digit]);
return (bits_table_[digit]);
}

std::string
BaseNEncoder::encode(const std::vector<uint8_t>& input) {
std::string encoded_output;
if (input.empty()) {
return(encoded_output);
return (encoded_output);
}

uint8_t cur_bit = 0x0;
Expand Down Expand Up @@ -130,7 +130,7 @@ BaseNEncoder::encode(const std::vector<uint8_t>& input) {
}
}

return(encoded_output);
return (encoded_output);
}

void
Expand Down Expand Up @@ -301,7 +301,7 @@ const std::vector<uint8_t> Base16Encoder::BITS_TABLE = {
string
encodeBase64(const vector<uint8_t>& binary) {
static Base64Encoder encoder;
return(encoder.encode(binary));
return (encoder.encode(binary));
}

void
Expand All @@ -313,7 +313,7 @@ decodeBase64 (const std::string& encoded_str, std::vector<uint8_t>& output) {
string
encodeBase32Hex(const vector<uint8_t>& binary) {
static Base32HexEncoder encoder;
return(encoder.encode(binary));
return (encoder.encode(binary));
}

void
Expand All @@ -325,7 +325,7 @@ decodeBase32Hex(const std::string& encoded_str, std::vector<uint8_t>& output) {
string
encodeHex(const vector<uint8_t>& binary) {
static Base16Encoder encoder;
return(encoder.encode(binary));
return (encoder.encode(binary));
}

void
Expand Down
36 changes: 18 additions & 18 deletions src/lib/util/encode/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& bits_table,
Expand Down Expand Up @@ -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<uint8_t>& 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:
Expand All @@ -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::vector<uint8_t>bits_table_;

/// @brief Number of data bits represented by a digit
Expand All @@ -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
Expand Down Expand Up @@ -278,10 +278,10 @@ std::string encodeHex(const std::vector<uint8_t>& binary);
/// @throw BadValue if the input string is invalid.
void decodeHex(const std::string& encoded_str, std::vector<uint8_t>& 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<uint8_t> bin(value.begin(), value.end());
return ("0x" + encodeHex(bin));
Expand Down
38 changes: 19 additions & 19 deletions src/lib/util/tests/encode_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseNEncoder> BaseNEncoderPtr;

/// @brief Defines a encoding function.
/// @brief Defines an encoding function.
typedef std::function<std::string (const std::vector<uint8_t>&)> EncodeFunc;

/// @brief Defines a decoding function.
typedef std::function<void (const std::string&, std::vector<uint8_t>&)> 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
Expand Down Expand Up @@ -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::vector<uint8_t>input_data(input.begin(), input.end());
std::string output_str;
ASSERT_NO_THROW_LOG(output_str = (encode_func_)(input_data));
Expand Down Expand Up @@ -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<uint8_t> decoded_output;
ASSERT_NO_THROW_LOG((decode_func_)(encoded_str, decoded_output));
std::string tmp(decoded_output.begin(), decoded_output.end());
Expand All @@ -119,7 +119,7 @@ class EncodeDecodeTest : public :: testing::Test {
///
/// @param encoded_strings list of invalid encoded strings
void decodeInvalid(std::vector<std::string>& encoded_strings) {
for ( const auto& encoded_str : encoded_strings ) {
for (auto const& encoded_str : encoded_strings) {
std::vector<uint8_t> decoded_output;
EXPECT_THROW((decode_func_)(encoded_str, decoded_output), BadValue);
}
Expand Down Expand Up @@ -284,12 +284,12 @@ TEST_F(Base64Test, whiteSpace) {
// Verify invalid encodings are handled properly in Base64
TEST_F(Base64Test, decodeInvalid) {
std::vector<std::string> 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=",
Expand All @@ -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();
Expand Down Expand Up @@ -336,13 +331,13 @@ TEST_F(Base32HexTest, whiteSpace) {
// Verify invalid encodings are handled properly in Base32Hex
TEST_F(Base32HexTest, decodeInvalid) {
std::vector<std::string> 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======"
Expand All @@ -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();
Expand Down Expand Up @@ -378,7 +378,7 @@ TEST_F(Base16Test, whiteSpace) {
// Verify invalid encodings are handled properly in Base16
TEST_F(Base16Test, decodeInvalid) {
std::vector<std::string> encoded_strings = {
// Non hex digits should fail
// Non hex digits should fail.
"lx",
// Encoded string must have an even number of characters.
"dea"
Expand Down

0 comments on commit 94ac8f4

Please sign in to comment.