OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/tools/quic/test_tools/quic_test_server.h" | 5 #include "net/tools/quic/test_tools/quic_test_server.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/quic/chromium/quic_chromium_connection_helper.h" | 13 #include "net/quic/chromium/quic_chromium_connection_helper.h" |
14 #include "net/quic/core/crypto/crypto_handshake.h" | 14 #include "net/quic/core/crypto/crypto_handshake.h" |
15 #include "net/quic/core/crypto/quic_crypto_server_config.h" | 15 #include "net/quic/core/crypto/quic_crypto_server_config.h" |
16 #include "net/quic/core/crypto/quic_random.h" | 16 #include "net/quic/core/crypto/quic_random.h" |
17 #include "net/quic/core/quic_config.h" | 17 #include "net/quic/core/quic_config.h" |
18 #include "net/quic/core/quic_connection.h" | 18 #include "net/quic/core/quic_connection.h" |
19 #include "net/quic/core/quic_packet_writer.h" | 19 #include "net/quic/core/quic_packet_writer.h" |
20 #include "net/quic/core/quic_protocol.h" | 20 #include "net/quic/core/quic_packets.h" |
21 #include "net/tools/quic/quic_epoll_alarm_factory.h" | 21 #include "net/tools/quic/quic_epoll_alarm_factory.h" |
22 #include "net/tools/quic/quic_epoll_connection_helper.h" | 22 #include "net/tools/quic/quic_epoll_connection_helper.h" |
23 #include "net/tools/quic/quic_simple_crypto_server_stream_helper.h" | 23 #include "net/tools/quic/quic_simple_crypto_server_stream_helper.h" |
24 #include "net/tools/quic/quic_simple_dispatcher.h" | 24 #include "net/tools/quic/quic_simple_dispatcher.h" |
25 #include "net/tools/quic/quic_simple_server_session.h" | 25 #include "net/tools/quic/quic_simple_server_session.h" |
26 #include "net/tools/quic/quic_simple_server_stream.h" | 26 #include "net/tools/quic/quic_simple_server_stream.h" |
27 | 27 |
28 namespace net { | 28 namespace net { |
29 namespace test { | 29 namespace test { |
30 | 30 |
31 class CustomStreamSession : public QuicSimpleServerSession { | 31 class CustomStreamSession : public QuicSimpleServerSession { |
32 public: | 32 public: |
33 CustomStreamSession( | 33 CustomStreamSession( |
34 const QuicConfig& config, | 34 const QuicConfig& config, |
35 QuicConnection* connection, | 35 QuicConnection* connection, |
36 QuicSession::Visitor* visitor, | 36 QuicSession::Visitor* visitor, |
37 QuicCryptoServerStream::Helper* helper, | 37 QuicCryptoServerStream::Helper* helper, |
38 const QuicCryptoServerConfig* crypto_config, | 38 const QuicCryptoServerConfig* crypto_config, |
39 QuicCompressedCertsCache* compressed_certs_cache, | 39 QuicCompressedCertsCache* compressed_certs_cache, |
40 QuicTestServer::StreamFactory* stream_factory, | 40 QuicTestServer::StreamFactory* stream_factory, |
41 QuicTestServer::CryptoStreamFactory* crypto_stream_factory, | 41 QuicTestServer::CryptoStreamFactory* crypto_stream_factory, |
42 QuicInMemoryCache* in_memory_cache) | 42 QuicHttpResponseCache* response_cache) |
43 : QuicSimpleServerSession(config, | 43 : QuicSimpleServerSession(config, |
44 connection, | 44 connection, |
45 visitor, | 45 visitor, |
46 helper, | 46 helper, |
47 crypto_config, | 47 crypto_config, |
48 compressed_certs_cache, | 48 compressed_certs_cache, |
49 in_memory_cache), | 49 response_cache), |
50 stream_factory_(stream_factory), | 50 stream_factory_(stream_factory), |
51 crypto_stream_factory_(crypto_stream_factory) {} | 51 crypto_stream_factory_(crypto_stream_factory) {} |
52 | 52 |
53 QuicSpdyStream* CreateIncomingDynamicStream(QuicStreamId id) override { | 53 QuicSpdyStream* CreateIncomingDynamicStream(QuicStreamId id) override { |
54 if (!ShouldCreateIncomingDynamicStream(id)) { | 54 if (!ShouldCreateIncomingDynamicStream(id)) { |
55 return nullptr; | 55 return nullptr; |
56 } | 56 } |
57 if (stream_factory_) { | 57 if (stream_factory_) { |
58 QuicSpdyStream* stream = | 58 QuicSpdyStream* stream = |
59 stream_factory_->CreateStream(id, this, in_memory_cache()); | 59 stream_factory_->CreateStream(id, this, response_cache()); |
60 ActivateStream(base::WrapUnique(stream)); | 60 ActivateStream(base::WrapUnique(stream)); |
61 return stream; | 61 return stream; |
62 } | 62 } |
63 return QuicSimpleServerSession::CreateIncomingDynamicStream(id); | 63 return QuicSimpleServerSession::CreateIncomingDynamicStream(id); |
64 } | 64 } |
65 | 65 |
66 QuicCryptoServerStreamBase* CreateQuicCryptoServerStream( | 66 QuicCryptoServerStreamBase* CreateQuicCryptoServerStream( |
67 const QuicCryptoServerConfig* crypto_config, | 67 const QuicCryptoServerConfig* crypto_config, |
68 QuicCompressedCertsCache* compressed_certs_cache) override { | 68 QuicCompressedCertsCache* compressed_certs_cache) override { |
69 if (crypto_stream_factory_) { | 69 if (crypto_stream_factory_) { |
(...skipping 10 matching lines...) Expand all Loading... |
80 | 80 |
81 class QuicTestDispatcher : public QuicSimpleDispatcher { | 81 class QuicTestDispatcher : public QuicSimpleDispatcher { |
82 public: | 82 public: |
83 QuicTestDispatcher( | 83 QuicTestDispatcher( |
84 const QuicConfig& config, | 84 const QuicConfig& config, |
85 const QuicCryptoServerConfig* crypto_config, | 85 const QuicCryptoServerConfig* crypto_config, |
86 QuicVersionManager* version_manager, | 86 QuicVersionManager* version_manager, |
87 std::unique_ptr<QuicConnectionHelperInterface> helper, | 87 std::unique_ptr<QuicConnectionHelperInterface> helper, |
88 std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, | 88 std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, |
89 std::unique_ptr<QuicAlarmFactory> alarm_factory, | 89 std::unique_ptr<QuicAlarmFactory> alarm_factory, |
90 QuicInMemoryCache* in_memory_cache) | 90 QuicHttpResponseCache* response_cache) |
91 : QuicSimpleDispatcher(config, | 91 : QuicSimpleDispatcher(config, |
92 crypto_config, | 92 crypto_config, |
93 version_manager, | 93 version_manager, |
94 std::move(helper), | 94 std::move(helper), |
95 std::move(session_helper), | 95 std::move(session_helper), |
96 std::move(alarm_factory), | 96 std::move(alarm_factory), |
97 in_memory_cache), | 97 response_cache), |
98 session_factory_(nullptr), | 98 session_factory_(nullptr), |
99 stream_factory_(nullptr), | 99 stream_factory_(nullptr), |
100 crypto_stream_factory_(nullptr) {} | 100 crypto_stream_factory_(nullptr) {} |
101 | 101 |
102 QuicServerSessionBase* CreateQuicSession( | 102 QuicServerSessionBase* CreateQuicSession( |
103 QuicConnectionId id, | 103 QuicConnectionId id, |
104 const QuicSocketAddress& client) override { | 104 const QuicSocketAddress& client) override { |
105 base::AutoLock lock(factory_lock_); | 105 base::AutoLock lock(factory_lock_); |
106 if (session_factory_ == nullptr && stream_factory_ == nullptr && | 106 if (session_factory_ == nullptr && stream_factory_ == nullptr && |
107 crypto_stream_factory_ == nullptr) { | 107 crypto_stream_factory_ == nullptr) { |
108 return QuicSimpleDispatcher::CreateQuicSession(id, client); | 108 return QuicSimpleDispatcher::CreateQuicSession(id, client); |
109 } | 109 } |
110 QuicConnection* connection = new QuicConnection( | 110 QuicConnection* connection = new QuicConnection( |
111 id, client, helper(), alarm_factory(), CreatePerConnectionWriter(), | 111 id, client, helper(), alarm_factory(), CreatePerConnectionWriter(), |
112 /* owns_writer= */ true, Perspective::IS_SERVER, | 112 /* owns_writer= */ true, Perspective::IS_SERVER, |
113 GetSupportedVersions()); | 113 GetSupportedVersions()); |
114 | 114 |
115 QuicServerSessionBase* session = nullptr; | 115 QuicServerSessionBase* session = nullptr; |
116 if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { | 116 if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { |
117 session = new CustomStreamSession( | 117 session = new CustomStreamSession( |
118 config(), connection, this, session_helper(), crypto_config(), | 118 config(), connection, this, session_helper(), crypto_config(), |
119 compressed_certs_cache(), stream_factory_, crypto_stream_factory_, | 119 compressed_certs_cache(), stream_factory_, crypto_stream_factory_, |
120 in_memory_cache()); | 120 response_cache()); |
121 } else { | 121 } else { |
122 session = session_factory_->CreateSession( | 122 session = session_factory_->CreateSession( |
123 config(), connection, this, session_helper(), crypto_config(), | 123 config(), connection, this, session_helper(), crypto_config(), |
124 compressed_certs_cache(), in_memory_cache()); | 124 compressed_certs_cache(), response_cache()); |
125 } | 125 } |
126 session->Initialize(); | 126 session->Initialize(); |
127 return session; | 127 return session; |
128 } | 128 } |
129 | 129 |
130 void SetSessionFactory(QuicTestServer::SessionFactory* factory) { | 130 void SetSessionFactory(QuicTestServer::SessionFactory* factory) { |
131 base::AutoLock lock(factory_lock_); | 131 base::AutoLock lock(factory_lock_); |
132 DCHECK(session_factory_ == nullptr); | 132 DCHECK(session_factory_ == nullptr); |
133 DCHECK(stream_factory_ == nullptr); | 133 DCHECK(stream_factory_ == nullptr); |
134 DCHECK(crypto_stream_factory_ == nullptr); | 134 DCHECK(crypto_stream_factory_ == nullptr); |
(...skipping 15 matching lines...) Expand all Loading... |
150 } | 150 } |
151 | 151 |
152 private: | 152 private: |
153 base::Lock factory_lock_; | 153 base::Lock factory_lock_; |
154 QuicTestServer::SessionFactory* session_factory_; // Not owned. | 154 QuicTestServer::SessionFactory* session_factory_; // Not owned. |
155 QuicTestServer::StreamFactory* stream_factory_; // Not owned. | 155 QuicTestServer::StreamFactory* stream_factory_; // Not owned. |
156 QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. | 156 QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. |
157 }; | 157 }; |
158 | 158 |
159 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source, | 159 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source, |
160 QuicInMemoryCache* in_memory_cache) | 160 QuicHttpResponseCache* response_cache) |
161 : QuicServer(std::move(proof_source), in_memory_cache) {} | 161 : QuicServer(std::move(proof_source), response_cache) {} |
162 | 162 |
163 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source, | 163 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source, |
164 const QuicConfig& config, | 164 const QuicConfig& config, |
165 const QuicVersionVector& supported_versions, | 165 const QuicVersionVector& supported_versions, |
166 QuicInMemoryCache* in_memory_cache) | 166 QuicHttpResponseCache* response_cache) |
167 : QuicServer(std::move(proof_source), | 167 : QuicServer(std::move(proof_source), |
168 config, | 168 config, |
169 QuicCryptoServerConfig::ConfigOptions(), | 169 QuicCryptoServerConfig::ConfigOptions(), |
170 supported_versions, | 170 supported_versions, |
171 in_memory_cache) {} | 171 response_cache) {} |
172 | 172 |
173 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { | 173 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { |
174 return new QuicTestDispatcher( | 174 return new QuicTestDispatcher( |
175 config(), &crypto_config(), version_manager(), | 175 config(), &crypto_config(), version_manager(), |
176 std::unique_ptr<QuicEpollConnectionHelper>(new QuicEpollConnectionHelper( | 176 std::unique_ptr<QuicEpollConnectionHelper>(new QuicEpollConnectionHelper( |
177 epoll_server(), QuicAllocator::BUFFER_POOL)), | 177 epoll_server(), QuicAllocator::BUFFER_POOL)), |
178 std::unique_ptr<QuicCryptoServerStream::Helper>( | 178 std::unique_ptr<QuicCryptoServerStream::Helper>( |
179 new QuicSimpleCryptoServerStreamHelper(QuicRandom::GetInstance())), | 179 new QuicSimpleCryptoServerStreamHelper(QuicRandom::GetInstance())), |
180 std::unique_ptr<QuicEpollAlarmFactory>( | 180 std::unique_ptr<QuicEpollAlarmFactory>( |
181 new QuicEpollAlarmFactory(epoll_server())), | 181 new QuicEpollAlarmFactory(epoll_server())), |
182 in_memory_cache()); | 182 response_cache()); |
183 } | 183 } |
184 | 184 |
185 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { | 185 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { |
186 DCHECK(dispatcher()); | 186 DCHECK(dispatcher()); |
187 static_cast<QuicTestDispatcher*>(dispatcher())->SetSessionFactory(factory); | 187 static_cast<QuicTestDispatcher*>(dispatcher())->SetSessionFactory(factory); |
188 } | 188 } |
189 | 189 |
190 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { | 190 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { |
191 static_cast<QuicTestDispatcher*>(dispatcher())->SetStreamFactory(factory); | 191 static_cast<QuicTestDispatcher*>(dispatcher())->SetStreamFactory(factory); |
192 } | 192 } |
193 | 193 |
194 void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { | 194 void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { |
195 static_cast<QuicTestDispatcher*>(dispatcher()) | 195 static_cast<QuicTestDispatcher*>(dispatcher()) |
196 ->SetCryptoStreamFactory(factory); | 196 ->SetCryptoStreamFactory(factory); |
197 } | 197 } |
198 | 198 |
199 /////////////////////////// TEST SESSIONS /////////////////////////////// | 199 /////////////////////////// TEST SESSIONS /////////////////////////////// |
200 | 200 |
201 ImmediateGoAwaySession::ImmediateGoAwaySession( | 201 ImmediateGoAwaySession::ImmediateGoAwaySession( |
202 const QuicConfig& config, | 202 const QuicConfig& config, |
203 QuicConnection* connection, | 203 QuicConnection* connection, |
204 QuicSession::Visitor* visitor, | 204 QuicSession::Visitor* visitor, |
205 QuicCryptoServerStream::Helper* helper, | 205 QuicCryptoServerStream::Helper* helper, |
206 const QuicCryptoServerConfig* crypto_config, | 206 const QuicCryptoServerConfig* crypto_config, |
207 QuicCompressedCertsCache* compressed_certs_cache, | 207 QuicCompressedCertsCache* compressed_certs_cache, |
208 QuicInMemoryCache* in_memory_cache) | 208 QuicHttpResponseCache* response_cache) |
209 : QuicSimpleServerSession(config, | 209 : QuicSimpleServerSession(config, |
210 connection, | 210 connection, |
211 visitor, | 211 visitor, |
212 helper, | 212 helper, |
213 crypto_config, | 213 crypto_config, |
214 compressed_certs_cache, | 214 compressed_certs_cache, |
215 in_memory_cache) {} | 215 response_cache) {} |
216 | 216 |
217 void ImmediateGoAwaySession::OnStreamFrame(const QuicStreamFrame& frame) { | 217 void ImmediateGoAwaySession::OnStreamFrame(const QuicStreamFrame& frame) { |
218 SendGoAway(QUIC_PEER_GOING_AWAY, ""); | 218 SendGoAway(QUIC_PEER_GOING_AWAY, ""); |
219 QuicSimpleServerSession::OnStreamFrame(frame); | 219 QuicSimpleServerSession::OnStreamFrame(frame); |
220 } | 220 } |
221 | 221 |
222 } // namespace test | 222 } // namespace test |
223 } // namespace net | 223 } // namespace net |
OLD | NEW |