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/sslidentity.h" | 20 #include "webrtc/base/sslidentity.h" |
21 #include "webrtc/p2p/base/faketransportcontroller.h" | 21 #include "webrtc/p2p/base/faketransportcontroller.h" |
22 | 22 |
23 using cricket::ConnectionRole; | 23 using cricket::ConnectionRole; |
24 using cricket::IceRole; | 24 using cricket::IceRole; |
25 using cricket::QuicTransportChannel; | 25 using cricket::QuicTransportChannel; |
26 using cricket::ReliableQuicStream; | |
26 using cricket::TransportChannel; | 27 using cricket::TransportChannel; |
27 using cricket::TransportDescription; | 28 using cricket::TransportDescription; |
28 | 29 |
29 // Timeout in milliseconds for asynchronous operations in unit tests. | 30 // Timeout in milliseconds for asynchronous operations in unit tests. |
30 static const int kTimeoutMs = 1000; | 31 static const int kTimeoutMs = 1000; |
31 | 32 |
32 // Export keying material parameters. | 33 // Export keying material parameters. |
33 static const char kExporterLabel[] = "label"; | 34 static const char kExporterLabel[] = "label"; |
34 static const uint8_t kExporterContext[] = "context"; | 35 static const uint8_t kExporterContext[] = "context"; |
35 static const size_t kExporterContextLength = sizeof(kExporterContext); | 36 static const size_t kExporterContextLength = sizeof(kExporterContext); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 // negotiation. | 91 // negotiation. |
91 class QuicTestPeer : public sigslot::has_slots<> { | 92 class QuicTestPeer : public sigslot::has_slots<> { |
92 public: | 93 public: |
93 explicit QuicTestPeer(const std::string& name) | 94 explicit QuicTestPeer(const std::string& name) |
94 : name_(name), | 95 : name_(name), |
95 bytes_sent_(0), | 96 bytes_sent_(0), |
96 ice_channel_(name_, 0), | 97 ice_channel_(name_, 0), |
97 quic_channel_(&ice_channel_) { | 98 quic_channel_(&ice_channel_) { |
98 quic_channel_.SignalReadPacket.connect( | 99 quic_channel_.SignalReadPacket.connect( |
99 this, &QuicTestPeer::OnTransportChannelReadPacket); | 100 this, &QuicTestPeer::OnTransportChannelReadPacket); |
101 quic_channel_.SignalIncomingStream.connect(this, | |
102 &QuicTestPeer::OnIncomingStream); | |
103 quic_channel_.SignalClosed.connect(this, &QuicTestPeer::OnClosed); | |
100 ice_channel_.SetAsync(true); | 104 ice_channel_.SetAsync(true); |
101 rtc::scoped_refptr<rtc::RTCCertificate> local_cert = | 105 rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
102 rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( | 106 rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( |
103 rtc::SSLIdentity::Generate(name_, rtc::KT_DEFAULT))); | 107 rtc::SSLIdentity::Generate(name_, rtc::KT_DEFAULT))); |
104 quic_channel_.SetLocalCertificate(local_cert); | 108 quic_channel_.SetLocalCertificate(local_cert); |
105 local_fingerprint_.reset(CreateFingerprint(local_cert.get())); | 109 local_fingerprint_.reset(CreateFingerprint(local_cert.get())); |
106 } | 110 } |
107 | 111 |
108 // Connects |ice_channel_| to that of the other peer. | 112 // Connects |ice_channel_| to that of the other peer. |
109 void Connect(QuicTestPeer* other_peer) { | 113 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_; } | 195 size_t bytes_sent() const { return bytes_sent_; } |
192 | 196 |
193 FailableTransportChannel* ice_channel() { return &ice_channel_; } | 197 FailableTransportChannel* ice_channel() { return &ice_channel_; } |
194 | 198 |
195 QuicTransportChannel* quic_channel() { return &quic_channel_; } | 199 QuicTransportChannel* quic_channel() { return &quic_channel_; } |
196 | 200 |
197 rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { | 201 rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { |
198 return local_fingerprint_; | 202 return local_fingerprint_; |
199 } | 203 } |
200 | 204 |
205 ReliableQuicStream* incoming_quic_stream() { return incoming_quic_stream_; } | |
206 | |
207 bool signal_closed_emitted() const { return signal_closed_emitted_; } | |
208 | |
201 private: | 209 private: |
202 // QUIC channel callback. | 210 // QuicTransportChannel callbacks. |
203 void OnTransportChannelReadPacket(TransportChannel* channel, | 211 void OnTransportChannelReadPacket(TransportChannel* channel, |
204 const char* data, | 212 const char* data, |
205 size_t size, | 213 size_t size, |
206 const rtc::PacketTime& packet_time, | 214 const rtc::PacketTime& packet_time, |
207 int flags) { | 215 int flags) { |
208 bytes_received_ += size; | 216 bytes_received_ += size; |
209 // Only SRTP packets should have the bypass flag set. | 217 // Only SRTP packets should have the bypass flag set. |
210 int expected_flags = IsRtpLeadByte(data[0]) ? cricket::PF_SRTP_BYPASS : 0; | 218 int expected_flags = IsRtpLeadByte(data[0]) ? cricket::PF_SRTP_BYPASS : 0; |
211 ASSERT_EQ(expected_flags, flags); | 219 ASSERT_EQ(expected_flags, flags); |
212 } | 220 } |
221 void OnIncomingStream(ReliableQuicStream* stream) { | |
222 incoming_quic_stream_ = stream; | |
223 } | |
224 void OnClosed() { signal_closed_emitted_ = true; } | |
213 | 225 |
214 std::string name_; // Channel name. | 226 std::string name_; // Channel name. |
215 size_t bytes_sent_; // Bytes sent by QUIC channel. | 227 size_t bytes_sent_; // Bytes sent by QUIC channel. |
216 size_t bytes_received_; // Bytes received by QUIC channel. | 228 size_t bytes_received_; // Bytes received by QUIC channel. |
217 FailableTransportChannel ice_channel_; // Simulates an ICE channel. | 229 FailableTransportChannel ice_channel_; // Simulates an ICE channel. |
218 QuicTransportChannel quic_channel_; // QUIC channel to test. | 230 QuicTransportChannel quic_channel_; // QUIC channel to test. |
219 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; | 231 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; |
232 ReliableQuicStream* incoming_quic_stream_ = nullptr; | |
233 bool signal_closed_emitted_ = false; | |
220 }; | 234 }; |
221 | 235 |
222 class QuicTransportChannelTest : public testing::Test { | 236 class QuicTransportChannelTest : public testing::Test { |
223 public: | 237 public: |
224 QuicTransportChannelTest() : peer1_("P1"), peer2_("P2") {} | 238 QuicTransportChannelTest() : peer1_("P1"), peer2_("P2") {} |
225 | 239 |
226 // Performs negotiation before QUIC handshake, then connects the fake | 240 // Performs negotiation before QUIC handshake, then connects the fake |
227 // transport channels of each peer. As a side effect, the QUIC channels | 241 // transport channels of each peer. As a side effect, the QUIC channels |
228 // start sending handshake messages. |peer1_| has a client role and |peer2_| | 242 // start sending handshake messages. |peer1_| has a client role and |peer2_| |
229 // has server role in the QUIC handshake. | 243 // has server role in the QUIC handshake. |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 | 493 |
480 // Test that if the ICE channel becomes receiving before the QUIC channel is | 494 // Test that if the ICE channel becomes receiving before the QUIC channel is |
481 // connected, then the QUIC channel becomes receiving. | 495 // connected, then the QUIC channel becomes receiving. |
482 TEST_F(QuicTransportChannelTest, IceReceivingBeforeConnected) { | 496 TEST_F(QuicTransportChannelTest, IceReceivingBeforeConnected) { |
483 Connect(); | 497 Connect(); |
484 peer1_.ice_channel()->SetReceiving(true); | 498 peer1_.ice_channel()->SetReceiving(true); |
485 ASSERT_TRUE(peer1_.ice_channel()->receiving()); | 499 ASSERT_TRUE(peer1_.ice_channel()->receiving()); |
486 ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs); | 500 ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs); |
487 EXPECT_TRUE(peer1_.quic_channel()->receiving()); | 501 EXPECT_TRUE(peer1_.quic_channel()->receiving()); |
488 } | 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. | |
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 SignalClosed is emitted when the QuicConnection closes. | |
518 TEST_F(QuicTransportChannelTest, SignalClosedEmmitted) { | |
Taylor Brandstetter
2016/04/05 22:19:35
Emmitted => Emitted
mikescarlett
2016/04/06 17:31:54
Done.
| |
519 Connect(); | |
520 ASSERT_TRUE_WAIT(quic_connected(), kTimeoutMs); | |
521 ASSERT_FALSE(peer1_.signal_closed_emitted()); | |
522 ReliableQuicStream* stream = peer1_.quic_channel()->CreateQuicStream(); | |
523 ASSERT_NE(nullptr, stream); | |
524 stream->CloseConnectionWithDetails(net::QuicErrorCode::QUIC_NO_ERROR, | |
525 "Closing QUIC for testing"); | |
526 ASSERT_TRUE(peer1_.signal_closed_emitted()); | |
527 } | |
Taylor Brandstetter
2016/04/05 22:19:35
Should this also wait for peer2_signal_closed_emit
mikescarlett
2016/04/06 17:31:54
Yes it should. Fixed.
| |
OLD | NEW |