summaryrefslogtreecommitdiff
path: root/absl/strings/escaping_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/strings/escaping_test.cc')
-rw-r--r--absl/strings/escaping_test.cc36
1 files changed, 36 insertions, 0 deletions
diff --git a/absl/strings/escaping_test.cc b/absl/strings/escaping_test.cc
index ca1ee45c..25cb685b 100644
--- a/absl/strings/escaping_test.cc
+++ b/absl/strings/escaping_test.cc
@@ -689,6 +689,42 @@ TEST(Base64, DISABLED_HugeData) {
EXPECT_EQ(huge, unescaped);
}
+TEST(Escaping, HexStringToBytesBackToHex) {
+ std::string bytes, hex;
+
+ constexpr absl::string_view kTestHexLower = "1c2f0032f40123456789abcdef";
+ constexpr absl::string_view kTestHexUpper = "1C2F0032F40123456789ABCDEF";
+ constexpr absl::string_view kTestBytes = absl::string_view(
+ "\x1c\x2f\x00\x32\xf4\x01\x23\x45\x67\x89\xab\xcd\xef", 13);
+
+ EXPECT_TRUE(absl::HexStringToBytes(kTestHexLower, &bytes));
+ EXPECT_EQ(bytes, kTestBytes);
+
+ EXPECT_TRUE(absl::HexStringToBytes(kTestHexUpper, &bytes));
+ EXPECT_EQ(bytes, kTestBytes);
+
+ hex = absl::BytesToHexString(kTestBytes);
+ EXPECT_EQ(hex, kTestHexLower);
+
+ // Same buffer.
+ // We do not care if this works since we do not promise it in the contract.
+ // The purpose of this test is to to see if the program will crash or if
+ // sanitizers will catch anything.
+ bytes = std::string(kTestHexUpper);
+ (void)absl::HexStringToBytes(bytes, &bytes);
+
+ // Length not a multiple of two.
+ EXPECT_FALSE(absl::HexStringToBytes("1c2f003", &bytes));
+
+ // Not hex.
+ EXPECT_FALSE(absl::HexStringToBytes("1c2f00ft", &bytes));
+
+ // Empty input.
+ bytes = "abc";
+ EXPECT_TRUE(absl::HexStringToBytes("", &bytes));
+ EXPECT_EQ("", bytes); // Results in empty output.
+}
+
TEST(HexAndBack, HexStringToBytes_and_BytesToHexString) {
std::string hex_mixed = "0123456789abcdefABCDEF";
std::string bytes_expected = "\x01\x23\x45\x67\x89\xab\xcd\xef\xAB\xCD\xEF";