OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_crypto_server_stream.h" | 5 #include "net/quic/quic_crypto_server_stream.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "net/quic/crypto/aes_128_gcm_encrypter.h" | 11 #include "net/quic/crypto/aes_128_gcm_encrypter.h" |
12 #include "net/quic/crypto/crypto_framer.h" | 12 #include "net/quic/crypto/crypto_framer.h" |
13 #include "net/quic/crypto/crypto_handshake.h" | 13 #include "net/quic/crypto/crypto_handshake.h" |
14 #include "net/quic/crypto/crypto_protocol.h" | 14 #include "net/quic/crypto/crypto_protocol.h" |
| 15 #include "net/quic/crypto/crypto_server_config.h" |
15 #include "net/quic/crypto/crypto_utils.h" | 16 #include "net/quic/crypto/crypto_utils.h" |
16 #include "net/quic/crypto/quic_decrypter.h" | 17 #include "net/quic/crypto/quic_decrypter.h" |
17 #include "net/quic/crypto/quic_encrypter.h" | 18 #include "net/quic/crypto/quic_encrypter.h" |
| 19 #include "net/quic/quic_crypto_client_stream.h" |
| 20 #include "net/quic/quic_crypto_server_stream.h" |
18 #include "net/quic/quic_protocol.h" | 21 #include "net/quic/quic_protocol.h" |
19 #include "net/quic/quic_session.h" | 22 #include "net/quic/quic_session.h" |
20 #include "net/quic/test_tools/crypto_test_utils.h" | 23 #include "net/quic/test_tools/crypto_test_utils.h" |
21 #include "net/quic/test_tools/quic_test_utils.h" | 24 #include "net/quic/test_tools/quic_test_utils.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
24 | 27 |
25 namespace net { | 28 namespace net { |
26 class QuicConnection; | 29 class QuicConnection; |
27 class ReliableQuicStream; | 30 class ReliableQuicStream; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 class QuicCryptoServerStreamTest : public ::testing::Test { | 69 class QuicCryptoServerStreamTest : public ::testing::Test { |
67 public: | 70 public: |
68 QuicCryptoServerStreamTest() | 71 QuicCryptoServerStreamTest() |
69 : guid_(1), | 72 : guid_(1), |
70 addr_(ParseIPLiteralToNumber("192.0.2.33", &ip_) ? | 73 addr_(ParseIPLiteralToNumber("192.0.2.33", &ip_) ? |
71 ip_ : IPAddressNumber(), 1), | 74 ip_ : IPAddressNumber(), 1), |
72 connection_(new PacketSavingConnection(guid_, addr_, true)), | 75 connection_(new PacketSavingConnection(guid_, addr_, true)), |
73 session_(connection_, true), | 76 session_(connection_, true), |
74 crypto_config_(QuicCryptoServerConfig::TESTING), | 77 crypto_config_(QuicCryptoServerConfig::TESTING), |
75 stream_(config_, crypto_config_, &session_) { | 78 stream_(config_, crypto_config_, &session_) { |
| 79 // We advance the clock initially because the default time is zero and the |
| 80 // strike register worries that we've just overflowed a uint32 time. |
| 81 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(100000)); |
| 82 // TODO(rtenneti): Enable testing of ProofSource. |
| 83 // crypto_config_.SetProofSource(CryptoTestUtils::ProofSourceForTesting()); |
| 84 |
76 CryptoTestUtils::SetupCryptoServerConfigForTest( | 85 CryptoTestUtils::SetupCryptoServerConfigForTest( |
77 connection_->clock(), connection_->random_generator(), &config_, | 86 connection_->clock(), connection_->random_generator(), &config_, |
78 &crypto_config_); | 87 &crypto_config_); |
79 } | 88 } |
80 | 89 |
81 void ConstructHandshakeMessage() { | 90 void ConstructHandshakeMessage() { |
82 CryptoFramer framer; | 91 CryptoFramer framer; |
83 message_data_.reset(framer.ConstructHandshakeMessage(message_)); | 92 message_data_.reset(framer.ConstructHandshakeMessage(message_)); |
84 } | 93 } |
85 | 94 |
86 void CompleteCryptoHandshake() { | 95 int CompleteCryptoHandshake() { |
87 CryptoTestUtils::HandshakeWithFakeClient(connection_, &stream_); | 96 return CryptoTestUtils::HandshakeWithFakeClient(connection_, &stream_); |
88 } | 97 } |
89 | 98 |
90 protected: | 99 protected: |
91 IPAddressNumber ip_; | 100 IPAddressNumber ip_; |
92 QuicGuid guid_; | 101 QuicGuid guid_; |
93 IPEndPoint addr_; | 102 IPEndPoint addr_; |
94 PacketSavingConnection* connection_; | 103 PacketSavingConnection* connection_; |
95 TestSession session_; | 104 TestSession session_; |
96 QuicConfig config_; | 105 QuicConfig config_; |
97 QuicCryptoServerConfig crypto_config_; | 106 QuicCryptoServerConfig crypto_config_; |
(...skipping 10 matching lines...) Expand all Loading... |
108 | 117 |
109 EXPECT_FALSE(stream_.handshake_complete()); | 118 EXPECT_FALSE(stream_.handshake_complete()); |
110 } | 119 } |
111 | 120 |
112 TEST_F(QuicCryptoServerStreamTest, ConnectedAfterCHLO) { | 121 TEST_F(QuicCryptoServerStreamTest, ConnectedAfterCHLO) { |
113 if (!Aes128GcmEncrypter::IsSupported()) { | 122 if (!Aes128GcmEncrypter::IsSupported()) { |
114 LOG(INFO) << "AES GCM not supported. Test skipped."; | 123 LOG(INFO) << "AES GCM not supported. Test skipped."; |
115 return; | 124 return; |
116 } | 125 } |
117 | 126 |
118 CompleteCryptoHandshake(); | 127 EXPECT_EQ(2, CompleteCryptoHandshake()); |
119 EXPECT_TRUE(stream_.handshake_complete()); | 128 EXPECT_TRUE(stream_.handshake_complete()); |
120 } | 129 } |
121 | 130 |
| 131 TEST_F(QuicCryptoServerStreamTest, ZeroRTT) { |
| 132 if (!Aes128GcmEncrypter::IsSupported()) { |
| 133 LOG(INFO) << "AES GCM not supported. Test skipped."; |
| 134 return; |
| 135 } |
| 136 |
| 137 QuicGuid guid(1); |
| 138 IPAddressNumber ip; |
| 139 ParseIPLiteralToNumber("127.0.0.1", &ip); |
| 140 IPEndPoint addr(ip, 0); |
| 141 PacketSavingConnection* client_conn = |
| 142 new PacketSavingConnection(guid, addr, false); |
| 143 PacketSavingConnection* server_conn = |
| 144 new PacketSavingConnection(guid, addr, false); |
| 145 client_conn->AdvanceTime(QuicTime::Delta::FromSeconds(1000000)); |
| 146 server_conn->AdvanceTime(QuicTime::Delta::FromSeconds(1000000)); |
| 147 |
| 148 scoped_ptr<TestSession> client_session(new TestSession(client_conn, true)); |
| 149 scoped_ptr<TestSession> server_session(new TestSession(server_conn, true)); |
| 150 |
| 151 QuicConfig client_config; |
| 152 QuicCryptoClientConfig client_crypto_config; |
| 153 |
| 154 client_config.SetDefaults(); |
| 155 client_crypto_config.SetDefaults(); |
| 156 |
| 157 scoped_ptr<QuicCryptoClientStream> client(new QuicCryptoClientStream( |
| 158 "test.example.com", client_config, client_session.get(), |
| 159 &client_crypto_config)); |
| 160 |
| 161 // Do a first handshake in order to prime the client config with the server's |
| 162 // information. |
| 163 CHECK(client->CryptoConnect()); |
| 164 CHECK_EQ(1u, client_conn->packets_.size()); |
| 165 |
| 166 scoped_ptr<QuicCryptoServerStream> server( |
| 167 new QuicCryptoServerStream(config_, crypto_config_, |
| 168 server_session.get())); |
| 169 |
| 170 CryptoTestUtils::CommunicateHandshakeMessages( |
| 171 client_conn, client.get(), server_conn, server.get()); |
| 172 EXPECT_EQ(2, client->num_sent_client_hellos()); |
| 173 |
| 174 // Now do another handshake, hopefully in 0-RTT. |
| 175 LOG(INFO) << "Resetting for 0-RTT handshake attempt"; |
| 176 |
| 177 client_conn = new PacketSavingConnection(guid, addr, false); |
| 178 server_conn = new PacketSavingConnection(guid, addr, false); |
| 179 // We need to advance time past the strike-server window so that it's |
| 180 // authoritative in this time span. |
| 181 client_conn->AdvanceTime(QuicTime::Delta::FromSeconds(1002000)); |
| 182 server_conn->AdvanceTime(QuicTime::Delta::FromSeconds(1002000)); |
| 183 |
| 184 // This causes the client's nonce to be different and thus stops the |
| 185 // strike-register from rejecting the repeated nonce. |
| 186 client_conn->random_generator()->Reseed(NULL, 0); |
| 187 client_session.reset(new TestSession(client_conn, true)); |
| 188 server_session.reset(new TestSession(server_conn, true)); |
| 189 client.reset(new QuicCryptoClientStream( |
| 190 "test.example.com", client_config, client_session.get(), |
| 191 &client_crypto_config)); |
| 192 server.reset(new QuicCryptoServerStream(config_, crypto_config_, |
| 193 server_session.get())); |
| 194 |
| 195 CHECK(client->CryptoConnect()); |
| 196 |
| 197 CryptoTestUtils::CommunicateHandshakeMessages( |
| 198 client_conn, client.get(), server_conn, server.get()); |
| 199 EXPECT_EQ(1, client->num_sent_client_hellos()); |
| 200 } |
| 201 |
122 TEST_F(QuicCryptoServerStreamTest, MessageAfterHandshake) { | 202 TEST_F(QuicCryptoServerStreamTest, MessageAfterHandshake) { |
123 if (!Aes128GcmEncrypter::IsSupported()) { | 203 if (!Aes128GcmEncrypter::IsSupported()) { |
124 LOG(INFO) << "AES GCM not supported. Test skipped."; | 204 LOG(INFO) << "AES GCM not supported. Test skipped."; |
125 return; | 205 return; |
126 } | 206 } |
127 | 207 |
128 CompleteCryptoHandshake(); | 208 CompleteCryptoHandshake(); |
129 EXPECT_CALL(*connection_, SendConnectionClose( | 209 EXPECT_CALL(*connection_, SendConnectionClose( |
130 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE)); | 210 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE)); |
131 message_.set_tag(kCHLO); | 211 message_.set_tag(kCHLO); |
(...skipping 10 matching lines...) Expand all Loading... |
142 message_.set_tag(kSHLO); | 222 message_.set_tag(kSHLO); |
143 ConstructHandshakeMessage(); | 223 ConstructHandshakeMessage(); |
144 EXPECT_CALL(*connection_, SendConnectionClose( | 224 EXPECT_CALL(*connection_, SendConnectionClose( |
145 QUIC_INVALID_CRYPTO_MESSAGE_TYPE)); | 225 QUIC_INVALID_CRYPTO_MESSAGE_TYPE)); |
146 stream_.ProcessData(message_data_->data(), message_data_->length()); | 226 stream_.ProcessData(message_data_->data(), message_data_->length()); |
147 } | 227 } |
148 | 228 |
149 } // namespace | 229 } // namespace |
150 } // namespace test | 230 } // namespace test |
151 } // namespace net | 231 } // namespace net |
OLD | NEW |