Remove hex_encode functions with raw buffer output from the header file

Moved into the anonymous namespace in string_encode.cc.

Bug: webrtc:6424
Change-Id: I5e8ea0f02c94d6de82ca4f875d16862eb2db3d2b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/138073
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28034}
This commit is contained in:
Niels Möller
2019-05-23 12:10:26 +02:00
committed by Commit Bot
parent 39ece6d315
commit e7e3601614
3 changed files with 37 additions and 94 deletions

View File

@ -19,7 +19,7 @@ namespace rtc {
class HexEncodeTest : public ::testing::Test {
public:
HexEncodeTest() : enc_res_(0), dec_res_(0) {
HexEncodeTest() : dec_res_(0) {
for (size_t i = 0; i < sizeof(data_); ++i) {
data_[i] = (i + 128) & 0xff;
}
@ -27,111 +27,74 @@ class HexEncodeTest : public ::testing::Test {
}
char data_[10];
char encoded_[31];
char decoded_[11];
size_t enc_res_;
size_t dec_res_;
};
// Test that we can convert to/from hex with no delimiter.
TEST_F(HexEncodeTest, TestWithNoDelimiter) {
enc_res_ = hex_encode(encoded_, sizeof(encoded_), data_, sizeof(data_));
ASSERT_EQ(sizeof(data_) * 2, enc_res_);
ASSERT_STREQ("80818283848586878889", encoded_);
dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
std::string encoded = hex_encode(data_, sizeof(data_));
EXPECT_EQ("80818283848586878889", encoded);
dec_res_ =
hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
ASSERT_EQ(sizeof(data_), dec_res_);
ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
}
// Test that we can convert to/from hex with a colon delimiter.
TEST_F(HexEncodeTest, TestWithDelimiter) {
enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), data_,
sizeof(data_), ':');
ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
ASSERT_STREQ("80:81:82:83:84:85:86:87:88:89", encoded_);
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
enc_res_, ':');
std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
EXPECT_EQ("80:81:82:83:84:85:86:87:88:89", encoded);
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
encoded.data(), encoded.size(), ':');
ASSERT_EQ(sizeof(data_), dec_res_);
ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
}
// Test that encoding with one delimiter and decoding with another fails.
TEST_F(HexEncodeTest, TestWithWrongDelimiter) {
enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), data_,
sizeof(data_), ':');
ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
enc_res_, '/');
std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
encoded.data(), encoded.size(), '/');
ASSERT_EQ(0U, dec_res_);
}
// Test that encoding without a delimiter and decoding with one fails.
TEST_F(HexEncodeTest, TestExpectedDelimiter) {
enc_res_ = hex_encode(encoded_, sizeof(encoded_), data_, sizeof(data_));
ASSERT_EQ(sizeof(data_) * 2, enc_res_);
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
enc_res_, ':');
std::string encoded = hex_encode(data_, sizeof(data_));
EXPECT_EQ(sizeof(data_) * 2, encoded.size());
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
encoded.data(), encoded.size(), ':');
ASSERT_EQ(0U, dec_res_);
}
// Test that encoding with a delimiter and decoding without one fails.
TEST_F(HexEncodeTest, TestExpectedNoDelimiter) {
enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), data_,
sizeof(data_), ':');
ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
EXPECT_EQ(sizeof(data_) * 3 - 1, encoded.size());
dec_res_ =
hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
ASSERT_EQ(0U, dec_res_);
}
// Test that we handle a zero-length buffer with no delimiter.
TEST_F(HexEncodeTest, TestZeroLengthNoDelimiter) {
enc_res_ = hex_encode(encoded_, sizeof(encoded_), "", 0);
ASSERT_EQ(0U, enc_res_);
dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
std::string encoded = hex_encode("", 0);
EXPECT_TRUE(encoded.empty());
dec_res_ =
hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
ASSERT_EQ(0U, dec_res_);
}
// Test that we handle a zero-length buffer with a delimiter.
TEST_F(HexEncodeTest, TestZeroLengthWithDelimiter) {
enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), "", 0, ':');
ASSERT_EQ(0U, enc_res_);
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
enc_res_, ':');
std::string encoded = hex_encode_with_delimiter("", 0, ':');
EXPECT_TRUE(encoded.empty());
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
encoded.data(), encoded.size(), ':');
ASSERT_EQ(0U, dec_res_);
}
// Test the std::string variants that take no delimiter.
TEST_F(HexEncodeTest, TestHelpersNoDelimiter) {
std::string result = hex_encode(data_, sizeof(data_));
ASSERT_EQ("80818283848586878889", result);
dec_res_ = hex_decode(decoded_, sizeof(decoded_), result);
ASSERT_EQ(sizeof(data_), dec_res_);
ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
}
// Test the std::string variants that use a delimiter.
TEST_F(HexEncodeTest, TestHelpersWithDelimiter) {
std::string result = hex_encode_with_delimiter(data_, sizeof(data_), ':');
ASSERT_EQ("80:81:82:83:84:85:86:87:88:89", result);
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), result, ':');
ASSERT_EQ(sizeof(data_), dec_res_);
ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
}
// Test that encoding into a too-small output buffer (without delimiter) fails.
TEST_F(HexEncodeTest, TestEncodeTooShort) {
enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(data_) * 2, data_,
sizeof(data_), 0);
ASSERT_EQ(0U, enc_res_);
}
// Test that encoding into a too-small output buffer (with delimiter) fails.
TEST_F(HexEncodeTest, TestEncodeWithDelimiterTooShort) {
enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(data_) * 3 - 1, data_,
sizeof(data_), ':');
ASSERT_EQ(0U, enc_res_);
}
// Test that decoding into a too-small output buffer fails.
TEST_F(HexEncodeTest, TestDecodeTooShort) {
dec_res_ = hex_decode_with_delimiter(decoded_, 4, "0123456789", 10, 0);