Chromium Code Reviews| Index: webrtc/p2p/base/faketransportcontroller.h |
| diff --git a/webrtc/p2p/base/faketransportcontroller.h b/webrtc/p2p/base/faketransportcontroller.h |
| index d42a93ddae9f95cf1558f9d32f3273f5e9a113c3..ac9d57192a59020be072b03d8dc47bdbcfb825f2 100644 |
| --- a/webrtc/p2p/base/faketransportcontroller.h |
| +++ b/webrtc/p2p/base/faketransportcontroller.h |
| @@ -16,10 +16,6 @@ |
| #include <string> |
| #include <vector> |
| -#include "webrtc/p2p/base/candidatepairinterface.h" |
| -#include "webrtc/p2p/base/transportchannel.h" |
| -#include "webrtc/p2p/base/transportcontroller.h" |
| -#include "webrtc/p2p/base/transportchannelimpl.h" |
| #include "webrtc/base/bind.h" |
| #include "webrtc/base/buffer.h" |
| #include "webrtc/base/fakesslidentity.h" |
| @@ -27,6 +23,11 @@ |
| #include "webrtc/base/sigslot.h" |
| #include "webrtc/base/sslfingerprint.h" |
| #include "webrtc/base/thread.h" |
| +#include "webrtc/p2p/base/candidatepairinterface.h" |
| +#include "webrtc/p2p/base/dtlstransportinternal.h" |
| +#include "webrtc/p2p/base/transportchannel.h" |
| +#include "webrtc/p2p/base/transportcontroller.h" |
| +#include "webrtc/p2p/base/transportchannelimpl.h" |
| #ifdef HAVE_QUIC |
| #include "webrtc/p2p/quic/quictransport.h" |
| @@ -340,6 +341,278 @@ class FakeTransportChannel : public TransportChannelImpl, |
| bool had_connection_ = false; |
| }; |
| +class FakeDtlsTransport : public DtlsTransportInternal, |
| + public rtc::MessageHandler { |
| + public: |
| + FakeDtlsTransport(FakeTransportChannel* ice_transport) |
| + : ice_transport_(ice_transport), |
| + transport_name_(ice_transport->transport_name()), |
| + component_(ice_transport->component()), |
| + dtls_fingerprint_("", nullptr, 0) {} |
| + ~FakeDtlsTransport() { Reset(); } |
| + |
| + uint64_t IceTiebreaker() const { return ice_transport_->IceTiebreaker(); } |
| + IceMode remote_ice_mode() const { return ice_transport_->remote_ice_mode(); } |
| + const std::string& ice_ufrag() const { return ice_transport_->ice_ufrag(); } |
| + const std::string& ice_pwd() const { return ice_transport_->ice_pwd(); } |
| + const std::string& remote_ice_ufrag() const { |
| + return ice_transport_->remote_ice_ufrag(); |
| + } |
| + const std::string& remote_ice_pwd() const { |
| + return ice_transport_->remote_ice_pwd(); |
| + } |
| + |
| + DtlsTransportState dtls_state() const override { return dtls_state_; } |
| + |
| + const std::string& transport_name() const override { return transport_name_; } |
| + |
| + int component() const override { return component_; } |
| + |
| + const rtc::SSLFingerprint& dtls_fingerprint() const { |
| + return dtls_fingerprint_; |
| + } |
| + |
| + // If async, will send packets by "Post"-ing to message queue instead of |
| + // synchronously "Send"-ing. |
| + void SetAsync(bool async) { async_ = async; } |
| + void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; } |
|
Taylor Brandstetter
2017/01/03 23:21:59
I think the async settings only need to go in one
Zhi Huang
2017/01/10 18:30:51
I'll clean up the duplication when merging the two
|
| + |
| + IceRole GetIceRole() const { return ice_transport_->GetIceRole(); } |
| + |
| + bool SetRemoteFingerprint(const std::string& alg, |
| + const uint8_t* digest, |
| + size_t digest_len) override { |
| + dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); |
| + return true; |
| + } |
| + |
| + bool SetSslRole(rtc::SSLRole role) override { |
| + ssl_role_ = role; |
| + return true; |
| + } |
| + |
| + bool GetSslRole(rtc::SSLRole* role) const override { |
| + *role = ssl_role_; |
| + return true; |
| + } |
| + |
| + IceGatheringState gathering_state() const { |
| + return ice_transport_->gathering_state(); |
| + } |
| + |
| + void Reset() { |
| + if (state_ != STATE_INIT) { |
| + state_ = STATE_INIT; |
| + if (dest_) { |
| + dest_->state_ = STATE_INIT; |
| + dest_->dest_ = nullptr; |
| + dest_ = nullptr; |
| + } |
| + } |
| + } |
| + |
| + void SetWritable(bool writable) { |
| + ice_transport_->SetWritable(writable); |
| + SignalWritableState(this); |
| + } |
| + |
| + // Simulates the two transport channels connecting to each other. |
| + // If |asymmetric| is true this method only affects this FakeDtlsTransport. |
| + // If false, it affects |dest| as well. |
| + void SetDestination(FakeDtlsTransport* dest, bool asymmetric = false) { |
| + if (state_ == STATE_INIT && dest) { |
| + // This simulates the delivery of candidates. |
| + dest_ = dest; |
| + if (local_cert_ && dest_->local_cert_) { |
| + do_dtls_ = true; |
| + NegotiateSrtpCiphers(); |
| + } |
| + state_ = STATE_CONNECTED; |
| + SetWritable(true); |
| + if (!asymmetric) { |
| + dest->SetDestination(this, true); |
| + } |
| + } else if (state_ == STATE_CONNECTED && !dest) { |
| + // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
| + dest_ = nullptr; |
| + state_ = STATE_INIT; |
| + SetWritable(false); |
| + } |
| + } |
| + |
| + void SetConnectionCount(size_t connection_count) { |
| + ice_transport_->SetConnectionCount(connection_count); |
| + } |
| + |
| + void SetCandidatesGatheringComplete() { |
| + ice_transport_->SetCandidatesGatheringComplete(); |
| + } |
| + |
| + void SetReceiving(bool receiving) { |
| + ice_transport_->SetReceiving(receiving); |
| + SignalReceivingState(this); |
| + } |
| + |
| + int receiving_timeout() const { return ice_transport_->receiving_timeout(); } |
| + bool gather_continually() const { |
| + return ice_transport_->gather_continually(); |
| + } |
| + |
| + int SendPacket(const char* data, |
| + size_t len, |
| + const rtc::PacketOptions& options, |
| + int flags) override { |
| + if (state_ != STATE_CONNECTED) { |
| + return -1; |
| + } |
| + |
| + if (flags != PF_SRTP_BYPASS && flags != 0) { |
| + return -1; |
| + } |
| + |
|
Taylor Brandstetter
2017/01/03 23:21:59
Can this just call ice_transport_->SendPacket, and
Zhi Huang
2017/01/10 18:30:51
Done.
|
| + PacketMessageData* packet = new PacketMessageData(data, len); |
| + if (async_) { |
| + if (async_delay_ms_) { |
| + rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, async_delay_ms_, |
| + this, 0, packet); |
| + } else { |
| + rtc::Thread::Current()->Post(RTC_FROM_HERE, this, 0, packet); |
| + } |
| + } else { |
| + rtc::Thread::Current()->Send(RTC_FROM_HERE, this, 0, packet); |
| + } |
| + rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis()); |
| + SignalSentPacket(this, sent_packet); |
| + return static_cast<int>(len); |
| + } |
| + |
| + bool GetOption(rtc::Socket::Option opt, int* value) override { return true; } |
| + |
| + const Candidates& remote_candidates() const { |
| + return ice_transport_->remote_candidates(); |
| + } |
| + |
| + void OnMessage(rtc::Message* msg) override { |
| + PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata); |
| + dest_->SignalReadPacket(dest_, data->packet.data<char>(), |
| + data->packet.size(), rtc::CreatePacketTime(0), 0); |
| + delete data; |
| + } |
| + |
| + bool SetLocalCertificate( |
| + const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
| + local_cert_ = certificate; |
| + return true; |
| + } |
| + |
| + void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) { |
| + remote_cert_ = cert; |
| + } |
| + |
| + bool IsDtlsActive() const override { return do_dtls_; } |
| + |
| + bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { |
| + srtp_ciphers_ = ciphers; |
| + return true; |
| + } |
| + |
| + bool GetSrtpCryptoSuite(int* crypto_suite) override { |
| + if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| + *crypto_suite = chosen_crypto_suite_; |
| + return true; |
| + } |
| + return false; |
| + } |
| + |
| + bool GetSslCipherSuite(int* cipher_suite) override { return false; } |
| + |
| + rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override { |
| + return local_cert_; |
| + } |
| + |
| + std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate() |
| + const override { |
| + return remote_cert_ ? std::unique_ptr<rtc::SSLCertificate>( |
| + remote_cert_->GetReference()) |
| + : nullptr; |
| + } |
| + |
| + bool ExportKeyingMaterial(const std::string& label, |
| + const uint8_t* context, |
| + size_t context_len, |
| + bool use_context, |
| + uint8_t* result, |
| + size_t result_len) override { |
| + if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| + memset(result, 0xff, result_len); |
| + return true; |
| + } |
| + |
| + return false; |
| + } |
| + |
| + void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { |
| + ssl_max_version_ = version; |
| + } |
| + rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
| + return ssl_max_version_; |
| + } |
| + |
| + TransportChannelImpl* ice_transport() override { return ice_transport_; } |
| + |
| + bool writable() const override { return ice_transport_->writable(); } |
| + |
| + bool receiving() const override { return ice_transport_->receiving(); } |
| + |
| + int GetError() override { return ice_transport_->GetError(); } |
| + |
| + int SetOption(rtc::Socket::Option opt, int value) override { |
| + return ice_transport_->SetOption(opt, value); |
| + } |
| + |
| + bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { |
| + std::vector<int> crypto_suites; |
| + for (const auto cipher : ciphers) { |
| + crypto_suites.push_back(rtc::SrtpCryptoSuiteFromName(cipher)); |
| + } |
| + return SetSrtpCryptoSuites(crypto_suites); |
| + } |
| + |
| + private: |
| + void NegotiateSrtpCiphers() { |
| + for (std::vector<int>::const_iterator it1 = srtp_ciphers_.begin(); |
| + it1 != srtp_ciphers_.end(); ++it1) { |
| + for (std::vector<int>::const_iterator it2 = dest_->srtp_ciphers_.begin(); |
| + it2 != dest_->srtp_ciphers_.end(); ++it2) { |
| + if (*it1 == *it2) { |
| + chosen_crypto_suite_ = *it1; |
| + return; |
| + } |
| + } |
| + } |
| + } |
| + |
| + enum State { STATE_INIT, STATE_CONNECTED }; |
| + FakeTransportChannel* ice_transport_; |
| + std::string transport_name_; |
| + int component_; |
| + FakeDtlsTransport* dest_ = nullptr; |
| + State state_ = STATE_INIT; |
| + bool async_ = false; |
| + int async_delay_ms_ = 0; |
| + Candidates remote_candidates_; |
| + rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; |
| + rtc::FakeSSLCertificate* remote_cert_ = nullptr; |
| + bool do_dtls_ = false; |
| + std::vector<int> srtp_ciphers_; |
| + int chosen_crypto_suite_ = rtc::SRTP_INVALID_CRYPTO_SUITE; |
| + rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12; |
| + rtc::SSLFingerprint dtls_fingerprint_; |
| + rtc::SSLRole ssl_role_ = rtc::SSL_CLIENT; |
| + |
| + DtlsTransportState dtls_state_ = DTLS_TRANSPORT_NEW; |
| +}; |
| + |
| // Fake candidate pair class, which can be passed to BaseChannel for testing |
| // purposes. |
| class FakeCandidatePair : public CandidatePairInterface { |
| @@ -392,10 +665,9 @@ class FakeTransportController : public TransportController { |
| SetIceRole(role); |
| } |
| - FakeTransportChannel* GetFakeTransportChannel_n( |
| - const std::string& transport_name, |
| - int component) { |
| - return static_cast<FakeTransportChannel*>( |
| + FakeDtlsTransport* GetFakeDtlsTransport_n(const std::string& transport_name, |
| + int component) { |
| + return static_cast<FakeDtlsTransport*>( |
| get_channel_for_testing(transport_name, component)); |
| } |
| @@ -450,21 +722,21 @@ class FakeTransportController : public TransportController { |
| TransportChannelImpl* CreateIceTransportChannel_n( |
| const std::string& transport_name, |
| int component) override { |
| - return nullptr; |
| + return new FakeTransportChannel(transport_name, component); |
| } |
| - TransportChannelImpl* CreateDtlsTransportChannel_n( |
| + DtlsTransportInternal* CreateDtlsTransportChannel_n( |
| const std::string& transport_name, |
| int component, |
| - TransportChannelImpl*) override { |
| - return new FakeTransportChannel(transport_name, component); |
| + TransportChannelImpl* ice) override { |
| + return new FakeDtlsTransport(static_cast<FakeTransportChannel*>(ice)); |
| } |
| private: |
| void SetChannelDestinations_n(FakeTransportController* dest) { |
| - for (TransportChannelImpl* tc : channels_for_testing()) { |
| - FakeTransportChannel* local = static_cast<FakeTransportChannel*>(tc); |
| - FakeTransportChannel* remote = dest->GetFakeTransportChannel_n( |
| + for (DtlsTransportInternal* tc : channels_for_testing()) { |
| + FakeDtlsTransport* local = static_cast<FakeDtlsTransport*>(tc); |
| + FakeDtlsTransport* remote = dest->GetFakeDtlsTransport_n( |
| local->transport_name(), local->component()); |
| if (remote) { |
| bool asymmetric = false; |