Index: webrtc/api/quicdatachannel_unittest.cc |
diff --git a/webrtc/api/quicdatachannel_unittest.cc b/webrtc/api/quicdatachannel_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a55ead06a1692ac46884966320f48c1509969e39 |
--- /dev/null |
+++ b/webrtc/api/quicdatachannel_unittest.cc |
@@ -0,0 +1,405 @@ |
+/* |
+ * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/api/quicdatachannel.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "webrtc/base/bind.h" |
+#include "webrtc/base/gunit.h" |
+#include "webrtc/p2p/base/faketransportcontroller.h" |
+#include "webrtc/p2p/quic/quictransportchannel.h" |
+#include "webrtc/p2p/quic/reliablequicstream.h" |
+ |
+using cricket::FakeTransportChannel; |
+using cricket::QuicTransportChannel; |
+using cricket::ReliableQuicStream; |
+ |
+using webrtc::DataBuffer; |
+using webrtc::DataChannelObserver; |
+using webrtc::DataChannelInit; |
+using webrtc::QuicDataChannel; |
+ |
+// Timeout for asynchronous operations. |
+static const int kTimeoutMs = 1000; // milliseconds |
+ |
+// Small messages that can be sent within a single QUIC packet. |
+static const std::string kSmallMessage1 = "Hello, world!"; |
+static const std::string kSmallMessage2 = "WebRTC"; |
+static const std::string kSmallMessage3 = "1"; |
+static const std::string kSmallMessage4 = "abcdefghijklmnopqrstuvwxyz"; |
+static const DataBuffer kSmallBuffer1(kSmallMessage1); |
+static const DataBuffer kSmallBuffer2(kSmallMessage2); |
+static const DataBuffer kSmallBuffer3(kSmallMessage3); |
+static const DataBuffer kSmallBuffer4(kSmallMessage4); |
+ |
+// Large messages (> 1350 bytes) that exceed the max size of a QUIC packet. |
+// These are < 16 KB so they don't exceed the QUIC stream flow control limit. |
+static const std::string kLargeMessage1 = std::string("a", 2000); |
+static const std::string kLargeMessage2 = std::string("a", 4000); |
+static const std::string kLargeMessage3 = std::string("a", 8000); |
+static const std::string kLargeMessage4 = std::string("a", 12000); |
+static const DataBuffer kLargeBuffer1(kLargeMessage1); |
+static const DataBuffer kLargeBuffer2(kLargeMessage2); |
+static const DataBuffer kLargeBuffer3(kLargeMessage3); |
+static const DataBuffer kLargeBuffer4(kLargeMessage4); |
+ |
+// Too large message (> 16 KB) that violates the QUIC stream flow control limit. |
Taylor Brandstetter
2016/04/01 23:23:42
nit: maybe say "oversized" instead of "too large"
mikescarlett
2016/04/05 19:58:50
Done.
|
+static const std::string kTooLargeMessage = std::string("a", 20000); |
+static const DataBuffer kTooLargeBuffer(kTooLargeMessage); |
+ |
+// FakeObserver receives messages from the QuicDataChannel. |
+class FakeObserver : public DataChannelObserver { |
+ public: |
+ FakeObserver() |
+ : messages_received_(0), |
+ on_state_change_count_(0), |
+ on_buffered_amount_change_count_(0) {} |
+ |
+ void OnStateChange() { ++on_state_change_count_; } |
+ |
+ void OnBufferedAmountChange(uint64_t previous_amount) { |
+ ++on_buffered_amount_change_count_; |
+ } |
+ |
+ void OnMessage(const webrtc::DataBuffer& buffer) { |
+ messages_.push_back(std::string(buffer.data.data<char>(), buffer.size())); |
+ ++messages_received_; |
+ } |
+ |
+ void ResetOnStateChangeCount() { on_state_change_count_ = 0; } |
+ |
+ void ResetOnBufferedAmountChangeCount() { |
+ on_buffered_amount_change_count_ = 0; |
+ } |
+ |
+ const std::vector<std::string>& messages() const { return messages_; } |
+ |
+ size_t messages_received() const { return messages_received_; } |
+ |
+ size_t on_state_change_count() const { return on_state_change_count_; } |
+ |
+ size_t on_buffered_amount_change_count() const { |
+ return on_buffered_amount_change_count_; |
+ } |
+ |
+ private: |
+ std::vector<std::string> messages_; |
+ size_t messages_received_; |
+ size_t on_state_change_count_; |
+ size_t on_buffered_amount_change_count_; |
+}; |
+ |
+// A peer who creates one or more QuicDataChannels to send or receive data. |
+class QuicDataChannelPeer : public sigslot::has_slots<> { |
+ public: |
+ QuicDataChannelPeer(rtc::Thread* signaling_thread, rtc::Thread* worker_thread) |
+ : ice_transport_channel_("data", 0), |
+ quic_transport_channel_(&ice_transport_channel_), |
+ signaling_thread_(signaling_thread), |
+ worker_thread_(worker_thread) { |
+ ice_transport_channel_.SetAsync(true); |
+ quic_transport_channel_.SignalIncomingStream.connect( |
+ this, &QuicDataChannelPeer::OnIncomingStream); |
+ } |
+ |
+ void GenerateCertificateAndFingerprint() { |
+ rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
+ rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( |
+ rtc::SSLIdentity::Generate("cert_name", rtc::KT_DEFAULT))); |
+ quic_transport_channel_.SetLocalCertificate(local_cert); |
+ local_fingerprint_.reset(CreateFingerprint(local_cert.get())); |
+ } |
+ |
+ QuicDataChannel* CreateDataChannel(int id, |
+ const std::string& label, |
+ const std::string& protocol) { |
+ DataChannelInit config; |
+ config.id = id; |
+ config.protocol = protocol; |
+ |
+ QuicDataChannel* data_channel = |
+ new QuicDataChannel(&quic_transport_channel_, signaling_thread_, |
+ worker_thread_, label, &config); |
+ quic_data_channels_.push_back( |
+ rtc::scoped_ptr<QuicDataChannel>(data_channel)); |
+ data_channel_map_[id] = data_channel; |
+ return data_channel; |
+ } |
+ |
+ // Called when a QUIC stream is created for incoming data. |
+ void OnIncomingStream(ReliableQuicStream* stream) { |
+ incoming_stream_ = stream; |
+ incoming_stream_->SignalDataReceived.connect( |
+ this, &QuicDataChannelPeer::OnDataReceived); |
+ } |
+ |
+ // Called when the first QUIC stream frame is received for incoming data. |
Taylor Brandstetter
2016/04/01 23:23:42
I'd add a comment saying that this is simulating t
mikescarlett
2016/04/05 19:58:50
I'm redoing this anyway by having it use QuicDataT
|
+ void OnDataReceived(net::QuicStreamId id, const char* data, size_t len) { |
+ incoming_stream_->SignalDataReceived.disconnect(this); |
+ ASSERT_EQ(incoming_stream_->id(), id); |
+ // Consume bytes for data channel id and retrieve the data channel which |
+ // will handle the message. |
+ rtc::ByteBuffer remaining_bytes(data, len, |
+ rtc::ByteBuffer::ByteOrder::ORDER_HOST); |
+ uint64_t data_channel_id; |
+ ASSERT_TRUE(remaining_bytes.ReadVarint(&data_channel_id)); |
+ const auto& kv = data_channel_map_.find(data_channel_id); |
+ // Creating a new data channel via a hello message is not implemented yet, |
+ // so it is assumed the data channel exists. |
+ ASSERT_TRUE(kv != data_channel_map_.end()); |
+ QuicDataChannel* data_channel = kv->second; |
+ data_channel->OnIncomingStream(incoming_stream_, &remaining_bytes); |
+ } |
+ |
+ // Connects |ice_transport_channel_| to that of the other peer. |
+ void Connect(QuicDataChannelPeer* other_peer) { |
+ ice_transport_channel_.Connect(); |
+ other_peer->ice_transport_channel_.Connect(); |
+ ice_transport_channel_.SetDestination(&other_peer->ice_transport_channel_); |
+ } |
+ |
+ rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { |
+ return local_fingerprint_; |
+ } |
+ |
+ QuicTransportChannel* quic_transport_channel() { |
+ return &quic_transport_channel_; |
+ } |
+ |
+ private: |
+ // Creates a fingerprint from a certificate. |
+ rtc::SSLFingerprint* CreateFingerprint(rtc::RTCCertificate* cert) { |
+ std::string digest_algorithm; |
+ cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm); |
+ rtc::scoped_ptr<rtc::SSLFingerprint> fingerprint( |
+ rtc::SSLFingerprint::Create(digest_algorithm, cert->identity())); |
+ return fingerprint.release(); |
+ } |
+ |
+ std::vector<rtc::scoped_ptr<QuicDataChannel>> quic_data_channels_; |
+ std::unordered_map<int, QuicDataChannel*> data_channel_map_; |
+ |
+ FakeTransportChannel ice_transport_channel_; |
+ QuicTransportChannel quic_transport_channel_; |
+ |
+ rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; |
+ |
+ ReliableQuicStream* incoming_stream_ = nullptr; |
+ |
+ rtc::Thread* const signaling_thread_; |
+ rtc::Thread* const worker_thread_; |
+}; |
+ |
+class QuicDataChannelTest : public testing::Test { |
+ public: |
+ QuicDataChannelTest() |
+ : peer1_(rtc::Thread::Current(), rtc::Thread::Current()), |
+ peer2_(rtc::Thread::Current(), rtc::Thread::Current()) {} |
+ |
+ void EstablishConnection() { |
+ SetCryptoParameters(); |
+ peer1_.Connect(&peer2_); |
+ ASSERT_TRUE_WAIT(peer1_.quic_transport_channel()->writable() && |
+ peer2_.quic_transport_channel()->writable(), |
+ kTimeoutMs); |
+ } |
+ |
+ // Sets crypto parameters required for the QUIC handshake. |
+ void SetCryptoParameters() { |
+ peer1_.GenerateCertificateAndFingerprint(); |
+ peer2_.GenerateCertificateAndFingerprint(); |
+ |
+ peer1_.quic_transport_channel()->SetSslRole(rtc::SSL_CLIENT); |
+ peer2_.quic_transport_channel()->SetSslRole(rtc::SSL_SERVER); |
+ |
+ rtc::scoped_ptr<rtc::SSLFingerprint>& peer1_fingerprint = |
+ peer1_.local_fingerprint(); |
+ rtc::scoped_ptr<rtc::SSLFingerprint>& peer2_fingerprint = |
+ peer2_.local_fingerprint(); |
+ |
+ peer1_.quic_transport_channel()->SetRemoteFingerprint( |
+ peer2_fingerprint->algorithm, |
+ reinterpret_cast<const uint8_t*>(peer2_fingerprint->digest.data()), |
+ peer2_fingerprint->digest.size()); |
+ peer2_.quic_transport_channel()->SetRemoteFingerprint( |
+ peer1_fingerprint->algorithm, |
+ reinterpret_cast<const uint8_t*>(peer1_fingerprint->digest.data()), |
+ peer1_fingerprint->digest.size()); |
+ } |
+ |
+ protected: |
+ QuicDataChannelPeer peer1_; |
+ QuicDataChannelPeer peer2_; |
+}; |
+ |
+// Test that QuicDataChannel transfers messages small enough to fit into a |
+// single QUIC stream frame. |
+TEST_F(QuicDataChannelTest, TransferSmallMessage) { |
+ // Establish an encrypted connection. |
+ EstablishConnection(); |
+ // Init data channels. |
+ int data_channel_id = 2; |
+ std::string label = "label"; |
+ std::string protocol = "protocol"; |
+ QuicDataChannel* peer1_data_channel = |
+ peer1_.CreateDataChannel(data_channel_id, label, protocol); |
+ ASSERT_TRUE(peer1_data_channel->state() == |
+ webrtc::DataChannelInterface::kOpen); |
+ QuicDataChannel* peer2_data_channel = |
+ peer2_.CreateDataChannel(data_channel_id, label, protocol); |
+ ASSERT_TRUE(peer2_data_channel->state() == |
+ webrtc::DataChannelInterface::kOpen); |
+ FakeObserver peer1_observer; |
+ peer1_data_channel->RegisterObserver(&peer1_observer); |
+ FakeObserver peer2_observer; |
+ peer2_data_channel->RegisterObserver(&peer2_observer); |
+ // peer1 -> peer2 |
+ ASSERT_TRUE(peer1_data_channel->Send(kSmallBuffer1)); |
+ ASSERT_TRUE_WAIT(peer2_observer.messages_received() == 1, kTimeoutMs); |
+ EXPECT_EQ(peer2_observer.messages()[0], kSmallMessage1); |
+ // peer2 -> peer1 |
+ ASSERT_TRUE(peer2_data_channel->Send(kSmallBuffer2)); |
+ ASSERT_TRUE_WAIT(peer1_observer.messages_received() == 1, kTimeoutMs); |
+ EXPECT_EQ(peer1_observer.messages()[0], kSmallMessage2); |
+ // peer2 -> peer1 |
+ ASSERT_TRUE(peer2_data_channel->Send(kSmallBuffer3)); |
+ ASSERT_TRUE_WAIT(peer1_observer.messages_received() == 2, kTimeoutMs); |
+ EXPECT_EQ(peer1_observer.messages()[1], kSmallMessage3); |
+ // peer1 -> peer2 |
+ ASSERT_TRUE(peer1_data_channel->Send(kSmallBuffer4)); |
+ ASSERT_TRUE_WAIT(peer2_observer.messages_received() == 2, kTimeoutMs); |
+ EXPECT_EQ(peer2_observer.messages()[1], kSmallMessage4); |
+} |
+ |
+// Test that QuicDataChannel transfers messages large enough to fit into |
+// multiple QUIC stream frames, which don't violate the QUIC flow control limit. |
+// These require buffering by the QuicDataChannel. |
+TEST_F(QuicDataChannelTest, TransferLargeMessage) { |
+ // Establish an encrypted connection. |
+ EstablishConnection(); |
+ // Init data channels. |
+ int data_channel_id = 347; |
+ std::string label = "label"; |
+ std::string protocol = "protocol"; |
+ QuicDataChannel* peer1_data_channel = |
+ peer1_.CreateDataChannel(data_channel_id, label, protocol); |
+ ASSERT_TRUE(peer1_data_channel->state() == |
+ webrtc::DataChannelInterface::kOpen); |
+ QuicDataChannel* peer2_data_channel = |
+ peer2_.CreateDataChannel(data_channel_id, label, protocol); |
+ ASSERT_TRUE(peer2_data_channel->state() == |
+ webrtc::DataChannelInterface::kOpen); |
+ FakeObserver peer1_observer; |
+ peer1_data_channel->RegisterObserver(&peer1_observer); |
+ FakeObserver peer2_observer; |
+ peer2_data_channel->RegisterObserver(&peer2_observer); |
+ // peer1 -> peer2 |
+ ASSERT_TRUE(peer1_data_channel->Send(kLargeBuffer1)); |
+ ASSERT_TRUE_WAIT(peer2_observer.messages_received() == 1, kTimeoutMs); |
+ EXPECT_EQ(peer2_observer.messages()[0], kLargeMessage1); |
+ // peer2 -> peer1 |
+ ASSERT_TRUE(peer2_data_channel->Send(kLargeBuffer2)); |
+ ASSERT_TRUE_WAIT(peer1_observer.messages_received() == 1, kTimeoutMs); |
+ EXPECT_EQ(peer1_observer.messages()[0], kLargeMessage2); |
+ // peer2 -> peer1 |
+ ASSERT_TRUE(peer2_data_channel->Send(kLargeBuffer3)); |
+ ASSERT_TRUE_WAIT(peer1_observer.messages_received() == 2, kTimeoutMs); |
+ EXPECT_EQ(peer1_observer.messages()[1], kLargeMessage3); |
+ // peer1 -> peer2 |
+ ASSERT_TRUE(peer1_data_channel->Send(kLargeBuffer4)); |
+ ASSERT_TRUE_WAIT(peer2_observer.messages_received() == 2, kTimeoutMs); |
+ EXPECT_EQ(peer2_observer.messages()[1], kLargeMessage4); |
+} |
+ |
+// Test that when a message size exceeds the flow control limit (> 16KB), |
+// QuicDataChannel becomes write blocked. The first 16KB are sent to the remote |
+// peer, while the remaining bytes are buffered until the remote peer sends a |
+// WINDOW_UPDATE frame. |
+TEST_F(QuicDataChannelTest, TransferTooLargeMessage) { |
+ // Establish an encrypted connection. |
+ EstablishConnection(); |
+ int data_channel_id = 189; |
+ std::string label = "label"; |
+ std::string protocol = "protocol"; |
+ QuicDataChannel* peer1_data_channel = |
+ peer1_.CreateDataChannel(data_channel_id, label, protocol); |
+ QuicDataChannel* peer2_data_channel = |
+ peer2_.CreateDataChannel(data_channel_id, label, protocol); |
+ FakeObserver peer2_observer; |
+ peer2_data_channel->RegisterObserver(&peer2_observer); |
+ ASSERT_FALSE(peer1_data_channel->Send(kTooLargeBuffer)); |
Taylor Brandstetter
2016/04/01 23:23:42
Shouldn't the test also wait until the message is
|
+} |
+ |
+// Test that when multiple QuicDataChannels are in use, messages go to the |
+// correct data channel. |
+TEST_F(QuicDataChannelTest, TransferMultipleChannels) { |
+ // Establish an encrypted connection. |
+ EstablishConnection(); |
+ std::string label = "label"; |
+ std::string protocol = "protocol"; |
+ |
+ QuicDataChannel* peer1_data_channels[5]; |
+ QuicDataChannel* peer2_data_channels[5]; |
+ |
+ for (int i = 0; i < 5; ++i) { |
+ peer1_data_channels[i] = peer1_.CreateDataChannel(i, label, protocol); |
+ peer2_data_channels[i] = peer2_.CreateDataChannel(i, label, protocol); |
+ } |
+ for (int i = 0; i < 5; ++i) { |
+ QuicDataChannel* peer1_data_channel = peer1_data_channels[i]; |
+ QuicDataChannel* peer2_data_channel = peer2_data_channels[i]; |
+ FakeObserver peer1_observer; |
+ peer1_data_channel->RegisterObserver(&peer1_observer); |
+ FakeObserver peer2_observer; |
+ peer2_data_channel->RegisterObserver(&peer2_observer); |
+ // peer1 -> peer2 |
+ ASSERT_TRUE(peer1_data_channel->Send(kSmallBuffer1)); |
+ ASSERT_TRUE_WAIT(peer2_observer.messages_received() == 1, kTimeoutMs); |
+ EXPECT_EQ(peer2_observer.messages()[0], kSmallMessage1); |
+ } |
+} |
+ |
+// Test that the QuicDataChannel does not send before it is open. |
+TEST_F(QuicDataChannelTest, TransferDataBeforeChannelOpen) { |
+ int data_channel_id = 6; |
+ std::string label = "label"; |
+ std::string protocol = "protocol"; |
+ QuicDataChannel* data_channel = |
+ peer1_.CreateDataChannel(data_channel_id, label, protocol); |
+ ASSERT_TRUE(data_channel->state() == |
+ webrtc::DataChannelInterface::kConnecting); |
+ EXPECT_FALSE(data_channel->Send(kSmallBuffer1)); |
+} |
+ |
+// Test that the QuicDataChannel does not send after it is closed. |
+TEST_F(QuicDataChannelTest, TransferDataAfterChannelClosed) { |
+ int data_channel_id = 42; |
+ std::string label = "label"; |
+ std::string protocol = "protocol"; |
+ QuicDataChannel* data_channel = |
+ peer1_.CreateDataChannel(data_channel_id, label, protocol); |
+ data_channel->Close(); |
+ ASSERT_TRUE(data_channel->state() == webrtc::DataChannelInterface::kClosed); |
+ EXPECT_FALSE(data_channel->Send(kSmallBuffer1)); |
+} |
+ |
+// If a message is empty, nothing is sent and the QuicDataChannel returns true. |
+TEST_F(QuicDataChannelTest, TransferEmptyData) { |
+ int data_channel_id = 69; |
+ std::string label = "label"; |
+ std::string protocol = "protocol"; |
+ EstablishConnection(); |
+ QuicDataChannel* data_channel = |
+ peer1_.CreateDataChannel(data_channel_id, label, protocol); |
+ ASSERT_TRUE(data_channel->state() == webrtc::DataChannelInterface::kOpen); |
+ EXPECT_TRUE(data_channel->Send(DataBuffer(""))); |
+} |