summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quic/core/qpack/qpack_instruction_decoder_test.cc
blob: f0d1d1d05647007497fa60d5033358163a22f64e (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2018 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_instruction_decoder.h"

#include <algorithm>

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "net/third_party/quiche/src/quic/core/qpack/qpack_constants.h"
#include "net/third_party/quiche/src/quic/core/qpack/qpack_test_utils.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"

using ::testing::_;
using ::testing::Eq;
using ::testing::Expectation;
using ::testing::Return;
using ::testing::StrictMock;
using ::testing::Values;

namespace quic {
namespace test {
namespace {

// This instruction has three fields: an S bit and two varints.
const QpackInstruction* TestInstruction1() {
  static const QpackInstruction* const instruction =
      new QpackInstruction{QpackInstructionOpcode{0x00, 0x80},
                           {{QpackInstructionFieldType::kSbit, 0x40},
                            {QpackInstructionFieldType::kVarint, 6},
                            {QpackInstructionFieldType::kVarint2, 8}}};
  return instruction;
}

// This instruction has two fields: a header name with a 6-bit prefix, and a
// header value with a 7-bit prefix, both preceded by a Huffman bit.
const QpackInstruction* TestInstruction2() {
  static const QpackInstruction* const instruction =
      new QpackInstruction{QpackInstructionOpcode{0x80, 0x80},
                           {{QpackInstructionFieldType::kName, 6},
                            {QpackInstructionFieldType::kValue, 7}}};
  return instruction;
}

const QpackLanguage* TestLanguage() {
  static const QpackLanguage* const language =
      new QpackLanguage{TestInstruction1(), TestInstruction2()};
  return language;
}

class MockDelegate : public QpackInstructionDecoder::Delegate {
 public:
  MockDelegate() {
    ON_CALL(*this, OnInstructionDecoded(_)).WillByDefault(Return(true));
  }

  MockDelegate(const MockDelegate&) = delete;
  MockDelegate& operator=(const MockDelegate&) = delete;
  ~MockDelegate() override = default;

  MOCK_METHOD1(OnInstructionDecoded, bool(const QpackInstruction* instruction));
  MOCK_METHOD1(OnError, void(QuicStringPiece error_message));
};

class QpackInstructionDecoderTest : public QuicTestWithParam<FragmentMode> {
 public:
  QpackInstructionDecoderTest()
      : decoder_(TestLanguage(), &delegate_), fragment_mode_(GetParam()) {}
  ~QpackInstructionDecoderTest() override = default;

 protected:
  // Decode one full instruction with fragment sizes dictated by
  // |fragment_mode_|.
  // Verifies that AtInstructionBoundary() returns true before and after the
  // instruction, and returns false while decoding is in progress.
  void DecodeInstruction(QuicStringPiece data) {
    EXPECT_TRUE(decoder_.AtInstructionBoundary());

    FragmentSizeGenerator fragment_size_generator =
        FragmentModeToFragmentSizeGenerator(fragment_mode_);

    while (!data.empty()) {
      size_t fragment_size = std::min(fragment_size_generator(), data.size());
      decoder_.Decode(data.substr(0, fragment_size));
      data = data.substr(fragment_size);
      if (!data.empty()) {
        EXPECT_FALSE(decoder_.AtInstructionBoundary());
      }
    }

    EXPECT_TRUE(decoder_.AtInstructionBoundary());
  }

  StrictMock<MockDelegate> delegate_;
  QpackInstructionDecoder decoder_;

 private:
  const FragmentMode fragment_mode_;
};

INSTANTIATE_TEST_SUITE_P(,
                         QpackInstructionDecoderTest,
                         Values(FragmentMode::kSingleChunk,
                                FragmentMode::kOctetByOctet));

TEST_P(QpackInstructionDecoderTest, SBitAndVarint2) {
  EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1()));
  DecodeInstruction(QuicTextUtils::HexDecode("7f01ff65"));

  EXPECT_TRUE(decoder_.s_bit());
  EXPECT_EQ(64u, decoder_.varint());
  EXPECT_EQ(356u, decoder_.varint2());

  EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1()));
  DecodeInstruction(QuicTextUtils::HexDecode("05c8"));

  EXPECT_FALSE(decoder_.s_bit());
  EXPECT_EQ(5u, decoder_.varint());
  EXPECT_EQ(200u, decoder_.varint2());
}

TEST_P(QpackInstructionDecoderTest, NameAndValue) {
  EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction2()));
  DecodeInstruction(QuicTextUtils::HexDecode("83666f6f03626172"));

  EXPECT_EQ("foo", decoder_.name());
  EXPECT_EQ("bar", decoder_.value());

  EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction2()));
  DecodeInstruction(QuicTextUtils::HexDecode("8000"));

  EXPECT_EQ("", decoder_.name());
  EXPECT_EQ("", decoder_.value());

  EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction2()));
  DecodeInstruction(QuicTextUtils::HexDecode("c294e7838c767f"));

  EXPECT_EQ("foo", decoder_.name());
  EXPECT_EQ("bar", decoder_.value());
}

TEST_P(QpackInstructionDecoderTest, InvalidHuffmanEncoding) {
  EXPECT_CALL(delegate_, OnError(Eq("Error in Huffman-encoded string.")));
  decoder_.Decode(QuicTextUtils::HexDecode("c1ff"));
}

TEST_P(QpackInstructionDecoderTest, InvalidVarintEncoding) {
  EXPECT_CALL(delegate_, OnError(Eq("Encoded integer too large.")));
  decoder_.Decode(QuicTextUtils::HexDecode("ffffffffffffffffffffff"));
}

TEST_P(QpackInstructionDecoderTest, DelegateSignalsError) {
  // First instruction is valid.
  Expectation first_call =
      EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1()))
          .WillOnce(Return(true));
  // Second instruction is invalid.  Decoding must halt.
  EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1()))
      .After(first_call)
      .WillOnce(Return(false));
  decoder_.Decode(QuicTextUtils::HexDecode("01000200030004000500"));

  EXPECT_EQ(2u, decoder_.varint());
}

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