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/quictransportchannel.h"
12 
13 #include <memory>
14 #include <set>
15 #include <string>
16 #include <vector>
17 
18 #include "webrtc/base/common.h"
19 #include "webrtc/base/gunit.h"
20 #include "webrtc/base/sslidentity.h"
21 #include "webrtc/p2p/base/faketransportcontroller.h"
22 
23 using cricket::ConnectionRole;
24 using cricket::IceRole;
25 using cricket::QuicTransportChannel;
26 using cricket::ReliableQuicStream;
27 using cricket::TransportChannel;
28 using cricket::TransportDescription;
29 
30 // Timeout in milliseconds for asynchronous operations in unit tests.
31 static const int kTimeoutMs = 1000;
32 
33 // Export keying material parameters.
34 static const char kExporterLabel[] = "label";
35 static const uint8_t kExporterContext[] = "context";
36 static const size_t kExporterContextLength = sizeof(kExporterContext);
37 static const size_t kOutputKeyLength = 20;
38 
39 // Packet size for SRTP.
40 static const size_t kPacketSize = 100;
41 
42 // Indicates ICE channel has no write error.
43 static const int kNoWriteError = 0;
44 
45 // ICE parameters.
46 static const char kIceUfrag[] = "TESTICEUFRAG0001";
47 static const char kIcePwd[] = "TESTICEPWD00000000000001";
48 
49 // QUIC packet parameters.
50 static const net::IPAddress kIpAddress(0, 0, 0, 0);
51 static const net::IPEndPoint kIpEndpoint(kIpAddress, 0);
52 
53 // Detects incoming RTP packets.
IsRtpLeadByte(uint8_t b)54 static bool IsRtpLeadByte(uint8_t b) {
55   return (b & 0xC0) == 0x80;
56 }
57 
58 // Maps SSL role to ICE connection role. The peer with a client role is assumed
59 // to be the one who initiates the connection.
SslRoleToConnectionRole(rtc::SSLRole ssl_role)60 static ConnectionRole SslRoleToConnectionRole(rtc::SSLRole ssl_role) {
61   return (ssl_role == rtc::SSL_CLIENT) ? cricket::CONNECTIONROLE_ACTIVE
62                                        : cricket::CONNECTIONROLE_PASSIVE;
63 }
64 
65 // Allows cricket::FakeTransportChannel to simulate write blocked
66 // and write error states.
67 // TODO(mikescarlett): Add this functionality to cricket::FakeTransportChannel.
68 class FailableTransportChannel : public cricket::FakeTransportChannel {
69  public:
FailableTransportChannel(const std::string & name,int component)70   FailableTransportChannel(const std::string& name, int component)
71       : cricket::FakeTransportChannel(name, component), error_(kNoWriteError) {}
GetError()72   int GetError() override { return error_; }
SetError(int error)73   void SetError(int error) { error_ = error; }
SendPacket(const char * data,size_t len,const rtc::PacketOptions & options,int flags)74   int SendPacket(const char* data,
75                  size_t len,
76                  const rtc::PacketOptions& options,
77                  int flags) override {
78     if (error_ == kNoWriteError) {
79       return cricket::FakeTransportChannel::SendPacket(data, len, options,
80                                                        flags);
81     }
82     return -1;
83   }
84 
85  private:
86   int error_;
87 };
88 
89 // Peer who establishes a handshake using a QuicTransportChannel, which wraps
90 // a FailableTransportChannel to simulate network connectivity and ICE
91 // negotiation.
92 class QuicTestPeer : public sigslot::has_slots<> {
93  public:
QuicTestPeer(const std::string & name)94   explicit QuicTestPeer(const std::string& name)
95       : name_(name),
96         bytes_sent_(0),
97         ice_channel_(new FailableTransportChannel(name_, 0)),
98         quic_channel_(ice_channel_),
99         incoming_stream_count_(0) {
100     quic_channel_.SignalReadPacket.connect(
101         this, &QuicTestPeer::OnTransportChannelReadPacket);
102     quic_channel_.SignalIncomingStream.connect(this,
103                                                &QuicTestPeer::OnIncomingStream);
104     quic_channel_.SignalClosed.connect(this, &QuicTestPeer::OnClosed);
105     ice_channel_->SetAsync(true);
106     rtc::scoped_refptr<rtc::RTCCertificate> local_cert =
107         rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
108             rtc::SSLIdentity::Generate(name_, rtc::KT_DEFAULT)));
109     quic_channel_.SetLocalCertificate(local_cert);
110     local_fingerprint_.reset(CreateFingerprint(local_cert.get()));
111   }
112 
113   // Connects |ice_channel_| to that of the other peer.
Connect(QuicTestPeer * other_peer)114   void Connect(QuicTestPeer* other_peer) {
115     ice_channel_->SetDestination(other_peer->ice_channel_);
116   }
117 
118   // Disconnects |ice_channel_|.
Disconnect()119   void Disconnect() { ice_channel_->SetDestination(nullptr); }
120 
121   // Generates ICE credentials and passes them to |quic_channel_|.
SetIceParameters(IceRole local_ice_role,ConnectionRole local_connection_role,ConnectionRole remote_connection_role,rtc::SSLFingerprint * remote_fingerprint)122   void SetIceParameters(IceRole local_ice_role,
123                         ConnectionRole local_connection_role,
124                         ConnectionRole remote_connection_role,
125                         rtc::SSLFingerprint* remote_fingerprint) {
126     quic_channel_.SetIceRole(local_ice_role);
127     quic_channel_.SetIceTiebreaker(
128         (local_ice_role == cricket::ICEROLE_CONTROLLING) ? 1 : 2);
129 
130     TransportDescription local_desc(
131         std::vector<std::string>(), kIceUfrag, kIcePwd, cricket::ICEMODE_FULL,
132         local_connection_role, local_fingerprint_.get());
133     TransportDescription remote_desc(
134         std::vector<std::string>(), kIceUfrag, kIcePwd, cricket::ICEMODE_FULL,
135         remote_connection_role, remote_fingerprint);
136 
137     quic_channel_.SetIceParameters(local_desc.GetIceParameters());
138     quic_channel_.SetRemoteIceParameters(remote_desc.GetIceParameters());
139   }
140 
141   // Creates fingerprint from certificate.
CreateFingerprint(rtc::RTCCertificate * cert)142   rtc::SSLFingerprint* CreateFingerprint(rtc::RTCCertificate* cert) {
143     std::string digest_algorithm;
144     bool get_digest_algorithm =
145         cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm);
146     if (!get_digest_algorithm || digest_algorithm.empty()) {
147       return nullptr;
148     }
149     std::unique_ptr<rtc::SSLFingerprint> fingerprint(
150         rtc::SSLFingerprint::Create(digest_algorithm, cert->identity()));
151     if (digest_algorithm != rtc::DIGEST_SHA_256) {
152       return nullptr;
153     }
154     return fingerprint.release();
155   }
156 
157   // Sends SRTP packet to the other peer via |quic_channel_|.
SendSrtpPacket()158   int SendSrtpPacket() {
159     char packet[kPacketSize];
160     packet[0] = 0x80;  // Make the packet header look like RTP.
161     int rv = quic_channel_.SendPacket(
162         &packet[0], kPacketSize, rtc::PacketOptions(), cricket::PF_SRTP_BYPASS);
163     bytes_sent_ += rv;
164     return rv;
165   }
166 
167   // Sends a non-SRTP packet with the PF_SRTP_BYPASS flag via |quic_channel_|.
SendInvalidSrtpPacket()168   int SendInvalidSrtpPacket() {
169     char packet[kPacketSize];
170     // Fill the packet with 0 to form an invalid SRTP packet.
171     memset(packet, 0, kPacketSize);
172     return quic_channel_.SendPacket(
173         &packet[0], kPacketSize, rtc::PacketOptions(), cricket::PF_SRTP_BYPASS);
174   }
175 
176   // Sends an RTP packet to the other peer via |quic_channel_|, without the SRTP
177   // bypass flag.
SendRtpPacket()178   int SendRtpPacket() {
179     char packet[kPacketSize];
180     packet[0] = 0x80;  // Make the packet header look like RTP.
181     return quic_channel_.SendPacket(&packet[0], kPacketSize,
182                                     rtc::PacketOptions(), 0);
183   }
184 
ClearBytesSent()185   void ClearBytesSent() { bytes_sent_ = 0; }
186 
ClearBytesReceived()187   void ClearBytesReceived() { bytes_received_ = 0; }
188 
SetWriteError(int error)189   void SetWriteError(int error) { ice_channel_->SetError(error); }
190 
bytes_received() const191   size_t bytes_received() const { return bytes_received_; }
192 
bytes_sent() const193   size_t bytes_sent() const { return bytes_sent_; }
194 
ice_channel()195   FailableTransportChannel* ice_channel() { return ice_channel_; }
196 
quic_channel()197   QuicTransportChannel* quic_channel() { return &quic_channel_; }
198 
local_fingerprint()199   std::unique_ptr<rtc::SSLFingerprint>& local_fingerprint() {
200     return local_fingerprint_;
201   }
202 
incoming_quic_stream()203   ReliableQuicStream* incoming_quic_stream() { return incoming_quic_stream_; }
204 
incoming_stream_count() const205   size_t incoming_stream_count() const { return incoming_stream_count_; }
206 
signal_closed_emitted() const207   bool signal_closed_emitted() const { return signal_closed_emitted_; }
208 
209  private:
210   // QuicTransportChannel callbacks.
OnTransportChannelReadPacket(TransportChannel * channel,const char * data,size_t size,const rtc::PacketTime & packet_time,int flags)211   void OnTransportChannelReadPacket(TransportChannel* channel,
212                                     const char* data,
213                                     size_t size,
214                                     const rtc::PacketTime& packet_time,
215                                     int flags) {
216     bytes_received_ += size;
217     // Only SRTP packets should have the bypass flag set.
218     int expected_flags = IsRtpLeadByte(data[0]) ? cricket::PF_SRTP_BYPASS : 0;
219     ASSERT_EQ(expected_flags, flags);
220   }
OnIncomingStream(ReliableQuicStream * stream)221   void OnIncomingStream(ReliableQuicStream* stream) {
222     incoming_quic_stream_ = stream;
223     ++incoming_stream_count_;
224   }
OnClosed()225   void OnClosed() { signal_closed_emitted_ = true; }
226 
227   std::string name_;                      // Channel name.
228   size_t bytes_sent_;                     // Bytes sent by QUIC channel.
229   size_t bytes_received_;                 // Bytes received by QUIC channel.
230   FailableTransportChannel* ice_channel_;  // Simulates an ICE channel.
231   QuicTransportChannel quic_channel_;     // QUIC channel to test.
232   std::unique_ptr<rtc::SSLFingerprint> local_fingerprint_;
233   ReliableQuicStream* incoming_quic_stream_ = nullptr;
234   size_t incoming_stream_count_;
235   bool signal_closed_emitted_ = false;
236 };
237 
238 class QuicTransportChannelTest : public testing::Test {
239  public:
QuicTransportChannelTest()240   QuicTransportChannelTest() : peer1_("P1"), peer2_("P2") {}
241 
242   // Performs negotiation before QUIC handshake, then connects the fake
243   // transport channels of each peer. As a side effect, the QUIC channels
244   // start sending handshake messages. |peer1_| has a client role and |peer2_|
245   // has server role in the QUIC handshake.
Connect()246   void Connect() {
247     SetIceAndCryptoParameters(rtc::SSL_CLIENT, rtc::SSL_SERVER);
248     peer1_.Connect(&peer2_);
249   }
250 
251   // Disconnects the fake transport channels.
Disconnect()252   void Disconnect() {
253     peer1_.Disconnect();
254     peer2_.Disconnect();
255   }
256 
257   // Sets up ICE parameters and exchanges fingerprints before QUIC handshake.
SetIceAndCryptoParameters(rtc::SSLRole peer1_ssl_role,rtc::SSLRole peer2_ssl_role)258   void SetIceAndCryptoParameters(rtc::SSLRole peer1_ssl_role,
259                                  rtc::SSLRole peer2_ssl_role) {
260     peer1_.quic_channel()->SetSslRole(peer1_ssl_role);
261     peer2_.quic_channel()->SetSslRole(peer2_ssl_role);
262 
263     std::unique_ptr<rtc::SSLFingerprint>& peer1_fingerprint =
264         peer1_.local_fingerprint();
265     std::unique_ptr<rtc::SSLFingerprint>& peer2_fingerprint =
266         peer2_.local_fingerprint();
267 
268     peer1_.quic_channel()->SetRemoteFingerprint(
269         peer2_fingerprint->algorithm,
270         reinterpret_cast<const uint8_t*>(peer2_fingerprint->digest.data()),
271         peer2_fingerprint->digest.size());
272     peer2_.quic_channel()->SetRemoteFingerprint(
273         peer1_fingerprint->algorithm,
274         reinterpret_cast<const uint8_t*>(peer1_fingerprint->digest.data()),
275         peer1_fingerprint->digest.size());
276 
277     ConnectionRole peer1_connection_role =
278         SslRoleToConnectionRole(peer1_ssl_role);
279     ConnectionRole peer2_connection_role =
280         SslRoleToConnectionRole(peer2_ssl_role);
281 
282     peer1_.SetIceParameters(cricket::ICEROLE_CONTROLLED, peer1_connection_role,
283                             peer2_connection_role, peer2_fingerprint.get());
284     peer2_.SetIceParameters(cricket::ICEROLE_CONTROLLING, peer2_connection_role,
285                             peer1_connection_role, peer1_fingerprint.get());
286   }
287 
288   // Checks if QUIC handshake is done.
quic_connected()289   bool quic_connected() {
290     return peer1_.quic_channel()->quic_state() ==
291                cricket::QUIC_TRANSPORT_CONNECTED &&
292            peer2_.quic_channel()->quic_state() ==
293                cricket::QUIC_TRANSPORT_CONNECTED;
294   }
295 
296   // Checks if QUIC channels are writable.
quic_writable()297   bool quic_writable() {
298     return peer1_.quic_channel()->writable() &&
299            peer2_.quic_channel()->writable();
300   }
301 
302  protected:
303   // QUIC peer with a client role, who initiates the QUIC handshake.
304   QuicTestPeer peer1_;
305   // QUIC peer with a server role, who responds to the client peer.
306   QuicTestPeer peer2_;
307 };
308 
309 // Test that the QUIC channel passes ICE parameters to the underlying ICE
310 // channel.
TEST_F(QuicTransportChannelTest,ChannelSetupIce)311 TEST_F(QuicTransportChannelTest, ChannelSetupIce) {
312   SetIceAndCryptoParameters(rtc::SSL_CLIENT, rtc::SSL_SERVER);
313   FailableTransportChannel* channel1 = peer1_.ice_channel();
314   FailableTransportChannel* channel2 = peer2_.ice_channel();
315   EXPECT_EQ(cricket::ICEROLE_CONTROLLED, channel1->GetIceRole());
316   EXPECT_EQ(2u, channel1->IceTiebreaker());
317   EXPECT_EQ(kIceUfrag, channel1->ice_ufrag());
318   EXPECT_EQ(kIcePwd, channel1->ice_pwd());
319   EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel2->GetIceRole());
320   EXPECT_EQ(1u, channel2->IceTiebreaker());
321 }
322 
323 // Test that export keying material generates identical keys for both peers
324 // after the QUIC handshake.
TEST_F(QuicTransportChannelTest,ExportKeyingMaterial)325 TEST_F(QuicTransportChannelTest, ExportKeyingMaterial) {
326   Connect();
327   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
328   uint8_t key1[kOutputKeyLength];
329   uint8_t key2[kOutputKeyLength];
330 
331   bool from_success = peer1_.quic_channel()->ExportKeyingMaterial(
332       kExporterLabel, kExporterContext, kExporterContextLength, true, key1,
333       kOutputKeyLength);
334   ASSERT_TRUE(from_success);
335   bool to_success = peer2_.quic_channel()->ExportKeyingMaterial(
336       kExporterLabel, kExporterContext, kExporterContextLength, true, key2,
337       kOutputKeyLength);
338   ASSERT_TRUE(to_success);
339 
340   EXPECT_EQ(0, memcmp(key1, key2, sizeof(key1)));
341 }
342 
343 // Test that the QUIC channel is not writable before the QUIC handshake.
TEST_F(QuicTransportChannelTest,NotWritableBeforeHandshake)344 TEST_F(QuicTransportChannelTest, NotWritableBeforeHandshake) {
345   Connect();
346   EXPECT_FALSE(quic_writable());
347   Disconnect();
348   EXPECT_FALSE(quic_writable());
349   Connect();
350   EXPECT_FALSE(quic_writable());
351 }
352 
353 // Test that once handshake begins, QUIC is not writable until its completion.
TEST_F(QuicTransportChannelTest,QuicHandshake)354 TEST_F(QuicTransportChannelTest, QuicHandshake) {
355   Connect();
356   EXPECT_FALSE(quic_writable());
357   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
358   EXPECT_TRUE(quic_writable());
359 }
360 
361 // Test that Non-SRTP data is not sent using SendPacket(), regardless of QUIC
362 // channel state.
TEST_F(QuicTransportChannelTest,TransferNonSrtp)363 TEST_F(QuicTransportChannelTest, TransferNonSrtp) {
364   // Send data before ICE channel is connected.
365   peer1_.ClearBytesSent();
366   peer2_.ClearBytesReceived();
367   ASSERT_EQ(-1, peer1_.SendRtpPacket());
368   EXPECT_EQ(0u, peer1_.bytes_sent());
369   // Send data after ICE channel is connected, before QUIC handshake.
370   Connect();
371   peer1_.ClearBytesSent();
372   peer2_.ClearBytesReceived();
373   ASSERT_EQ(-1, peer1_.SendRtpPacket());
374   EXPECT_EQ(0u, peer1_.bytes_sent());
375   // Send data after QUIC handshake.
376   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
377   peer1_.ClearBytesSent();
378   peer2_.ClearBytesReceived();
379   ASSERT_EQ(-1, peer1_.SendRtpPacket());
380   EXPECT_EQ(0u, peer1_.bytes_sent());
381 }
382 
383 // Test that SRTP data is always be sent, regardless of QUIC channel state, when
384 // the ICE channel is connected.
TEST_F(QuicTransportChannelTest,TransferSrtp)385 TEST_F(QuicTransportChannelTest, TransferSrtp) {
386   // Send data after ICE channel is connected, before QUIC handshake.
387   Connect();
388   peer1_.ClearBytesSent();
389   peer2_.ClearBytesReceived();
390   ASSERT_EQ(kPacketSize, static_cast<size_t>(peer1_.SendSrtpPacket()));
391   EXPECT_EQ_WAIT(kPacketSize, peer2_.bytes_received(), kTimeoutMs);
392   EXPECT_EQ(kPacketSize, peer1_.bytes_sent());
393   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
394   // Send data after QUIC handshake.
395   peer1_.ClearBytesSent();
396   peer2_.ClearBytesReceived();
397   ASSERT_EQ(kPacketSize, static_cast<size_t>(peer1_.SendSrtpPacket()));
398   EXPECT_EQ_WAIT(kPacketSize, peer2_.bytes_received(), kTimeoutMs);
399   EXPECT_EQ(kPacketSize, peer1_.bytes_sent());
400 }
401 
402 // Test that invalid SRTP (non-SRTP data with
403 // PF_SRTP_BYPASS flag) fails to send with return value -1.
TEST_F(QuicTransportChannelTest,TransferInvalidSrtp)404 TEST_F(QuicTransportChannelTest, TransferInvalidSrtp) {
405   peer1_.ClearBytesSent();
406   peer2_.ClearBytesReceived();
407   EXPECT_EQ(-1, peer1_.SendInvalidSrtpPacket());
408   EXPECT_EQ(0u, peer2_.bytes_received());
409   Connect();
410   peer1_.ClearBytesSent();
411   peer2_.ClearBytesReceived();
412   EXPECT_EQ(-1, peer1_.SendInvalidSrtpPacket());
413   EXPECT_EQ(0u, peer2_.bytes_received());
414 }
415 
416 // Test that QuicTransportChannel::WritePacket blocks when the ICE
417 // channel is not writable, and otherwise succeeds.
TEST_F(QuicTransportChannelTest,QuicWritePacket)418 TEST_F(QuicTransportChannelTest, QuicWritePacket) {
419   peer1_.ice_channel()->SetDestination(peer2_.ice_channel());
420   std::string packet = "FAKEQUICPACKET";
421 
422   // QUIC should be write blocked when the ICE channel is not writable.
423   peer1_.ice_channel()->SetWritable(false);
424   EXPECT_TRUE(peer1_.quic_channel()->IsWriteBlocked());
425   net::WriteResult write_blocked_result = peer1_.quic_channel()->WritePacket(
426       packet.data(), packet.size(), kIpAddress, kIpEndpoint, nullptr);
427   EXPECT_EQ(net::WRITE_STATUS_BLOCKED, write_blocked_result.status);
428   EXPECT_EQ(EWOULDBLOCK, write_blocked_result.error_code);
429 
430   // QUIC should ignore errors when the ICE channel is writable.
431   peer1_.ice_channel()->SetWritable(true);
432   EXPECT_FALSE(peer1_.quic_channel()->IsWriteBlocked());
433   peer1_.SetWriteError(EWOULDBLOCK);
434   net::WriteResult ignore_error_result = peer1_.quic_channel()->WritePacket(
435       packet.data(), packet.size(), kIpAddress, kIpEndpoint, nullptr);
436   EXPECT_EQ(net::WRITE_STATUS_OK, ignore_error_result.status);
437   EXPECT_EQ(0, ignore_error_result.bytes_written);
438 
439   peer1_.SetWriteError(kNoWriteError);
440   net::WriteResult no_error_result = peer1_.quic_channel()->WritePacket(
441       packet.data(), packet.size(), kIpAddress, kIpEndpoint, nullptr);
442   EXPECT_EQ(net::WRITE_STATUS_OK, no_error_result.status);
443   EXPECT_EQ(static_cast<int>(packet.size()), no_error_result.bytes_written);
444 }
445 
446 // Test that SSL roles can be reversed before QUIC handshake.
TEST_F(QuicTransportChannelTest,QuicRoleReversalBeforeQuic)447 TEST_F(QuicTransportChannelTest, QuicRoleReversalBeforeQuic) {
448   EXPECT_TRUE(peer1_.quic_channel()->SetSslRole(rtc::SSL_SERVER));
449   EXPECT_TRUE(peer1_.quic_channel()->SetSslRole(rtc::SSL_CLIENT));
450   EXPECT_TRUE(peer1_.quic_channel()->SetSslRole(rtc::SSL_SERVER));
451 }
452 
453 // Test that SSL roles cannot be reversed after the QUIC handshake. SetSslRole
454 // returns true if the current SSL role equals the proposed SSL role.
TEST_F(QuicTransportChannelTest,QuicRoleReversalAfterQuic)455 TEST_F(QuicTransportChannelTest, QuicRoleReversalAfterQuic) {
456   Connect();
457   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
458   EXPECT_FALSE(peer1_.quic_channel()->SetSslRole(rtc::SSL_SERVER));
459   EXPECT_TRUE(peer1_.quic_channel()->SetSslRole(rtc::SSL_CLIENT));
460   EXPECT_FALSE(peer2_.quic_channel()->SetSslRole(rtc::SSL_CLIENT));
461   EXPECT_TRUE(peer2_.quic_channel()->SetSslRole(rtc::SSL_SERVER));
462 }
463 
464 // Set the SSL role, then test that GetSslRole returns the same value.
TEST_F(QuicTransportChannelTest,SetGetSslRole)465 TEST_F(QuicTransportChannelTest, SetGetSslRole) {
466   ASSERT_TRUE(peer1_.quic_channel()->SetSslRole(rtc::SSL_SERVER));
467   std::unique_ptr<rtc::SSLRole> role(new rtc::SSLRole());
468   ASSERT_TRUE(peer1_.quic_channel()->GetSslRole(role.get()));
469   EXPECT_EQ(rtc::SSL_SERVER, *role);
470 }
471 
472 // Test that after the QUIC handshake is complete, the QUIC handshake remains
473 // confirmed even if the ICE channel reconnects.
TEST_F(QuicTransportChannelTest,HandshakeConfirmedAfterReconnect)474 TEST_F(QuicTransportChannelTest, HandshakeConfirmedAfterReconnect) {
475   Connect();
476   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
477   Disconnect();
478   EXPECT_TRUE(quic_connected());
479   Connect();
480   EXPECT_TRUE(quic_connected());
481 }
482 
483 // Test that if the ICE channel becomes receiving after the QUIC channel is
484 // connected, then the QUIC channel becomes receiving.
TEST_F(QuicTransportChannelTest,IceReceivingAfterConnected)485 TEST_F(QuicTransportChannelTest, IceReceivingAfterConnected) {
486   Connect();
487   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
488   ASSERT_FALSE(peer1_.ice_channel()->receiving());
489   EXPECT_FALSE(peer1_.quic_channel()->receiving());
490   peer1_.ice_channel()->SetReceiving(true);
491   EXPECT_TRUE(peer1_.quic_channel()->receiving());
492 }
493 
494 // Test that if the ICE channel becomes receiving before the QUIC channel is
495 // connected, then the QUIC channel becomes receiving.
TEST_F(QuicTransportChannelTest,IceReceivingBeforeConnected)496 TEST_F(QuicTransportChannelTest, IceReceivingBeforeConnected) {
497   Connect();
498   peer1_.ice_channel()->SetReceiving(true);
499   ASSERT_TRUE(peer1_.ice_channel()->receiving());
500   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
501   EXPECT_TRUE(peer1_.quic_channel()->receiving());
502 }
503 
504 // Test that when peer 1 creates an outgoing stream, peer 2 creates an incoming
505 // QUIC stream with the same ID and fires OnIncomingStream.
TEST_F(QuicTransportChannelTest,CreateOutgoingAndIncomingQuicStream)506 TEST_F(QuicTransportChannelTest, CreateOutgoingAndIncomingQuicStream) {
507   Connect();
508   EXPECT_EQ(nullptr, peer1_.quic_channel()->CreateQuicStream());
509   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
510   ReliableQuicStream* stream = peer1_.quic_channel()->CreateQuicStream();
511   ASSERT_NE(nullptr, stream);
512   stream->Write("Hi", 2);
513   EXPECT_TRUE_WAIT(peer2_.incoming_quic_stream() != nullptr, kTimeoutMs);
514   EXPECT_EQ(stream->id(), peer2_.incoming_quic_stream()->id());
515 }
516 
517 // Test that if the QuicTransportChannel is unwritable, then all outgoing QUIC
518 // streams can send data once the QuicTransprotChannel becomes writable again.
TEST_F(QuicTransportChannelTest,OutgoingQuicStreamSendsDataAfterReconnect)519 TEST_F(QuicTransportChannelTest, OutgoingQuicStreamSendsDataAfterReconnect) {
520   Connect();
521   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
522   ReliableQuicStream* stream1 = peer1_.quic_channel()->CreateQuicStream();
523   ASSERT_NE(nullptr, stream1);
524   ReliableQuicStream* stream2 = peer1_.quic_channel()->CreateQuicStream();
525   ASSERT_NE(nullptr, stream2);
526 
527   peer1_.ice_channel()->SetWritable(false);
528   stream1->Write("First", 5);
529   EXPECT_EQ(5u, stream1->queued_data_bytes());
530   stream2->Write("Second", 6);
531   EXPECT_EQ(6u, stream2->queued_data_bytes());
532   EXPECT_EQ(0u, peer2_.incoming_stream_count());
533 
534   peer1_.ice_channel()->SetWritable(true);
535   EXPECT_EQ_WAIT(0u, stream1->queued_data_bytes(), kTimeoutMs);
536   EXPECT_EQ_WAIT(0u, stream2->queued_data_bytes(), kTimeoutMs);
537   EXPECT_EQ_WAIT(2u, peer2_.incoming_stream_count(), kTimeoutMs);
538 }
539 
540 // Test that SignalClosed is emitted when the QuicConnection closes.
TEST_F(QuicTransportChannelTest,SignalClosedEmitted)541 TEST_F(QuicTransportChannelTest, SignalClosedEmitted) {
542   Connect();
543   ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs);
544   ASSERT_FALSE(peer1_.signal_closed_emitted());
545   ReliableQuicStream* stream = peer1_.quic_channel()->CreateQuicStream();
546   ASSERT_NE(nullptr, stream);
547   stream->CloseConnectionWithDetails(net::QuicErrorCode::QUIC_NO_ERROR,
548                                      "Closing QUIC for testing");
549   EXPECT_TRUE(peer1_.signal_closed_emitted());
550   EXPECT_TRUE_WAIT(peer2_.signal_closed_emitted(), kTimeoutMs);
551 }
552