xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_error_codes_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2013 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/quic_error_codes.h"
6 
7 #include <cstdint>
8 
9 #include "openssl/ssl.h"
10 #include "quiche/quic/platform/api/quic_test.h"
11 
12 namespace quic {
13 namespace test {
14 namespace {
15 
16 using QuicErrorCodesTest = QuicTest;
17 
TEST_F(QuicErrorCodesTest,QuicErrorCodeToString)18 TEST_F(QuicErrorCodesTest, QuicErrorCodeToString) {
19   EXPECT_STREQ("QUIC_NO_ERROR", QuicErrorCodeToString(QUIC_NO_ERROR));
20 }
21 
TEST_F(QuicErrorCodesTest,QuicIetfTransportErrorCodeString)22 TEST_F(QuicErrorCodesTest, QuicIetfTransportErrorCodeString) {
23   EXPECT_EQ("CRYPTO_ERROR(missing extension)",
24             QuicIetfTransportErrorCodeString(
25                 static_cast<quic::QuicIetfTransportErrorCodes>(
26                     CRYPTO_ERROR_FIRST + SSL_AD_MISSING_EXTENSION)));
27 
28   EXPECT_EQ("NO_IETF_QUIC_ERROR",
29             QuicIetfTransportErrorCodeString(NO_IETF_QUIC_ERROR));
30   EXPECT_EQ("INTERNAL_ERROR", QuicIetfTransportErrorCodeString(INTERNAL_ERROR));
31   EXPECT_EQ("SERVER_BUSY_ERROR",
32             QuicIetfTransportErrorCodeString(SERVER_BUSY_ERROR));
33   EXPECT_EQ("FLOW_CONTROL_ERROR",
34             QuicIetfTransportErrorCodeString(FLOW_CONTROL_ERROR));
35   EXPECT_EQ("STREAM_LIMIT_ERROR",
36             QuicIetfTransportErrorCodeString(STREAM_LIMIT_ERROR));
37   EXPECT_EQ("STREAM_STATE_ERROR",
38             QuicIetfTransportErrorCodeString(STREAM_STATE_ERROR));
39   EXPECT_EQ("FINAL_SIZE_ERROR",
40             QuicIetfTransportErrorCodeString(FINAL_SIZE_ERROR));
41   EXPECT_EQ("FRAME_ENCODING_ERROR",
42             QuicIetfTransportErrorCodeString(FRAME_ENCODING_ERROR));
43   EXPECT_EQ("TRANSPORT_PARAMETER_ERROR",
44             QuicIetfTransportErrorCodeString(TRANSPORT_PARAMETER_ERROR));
45   EXPECT_EQ("CONNECTION_ID_LIMIT_ERROR",
46             QuicIetfTransportErrorCodeString(CONNECTION_ID_LIMIT_ERROR));
47   EXPECT_EQ("PROTOCOL_VIOLATION",
48             QuicIetfTransportErrorCodeString(PROTOCOL_VIOLATION));
49   EXPECT_EQ("INVALID_TOKEN", QuicIetfTransportErrorCodeString(INVALID_TOKEN));
50   EXPECT_EQ("CRYPTO_BUFFER_EXCEEDED",
51             QuicIetfTransportErrorCodeString(CRYPTO_BUFFER_EXCEEDED));
52   EXPECT_EQ("KEY_UPDATE_ERROR",
53             QuicIetfTransportErrorCodeString(KEY_UPDATE_ERROR));
54   EXPECT_EQ("AEAD_LIMIT_REACHED",
55             QuicIetfTransportErrorCodeString(AEAD_LIMIT_REACHED));
56 
57   EXPECT_EQ("Unknown(1024)",
58             QuicIetfTransportErrorCodeString(
59                 static_cast<quic::QuicIetfTransportErrorCodes>(0x400)));
60 }
61 
TEST_F(QuicErrorCodesTest,QuicErrorCodeToTransportErrorCode)62 TEST_F(QuicErrorCodesTest, QuicErrorCodeToTransportErrorCode) {
63   for (uint32_t internal_error_code = 0; internal_error_code < QUIC_LAST_ERROR;
64        ++internal_error_code) {
65     std::string internal_error_code_string =
66         QuicErrorCodeToString(static_cast<QuicErrorCode>(internal_error_code));
67     if (internal_error_code_string == "INVALID_ERROR_CODE") {
68       // Not a valid QuicErrorCode.
69       continue;
70     }
71     QuicErrorCodeToIetfMapping ietf_error_code =
72         QuicErrorCodeToTransportErrorCode(
73             static_cast<QuicErrorCode>(internal_error_code));
74     if (ietf_error_code.is_transport_close) {
75       QuicIetfTransportErrorCodes transport_error_code =
76           static_cast<QuicIetfTransportErrorCodes>(ietf_error_code.error_code);
77       bool is_transport_crypto_error_code =
78           transport_error_code >= 0x100 && transport_error_code <= 0x1ff;
79       if (is_transport_crypto_error_code) {
80         // Ensure that every QuicErrorCode that maps to a CRYPTO_ERROR code has
81         // a corresponding reverse mapping in TlsAlertToQuicErrorCode:
82         EXPECT_EQ(
83             internal_error_code,
84             TlsAlertToQuicErrorCode(transport_error_code - CRYPTO_ERROR_FIRST));
85       }
86       bool is_valid_transport_error_code =
87           transport_error_code <= 0x0f || is_transport_crypto_error_code;
88       EXPECT_TRUE(is_valid_transport_error_code) << internal_error_code_string;
89     } else {
90       // Non-transport errors are application errors, either HTTP/3 or QPACK.
91       uint64_t application_error_code = ietf_error_code.error_code;
92       bool is_valid_http3_error_code =
93           application_error_code >= 0x100 && application_error_code <= 0x110;
94       bool is_valid_qpack_error_code =
95           application_error_code >= 0x200 && application_error_code <= 0x202;
96       EXPECT_TRUE(is_valid_http3_error_code || is_valid_qpack_error_code)
97           << internal_error_code_string;
98     }
99   }
100 }
101 
102 using QuicRstErrorCodesTest = QuicTest;
103 
TEST_F(QuicRstErrorCodesTest,QuicRstStreamErrorCodeToString)104 TEST_F(QuicRstErrorCodesTest, QuicRstStreamErrorCodeToString) {
105   EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD",
106                QuicRstStreamErrorCodeToString(QUIC_BAD_APPLICATION_PAYLOAD));
107 }
108 
109 // When an IETF application protocol error code (sent on the wire in
110 // RESET_STREAM and STOP_SENDING frames) is translated into a
111 // QuicRstStreamErrorCode and back, it must yield the original value.
TEST_F(QuicRstErrorCodesTest,IetfResetStreamErrorCodeToRstStreamErrorCodeAndBack)112 TEST_F(QuicRstErrorCodesTest,
113        IetfResetStreamErrorCodeToRstStreamErrorCodeAndBack) {
114   for (uint64_t wire_code :
115        {static_cast<uint64_t>(QuicHttp3ErrorCode::HTTP3_NO_ERROR),
116         static_cast<uint64_t>(QuicHttp3ErrorCode::GENERAL_PROTOCOL_ERROR),
117         static_cast<uint64_t>(QuicHttp3ErrorCode::INTERNAL_ERROR),
118         static_cast<uint64_t>(QuicHttp3ErrorCode::STREAM_CREATION_ERROR),
119         static_cast<uint64_t>(QuicHttp3ErrorCode::CLOSED_CRITICAL_STREAM),
120         static_cast<uint64_t>(QuicHttp3ErrorCode::FRAME_UNEXPECTED),
121         static_cast<uint64_t>(QuicHttp3ErrorCode::FRAME_ERROR),
122         static_cast<uint64_t>(QuicHttp3ErrorCode::EXCESSIVE_LOAD),
123         static_cast<uint64_t>(QuicHttp3ErrorCode::ID_ERROR),
124         static_cast<uint64_t>(QuicHttp3ErrorCode::SETTINGS_ERROR),
125         static_cast<uint64_t>(QuicHttp3ErrorCode::MISSING_SETTINGS),
126         static_cast<uint64_t>(QuicHttp3ErrorCode::REQUEST_REJECTED),
127         static_cast<uint64_t>(QuicHttp3ErrorCode::REQUEST_CANCELLED),
128         static_cast<uint64_t>(QuicHttp3ErrorCode::REQUEST_INCOMPLETE),
129         static_cast<uint64_t>(QuicHttp3ErrorCode::CONNECT_ERROR),
130         static_cast<uint64_t>(QuicHttp3ErrorCode::VERSION_FALLBACK),
131         static_cast<uint64_t>(QuicHttpQpackErrorCode::DECOMPRESSION_FAILED),
132         static_cast<uint64_t>(QuicHttpQpackErrorCode::ENCODER_STREAM_ERROR),
133         static_cast<uint64_t>(QuicHttpQpackErrorCode::DECODER_STREAM_ERROR)}) {
134     QuicRstStreamErrorCode rst_stream_error_code =
135         IetfResetStreamErrorCodeToRstStreamErrorCode(wire_code);
136     EXPECT_EQ(wire_code, RstStreamErrorCodeToIetfResetStreamErrorCode(
137                              rst_stream_error_code));
138   }
139 }
140 
141 }  // namespace
142 }  // namespace test
143 }  // namespace quic
144