bnc | cd5ec3c | 2019-08-14 13:50:46 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 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 "net/third_party/quiche/src/quic/core/qpack/qpack_index_conversions.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 8 | |
| 9 | namespace quic { |
| 10 | namespace test { |
| 11 | namespace { |
| 12 | |
| 13 | struct { |
| 14 | uint64_t relative_index; |
| 15 | uint64_t inserted_entry_count; |
| 16 | uint64_t expected_absolute_index; |
| 17 | } kEncoderStreamRelativeIndexTestData[] = {{0, 1, 0}, {0, 2, 1}, {1, 2, 0}, |
| 18 | {0, 10, 9}, {5, 10, 4}, {9, 10, 0}}; |
| 19 | |
| 20 | TEST(QpackIndexConversions, EncoderStreamRelativeIndex) { |
| 21 | for (const auto& test_data : kEncoderStreamRelativeIndexTestData) { |
| 22 | uint64_t absolute_index = 42; |
| 23 | EXPECT_TRUE(QpackEncoderStreamRelativeIndexToAbsoluteIndex( |
| 24 | test_data.relative_index, test_data.inserted_entry_count, |
| 25 | &absolute_index)); |
| 26 | EXPECT_EQ(test_data.expected_absolute_index, absolute_index); |
| 27 | |
| 28 | EXPECT_EQ(test_data.relative_index, |
| 29 | QpackAbsoluteIndexToEncoderStreamRelativeIndex( |
| 30 | absolute_index, test_data.inserted_entry_count)); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | struct { |
| 35 | uint64_t relative_index; |
| 36 | uint64_t base; |
| 37 | uint64_t expected_absolute_index; |
| 38 | } kRequestStreamRelativeIndexTestData[] = {{0, 1, 0}, {0, 2, 1}, {1, 2, 0}, |
| 39 | {0, 10, 9}, {5, 10, 4}, {9, 10, 0}}; |
| 40 | |
| 41 | TEST(QpackIndexConversions, RequestStreamRelativeIndex) { |
| 42 | for (const auto& test_data : kRequestStreamRelativeIndexTestData) { |
| 43 | uint64_t absolute_index = 42; |
| 44 | EXPECT_TRUE(QpackRequestStreamRelativeIndexToAbsoluteIndex( |
| 45 | test_data.relative_index, test_data.base, &absolute_index)); |
| 46 | EXPECT_EQ(test_data.expected_absolute_index, absolute_index); |
| 47 | |
| 48 | EXPECT_EQ(test_data.relative_index, |
| 49 | QpackAbsoluteIndexToRequestStreamRelativeIndex(absolute_index, |
| 50 | test_data.base)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | struct { |
| 55 | uint64_t post_base_index; |
| 56 | uint64_t base; |
| 57 | uint64_t expected_absolute_index; |
| 58 | } kPostBaseIndexTestData[] = {{0, 1, 1}, {1, 0, 1}, {2, 0, 2}, |
| 59 | {1, 1, 2}, {0, 2, 2}, {1, 2, 3}}; |
| 60 | |
| 61 | TEST(QpackIndexConversions, PostBaseIndex) { |
| 62 | for (const auto& test_data : kPostBaseIndexTestData) { |
| 63 | uint64_t absolute_index = 42; |
| 64 | EXPECT_TRUE(QpackPostBaseIndexToAbsoluteIndex( |
| 65 | test_data.post_base_index, test_data.base, &absolute_index)); |
| 66 | EXPECT_EQ(test_data.expected_absolute_index, absolute_index); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | TEST(QpackIndexConversions, EncoderStreamRelativeIndexUnderflow) { |
| 71 | uint64_t absolute_index; |
| 72 | EXPECT_FALSE(QpackEncoderStreamRelativeIndexToAbsoluteIndex( |
| 73 | /* relative_index = */ 10, |
| 74 | /* inserted_entry_count = */ 10, &absolute_index)); |
| 75 | EXPECT_FALSE(QpackEncoderStreamRelativeIndexToAbsoluteIndex( |
| 76 | /* relative_index = */ 12, |
| 77 | /* inserted_entry_count = */ 10, &absolute_index)); |
| 78 | } |
| 79 | |
| 80 | TEST(QpackIndexConversions, RequestStreamRelativeIndexUnderflow) { |
| 81 | uint64_t absolute_index; |
| 82 | EXPECT_FALSE(QpackRequestStreamRelativeIndexToAbsoluteIndex( |
| 83 | /* relative_index = */ 10, |
| 84 | /* base = */ 10, &absolute_index)); |
| 85 | EXPECT_FALSE(QpackRequestStreamRelativeIndexToAbsoluteIndex( |
| 86 | /* relative_index = */ 12, |
| 87 | /* base = */ 10, &absolute_index)); |
| 88 | } |
| 89 | |
| 90 | TEST(QpackIndexConversions, QpackPostBaseIndexToAbsoluteIndexOverflow) { |
| 91 | uint64_t absolute_index; |
| 92 | EXPECT_FALSE(QpackPostBaseIndexToAbsoluteIndex( |
| 93 | /* post_base_index = */ 20, |
| 94 | /* base = */ std::numeric_limits<uint64_t>::max() - 10, &absolute_index)); |
| 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | } // namespace test |
| 99 | } // namespace quic |