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