Adopt absl::string_view in rtc_base/string_encode.*

Bug: webrtc:13579
Change-Id: If52108d151a12bde0e8d552ce7940948c08cef3a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/256812
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Jakob Ivarsson‎ <jakobi@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Ali Tofigh <alito@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36424}
This commit is contained in:
Ali Tofigh
2022-03-31 10:36:48 +02:00
committed by WebRTC LUCI CQ
parent f2599a7f43
commit fd6a4d6e2a
17 changed files with 125 additions and 114 deletions

View File

@ -14,6 +14,7 @@
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include "api/array_view.h"
#include "test/gtest.h"
namespace rtc {
@ -28,109 +29,106 @@ class HexEncodeTest : public ::testing::Test {
}
char data_[10];
absl::string_view data_view_{data_, sizeof(data_)};
char decoded_[11];
size_t dec_res_;
};
// Test that we can convert to/from hex with no delimiter.
TEST_F(HexEncodeTest, TestWithNoDelimiter) {
std::string encoded = hex_encode(data_, sizeof(data_));
std::string encoded = hex_encode(data_view_);
EXPECT_EQ("80818283848586878889", encoded);
dec_res_ =
hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
dec_res_ = hex_decode(ArrayView<char>(decoded_), encoded);
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) {
std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
std::string encoded = hex_encode_with_delimiter(data_view_, ':');
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(), ':');
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_), encoded, ':');
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) {
std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
encoded.data(), encoded.size(), '/');
std::string encoded = hex_encode_with_delimiter(data_view_, ':');
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_), encoded, '/');
ASSERT_EQ(0U, dec_res_);
}
// Test that encoding without a delimiter and decoding with one fails.
TEST_F(HexEncodeTest, TestExpectedDelimiter) {
std::string encoded = hex_encode(data_, sizeof(data_));
std::string encoded = hex_encode(data_view_);
EXPECT_EQ(sizeof(data_) * 2, encoded.size());
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
encoded.data(), encoded.size(), ':');
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_), encoded, ':');
ASSERT_EQ(0U, dec_res_);
}
// Test that encoding with a delimiter and decoding without one fails.
TEST_F(HexEncodeTest, TestExpectedNoDelimiter) {
std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
std::string encoded = hex_encode_with_delimiter(data_view_, ':');
EXPECT_EQ(sizeof(data_) * 3 - 1, encoded.size());
dec_res_ =
hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
dec_res_ = hex_decode(ArrayView<char>(decoded_), encoded);
ASSERT_EQ(0U, dec_res_);
}
// Test that we handle a zero-length buffer with no delimiter.
TEST_F(HexEncodeTest, TestZeroLengthNoDelimiter) {
std::string encoded = hex_encode("", 0);
std::string encoded = hex_encode("");
EXPECT_TRUE(encoded.empty());
dec_res_ =
hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
dec_res_ = hex_decode(ArrayView<char>(decoded_), encoded);
ASSERT_EQ(0U, dec_res_);
}
// Test that we handle a zero-length buffer with a delimiter.
TEST_F(HexEncodeTest, TestZeroLengthWithDelimiter) {
std::string encoded = hex_encode_with_delimiter("", 0, ':');
std::string encoded = hex_encode_with_delimiter("", ':');
EXPECT_TRUE(encoded.empty());
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
encoded.data(), encoded.size(), ':');
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_), encoded, ':');
ASSERT_EQ(0U, dec_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);
dec_res_ =
hex_decode_with_delimiter(ArrayView<char>(decoded_, 4), "0123456789", 0);
ASSERT_EQ(0U, dec_res_);
ASSERT_EQ(0x7f, decoded_[4]);
}
// Test that decoding non-hex data fails.
TEST_F(HexEncodeTest, TestDecodeBogusData) {
dec_res_ =
hex_decode_with_delimiter(decoded_, sizeof(decoded_), "axyz", 4, 0);
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_), "axyz", 0);
ASSERT_EQ(0U, dec_res_);
}
// Test that decoding an odd number of hex characters fails.
TEST_F(HexEncodeTest, TestDecodeOddHexDigits) {
dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), "012", 3, 0);
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_), "012", 0);
ASSERT_EQ(0U, dec_res_);
}
// Test that decoding a string with too many delimiters fails.
TEST_F(HexEncodeTest, TestDecodeWithDelimiterTooManyDelimiters) {
dec_res_ = hex_decode_with_delimiter(decoded_, 4, "01::23::45::67", 14, ':');
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_, 4),
"01::23::45::67", ':');
ASSERT_EQ(0U, dec_res_);
}
// Test that decoding a string with a leading delimiter fails.
TEST_F(HexEncodeTest, TestDecodeWithDelimiterLeadingDelimiter) {
dec_res_ = hex_decode_with_delimiter(decoded_, 4, ":01:23:45:67", 12, ':');
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_, 4),
":01:23:45:67", ':');
ASSERT_EQ(0U, dec_res_);
}
// Test that decoding a string with a trailing delimiter fails.
TEST_F(HexEncodeTest, TestDecodeWithDelimiterTrailingDelimiter) {
dec_res_ = hex_decode_with_delimiter(decoded_, 4, "01:23:45:67:", 12, ':');
dec_res_ = hex_decode_with_delimiter(ArrayView<char>(decoded_, 4),
"01:23:45:67:", ':');
ASSERT_EQ(0U, dec_res_);
}