mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 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> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 14 | #include <memory> |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 15 | #include <sstream> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "webrtc/base/bind.h" |
| 20 | #include "webrtc/base/gunit.h" |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 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); |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 69 | std::unique_ptr<rtc::SSLFingerprint> fingerprint( |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 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() |
mikescarlett | a97611a | 2016-04-29 22:09:27 -0700 | [diff] [blame] | 170 | : ice_transport_channel_(new FakeTransportChannel("data", 0)), |
| 171 | quic_transport_channel_(ice_transport_channel_) { |
| 172 | ice_transport_channel_->SetAsync(true); |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 173 | fake_quic_data_transport_.ConnectToTransportChannel( |
| 174 | &quic_transport_channel_); |
| 175 | } |
| 176 | |
| 177 | void GenerateCertificateAndFingerprint() { |
| 178 | rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 179 | rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 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) { |
mikescarlett | a97611a | 2016-04-29 22:09:27 -0700 | [diff] [blame] | 204 | ice_transport_channel_->Connect(); |
| 205 | other_peer->ice_transport_channel_->Connect(); |
| 206 | ice_transport_channel_->SetDestination(other_peer->ice_transport_channel_); |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 207 | } |
| 208 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 209 | std::unique_ptr<rtc::SSLFingerprint>& local_fingerprint() { |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 210 | return local_fingerprint_; |
| 211 | } |
| 212 | |
| 213 | QuicTransportChannel* quic_transport_channel() { |
| 214 | return &quic_transport_channel_; |
| 215 | } |
| 216 | |
| 217 | FakeTransportChannel* ice_transport_channel() { |
mikescarlett | a97611a | 2016-04-29 22:09:27 -0700 | [diff] [blame] | 218 | return ice_transport_channel_; |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | private: |
mikescarlett | a97611a | 2016-04-29 22:09:27 -0700 | [diff] [blame] | 222 | FakeTransportChannel* ice_transport_channel_; |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 223 | QuicTransportChannel quic_transport_channel_; |
| 224 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 225 | std::unique_ptr<rtc::SSLFingerprint> local_fingerprint_; |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 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 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 251 | std::unique_ptr<rtc::SSLFingerprint>& peer1_fingerprint = |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 252 | peer1_.local_fingerprint(); |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 253 | std::unique_ptr<rtc::SSLFingerprint>& peer2_fingerprint = |
mikescarlett | 9bc517f | 2016-04-29 18:30:55 -0700 | [diff] [blame] | 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 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), |
| 511 | kTimeoutMs); |
| 512 | // 2 state changes due to kClosing and kClosed. |
| 513 | EXPECT_EQ(3, observer.on_state_change_count()); |
| 514 | } |
| 515 | |
| 516 | // Tests that a QuicTransportChannel can be closed without being opened when it |
| 517 | // is connected to a transprot chanenl. |
| 518 | TEST_F(QuicDataChannelTest, NeverOpenedWithTransportChannel) { |
| 519 | rtc::scoped_refptr<QuicDataChannel> data_channel = |
| 520 | peer1_.CreateDataChannelWithTransportChannel(7, "label", "protocol"); |
| 521 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); |
| 522 | data_channel->Close(); |
| 523 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), |
| 524 | kTimeoutMs); |
| 525 | } |
| 526 | |
| 527 | // Tests that a QuicTransportChannel can be closed without being opened or |
| 528 | // connected to a transport channel. |
| 529 | TEST_F(QuicDataChannelTest, NeverOpenedWithoutTransportChannel) { |
| 530 | rtc::scoped_refptr<QuicDataChannel> data_channel = |
| 531 | peer1_.CreateDataChannelWithoutTransportChannel(7, "label", "protocol"); |
| 532 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); |
| 533 | data_channel->Close(); |
| 534 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), |
| 535 | kTimeoutMs); |
| 536 | } |
| 537 | |
| 538 | // Tests that the QuicDataChannel is closed when the QUIC connection closes. |
| 539 | TEST_F(QuicDataChannelTest, ClosedOnTransportError) { |
| 540 | ConnectTransportChannels(); |
| 541 | rtc::scoped_refptr<QuicDataChannel> data_channel = |
| 542 | peer1_.CreateDataChannelWithTransportChannel(1, "label", "protocol"); |
| 543 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); |
| 544 | ReliableQuicStream* stream = |
| 545 | peer1_.quic_transport_channel()->CreateQuicStream(); |
| 546 | ASSERT_NE(nullptr, stream); |
| 547 | stream->CloseConnectionWithDetails(net::QuicErrorCode::QUIC_NO_ERROR, |
| 548 | "Closing QUIC for testing"); |
| 549 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), |
| 550 | kTimeoutMs); |
| 551 | } |
| 552 | |
| 553 | // Tests that an already closed QuicDataChannel does not fire onStateChange and |
| 554 | // remains closed. |
| 555 | TEST_F(QuicDataChannelTest, DoesNotChangeStateWhenClosed) { |
| 556 | rtc::scoped_refptr<QuicDataChannel> data_channel = |
| 557 | peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); |
| 558 | FakeObserver observer; |
| 559 | data_channel->RegisterObserver(&observer); |
| 560 | data_channel->Close(); |
| 561 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), |
| 562 | kTimeoutMs); |
| 563 | // OnStateChange called for kClosing and kClosed. |
| 564 | EXPECT_EQ(2, observer.on_state_change_count()); |
| 565 | // Call Close() again to verify that the state cannot be kClosing. |
| 566 | data_channel->Close(); |
| 567 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); |
| 568 | EXPECT_EQ(2, observer.on_state_change_count()); |
| 569 | ConnectTransportChannels(); |
| 570 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); |
| 571 | EXPECT_EQ(2, observer.on_state_change_count()); |
| 572 | // writable => unwritable |
| 573 | peer1_.ice_transport_channel()->SetWritable(false); |
| 574 | ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); |
| 575 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); |
| 576 | EXPECT_EQ(2, observer.on_state_change_count()); |
| 577 | // unwritable => writable |
| 578 | peer1_.ice_transport_channel()->SetWritable(true); |
| 579 | ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); |
| 580 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); |
| 581 | EXPECT_EQ(2, observer.on_state_change_count()); |
| 582 | } |
| 583 | |
| 584 | // Tests that when the QuicDataChannel is open and the QuicTransportChannel |
| 585 | // transitions between writable and unwritable, it does not fire onStateChange |
| 586 | // and remains open. |
| 587 | TEST_F(QuicDataChannelTest, DoesNotChangeStateWhenTransportChannelReconnects) { |
| 588 | ConnectTransportChannels(); |
| 589 | rtc::scoped_refptr<QuicDataChannel> data_channel = |
| 590 | peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); |
| 591 | FakeObserver observer; |
| 592 | data_channel->RegisterObserver(&observer); |
| 593 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); |
| 594 | EXPECT_EQ(0, observer.on_state_change_count()); |
| 595 | // writable => unwritable |
| 596 | peer1_.ice_transport_channel()->SetWritable(false); |
| 597 | ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); |
| 598 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); |
| 599 | EXPECT_EQ(0, observer.on_state_change_count()); |
| 600 | // unwritable => writable |
| 601 | peer1_.ice_transport_channel()->SetWritable(true); |
| 602 | ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); |
| 603 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); |
| 604 | EXPECT_EQ(0, observer.on_state_change_count()); |
| 605 | } |
| 606 | |
| 607 | // Tests that SetTransportChannel returns false when setting a NULL transport |
| 608 | // channel or a transport channel that is not equivalent to the one already set. |
| 609 | TEST_F(QuicDataChannelTest, SetTransportChannelReturnValue) { |
| 610 | rtc::scoped_refptr<QuicDataChannel> data_channel = |
| 611 | peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); |
| 612 | EXPECT_FALSE(data_channel->SetTransportChannel(nullptr)); |
| 613 | QuicTransportChannel* transport_channel = peer1_.quic_transport_channel(); |
| 614 | EXPECT_TRUE(data_channel->SetTransportChannel(transport_channel)); |
| 615 | EXPECT_TRUE(data_channel->SetTransportChannel(transport_channel)); |
| 616 | QuicTransportChannel* other_transport_channel = |
| 617 | peer2_.quic_transport_channel(); |
| 618 | EXPECT_FALSE(data_channel->SetTransportChannel(other_transport_channel)); |
| 619 | } |
| 620 | |
| 621 | // Tests that the QUIC message header is encoded with the correct number of |
| 622 | // bytes and is properly decoded. |
| 623 | TEST_F(QuicDataChannelTest, EncodeParseQuicDataMessageHeader) { |
| 624 | int data_channel_id1 = 127; // 1 byte |
| 625 | uint64_t message_id1 = 0; // 1 byte |
| 626 | rtc::CopyOnWriteBuffer header1; |
| 627 | webrtc::WriteQuicDataChannelMessageHeader(data_channel_id1, message_id1, |
| 628 | &header1); |
| 629 | EXPECT_EQ(2u, header1.size()); |
| 630 | |
| 631 | int decoded_data_channel_id1; |
| 632 | uint64_t decoded_message_id1; |
| 633 | size_t bytes_read1; |
| 634 | ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader( |
| 635 | header1.data<char>(), header1.size(), &decoded_data_channel_id1, |
| 636 | &decoded_message_id1, &bytes_read1)); |
| 637 | EXPECT_EQ(data_channel_id1, decoded_data_channel_id1); |
| 638 | EXPECT_EQ(message_id1, decoded_message_id1); |
| 639 | EXPECT_EQ(2u, bytes_read1); |
| 640 | |
| 641 | int data_channel_id2 = 4178; // 2 bytes |
| 642 | uint64_t message_id2 = 1324921792003; // 6 bytes |
| 643 | rtc::CopyOnWriteBuffer header2; |
| 644 | webrtc::WriteQuicDataChannelMessageHeader(data_channel_id2, message_id2, |
| 645 | &header2); |
| 646 | EXPECT_EQ(8u, header2.size()); |
| 647 | |
| 648 | int decoded_data_channel_id2; |
| 649 | uint64_t decoded_message_id2; |
| 650 | size_t bytes_read2; |
| 651 | ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader( |
| 652 | header2.data<char>(), header2.size(), &decoded_data_channel_id2, |
| 653 | &decoded_message_id2, &bytes_read2)); |
| 654 | EXPECT_EQ(data_channel_id2, decoded_data_channel_id2); |
| 655 | EXPECT_EQ(message_id2, decoded_message_id2); |
| 656 | EXPECT_EQ(8u, bytes_read2); |
| 657 | } |
| 658 | |
| 659 | } // namespace |