- add sources.
[platform/framework/web/crosswalk.git] / src / net / quic / quic_config_test.cc
1 // Copyright (c) 2013 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/quic/quic_config.h"
6
7 #include "net/quic/crypto/crypto_handshake.h"
8 #include "net/quic/crypto/crypto_protocol.h"
9 #include "net/quic/quic_protocol.h"
10 #include "net/quic/quic_time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 using std::string;
14
15 namespace net {
16 namespace test {
17 namespace {
18
19 class QuicConfigTest : public ::testing::Test {
20  protected:
21   QuicConfigTest() {
22     config_.SetDefaults();
23     config_.set_initial_round_trip_time_us(kMaxInitialRoundTripTimeUs, 0);
24   }
25
26   QuicConfig config_;
27 };
28
29 TEST_F(QuicConfigTest, ToHandshakeMessage) {
30   config_.set_idle_connection_state_lifetime(QuicTime::Delta::FromSeconds(5),
31                                              QuicTime::Delta::FromSeconds(2));
32   config_.set_max_streams_per_connection(4, 2);
33   CryptoHandshakeMessage msg;
34   config_.ToHandshakeMessage(&msg);
35
36   uint32 value;
37   QuicErrorCode error = msg.GetUint32(kICSL, &value);
38   EXPECT_EQ(QUIC_NO_ERROR, error);
39   EXPECT_EQ(5u, value);
40
41   error = msg.GetUint32(kMSPC, &value);
42   EXPECT_EQ(QUIC_NO_ERROR, error);
43   EXPECT_EQ(4u, value);
44
45   const QuicTag* out;
46   size_t out_len;
47   error = msg.GetTaglist(kCGST, &out, &out_len);
48   EXPECT_EQ(1u, out_len);
49   EXPECT_EQ(kQBIC, *out);
50 }
51
52 TEST_F(QuicConfigTest, ProcessClientHello) {
53   QuicConfig client_config;
54   QuicTagVector cgst;
55   cgst.push_back(kINAR);
56   cgst.push_back(kQBIC);
57   client_config.set_congestion_control(cgst, kQBIC);
58   client_config.set_idle_connection_state_lifetime(
59       QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs),
60       QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs));
61   client_config.set_max_streams_per_connection(
62       2 * kDefaultMaxStreamsPerConnection, kDefaultMaxStreamsPerConnection);
63   client_config.set_initial_round_trip_time_us(
64       10 * base::Time::kMicrosecondsPerMillisecond,
65       10 * base::Time::kMicrosecondsPerMillisecond);
66
67   CryptoHandshakeMessage msg;
68   client_config.ToHandshakeMessage(&msg);
69   string error_details;
70   const QuicErrorCode error = config_.ProcessClientHello(msg, &error_details);
71   EXPECT_EQ(QUIC_NO_ERROR, error);
72   EXPECT_TRUE(config_.negotiated());
73   EXPECT_EQ(kQBIC, config_.congestion_control());
74   EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs),
75             config_.idle_connection_state_lifetime());
76   EXPECT_EQ(kDefaultMaxStreamsPerConnection,
77             config_.max_streams_per_connection());
78   EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_.keepalive_timeout());
79   EXPECT_EQ(10 * base::Time::kMicrosecondsPerMillisecond,
80             config_.initial_round_trip_time_us());
81 }
82
83 TEST_F(QuicConfigTest, ProcessServerHello) {
84   QuicConfig server_config;
85   QuicTagVector cgst;
86   cgst.push_back(kQBIC);
87   server_config.set_congestion_control(cgst, kQBIC);
88   server_config.set_idle_connection_state_lifetime(
89       QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs / 2),
90       QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs / 2));
91   server_config.set_max_streams_per_connection(
92       kDefaultMaxStreamsPerConnection / 2,
93       kDefaultMaxStreamsPerConnection / 2);
94   server_config.set_server_max_packet_size(kDefaultMaxPacketSize / 2,
95                                            kDefaultMaxPacketSize / 2);
96   server_config.set_server_initial_congestion_window(kDefaultInitialWindow / 2,
97                                                      kDefaultInitialWindow / 2);
98   server_config.set_initial_round_trip_time_us(
99       10 * base::Time::kMicrosecondsPerMillisecond,
100       10 * base::Time::kMicrosecondsPerMillisecond);
101
102   CryptoHandshakeMessage msg;
103   server_config.ToHandshakeMessage(&msg);
104   string error_details;
105   const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
106   EXPECT_EQ(QUIC_NO_ERROR, error);
107   EXPECT_TRUE(config_.negotiated());
108   EXPECT_EQ(kQBIC, config_.congestion_control());
109   EXPECT_EQ(QuicTime::Delta::FromSeconds(kDefaultTimeoutSecs / 2),
110             config_.idle_connection_state_lifetime());
111   EXPECT_EQ(kDefaultMaxStreamsPerConnection / 2,
112             config_.max_streams_per_connection());
113   EXPECT_EQ(kDefaultMaxPacketSize / 2, config_.server_max_packet_size());
114   EXPECT_EQ(kDefaultInitialWindow / 2,
115             config_.server_initial_congestion_window());
116   EXPECT_EQ(QuicTime::Delta::FromSeconds(0), config_.keepalive_timeout());
117   EXPECT_EQ(10 * base::Time::kMicrosecondsPerMillisecond,
118             config_.initial_round_trip_time_us());
119 }
120
121 TEST_F(QuicConfigTest, MissingValueInCHLO) {
122   CryptoHandshakeMessage msg;
123   msg.SetValue(kICSL, 1);
124   msg.SetVector(kCGST, QuicTagVector(1, kQBIC));
125   // Missing kMSPC. KATO is optional.
126   string error_details;
127   const QuicErrorCode error = config_.ProcessClientHello(msg, &error_details);
128   EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND, error);
129 }
130
131 TEST_F(QuicConfigTest, MissingValueInSHLO) {
132   CryptoHandshakeMessage msg;
133   msg.SetValue(kICSL, 1);
134   msg.SetValue(kMSPC, 3);
135   // Missing CGST. KATO is optional.
136   string error_details;
137   const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
138   EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND, error);
139 }
140
141 TEST_F(QuicConfigTest, OutOfBoundSHLO) {
142   QuicConfig server_config;
143   server_config.set_idle_connection_state_lifetime(
144       QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs),
145       QuicTime::Delta::FromSeconds(2 * kDefaultTimeoutSecs));
146
147   CryptoHandshakeMessage msg;
148   server_config.ToHandshakeMessage(&msg);
149   string error_details;
150   const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
151   EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE, error);
152 }
153
154 TEST_F(QuicConfigTest, MultipleNegotiatedValuesInVectorTag) {
155   QuicConfig server_config;
156   QuicTagVector cgst;
157   cgst.push_back(kQBIC);
158   cgst.push_back(kINAR);
159   server_config.set_congestion_control(cgst, kQBIC);
160
161   CryptoHandshakeMessage msg;
162   server_config.ToHandshakeMessage(&msg);
163   string error_details;
164   const QuicErrorCode error = config_.ProcessServerHello(msg, &error_details);
165   EXPECT_EQ(QUIC_INVALID_NEGOTIATED_VALUE, error);
166 }
167
168 TEST_F(QuicConfigTest, NoOverLapInCGST) {
169   QuicConfig server_config;
170   server_config.SetDefaults();
171   QuicTagVector cgst;
172   cgst.push_back(kINAR);
173   server_config.set_congestion_control(cgst, kINAR);
174
175   CryptoHandshakeMessage msg;
176   string error_details;
177   server_config.ToHandshakeMessage(&msg);
178   const QuicErrorCode error = config_.ProcessClientHello(msg, &error_details);
179   LOG(INFO) << QuicUtils::ErrorToString(error);
180   EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP, error);
181 }
182
183 }  // namespace
184 }  // namespace test
185 }  // namespace net