blob: f72c150da14daaa5f7b24f4cefeffc3ab278c680 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
QUICHE team5be974e2020-12-29 18:35:24 -05005#include "quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05006
bnc0ed49eb2020-04-09 12:06:21 -07007#include <cstdint>
8
9#include "third_party/boringssl/src/include/openssl/ssl.h"
QUICHE team5be974e2020-12-29 18:35:24 -050010#include "quic/platform/api/quic_test.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
12namespace quic {
13namespace test {
14namespace {
15
bnc8bea63a2020-08-26 10:36:06 -070016using QuicErrorCodesTest = QuicTest;
QUICHE teama6ef0a62019-03-07 20:34:33 -050017
18TEST_F(QuicErrorCodesTest, QuicErrorCodeToString) {
19 EXPECT_STREQ("QUIC_NO_ERROR", QuicErrorCodeToString(QUIC_NO_ERROR));
20}
21
bnc0ed49eb2020-04-09 12:06:21 -070022TEST_F(QuicErrorCodesTest, QuicIetfTransportErrorCodeString) {
23 EXPECT_EQ("Private(65280)",
24 QuicIetfTransportErrorCodeString(
25 static_cast<quic::QuicIetfTransportErrorCodes>(0xff00u)));
26
27 EXPECT_EQ("CRYPTO_ERROR(missing extension)",
28 QuicIetfTransportErrorCodeString(
29 static_cast<quic::QuicIetfTransportErrorCodes>(
30 CRYPTO_ERROR_FIRST + SSL_AD_MISSING_EXTENSION)));
31
32 EXPECT_EQ("NO_IETF_QUIC_ERROR",
33 QuicIetfTransportErrorCodeString(NO_IETF_QUIC_ERROR));
34 EXPECT_EQ("INTERNAL_ERROR", QuicIetfTransportErrorCodeString(INTERNAL_ERROR));
35 EXPECT_EQ("SERVER_BUSY_ERROR",
36 QuicIetfTransportErrorCodeString(SERVER_BUSY_ERROR));
37 EXPECT_EQ("FLOW_CONTROL_ERROR",
38 QuicIetfTransportErrorCodeString(FLOW_CONTROL_ERROR));
39 EXPECT_EQ("STREAM_LIMIT_ERROR",
40 QuicIetfTransportErrorCodeString(STREAM_LIMIT_ERROR));
41 EXPECT_EQ("STREAM_STATE_ERROR",
42 QuicIetfTransportErrorCodeString(STREAM_STATE_ERROR));
43 EXPECT_EQ("FINAL_SIZE_ERROR",
44 QuicIetfTransportErrorCodeString(FINAL_SIZE_ERROR));
45 EXPECT_EQ("FRAME_ENCODING_ERROR",
46 QuicIetfTransportErrorCodeString(FRAME_ENCODING_ERROR));
47 EXPECT_EQ("TRANSPORT_PARAMETER_ERROR",
48 QuicIetfTransportErrorCodeString(TRANSPORT_PARAMETER_ERROR));
49 EXPECT_EQ("CONNECTION_ID_LIMIT_ERROR",
50 QuicIetfTransportErrorCodeString(CONNECTION_ID_LIMIT_ERROR));
51 EXPECT_EQ("PROTOCOL_VIOLATION",
52 QuicIetfTransportErrorCodeString(PROTOCOL_VIOLATION));
53 EXPECT_EQ("INVALID_TOKEN", QuicIetfTransportErrorCodeString(INVALID_TOKEN));
54 EXPECT_EQ("CRYPTO_BUFFER_EXCEEDED",
55 QuicIetfTransportErrorCodeString(CRYPTO_BUFFER_EXCEEDED));
mattmd0744852020-10-16 14:42:01 -070056 EXPECT_EQ("KEY_UPDATE_ERROR",
57 QuicIetfTransportErrorCodeString(KEY_UPDATE_ERROR));
58 EXPECT_EQ("AEAD_LIMIT_REACHED",
59 QuicIetfTransportErrorCodeString(AEAD_LIMIT_REACHED));
bnc0ed49eb2020-04-09 12:06:21 -070060
61 EXPECT_EQ("Unknown(1024)",
62 QuicIetfTransportErrorCodeString(
63 static_cast<quic::QuicIetfTransportErrorCodes>(0x400)));
64}
65
66TEST_F(QuicErrorCodesTest, QuicErrorCodeToTransportErrorCode) {
67 for (int internal_error_code = 0; internal_error_code < QUIC_LAST_ERROR;
68 ++internal_error_code) {
69 std::string internal_error_code_string =
70 QuicErrorCodeToString(static_cast<QuicErrorCode>(internal_error_code));
71 if (internal_error_code_string == "INVALID_ERROR_CODE") {
72 // Not a valid QuicErrorCode.
73 continue;
74 }
75 QuicErrorCodeToIetfMapping ietf_error_code =
76 QuicErrorCodeToTransportErrorCode(
77 static_cast<QuicErrorCode>(internal_error_code));
bnc0054cc62020-04-09 18:22:57 -070078 if (ietf_error_code.is_transport_close) {
bnc0ed49eb2020-04-09 12:06:21 -070079 QuicIetfTransportErrorCodes transport_error_code =
bnc0054cc62020-04-09 18:22:57 -070080 static_cast<QuicIetfTransportErrorCodes>(ietf_error_code.error_code);
mattm55006b02021-01-14 15:09:54 -080081 bool is_transport_crypto_error_code =
82 transport_error_code >= 0x100 && transport_error_code <= 0x1ff;
83 if (is_transport_crypto_error_code) {
84 // Ensure that every QuicErrorCode that maps to a CRYPTO_ERROR code has
85 // a corresponding reverse mapping in TlsAlertToQuicErrorCode:
86 EXPECT_EQ(
87 internal_error_code,
88 TlsAlertToQuicErrorCode(transport_error_code - CRYPTO_ERROR_FIRST));
89 }
90 bool is_valid_transport_error_code =
91 transport_error_code <= 0x0f || is_transport_crypto_error_code;
bnc0ed49eb2020-04-09 12:06:21 -070092 EXPECT_TRUE(is_valid_transport_error_code) << internal_error_code_string;
93 } else {
94 // Non-transport errors are application errors, either HTTP/3 or QPACK.
bnc0054cc62020-04-09 18:22:57 -070095 uint64_t application_error_code = ietf_error_code.error_code;
bnc0ed49eb2020-04-09 12:06:21 -070096 bool is_valid_http3_error_code =
97 application_error_code >= 0x100 && application_error_code <= 0x110;
98 bool is_valid_qpack_error_code =
99 application_error_code >= 0x200 && application_error_code <= 0x202;
100 EXPECT_TRUE(is_valid_http3_error_code || is_valid_qpack_error_code)
101 << internal_error_code_string;
102 }
103 }
104}
105
bnc8bea63a2020-08-26 10:36:06 -0700106using QuicRstErrorCodesTest = QuicTest;
107
108TEST_F(QuicRstErrorCodesTest, QuicRstStreamErrorCodeToString) {
109 EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD",
110 QuicRstStreamErrorCodeToString(QUIC_BAD_APPLICATION_PAYLOAD));
111}
112
113// When an IETF application protocol error code (sent on the wire in
114// RESET_STREAM and STOP_SENDING frames) is translated into a
115// QuicRstStreamErrorCode and back, it must yield the original value.
116TEST_F(QuicRstErrorCodesTest,
117 IetfResetStreamErrorCodeToRstStreamErrorCodeAndBack) {
118 for (uint64_t wire_code :
119 {static_cast<uint64_t>(QuicHttp3ErrorCode::HTTP3_NO_ERROR),
120 static_cast<uint64_t>(QuicHttp3ErrorCode::GENERAL_PROTOCOL_ERROR),
121 static_cast<uint64_t>(QuicHttp3ErrorCode::INTERNAL_ERROR),
122 static_cast<uint64_t>(QuicHttp3ErrorCode::STREAM_CREATION_ERROR),
123 static_cast<uint64_t>(QuicHttp3ErrorCode::CLOSED_CRITICAL_STREAM),
124 static_cast<uint64_t>(QuicHttp3ErrorCode::FRAME_UNEXPECTED),
125 static_cast<uint64_t>(QuicHttp3ErrorCode::FRAME_ERROR),
126 static_cast<uint64_t>(QuicHttp3ErrorCode::EXCESSIVE_LOAD),
127 static_cast<uint64_t>(QuicHttp3ErrorCode::ID_ERROR),
128 static_cast<uint64_t>(QuicHttp3ErrorCode::SETTINGS_ERROR),
129 static_cast<uint64_t>(QuicHttp3ErrorCode::MISSING_SETTINGS),
130 static_cast<uint64_t>(QuicHttp3ErrorCode::REQUEST_REJECTED),
131 static_cast<uint64_t>(QuicHttp3ErrorCode::REQUEST_CANCELLED),
132 static_cast<uint64_t>(QuicHttp3ErrorCode::REQUEST_INCOMPLETE),
133 static_cast<uint64_t>(QuicHttp3ErrorCode::CONNECT_ERROR),
134 static_cast<uint64_t>(QuicHttp3ErrorCode::VERSION_FALLBACK),
135 static_cast<uint64_t>(QuicHttpQpackErrorCode::DECOMPRESSION_FAILED),
136 static_cast<uint64_t>(QuicHttpQpackErrorCode::ENCODER_STREAM_ERROR),
137 static_cast<uint64_t>(QuicHttpQpackErrorCode::DECODER_STREAM_ERROR)}) {
138 QuicRstStreamErrorCode rst_stream_error_code =
139 IetfResetStreamErrorCodeToRstStreamErrorCode(wire_code);
140 EXPECT_EQ(wire_code, RstStreamErrorCodeToIetfResetStreamErrorCode(
141 rst_stream_error_code));
142 }
143}
144
QUICHE teama6ef0a62019-03-07 20:34:33 -0500145} // namespace
146} // namespace test
147} // namespace quic