OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/p2p/quic/quictransportchannel.h" | 11 #include "webrtc/p2p/quic/quictransportchannel.h" |
12 | 12 |
13 #include <set> | 13 #include <set> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/base/common.h" | 17 #include "webrtc/base/common.h" |
18 #include "webrtc/base/gunit.h" | 18 #include "webrtc/base/gunit.h" |
19 #include "webrtc/base/scoped_ptr.h" | 19 #include "webrtc/base/scoped_ptr.h" |
| 20 #include "webrtc/base/sslfingerprint.h" |
20 #include "webrtc/base/sslidentity.h" | 21 #include "webrtc/base/sslidentity.h" |
21 #include "webrtc/p2p/base/faketransportcontroller.h" | 22 #include "webrtc/p2p/base/faketransportcontroller.h" |
22 | 23 |
23 using cricket::ConnectionRole; | 24 using cricket::ConnectionRole; |
24 using cricket::IceRole; | 25 using cricket::IceRole; |
25 using cricket::QuicTransportChannel; | 26 using cricket::QuicTransportChannel; |
| 27 using cricket::ReliableQuicStream; |
26 using cricket::TransportChannel; | 28 using cricket::TransportChannel; |
27 using cricket::TransportDescription; | 29 using cricket::TransportDescription; |
28 | 30 |
29 // Timeout in milliseconds for asynchronous operations in unit tests. | 31 // Timeout in milliseconds for asynchronous operations in unit tests. |
30 static const int kTimeoutMs = 1000; | 32 static const int kTimeoutMs = 1000; |
31 | 33 |
32 // Export keying material parameters. | 34 // Export keying material parameters. |
33 static const char kExporterLabel[] = "label"; | 35 static const char kExporterLabel[] = "label"; |
34 static const uint8_t kExporterContext[] = "context"; | 36 static const uint8_t kExporterContext[] = "context"; |
35 static const size_t kExporterContextLength = sizeof(kExporterContext); | 37 static const size_t kExporterContextLength = sizeof(kExporterContext); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // negotiation. | 92 // negotiation. |
91 class QuicTestPeer : public sigslot::has_slots<> { | 93 class QuicTestPeer : public sigslot::has_slots<> { |
92 public: | 94 public: |
93 explicit QuicTestPeer(const std::string& name) | 95 explicit QuicTestPeer(const std::string& name) |
94 : name_(name), | 96 : name_(name), |
95 bytes_sent_(0), | 97 bytes_sent_(0), |
96 ice_channel_(name_, 0), | 98 ice_channel_(name_, 0), |
97 quic_channel_(&ice_channel_) { | 99 quic_channel_(&ice_channel_) { |
98 quic_channel_.SignalReadPacket.connect( | 100 quic_channel_.SignalReadPacket.connect( |
99 this, &QuicTestPeer::OnTransportChannelReadPacket); | 101 this, &QuicTestPeer::OnTransportChannelReadPacket); |
| 102 quic_channel_.SignalIncomingStream.connect(this, |
| 103 &QuicTestPeer::OnIncomingStream); |
| 104 quic_channel_.SignalClosed.connect(this, &QuicTestPeer::OnClosed); |
100 ice_channel_.SetAsync(true); | 105 ice_channel_.SetAsync(true); |
101 rtc::scoped_refptr<rtc::RTCCertificate> local_cert = | 106 rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
102 rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( | 107 rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( |
103 rtc::SSLIdentity::Generate(name_, rtc::KT_DEFAULT))); | 108 rtc::SSLIdentity::Generate(name_, rtc::KT_DEFAULT))); |
104 quic_channel_.SetLocalCertificate(local_cert); | 109 quic_channel_.SetLocalCertificate(local_cert); |
105 local_fingerprint_.reset(CreateFingerprint(local_cert.get())); | 110 local_fingerprint_.reset(CreateFingerprint(local_cert.get())); |
106 } | 111 } |
107 | 112 |
108 // Connects |ice_channel_| to that of the other peer. | 113 // Connects |ice_channel_| to that of the other peer. |
109 void Connect(QuicTestPeer* other_peer) { | 114 void Connect(QuicTestPeer* other_peer) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 size_t bytes_sent() const { return bytes_sent_; } | 196 size_t bytes_sent() const { return bytes_sent_; } |
192 | 197 |
193 FailableTransportChannel* ice_channel() { return &ice_channel_; } | 198 FailableTransportChannel* ice_channel() { return &ice_channel_; } |
194 | 199 |
195 QuicTransportChannel* quic_channel() { return &quic_channel_; } | 200 QuicTransportChannel* quic_channel() { return &quic_channel_; } |
196 | 201 |
197 rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { | 202 rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { |
198 return local_fingerprint_; | 203 return local_fingerprint_; |
199 } | 204 } |
200 | 205 |
| 206 ReliableQuicStream* incoming_quic_stream() { return incoming_quic_stream_; } |
| 207 |
| 208 bool signal_closed_emitted() const { return signal_closed_emitted_; } |
| 209 |
201 private: | 210 private: |
202 // QUIC channel callback. | 211 // QuicTransportChannel callbacks. |
203 void OnTransportChannelReadPacket(TransportChannel* channel, | 212 void OnTransportChannelReadPacket(TransportChannel* channel, |
204 const char* data, | 213 const char* data, |
205 size_t size, | 214 size_t size, |
206 const rtc::PacketTime& packet_time, | 215 const rtc::PacketTime& packet_time, |
207 int flags) { | 216 int flags) { |
208 bytes_received_ += size; | 217 bytes_received_ += size; |
209 // Only SRTP packets should have the bypass flag set. | 218 // Only SRTP packets should have the bypass flag set. |
210 int expected_flags = IsRtpLeadByte(data[0]) ? cricket::PF_SRTP_BYPASS : 0; | 219 int expected_flags = IsRtpLeadByte(data[0]) ? cricket::PF_SRTP_BYPASS : 0; |
211 ASSERT_EQ(expected_flags, flags); | 220 ASSERT_EQ(expected_flags, flags); |
212 } | 221 } |
| 222 void OnIncomingStream(ReliableQuicStream* stream) { |
| 223 incoming_quic_stream_ = stream; |
| 224 } |
| 225 void OnClosed() { signal_closed_emitted_ = true; } |
213 | 226 |
214 std::string name_; // Channel name. | 227 std::string name_; // Channel name. |
215 size_t bytes_sent_; // Bytes sent by QUIC channel. | 228 size_t bytes_sent_; // Bytes sent by QUIC channel. |
216 size_t bytes_received_; // Bytes received by QUIC channel. | 229 size_t bytes_received_; // Bytes received by QUIC channel. |
217 FailableTransportChannel ice_channel_; // Simulates an ICE channel. | 230 FailableTransportChannel ice_channel_; // Simulates an ICE channel. |
218 QuicTransportChannel quic_channel_; // QUIC channel to test. | 231 QuicTransportChannel quic_channel_; // QUIC channel to test. |
219 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; | 232 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; |
| 233 ReliableQuicStream* incoming_quic_stream_ = nullptr; |
| 234 bool signal_closed_emitted_ = false; |
220 }; | 235 }; |
221 | 236 |
222 class QuicTransportChannelTest : public testing::Test { | 237 class QuicTransportChannelTest : public testing::Test { |
223 public: | 238 public: |
224 QuicTransportChannelTest() : peer1_("P1"), peer2_("P2") {} | 239 QuicTransportChannelTest() : peer1_("P1"), peer2_("P2") {} |
225 | 240 |
226 // Performs negotiation before QUIC handshake, then connects the fake | 241 // Performs negotiation before QUIC handshake, then connects the fake |
227 // transport channels of each peer. As a side effect, the QUIC channels | 242 // transport channels of each peer. As a side effect, the QUIC channels |
228 // start sending handshake messages. |peer1_| has a client role and |peer2_| | 243 // start sending handshake messages. |peer1_| has a client role and |peer2_| |
229 // has server role in the QUIC handshake. | 244 // has server role in the QUIC handshake. |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 | 494 |
480 // Test that if the ICE channel becomes receiving before the QUIC channel is | 495 // Test that if the ICE channel becomes receiving before the QUIC channel is |
481 // connected, then the QUIC channel becomes receiving. | 496 // connected, then the QUIC channel becomes receiving. |
482 TEST_F(QuicTransportChannelTest, IceReceivingBeforeConnected) { | 497 TEST_F(QuicTransportChannelTest, IceReceivingBeforeConnected) { |
483 Connect(); | 498 Connect(); |
484 peer1_.ice_channel()->SetReceiving(true); | 499 peer1_.ice_channel()->SetReceiving(true); |
485 ASSERT_TRUE(peer1_.ice_channel()->receiving()); | 500 ASSERT_TRUE(peer1_.ice_channel()->receiving()); |
486 ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs); | 501 ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs); |
487 EXPECT_TRUE(peer1_.quic_channel()->receiving()); | 502 EXPECT_TRUE(peer1_.quic_channel()->receiving()); |
488 } | 503 } |
| 504 |
| 505 // Test that when peer 1 creates an outgoing stream, peer 2 creates an incoming |
| 506 // QUIC stream with the same ID and fires OnIncomingStream. |
| 507 TEST_F(QuicTransportChannelTest, CreateOutgoingAndIncomingQuicStream) { |
| 508 Connect(); |
| 509 EXPECT_EQ(nullptr, peer1_.quic_channel()->CreateQuicStream()); |
| 510 ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs); |
| 511 ReliableQuicStream* stream = peer1_.quic_channel()->CreateQuicStream(); |
| 512 ASSERT_NE(nullptr, stream); |
| 513 stream->Write("Hi", 2); |
| 514 EXPECT_TRUE_WAIT(peer2_.incoming_quic_stream() != nullptr, kTimeoutMs); |
| 515 EXPECT_EQ(stream->id(), peer2_.incoming_quic_stream()->id()); |
| 516 } |
| 517 |
| 518 // Test that SignalClosed is emitted when the QuicConnection closes. |
| 519 TEST_F(QuicTransportChannelTest, SignalClosedEmitted) { |
| 520 Connect(); |
| 521 ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs); |
| 522 ASSERT_FALSE(peer1_.signal_closed_emitted()); |
| 523 ReliableQuicStream* stream = peer1_.quic_channel()->CreateQuicStream(); |
| 524 ASSERT_NE(nullptr, stream); |
| 525 stream->CloseConnectionWithDetails(net::QuicErrorCode::QUIC_NO_ERROR, |
| 526 "Closing QUIC for testing"); |
| 527 EXPECT_TRUE(peer1_.signal_closed_emitted()); |
| 528 EXPECT_TRUE_WAIT(peer2_.signal_closed_emitted(), kTimeoutMs); |
| 529 } |
OLD | NEW |