| OLD | NEW | 
| (Empty) |  | 
 |    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/api/quicdatachannel.h" | 
 |   12  | 
 |   13 #include <map> | 
 |   14 #include <sstream> | 
 |   15 #include <string> | 
 |   16 #include <vector> | 
 |   17  | 
 |   18 #include "webrtc/base/bind.h" | 
 |   19 #include "webrtc/base/gunit.h" | 
 |   20 #include "webrtc/base/scoped_ptr.h" | 
 |   21 #include "webrtc/base/scoped_ref_ptr.h" | 
 |   22 #include "webrtc/p2p/base/faketransportcontroller.h" | 
 |   23 #include "webrtc/p2p/quic/quictransportchannel.h" | 
 |   24 #include "webrtc/p2p/quic/reliablequicstream.h" | 
 |   25  | 
 |   26 using cricket::FakeTransportChannel; | 
 |   27 using cricket::QuicTransportChannel; | 
 |   28 using cricket::ReliableQuicStream; | 
 |   29  | 
 |   30 using webrtc::DataBuffer; | 
 |   31 using webrtc::DataChannelObserver; | 
 |   32 using webrtc::DataChannelInit; | 
 |   33 using webrtc::QuicDataChannel; | 
 |   34  | 
 |   35 namespace { | 
 |   36  | 
 |   37 // Timeout for asynchronous operations. | 
 |   38 static const int kTimeoutMs = 1000;  // milliseconds | 
 |   39  | 
 |   40 // Small messages that can be sent within a single QUIC packet. | 
 |   41 static const std::string kSmallMessage1 = "Hello, world!"; | 
 |   42 static const std::string kSmallMessage2 = "WebRTC"; | 
 |   43 static const std::string kSmallMessage3 = "1"; | 
 |   44 static const std::string kSmallMessage4 = "abcdefghijklmnopqrstuvwxyz"; | 
 |   45 static const DataBuffer kSmallBuffer1(kSmallMessage1); | 
 |   46 static const DataBuffer kSmallBuffer2(kSmallMessage2); | 
 |   47 static const DataBuffer kSmallBuffer3(kSmallMessage3); | 
 |   48 static const DataBuffer kSmallBuffer4(kSmallMessage4); | 
 |   49  | 
 |   50 // Large messages (> 1350 bytes) that exceed the max size of a QUIC packet. | 
 |   51 // These are < 16 KB so they don't exceed the QUIC stream flow control limit. | 
 |   52 static const std::string kLargeMessage1 = std::string("a", 2000); | 
 |   53 static const std::string kLargeMessage2 = std::string("a", 4000); | 
 |   54 static const std::string kLargeMessage3 = std::string("a", 8000); | 
 |   55 static const std::string kLargeMessage4 = std::string("a", 12000); | 
 |   56 static const DataBuffer kLargeBuffer1(kLargeMessage1); | 
 |   57 static const DataBuffer kLargeBuffer2(kLargeMessage2); | 
 |   58 static const DataBuffer kLargeBuffer3(kLargeMessage3); | 
 |   59 static const DataBuffer kLargeBuffer4(kLargeMessage4); | 
 |   60  | 
 |   61 // Oversized message (> 16 KB) that violates the QUIC stream flow control limit. | 
 |   62 static const std::string kOversizedMessage = std::string("a", 20000); | 
 |   63 static const DataBuffer kOversizedBuffer(kOversizedMessage); | 
 |   64  | 
 |   65 // Creates a fingerprint from a certificate. | 
 |   66 static rtc::SSLFingerprint* CreateFingerprint(rtc::RTCCertificate* cert) { | 
 |   67   std::string digest_algorithm; | 
 |   68   cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm); | 
 |   69   rtc::scoped_ptr<rtc::SSLFingerprint> fingerprint( | 
 |   70       rtc::SSLFingerprint::Create(digest_algorithm, cert->identity())); | 
 |   71   return fingerprint.release(); | 
 |   72 } | 
 |   73  | 
 |   74 // FakeObserver receives messages from the QuicDataChannel. | 
 |   75 class FakeObserver : public DataChannelObserver { | 
 |   76  public: | 
 |   77   FakeObserver() | 
 |   78       : on_state_change_count_(0), on_buffered_amount_change_count_(0) {} | 
 |   79  | 
 |   80   // DataChannelObserver overrides. | 
 |   81   void OnStateChange() override { ++on_state_change_count_; } | 
 |   82   void OnBufferedAmountChange(uint64_t previous_amount) override { | 
 |   83     ++on_buffered_amount_change_count_; | 
 |   84   } | 
 |   85   void OnMessage(const webrtc::DataBuffer& buffer) override { | 
 |   86     messages_.push_back(std::string(buffer.data.data<char>(), buffer.size())); | 
 |   87   } | 
 |   88  | 
 |   89   const std::vector<std::string>& messages() const { return messages_; } | 
 |   90  | 
 |   91   size_t messages_received() const { return messages_.size(); } | 
 |   92  | 
 |   93   size_t on_state_change_count() const { return on_state_change_count_; } | 
 |   94  | 
 |   95   size_t on_buffered_amount_change_count() const { | 
 |   96     return on_buffered_amount_change_count_; | 
 |   97   } | 
 |   98  | 
 |   99  private: | 
 |  100   std::vector<std::string> messages_; | 
 |  101   size_t on_state_change_count_; | 
 |  102   size_t on_buffered_amount_change_count_; | 
 |  103 }; | 
 |  104  | 
 |  105 // FakeQuicDataTransport simulates QuicDataTransport by dispatching QUIC | 
 |  106 // stream messages to data channels and encoding/decoding messages. | 
 |  107 class FakeQuicDataTransport : public sigslot::has_slots<> { | 
 |  108  public: | 
 |  109   FakeQuicDataTransport() {} | 
 |  110  | 
 |  111   void ConnectToTransportChannel(QuicTransportChannel* quic_transport_channel) { | 
 |  112     quic_transport_channel->SignalIncomingStream.connect( | 
 |  113         this, &FakeQuicDataTransport::OnIncomingStream); | 
 |  114   } | 
 |  115  | 
 |  116   rtc::scoped_refptr<QuicDataChannel> CreateDataChannel( | 
 |  117       int id, | 
 |  118       const std::string& label, | 
 |  119       const std::string& protocol) { | 
 |  120     DataChannelInit config; | 
 |  121     config.id = id; | 
 |  122     config.protocol = protocol; | 
 |  123     rtc::scoped_refptr<QuicDataChannel> data_channel(new QuicDataChannel( | 
 |  124         rtc::Thread::Current(), rtc::Thread::Current(), label, config)); | 
 |  125     data_channel_by_id_[id] = data_channel; | 
 |  126     return data_channel; | 
 |  127   } | 
 |  128  | 
 |  129  private: | 
 |  130   void OnIncomingStream(cricket::ReliableQuicStream* stream) { | 
 |  131     incoming_stream_ = stream; | 
 |  132     incoming_stream_->SignalDataReceived.connect( | 
 |  133         this, &FakeQuicDataTransport::OnDataReceived); | 
 |  134   } | 
 |  135  | 
 |  136   void OnDataReceived(net::QuicStreamId id, const char* data, size_t len) { | 
 |  137     ASSERT_EQ(incoming_stream_->id(), id); | 
 |  138     incoming_stream_->SignalDataReceived.disconnect(this); | 
 |  139     // Retrieve the data channel ID and message ID. | 
 |  140     int data_channel_id; | 
 |  141     uint64_t message_id; | 
 |  142     size_t bytes_read; | 
 |  143     ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader(data, len, &data_channel_id, | 
 |  144                                                    &message_id, &bytes_read)); | 
 |  145     data += bytes_read; | 
 |  146     len -= bytes_read; | 
 |  147     // Dispatch the message to the matching QuicDataChannel. | 
 |  148     const auto& kv = data_channel_by_id_.find(data_channel_id); | 
 |  149     ASSERT_NE(kv, data_channel_by_id_.end()); | 
 |  150     QuicDataChannel* data_channel = kv->second; | 
 |  151     QuicDataChannel::Message message; | 
 |  152     message.id = message_id; | 
 |  153     message.buffer = rtc::CopyOnWriteBuffer(data, len); | 
 |  154     message.stream = incoming_stream_; | 
 |  155     data_channel->OnIncomingMessage(std::move(message)); | 
 |  156     incoming_stream_ = nullptr; | 
 |  157   } | 
 |  158  | 
 |  159   // Map of data channel ID => QuicDataChannel. | 
 |  160   std::map<int, rtc::scoped_refptr<QuicDataChannel>> data_channel_by_id_; | 
 |  161   // Last incoming QUIC stream which has arrived. | 
 |  162   cricket::ReliableQuicStream* incoming_stream_ = nullptr; | 
 |  163 }; | 
 |  164  | 
 |  165 // A peer who creates a QuicDataChannel to transfer data, and simulates network | 
 |  166 // connectivity with a fake ICE channel wrapped by the QUIC transport channel. | 
 |  167 class QuicDataChannelPeer { | 
 |  168  public: | 
 |  169   QuicDataChannelPeer() | 
 |  170       : ice_transport_channel_("data", 0), | 
 |  171         quic_transport_channel_(&ice_transport_channel_) { | 
 |  172     ice_transport_channel_.SetAsync(true); | 
 |  173     fake_quic_data_transport_.ConnectToTransportChannel( | 
 |  174         &quic_transport_channel_); | 
 |  175   } | 
 |  176  | 
 |  177   void GenerateCertificateAndFingerprint() { | 
 |  178     rtc::scoped_refptr<rtc::RTCCertificate> local_cert = | 
 |  179         rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( | 
 |  180             rtc::SSLIdentity::Generate("cert_name", rtc::KT_DEFAULT))); | 
 |  181     quic_transport_channel_.SetLocalCertificate(local_cert); | 
 |  182     local_fingerprint_.reset(CreateFingerprint(local_cert.get())); | 
 |  183   } | 
 |  184  | 
 |  185   rtc::scoped_refptr<QuicDataChannel> CreateDataChannelWithTransportChannel( | 
 |  186       int id, | 
 |  187       const std::string& label, | 
 |  188       const std::string& protocol) { | 
 |  189     rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  190         fake_quic_data_transport_.CreateDataChannel(id, label, protocol); | 
 |  191     data_channel->SetTransportChannel(&quic_transport_channel_); | 
 |  192     return data_channel; | 
 |  193   } | 
 |  194  | 
 |  195   rtc::scoped_refptr<QuicDataChannel> CreateDataChannelWithoutTransportChannel( | 
 |  196       int id, | 
 |  197       const std::string& label, | 
 |  198       const std::string& protocol) { | 
 |  199     return fake_quic_data_transport_.CreateDataChannel(id, label, protocol); | 
 |  200   } | 
 |  201  | 
 |  202   // Connects |ice_transport_channel_| to that of the other peer. | 
 |  203   void Connect(QuicDataChannelPeer* other_peer) { | 
 |  204     ice_transport_channel_.Connect(); | 
 |  205     other_peer->ice_transport_channel_.Connect(); | 
 |  206     ice_transport_channel_.SetDestination(&other_peer->ice_transport_channel_); | 
 |  207   } | 
 |  208  | 
 |  209   rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { | 
 |  210     return local_fingerprint_; | 
 |  211   } | 
 |  212  | 
 |  213   QuicTransportChannel* quic_transport_channel() { | 
 |  214     return &quic_transport_channel_; | 
 |  215   } | 
 |  216  | 
 |  217   FakeTransportChannel* ice_transport_channel() { | 
 |  218     return &ice_transport_channel_; | 
 |  219   } | 
 |  220  | 
 |  221  private: | 
 |  222   FakeTransportChannel ice_transport_channel_; | 
 |  223   QuicTransportChannel quic_transport_channel_; | 
 |  224  | 
 |  225   rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; | 
 |  226  | 
 |  227   FakeQuicDataTransport fake_quic_data_transport_; | 
 |  228 }; | 
 |  229  | 
 |  230 class QuicDataChannelTest : public testing::Test { | 
 |  231  public: | 
 |  232   QuicDataChannelTest() {} | 
 |  233  | 
 |  234   // Connect the QuicTransportChannels and complete the crypto handshake. | 
 |  235   void ConnectTransportChannels() { | 
 |  236     SetCryptoParameters(); | 
 |  237     peer1_.Connect(&peer2_); | 
 |  238     ASSERT_TRUE_WAIT(peer1_.quic_transport_channel()->writable() && | 
 |  239                          peer2_.quic_transport_channel()->writable(), | 
 |  240                      kTimeoutMs); | 
 |  241   } | 
 |  242  | 
 |  243   // Sets crypto parameters required for the QUIC handshake. | 
 |  244   void SetCryptoParameters() { | 
 |  245     peer1_.GenerateCertificateAndFingerprint(); | 
 |  246     peer2_.GenerateCertificateAndFingerprint(); | 
 |  247  | 
 |  248     peer1_.quic_transport_channel()->SetSslRole(rtc::SSL_CLIENT); | 
 |  249     peer2_.quic_transport_channel()->SetSslRole(rtc::SSL_SERVER); | 
 |  250  | 
 |  251     rtc::scoped_ptr<rtc::SSLFingerprint>& peer1_fingerprint = | 
 |  252         peer1_.local_fingerprint(); | 
 |  253     rtc::scoped_ptr<rtc::SSLFingerprint>& peer2_fingerprint = | 
 |  254         peer2_.local_fingerprint(); | 
 |  255  | 
 |  256     peer1_.quic_transport_channel()->SetRemoteFingerprint( | 
 |  257         peer2_fingerprint->algorithm, | 
 |  258         reinterpret_cast<const uint8_t*>(peer2_fingerprint->digest.data()), | 
 |  259         peer2_fingerprint->digest.size()); | 
 |  260     peer2_.quic_transport_channel()->SetRemoteFingerprint( | 
 |  261         peer1_fingerprint->algorithm, | 
 |  262         reinterpret_cast<const uint8_t*>(peer1_fingerprint->digest.data()), | 
 |  263         peer1_fingerprint->digest.size()); | 
 |  264   } | 
 |  265  | 
 |  266  protected: | 
 |  267   QuicDataChannelPeer peer1_; | 
 |  268   QuicDataChannelPeer peer2_; | 
 |  269 }; | 
 |  270  | 
 |  271 // Tests that a QuicDataChannel transitions from connecting to open when | 
 |  272 // the QuicTransportChannel becomes writable for the first time. | 
 |  273 TEST_F(QuicDataChannelTest, DataChannelOpensWhenTransportChannelConnects) { | 
 |  274   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  275       peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | 
 |  276   EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | 
 |  277   ConnectTransportChannels(); | 
 |  278   EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, data_channel->state(), | 
 |  279                  kTimeoutMs); | 
 |  280 } | 
 |  281  | 
 |  282 // Tests that a QuicDataChannel transitions from connecting to open when | 
 |  283 // SetTransportChannel is called with a QuicTransportChannel that is already | 
 |  284 // writable. | 
 |  285 TEST_F(QuicDataChannelTest, DataChannelOpensWhenTransportChannelWritable) { | 
 |  286   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  287       peer1_.CreateDataChannelWithoutTransportChannel(4, "label", "protocol"); | 
 |  288   ConnectTransportChannels(); | 
 |  289   EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | 
 |  290   data_channel->SetTransportChannel(peer1_.quic_transport_channel()); | 
 |  291   EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | 
 |  292 } | 
 |  293  | 
 |  294 // Tests that the QuicDataChannel transfers messages small enough to fit into a | 
 |  295 // single QUIC stream frame. | 
 |  296 TEST_F(QuicDataChannelTest, TransferSmallMessage) { | 
 |  297   ConnectTransportChannels(); | 
 |  298   int data_channel_id = 2; | 
 |  299   std::string label = "label"; | 
 |  300   std::string protocol = "protocol"; | 
 |  301   rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | 
 |  302       peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  303                                                    protocol); | 
 |  304   ASSERT_TRUE(peer1_data_channel->state() == | 
 |  305               webrtc::DataChannelInterface::kOpen); | 
 |  306   rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | 
 |  307       peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  308                                                    protocol); | 
 |  309   ASSERT_TRUE(peer2_data_channel->state() == | 
 |  310               webrtc::DataChannelInterface::kOpen); | 
 |  311  | 
 |  312   FakeObserver peer1_observer; | 
 |  313   peer1_data_channel->RegisterObserver(&peer1_observer); | 
 |  314   FakeObserver peer2_observer; | 
 |  315   peer2_data_channel->RegisterObserver(&peer2_observer); | 
 |  316  | 
 |  317   // peer1 -> peer2 | 
 |  318   EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer1)); | 
 |  319   ASSERT_EQ_WAIT(1, peer2_observer.messages_received(), kTimeoutMs); | 
 |  320   EXPECT_EQ(kSmallMessage1, peer2_observer.messages()[0]); | 
 |  321   // peer2 -> peer1 | 
 |  322   EXPECT_TRUE(peer2_data_channel->Send(kSmallBuffer2)); | 
 |  323   ASSERT_EQ_WAIT(1, peer1_observer.messages_received(), kTimeoutMs); | 
 |  324   EXPECT_EQ(kSmallMessage2, peer1_observer.messages()[0]); | 
 |  325   // peer2 -> peer1 | 
 |  326   EXPECT_TRUE(peer2_data_channel->Send(kSmallBuffer3)); | 
 |  327   ASSERT_EQ_WAIT(2, peer1_observer.messages_received(), kTimeoutMs); | 
 |  328   EXPECT_EQ(kSmallMessage3, peer1_observer.messages()[1]); | 
 |  329   // peer1 -> peer2 | 
 |  330   EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer4)); | 
 |  331   ASSERT_EQ_WAIT(2, peer2_observer.messages_received(), kTimeoutMs); | 
 |  332   EXPECT_EQ(kSmallMessage4, peer2_observer.messages()[1]); | 
 |  333 } | 
 |  334  | 
 |  335 // Tests that QuicDataChannel transfers messages large enough to fit into | 
 |  336 // multiple QUIC stream frames, which don't violate the QUIC flow control limit. | 
 |  337 // These require buffering by the QuicDataChannel. | 
 |  338 TEST_F(QuicDataChannelTest, TransferLargeMessage) { | 
 |  339   ConnectTransportChannels(); | 
 |  340   int data_channel_id = 347; | 
 |  341   std::string label = "label"; | 
 |  342   std::string protocol = "protocol"; | 
 |  343   rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | 
 |  344       peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  345                                                    protocol); | 
 |  346   ASSERT_TRUE(peer1_data_channel->state() == | 
 |  347               webrtc::DataChannelInterface::kOpen); | 
 |  348   rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | 
 |  349       peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  350                                                    protocol); | 
 |  351   ASSERT_TRUE(peer2_data_channel->state() == | 
 |  352               webrtc::DataChannelInterface::kOpen); | 
 |  353  | 
 |  354   FakeObserver peer1_observer; | 
 |  355   peer1_data_channel->RegisterObserver(&peer1_observer); | 
 |  356   FakeObserver peer2_observer; | 
 |  357   peer2_data_channel->RegisterObserver(&peer2_observer); | 
 |  358  | 
 |  359   // peer1 -> peer2 | 
 |  360   EXPECT_TRUE(peer1_data_channel->Send(kLargeBuffer1)); | 
 |  361   ASSERT_TRUE_WAIT(peer2_observer.messages_received() == 1, kTimeoutMs); | 
 |  362   EXPECT_EQ(kLargeMessage1, peer2_observer.messages()[0]); | 
 |  363   // peer2 -> peer1 | 
 |  364   EXPECT_TRUE(peer2_data_channel->Send(kLargeBuffer2)); | 
 |  365   ASSERT_EQ_WAIT(1, peer1_observer.messages_received(), kTimeoutMs); | 
 |  366   EXPECT_EQ(kLargeMessage2, peer1_observer.messages()[0]); | 
 |  367   // peer2 -> peer1 | 
 |  368   EXPECT_TRUE(peer2_data_channel->Send(kLargeBuffer3)); | 
 |  369   ASSERT_EQ_WAIT(2, peer1_observer.messages_received(), kTimeoutMs); | 
 |  370   EXPECT_EQ(kLargeMessage3, peer1_observer.messages()[1]); | 
 |  371   // peer1 -> peer2 | 
 |  372   EXPECT_TRUE(peer1_data_channel->Send(kLargeBuffer4)); | 
 |  373   ASSERT_EQ_WAIT(2, peer2_observer.messages_received(), kTimeoutMs); | 
 |  374   EXPECT_EQ(kLargeMessage4, peer2_observer.messages()[1]); | 
 |  375 } | 
 |  376  | 
 |  377 // Tests that when a message size exceeds the flow control limit (> 16KB), the | 
 |  378 // QuicDataChannel can queue the data and send it after receiving window update | 
 |  379 // frames from the remote peer. | 
 |  380 TEST_F(QuicDataChannelTest, TransferOversizedMessage) { | 
 |  381   ConnectTransportChannels(); | 
 |  382   int data_channel_id = 189; | 
 |  383   std::string label = "label"; | 
 |  384   std::string protocol = "protocol"; | 
 |  385   rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | 
 |  386       peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  387                                                    protocol); | 
 |  388   rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | 
 |  389       peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  390                                                    protocol); | 
 |  391   ASSERT_TRUE(peer2_data_channel->state() == | 
 |  392               webrtc::DataChannelInterface::kOpen); | 
 |  393  | 
 |  394   FakeObserver peer1_observer; | 
 |  395   peer1_data_channel->RegisterObserver(&peer1_observer); | 
 |  396   FakeObserver peer2_observer; | 
 |  397   peer2_data_channel->RegisterObserver(&peer2_observer); | 
 |  398  | 
 |  399   EXPECT_TRUE(peer1_data_channel->Send(kOversizedBuffer)); | 
 |  400   EXPECT_EQ(1, peer1_data_channel->GetNumWriteBlockedStreams()); | 
 |  401   EXPECT_EQ_WAIT(1, peer2_data_channel->GetNumIncomingStreams(), kTimeoutMs); | 
 |  402   ASSERT_EQ_WAIT(1, peer2_observer.messages_received(), kTimeoutMs); | 
 |  403   EXPECT_EQ(kOversizedMessage, peer2_observer.messages()[0]); | 
 |  404   EXPECT_EQ(0, peer1_data_channel->GetNumWriteBlockedStreams()); | 
 |  405   EXPECT_EQ(0, peer2_data_channel->GetNumIncomingStreams()); | 
 |  406 } | 
 |  407  | 
 |  408 // Tests that empty messages can be sent. | 
 |  409 TEST_F(QuicDataChannelTest, TransferEmptyMessage) { | 
 |  410   ConnectTransportChannels(); | 
 |  411   int data_channel_id = 69; | 
 |  412   std::string label = "label"; | 
 |  413   std::string protocol = "protocol"; | 
 |  414   rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | 
 |  415       peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  416                                                    protocol); | 
 |  417   rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | 
 |  418       peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  419                                                    protocol); | 
 |  420   ASSERT_TRUE(peer2_data_channel->state() == | 
 |  421               webrtc::DataChannelInterface::kOpen); | 
 |  422  | 
 |  423   FakeObserver peer1_observer; | 
 |  424   peer1_data_channel->RegisterObserver(&peer1_observer); | 
 |  425   FakeObserver peer2_observer; | 
 |  426   peer2_data_channel->RegisterObserver(&peer2_observer); | 
 |  427  | 
 |  428   EXPECT_TRUE(peer1_data_channel->Send(DataBuffer(""))); | 
 |  429   ASSERT_EQ_WAIT(1, peer2_observer.messages_received(), kTimeoutMs); | 
 |  430   EXPECT_EQ("", peer2_observer.messages()[0]); | 
 |  431 } | 
 |  432  | 
 |  433 // Tests that when the QuicDataChannel is open and sends a message while the | 
 |  434 // QuicTransportChannel is unwritable, it gets buffered then received once the | 
 |  435 // QuicTransportChannel becomes writable again. | 
 |  436 TEST_F(QuicDataChannelTest, MessagesReceivedWhenTransportChannelReconnects) { | 
 |  437   ConnectTransportChannels(); | 
 |  438   int data_channel_id = 401; | 
 |  439   std::string label = "label"; | 
 |  440   std::string protocol = "protocol"; | 
 |  441   rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | 
 |  442       peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  443                                                    protocol); | 
 |  444   ASSERT_TRUE(peer1_data_channel->state() == | 
 |  445               webrtc::DataChannelInterface::kOpen); | 
 |  446   rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | 
 |  447       peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | 
 |  448                                                    protocol); | 
 |  449   ASSERT_TRUE(peer2_data_channel->state() == | 
 |  450               webrtc::DataChannelInterface::kOpen); | 
 |  451  | 
 |  452   FakeObserver peer1_observer; | 
 |  453   peer1_data_channel->RegisterObserver(&peer1_observer); | 
 |  454   FakeObserver peer2_observer; | 
 |  455   peer2_data_channel->RegisterObserver(&peer2_observer); | 
 |  456   // writable => unwritable | 
 |  457   peer1_.ice_transport_channel()->SetWritable(false); | 
 |  458   ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); | 
 |  459   // Verify that sent data is buffered. | 
 |  460   EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer1)); | 
 |  461   EXPECT_EQ(1, peer1_data_channel->GetNumWriteBlockedStreams()); | 
 |  462   EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer2)); | 
 |  463   EXPECT_EQ(2, peer1_data_channel->GetNumWriteBlockedStreams()); | 
 |  464   EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer3)); | 
 |  465   EXPECT_EQ(3, peer1_data_channel->GetNumWriteBlockedStreams()); | 
 |  466   EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer4)); | 
 |  467   EXPECT_EQ(4, peer1_data_channel->GetNumWriteBlockedStreams()); | 
 |  468   // unwritable => writable | 
 |  469   peer1_.ice_transport_channel()->SetWritable(true); | 
 |  470   ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); | 
 |  471   ASSERT_EQ_WAIT(4, peer2_observer.messages_received(), kTimeoutMs); | 
 |  472   EXPECT_EQ(0, peer1_data_channel->GetNumWriteBlockedStreams()); | 
 |  473   EXPECT_EQ(0, peer2_data_channel->GetNumIncomingStreams()); | 
 |  474 } | 
 |  475  | 
 |  476 // Tests that the QuicDataChannel does not send before it is open. | 
 |  477 TEST_F(QuicDataChannelTest, TransferMessageBeforeChannelOpens) { | 
 |  478   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  479       peer1_.CreateDataChannelWithTransportChannel(6, "label", "protocol"); | 
 |  480   ASSERT_TRUE(data_channel->state() == | 
 |  481               webrtc::DataChannelInterface::kConnecting); | 
 |  482   EXPECT_FALSE(data_channel->Send(kSmallBuffer1)); | 
 |  483 } | 
 |  484  | 
 |  485 // Tests that the QuicDataChannel does not send after it is closed. | 
 |  486 TEST_F(QuicDataChannelTest, TransferDataAfterChannelClosed) { | 
 |  487   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  488       peer1_.CreateDataChannelWithTransportChannel(42, "label", "protocol"); | 
 |  489   data_channel->Close(); | 
 |  490   ASSERT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | 
 |  491                  kTimeoutMs); | 
 |  492   EXPECT_FALSE(data_channel->Send(kSmallBuffer1)); | 
 |  493 } | 
 |  494  | 
 |  495 // Tests that QuicDataChannel state changes fire OnStateChanged() for the | 
 |  496 // observer, with the correct data channel states, when the data channel | 
 |  497 // transitions from kConnecting => kOpen => kClosing => kClosed. | 
 |  498 TEST_F(QuicDataChannelTest, OnStateChangedFired) { | 
 |  499   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  500       peer1_.CreateDataChannelWithTransportChannel(7, "label", "protocol"); | 
 |  501   FakeObserver observer; | 
 |  502   data_channel->RegisterObserver(&observer); | 
 |  503   EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | 
 |  504   EXPECT_EQ(0, observer.on_state_change_count()); | 
 |  505   ConnectTransportChannels(); | 
 |  506   EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, data_channel->state(), | 
 |  507                  kTimeoutMs); | 
 |  508   EXPECT_EQ(1, observer.on_state_change_count()); | 
 |  509   data_channel->Close(); | 
 |  510   // Closing the QuicDataChannel is completed on the worker thread. | 
 |  511   EXPECT_EQ(webrtc::DataChannelInterface::kClosing, data_channel->state()); | 
 |  512   EXPECT_EQ(2, observer.on_state_change_count()); | 
 |  513   EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | 
 |  514                  kTimeoutMs); | 
 |  515   EXPECT_EQ(3, observer.on_state_change_count()); | 
 |  516 } | 
 |  517  | 
 |  518 // Tests that a QuicTransportChannel can be closed without being opened when it | 
 |  519 // is connected to a transprot chanenl. | 
 |  520 TEST_F(QuicDataChannelTest, NeverOpenedWithTransportChannel) { | 
 |  521   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  522       peer1_.CreateDataChannelWithTransportChannel(7, "label", "protocol"); | 
 |  523   EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | 
 |  524   data_channel->Close(); | 
 |  525   EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | 
 |  526                  kTimeoutMs); | 
 |  527 } | 
 |  528  | 
 |  529 // Tests that a QuicTransportChannel can be closed without being opened or | 
 |  530 // connected to a transport channel. | 
 |  531 TEST_F(QuicDataChannelTest, NeverOpenedWithoutTransportChannel) { | 
 |  532   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  533       peer1_.CreateDataChannelWithoutTransportChannel(7, "label", "protocol"); | 
 |  534   EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | 
 |  535   data_channel->Close(); | 
 |  536   EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | 
 |  537                  kTimeoutMs); | 
 |  538 } | 
 |  539  | 
 |  540 // Tests that the QuicDataChannel is closed when the QUIC connection closes. | 
 |  541 TEST_F(QuicDataChannelTest, ClosedOnTransportError) { | 
 |  542   ConnectTransportChannels(); | 
 |  543   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  544       peer1_.CreateDataChannelWithTransportChannel(1, "label", "protocol"); | 
 |  545   EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | 
 |  546   ReliableQuicStream* stream = | 
 |  547       peer1_.quic_transport_channel()->CreateQuicStream(); | 
 |  548   ASSERT_NE(nullptr, stream); | 
 |  549   stream->CloseConnectionWithDetails(net::QuicErrorCode::QUIC_NO_ERROR, | 
 |  550                                      "Closing QUIC for testing"); | 
 |  551   EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | 
 |  552                  kTimeoutMs); | 
 |  553 } | 
 |  554  | 
 |  555 // Tests that an already closed QuicDataChannel does not fire onStateChange and | 
 |  556 // remains closed. | 
 |  557 TEST_F(QuicDataChannelTest, DoesNotChangeStateWhenClosed) { | 
 |  558   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  559       peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | 
 |  560   FakeObserver observer; | 
 |  561   data_channel->RegisterObserver(&observer); | 
 |  562   data_channel->Close(); | 
 |  563   EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | 
 |  564                  kTimeoutMs); | 
 |  565   // OnStateChange called for kClosing and kClosed. | 
 |  566   EXPECT_EQ(2, observer.on_state_change_count()); | 
 |  567   // Call Close() again to verify that the state cannot be kClosing. | 
 |  568   data_channel->Close(); | 
 |  569   EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | 
 |  570   EXPECT_EQ(2, observer.on_state_change_count()); | 
 |  571   ConnectTransportChannels(); | 
 |  572   EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | 
 |  573   EXPECT_EQ(2, observer.on_state_change_count()); | 
 |  574   // writable => unwritable | 
 |  575   peer1_.ice_transport_channel()->SetWritable(false); | 
 |  576   ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); | 
 |  577   EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | 
 |  578   EXPECT_EQ(2, observer.on_state_change_count()); | 
 |  579   // unwritable => writable | 
 |  580   peer1_.ice_transport_channel()->SetWritable(true); | 
 |  581   ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); | 
 |  582   EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | 
 |  583   EXPECT_EQ(2, observer.on_state_change_count()); | 
 |  584 } | 
 |  585  | 
 |  586 // Tests that when the QuicDataChannel is open and the QuicTransportChannel | 
 |  587 // transitions between writable and unwritable, it does not fire onStateChange | 
 |  588 // and remains open. | 
 |  589 TEST_F(QuicDataChannelTest, DoesNotChangeStateWhenTransportChannelReconnects) { | 
 |  590   ConnectTransportChannels(); | 
 |  591   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  592       peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | 
 |  593   FakeObserver observer; | 
 |  594   data_channel->RegisterObserver(&observer); | 
 |  595   EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | 
 |  596   EXPECT_EQ(0, observer.on_state_change_count()); | 
 |  597   // writable => unwritable | 
 |  598   peer1_.ice_transport_channel()->SetWritable(false); | 
 |  599   ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); | 
 |  600   EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | 
 |  601   EXPECT_EQ(0, observer.on_state_change_count()); | 
 |  602   // unwritable => writable | 
 |  603   peer1_.ice_transport_channel()->SetWritable(true); | 
 |  604   ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); | 
 |  605   EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | 
 |  606   EXPECT_EQ(0, observer.on_state_change_count()); | 
 |  607 } | 
 |  608  | 
 |  609 // Tests that SetTransportChannel returns false when setting a NULL transport | 
 |  610 // channel or a transport channel that is not equivalent to the one already set. | 
 |  611 TEST_F(QuicDataChannelTest, SetTransportChannelReturnValue) { | 
 |  612   rtc::scoped_refptr<QuicDataChannel> data_channel = | 
 |  613       peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | 
 |  614   EXPECT_FALSE(data_channel->SetTransportChannel(nullptr)); | 
 |  615   QuicTransportChannel* transport_channel = peer1_.quic_transport_channel(); | 
 |  616   EXPECT_TRUE(data_channel->SetTransportChannel(transport_channel)); | 
 |  617   EXPECT_TRUE(data_channel->SetTransportChannel(transport_channel)); | 
 |  618   QuicTransportChannel* other_transport_channel = | 
 |  619       peer2_.quic_transport_channel(); | 
 |  620   EXPECT_FALSE(data_channel->SetTransportChannel(other_transport_channel)); | 
 |  621 } | 
 |  622  | 
 |  623 // Tests that the QUIC message header is encoded with the correct number of | 
 |  624 // bytes and is properly decoded. | 
 |  625 TEST_F(QuicDataChannelTest, EncodeParseQuicDataMessageHeader) { | 
 |  626   int data_channel_id1 = 127;  // 1 byte | 
 |  627   uint64_t message_id1 = 0;    // 1 byte | 
 |  628   rtc::CopyOnWriteBuffer header1; | 
 |  629   webrtc::WriteQuicDataChannelMessageHeader(data_channel_id1, message_id1, | 
 |  630                                             &header1); | 
 |  631   EXPECT_EQ(2u, header1.size()); | 
 |  632  | 
 |  633   int decoded_data_channel_id1; | 
 |  634   uint64_t decoded_message_id1; | 
 |  635   size_t bytes_read1; | 
 |  636   ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader( | 
 |  637       header1.data<char>(), header1.size(), &decoded_data_channel_id1, | 
 |  638       &decoded_message_id1, &bytes_read1)); | 
 |  639   EXPECT_EQ(data_channel_id1, decoded_data_channel_id1); | 
 |  640   EXPECT_EQ(message_id1, decoded_message_id1); | 
 |  641   EXPECT_EQ(2u, bytes_read1); | 
 |  642  | 
 |  643   int data_channel_id2 = 4178;           // 2 bytes | 
 |  644   uint64_t message_id2 = 1324921792003;  // 6 bytes | 
 |  645   rtc::CopyOnWriteBuffer header2; | 
 |  646   webrtc::WriteQuicDataChannelMessageHeader(data_channel_id2, message_id2, | 
 |  647                                             &header2); | 
 |  648   EXPECT_EQ(8u, header2.size()); | 
 |  649  | 
 |  650   int decoded_data_channel_id2; | 
 |  651   uint64_t decoded_message_id2; | 
 |  652   size_t bytes_read2; | 
 |  653   ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader( | 
 |  654       header2.data<char>(), header2.size(), &decoded_data_channel_id2, | 
 |  655       &decoded_message_id2, &bytes_read2)); | 
 |  656   EXPECT_EQ(data_channel_id2, decoded_data_channel_id2); | 
 |  657   EXPECT_EQ(message_id2, decoded_message_id2); | 
 |  658   EXPECT_EQ(8u, bytes_read2); | 
 |  659 } | 
 |  660  | 
 |  661 }  // namespace | 
| OLD | NEW |