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/quicdatatransport.h" |
| 12 |
| 13 #include <set> |
| 14 #include <string> |
| 15 #include <unordered_map> |
| 16 #include <vector> |
| 17 |
| 18 #include "webrtc/api/quicdatachannel.h" |
| 19 #include "webrtc/base/bytebuffer.h" |
| 20 #include "webrtc/base/gunit.h" |
| 21 #include "webrtc/p2p/base/faketransportcontroller.h" |
| 22 #include "webrtc/p2p/quic/quictransportchannel.h" |
| 23 #include "webrtc/p2p/quic/reliablequicstream.h" |
| 24 |
| 25 using webrtc::DataBuffer; |
| 26 using webrtc::DataChannelInit; |
| 27 using webrtc::DataChannelInterface; |
| 28 using webrtc::DataChannelObserver; |
| 29 using webrtc::QuicDataChannel; |
| 30 using webrtc::QuicDataTransport; |
| 31 using cricket::FakeTransportChannel; |
| 32 using cricket::QuicTransportChannel; |
| 33 using cricket::ReliableQuicStream; |
| 34 |
| 35 namespace { |
| 36 |
| 37 // Timeout for asynchronous operations. |
| 38 static const int kTimeoutMs = 1000; // milliseconds |
| 39 |
| 40 // FakeObserver receives messages from the data channel. |
| 41 class FakeObserver : public DataChannelObserver { |
| 42 public: |
| 43 FakeObserver() {} |
| 44 |
| 45 void OnStateChange() override {} |
| 46 |
| 47 void OnBufferedAmountChange(uint64_t previous_amount) override {} |
| 48 |
| 49 void OnMessage(const webrtc::DataBuffer& buffer) override { |
| 50 messages_.push_back(std::string(buffer.data.data<char>(), buffer.size())); |
| 51 } |
| 52 |
| 53 const std::vector<std::string>& messages() const { return messages_; } |
| 54 |
| 55 size_t messages_received() const { return messages_.size(); } |
| 56 |
| 57 private: |
| 58 std::vector<std::string> messages_; |
| 59 }; |
| 60 |
| 61 // A peer who uses a QUIC transport channel and fake ICE transport channel to |
| 62 // send or receive data. |
| 63 class QuicDataTransportPeer { |
| 64 public: |
| 65 QuicDataTransportPeer() |
| 66 : quic_data_transport_(rtc::Thread::Current(), rtc::Thread::Current()), |
| 67 ice_transport_channel_("data", 0), |
| 68 quic_transport_channel_(&ice_transport_channel_) { |
| 69 ice_transport_channel_.SetAsync(true); |
| 70 } |
| 71 |
| 72 void GenerateCertificateAndFingerprint() { |
| 73 rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
| 74 rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( |
| 75 rtc::SSLIdentity::Generate("cert_name", rtc::KT_DEFAULT))); |
| 76 quic_transport_channel_.SetLocalCertificate(local_cert); |
| 77 local_fingerprint_.reset(CreateFingerprint(local_cert.get())); |
| 78 } |
| 79 |
| 80 // Connects |ice_transport_channel_| to that of the other peer. |
| 81 void Connect(QuicDataTransportPeer* other_peer) { |
| 82 ice_transport_channel_.Connect(); |
| 83 other_peer->ice_transport_channel_.Connect(); |
| 84 ice_transport_channel_.SetDestination(&other_peer->ice_transport_channel_); |
| 85 } |
| 86 |
| 87 rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { |
| 88 return local_fingerprint_; |
| 89 } |
| 90 |
| 91 QuicTransportChannel* quic_transport_channel() { |
| 92 return &quic_transport_channel_; |
| 93 } |
| 94 |
| 95 // Write a messge directly to the ReliableQuicStream. |
| 96 void WriteMessage(int data_channel_id, |
| 97 uint64_t message_id, |
| 98 const std::string& message) { |
| 99 ReliableQuicStream* stream = quic_transport_channel_.CreateQuicStream(); |
| 100 rtc::CopyOnWriteBuffer payload; |
| 101 webrtc::WriteQuicDataChannelMessageHeader(data_channel_id, message_id, |
| 102 &payload); |
| 103 stream->Write(payload.data<char>(), payload.size(), false); |
| 104 stream->Write(message.data(), message.size(), true); |
| 105 } |
| 106 |
| 107 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( |
| 108 const DataChannelInit* config) { |
| 109 return quic_data_transport_.CreateDataChannel("testing", config); |
| 110 } |
| 111 |
| 112 QuicDataTransport* quic_data_transport() { return &quic_data_transport_; } |
| 113 |
| 114 private: |
| 115 // Creates a fingerprint from a certificate. |
| 116 rtc::SSLFingerprint* CreateFingerprint(rtc::RTCCertificate* cert) { |
| 117 std::string digest_algorithm; |
| 118 cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm); |
| 119 rtc::scoped_ptr<rtc::SSLFingerprint> fingerprint( |
| 120 rtc::SSLFingerprint::Create(digest_algorithm, cert->identity())); |
| 121 return fingerprint.release(); |
| 122 } |
| 123 |
| 124 QuicDataTransport quic_data_transport_; |
| 125 FakeTransportChannel ice_transport_channel_; |
| 126 QuicTransportChannel quic_transport_channel_; |
| 127 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; |
| 128 }; |
| 129 |
| 130 class QuicDataTransportTest : public testing::Test { |
| 131 public: |
| 132 QuicDataTransportTest() {} |
| 133 |
| 134 void ConnectTransportChannels() { |
| 135 SetCryptoParameters(); |
| 136 peer1_.Connect(&peer2_); |
| 137 ASSERT_TRUE_WAIT(peer1_.quic_transport_channel()->writable() && |
| 138 peer2_.quic_transport_channel()->writable(), |
| 139 kTimeoutMs); |
| 140 } |
| 141 |
| 142 void SetTransportChannels() { |
| 143 ASSERT_TRUE(peer1_.quic_data_transport()->SetTransportChannel( |
| 144 peer1_.quic_transport_channel())); |
| 145 ASSERT_TRUE(peer2_.quic_data_transport()->SetTransportChannel( |
| 146 peer2_.quic_transport_channel())); |
| 147 } |
| 148 |
| 149 // Sets crypto parameters required for the QUIC handshake. |
| 150 void SetCryptoParameters() { |
| 151 peer1_.GenerateCertificateAndFingerprint(); |
| 152 peer2_.GenerateCertificateAndFingerprint(); |
| 153 |
| 154 peer1_.quic_transport_channel()->SetSslRole(rtc::SSL_CLIENT); |
| 155 peer2_.quic_transport_channel()->SetSslRole(rtc::SSL_SERVER); |
| 156 |
| 157 rtc::scoped_ptr<rtc::SSLFingerprint>& peer1_fingerprint = |
| 158 peer1_.local_fingerprint(); |
| 159 rtc::scoped_ptr<rtc::SSLFingerprint>& peer2_fingerprint = |
| 160 peer2_.local_fingerprint(); |
| 161 |
| 162 peer1_.quic_transport_channel()->SetRemoteFingerprint( |
| 163 peer2_fingerprint->algorithm, |
| 164 reinterpret_cast<const uint8_t*>(peer2_fingerprint->digest.data()), |
| 165 peer2_fingerprint->digest.size()); |
| 166 peer2_.quic_transport_channel()->SetRemoteFingerprint( |
| 167 peer1_fingerprint->algorithm, |
| 168 reinterpret_cast<const uint8_t*>(peer1_fingerprint->digest.data()), |
| 169 peer1_fingerprint->digest.size()); |
| 170 } |
| 171 |
| 172 protected: |
| 173 QuicDataTransportPeer peer1_; |
| 174 QuicDataTransportPeer peer2_; |
| 175 }; |
| 176 |
| 177 // Tests creation and destruction of data channels. |
| 178 TEST_F(QuicDataTransportTest, CreateAndDestroyDataChannels) { |
| 179 QuicDataTransport* quic_data_transport = peer2_.quic_data_transport(); |
| 180 EXPECT_FALSE(quic_data_transport->HasDataChannels()); |
| 181 for (int data_channel_id = 0; data_channel_id < 5; ++data_channel_id) { |
| 182 EXPECT_FALSE(quic_data_transport->HasDataChannel(data_channel_id)); |
| 183 webrtc::DataChannelInit config; |
| 184 config.id = data_channel_id; |
| 185 rtc::scoped_refptr<DataChannelInterface> data_channel = |
| 186 peer2_.CreateDataChannel(&config); |
| 187 EXPECT_NE(nullptr, data_channel); |
| 188 EXPECT_EQ(data_channel_id, data_channel->id()); |
| 189 EXPECT_TRUE(quic_data_transport->HasDataChannel(data_channel_id)); |
| 190 } |
| 191 EXPECT_TRUE(quic_data_transport->HasDataChannels()); |
| 192 for (int data_channel_id = 0; data_channel_id < 5; ++data_channel_id) { |
| 193 quic_data_transport->DestroyDataChannel(data_channel_id); |
| 194 EXPECT_FALSE(quic_data_transport->HasDataChannel(data_channel_id)); |
| 195 } |
| 196 EXPECT_FALSE(quic_data_transport->HasDataChannels()); |
| 197 } |
| 198 |
| 199 // Tests that the QuicDataTransport does not allow creating multiple |
| 200 // QuicDataChannels with the same id. |
| 201 TEST_F(QuicDataTransportTest, CannotCreateDataChannelsWithSameId) { |
| 202 webrtc::DataChannelInit config; |
| 203 config.id = 2; |
| 204 EXPECT_NE(nullptr, peer2_.CreateDataChannel(&config)); |
| 205 EXPECT_EQ(nullptr, peer2_.CreateDataChannel(&config)); |
| 206 } |
| 207 |
| 208 // Tests that any data channels created by the QuicDataTransport are in state |
| 209 // kConnecting before the QuicTransportChannel is set, then transiton to state |
| 210 // kOpen when the transport channel becomes writable. |
| 211 TEST_F(QuicDataTransportTest, DataChannelsOpenWhenTransportChannelWritable) { |
| 212 webrtc::DataChannelInit config1; |
| 213 config1.id = 7; |
| 214 rtc::scoped_refptr<DataChannelInterface> data_channel1 = |
| 215 peer2_.CreateDataChannel(&config1); |
| 216 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel1->state()); |
| 217 SetTransportChannels(); |
| 218 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel1->state()); |
| 219 webrtc::DataChannelInit config2; |
| 220 config2.id = 14; |
| 221 rtc::scoped_refptr<DataChannelInterface> data_channel2 = |
| 222 peer2_.CreateDataChannel(&config2); |
| 223 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel2->state()); |
| 224 // Existing data channels should open once the transport channel is writable. |
| 225 ConnectTransportChannels(); |
| 226 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, data_channel1->state(), |
| 227 kTimeoutMs); |
| 228 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, data_channel2->state(), |
| 229 kTimeoutMs); |
| 230 // Any data channels created afterwards should start in state kOpen. |
| 231 webrtc::DataChannelInit config3; |
| 232 config3.id = 21; |
| 233 rtc::scoped_refptr<DataChannelInterface> data_channel3 = |
| 234 peer2_.CreateDataChannel(&config3); |
| 235 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel3->state()); |
| 236 } |
| 237 |
| 238 // Tests that the QuicTransport dispatches messages for one QuicDataChannel. |
| 239 TEST_F(QuicDataTransportTest, ReceiveMessagesForSingleDataChannel) { |
| 240 ConnectTransportChannels(); |
| 241 SetTransportChannels(); |
| 242 |
| 243 int data_channel_id = 1337; |
| 244 webrtc::DataChannelInit config; |
| 245 config.id = data_channel_id; |
| 246 rtc::scoped_refptr<DataChannelInterface> peer2_data_channel = |
| 247 peer2_.CreateDataChannel(&config); |
| 248 FakeObserver observer; |
| 249 peer2_data_channel->RegisterObserver(&observer); |
| 250 |
| 251 uint64_t message1_id = 26u; |
| 252 peer1_.WriteMessage(data_channel_id, message1_id, "Testing"); |
| 253 ASSERT_EQ_WAIT(1, observer.messages_received(), kTimeoutMs); |
| 254 EXPECT_EQ("Testing", observer.messages()[0]); |
| 255 |
| 256 uint64_t message2_id = 402u; |
| 257 peer1_.WriteMessage(data_channel_id, message2_id, "Hello, World!"); |
| 258 ASSERT_EQ_WAIT(2, observer.messages_received(), kTimeoutMs); |
| 259 EXPECT_EQ("Hello, World!", observer.messages()[1]); |
| 260 |
| 261 uint64_t message3_id = 100260415u; |
| 262 peer1_.WriteMessage(data_channel_id, message3_id, "Third message"); |
| 263 ASSERT_EQ_WAIT(3, observer.messages_received(), kTimeoutMs); |
| 264 EXPECT_EQ("Third message", observer.messages()[2]); |
| 265 } |
| 266 |
| 267 // Tests that the QuicTransport dispatches messages to the correct data channel |
| 268 // when multiple are in use. |
| 269 TEST_F(QuicDataTransportTest, ReceiveMessagesForMultipleDataChannels) { |
| 270 ConnectTransportChannels(); |
| 271 SetTransportChannels(); |
| 272 |
| 273 std::vector<rtc::scoped_refptr<DataChannelInterface>> data_channels; |
| 274 for (int data_channel_id = 0; data_channel_id < 5; ++data_channel_id) { |
| 275 webrtc::DataChannelInit config; |
| 276 config.id = data_channel_id; |
| 277 data_channels.push_back(peer2_.CreateDataChannel(&config)); |
| 278 } |
| 279 |
| 280 for (int data_channel_id = 0; data_channel_id < 5; ++data_channel_id) { |
| 281 uint64_t message1_id = 48023u; |
| 282 FakeObserver observer; |
| 283 DataChannelInterface* peer2_data_channel = |
| 284 data_channels[data_channel_id].get(); |
| 285 peer2_data_channel->RegisterObserver(&observer); |
| 286 peer1_.WriteMessage(data_channel_id, message1_id, "Testing"); |
| 287 ASSERT_EQ_WAIT(1, observer.messages_received(), kTimeoutMs); |
| 288 EXPECT_EQ("Testing", observer.messages()[0]); |
| 289 |
| 290 uint64_t message2_id = 1372643095u; |
| 291 peer1_.WriteMessage(data_channel_id, message2_id, "Hello, World!"); |
| 292 ASSERT_EQ_WAIT(2, observer.messages_received(), kTimeoutMs); |
| 293 EXPECT_EQ("Hello, World!", observer.messages()[1]); |
| 294 } |
| 295 } |
| 296 |
| 297 // Tests end-to-end that both peers can use multiple QuicDataChannels to |
| 298 // send/receive messages using a QuicDataTransport. |
| 299 TEST_F(QuicDataTransportTest, EndToEndSendReceiveMessages) { |
| 300 ConnectTransportChannels(); |
| 301 SetTransportChannels(); |
| 302 |
| 303 std::vector<rtc::scoped_refptr<DataChannelInterface>> peer1_data_channels; |
| 304 std::vector<rtc::scoped_refptr<DataChannelInterface>> peer2_data_channels; |
| 305 |
| 306 for (int data_channel_id = 0; data_channel_id < 5; ++data_channel_id) { |
| 307 webrtc::DataChannelInit config; |
| 308 config.id = data_channel_id; |
| 309 peer1_data_channels.push_back(peer1_.CreateDataChannel(&config)); |
| 310 peer2_data_channels.push_back(peer2_.CreateDataChannel(&config)); |
| 311 } |
| 312 |
| 313 for (int data_channel_id = 0; data_channel_id < 5; ++data_channel_id) { |
| 314 DataChannelInterface* peer1_data_channel = |
| 315 peer1_data_channels[data_channel_id].get(); |
| 316 FakeObserver observer1; |
| 317 peer1_data_channel->RegisterObserver(&observer1); |
| 318 DataChannelInterface* peer2_data_channel = |
| 319 peer2_data_channels[data_channel_id].get(); |
| 320 FakeObserver observer2; |
| 321 peer2_data_channel->RegisterObserver(&observer2); |
| 322 |
| 323 peer1_data_channel->Send(webrtc::DataBuffer("Peer 1 message 1")); |
| 324 ASSERT_EQ_WAIT(1, observer2.messages_received(), kTimeoutMs); |
| 325 EXPECT_EQ("Peer 1 message 1", observer2.messages()[0]); |
| 326 |
| 327 peer1_data_channel->Send(webrtc::DataBuffer("Peer 1 message 2")); |
| 328 ASSERT_EQ_WAIT(2, observer2.messages_received(), kTimeoutMs); |
| 329 EXPECT_EQ("Peer 1 message 2", observer2.messages()[1]); |
| 330 |
| 331 peer2_data_channel->Send(webrtc::DataBuffer("Peer 2 message 1")); |
| 332 ASSERT_EQ_WAIT(1, observer1.messages_received(), kTimeoutMs); |
| 333 EXPECT_EQ("Peer 2 message 1", observer1.messages()[0]); |
| 334 |
| 335 peer2_data_channel->Send(webrtc::DataBuffer("Peer 2 message 2")); |
| 336 ASSERT_EQ_WAIT(2, observer1.messages_received(), kTimeoutMs); |
| 337 EXPECT_EQ("Peer 2 message 2", observer1.messages()[1]); |
| 338 } |
| 339 } |
| 340 |
| 341 // Tests that SetTransportChannel returns false when setting a NULL transport |
| 342 // channel or a transport channel that is not equivalent to the one already set. |
| 343 TEST_F(QuicDataTransportTest, SetTransportChannelReturnValue) { |
| 344 QuicDataTransport* quic_data_transport = peer1_.quic_data_transport(); |
| 345 EXPECT_FALSE(quic_data_transport->SetTransportChannel(nullptr)); |
| 346 QuicTransportChannel* transport_channel = peer1_.quic_transport_channel(); |
| 347 EXPECT_TRUE(quic_data_transport->SetTransportChannel(transport_channel)); |
| 348 EXPECT_TRUE(quic_data_transport->SetTransportChannel(transport_channel)); |
| 349 QuicTransportChannel* other_transport_channel = |
| 350 peer2_.quic_transport_channel(); |
| 351 EXPECT_FALSE( |
| 352 quic_data_transport->SetTransportChannel(other_transport_channel)); |
| 353 } |
| 354 |
| 355 } // namespace |
OLD | NEW |