diff options
author | Benjamin Barenblat <bbaren@google.com> | 2024-09-03 11:49:29 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@google.com> | 2024-09-03 11:49:29 -0400 |
commit | c1afa8b8238c25591ca80d068477aa7d4ce05fc8 (patch) | |
tree | 284a9f8b319de5783ff83ad004a9e390cb60fd0d /absl/strings/escaping_test.cc | |
parent | 23778b53f420f54eebc195dd8430e79bda165e5b (diff) | |
parent | 4447c7562e3bc702ade25105912dce503f0c4010 (diff) | |
download | abseil-c1afa8b8238c25591ca80d068477aa7d4ce05fc8.tar.gz abseil-c1afa8b8238c25591ca80d068477aa7d4ce05fc8.tar.bz2 abseil-c1afa8b8238c25591ca80d068477aa7d4ce05fc8.zip |
Merge new upstream LTS 20240722.0
Diffstat (limited to 'absl/strings/escaping_test.cc')
-rw-r--r-- | absl/strings/escaping_test.cc | 36 |
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"; |