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