|
| 1 | +/** @file |
| 2 | +
|
| 3 | + Tests for QUIC connection ID initialization. |
| 4 | +
|
| 5 | + @section license License |
| 6 | +
|
| 7 | + Licensed to the Apache Software Foundation (ASF) under one |
| 8 | + or more contributor license agreements. See the NOTICE file |
| 9 | + distributed with this work for additional information |
| 10 | + regarding copyright ownership. The ASF licenses this file |
| 11 | + to you under the Apache License, Version 2.0 (the |
| 12 | + "License"); you may not use this file except in compliance |
| 13 | + with the License. You may obtain a copy of the License at |
| 14 | +
|
| 15 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | +
|
| 17 | + Unless required by applicable law or agreed to in writing, software |
| 18 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + See the License for the specific language governing permissions and |
| 21 | + limitations under the License. |
| 22 | + */ |
| 23 | + |
| 24 | +#include "iocore/net/quic/QUICTypes.h" |
| 25 | +#include "tsutil/PostScript.h" |
| 26 | + |
| 27 | +#include <catch2/catch_test_macros.hpp> |
| 28 | + |
| 29 | +#include <cstdint> |
| 30 | +#include <type_traits> |
| 31 | + |
| 32 | +static_assert(!std::is_default_constructible_v<QUICConnectionId>); |
| 33 | + |
| 34 | +TEST_CASE("QUICConnectionId requires explicit initialization", "[quic]") |
| 35 | +{ |
| 36 | + SECTION("empty connection ID") |
| 37 | + { |
| 38 | + QUICConnectionId cid = QUICConnectionId::ZERO(); |
| 39 | + |
| 40 | + CHECK(cid.length() == 0); |
| 41 | + CHECK(cid.is_zero()); |
| 42 | + CHECK(cid.hex() == "0x"); |
| 43 | + CHECK(static_cast<uint64_t>(cid) == 0); |
| 44 | + } |
| 45 | + |
| 46 | + SECTION("connection ID from bytes") |
| 47 | + { |
| 48 | + uint8_t const raw[] = {0x01, 0x02, 0x03, 0x04}; |
| 49 | + QUICConnectionId cid{raw, static_cast<uint8_t>(sizeof(raw))}; |
| 50 | + |
| 51 | + CHECK(cid.length() == sizeof(raw)); |
| 52 | + CHECK_FALSE(cid.is_zero()); |
| 53 | + CHECK(cid.h32() == 0x01020304); |
| 54 | + CHECK(static_cast<uint64_t>(cid) == 0x0102030400000000ULL); |
| 55 | + CHECK(cid.hex() == "0x01020304"); |
| 56 | + } |
| 57 | + |
| 58 | + SECTION("random connection ID") |
| 59 | + { |
| 60 | + uint8_t const previous_scid_len = QUICConnectionId::SCID_LEN; |
| 61 | + ts::PostScript restore_scid_len([previous_scid_len]() -> void { QUICConnectionId::SCID_LEN = previous_scid_len; }); |
| 62 | + |
| 63 | + QUICConnectionId::SCID_LEN = 18; |
| 64 | + QUICConnectionId cid = QUICConnectionId::random(); |
| 65 | + |
| 66 | + CHECK(cid.length() == 18); |
| 67 | + CHECK(cid.hex().size() == 2 + 18 * 2); |
| 68 | + } |
| 69 | +} |
0 commit comments