summaryrefslogtreecommitdiff
path: root/chromium/net/quic/quic_config_test.cc
blob: eeaa97deaabdc930c57b727f79ffd42b0829d63c (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
// Copyright (c) 2013 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/quic/quic_config.h"

#include "net/quic/crypto/crypto_handshake.h"
#include "net/quic/crypto/crypto_protocol.h"
#include "net/quic/quic_protocol.h"
#include "net/quic/quic_time.h"
#include "testing/gtest/include/gtest/gtest.h"

using std::string;

namespace net {
namespace test {
namespace {

class QuicConfigTest : public ::testing::Test {
 protected:
  QuicConfigTest() {
    config_.SetDefaults();
  }

  QuicConfig config_;
};

TEST_F(QuicConfigTest, ToHandshakeMessage) {
  config_.set_idle_connection_state_lifetime(QuicTime::Delta::FromSeconds(5),
                                             QuicTime::Delta::FromSeconds(2));
  config_.set_max_streams_per_connection(4, 2);
  CryptoHandshakeMessage msg;
  config_.ToHandshakeMessage(&msg);

  uint32 value;
  QuicErrorCode error = msg.GetUint32(kICSL, &value);
  EXPECT_EQ(QUIC_NO_ERROR, error);
  EXPECT_EQ(5u, value);

  error = msg.GetUint32(kMSPC, &value);
  EXPECT_EQ(QUIC_NO_ERROR, error);
  EXPECT_EQ(4u, value);

  const QuicTag* out;
  size_t out_len;
  error = msg.GetTaglist(kCGST, &out, &out_len);
  EXPECT_EQ(1u, out_len);
  EXPECT_EQ(kQBIC, *out);
}

TEST_F(QuicConfigTest, ProcessClientHello) {
  QuicConfig client_config;
  QuicTagVector cgst;
  cgst.push_back(kINAR);
  cgst.push_back(kQBIC);
  client_config.set_congestion_control(cgst, kQBIC);
  client_config.set_idle_connection_state_lifetime(
      QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs),
      QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs));
  client_config.set_max_streams_per_connection(
      2 * kDefaultMaxStreamsPerConnection, kDefaultMaxStreamsPerConnection);

  CryptoHandshakeMessage msg;
  client_config.ToHandshakeMessage(&msg);
  string error_details;
  const QuicErrorCode error = config_.ProcessClientHello(msg, &error_details);
  EXPECT_EQ(QUIC_NO_ERROR, error);
  EXPECT_TRUE(config_.negotiated());
  EXPECT_EQ(kQBIC, config_.congestion_control());
  EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs),
            config_.idle_connection_state_lifetime());
  EXPECT_EQ(kDefaultMaxStreamsPerConnection,
            config_.max_streams_per_connection());
  EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_.keepalive_timeout());
}

TEST_F(QuicConfigTest, ProcessServerHello) {
  QuicConfig server_config;
  QuicTagVector cgst;
  cgst.push_back(kQBIC);
  server_config.set_congestion_control(cgst, kQBIC);
  server_config.set_idle_connection_state_lifetime(
      QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs / 2),
      QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs / 2));
  server_config.set_max_streams_per_connection(
      kDefaultMaxStreamsPerConnection / 2,
      kDefaultMaxStreamsPerConnection / 2);

  CryptoHandshakeMessage msg;
  server_config.ToHandshakeMessage(&msg);
  string error_details;
  const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
  EXPECT_EQ(QUIC_NO_ERROR, error);
  EXPECT_TRUE(config_.negotiated());
  EXPECT_EQ(kQBIC, config_.congestion_control());
  EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs / 2),
            config_.idle_connection_state_lifetime());
  EXPECT_EQ(kDefaultMaxStreamsPerConnection / 2,
            config_.max_streams_per_connection());
  EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_.keepalive_timeout());
}

TEST_F(QuicConfigTest, MissingValueInCHLO) {
  CryptoHandshakeMessage msg;
  msg.SetValue(kICSL, 1);
  msg.SetVector(kCGST, QuicTagVector(1, kQBIC));
  // Missing kMSPC. KATO is optional.
  string error_details;
  const QuicErrorCode error = config_.ProcessClientHello(msg, &error_details);
  EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND, error);
}

TEST_F(QuicConfigTest, MissingValueInSHLO) {
  CryptoHandshakeMessage msg;
  msg.SetValue(kICSL, 1);
  msg.SetValue(kMSPC, 3);
  // Missing CGST. KATO is optional.
  string error_details;
  const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
  EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND, error);
}

TEST_F(QuicConfigTest, OutOfBoundSHLO) {
  QuicConfig server_config;
  server_config.set_idle_connection_state_lifetime(
      QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs),
      QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs));

  CryptoHandshakeMessage msg;
  server_config.ToHandshakeMessage(&msg);
  string error_details;
  const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
  EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE, error);
}

TEST_F(QuicConfigTest, MultipleNegotiatedValuesInVectorTag) {
  QuicConfig server_config;
  QuicTagVector cgst;
  cgst.push_back(kQBIC);
  cgst.push_back(kINAR);
  server_config.set_congestion_control(cgst, kQBIC);

  CryptoHandshakeMessage msg;
  server_config.ToHandshakeMessage(&msg);
  string error_details;
  const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
  EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE, error);
}

TEST_F(QuicConfigTest, NoOverLapInCGST) {
  QuicConfig server_config;
  server_config.SetDefaults();
  QuicTagVector cgst;
  cgst.push_back(kINAR);
  server_config.set_congestion_control(cgst, kINAR);

  CryptoHandshakeMessage msg;
  string error_details;
  server_config.ToHandshakeMessage(&msg);
  const QuicErrorCode error = config_.ProcessClientHello(msg, &error_details);
  LOG(INFO) << QuicUtils::ErrorToString(error);
  EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP, error);
}

}  // namespace
}  // namespace test
}  // namespace net