blob: 28eb86caca4dfe72b1319b1fb4dbb9484cb23fbb [file] [log] [blame]
deadbeefcbecd352015-09-23 11:50:27 -07001/*
2 * Copyright 2009 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#ifndef WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_
12#define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_
13
14#include <map>
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <string>
17#include <vector>
18
deadbeefcbecd352015-09-23 11:50:27 -070019#include "webrtc/base/bind.h"
20#include "webrtc/base/buffer.h"
21#include "webrtc/base/fakesslidentity.h"
22#include "webrtc/base/messagequeue.h"
23#include "webrtc/base/sigslot.h"
24#include "webrtc/base/sslfingerprint.h"
25#include "webrtc/base/thread.h"
zhihuangd06adf62017-01-12 15:58:31 -080026#include "webrtc/p2p/base/candidatepairinterface.h"
27#include "webrtc/p2p/base/icetransportinternal.h"
28#include "webrtc/p2p/base/transportchannel.h"
29#include "webrtc/p2p/base/transportchannelimpl.h"
30#include "webrtc/p2p/base/transportcontroller.h"
deadbeefcbecd352015-09-23 11:50:27 -070031
zhihuang9763d562016-08-05 11:14:50 -070032#ifdef HAVE_QUIC
33#include "webrtc/p2p/quic/quictransport.h"
34#endif
35
deadbeefcbecd352015-09-23 11:50:27 -070036namespace cricket {
37
stefanc1aeaf02015-10-15 07:26:07 -070038namespace {
deadbeefcbecd352015-09-23 11:50:27 -070039struct PacketMessageData : public rtc::MessageData {
40 PacketMessageData(const char* data, size_t len) : packet(data, len) {}
41 rtc::Buffer packet;
42};
stefanc1aeaf02015-10-15 07:26:07 -070043} // namespace
deadbeefcbecd352015-09-23 11:50:27 -070044
zhihuangd06adf62017-01-12 15:58:31 -080045class FakeIceTransport : public IceTransportInternal,
46 public rtc::MessageHandler {
47 public:
48 explicit FakeIceTransport(const std::string& name, int component)
49 : name_(name), component_(component) {}
50 ~FakeIceTransport() { Reset(); }
51
52 const std::string& transport_name() const override { return name_; }
53 int component() const override { return component_; }
54 uint64_t IceTiebreaker() const { return tiebreaker_; }
55 IceMode remote_ice_mode() const { return remote_ice_mode_; }
56 const std::string& ice_ufrag() const { return ice_ufrag_; }
57 const std::string& ice_pwd() const { return ice_pwd_; }
58 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; }
59 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; }
60
61 // If async, will send packets by "Post"-ing to message queue instead of
62 // synchronously "Send"-ing.
63 void SetAsync(bool async) { async_ = async; }
64 void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; }
65
66 IceTransportState GetState() const override {
67 if (connection_count_ == 0) {
68 return had_connection_ ? IceTransportState::STATE_FAILED
69 : IceTransportState::STATE_INIT;
70 }
71
72 if (connection_count_ == 1) {
73 return IceTransportState::STATE_COMPLETED;
74 }
75
76 return IceTransportState::STATE_CONNECTING;
77 }
78
79 void SetIceRole(IceRole role) override { role_ = role; }
80 IceRole GetIceRole() const override { return role_; }
81 void SetIceTiebreaker(uint64_t tiebreaker) override {
82 tiebreaker_ = tiebreaker;
83 }
84 void SetIceParameters(const IceParameters& ice_params) override {
85 ice_ufrag_ = ice_params.ufrag;
86 ice_pwd_ = ice_params.pwd;
87 }
88 void SetRemoteIceParameters(const IceParameters& params) override {
89 remote_ice_ufrag_ = params.ufrag;
90 remote_ice_pwd_ = params.pwd;
91 }
92
93 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; }
94
95 void MaybeStartGathering() override {
96 if (gathering_state_ == kIceGatheringNew) {
97 gathering_state_ = kIceGatheringGathering;
98 SignalGatheringState(this);
99 }
100 }
101
102 IceGatheringState gathering_state() const override {
103 return gathering_state_;
104 }
105
106 void Reset() {
107 if (state_ != STATE_INIT) {
108 state_ = STATE_INIT;
109 if (dest_) {
110 dest_->state_ = STATE_INIT;
111 dest_->dest_ = nullptr;
112 dest_ = nullptr;
113 }
114 }
115 }
116
117 void SetWritable(bool writable) { set_writable(writable); }
118
119 void set_writable(bool writable) {
120 if (writable_ == writable) {
121 return;
122 }
123 LOG(INFO) << "set_writable from:" << writable_ << " to " << writable;
124 writable_ = writable;
125 if (writable_) {
126 SignalReadyToSend(this);
127 }
128 SignalWritableState(this);
129 }
130 bool writable() const override { return writable_; }
131
132 // Simulates the two transports connecting to each other.
133 // If |asymmetric| is true this method only affects this FakeIceTransport.
134 // If false, it affects |dest| as well.
135 void SetDestination(FakeIceTransport* dest, bool asymmetric = false) {
136 if (state_ == STATE_INIT && dest) {
137 // This simulates the delivery of candidates.
138 dest_ = dest;
139 state_ = STATE_CONNECTED;
140 set_writable(true);
141 if (!asymmetric) {
142 dest->SetDestination(this, true);
143 }
144 } else if (state_ == STATE_CONNECTED && !dest) {
145 // Simulates loss of connectivity, by asymmetrically forgetting dest_.
146 dest_ = nullptr;
147 state_ = STATE_INIT;
148 set_writable(false);
149 }
150 }
151
152 void SetConnectionCount(size_t connection_count) {
153 size_t old_connection_count = connection_count_;
154 connection_count_ = connection_count;
155 if (connection_count)
156 had_connection_ = true;
157 // In this fake transport channel, |connection_count_| determines the
158 // transport channel state.
159 if (connection_count_ < old_connection_count)
160 SignalStateChanged(this);
161 }
162
163 void SetCandidatesGatheringComplete() {
164 if (gathering_state_ != kIceGatheringComplete) {
165 gathering_state_ = kIceGatheringComplete;
166 SignalGatheringState(this);
167 }
168 }
169
170 void SetReceiving(bool receiving) { set_receiving(receiving); }
171
172 void set_receiving(bool receiving) {
173 if (receiving_ == receiving) {
174 return;
175 }
176 receiving_ = receiving;
177 SignalReceivingState(this);
178 }
179 bool receiving() const override { return receiving_; }
180
181 void SetIceConfig(const IceConfig& config) override { ice_config_ = config; }
182
183 int receiving_timeout() const { return ice_config_.receiving_timeout; }
184 bool gather_continually() const { return ice_config_.gather_continually(); }
185
186 int SendPacket(const char* data,
187 size_t len,
188 const rtc::PacketOptions& options,
189 int flags) override {
190 if (state_ != STATE_CONNECTED) {
191 return -1;
192 }
193
194 if (flags != PF_SRTP_BYPASS && flags != 0) {
195 return -1;
196 }
197
198 PacketMessageData* packet = new PacketMessageData(data, len);
199 if (async_) {
200 if (async_delay_ms_) {
201 rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, async_delay_ms_,
202 this, 0, packet);
203 } else {
204 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, 0, packet);
205 }
206 } else {
207 rtc::Thread::Current()->Send(RTC_FROM_HERE, this, 0, packet);
208 }
209 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
210 SignalSentPacket(this, sent_packet);
211 return static_cast<int>(len);
212 }
213 int SetOption(rtc::Socket::Option opt, int value) override { return true; }
214 bool GetOption(rtc::Socket::Option opt, int* value) override { return true; }
215 int GetError() override { return 0; }
216
217 void AddRemoteCandidate(const Candidate& candidate) override {
218 remote_candidates_.push_back(candidate);
219 }
220
221 void RemoveRemoteCandidate(const Candidate& candidate) override {}
222
223 const Candidates& remote_candidates() const { return remote_candidates_; }
224
225 void OnMessage(rtc::Message* msg) override {
226 PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata);
227 dest_->SignalReadPacket(dest_, data->packet.data<char>(),
228 data->packet.size(), rtc::CreatePacketTime(0), 0);
229 delete data;
230 }
231
232 bool GetStats(ConnectionInfos* infos) override {
233 ConnectionInfo info;
234 infos->clear();
235 infos->push_back(info);
236 return true;
237 }
238
239 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override {
240 }
241
242 private:
243 std::string name_;
244 int component_;
245 enum State { STATE_INIT, STATE_CONNECTED };
246 FakeIceTransport* dest_ = nullptr;
247 State state_ = STATE_INIT;
248 bool async_ = false;
249 int async_delay_ms_ = 0;
250 Candidates remote_candidates_;
251 IceConfig ice_config_;
252 IceRole role_ = ICEROLE_UNKNOWN;
253 uint64_t tiebreaker_ = 0;
254 std::string ice_ufrag_;
255 std::string ice_pwd_;
256 std::string remote_ice_ufrag_;
257 std::string remote_ice_pwd_;
258 IceMode remote_ice_mode_ = ICEMODE_FULL;
259 size_t connection_count_ = 0;
260 IceGatheringState gathering_state_ = kIceGatheringNew;
261 bool had_connection_ = false;
262 bool writable_ = false;
263 bool receiving_ = false;
264};
265
deadbeefcbecd352015-09-23 11:50:27 -0700266// Fake transport channel class, which can be passed to anything that needs a
267// transport channel. Can be informed of another FakeTransportChannel via
268// SetDestination.
269// TODO(hbos): Move implementation to .cc file, this and other classes in file.
270class FakeTransportChannel : public TransportChannelImpl,
271 public rtc::MessageHandler {
272 public:
mikescarlettb9dd7c52016-02-19 20:43:45 -0800273 explicit FakeTransportChannel(const std::string& name, int component)
deadbeefcbecd352015-09-23 11:50:27 -0700274 : TransportChannelImpl(name, component),
deadbeefcbecd352015-09-23 11:50:27 -0700275 dtls_fingerprint_("", nullptr, 0) {}
276 ~FakeTransportChannel() { Reset(); }
277
Peter Boström0c4e06b2015-10-07 12:23:21 +0200278 uint64_t IceTiebreaker() const { return tiebreaker_; }
deadbeefcbecd352015-09-23 11:50:27 -0700279 IceMode remote_ice_mode() const { return remote_ice_mode_; }
280 const std::string& ice_ufrag() const { return ice_ufrag_; }
281 const std::string& ice_pwd() const { return ice_pwd_; }
282 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; }
283 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; }
284 const rtc::SSLFingerprint& dtls_fingerprint() const {
285 return dtls_fingerprint_;
286 }
287
288 // If async, will send packets by "Post"-ing to message queue instead of
289 // synchronously "Send"-ing.
290 void SetAsync(bool async) { async_ = async; }
deadbeef89824f62016-09-30 11:55:43 -0700291 void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; }
deadbeefcbecd352015-09-23 11:50:27 -0700292
zhihuangd06adf62017-01-12 15:58:31 -0800293 IceTransportState GetState() const override {
deadbeefcbecd352015-09-23 11:50:27 -0700294 if (connection_count_ == 0) {
zhihuangd06adf62017-01-12 15:58:31 -0800295 return had_connection_ ? IceTransportState::STATE_FAILED
296 : IceTransportState::STATE_INIT;
deadbeefcbecd352015-09-23 11:50:27 -0700297 }
298
299 if (connection_count_ == 1) {
zhihuangd06adf62017-01-12 15:58:31 -0800300 return IceTransportState::STATE_COMPLETED;
deadbeefcbecd352015-09-23 11:50:27 -0700301 }
302
zhihuangd06adf62017-01-12 15:58:31 -0800303 return IceTransportState::STATE_CONNECTING;
deadbeefcbecd352015-09-23 11:50:27 -0700304 }
305
306 void SetIceRole(IceRole role) override { role_ = role; }
307 IceRole GetIceRole() const override { return role_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200308 void SetIceTiebreaker(uint64_t tiebreaker) override {
deadbeefcbecd352015-09-23 11:50:27 -0700309 tiebreaker_ = tiebreaker;
310 }
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700311 void SetIceParameters(const IceParameters& ice_params) override {
312 ice_ufrag_ = ice_params.ufrag;
313 ice_pwd_ = ice_params.pwd;
deadbeefcbecd352015-09-23 11:50:27 -0700314 }
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700315 void SetRemoteIceParameters(const IceParameters& params) override {
316 remote_ice_ufrag_ = params.ufrag;
317 remote_ice_pwd_ = params.pwd;
deadbeefcbecd352015-09-23 11:50:27 -0700318 }
319
320 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; }
321 bool SetRemoteFingerprint(const std::string& alg,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200322 const uint8_t* digest,
deadbeefcbecd352015-09-23 11:50:27 -0700323 size_t digest_len) override {
324 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len);
325 return true;
326 }
327 bool SetSslRole(rtc::SSLRole role) override {
328 ssl_role_ = role;
329 return true;
330 }
331 bool GetSslRole(rtc::SSLRole* role) const override {
332 *role = ssl_role_;
333 return true;
334 }
335
deadbeefcbecd352015-09-23 11:50:27 -0700336 void MaybeStartGathering() override {
337 if (gathering_state_ == kIceGatheringNew) {
338 gathering_state_ = kIceGatheringGathering;
339 SignalGatheringState(this);
340 }
341 }
342
343 IceGatheringState gathering_state() const override {
344 return gathering_state_;
345 }
346
347 void Reset() {
348 if (state_ != STATE_INIT) {
349 state_ = STATE_INIT;
350 if (dest_) {
351 dest_->state_ = STATE_INIT;
352 dest_->dest_ = nullptr;
353 dest_ = nullptr;
354 }
355 }
356 }
357
358 void SetWritable(bool writable) { set_writable(writable); }
359
deadbeefe84cd2e2016-05-04 17:16:34 -0700360 // Simulates the two transport channels connecting to each other.
361 // If |asymmetric| is true this method only affects this FakeTransportChannel.
362 // If false, it affects |dest| as well.
363 void SetDestination(FakeTransportChannel* dest, bool asymmetric = false) {
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700364 if (state_ == STATE_INIT && dest) {
deadbeefcbecd352015-09-23 11:50:27 -0700365 // This simulates the delivery of candidates.
366 dest_ = dest;
deadbeefcbecd352015-09-23 11:50:27 -0700367 if (local_cert_ && dest_->local_cert_) {
368 do_dtls_ = true;
deadbeefcbecd352015-09-23 11:50:27 -0700369 NegotiateSrtpCiphers();
370 }
371 state_ = STATE_CONNECTED;
deadbeefcbecd352015-09-23 11:50:27 -0700372 set_writable(true);
deadbeefe84cd2e2016-05-04 17:16:34 -0700373 if (!asymmetric) {
374 dest->SetDestination(this, true);
375 }
deadbeefcbecd352015-09-23 11:50:27 -0700376 } else if (state_ == STATE_CONNECTED && !dest) {
377 // Simulates loss of connectivity, by asymmetrically forgetting dest_.
378 dest_ = nullptr;
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700379 state_ = STATE_INIT;
deadbeefcbecd352015-09-23 11:50:27 -0700380 set_writable(false);
381 }
382 }
383
384 void SetConnectionCount(size_t connection_count) {
385 size_t old_connection_count = connection_count_;
386 connection_count_ = connection_count;
387 if (connection_count)
388 had_connection_ = true;
Honghai Zhang1590c392016-05-24 13:15:02 -0700389 // In this fake transport channel, |connection_count_| determines the
390 // transport channel state.
deadbeefcbecd352015-09-23 11:50:27 -0700391 if (connection_count_ < old_connection_count)
Honghai Zhang1590c392016-05-24 13:15:02 -0700392 SignalStateChanged(this);
deadbeefcbecd352015-09-23 11:50:27 -0700393 }
394
395 void SetCandidatesGatheringComplete() {
396 if (gathering_state_ != kIceGatheringComplete) {
397 gathering_state_ = kIceGatheringComplete;
398 SignalGatheringState(this);
399 }
400 }
401
402 void SetReceiving(bool receiving) { set_receiving(receiving); }
403
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700404 void SetIceConfig(const IceConfig& config) override { ice_config_ = config; }
deadbeefcbecd352015-09-23 11:50:27 -0700405
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700406 int receiving_timeout() const { return ice_config_.receiving_timeout; }
407 bool gather_continually() const { return ice_config_.gather_continually(); }
deadbeefcbecd352015-09-23 11:50:27 -0700408
409 int SendPacket(const char* data,
410 size_t len,
411 const rtc::PacketOptions& options,
412 int flags) override {
413 if (state_ != STATE_CONNECTED) {
414 return -1;
415 }
416
417 if (flags != PF_SRTP_BYPASS && flags != 0) {
418 return -1;
419 }
420
421 PacketMessageData* packet = new PacketMessageData(data, len);
422 if (async_) {
deadbeef89824f62016-09-30 11:55:43 -0700423 if (async_delay_ms_) {
424 rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, async_delay_ms_,
425 this, 0, packet);
426 } else {
427 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, 0, packet);
428 }
deadbeefcbecd352015-09-23 11:50:27 -0700429 } else {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700430 rtc::Thread::Current()->Send(RTC_FROM_HERE, this, 0, packet);
deadbeefcbecd352015-09-23 11:50:27 -0700431 }
nisse1bffc1d2016-05-02 08:18:55 -0700432 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
stefanc1aeaf02015-10-15 07:26:07 -0700433 SignalSentPacket(this, sent_packet);
deadbeefcbecd352015-09-23 11:50:27 -0700434 return static_cast<int>(len);
435 }
436 int SetOption(rtc::Socket::Option opt, int value) override { return true; }
437 bool GetOption(rtc::Socket::Option opt, int* value) override { return true; }
438 int GetError() override { return 0; }
439
440 void AddRemoteCandidate(const Candidate& candidate) override {
441 remote_candidates_.push_back(candidate);
442 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700443
444 void RemoveRemoteCandidate(const Candidate& candidate) override {}
445
deadbeefcbecd352015-09-23 11:50:27 -0700446 const Candidates& remote_candidates() const { return remote_candidates_; }
447
448 void OnMessage(rtc::Message* msg) override {
449 PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata);
450 dest_->SignalReadPacket(dest_, data->packet.data<char>(),
451 data->packet.size(), rtc::CreatePacketTime(0), 0);
452 delete data;
453 }
454
455 bool SetLocalCertificate(
nisseef8b61e2016-04-29 06:09:15 -0700456 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
deadbeefcbecd352015-09-23 11:50:27 -0700457 local_cert_ = certificate;
458 return true;
459 }
460
461 void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) {
462 remote_cert_ = cert;
463 }
464
465 bool IsDtlsActive() const override { return do_dtls_; }
466
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800467 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override {
deadbeefcbecd352015-09-23 11:50:27 -0700468 srtp_ciphers_ = ciphers;
469 return true;
470 }
471
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800472 bool GetSrtpCryptoSuite(int* crypto_suite) override {
473 if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) {
474 *crypto_suite = chosen_crypto_suite_;
deadbeefcbecd352015-09-23 11:50:27 -0700475 return true;
476 }
477 return false;
478 }
479
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800480 bool GetSslCipherSuite(int* cipher_suite) override { return false; }
deadbeefcbecd352015-09-23 11:50:27 -0700481
nisseef8b61e2016-04-29 06:09:15 -0700482 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
deadbeefcbecd352015-09-23 11:50:27 -0700483 return local_cert_;
484 }
485
jbauch555604a2016-04-26 03:13:22 -0700486 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate()
kwibergb4d01c42016-04-06 05:15:06 -0700487 const override {
jbauch555604a2016-04-26 03:13:22 -0700488 return remote_cert_ ? std::unique_ptr<rtc::SSLCertificate>(
kwibergb4d01c42016-04-06 05:15:06 -0700489 remote_cert_->GetReference())
490 : nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700491 }
492
493 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200494 const uint8_t* context,
deadbeefcbecd352015-09-23 11:50:27 -0700495 size_t context_len,
496 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200497 uint8_t* result,
deadbeefcbecd352015-09-23 11:50:27 -0700498 size_t result_len) override {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800499 if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) {
deadbeefcbecd352015-09-23 11:50:27 -0700500 memset(result, 0xff, result_len);
501 return true;
502 }
503
504 return false;
505 }
506
deadbeefcbecd352015-09-23 11:50:27 -0700507 bool GetStats(ConnectionInfos* infos) override {
508 ConnectionInfo info;
509 infos->clear();
510 infos->push_back(info);
511 return true;
512 }
513
514 void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) {
515 ssl_max_version_ = version;
516 }
517 rtc::SSLProtocolVersion ssl_max_protocol_version() const {
518 return ssl_max_version_;
519 }
520
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700521 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override {
522 }
523
deadbeefcbecd352015-09-23 11:50:27 -0700524 private:
deadbeefe84cd2e2016-05-04 17:16:34 -0700525 void NegotiateSrtpCiphers() {
526 for (std::vector<int>::const_iterator it1 = srtp_ciphers_.begin();
527 it1 != srtp_ciphers_.end(); ++it1) {
528 for (std::vector<int>::const_iterator it2 = dest_->srtp_ciphers_.begin();
529 it2 != dest_->srtp_ciphers_.end(); ++it2) {
530 if (*it1 == *it2) {
531 chosen_crypto_suite_ = *it1;
532 return;
533 }
534 }
535 }
536 }
537
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700538 enum State { STATE_INIT, STATE_CONNECTED };
deadbeefcbecd352015-09-23 11:50:27 -0700539 FakeTransportChannel* dest_ = nullptr;
540 State state_ = STATE_INIT;
541 bool async_ = false;
deadbeef89824f62016-09-30 11:55:43 -0700542 int async_delay_ms_ = 0;
deadbeefcbecd352015-09-23 11:50:27 -0700543 Candidates remote_candidates_;
544 rtc::scoped_refptr<rtc::RTCCertificate> local_cert_;
545 rtc::FakeSSLCertificate* remote_cert_ = nullptr;
546 bool do_dtls_ = false;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800547 std::vector<int> srtp_ciphers_;
548 int chosen_crypto_suite_ = rtc::SRTP_INVALID_CRYPTO_SUITE;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700549 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700550 IceRole role_ = ICEROLE_UNKNOWN;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200551 uint64_t tiebreaker_ = 0;
deadbeefcbecd352015-09-23 11:50:27 -0700552 std::string ice_ufrag_;
553 std::string ice_pwd_;
554 std::string remote_ice_ufrag_;
555 std::string remote_ice_pwd_;
556 IceMode remote_ice_mode_ = ICEMODE_FULL;
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800557 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700558 rtc::SSLFingerprint dtls_fingerprint_;
559 rtc::SSLRole ssl_role_ = rtc::SSL_CLIENT;
560 size_t connection_count_ = 0;
561 IceGatheringState gathering_state_ = kIceGatheringNew;
562 bool had_connection_ = false;
563};
564
Honghai Zhangcc411c02016-03-29 17:27:21 -0700565// Fake candidate pair class, which can be passed to BaseChannel for testing
566// purposes.
567class FakeCandidatePair : public CandidatePairInterface {
568 public:
569 FakeCandidatePair(const Candidate& local_candidate,
570 const Candidate& remote_candidate)
571 : local_candidate_(local_candidate),
572 remote_candidate_(remote_candidate) {}
573 const Candidate& local_candidate() const override { return local_candidate_; }
574 const Candidate& remote_candidate() const override {
575 return remote_candidate_;
576 }
577
578 private:
579 Candidate local_candidate_;
580 Candidate remote_candidate_;
581};
582
deadbeefcbecd352015-09-23 11:50:27 -0700583// Fake TransportController class, which can be passed into a BaseChannel object
584// for test purposes. Can be connected to other FakeTransportControllers via
585// Connect().
586//
587// This fake is unusual in that for the most part, it's implemented with the
588// real TransportController code, but with fake TransportChannels underneath.
589class FakeTransportController : public TransportController {
590 public:
591 FakeTransportController()
592 : TransportController(rtc::Thread::Current(),
593 rtc::Thread::Current(),
deadbeef49f34fd2016-12-06 16:22:06 -0800594 nullptr) {}
deadbeefcbecd352015-09-23 11:50:27 -0700595
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700596 explicit FakeTransportController(bool redetermine_role_on_ice_restart)
597 : TransportController(rtc::Thread::Current(),
598 rtc::Thread::Current(),
599 nullptr,
deadbeef49f34fd2016-12-06 16:22:06 -0800600 redetermine_role_on_ice_restart) {}
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700601
deadbeefcbecd352015-09-23 11:50:27 -0700602 explicit FakeTransportController(IceRole role)
603 : TransportController(rtc::Thread::Current(),
604 rtc::Thread::Current(),
deadbeef49f34fd2016-12-06 16:22:06 -0800605 nullptr) {
deadbeefcbecd352015-09-23 11:50:27 -0700606 SetIceRole(role);
607 }
608
johan27c3d5b2016-10-17 00:54:57 -0700609 explicit FakeTransportController(rtc::Thread* network_thread)
deadbeef49f34fd2016-12-06 16:22:06 -0800610 : TransportController(rtc::Thread::Current(), network_thread, nullptr) {}
deadbeefcbecd352015-09-23 11:50:27 -0700611
johan27c3d5b2016-10-17 00:54:57 -0700612 FakeTransportController(rtc::Thread* network_thread, IceRole role)
deadbeef49f34fd2016-12-06 16:22:06 -0800613 : TransportController(rtc::Thread::Current(), network_thread, nullptr) {
deadbeefcbecd352015-09-23 11:50:27 -0700614 SetIceRole(role);
615 }
616
deadbeef49f34fd2016-12-06 16:22:06 -0800617 FakeTransportChannel* GetFakeTransportChannel_n(
618 const std::string& transport_name,
619 int component) {
620 return static_cast<FakeTransportChannel*>(
621 get_channel_for_testing(transport_name, component));
deadbeefcbecd352015-09-23 11:50:27 -0700622 }
623
deadbeef49f34fd2016-12-06 16:22:06 -0800624 // Simulate the exchange of transport descriptions, and the gathering and
625 // exchange of ICE candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700626 void Connect(FakeTransportController* dest) {
deadbeef49f34fd2016-12-06 16:22:06 -0800627 for (const std::string& transport_name : transport_names_for_testing()) {
628 TransportDescription local_desc(
629 std::vector<std::string>(),
630 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH),
631 rtc::CreateRandomString(cricket::ICE_PWD_LENGTH),
632 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, nullptr);
633 TransportDescription remote_desc(
634 std::vector<std::string>(),
635 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH),
636 rtc::CreateRandomString(cricket::ICE_PWD_LENGTH),
637 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, nullptr);
638 std::string err;
639 SetLocalTransportDescription(transport_name, local_desc,
640 cricket::CA_OFFER, &err);
641 dest->SetRemoteTransportDescription(transport_name, local_desc,
642 cricket::CA_OFFER, &err);
643 dest->SetLocalTransportDescription(transport_name, remote_desc,
644 cricket::CA_ANSWER, &err);
645 SetRemoteTransportDescription(transport_name, remote_desc,
646 cricket::CA_ANSWER, &err);
647 }
648 MaybeStartGathering();
649 dest->MaybeStartGathering();
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200650 network_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700651 RTC_FROM_HERE,
deadbeef49f34fd2016-12-06 16:22:06 -0800652 rtc::Bind(&FakeTransportController::SetChannelDestinations_n, this,
653 dest));
deadbeefcbecd352015-09-23 11:50:27 -0700654 }
655
Honghai Zhangcc411c02016-03-29 17:27:21 -0700656 FakeCandidatePair* CreateFakeCandidatePair(
657 const rtc::SocketAddress& local_address,
658 int16_t local_network_id,
659 const rtc::SocketAddress& remote_address,
660 int16_t remote_network_id) {
661 Candidate local_candidate(0, "udp", local_address, 0u, "", "", "local", 0,
662 "foundation", local_network_id, 0);
663 Candidate remote_candidate(0, "udp", remote_address, 0u, "", "", "local", 0,
664 "foundation", remote_network_id, 0);
665 return new FakeCandidatePair(local_candidate, remote_candidate);
666 }
667
deadbeef57fd7262016-12-06 15:28:55 -0800668 protected:
deadbeef49f34fd2016-12-06 16:22:06 -0800669 // The ICE channel is never actually used by TransportController directly,
670 // since (currently) the DTLS channel pretends to be both ICE + DTLS. This
671 // will change when we get rid of TransportChannelImpl.
zhihuangd06adf62017-01-12 15:58:31 -0800672 IceTransportInternal* CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800673 const std::string& transport_name,
674 int component) override {
675 return nullptr;
deadbeef57fd7262016-12-06 15:28:55 -0800676 }
677
deadbeef49f34fd2016-12-06 16:22:06 -0800678 TransportChannelImpl* CreateDtlsTransportChannel_n(
679 const std::string& transport_name,
680 int component,
zhihuangd06adf62017-01-12 15:58:31 -0800681 IceTransportInternal*) override {
deadbeef49f34fd2016-12-06 16:22:06 -0800682 return new FakeTransportChannel(transport_name, component);
deadbeefcbecd352015-09-23 11:50:27 -0700683 }
684
685 private:
deadbeef49f34fd2016-12-06 16:22:06 -0800686 void SetChannelDestinations_n(FakeTransportController* dest) {
687 for (TransportChannelImpl* tc : channels_for_testing()) {
688 FakeTransportChannel* local = static_cast<FakeTransportChannel*>(tc);
689 FakeTransportChannel* remote = dest->GetFakeTransportChannel_n(
690 local->transport_name(), local->component());
691 if (remote) {
692 bool asymmetric = false;
693 local->SetDestination(remote, asymmetric);
694 }
695 }
696 }
deadbeefcbecd352015-09-23 11:50:27 -0700697};
698
699} // namespace cricket
700
701#endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_