Replace deprecated version of absl::HexStringToBytes Replaces the deprecated version of absl::HexStringToBytes that returns std::string with the version that returns bool and populates a passed in string with the bytes. This CL is not expected to introduce any changes in functionality other than now verifying that absl::HexStringToBytes works successfully. This allows the Chrome build of the library to build without the `-Wno-deprecated-declarations` clang flag. PiperOrigin-RevId: 612568046
diff --git a/quiche/common/capsule_test.cc b/quiche/common/capsule_test.cc index fd07608..c51f816 100644 --- a/quiche/common/capsule_test.cc +++ b/quiche/common/capsule_test.cc
@@ -73,12 +73,14 @@ }; TEST_F(CapsuleTest, DatagramCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( - "00" // DATAGRAM capsule type - "08" // capsule length - "a1a2a3a4a5a6a7a8" // HTTP Datagram payload - ); - std::string datagram_payload = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("00" // DATAGRAM capsule type + "08" // capsule length + "a1a2a3a4a5a6a7a8", // HTTP Datagram payload + &capsule_fragment)); + std::string datagram_payload; + ASSERT_TRUE(absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &datagram_payload)); Capsule expected_capsule = Capsule::Datagram(datagram_payload); { EXPECT_CALL(visitor_, OnCapsule(expected_capsule)); @@ -89,7 +91,8 @@ } TEST_F(CapsuleTest, DatagramCapsuleViaHeader) { - std::string datagram_payload = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); + std::string datagram_payload; + ASSERT_TRUE(absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &datagram_payload)); quiche::QuicheBuffer expected_capsule = SerializeCapsule( Capsule::Datagram(datagram_payload), SimpleBufferAllocator::Get()); quiche::QuicheBuffer actual_header = SerializeDatagramCapsuleHeader( @@ -99,12 +102,14 @@ } TEST_F(CapsuleTest, LegacyDatagramCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( - "80ff37a0" // LEGACY_DATAGRAM capsule type - "08" // capsule length - "a1a2a3a4a5a6a7a8" // HTTP Datagram payload - ); - std::string datagram_payload = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("80ff37a0" // LEGACY_DATAGRAM capsule type + "08" // capsule length + "a1a2a3a4a5a6a7a8", // HTTP Datagram payload + &capsule_fragment)); + std::string datagram_payload; + ASSERT_TRUE(absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &datagram_payload)); Capsule expected_capsule = Capsule::LegacyDatagram(datagram_payload); { EXPECT_CALL(visitor_, OnCapsule(expected_capsule)); @@ -115,12 +120,14 @@ } TEST_F(CapsuleTest, LegacyDatagramWithoutContextCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( - "80ff37a5" // LEGACY_DATAGRAM_WITHOUT_CONTEXT capsule type - "08" // capsule length - "a1a2a3a4a5a6a7a8" // HTTP Datagram payload - ); - std::string datagram_payload = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); + std::string capsule_fragment; + ASSERT_TRUE(absl::HexStringToBytes( + "80ff37a5" // LEGACY_DATAGRAM_WITHOUT_CONTEXT capsule type + "08" // capsule length + "a1a2a3a4a5a6a7a8", // HTTP Datagram payload + &capsule_fragment)); + std::string datagram_payload; + ASSERT_TRUE(absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &datagram_payload)); Capsule expected_capsule = Capsule::LegacyDatagramWithoutContext(datagram_payload); { @@ -132,12 +139,13 @@ } TEST_F(CapsuleTest, CloseWebTransportStreamCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( - "6843" // CLOSE_WEBTRANSPORT_STREAM capsule type - "09" // capsule length - "00001234" // 0x1234 error code - "68656c6c6f" // "hello" error message - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("6843" // CLOSE_WEBTRANSPORT_STREAM capsule type + "09" // capsule length + "00001234" // 0x1234 error code + "68656c6c6f", // "hello" error message + &capsule_fragment)); Capsule expected_capsule = Capsule::CloseWebTransportSession( /*error_code=*/0x1234, /*error_message=*/"hello"); { @@ -149,10 +157,11 @@ } TEST_F(CapsuleTest, DrainWebTransportStreamCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( + std::string capsule_fragment; + ASSERT_TRUE(absl::HexStringToBytes( "800078ae" // DRAIN_WEBTRANSPORT_STREAM capsule type - "00" // capsule length - ); + "00", // capsule length + &capsule_fragment)); Capsule expected_capsule = Capsule(DrainWebTransportSessionCapsule()); { EXPECT_CALL(visitor_, OnCapsule(expected_capsule)); @@ -163,7 +172,8 @@ } TEST_F(CapsuleTest, AddressAssignCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( + std::string capsule_fragment; + ASSERT_TRUE(absl::HexStringToBytes( "9ECA6A00" // ADDRESS_ASSIGN capsule type "1A" // capsule length = 26 // first assigned address @@ -175,8 +185,8 @@ "01" // request ID = 1 "06" // IP version = 6 "20010db8123456780000000000000000" // 2001:db8:1234:5678:: - "40" // prefix length = 64 - ); + "40", // prefix length = 64 + &capsule_fragment)); Capsule expected_capsule = Capsule::AddressAssign(); quiche::QuicheIpAddress ip_address1; ip_address1.FromString("192.0.2.42"); @@ -203,7 +213,8 @@ } TEST_F(CapsuleTest, AddressRequestCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( + std::string capsule_fragment; + ASSERT_TRUE(absl::HexStringToBytes( "9ECA6A01" // ADDRESS_REQUEST capsule type "1A" // capsule length = 26 // first requested address @@ -215,8 +226,8 @@ "01" // request ID = 1 "06" // IP version = 6 "20010db8123456780000000000000000" // 2001:db8:1234:5678:: - "40" // prefix length = 64 - ); + "40", // prefix length = 64 + &capsule_fragment)); Capsule expected_capsule = Capsule::AddressRequest(); quiche::QuicheIpAddress ip_address1; ip_address1.FromString("192.0.2.42"); @@ -243,7 +254,8 @@ } TEST_F(CapsuleTest, RouteAdvertisementCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( + std::string capsule_fragment; + ASSERT_TRUE(absl::HexStringToBytes( "9ECA6A02" // ROUTE_ADVERTISEMENT capsule type "2C" // capsule length = 44 // first IP address range @@ -255,8 +267,8 @@ "06" // IP version = 6 "00000000000000000000000000000000" // :: "ffffffffffffffffffffffffffffffff" // all ones IPv6 address - "01" // ip protocol = 1 (ICMP) - ); + "01", // ip protocol = 1 (ICMP) + &capsule_fragment)); Capsule expected_capsule = Capsule::RouteAdvertisement(); IpAddressRange ip_address_range1; ip_address_range1.start_ip_address.FromString("192.0.2.24"); @@ -280,12 +292,13 @@ } TEST_F(CapsuleTest, WebTransportStreamData) { - std::string capsule_fragment = absl::HexStringToBytes( - "990b4d3b" // WT_STREAM without FIN - "04" // capsule length - "17" // stream ID - "abcdef" // stream payload - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("990b4d3b" // WT_STREAM without FIN + "04" // capsule length + "17" // stream ID + "abcdef", // stream payload + &capsule_fragment)); Capsule expected_capsule = Capsule(WebTransportStreamDataCapsule()); expected_capsule.web_transport_stream_data().stream_id = 0x17; expected_capsule.web_transport_stream_data().data = "\xab\xcd\xef"; @@ -298,12 +311,13 @@ TestSerialization(expected_capsule, capsule_fragment); } TEST_F(CapsuleTest, WebTransportStreamDataHeader) { - std::string capsule_fragment = absl::HexStringToBytes( + std::string capsule_fragment; + ASSERT_TRUE(absl::HexStringToBytes( "990b4d3b" // WT_STREAM without FIN "04" // capsule length - "17" // stream ID + "17", // stream ID // three bytes of stream payload implied below - ); + &capsule_fragment)); QuicheBufferAllocator* allocator = SimpleBufferAllocator::Get(); QuicheBuffer capsule_header = quiche::SerializeWebTransportStreamCapsuleHeader(0x17, /*fin=*/false, 3, @@ -311,12 +325,13 @@ EXPECT_EQ(capsule_header.AsStringView(), capsule_fragment); } TEST_F(CapsuleTest, WebTransportStreamDataWithFin) { - std::string capsule_fragment = absl::HexStringToBytes( - "990b4d3c" // data with FIN - "04" // capsule length - "17" // stream ID - "abcdef" // stream payload - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("990b4d3c" // data with FIN + "04" // capsule length + "17" // stream ID + "abcdef", // stream payload + &capsule_fragment)); Capsule expected_capsule = Capsule(WebTransportStreamDataCapsule()); expected_capsule.web_transport_stream_data().stream_id = 0x17; expected_capsule.web_transport_stream_data().data = "\xab\xcd\xef"; @@ -330,12 +345,13 @@ } TEST_F(CapsuleTest, WebTransportResetStream) { - std::string capsule_fragment = absl::HexStringToBytes( - "990b4d39" // WT_RESET_STREAM - "02" // capsule length - "17" // stream ID - "07" // error code - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("990b4d39" // WT_RESET_STREAM + "02" // capsule length + "17" // stream ID + "07", // error code + &capsule_fragment)); Capsule expected_capsule = Capsule(WebTransportResetStreamCapsule()); expected_capsule.web_transport_reset_stream().stream_id = 0x17; expected_capsule.web_transport_reset_stream().error_code = 0x07; @@ -348,12 +364,13 @@ } TEST_F(CapsuleTest, WebTransportStopSending) { - std::string capsule_fragment = absl::HexStringToBytes( - "990b4d3a" // WT_STOP_SENDING - "02" // capsule length - "17" // stream ID - "07" // error code - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("990b4d3a" // WT_STOP_SENDING + "02" // capsule length + "17" // stream ID + "07", // error code + &capsule_fragment)); Capsule expected_capsule = Capsule(WebTransportStopSendingCapsule()); expected_capsule.web_transport_stop_sending().stream_id = 0x17; expected_capsule.web_transport_stop_sending().error_code = 0x07; @@ -366,12 +383,13 @@ } TEST_F(CapsuleTest, WebTransportMaxStreamData) { - std::string capsule_fragment = absl::HexStringToBytes( - "990b4d3e" // WT_MAX_STREAM_DATA - "02" // capsule length - "17" // stream ID - "10" // max stream data - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("990b4d3e" // WT_MAX_STREAM_DATA + "02" // capsule length + "17" // stream ID + "10", // max stream data + &capsule_fragment)); Capsule expected_capsule = Capsule(WebTransportMaxStreamDataCapsule()); expected_capsule.web_transport_max_stream_data().stream_id = 0x17; expected_capsule.web_transport_max_stream_data().max_stream_data = 0x10; @@ -384,11 +402,12 @@ } TEST_F(CapsuleTest, WebTransportMaxStreamsBi) { - std::string capsule_fragment = absl::HexStringToBytes( - "990b4d3f" // WT_MAX_STREAMS (bidi) - "01" // capsule length - "17" // max streams - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("990b4d3f" // WT_MAX_STREAMS (bidi) + "01" // capsule length + "17", // max streams + &capsule_fragment)); Capsule expected_capsule = Capsule(WebTransportMaxStreamsCapsule()); expected_capsule.web_transport_max_streams().stream_type = StreamType::kBidirectional; @@ -402,11 +421,12 @@ } TEST_F(CapsuleTest, WebTransportMaxStreamsUni) { - std::string capsule_fragment = absl::HexStringToBytes( - "990b4d40" // WT_MAX_STREAMS (unidi) - "01" // capsule length - "17" // max streams - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("990b4d40" // WT_MAX_STREAMS (unidi) + "01" // capsule length + "17", // max streams + &capsule_fragment)); Capsule expected_capsule = Capsule(WebTransportMaxStreamsCapsule()); expected_capsule.web_transport_max_streams().stream_type = StreamType::kUnidirectional; @@ -420,12 +440,15 @@ } TEST_F(CapsuleTest, UnknownCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( - "17" // unknown capsule type of 0x17 - "08" // capsule length - "a1a2a3a4a5a6a7a8" // unknown capsule data - ); - std::string unknown_capsule_data = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("17" // unknown capsule type of 0x17 + "08" // capsule length + "a1a2a3a4a5a6a7a8", // unknown capsule data + &capsule_fragment)); + std::string unknown_capsule_data; + ASSERT_TRUE( + absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &unknown_capsule_data)); Capsule expected_capsule = Capsule::Unknown(0x17, unknown_capsule_data); { EXPECT_CALL(visitor_, OnCapsule(expected_capsule)); @@ -436,16 +459,19 @@ } TEST_F(CapsuleTest, TwoCapsules) { - std::string capsule_fragment = absl::HexStringToBytes( - "00" // DATAGRAM capsule type - "08" // capsule length - "a1a2a3a4a5a6a7a8" // HTTP Datagram payload - "00" // DATAGRAM capsule type - "08" // capsule length - "b1b2b3b4b5b6b7b8" // HTTP Datagram payload - ); - std::string datagram_payload1 = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); - std::string datagram_payload2 = absl::HexStringToBytes("b1b2b3b4b5b6b7b8"); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("00" // DATAGRAM capsule type + "08" // capsule length + "a1a2a3a4a5a6a7a8" // HTTP Datagram payload + "00" // DATAGRAM capsule type + "08" // capsule length + "b1b2b3b4b5b6b7b8", // HTTP Datagram payload + &capsule_fragment)); + std::string datagram_payload1; + ASSERT_TRUE(absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &datagram_payload1)); + std::string datagram_payload2; + ASSERT_TRUE(absl::HexStringToBytes("b1b2b3b4b5b6b7b8", &datagram_payload2)); Capsule expected_capsule1 = Capsule::Datagram(datagram_payload1); Capsule expected_capsule2 = Capsule::Datagram(datagram_payload2); { @@ -458,22 +484,27 @@ } TEST_F(CapsuleTest, TwoCapsulesPartialReads) { - std::string capsule_fragment1 = absl::HexStringToBytes( - "00" // first capsule DATAGRAM capsule type - "08" // first capsule length - "a1a2a3a4" // first half of HTTP Datagram payload of first capsule - ); - std::string capsule_fragment2 = absl::HexStringToBytes( + std::string capsule_fragment1; + ASSERT_TRUE(absl::HexStringToBytes( + "00" // first capsule DATAGRAM capsule type + "08" // first capsule length + "a1a2a3a4", // first half of HTTP Datagram payload of first capsule + &capsule_fragment1)); + std::string capsule_fragment2; + ASSERT_TRUE(absl::HexStringToBytes( "a5a6a7a8" // second half of HTTP Datagram payload 1 - "00" // second capsule DATAGRAM capsule type - ); - std::string capsule_fragment3 = absl::HexStringToBytes( - "08" // second capsule length - "b1b2b3b4b5b6b7b8" // HTTP Datagram payload of second capsule - ); + "00", // second capsule DATAGRAM capsule type + &capsule_fragment2)); + std::string capsule_fragment3; + ASSERT_TRUE(absl::HexStringToBytes( + "08" // second capsule length + "b1b2b3b4b5b6b7b8", // HTTP Datagram payload of second capsule + &capsule_fragment3)); capsule_parser_.ErrorIfThereIsRemainingBufferedData(); - std::string datagram_payload1 = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); - std::string datagram_payload2 = absl::HexStringToBytes("b1b2b3b4b5b6b7b8"); + std::string datagram_payload1; + ASSERT_TRUE(absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &datagram_payload1)); + std::string datagram_payload2; + ASSERT_TRUE(absl::HexStringToBytes("b1b2b3b4b5b6b7b8", &datagram_payload2)); Capsule expected_capsule1 = Capsule::Datagram(datagram_payload1); Capsule expected_capsule2 = Capsule::Datagram(datagram_payload2); { @@ -488,16 +519,19 @@ } TEST_F(CapsuleTest, TwoCapsulesOneByteAtATime) { - std::string capsule_fragment = absl::HexStringToBytes( - "00" // DATAGRAM capsule type - "08" // capsule length - "a1a2a3a4a5a6a7a8" // HTTP Datagram payload - "00" // DATAGRAM capsule type - "08" // capsule length - "b1b2b3b4b5b6b7b8" // HTTP Datagram payload - ); - std::string datagram_payload1 = absl::HexStringToBytes("a1a2a3a4a5a6a7a8"); - std::string datagram_payload2 = absl::HexStringToBytes("b1b2b3b4b5b6b7b8"); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("00" // DATAGRAM capsule type + "08" // capsule length + "a1a2a3a4a5a6a7a8" // HTTP Datagram payload + "00" // DATAGRAM capsule type + "08" // capsule length + "b1b2b3b4b5b6b7b8", // HTTP Datagram payload + &capsule_fragment)); + std::string datagram_payload1; + ASSERT_TRUE(absl::HexStringToBytes("a1a2a3a4a5a6a7a8", &datagram_payload1)); + std::string datagram_payload2; + ASSERT_TRUE(absl::HexStringToBytes("b1b2b3b4b5b6b7b8", &datagram_payload2)); Capsule expected_capsule1 = Capsule::Datagram(datagram_payload1); Capsule expected_capsule2 = Capsule::Datagram(datagram_payload2); for (size_t i = 0; i < capsule_fragment.size(); i++) { @@ -526,11 +560,12 @@ } TEST_F(CapsuleTest, PartialCapsuleThenError) { - std::string capsule_fragment = absl::HexStringToBytes( - "00" // DATAGRAM capsule type - "08" // capsule length - "a1a2a3a4" // first half of HTTP Datagram payload - ); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("00" // DATAGRAM capsule type + "08" // capsule length + "a1a2a3a4", // first half of HTTP Datagram payload + &capsule_fragment)); EXPECT_CALL(visitor_, OnCapsule(_)).Times(0); { EXPECT_CALL(visitor_, OnCapsuleParseFailure(_)).Times(0); @@ -545,11 +580,12 @@ } TEST_F(CapsuleTest, RejectOverlyLongCapsule) { - std::string capsule_fragment = absl::HexStringToBytes( - "17" // unknown capsule type of 0x17 - "80123456" // capsule length - ) + - std::string(1111111, '?'); + std::string capsule_fragment; + ASSERT_TRUE( + absl::HexStringToBytes("17" // unknown capsule type of 0x17 + "80123456", // capsule length + &capsule_fragment)); + absl::StrAppend(&capsule_fragment, std::string(1111111, '?')); EXPECT_CALL(visitor_, OnCapsuleParseFailure( "Refusing to buffer too much capsule data")); EXPECT_FALSE(capsule_parser_.IngestCapsuleFragment(capsule_fragment));
diff --git a/quiche/common/quiche_text_utils_test.cc b/quiche/common/quiche_text_utils_test.cc index cc62d78..d535614 100644 --- a/quiche/common/quiche_text_utils_test.cc +++ b/quiche/common/quiche_text_utils_test.cc
@@ -31,7 +31,9 @@ TEST(QuicheTextUtilsTest, HexDump) { // Verify output for empty input. - EXPECT_EQ("", quiche::QuicheTextUtils::HexDump(absl::HexStringToBytes(""))); + std::string empty; + ASSERT_TRUE(absl::HexStringToBytes("", &empty)); + EXPECT_EQ("", quiche::QuicheTextUtils::HexDump(empty)); // Verify output of the HexDump method is as expected. char packet[] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x51, 0x55, 0x49, 0x43, 0x21, @@ -51,13 +53,17 @@ "0x0040: 6c69 6e65 7320 6f66 206f 7574 7075 742e lines.of.output.\n" "0x0050: 0102 03 ...\n"); // Verify that 0x21 and 0x7e are printable, 0x20 and 0x7f are not. - EXPECT_EQ( - "0x0000: 2021 7e7f .!~.\n", - quiche::QuicheTextUtils::HexDump(absl::HexStringToBytes("20217e7f"))); + std::string printable_and_unprintable_chars; + ASSERT_TRUE( + absl::HexStringToBytes("20217e7f", &printable_and_unprintable_chars)); + EXPECT_EQ("0x0000: 2021 7e7f .!~.\n", + quiche::QuicheTextUtils::HexDump(printable_and_unprintable_chars)); // Verify that values above numeric_limits<unsigned char>::max() are formatted // properly on platforms where char is unsigned. + std::string large_chars; + ASSERT_TRUE(absl::HexStringToBytes("90aaff", &large_chars)); EXPECT_EQ("0x0000: 90aa ff ...\n", - quiche::QuicheTextUtils::HexDump(absl::HexStringToBytes("90aaff"))); + quiche::QuicheTextUtils::HexDump(large_chars)); } TEST(QuicheTextUtilsTest, Base64Encode) {
diff --git a/quiche/common/wire_serialization_test.cc b/quiche/common/wire_serialization_test.cc index f35c13d..98713d4 100644 --- a/quiche/common/wire_serialization_test.cc +++ b/quiche/common/wire_serialization_test.cc
@@ -6,10 +6,12 @@ #include <limits> #include <optional> +#include <string> #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "quiche/common/platform/api/quiche_expect_bug.h" #include "quiche/common/platform/api/quiche_test.h" @@ -45,7 +47,9 @@ template <typename... Ts> void ExpectEncodingHex(const std::string& description, absl::string_view expected_hex, Ts... data) { - ExpectEncoding(description, absl::HexStringToBytes(expected_hex), data...); + std::string expected; + ASSERT_TRUE(absl::HexStringToBytes(expected_hex, &expected)); + ExpectEncoding(description, expected, data...); } TEST(SerializationTest, SerializeStrings) { @@ -79,7 +83,9 @@ QUICHE_ASSERT_OK( SerializeIntoWriter(writer, WireUint16(0x1234), WireUint16(0xabcd))); absl::string_view actual(writer.data(), writer.length()); - EXPECT_EQ(actual, absl::HexStringToBytes("3412cdab")); + std::string expected; + ASSERT_TRUE(absl::HexStringToBytes("3412cdab", &expected)); + EXPECT_EQ(actual, expected); } TEST(SerializationTest, SerializeVarInt62) {
diff --git a/quiche/oblivious_http/buffers/oblivious_http_integration_test.cc b/quiche/oblivious_http/buffers/oblivious_http_integration_test.cc index b9e17f6..535b585 100644 --- a/quiche/oblivious_http/buffers/oblivious_http_integration_test.cc +++ b/quiche/oblivious_http/buffers/oblivious_http_integration_test.cc
@@ -3,6 +3,7 @@ #include <string> #include "absl/strings/escaping.h" +#include "absl/strings/string_view.h" #include "openssl/hpke.h" #include "quiche/common/platform/api/quiche_test.h" #include "quiche/oblivious_http/buffers/oblivious_http_response.h" @@ -20,13 +21,17 @@ std::string GetHpkePrivateKey() { absl::string_view hpke_key_hex = "b77431ecfa8f4cfc30d6e467aafa06944dffe28cb9dd1409e33a3045f5adc8a1"; - return absl::HexStringToBytes(hpke_key_hex); + std::string hpke_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(hpke_key_hex, &hpke_key_bytes)); + return hpke_key_bytes; } std::string GetHpkePublicKey() { absl::string_view public_key = "6d21cfe09fbea5122f9ebc2eb2a69fcc4f06408cd54aac934f012e76fcdcef62"; - return absl::HexStringToBytes(public_key); + std::string public_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(public_key, &public_key_bytes)); + return public_key_bytes; } const ObliviousHttpHeaderKeyConfig GetOhttpKeyConfig(uint8_t key_id,
diff --git a/quiche/oblivious_http/buffers/oblivious_http_request_test.cc b/quiche/oblivious_http/buffers/oblivious_http_request_test.cc index b788e5c..cf2f482 100644 --- a/quiche/oblivious_http/buffers/oblivious_http_request_test.cc +++ b/quiche/oblivious_http/buffers/oblivious_http_request_test.cc
@@ -25,31 +25,42 @@ std::string GetHpkePrivateKey() { absl::string_view hpke_key_hex = "b77431ecfa8f4cfc30d6e467aafa06944dffe28cb9dd1409e33a3045f5adc8a1"; - return absl::HexStringToBytes(hpke_key_hex); + std::string hpke_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(hpke_key_hex, &hpke_key_bytes)); + return hpke_key_bytes; } std::string GetHpkePublicKey() { absl::string_view public_key = "6d21cfe09fbea5122f9ebc2eb2a69fcc4f06408cd54aac934f012e76fcdcef62"; - return absl::HexStringToBytes(public_key); + std::string public_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(public_key, &public_key_bytes)); + return public_key_bytes; } std::string GetAlternativeHpkePublicKey() { absl::string_view public_key = "6d21cfe09fbea5122f9ebc2eb2a69fcc4f06408cd54aac934f012e76fcdcef63"; - return absl::HexStringToBytes(public_key); + std::string public_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(public_key, &public_key_bytes)); + return public_key_bytes; } std::string GetSeed() { absl::string_view seed = "52c4a758a802cd8b936eceea314432798d5baf2d7e9235dc084ab1b9cfa2f736"; - return absl::HexStringToBytes(seed); + std::string seed_bytes; + EXPECT_TRUE(absl::HexStringToBytes(seed, &seed_bytes)); + return seed_bytes; } std::string GetSeededEncapsulatedKey() { absl::string_view encapsulated_key = "37fda3567bdbd628e88668c3c8d7e97d1d1253b6d4ea6d44c150f741f1bf4431"; - return absl::HexStringToBytes(encapsulated_key); + std::string encapsulated_key_bytes; + EXPECT_TRUE( + absl::HexStringToBytes(encapsulated_key, &encapsulated_key_bytes)); + return encapsulated_key_bytes; } bssl::UniquePtr<EVP_HPKE_KEY> ConstructHpkeKey( @@ -94,10 +105,15 @@ "c0185204b4d63525"; // Initialize Request obj to Decapsulate (decrypt). + std::string encapsulated_request_bytes; + ASSERT_TRUE(absl::HexStringToBytes(kEncapsulatedRequest, + &encapsulated_request_bytes)); + std::string x25519_secret_key_bytes; + ASSERT_TRUE( + absl::HexStringToBytes(kX25519SecretKey, &x25519_secret_key_bytes)); auto instance = ObliviousHttpRequest::CreateServerObliviousRequest( - absl::HexStringToBytes(kEncapsulatedRequest), - *(ConstructHpkeKey(absl::HexStringToBytes(kX25519SecretKey), - ohttp_key_config)), + encapsulated_request_bytes, + *(ConstructHpkeKey(x25519_secret_key_bytes, ohttp_key_config)), ohttp_key_config); ASSERT_TRUE(instance.ok()); auto decrypted = instance->GetPlaintextData(); @@ -106,15 +122,21 @@ // https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-03.html#appendix-A-10 constexpr absl::string_view kExpectedEphemeralPublicKey = "4b28f881333e7c164ffc499ad9796f877f4e1051ee6d31bad19dec96c208b472"; + std::string expected_ephemeral_public_key_bytes; + ASSERT_TRUE(absl::HexStringToBytes(kExpectedEphemeralPublicKey, + &expected_ephemeral_public_key_bytes)); auto oblivious_request_context = std::move(instance.value()).ReleaseContext(); EXPECT_EQ(oblivious_request_context.encapsulated_key_, - absl::HexStringToBytes(kExpectedEphemeralPublicKey)); + expected_ephemeral_public_key_bytes); // Binary HTTP message. // https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-03.html#appendix-A-6 constexpr absl::string_view kExpectedBinaryHTTPMessage = "00034745540568747470730b6578616d706c652e636f6d012f"; - EXPECT_EQ(decrypted, absl::HexStringToBytes(kExpectedBinaryHTTPMessage)); + std::string expected_binary_http_message_bytes; + ASSERT_TRUE(absl::HexStringToBytes(kExpectedBinaryHTTPMessage, + &expected_binary_http_message_bytes)); + EXPECT_EQ(decrypted, expected_binary_http_message_bytes); } TEST(ObliviousHttpRequest, TestEncapsulatedRequestStructure) { @@ -167,12 +189,15 @@ GetSeededEncapsulatedKey()); absl::string_view expected_encrypted_request = "9f37cfed07d0111ecd2c34f794671759bcbd922a"; + std::string expected_encrypted_request_bytes; + ASSERT_TRUE(absl::HexStringToBytes(expected_encrypted_request, + &expected_encrypted_request_bytes)); EXPECT_NE(ohttp_request_context.hpke_context_, nullptr); size_t encapsulated_key_len = EVP_HPKE_KEM_enc_len( EVP_HPKE_CTX_kem(ohttp_request_context.hpke_context_.get())); int encrypted_payload_offset = kHeaderLength + encapsulated_key_len; EXPECT_EQ(encapsulated_request.substr(encrypted_payload_offset), - absl::HexStringToBytes(expected_encrypted_request)); + expected_encrypted_request_bytes); } TEST(ObliviousHttpRequest,
diff --git a/quiche/oblivious_http/common/oblivious_http_header_key_config_test.cc b/quiche/oblivious_http/common/oblivious_http_header_key_config_test.cc index 73e4c07..f9a8348 100644 --- a/quiche/oblivious_http/common/oblivious_http_header_key_config_test.cc +++ b/quiche/oblivious_http/common/oblivious_http_header_key_config_test.cc
@@ -1,6 +1,7 @@ #include "quiche/oblivious_http/common/oblivious_http_header_key_config.h" #include <cstdint> +#include <string> #include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" @@ -169,19 +170,24 @@ } TEST(ObliviousHttpKeyConfigs, SingleKeyConfig) { - std::string key = absl::HexStringToBytes( + std::string key; + ASSERT_TRUE(absl::HexStringToBytes( "4b0020f83e0a17cbdb18d2684dd2a9b087a43e5f3fa3fa27a049bc746a6e97a1e0244b00" - "0400010002"); + "0400010002", + &key)); auto configs = ObliviousHttpKeyConfigs::ParseConcatenatedKeys(key).value(); EXPECT_THAT(configs, Property(&ObliviousHttpKeyConfigs::NumKeys, 1)); EXPECT_THAT( configs.PreferredConfig(), AllOf(HasKeyId(0x4b), HasKemId(EVP_HPKE_DHKEM_X25519_HKDF_SHA256), HasKdfId(EVP_HPKE_HKDF_SHA256), HasAeadId(EVP_HPKE_AES_256_GCM))); + std::string expected_public_key; + ASSERT_TRUE(absl::HexStringToBytes( + "f83e0a17cbdb18d2684dd2a9b087a43e5f3fa3fa27a049bc746a6e97a1e0244b", + &expected_public_key)); EXPECT_THAT( configs.GetPublicKeyForId(configs.PreferredConfig().GetKeyId()).value(), - StrEq(absl::HexStringToBytes( - "f83e0a17cbdb18d2684dd2a9b087a43e5f3fa3fa27a049bc746a6e97a1e0244b"))); + StrEq(expected_public_key)); } TEST(ObliviousHttpKeyConfigs, TwoSimilarKeyConfigs) {
diff --git a/quiche/oblivious_http/oblivious_http_client_test.cc b/quiche/oblivious_http/oblivious_http_client_test.cc index a2768a5..65c6cc9 100644 --- a/quiche/oblivious_http/oblivious_http_client_test.cc +++ b/quiche/oblivious_http/oblivious_http_client_test.cc
@@ -19,14 +19,18 @@ // Dev/Test private key generated using Keystore. absl::string_view hpke_key_hex = "b77431ecfa8f4cfc30d6e467aafa06944dffe28cb9dd1409e33a3045f5adc8a1"; - return absl::HexStringToBytes(hpke_key_hex); + std::string hpke_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(hpke_key_hex, &hpke_key_bytes)); + return hpke_key_bytes; } std::string GetHpkePublicKey() { // Dev/Test public key generated using Keystore. absl::string_view public_key = "6d21cfe09fbea5122f9ebc2eb2a69fcc4f06408cd54aac934f012e76fcdcef62"; - return absl::HexStringToBytes(public_key); + std::string public_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(public_key, &public_key_bytes)); + return public_key_bytes; } const ObliviousHttpHeaderKeyConfig GetOhttpKeyConfig(uint8_t key_id,
diff --git a/quiche/oblivious_http/oblivious_http_gateway_test.cc b/quiche/oblivious_http/oblivious_http_gateway_test.cc index af03b21..ad52585 100644 --- a/quiche/oblivious_http/oblivious_http_gateway_test.cc +++ b/quiche/oblivious_http/oblivious_http_gateway_test.cc
@@ -21,14 +21,18 @@ // Dev/Test private key generated using Keystore. absl::string_view hpke_key_hex = "b77431ecfa8f4cfc30d6e467aafa06944dffe28cb9dd1409e33a3045f5adc8a1"; - return absl::HexStringToBytes(hpke_key_hex); + std::string hpke_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(hpke_key_hex, &hpke_key_bytes)); + return hpke_key_bytes; } std::string GetHpkePublicKey() { // Dev/Test public key generated using Keystore. absl::string_view public_key = "6d21cfe09fbea5122f9ebc2eb2a69fcc4f06408cd54aac934f012e76fcdcef62"; - return absl::HexStringToBytes(public_key); + std::string public_key_bytes; + EXPECT_TRUE(absl::HexStringToBytes(public_key, &public_key_bytes)); + return public_key_bytes; } const ObliviousHttpHeaderKeyConfig GetOhttpKeyConfig(uint8_t key_id, @@ -46,9 +50,12 @@ // https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-03.html#appendix-A-2 constexpr absl::string_view kX25519SecretKey = "3c168975674b2fa8e465970b79c8dcf09f1c741626480bd4c6162fc5b6a98e1a"; + std::string x25519_secret_key_bytes; + ASSERT_TRUE( + absl::HexStringToBytes(kX25519SecretKey, &x25519_secret_key_bytes)); auto instance = ObliviousHttpGateway::Create( - /*hpke_private_key*/ absl::HexStringToBytes(kX25519SecretKey), + /*hpke_private_key*/ x25519_secret_key_bytes, /*ohttp_key_config*/ GetOhttpKeyConfig( /*key_id=*/1, EVP_HPKE_DHKEM_X25519_HKDF_SHA256, EVP_HPKE_HKDF_SHA256, EVP_HPKE_AES_128_GCM)); @@ -59,9 +66,12 @@ "010020000100014b28f881333e7c164ffc499ad9796f877f4e1051ee6d31bad19dec96c2" "08b4726374e469135906992e1268c594d2a10c695d858c40a026e7965e7d86b83dd440b2" "c0185204b4d63525"; + std::string encapsulated_request_bytes; + ASSERT_TRUE(absl::HexStringToBytes(kEncapsulatedRequest, + &encapsulated_request_bytes)); - auto decrypted_req = instance->DecryptObliviousHttpRequest( - absl::HexStringToBytes(kEncapsulatedRequest)); + auto decrypted_req = + instance->DecryptObliviousHttpRequest(encapsulated_request_bytes); ASSERT_TRUE(decrypted_req.ok()); ASSERT_FALSE(decrypted_req->GetPlaintextData().empty()); } @@ -75,8 +85,10 @@ absl::string_view encrypted_req_1 = "010020000100025f20b60306b61ad9ecad389acd752ca75c4e2969469809fe3d84aae137" "f73e4ccfe9ba71f12831fdce6c8202fbd38a84c5d8a73ac4c8ea6c10592594845f"; - auto decapsulated_req_1 = instance->DecryptObliviousHttpRequest( - absl::HexStringToBytes(encrypted_req_1)); + std::string encrypted_req_1_bytes; + ASSERT_TRUE(absl::HexStringToBytes(encrypted_req_1, &encrypted_req_1_bytes)); + auto decapsulated_req_1 = + instance->DecryptObliviousHttpRequest(encrypted_req_1_bytes); ASSERT_TRUE(decapsulated_req_1.ok()); ASSERT_FALSE(decapsulated_req_1->GetPlaintextData().empty()); @@ -84,8 +96,10 @@ absl::string_view encrypted_req_2 = "01002000010002285ebc2fcad72cc91b378050cac29a62feea9cd97829335ee9fc87e672" "4fa13ff2efdff620423d54225d3099088e7b32a5165f805a5d922918865a0a447a"; - auto decapsulated_req_2 = instance->DecryptObliviousHttpRequest( - absl::HexStringToBytes(encrypted_req_2)); + std::string encrypted_req_2_bytes; + ASSERT_TRUE(absl::HexStringToBytes(encrypted_req_2, &encrypted_req_2_bytes)); + auto decapsulated_req_2 = + instance->DecryptObliviousHttpRequest(encrypted_req_2_bytes); ASSERT_TRUE(decapsulated_req_2.ok()); ASSERT_FALSE(decapsulated_req_2->GetPlaintextData().empty()); } @@ -141,17 +155,25 @@ EVP_HPKE_HKDF_SHA256, EVP_HPKE_AES_256_GCM), QuicheRandom::GetInstance()); // Setup contexts first. - auto decrypted_request_1 = instance->DecryptObliviousHttpRequest( + std::string encrypted_request_1_bytes; + ASSERT_TRUE( absl::HexStringToBytes("010020000100025f20b60306b61ad9ecad389acd752ca75c4" "e2969469809fe3d84aae137" "f73e4ccfe9ba71f12831fdce6c8202fbd38a84c5d8a73ac4c" - "8ea6c10592594845f")); + "8ea6c10592594845f", + &encrypted_request_1_bytes)); + auto decrypted_request_1 = + instance->DecryptObliviousHttpRequest(encrypted_request_1_bytes); ASSERT_TRUE(decrypted_request_1.ok()); - auto decrypted_request_2 = instance->DecryptObliviousHttpRequest( + std::string encrypted_request_2_bytes; + ASSERT_TRUE( absl::HexStringToBytes("01002000010002285ebc2fcad72cc91b378050cac29a62fee" "a9cd97829335ee9fc87e672" "4fa13ff2efdff620423d54225d3099088e7b32a5165f805a5" - "d922918865a0a447a")); + "d922918865a0a447a", + &encrypted_request_2_bytes)); + auto decrypted_request_2 = + instance->DecryptObliviousHttpRequest(encrypted_request_2_bytes); ASSERT_TRUE(decrypted_request_2.ok()); // Extract contexts and handle the response for each corresponding request. @@ -204,20 +226,22 @@ EVP_HPKE_HKDF_SHA256, EVP_HPKE_AES_256_GCM), QuicheRandom::GetInstance()); - TestQuicheThread t1( - *gateway_receiver, + std::string request_payload_1; + ASSERT_TRUE( absl::HexStringToBytes("010020000100025f20b60306b61ad9ecad389acd752ca75c4" "e2969469809fe3d84aae137" "f73e4ccfe9ba71f12831fdce6c8202fbd38a84c5d8a73ac4c" - "8ea6c10592594845f"), - "test response 1"); - TestQuicheThread t2( - *gateway_receiver, + "8ea6c10592594845f", + &request_payload_1)); + TestQuicheThread t1(*gateway_receiver, request_payload_1, "test response 1"); + std::string request_payload_2; + ASSERT_TRUE( absl::HexStringToBytes("01002000010002285ebc2fcad72cc91b378050cac29a62fee" "a9cd97829335ee9fc87e672" "4fa13ff2efdff620423d54225d3099088e7b32a5165f805a5" - "d922918865a0a447a"), - "test response 2"); + "d922918865a0a447a", + &request_payload_2)); + TestQuicheThread t2(*gateway_receiver, request_payload_2, "test response 2"); t1.Start(); t2.Start(); t1.Join();