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 : ice_transport_channel_("data", 0), |
| 67 quic_transport_channel_(&ice_transport_channel_) { |
| 68 ice_transport_channel_.SetAsync(true); |
| 69 } |
| 70 |
| 71 void GenerateCertificateAndFingerprint() { |
| 72 rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
| 73 rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( |
| 74 rtc::SSLIdentity::Generate("cert_name", rtc::KT_DEFAULT))); |
| 75 quic_transport_channel_.SetLocalCertificate(local_cert); |
| 76 local_fingerprint_.reset(CreateFingerprint(local_cert.get())); |
| 77 } |
| 78 |
| 79 // Connects |ice_transport_channel_| to that of the other peer. |
| 80 void Connect(QuicDataTransportPeer* other_peer) { |
| 81 ice_transport_channel_.Connect(); |
| 82 other_peer->ice_transport_channel_.Connect(); |
| 83 ice_transport_channel_.SetDestination(&other_peer->ice_transport_channel_); |
| 84 } |
| 85 |
| 86 rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() { |
| 87 return local_fingerprint_; |
| 88 } |
| 89 |
| 90 QuicTransportChannel* quic_transport_channel() { |
| 91 return &quic_transport_channel_; |
| 92 } |
| 93 |
| 94 private: |
| 95 // Creates a fingerprint from a certificate. |
| 96 rtc::SSLFingerprint* CreateFingerprint(rtc::RTCCertificate* cert) { |
| 97 std::string digest_algorithm; |
| 98 cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm); |
| 99 rtc::scoped_ptr<rtc::SSLFingerprint> fingerprint( |
| 100 rtc::SSLFingerprint::Create(digest_algorithm, cert->identity())); |
| 101 return fingerprint.release(); |
| 102 } |
| 103 |
| 104 FakeTransportChannel ice_transport_channel_; |
| 105 QuicTransportChannel quic_transport_channel_; |
| 106 |
| 107 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_; |
| 108 }; |
| 109 |
| 110 class QuicDataTransportTest : public testing::Test { |
| 111 public: |
| 112 QuicDataTransportTest() { |
| 113 quic_transport_.SetTransportChannel(peer2_.quic_transport_channel()); |
| 114 } |
| 115 |
| 116 void ConnectTransportChannels() { |
| 117 SetCryptoParameters(); |
| 118 peer1_.Connect(&peer2_); |
| 119 ASSERT_TRUE_WAIT(peer1_.quic_transport_channel()->writable() && |
| 120 peer2_.quic_transport_channel()->writable(), |
| 121 kTimeoutMs); |
| 122 } |
| 123 |
| 124 // Sets crypto parameters required for the QUIC handshake. |
| 125 void SetCryptoParameters() { |
| 126 peer1_.GenerateCertificateAndFingerprint(); |
| 127 peer2_.GenerateCertificateAndFingerprint(); |
| 128 |
| 129 peer1_.quic_transport_channel()->SetSslRole(rtc::SSL_CLIENT); |
| 130 peer2_.quic_transport_channel()->SetSslRole(rtc::SSL_SERVER); |
| 131 |
| 132 rtc::scoped_ptr<rtc::SSLFingerprint>& peer1_fingerprint = |
| 133 peer1_.local_fingerprint(); |
| 134 rtc::scoped_ptr<rtc::SSLFingerprint>& peer2_fingerprint = |
| 135 peer2_.local_fingerprint(); |
| 136 |
| 137 peer1_.quic_transport_channel()->SetRemoteFingerprint( |
| 138 peer2_fingerprint->algorithm, |
| 139 reinterpret_cast<const uint8_t*>(peer2_fingerprint->digest.data()), |
| 140 peer2_fingerprint->digest.size()); |
| 141 peer2_.quic_transport_channel()->SetRemoteFingerprint( |
| 142 peer1_fingerprint->algorithm, |
| 143 reinterpret_cast<const uint8_t*>(peer1_fingerprint->digest.data()), |
| 144 peer1_fingerprint->digest.size()); |
| 145 } |
| 146 |
| 147 void WriteMessage(ReliableQuicStream* stream, |
| 148 int data_channel_id, |
| 149 uint64_t message_id, |
| 150 const std::string& message) { |
| 151 rtc::CopyOnWriteBuffer payload; |
| 152 quic_transport_.Encode(DataBuffer(message), data_channel_id, message_id, |
| 153 &payload); |
| 154 stream->Write(payload.data<char>(), payload.size(), true); |
| 155 } |
| 156 |
| 157 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( |
| 158 const DataChannelInit* config) { |
| 159 return quic_transport_.CreateDataChannel( |
| 160 rtc::Thread::Current(), rtc::Thread::Current(), "testing", config); |
| 161 } |
| 162 |
| 163 protected: |
| 164 QuicDataTransportPeer peer1_; |
| 165 QuicDataTransportPeer peer2_; |
| 166 |
| 167 QuicDataTransport quic_transport_; |
| 168 }; |
| 169 |
| 170 // Test that the QuicTransport dispatches messages for a single QuicDataChannel. |
| 171 TEST_F(QuicDataTransportTest, ReceiveMessagesForSingleDataChannel) { |
| 172 ConnectTransportChannels(); |
| 173 int data_channel_id = 1337; |
| 174 webrtc::DataChannelInit config; |
| 175 config.id = data_channel_id; |
| 176 rtc::scoped_refptr<DataChannelInterface> peer2_data_channel = |
| 177 CreateDataChannel(&config); |
| 178 FakeObserver observer; |
| 179 peer2_data_channel->RegisterObserver(&observer); |
| 180 |
| 181 ReliableQuicStream* stream1 = |
| 182 peer1_.quic_transport_channel()->CreateQuicStream(); |
| 183 uint64_t message1_id = 26u; |
| 184 WriteMessage(stream1, data_channel_id, message1_id, "Testing"); |
| 185 ASSERT_TRUE_WAIT(observer.messages_received() == 1, kTimeoutMs); |
| 186 EXPECT_EQ("Testing", observer.messages()[0]); |
| 187 |
| 188 ReliableQuicStream* stream2 = |
| 189 peer1_.quic_transport_channel()->CreateQuicStream(); |
| 190 uint64_t message2_id = 402u; |
| 191 WriteMessage(stream2, data_channel_id, message2_id, "Hello, World!"); |
| 192 ASSERT_TRUE_WAIT(observer.messages_received() == 2, kTimeoutMs); |
| 193 EXPECT_EQ("Hello, World!", observer.messages()[1]); |
| 194 |
| 195 ReliableQuicStream* stream3 = |
| 196 peer1_.quic_transport_channel()->CreateQuicStream(); |
| 197 uint64_t message3_id = 100260415u; |
| 198 WriteMessage(stream3, data_channel_id, message3_id, "Third message"); |
| 199 ASSERT_TRUE_WAIT(observer.messages_received() == 3, kTimeoutMs); |
| 200 EXPECT_EQ("Third message", observer.messages()[2]); |
| 201 } |
| 202 |
| 203 // Test that the QuicTransport dispatches messages for multiple |
| 204 // QuicDataChannels. |
| 205 TEST_F(QuicDataTransportTest, ReceiveMessagesForMultipleDataChannels) { |
| 206 ConnectTransportChannels(); |
| 207 |
| 208 std::vector<rtc::scoped_refptr<DataChannelInterface>> data_channels; |
| 209 for (int data_channel_id = 0; data_channel_id < 10; ++data_channel_id) { |
| 210 webrtc::DataChannelInit config; |
| 211 config.id = data_channel_id; |
| 212 data_channels.push_back(CreateDataChannel(&config)); |
| 213 } |
| 214 |
| 215 for (int data_channel_id = 0; data_channel_id < 10; ++data_channel_id) { |
| 216 ReliableQuicStream* stream1 = |
| 217 peer1_.quic_transport_channel()->CreateQuicStream(); |
| 218 uint64_t message1_id = 48023u; |
| 219 FakeObserver observer; |
| 220 DataChannelInterface* peer2_data_channel = |
| 221 data_channels[data_channel_id].get(); |
| 222 peer2_data_channel->RegisterObserver(&observer); |
| 223 WriteMessage(stream1, data_channel_id, message1_id, "Testing"); |
| 224 ASSERT_TRUE_WAIT(observer.messages_received() == 1, kTimeoutMs); |
| 225 EXPECT_EQ("Testing", observer.messages()[0]); |
| 226 |
| 227 ReliableQuicStream* stream2 = |
| 228 peer1_.quic_transport_channel()->CreateQuicStream(); |
| 229 uint64_t message2_id = 1372643095u; |
| 230 WriteMessage(stream2, data_channel_id, message2_id, "Hello, World!"); |
| 231 ASSERT_TRUE_WAIT(observer.messages_received() == 2, kTimeoutMs); |
| 232 EXPECT_EQ("Hello, World!", observer.messages()[1]); |
| 233 } |
| 234 } |
| 235 |
| 236 } // namespace |
OLD | NEW |