summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quic/core/qpack/qpack_index_conversions_test.cc
blob: 214dff5f27da8b585a356214f40365c11ef3248a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/third_party/quiche/src/quic/core/qpack/qpack_index_conversions.h"

#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"

namespace quic {
namespace test {
namespace {

struct {
  uint64_t relative_index;
  uint64_t inserted_entry_count;
  uint64_t expected_absolute_index;
} kEncoderStreamRelativeIndexTestData[] = {{0, 1, 0},  {0, 2, 1},  {1, 2, 0},
                                           {0, 10, 9}, {5, 10, 4}, {9, 10, 0}};

TEST(QpackIndexConversions, EncoderStreamRelativeIndex) {
  for (const auto& test_data : kEncoderStreamRelativeIndexTestData) {
    uint64_t absolute_index = 42;
    EXPECT_TRUE(QpackEncoderStreamRelativeIndexToAbsoluteIndex(
        test_data.relative_index, test_data.inserted_entry_count,
        &absolute_index));
    EXPECT_EQ(test_data.expected_absolute_index, absolute_index);

    EXPECT_EQ(test_data.relative_index,
              QpackAbsoluteIndexToEncoderStreamRelativeIndex(
                  absolute_index, test_data.inserted_entry_count));
  }
}

struct {
  uint64_t relative_index;
  uint64_t base;
  uint64_t expected_absolute_index;
} kRequestStreamRelativeIndexTestData[] = {{0, 1, 0},  {0, 2, 1},  {1, 2, 0},
                                           {0, 10, 9}, {5, 10, 4}, {9, 10, 0}};

TEST(QpackIndexConversions, RequestStreamRelativeIndex) {
  for (const auto& test_data : kRequestStreamRelativeIndexTestData) {
    uint64_t absolute_index = 42;
    EXPECT_TRUE(QpackRequestStreamRelativeIndexToAbsoluteIndex(
        test_data.relative_index, test_data.base, &absolute_index));
    EXPECT_EQ(test_data.expected_absolute_index, absolute_index);

    EXPECT_EQ(test_data.relative_index,
              QpackAbsoluteIndexToRequestStreamRelativeIndex(absolute_index,
                                                             test_data.base));
  }
}

struct {
  uint64_t post_base_index;
  uint64_t base;
  uint64_t expected_absolute_index;
} kPostBaseIndexTestData[] = {{0, 1, 1}, {1, 0, 1}, {2, 0, 2},
                              {1, 1, 2}, {0, 2, 2}, {1, 2, 3}};

TEST(QpackIndexConversions, PostBaseIndex) {
  for (const auto& test_data : kPostBaseIndexTestData) {
    uint64_t absolute_index = 42;
    EXPECT_TRUE(QpackPostBaseIndexToAbsoluteIndex(
        test_data.post_base_index, test_data.base, &absolute_index));
    EXPECT_EQ(test_data.expected_absolute_index, absolute_index);
  }
}

TEST(QpackIndexConversions, EncoderStreamRelativeIndexUnderflow) {
  uint64_t absolute_index;
  EXPECT_FALSE(QpackEncoderStreamRelativeIndexToAbsoluteIndex(
      /* relative_index = */ 10,
      /* inserted_entry_count = */ 10, &absolute_index));
  EXPECT_FALSE(QpackEncoderStreamRelativeIndexToAbsoluteIndex(
      /* relative_index = */ 12,
      /* inserted_entry_count = */ 10, &absolute_index));
}

TEST(QpackIndexConversions, RequestStreamRelativeIndexUnderflow) {
  uint64_t absolute_index;
  EXPECT_FALSE(QpackRequestStreamRelativeIndexToAbsoluteIndex(
      /* relative_index = */ 10,
      /* base = */ 10, &absolute_index));
  EXPECT_FALSE(QpackRequestStreamRelativeIndexToAbsoluteIndex(
      /* relative_index = */ 12,
      /* base = */ 10, &absolute_index));
}

TEST(QpackIndexConversions, QpackPostBaseIndexToAbsoluteIndexOverflow) {
  uint64_t absolute_index;
  EXPECT_FALSE(QpackPostBaseIndexToAbsoluteIndex(
      /* post_base_index = */ 20,
      /* base = */ std::numeric_limits<uint64_t>::max() - 10, &absolute_index));
}

}  // namespace
}  // namespace test
}  // namespace quic