mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/p2p/quic/quicsession.h" |
| 12 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 13 | #include <memory> |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "net/base/ip_endpoint.h" |
| 18 | #include "net/quic/crypto/crypto_server_config_protobuf.h" |
| 19 | #include "net/quic/crypto/quic_random.h" |
| 20 | #include "net/quic/crypto/proof_source.h" |
| 21 | #include "net/quic/crypto/proof_verifier.h" |
| 22 | #include "net/quic/crypto/quic_crypto_client_config.h" |
| 23 | #include "net/quic/crypto/quic_crypto_server_config.h" |
| 24 | #include "net/quic/quic_crypto_client_stream.h" |
| 25 | #include "net/quic/quic_crypto_server_stream.h" |
| 26 | #include "webrtc/base/common.h" |
| 27 | #include "webrtc/base/gunit.h" |
| 28 | #include "webrtc/p2p/base/faketransportcontroller.h" |
| 29 | #include "webrtc/p2p/quic/quicconnectionhelper.h" |
| 30 | #include "webrtc/p2p/quic/reliablequicstream.h" |
| 31 | |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 32 | using net::IPAddress; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 33 | using net::IPEndPoint; |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 34 | using net::PerPacketOptions; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 35 | using net::Perspective; |
| 36 | using net::ProofVerifyContext; |
| 37 | using net::ProofVerifyDetails; |
| 38 | using net::QuicByteCount; |
| 39 | using net::QuicClock; |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 40 | using net::QuicCompressedCertsCache; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 41 | using net::QuicConfig; |
| 42 | using net::QuicConnection; |
| 43 | using net::QuicCryptoClientConfig; |
| 44 | using net::QuicCryptoServerConfig; |
| 45 | using net::QuicCryptoClientStream; |
| 46 | using net::QuicCryptoServerStream; |
| 47 | using net::QuicCryptoStream; |
| 48 | using net::QuicErrorCode; |
| 49 | using net::QuicPacketWriter; |
| 50 | using net::QuicRandom; |
| 51 | using net::QuicServerConfigProtobuf; |
| 52 | using net::QuicServerId; |
| 53 | using net::QuicStreamId; |
| 54 | using net::WriteResult; |
| 55 | using net::WriteStatus; |
| 56 | |
| 57 | using cricket::FakeTransportChannel; |
| 58 | using cricket::QuicConnectionHelper; |
| 59 | using cricket::QuicSession; |
| 60 | using cricket::ReliableQuicStream; |
| 61 | using cricket::TransportChannel; |
| 62 | |
| 63 | using rtc::Thread; |
| 64 | |
| 65 | // Timeout for running asynchronous operations within unit tests. |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 66 | static const int kTimeoutMs = 1000; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 67 | // Testing SpdyPriority value for creating outgoing ReliableQuicStream. |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 68 | static const uint8_t kDefaultPriority = 3; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 69 | // TExport keying material function |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 70 | static const char kExporterLabel[] = "label"; |
| 71 | static const char kExporterContext[] = "context"; |
| 72 | static const size_t kExporterContextLen = sizeof(kExporterContext); |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 73 | // Identifies QUIC server session |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 74 | static const QuicServerId kServerId("www.google.com", 443); |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 75 | |
| 76 | // Used by QuicCryptoServerConfig to provide server credentials, returning a |
| 77 | // canned response equal to |success|. |
| 78 | class FakeProofSource : public net::ProofSource { |
| 79 | public: |
| 80 | explicit FakeProofSource(bool success) : success_(success) {} |
| 81 | |
| 82 | // ProofSource override. |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 83 | bool GetProof(const IPAddress& server_ip, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 84 | const std::string& hostname, |
| 85 | const std::string& server_config, |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 86 | net::QuicVersion quic_version, |
| 87 | base::StringPiece chlo_hash, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 88 | bool ecdsa_ok, |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 89 | scoped_refptr<net::ProofSource::Chain>* out_certs, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 90 | std::string* out_signature, |
| 91 | std::string* out_leaf_cert_sct) override { |
| 92 | if (success_) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 93 | std::vector<std::string> certs; |
| 94 | certs.push_back("Required to establish handshake"); |
| 95 | *out_certs = new ProofSource::Chain(certs); |
| 96 | *out_signature = "Signature"; |
| 97 | *out_leaf_cert_sct = "Time"; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 98 | } |
| 99 | return success_; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | // Whether or not obtaining proof source succeeds. |
| 104 | bool success_; |
| 105 | }; |
| 106 | |
| 107 | // Used by QuicCryptoClientConfig to verify server credentials, returning a |
| 108 | // canned response of QUIC_SUCCESS if |success| is true. |
| 109 | class FakeProofVerifier : public net::ProofVerifier { |
| 110 | public: |
| 111 | explicit FakeProofVerifier(bool success) : success_(success) {} |
| 112 | |
| 113 | // ProofVerifier override |
| 114 | net::QuicAsyncStatus VerifyProof( |
| 115 | const std::string& hostname, |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 116 | const uint16_t port, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 117 | const std::string& server_config, |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 118 | net::QuicVersion quic_version, |
| 119 | base::StringPiece chlo_hash, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 120 | const std::vector<std::string>& certs, |
| 121 | const std::string& cert_sct, |
| 122 | const std::string& signature, |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 123 | const ProofVerifyContext* context, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 124 | std::string* error_details, |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 125 | std::unique_ptr<net::ProofVerifyDetails>* verify_details, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 126 | net::ProofVerifierCallback* callback) override { |
| 127 | return success_ ? net::QUIC_SUCCESS : net::QUIC_FAILURE; |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | // Whether or not proof verification succeeds. |
| 132 | bool success_; |
| 133 | }; |
| 134 | |
| 135 | // Writes QUIC packets to a fake transport channel that simulates a network. |
| 136 | class FakeQuicPacketWriter : public QuicPacketWriter { |
| 137 | public: |
| 138 | explicit FakeQuicPacketWriter(FakeTransportChannel* fake_channel) |
| 139 | : fake_channel_(fake_channel) {} |
| 140 | |
| 141 | // Sends packets across the network. |
| 142 | WriteResult WritePacket(const char* buffer, |
| 143 | size_t buf_len, |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 144 | const IPAddress& self_address, |
| 145 | const IPEndPoint& peer_address, |
| 146 | PerPacketOptions* options) override { |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 147 | rtc::PacketOptions packet_options; |
| 148 | int rv = fake_channel_->SendPacket(buffer, buf_len, packet_options, 0); |
| 149 | net::WriteStatus status; |
| 150 | if (rv > 0) { |
| 151 | status = net::WRITE_STATUS_OK; |
| 152 | } else if (fake_channel_->GetError() == EWOULDBLOCK) { |
| 153 | status = net::WRITE_STATUS_BLOCKED; |
| 154 | } else { |
| 155 | status = net::WRITE_STATUS_ERROR; |
| 156 | } |
| 157 | return net::WriteResult(status, rv); |
| 158 | } |
| 159 | |
| 160 | // Returns true if the writer buffers and subsequently rewrites data |
| 161 | // when an attempt to write results in the underlying socket becoming |
| 162 | // write blocked. |
| 163 | bool IsWriteBlockedDataBuffered() const override { return true; } |
| 164 | |
| 165 | // Returns true if the network socket is not writable. |
| 166 | bool IsWriteBlocked() const override { return !fake_channel_->writable(); } |
| 167 | |
| 168 | // Records that the socket has become writable, for example when an EPOLLOUT |
| 169 | // is received or an asynchronous write completes. |
| 170 | void SetWritable() override { fake_channel_->SetWritable(true); } |
| 171 | |
| 172 | // Returns the maximum size of the packet which can be written using this |
| 173 | // writer for the supplied peer address. This size may actually exceed the |
| 174 | // size of a valid QUIC packet. |
| 175 | QuicByteCount GetMaxPacketSize( |
| 176 | const IPEndPoint& peer_address) const override { |
| 177 | return net::kMaxPacketSize; |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | FakeTransportChannel* fake_channel_; |
| 182 | }; |
| 183 | |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 184 | // Wrapper for QuicSession and transport channel that stores incoming data. |
| 185 | class QuicSessionForTest : public QuicSession { |
| 186 | public: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 187 | QuicSessionForTest(std::unique_ptr<net::QuicConnection> connection, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 188 | const net::QuicConfig& config, |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 189 | std::unique_ptr<FakeTransportChannel> channel) |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 190 | : QuicSession(std::move(connection), config), |
| 191 | channel_(std::move(channel)) { |
| 192 | channel_->SignalReadPacket.connect( |
| 193 | this, &QuicSessionForTest::OnChannelReadPacket); |
| 194 | } |
| 195 | |
| 196 | // Called when channel has packets to read. |
| 197 | void OnChannelReadPacket(TransportChannel* channel, |
| 198 | const char* data, |
| 199 | size_t size, |
| 200 | const rtc::PacketTime& packet_time, |
| 201 | int flags) { |
| 202 | OnReadPacket(data, size); |
| 203 | } |
| 204 | |
| 205 | // Called when peer receives incoming stream from another peer. |
| 206 | void OnIncomingStream(ReliableQuicStream* stream) { |
| 207 | stream->SignalDataReceived.connect(this, |
| 208 | &QuicSessionForTest::OnDataReceived); |
| 209 | last_incoming_stream_ = stream; |
| 210 | } |
| 211 | |
| 212 | // Called when peer has data to read from incoming stream. |
| 213 | void OnDataReceived(net::QuicStreamId id, const char* data, size_t length) { |
| 214 | last_received_data_ = std::string(data, length); |
| 215 | } |
| 216 | |
| 217 | std::string data() { return last_received_data_; } |
| 218 | |
| 219 | bool has_data() { return data().size() > 0; } |
| 220 | |
| 221 | FakeTransportChannel* channel() { return channel_.get(); } |
| 222 | |
| 223 | ReliableQuicStream* incoming_stream() { return last_incoming_stream_; } |
| 224 | |
| 225 | private: |
| 226 | // Transports QUIC packets to/from peer. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 227 | std::unique_ptr<FakeTransportChannel> channel_; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 228 | // Stores data received by peer once it is sent from the other peer. |
| 229 | std::string last_received_data_; |
| 230 | // Handles incoming streams from sender. |
| 231 | ReliableQuicStream* last_incoming_stream_ = nullptr; |
| 232 | }; |
| 233 | |
| 234 | // Simulates data transfer between two peers using QUIC. |
| 235 | class QuicSessionTest : public ::testing::Test, |
| 236 | public QuicCryptoClientStream::ProofHandler { |
| 237 | public: |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 238 | QuicSessionTest() |
| 239 | : quic_helper_(rtc::Thread::Current()), |
| 240 | quic_compressed_certs_cache_( |
| 241 | QuicCompressedCertsCache::kQuicCompressedCertsCacheSize) {} |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 242 | |
| 243 | // Instantiates |client_peer_| and |server_peer_|. |
| 244 | void CreateClientAndServerSessions(); |
| 245 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 246 | std::unique_ptr<QuicSessionForTest> CreateSession( |
| 247 | std::unique_ptr<FakeTransportChannel> channel, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 248 | Perspective perspective); |
| 249 | |
| 250 | QuicCryptoClientStream* CreateCryptoClientStream(QuicSessionForTest* session, |
| 251 | bool handshake_success); |
| 252 | QuicCryptoServerStream* CreateCryptoServerStream(QuicSessionForTest* session, |
| 253 | bool handshake_success); |
| 254 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 255 | std::unique_ptr<QuicConnection> CreateConnection( |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 256 | FakeTransportChannel* channel, |
| 257 | Perspective perspective); |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 258 | |
| 259 | void StartHandshake(bool client_handshake_success, |
| 260 | bool server_handshake_success); |
| 261 | |
| 262 | // Test handshake establishment and sending/receiving of data. |
| 263 | void TestStreamConnection(QuicSessionForTest* from_session, |
| 264 | QuicSessionForTest* to_session); |
| 265 | // Test that client and server are not connected after handshake failure. |
| 266 | void TestDisconnectAfterFailedHandshake(); |
| 267 | |
| 268 | // QuicCryptoClientStream::ProofHelper overrides. |
| 269 | void OnProofValid( |
| 270 | const QuicCryptoClientConfig::CachedState& cached) override {} |
| 271 | void OnProofVerifyDetailsAvailable( |
| 272 | const ProofVerifyDetails& verify_details) override {} |
| 273 | |
| 274 | protected: |
| 275 | QuicConnectionHelper quic_helper_; |
| 276 | QuicConfig config_; |
| 277 | QuicClock clock_; |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 278 | QuicCompressedCertsCache quic_compressed_certs_cache_; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 279 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 280 | std::unique_ptr<QuicSessionForTest> client_peer_; |
| 281 | std::unique_ptr<QuicSessionForTest> server_peer_; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | // Initializes "client peer" who begins crypto handshake and "server peer" who |
| 285 | // establishes encryption with client. |
| 286 | void QuicSessionTest::CreateClientAndServerSessions() { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 287 | std::unique_ptr<FakeTransportChannel> channel1( |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 288 | new FakeTransportChannel("channel1", 0)); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 289 | std::unique_ptr<FakeTransportChannel> channel2( |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 290 | new FakeTransportChannel("channel2", 0)); |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 291 | |
| 292 | // Prevent channel1->OnReadPacket and channel2->OnReadPacket from calling |
| 293 | // themselves in a loop, which causes to future packets to be recursively |
| 294 | // consumed while the current thread blocks consumption of current ones. |
| 295 | channel2->SetAsync(true); |
| 296 | |
| 297 | // Configure peers to send packets to each other. |
| 298 | channel1->Connect(); |
| 299 | channel2->Connect(); |
| 300 | channel1->SetDestination(channel2.get()); |
| 301 | |
| 302 | client_peer_ = CreateSession(std::move(channel1), Perspective::IS_CLIENT); |
| 303 | server_peer_ = CreateSession(std::move(channel2), Perspective::IS_SERVER); |
| 304 | } |
| 305 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 306 | std::unique_ptr<QuicSessionForTest> QuicSessionTest::CreateSession( |
| 307 | std::unique_ptr<FakeTransportChannel> channel, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 308 | Perspective perspective) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 309 | std::unique_ptr<QuicConnection> quic_connection = |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 310 | CreateConnection(channel.get(), perspective); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 311 | return std::unique_ptr<QuicSessionForTest>(new QuicSessionForTest( |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 312 | std::move(quic_connection), config_, std::move(channel))); |
| 313 | } |
| 314 | |
| 315 | QuicCryptoClientStream* QuicSessionTest::CreateCryptoClientStream( |
| 316 | QuicSessionForTest* session, |
| 317 | bool handshake_success) { |
| 318 | QuicCryptoClientConfig* client_config = |
| 319 | new QuicCryptoClientConfig(new FakeProofVerifier(handshake_success)); |
| 320 | return new QuicCryptoClientStream( |
| 321 | kServerId, session, new ProofVerifyContext(), client_config, this); |
| 322 | } |
| 323 | |
| 324 | QuicCryptoServerStream* QuicSessionTest::CreateCryptoServerStream( |
| 325 | QuicSessionForTest* session, |
| 326 | bool handshake_success) { |
| 327 | QuicCryptoServerConfig* server_config = |
| 328 | new QuicCryptoServerConfig("TESTING", QuicRandom::GetInstance(), |
| 329 | new FakeProofSource(handshake_success)); |
| 330 | // Provide server with serialized config string to prove ownership. |
| 331 | QuicCryptoServerConfig::ConfigOptions options; |
| 332 | QuicServerConfigProtobuf* primary_config = server_config->GenerateConfig( |
| 333 | QuicRandom::GetInstance(), &clock_, options); |
| 334 | server_config->AddConfig(primary_config, clock_.WallNow()); |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 335 | bool use_stateless_rejects_if_peer_supported = false; |
| 336 | return new QuicCryptoServerStream( |
| 337 | server_config, &quic_compressed_certs_cache_, |
| 338 | use_stateless_rejects_if_peer_supported, session); |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 339 | } |
| 340 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 341 | std::unique_ptr<QuicConnection> QuicSessionTest::CreateConnection( |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 342 | FakeTransportChannel* channel, |
| 343 | Perspective perspective) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 344 | FakeQuicPacketWriter* writer = new FakeQuicPacketWriter(channel); |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 345 | |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 346 | IPAddress ip(0, 0, 0, 0); |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 347 | bool owns_writer = true; |
| 348 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 349 | return std::unique_ptr<QuicConnection>(new QuicConnection( |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 350 | 0, net::IPEndPoint(ip, 0), &quic_helper_, writer, owns_writer, |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 351 | perspective, net::QuicSupportedVersions())); |
| 352 | } |
| 353 | |
| 354 | void QuicSessionTest::StartHandshake(bool client_handshake_success, |
| 355 | bool server_handshake_success) { |
| 356 | server_peer_->StartServerHandshake( |
| 357 | CreateCryptoServerStream(server_peer_.get(), server_handshake_success)); |
| 358 | client_peer_->StartClientHandshake( |
| 359 | CreateCryptoClientStream(client_peer_.get(), client_handshake_success)); |
| 360 | } |
| 361 | |
| 362 | void QuicSessionTest::TestStreamConnection(QuicSessionForTest* from_session, |
| 363 | QuicSessionForTest* to_session) { |
| 364 | // Wait for crypto handshake to finish then check if encryption established. |
| 365 | ASSERT_TRUE_WAIT(from_session->IsCryptoHandshakeConfirmed() && |
| 366 | to_session->IsCryptoHandshakeConfirmed(), |
| 367 | kTimeoutMs); |
| 368 | |
| 369 | ASSERT_TRUE(from_session->IsEncryptionEstablished()); |
| 370 | ASSERT_TRUE(to_session->IsEncryptionEstablished()); |
| 371 | |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame^] | 372 | std::string from_key; |
| 373 | std::string to_key; |
mikescarlett | cd0e475 | 2016-02-08 17:35:47 -0800 | [diff] [blame] | 374 | |
| 375 | bool from_success = from_session->ExportKeyingMaterial( |
| 376 | kExporterLabel, kExporterContext, kExporterContextLen, &from_key); |
| 377 | ASSERT_TRUE(from_success); |
| 378 | bool to_success = to_session->ExportKeyingMaterial( |
| 379 | kExporterLabel, kExporterContext, kExporterContextLen, &to_key); |
| 380 | ASSERT_TRUE(to_success); |
| 381 | |
| 382 | EXPECT_EQ(from_key.size(), kExporterContextLen); |
| 383 | EXPECT_EQ(from_key, to_key); |
| 384 | |
| 385 | // Now we can establish encrypted outgoing stream. |
| 386 | ReliableQuicStream* outgoing_stream = |
| 387 | from_session->CreateOutgoingDynamicStream(kDefaultPriority); |
| 388 | ASSERT_NE(nullptr, outgoing_stream); |
| 389 | EXPECT_TRUE(from_session->HasOpenDynamicStreams()); |
| 390 | |
| 391 | outgoing_stream->SignalDataReceived.connect( |
| 392 | from_session, &QuicSessionForTest::OnDataReceived); |
| 393 | to_session->SignalIncomingStream.connect( |
| 394 | to_session, &QuicSessionForTest::OnIncomingStream); |
| 395 | |
| 396 | // Send a test message from peer 1 to peer 2. |
| 397 | const char kTestMessage[] = "Hello, World!"; |
| 398 | outgoing_stream->Write(kTestMessage, strlen(kTestMessage)); |
| 399 | |
| 400 | // Wait for peer 2 to receive messages. |
| 401 | ASSERT_TRUE_WAIT(to_session->has_data(), kTimeoutMs); |
| 402 | |
| 403 | ReliableQuicStream* incoming = to_session->incoming_stream(); |
| 404 | ASSERT_TRUE(incoming); |
| 405 | EXPECT_TRUE(to_session->HasOpenDynamicStreams()); |
| 406 | |
| 407 | EXPECT_EQ(to_session->data(), kTestMessage); |
| 408 | |
| 409 | // Send a test message from peer 2 to peer 1. |
| 410 | const char kTestResponse[] = "Response"; |
| 411 | incoming->Write(kTestResponse, strlen(kTestResponse)); |
| 412 | |
| 413 | // Wait for peer 1 to receive messages. |
| 414 | ASSERT_TRUE_WAIT(from_session->has_data(), kTimeoutMs); |
| 415 | |
| 416 | EXPECT_EQ(from_session->data(), kTestResponse); |
| 417 | } |
| 418 | |
| 419 | // Client and server should disconnect when proof verification fails. |
| 420 | void QuicSessionTest::TestDisconnectAfterFailedHandshake() { |
| 421 | EXPECT_TRUE_WAIT(!client_peer_->connection()->connected(), kTimeoutMs); |
| 422 | EXPECT_TRUE_WAIT(!server_peer_->connection()->connected(), kTimeoutMs); |
| 423 | |
| 424 | EXPECT_FALSE(client_peer_->IsEncryptionEstablished()); |
| 425 | EXPECT_FALSE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 426 | |
| 427 | EXPECT_FALSE(server_peer_->IsEncryptionEstablished()); |
| 428 | EXPECT_FALSE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 429 | } |
| 430 | |
| 431 | // Establish encryption then send message from client to server. |
| 432 | TEST_F(QuicSessionTest, ClientToServer) { |
| 433 | CreateClientAndServerSessions(); |
| 434 | StartHandshake(true, true); |
| 435 | TestStreamConnection(client_peer_.get(), server_peer_.get()); |
| 436 | } |
| 437 | |
| 438 | // Establish encryption then send message from server to client. |
| 439 | TEST_F(QuicSessionTest, ServerToClient) { |
| 440 | CreateClientAndServerSessions(); |
| 441 | StartHandshake(true, true); |
| 442 | TestStreamConnection(server_peer_.get(), client_peer_.get()); |
| 443 | } |
| 444 | |
| 445 | // Make client fail to verify proof from server. |
| 446 | TEST_F(QuicSessionTest, ClientRejection) { |
| 447 | CreateClientAndServerSessions(); |
| 448 | StartHandshake(false, true); |
| 449 | TestDisconnectAfterFailedHandshake(); |
| 450 | } |
| 451 | |
| 452 | // Make server fail to give proof to client. |
| 453 | TEST_F(QuicSessionTest, ServerRejection) { |
| 454 | CreateClientAndServerSessions(); |
| 455 | StartHandshake(true, false); |
| 456 | TestDisconnectAfterFailedHandshake(); |
| 457 | } |
| 458 | |
| 459 | // Test that data streams are not created before handshake. |
| 460 | TEST_F(QuicSessionTest, CannotCreateDataStreamBeforeHandshake) { |
| 461 | CreateClientAndServerSessions(); |
| 462 | EXPECT_EQ(nullptr, server_peer_->CreateOutgoingDynamicStream(5)); |
| 463 | EXPECT_EQ(nullptr, client_peer_->CreateOutgoingDynamicStream(5)); |
| 464 | } |
mikescarlett | 18b67a5 | 2016-04-11 16:56:23 -0700 | [diff] [blame] | 465 | |
| 466 | // Test that closing a QUIC stream causes the QuicSession to remove it. |
| 467 | TEST_F(QuicSessionTest, CloseQuicStream) { |
| 468 | CreateClientAndServerSessions(); |
| 469 | StartHandshake(true, true); |
| 470 | ASSERT_TRUE_WAIT(client_peer_->IsCryptoHandshakeConfirmed() && |
| 471 | server_peer_->IsCryptoHandshakeConfirmed(), |
| 472 | kTimeoutMs); |
| 473 | ReliableQuicStream* stream = client_peer_->CreateOutgoingDynamicStream(5); |
| 474 | ASSERT_NE(nullptr, stream); |
| 475 | EXPECT_FALSE(client_peer_->IsClosedStream(stream->id())); |
| 476 | stream->Close(); |
| 477 | EXPECT_TRUE(client_peer_->IsClosedStream(stream->id())); |
| 478 | } |