xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/http/web_transport_http3_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "quiche/quic/core/http/web_transport_http3.h"
6 
7 #include <cstdint>
8 #include <limits>
9 #include <optional>
10 
11 #include "quiche/quic/platform/api/quic_test.h"
12 
13 namespace quic {
14 namespace {
15 
16 using ::testing::Optional;
17 
TEST(WebTransportHttp3Test,ErrorCodesToHttp3)18 TEST(WebTransportHttp3Test, ErrorCodesToHttp3) {
19   EXPECT_EQ(0x52e4a40fa8dbu, WebTransportErrorToHttp3(0x00));
20   EXPECT_EQ(0x52e4a40fa9e2u, WebTransportErrorToHttp3(0xff));
21   EXPECT_EQ(0x52e5ac983162u, WebTransportErrorToHttp3(0xffffffff));
22 
23   EXPECT_EQ(0x52e4a40fa8f7u, WebTransportErrorToHttp3(0x1c));
24   EXPECT_EQ(0x52e4a40fa8f8u, WebTransportErrorToHttp3(0x1d));
25   //        0x52e4a40fa8f9 is a GREASE codepoint
26   EXPECT_EQ(0x52e4a40fa8fau, WebTransportErrorToHttp3(0x1e));
27 }
28 
TEST(WebTransportHttp3Test,ErrorCodesToWebTransport)29 TEST(WebTransportHttp3Test, ErrorCodesToWebTransport) {
30   EXPECT_THAT(Http3ErrorToWebTransport(0x52e4a40fa8db), Optional(0x00));
31   EXPECT_THAT(Http3ErrorToWebTransport(0x52e4a40fa9e2), Optional(0xff));
32   EXPECT_THAT(Http3ErrorToWebTransport(0x52e5ac983162u), Optional(0xffffffff));
33 
34   EXPECT_THAT(Http3ErrorToWebTransport(0x52e4a40fa8f7), Optional(0x1cu));
35   EXPECT_THAT(Http3ErrorToWebTransport(0x52e4a40fa8f8), Optional(0x1du));
36   EXPECT_THAT(Http3ErrorToWebTransport(0x52e4a40fa8f9), std::nullopt);
37   EXPECT_THAT(Http3ErrorToWebTransport(0x52e4a40fa8fa), Optional(0x1eu));
38 
39   EXPECT_EQ(Http3ErrorToWebTransport(0), std::nullopt);
40   EXPECT_EQ(Http3ErrorToWebTransport(std::numeric_limits<uint64_t>::max()),
41             std::nullopt);
42 }
43 
TEST(WebTransportHttp3Test,ErrorCodeRoundTrip)44 TEST(WebTransportHttp3Test, ErrorCodeRoundTrip) {
45   for (int error = 0; error <= 65536; error++) {
46     uint64_t http_error = WebTransportErrorToHttp3(error);
47     std::optional<WebTransportStreamError> mapped_back =
48         quic::Http3ErrorToWebTransport(http_error);
49     ASSERT_THAT(mapped_back, Optional(error));
50   }
51   for (int64_t error = 0; error < std::numeric_limits<uint32_t>::max();
52        error += 65537) {
53     uint64_t http_error = WebTransportErrorToHttp3(error);
54     std::optional<WebTransportStreamError> mapped_back =
55         quic::Http3ErrorToWebTransport(http_error);
56     ASSERT_THAT(mapped_back, Optional(error));
57   }
58 }
59 
60 }  // namespace
61 }  // namespace quic
62