blob: e17956769fbbf5637f8ca15b537ece132d79c3fa [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
Zhi Huangb5261582017-09-29 10:51:43 -070011#ifndef PC_TEST_FAKETRANSPORTCONTROLLER_H_
12#define PC_TEST_FAKETRANSPORTCONTROLLER_H_
deadbeefcbecd352015-09-23 11:50:27 -070013
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
deadbeefcbecd352015-09-23 11:50:27 -070015#include <string>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/fakedtlstransport.h"
19#include "p2p/base/fakeicetransport.h"
Zhi Huangb5261582017-09-29 10:51:43 -070020#include "pc/transportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/bind.h"
22#include "rtc_base/sslfingerprint.h"
23#include "rtc_base/thread.h"
deadbeefcbecd352015-09-23 11:50:27 -070024
25namespace cricket {
26
deadbeeff5346592017-01-24 21:51:21 -080027// Fake TransportController class, which can be passed into a WebRtcSession
28// object for test purposes. Can be connected to other FakeTransportControllers
29// via Connect().
deadbeefcbecd352015-09-23 11:50:27 -070030//
31// This fake is unusual in that for the most part, it's implemented with the
32// real TransportController code, but with fake TransportChannels underneath.
33class FakeTransportController : public TransportController {
34 public:
35 FakeTransportController()
36 : TransportController(rtc::Thread::Current(),
37 rtc::Thread::Current(),
deadbeef7914b8c2017-04-21 03:23:33 -070038 nullptr,
39 /*redetermine_role_on_ice_restart=*/true,
40 rtc::CryptoOptions()) {}
deadbeefcbecd352015-09-23 11:50:27 -070041
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070042 explicit FakeTransportController(bool redetermine_role_on_ice_restart)
43 : TransportController(rtc::Thread::Current(),
44 rtc::Thread::Current(),
45 nullptr,
deadbeef7914b8c2017-04-21 03:23:33 -070046 redetermine_role_on_ice_restart,
47 rtc::CryptoOptions()) {}
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070048
deadbeefcbecd352015-09-23 11:50:27 -070049 explicit FakeTransportController(IceRole role)
50 : TransportController(rtc::Thread::Current(),
51 rtc::Thread::Current(),
deadbeef7914b8c2017-04-21 03:23:33 -070052 nullptr,
53 /*redetermine_role_on_ice_restart=*/true,
54 rtc::CryptoOptions()) {
deadbeefcbecd352015-09-23 11:50:27 -070055 SetIceRole(role);
56 }
57
johan27c3d5b2016-10-17 00:54:57 -070058 explicit FakeTransportController(rtc::Thread* network_thread)
deadbeef7914b8c2017-04-21 03:23:33 -070059 : TransportController(rtc::Thread::Current(),
60 network_thread,
61 nullptr,
62 /*redetermine_role_on_ice_restart=*/true,
63 rtc::CryptoOptions()) {}
deadbeefcbecd352015-09-23 11:50:27 -070064
johan27c3d5b2016-10-17 00:54:57 -070065 FakeTransportController(rtc::Thread* network_thread, IceRole role)
deadbeef7914b8c2017-04-21 03:23:33 -070066 : TransportController(rtc::Thread::Current(),
67 network_thread,
68 nullptr,
69 /*redetermine_role_on_ice_restart=*/true,
70 rtc::CryptoOptions()) {
deadbeefcbecd352015-09-23 11:50:27 -070071 SetIceRole(role);
72 }
73
zhihuangb2cdd932017-01-19 16:54:25 -080074 FakeDtlsTransport* GetFakeDtlsTransport_n(const std::string& transport_name,
75 int component) {
76 return static_cast<FakeDtlsTransport*>(
deadbeef49f34fd2016-12-06 16:22:06 -080077 get_channel_for_testing(transport_name, component));
deadbeefcbecd352015-09-23 11:50:27 -070078 }
79
deadbeef49f34fd2016-12-06 16:22:06 -080080 // Simulate the exchange of transport descriptions, and the gathering and
81 // exchange of ICE candidates.
deadbeefcbecd352015-09-23 11:50:27 -070082 void Connect(FakeTransportController* dest) {
deadbeef49f34fd2016-12-06 16:22:06 -080083 for (const std::string& transport_name : transport_names_for_testing()) {
deadbeef3e4faae2017-01-20 22:43:34 -080084 std::unique_ptr<rtc::SSLFingerprint> local_fingerprint;
85 std::unique_ptr<rtc::SSLFingerprint> remote_fingerprint;
86 if (certificate_for_testing()) {
87 local_fingerprint.reset(rtc::SSLFingerprint::CreateFromCertificate(
88 certificate_for_testing()));
89 }
90 if (dest->certificate_for_testing()) {
91 remote_fingerprint.reset(rtc::SSLFingerprint::CreateFromCertificate(
92 dest->certificate_for_testing()));
93 }
deadbeef49f34fd2016-12-06 16:22:06 -080094 TransportDescription local_desc(
95 std::vector<std::string>(),
96 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH),
97 rtc::CreateRandomString(cricket::ICE_PWD_LENGTH),
deadbeef8662f942017-01-20 21:20:51 -080098 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE,
deadbeef3e4faae2017-01-20 22:43:34 -080099 local_fingerprint.get());
deadbeef49f34fd2016-12-06 16:22:06 -0800100 TransportDescription remote_desc(
101 std::vector<std::string>(),
102 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH),
103 rtc::CreateRandomString(cricket::ICE_PWD_LENGTH),
deadbeef8662f942017-01-20 21:20:51 -0800104 cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE,
deadbeef3e4faae2017-01-20 22:43:34 -0800105 remote_fingerprint.get());
deadbeef49f34fd2016-12-06 16:22:06 -0800106 std::string err;
107 SetLocalTransportDescription(transport_name, local_desc,
108 cricket::CA_OFFER, &err);
109 dest->SetRemoteTransportDescription(transport_name, local_desc,
110 cricket::CA_OFFER, &err);
111 dest->SetLocalTransportDescription(transport_name, remote_desc,
112 cricket::CA_ANSWER, &err);
113 SetRemoteTransportDescription(transport_name, remote_desc,
114 cricket::CA_ANSWER, &err);
115 }
116 MaybeStartGathering();
117 dest->MaybeStartGathering();
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200118 network_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700119 RTC_FROM_HERE,
deadbeef49f34fd2016-12-06 16:22:06 -0800120 rtc::Bind(&FakeTransportController::SetChannelDestinations_n, this,
121 dest));
deadbeefcbecd352015-09-23 11:50:27 -0700122 }
123
zhihuangf5b251b2017-01-12 19:37:48 -0800124 void DestroyRtcpTransport(const std::string& transport_name) {
zhihuangb2cdd932017-01-19 16:54:25 -0800125 DestroyDtlsTransport_n(transport_name,
126 cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -0800127 }
128
deadbeef57fd7262016-12-06 15:28:55 -0800129 protected:
zhihuangd06adf62017-01-12 15:58:31 -0800130 IceTransportInternal* CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800131 const std::string& transport_name,
132 int component) override {
zhihuangb2cdd932017-01-19 16:54:25 -0800133 return new FakeIceTransport(transport_name, component);
deadbeef57fd7262016-12-06 15:28:55 -0800134 }
135
zhihuangb2cdd932017-01-19 16:54:25 -0800136 DtlsTransportInternal* CreateDtlsTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800137 const std::string& transport_name,
138 int component,
zhihuangb2cdd932017-01-19 16:54:25 -0800139 IceTransportInternal* ice) override {
140 return new FakeDtlsTransport(static_cast<FakeIceTransport*>(ice));
deadbeefcbecd352015-09-23 11:50:27 -0700141 }
142
143 private:
deadbeef49f34fd2016-12-06 16:22:06 -0800144 void SetChannelDestinations_n(FakeTransportController* dest) {
zhihuangb2cdd932017-01-19 16:54:25 -0800145 for (DtlsTransportInternal* tc : channels_for_testing()) {
146 FakeDtlsTransport* local = static_cast<FakeDtlsTransport*>(tc);
147 FakeDtlsTransport* remote = dest->GetFakeDtlsTransport_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800148 local->transport_name(), local->component());
149 if (remote) {
150 bool asymmetric = false;
151 local->SetDestination(remote, asymmetric);
152 }
153 }
154 }
deadbeefcbecd352015-09-23 11:50:27 -0700155};
156
157} // namespace cricket
158
Zhi Huangb5261582017-09-29 10:51:43 -0700159#endif // PC_TEST_FAKETRANSPORTCONTROLLER_H_