QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/core/quic_error_codes.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
bnc | 0ed49eb | 2020-04-09 12:06:21 -0700 | [diff] [blame] | 7 | #include <cstdint> |
| 8 | |
| 9 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 10 | #include "quic/platform/api/quic_test.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
| 12 | namespace quic { |
| 13 | namespace test { |
| 14 | namespace { |
| 15 | |
bnc | 8bea63a | 2020-08-26 10:36:06 -0700 | [diff] [blame] | 16 | using QuicErrorCodesTest = QuicTest; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | |
| 18 | TEST_F(QuicErrorCodesTest, QuicErrorCodeToString) { |
| 19 | EXPECT_STREQ("QUIC_NO_ERROR", QuicErrorCodeToString(QUIC_NO_ERROR)); |
| 20 | } |
| 21 | |
bnc | 0ed49eb | 2020-04-09 12:06:21 -0700 | [diff] [blame] | 22 | TEST_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)); |
mattm | d074485 | 2020-10-16 14:42:01 -0700 | [diff] [blame] | 56 | EXPECT_EQ("KEY_UPDATE_ERROR", |
| 57 | QuicIetfTransportErrorCodeString(KEY_UPDATE_ERROR)); |
| 58 | EXPECT_EQ("AEAD_LIMIT_REACHED", |
| 59 | QuicIetfTransportErrorCodeString(AEAD_LIMIT_REACHED)); |
bnc | 0ed49eb | 2020-04-09 12:06:21 -0700 | [diff] [blame] | 60 | |
| 61 | EXPECT_EQ("Unknown(1024)", |
| 62 | QuicIetfTransportErrorCodeString( |
| 63 | static_cast<quic::QuicIetfTransportErrorCodes>(0x400))); |
| 64 | } |
| 65 | |
| 66 | TEST_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)); |
bnc | 0054cc6 | 2020-04-09 18:22:57 -0700 | [diff] [blame] | 78 | if (ietf_error_code.is_transport_close) { |
bnc | 0ed49eb | 2020-04-09 12:06:21 -0700 | [diff] [blame] | 79 | QuicIetfTransportErrorCodes transport_error_code = |
bnc | 0054cc6 | 2020-04-09 18:22:57 -0700 | [diff] [blame] | 80 | static_cast<QuicIetfTransportErrorCodes>(ietf_error_code.error_code); |
mattm | 55006b0 | 2021-01-14 15:09:54 -0800 | [diff] [blame] | 81 | 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; |
bnc | 0ed49eb | 2020-04-09 12:06:21 -0700 | [diff] [blame] | 92 | 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. |
bnc | 0054cc6 | 2020-04-09 18:22:57 -0700 | [diff] [blame] | 95 | uint64_t application_error_code = ietf_error_code.error_code; |
bnc | 0ed49eb | 2020-04-09 12:06:21 -0700 | [diff] [blame] | 96 | 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 | |
bnc | 8bea63a | 2020-08-26 10:36:06 -0700 | [diff] [blame] | 106 | using QuicRstErrorCodesTest = QuicTest; |
| 107 | |
| 108 | TEST_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. |
| 116 | TEST_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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 145 | } // namespace |
| 146 | } // namespace test |
| 147 | } // namespace quic |