| Index: webrtc/api/quicdatatransport_unittest.cc
|
| diff --git a/webrtc/api/quicdatatransport_unittest.cc b/webrtc/api/quicdatatransport_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ad100f1496a0993eb34b2a9701e90d33ecc2cb7b
|
| --- /dev/null
|
| +++ b/webrtc/api/quicdatatransport_unittest.cc
|
| @@ -0,0 +1,236 @@
|
| +/*
|
| + * Copyright 2016 The WebRTC project authors. All Rights Reserved.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license
|
| + * that can be found in the LICENSE file in the root of the source
|
| + * tree. An additional intellectual property rights grant can be found
|
| + * in the file PATENTS. All contributing project authors may
|
| + * be found in the AUTHORS file in the root of the source tree.
|
| + */
|
| +
|
| +#include "webrtc/api/quicdatatransport.h"
|
| +
|
| +#include <set>
|
| +#include <string>
|
| +#include <unordered_map>
|
| +#include <vector>
|
| +
|
| +#include "webrtc/api/quicdatachannel.h"
|
| +#include "webrtc/base/bytebuffer.h"
|
| +#include "webrtc/base/gunit.h"
|
| +#include "webrtc/p2p/base/faketransportcontroller.h"
|
| +#include "webrtc/p2p/quic/quictransportchannel.h"
|
| +#include "webrtc/p2p/quic/reliablequicstream.h"
|
| +
|
| +using webrtc::DataBuffer;
|
| +using webrtc::DataChannelInit;
|
| +using webrtc::DataChannelInterface;
|
| +using webrtc::DataChannelObserver;
|
| +using webrtc::QuicDataChannel;
|
| +using webrtc::QuicDataTransport;
|
| +using cricket::FakeTransportChannel;
|
| +using cricket::QuicTransportChannel;
|
| +using cricket::ReliableQuicStream;
|
| +
|
| +namespace {
|
| +
|
| +// Timeout for asynchronous operations.
|
| +static const int kTimeoutMs = 1000; // milliseconds
|
| +
|
| +// FakeObserver receives messages from the data channel.
|
| +class FakeObserver : public DataChannelObserver {
|
| + public:
|
| + FakeObserver() {}
|
| +
|
| + void OnStateChange() override {}
|
| +
|
| + void OnBufferedAmountChange(uint64_t previous_amount) override {}
|
| +
|
| + void OnMessage(const webrtc::DataBuffer& buffer) override {
|
| + messages_.push_back(std::string(buffer.data.data<char>(), buffer.size()));
|
| + }
|
| +
|
| + const std::vector<std::string>& messages() const { return messages_; }
|
| +
|
| + size_t messages_received() const { return messages_.size(); }
|
| +
|
| + private:
|
| + std::vector<std::string> messages_;
|
| +};
|
| +
|
| +// A peer who uses a QUIC transport channel and fake ICE transport channel to
|
| +// send or receive data.
|
| +class QuicDataTransportPeer {
|
| + public:
|
| + QuicDataTransportPeer()
|
| + : ice_transport_channel_("data", 0),
|
| + quic_transport_channel_(&ice_transport_channel_) {
|
| + ice_transport_channel_.SetAsync(true);
|
| + }
|
| +
|
| + void GenerateCertificateAndFingerprint() {
|
| + rtc::scoped_refptr<rtc::RTCCertificate> local_cert =
|
| + rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>(
|
| + rtc::SSLIdentity::Generate("cert_name", rtc::KT_DEFAULT)));
|
| + quic_transport_channel_.SetLocalCertificate(local_cert);
|
| + local_fingerprint_.reset(CreateFingerprint(local_cert.get()));
|
| + }
|
| +
|
| + // Connects |ice_transport_channel_| to that of the other peer.
|
| + void Connect(QuicDataTransportPeer* other_peer) {
|
| + ice_transport_channel_.Connect();
|
| + other_peer->ice_transport_channel_.Connect();
|
| + ice_transport_channel_.SetDestination(&other_peer->ice_transport_channel_);
|
| + }
|
| +
|
| + rtc::scoped_ptr<rtc::SSLFingerprint>& local_fingerprint() {
|
| + return local_fingerprint_;
|
| + }
|
| +
|
| + QuicTransportChannel* quic_transport_channel() {
|
| + return &quic_transport_channel_;
|
| + }
|
| +
|
| + private:
|
| + // Creates a fingerprint from a certificate.
|
| + rtc::SSLFingerprint* CreateFingerprint(rtc::RTCCertificate* cert) {
|
| + std::string digest_algorithm;
|
| + cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm);
|
| + rtc::scoped_ptr<rtc::SSLFingerprint> fingerprint(
|
| + rtc::SSLFingerprint::Create(digest_algorithm, cert->identity()));
|
| + return fingerprint.release();
|
| + }
|
| +
|
| + FakeTransportChannel ice_transport_channel_;
|
| + QuicTransportChannel quic_transport_channel_;
|
| +
|
| + rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint_;
|
| +};
|
| +
|
| +class QuicDataTransportTest : public testing::Test {
|
| + public:
|
| + QuicDataTransportTest() {
|
| + quic_transport_.SetTransportChannel(peer2_.quic_transport_channel());
|
| + }
|
| +
|
| + void ConnectTransportChannels() {
|
| + SetCryptoParameters();
|
| + peer1_.Connect(&peer2_);
|
| + ASSERT_TRUE_WAIT(peer1_.quic_transport_channel()->writable() &&
|
| + peer2_.quic_transport_channel()->writable(),
|
| + kTimeoutMs);
|
| + }
|
| +
|
| + // Sets crypto parameters required for the QUIC handshake.
|
| + void SetCryptoParameters() {
|
| + peer1_.GenerateCertificateAndFingerprint();
|
| + peer2_.GenerateCertificateAndFingerprint();
|
| +
|
| + peer1_.quic_transport_channel()->SetSslRole(rtc::SSL_CLIENT);
|
| + peer2_.quic_transport_channel()->SetSslRole(rtc::SSL_SERVER);
|
| +
|
| + rtc::scoped_ptr<rtc::SSLFingerprint>& peer1_fingerprint =
|
| + peer1_.local_fingerprint();
|
| + rtc::scoped_ptr<rtc::SSLFingerprint>& peer2_fingerprint =
|
| + peer2_.local_fingerprint();
|
| +
|
| + peer1_.quic_transport_channel()->SetRemoteFingerprint(
|
| + peer2_fingerprint->algorithm,
|
| + reinterpret_cast<const uint8_t*>(peer2_fingerprint->digest.data()),
|
| + peer2_fingerprint->digest.size());
|
| + peer2_.quic_transport_channel()->SetRemoteFingerprint(
|
| + peer1_fingerprint->algorithm,
|
| + reinterpret_cast<const uint8_t*>(peer1_fingerprint->digest.data()),
|
| + peer1_fingerprint->digest.size());
|
| + }
|
| +
|
| + void WriteMessage(ReliableQuicStream* stream,
|
| + int data_channel_id,
|
| + uint64_t message_id,
|
| + const std::string& message) {
|
| + rtc::CopyOnWriteBuffer payload;
|
| + quic_transport_.Encode(DataBuffer(message), data_channel_id, message_id,
|
| + &payload);
|
| + stream->Write(payload.data<char>(), payload.size(), true);
|
| + }
|
| +
|
| + rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
|
| + const DataChannelInit* config) {
|
| + return quic_transport_.CreateDataChannel(
|
| + rtc::Thread::Current(), rtc::Thread::Current(), "testing", config);
|
| + }
|
| +
|
| + protected:
|
| + QuicDataTransportPeer peer1_;
|
| + QuicDataTransportPeer peer2_;
|
| +
|
| + QuicDataTransport quic_transport_;
|
| +};
|
| +
|
| +// Test that the QuicTransport dispatches messages for a single QuicDataChannel.
|
| +TEST_F(QuicDataTransportTest, ReceiveMessagesForSingleDataChannel) {
|
| + ConnectTransportChannels();
|
| + int data_channel_id = 1337;
|
| + webrtc::DataChannelInit config;
|
| + config.id = data_channel_id;
|
| + rtc::scoped_refptr<DataChannelInterface> peer2_data_channel =
|
| + CreateDataChannel(&config);
|
| + FakeObserver observer;
|
| + peer2_data_channel->RegisterObserver(&observer);
|
| +
|
| + ReliableQuicStream* stream1 =
|
| + peer1_.quic_transport_channel()->CreateQuicStream();
|
| + uint64_t message1_id = 26u;
|
| + WriteMessage(stream1, data_channel_id, message1_id, "Testing");
|
| + ASSERT_TRUE_WAIT(observer.messages_received() == 1, kTimeoutMs);
|
| + EXPECT_EQ("Testing", observer.messages()[0]);
|
| +
|
| + ReliableQuicStream* stream2 =
|
| + peer1_.quic_transport_channel()->CreateQuicStream();
|
| + uint64_t message2_id = 402u;
|
| + WriteMessage(stream2, data_channel_id, message2_id, "Hello, World!");
|
| + ASSERT_TRUE_WAIT(observer.messages_received() == 2, kTimeoutMs);
|
| + EXPECT_EQ("Hello, World!", observer.messages()[1]);
|
| +
|
| + ReliableQuicStream* stream3 =
|
| + peer1_.quic_transport_channel()->CreateQuicStream();
|
| + uint64_t message3_id = 100260415u;
|
| + WriteMessage(stream3, data_channel_id, message3_id, "Third message");
|
| + ASSERT_TRUE_WAIT(observer.messages_received() == 3, kTimeoutMs);
|
| + EXPECT_EQ("Third message", observer.messages()[2]);
|
| +}
|
| +
|
| +// Test that the QuicTransport dispatches messages for multiple
|
| +// QuicDataChannels.
|
| +TEST_F(QuicDataTransportTest, ReceiveMessagesForMultipleDataChannels) {
|
| + ConnectTransportChannels();
|
| +
|
| + std::vector<rtc::scoped_refptr<DataChannelInterface>> data_channels;
|
| + for (int data_channel_id = 0; data_channel_id < 10; ++data_channel_id) {
|
| + webrtc::DataChannelInit config;
|
| + config.id = data_channel_id;
|
| + data_channels.push_back(CreateDataChannel(&config));
|
| + }
|
| +
|
| + for (int data_channel_id = 0; data_channel_id < 10; ++data_channel_id) {
|
| + ReliableQuicStream* stream1 =
|
| + peer1_.quic_transport_channel()->CreateQuicStream();
|
| + uint64_t message1_id = 48023u;
|
| + FakeObserver observer;
|
| + DataChannelInterface* peer2_data_channel =
|
| + data_channels[data_channel_id].get();
|
| + peer2_data_channel->RegisterObserver(&observer);
|
| + WriteMessage(stream1, data_channel_id, message1_id, "Testing");
|
| + ASSERT_TRUE_WAIT(observer.messages_received() == 1, kTimeoutMs);
|
| + EXPECT_EQ("Testing", observer.messages()[0]);
|
| +
|
| + ReliableQuicStream* stream2 =
|
| + peer1_.quic_transport_channel()->CreateQuicStream();
|
| + uint64_t message2_id = 1372643095u;
|
| + WriteMessage(stream2, data_channel_id, message2_id, "Hello, World!");
|
| + ASSERT_TRUE_WAIT(observer.messages_received() == 2, kTimeoutMs);
|
| + EXPECT_EQ("Hello, World!", observer.messages()[1]);
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
|
|