deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1 | /* |
| 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 Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 11 | #ifndef PC_TEST_FAKETRANSPORTCONTROLLER_H_ |
| 12 | #define PC_TEST_FAKETRANSPORTCONTROLLER_H_ |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 13 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 14 | #include <memory> |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "p2p/base/fakedtlstransport.h" |
| 19 | #include "p2p/base/fakeicetransport.h" |
Zhi Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 20 | #include "pc/transportcontroller.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/bind.h" |
| 22 | #include "rtc_base/sslfingerprint.h" |
| 23 | #include "rtc_base/thread.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 24 | |
| 25 | namespace cricket { |
| 26 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 27 | // Fake TransportController class, which can be passed into a WebRtcSession |
| 28 | // object for test purposes. Can be connected to other FakeTransportControllers |
| 29 | // via Connect(). |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 30 | // |
| 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. |
| 33 | class FakeTransportController : public TransportController { |
| 34 | public: |
| 35 | FakeTransportController() |
| 36 | : TransportController(rtc::Thread::Current(), |
| 37 | rtc::Thread::Current(), |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 38 | nullptr, |
| 39 | /*redetermine_role_on_ice_restart=*/true, |
| 40 | rtc::CryptoOptions()) {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 41 | |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 42 | explicit FakeTransportController(bool redetermine_role_on_ice_restart) |
| 43 | : TransportController(rtc::Thread::Current(), |
| 44 | rtc::Thread::Current(), |
| 45 | nullptr, |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 46 | redetermine_role_on_ice_restart, |
| 47 | rtc::CryptoOptions()) {} |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 48 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 49 | explicit FakeTransportController(IceRole role) |
| 50 | : TransportController(rtc::Thread::Current(), |
| 51 | rtc::Thread::Current(), |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 52 | nullptr, |
| 53 | /*redetermine_role_on_ice_restart=*/true, |
| 54 | rtc::CryptoOptions()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 55 | SetIceRole(role); |
| 56 | } |
| 57 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 58 | explicit FakeTransportController(rtc::Thread* network_thread) |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 59 | : TransportController(rtc::Thread::Current(), |
| 60 | network_thread, |
| 61 | nullptr, |
| 62 | /*redetermine_role_on_ice_restart=*/true, |
| 63 | rtc::CryptoOptions()) {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 64 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 65 | FakeTransportController(rtc::Thread* network_thread, IceRole role) |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 66 | : TransportController(rtc::Thread::Current(), |
| 67 | network_thread, |
| 68 | nullptr, |
| 69 | /*redetermine_role_on_ice_restart=*/true, |
| 70 | rtc::CryptoOptions()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 71 | SetIceRole(role); |
| 72 | } |
| 73 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 74 | FakeDtlsTransport* GetFakeDtlsTransport_n(const std::string& transport_name, |
| 75 | int component) { |
| 76 | return static_cast<FakeDtlsTransport*>( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 77 | get_channel_for_testing(transport_name, component)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 78 | } |
| 79 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 80 | // Simulate the exchange of transport descriptions, and the gathering and |
| 81 | // exchange of ICE candidates. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 82 | void Connect(FakeTransportController* dest) { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 83 | for (const std::string& transport_name : transport_names_for_testing()) { |
deadbeef | 3e4faae | 2017-01-20 22:43:34 -0800 | [diff] [blame] | 84 | 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 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 94 | TransportDescription local_desc( |
| 95 | std::vector<std::string>(), |
| 96 | rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), |
| 97 | rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 98 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, |
deadbeef | 3e4faae | 2017-01-20 22:43:34 -0800 | [diff] [blame] | 99 | local_fingerprint.get()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 100 | TransportDescription remote_desc( |
| 101 | std::vector<std::string>(), |
| 102 | rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), |
| 103 | rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 104 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, |
deadbeef | 3e4faae | 2017-01-20 22:43:34 -0800 | [diff] [blame] | 105 | remote_fingerprint.get()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 106 | 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 Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 118 | network_thread()->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 119 | RTC_FROM_HERE, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 120 | rtc::Bind(&FakeTransportController::SetChannelDestinations_n, this, |
| 121 | dest)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 122 | } |
| 123 | |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 124 | void DestroyRtcpTransport(const std::string& transport_name) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 125 | DestroyDtlsTransport_n(transport_name, |
| 126 | cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 127 | } |
| 128 | |
deadbeef | 57fd726 | 2016-12-06 15:28:55 -0800 | [diff] [blame] | 129 | protected: |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 130 | IceTransportInternal* CreateIceTransportChannel_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 131 | const std::string& transport_name, |
| 132 | int component) override { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 133 | return new FakeIceTransport(transport_name, component); |
deadbeef | 57fd726 | 2016-12-06 15:28:55 -0800 | [diff] [blame] | 134 | } |
| 135 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 136 | DtlsTransportInternal* CreateDtlsTransportChannel_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 137 | const std::string& transport_name, |
| 138 | int component, |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 139 | IceTransportInternal* ice) override { |
| 140 | return new FakeDtlsTransport(static_cast<FakeIceTransport*>(ice)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private: |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 144 | void SetChannelDestinations_n(FakeTransportController* dest) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 145 | for (DtlsTransportInternal* tc : channels_for_testing()) { |
| 146 | FakeDtlsTransport* local = static_cast<FakeDtlsTransport*>(tc); |
| 147 | FakeDtlsTransport* remote = dest->GetFakeDtlsTransport_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 148 | local->transport_name(), local->component()); |
| 149 | if (remote) { |
| 150 | bool asymmetric = false; |
| 151 | local->SetDestination(remote, asymmetric); |
| 152 | } |
| 153 | } |
| 154 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | } // namespace cricket |
| 158 | |
Zhi Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 159 | #endif // PC_TEST_FAKETRANSPORTCONTROLLER_H_ |