deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #include <map> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 12 | #include <memory> |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "p2p/base/dtlstransport.h" |
| 15 | #include "p2p/base/fakeportallocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "p2p/base/p2ptransportchannel.h" |
| 17 | #include "p2p/base/portallocator.h" |
Zhi Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 18 | #include "pc/test/faketransportcontroller.h" |
| 19 | #include "pc/transportcontroller.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/fakesslidentity.h" |
| 21 | #include "rtc_base/gunit.h" |
| 22 | #include "rtc_base/helpers.h" |
| 23 | #include "rtc_base/sslidentity.h" |
| 24 | #include "rtc_base/thread.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 25 | |
| 26 | static const int kTimeout = 100; |
| 27 | static const char kIceUfrag1[] = "TESTICEUFRAG0001"; |
| 28 | static const char kIcePwd1[] = "TESTICEPWD00000000000001"; |
| 29 | static const char kIceUfrag2[] = "TESTICEUFRAG0002"; |
| 30 | static const char kIcePwd2[] = "TESTICEPWD00000000000002"; |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 31 | static const char kIceUfrag3[] = "TESTICEUFRAG0003"; |
| 32 | static const char kIcePwd3[] = "TESTICEPWD00000000000003"; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 33 | |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 34 | namespace cricket { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 35 | |
| 36 | // Only subclassing from FakeTransportController because currently that's the |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 37 | // only way to have a TransportController with fake ICE/DTLS transports. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 38 | // |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 39 | // TODO(deadbeef): Pass a "TransportFactory" or something similar into |
| 40 | // TransportController, instead of using inheritance in this way for testing. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 41 | typedef FakeTransportController TransportControllerForTest; |
| 42 | |
| 43 | class TransportControllerTest : public testing::Test, |
| 44 | public sigslot::has_slots<> { |
| 45 | public: |
| 46 | TransportControllerTest() |
| 47 | : transport_controller_(new TransportControllerForTest()), |
| 48 | signaling_thread_(rtc::Thread::Current()) { |
| 49 | ConnectTransportControllerSignals(); |
| 50 | } |
| 51 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 52 | void CreateTransportControllerWithNetworkThread() { |
| 53 | if (!network_thread_) { |
| 54 | network_thread_ = rtc::Thread::CreateWithSocketServer(); |
| 55 | network_thread_->Start(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 56 | } |
| 57 | transport_controller_.reset( |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 58 | new TransportControllerForTest(network_thread_.get())); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 59 | ConnectTransportControllerSignals(); |
| 60 | } |
| 61 | |
| 62 | void ConnectTransportControllerSignals() { |
| 63 | transport_controller_->SignalConnectionState.connect( |
| 64 | this, &TransportControllerTest::OnConnectionState); |
| 65 | transport_controller_->SignalReceiving.connect( |
| 66 | this, &TransportControllerTest::OnReceiving); |
| 67 | transport_controller_->SignalGatheringState.connect( |
| 68 | this, &TransportControllerTest::OnGatheringState); |
| 69 | transport_controller_->SignalCandidatesGathered.connect( |
| 70 | this, &TransportControllerTest::OnCandidatesGathered); |
| 71 | } |
| 72 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 73 | FakeDtlsTransport* CreateFakeDtlsTransport(const std::string& content, |
| 74 | int component) { |
| 75 | DtlsTransportInternal* transport = |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 76 | transport_controller_->CreateDtlsTransport_n(content, component); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 77 | return static_cast<FakeDtlsTransport*>(transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 78 | } |
| 79 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 80 | void DestroyFakeDtlsTransport(const std::string& content, int component) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 81 | transport_controller_->DestroyDtlsTransport_n(content, component); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Candidate CreateCandidate(int component) { |
| 85 | Candidate c; |
| 86 | c.set_address(rtc::SocketAddress("192.168.1.1", 8000)); |
| 87 | c.set_component(1); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 88 | c.set_protocol(UDP_PROTOCOL_NAME); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 89 | c.set_priority(1); |
| 90 | return c; |
| 91 | } |
| 92 | |
| 93 | // Used for thread hopping test. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 94 | void CreateFakeDtlsTransportsAndCompleteConnectionOnNetworkThread() { |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 95 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 96 | RTC_FROM_HERE, |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 97 | rtc::Bind(&TransportControllerTest:: |
| 98 | CreateFakeDtlsTransportsAndCompleteConnection_w, |
| 99 | this)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 100 | } |
| 101 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 102 | void CreateFakeDtlsTransportsAndCompleteConnection_w() { |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 103 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 104 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 105 | ASSERT_NE(nullptr, transport1); |
| 106 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 107 | ASSERT_NE(nullptr, transport2); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 108 | |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 109 | TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1, |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 110 | kIcePwd1, ICEMODE_FULL, |
| 111 | CONNECTIONROLE_ACTPASS, nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 112 | std::string err; |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 113 | transport_controller_->SetLocalTransportDescription("audio", local_desc, |
| 114 | CA_OFFER, &err); |
| 115 | transport_controller_->SetLocalTransportDescription("video", local_desc, |
| 116 | CA_OFFER, &err); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 117 | transport_controller_->MaybeStartGathering(); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 118 | transport1->fake_ice_transport()->SignalCandidateGathered( |
| 119 | transport1->fake_ice_transport(), CreateCandidate(1)); |
| 120 | transport2->fake_ice_transport()->SignalCandidateGathered( |
| 121 | transport2->fake_ice_transport(), CreateCandidate(1)); |
| 122 | transport1->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 123 | transport2->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 124 | transport1->fake_ice_transport()->SetConnectionCount(2); |
| 125 | transport2->fake_ice_transport()->SetConnectionCount(2); |
| 126 | transport1->SetReceiving(true); |
| 127 | transport2->SetReceiving(true); |
| 128 | transport1->SetWritable(true); |
| 129 | transport2->SetWritable(true); |
| 130 | transport1->fake_ice_transport()->SetConnectionCount(1); |
| 131 | transport2->fake_ice_transport()->SetConnectionCount(1); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 132 | } |
| 133 | |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 134 | IceConfig CreateIceConfig( |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 135 | int receiving_timeout, |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 136 | ContinualGatheringPolicy continual_gathering_policy) { |
| 137 | IceConfig config; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 138 | config.receiving_timeout = receiving_timeout; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 139 | config.continual_gathering_policy = continual_gathering_policy; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 140 | return config; |
| 141 | } |
| 142 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 143 | protected: |
| 144 | void OnConnectionState(IceConnectionState state) { |
| 145 | if (!signaling_thread_->IsCurrent()) { |
| 146 | signaled_on_non_signaling_thread_ = true; |
| 147 | } |
| 148 | connection_state_ = state; |
| 149 | ++connection_state_signal_count_; |
| 150 | } |
| 151 | |
| 152 | void OnReceiving(bool receiving) { |
| 153 | if (!signaling_thread_->IsCurrent()) { |
| 154 | signaled_on_non_signaling_thread_ = true; |
| 155 | } |
| 156 | receiving_ = receiving; |
| 157 | ++receiving_signal_count_; |
| 158 | } |
| 159 | |
| 160 | void OnGatheringState(IceGatheringState state) { |
| 161 | if (!signaling_thread_->IsCurrent()) { |
| 162 | signaled_on_non_signaling_thread_ = true; |
| 163 | } |
| 164 | gathering_state_ = state; |
| 165 | ++gathering_state_signal_count_; |
| 166 | } |
| 167 | |
| 168 | void OnCandidatesGathered(const std::string& transport_name, |
| 169 | const Candidates& candidates) { |
| 170 | if (!signaling_thread_->IsCurrent()) { |
| 171 | signaled_on_non_signaling_thread_ = true; |
| 172 | } |
| 173 | candidates_[transport_name].insert(candidates_[transport_name].end(), |
| 174 | candidates.begin(), candidates.end()); |
| 175 | ++candidates_signal_count_; |
| 176 | } |
| 177 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 178 | std::unique_ptr<rtc::Thread> network_thread_; // Not used for most tests. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 179 | std::unique_ptr<TransportControllerForTest> transport_controller_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 180 | |
| 181 | // Information received from signals from transport controller. |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 182 | IceConnectionState connection_state_ = kIceConnectionConnecting; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 183 | bool receiving_ = false; |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 184 | IceGatheringState gathering_state_ = kIceGatheringNew; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 185 | // transport_name => candidates |
| 186 | std::map<std::string, Candidates> candidates_; |
| 187 | // Counts of each signal emitted. |
| 188 | int connection_state_signal_count_ = 0; |
| 189 | int receiving_signal_count_ = 0; |
| 190 | int gathering_state_signal_count_ = 0; |
| 191 | int candidates_signal_count_ = 0; |
| 192 | |
| 193 | // Used to make sure signals only come on signaling thread. |
| 194 | rtc::Thread* const signaling_thread_ = nullptr; |
| 195 | bool signaled_on_non_signaling_thread_ = false; |
| 196 | }; |
| 197 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 198 | TEST_F(TransportControllerTest, TestSetIceConfig) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 199 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 200 | ASSERT_NE(nullptr, transport1); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 201 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 202 | transport_controller_->SetIceConfig( |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 203 | CreateIceConfig(1000, GATHER_CONTINUALLY)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 204 | EXPECT_EQ(1000, transport1->fake_ice_transport()->receiving_timeout()); |
| 205 | EXPECT_TRUE(transport1->fake_ice_transport()->gather_continually()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 206 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 207 | transport_controller_->SetIceConfig( |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 208 | CreateIceConfig(1000, GATHER_CONTINUALLY_AND_RECOVER)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 209 | // Test that value stored in controller is applied to new transports. |
| 210 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 211 | ASSERT_NE(nullptr, transport2); |
| 212 | EXPECT_EQ(1000, transport2->fake_ice_transport()->receiving_timeout()); |
| 213 | EXPECT_TRUE(transport2->fake_ice_transport()->gather_continually()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TEST_F(TransportControllerTest, TestSetSslMaxProtocolVersion) { |
| 217 | EXPECT_TRUE(transport_controller_->SetSslMaxProtocolVersion( |
| 218 | rtc::SSL_PROTOCOL_DTLS_12)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 219 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 220 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 221 | ASSERT_NE(nullptr, transport); |
| 222 | EXPECT_EQ(rtc::SSL_PROTOCOL_DTLS_12, transport->ssl_max_protocol_version()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 223 | |
| 224 | // Setting max version after transport is created should fail. |
| 225 | EXPECT_FALSE(transport_controller_->SetSslMaxProtocolVersion( |
| 226 | rtc::SSL_PROTOCOL_DTLS_10)); |
| 227 | } |
| 228 | |
| 229 | TEST_F(TransportControllerTest, TestSetIceRole) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 230 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 231 | ASSERT_NE(nullptr, transport1); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 232 | |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 233 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 234 | EXPECT_EQ(ICEROLE_CONTROLLING, |
| 235 | transport1->fake_ice_transport()->GetIceRole()); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 236 | transport_controller_->SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 237 | EXPECT_EQ(ICEROLE_CONTROLLED, transport1->fake_ice_transport()->GetIceRole()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 238 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 239 | // Test that value stored in controller is applied to new transports. |
| 240 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 241 | ASSERT_NE(nullptr, transport2); |
| 242 | EXPECT_EQ(ICEROLE_CONTROLLED, transport2->fake_ice_transport()->GetIceRole()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 243 | } |
| 244 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 245 | // Test that when one transport encounters a role conflict, the ICE role is |
| 246 | // swapped on every transport. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 247 | TEST_F(TransportControllerTest, TestIceRoleConflict) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 248 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 249 | ASSERT_NE(nullptr, transport1); |
| 250 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 251 | ASSERT_NE(nullptr, transport2); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 252 | |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 253 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 254 | EXPECT_EQ(ICEROLE_CONTROLLING, |
| 255 | transport1->fake_ice_transport()->GetIceRole()); |
| 256 | EXPECT_EQ(ICEROLE_CONTROLLING, |
| 257 | transport2->fake_ice_transport()->GetIceRole()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 258 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 259 | transport1->fake_ice_transport()->SignalRoleConflict( |
| 260 | transport1->fake_ice_transport()); |
| 261 | EXPECT_EQ(ICEROLE_CONTROLLED, transport1->fake_ice_transport()->GetIceRole()); |
| 262 | EXPECT_EQ(ICEROLE_CONTROLLED, transport2->fake_ice_transport()->GetIceRole()); |
deadbeef | 1c20610 | 2016-05-27 13:34:37 -0700 | [diff] [blame] | 263 | |
| 264 | // Should be able to handle a second role conflict. The remote endpoint can |
| 265 | // change its role/tie-breaker when it does an ICE restart. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 266 | transport2->fake_ice_transport()->SignalRoleConflict( |
| 267 | transport2->fake_ice_transport()); |
| 268 | EXPECT_EQ(ICEROLE_CONTROLLING, |
| 269 | transport1->fake_ice_transport()->GetIceRole()); |
| 270 | EXPECT_EQ(ICEROLE_CONTROLLING, |
| 271 | transport2->fake_ice_transport()->GetIceRole()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | TEST_F(TransportControllerTest, TestGetSslRole) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 275 | rtc::SSLRole role; |
deadbeef | d8cfa1a | 2017-03-27 10:33:26 -0700 | [diff] [blame] | 276 | CreateFakeDtlsTransport("audio", 1); |
| 277 | |
| 278 | // Should return false before role has been negotiated. |
| 279 | EXPECT_FALSE(transport_controller_->GetSslRole("audio", &role)); |
| 280 | |
| 281 | // To negotiate an SSL role, need to set a local certificate, and |
| 282 | // local/remote transport descriptions with DTLS info. |
| 283 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
| 284 | rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
| 285 | rtc::SSLIdentity::Generate("testing", rtc::KT_ECDSA))); |
| 286 | std::unique_ptr<rtc::SSLFingerprint> fingerprint( |
| 287 | rtc::SSLFingerprint::CreateFromCertificate(certificate)); |
| 288 | transport_controller_->SetLocalCertificate(certificate); |
| 289 | |
| 290 | // Set the same fingerprint on both sides since the remote fingerprint |
| 291 | // doesn't really matter for this test. |
| 292 | TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1, |
| 293 | kIcePwd1, ICEMODE_FULL, |
| 294 | CONNECTIONROLE_ACTPASS, fingerprint.get()); |
| 295 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag2, |
| 296 | kIcePwd2, ICEMODE_FULL, |
| 297 | CONNECTIONROLE_ACTIVE, fingerprint.get()); |
| 298 | std::string err; |
| 299 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 300 | "audio", local_desc, cricket::CA_OFFER, &err)); |
| 301 | EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 302 | "audio", remote_desc, cricket::CA_ANSWER, &err)); |
| 303 | |
| 304 | // Finally we can get the role. Should be "server" since the remote |
| 305 | // endpoint's role was "active". |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 306 | EXPECT_TRUE(transport_controller_->GetSslRole("audio", &role)); |
deadbeef | d8cfa1a | 2017-03-27 10:33:26 -0700 | [diff] [blame] | 307 | EXPECT_EQ(rtc::SSL_SERVER, role); |
| 308 | |
| 309 | // Lastly, test that GetSslRole returns false for a nonexistent transport. |
| 310 | EXPECT_FALSE(transport_controller_->GetSslRole("video", &role)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | TEST_F(TransportControllerTest, TestSetAndGetLocalCertificate) { |
| 314 | rtc::scoped_refptr<rtc::RTCCertificate> certificate1 = |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 315 | rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 316 | rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT))); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 317 | rtc::scoped_refptr<rtc::RTCCertificate> certificate2 = |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 318 | rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 319 | rtc::SSLIdentity::Generate("session2", rtc::KT_DEFAULT))); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 320 | rtc::scoped_refptr<rtc::RTCCertificate> returned_certificate; |
| 321 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 322 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 323 | ASSERT_NE(nullptr, transport1); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 324 | |
| 325 | EXPECT_TRUE(transport_controller_->SetLocalCertificate(certificate1)); |
| 326 | EXPECT_TRUE(transport_controller_->GetLocalCertificate( |
| 327 | "audio", &returned_certificate)); |
| 328 | EXPECT_EQ(certificate1->identity()->certificate().ToPEMString(), |
| 329 | returned_certificate->identity()->certificate().ToPEMString()); |
| 330 | |
| 331 | // Should fail if called for a nonexistant transport. |
| 332 | EXPECT_FALSE(transport_controller_->GetLocalCertificate( |
| 333 | "video", &returned_certificate)); |
| 334 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 335 | // Test that identity stored in controller is applied to new transports. |
| 336 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 337 | ASSERT_NE(nullptr, transport2); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 338 | EXPECT_TRUE(transport_controller_->GetLocalCertificate( |
| 339 | "video", &returned_certificate)); |
| 340 | EXPECT_EQ(certificate1->identity()->certificate().ToPEMString(), |
| 341 | returned_certificate->identity()->certificate().ToPEMString()); |
| 342 | |
| 343 | // Shouldn't be able to change the identity once set. |
| 344 | EXPECT_FALSE(transport_controller_->SetLocalCertificate(certificate2)); |
| 345 | } |
| 346 | |
| 347 | TEST_F(TransportControllerTest, TestGetRemoteSSLCertificate) { |
| 348 | rtc::FakeSSLCertificate fake_certificate("fake_data"); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 349 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 350 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 351 | ASSERT_NE(nullptr, transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 352 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 353 | transport->SetRemoteSSLCertificate(&fake_certificate); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 354 | std::unique_ptr<rtc::SSLCertificate> returned_certificate = |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 355 | transport_controller_->GetRemoteSSLCertificate("audio"); |
| 356 | EXPECT_TRUE(returned_certificate); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 357 | EXPECT_EQ(fake_certificate.ToPEMString(), |
| 358 | returned_certificate->ToPEMString()); |
| 359 | |
| 360 | // Should fail if called for a nonexistant transport. |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 361 | EXPECT_FALSE(transport_controller_->GetRemoteSSLCertificate("video")); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST_F(TransportControllerTest, TestSetLocalTransportDescription) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 365 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 366 | ASSERT_NE(nullptr, transport); |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 367 | TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1, |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 368 | kIcePwd1, ICEMODE_FULL, |
| 369 | CONNECTIONROLE_ACTPASS, nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 370 | std::string err; |
| 371 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 372 | "audio", local_desc, CA_OFFER, &err)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 373 | // Check that ICE ufrag and pwd were propagated to transport. |
| 374 | EXPECT_EQ(kIceUfrag1, transport->fake_ice_transport()->ice_ufrag()); |
| 375 | EXPECT_EQ(kIcePwd1, transport->fake_ice_transport()->ice_pwd()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 376 | // After setting local description, we should be able to start gathering |
| 377 | // candidates. |
| 378 | transport_controller_->MaybeStartGathering(); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 379 | EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 380 | EXPECT_EQ(1, gathering_state_signal_count_); |
| 381 | } |
| 382 | |
| 383 | TEST_F(TransportControllerTest, TestSetRemoteTransportDescription) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 384 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 385 | ASSERT_NE(nullptr, transport); |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 386 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1, |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 387 | kIcePwd1, ICEMODE_FULL, |
| 388 | CONNECTIONROLE_ACTPASS, nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 389 | std::string err; |
| 390 | EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription( |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 391 | "audio", remote_desc, CA_OFFER, &err)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 392 | // Check that ICE ufrag and pwd were propagated to transport. |
| 393 | EXPECT_EQ(kIceUfrag1, transport->fake_ice_transport()->remote_ice_ufrag()); |
| 394 | EXPECT_EQ(kIcePwd1, transport->fake_ice_transport()->remote_ice_pwd()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | TEST_F(TransportControllerTest, TestAddRemoteCandidates) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 398 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 399 | ASSERT_NE(nullptr, transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 400 | Candidates candidates; |
| 401 | candidates.push_back(CreateCandidate(1)); |
| 402 | std::string err; |
| 403 | EXPECT_TRUE( |
| 404 | transport_controller_->AddRemoteCandidates("audio", candidates, &err)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 405 | EXPECT_EQ(1U, transport->fake_ice_transport()->remote_candidates().size()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TEST_F(TransportControllerTest, TestReadyForRemoteCandidates) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 409 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 410 | ASSERT_NE(nullptr, transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 411 | // We expect to be ready for remote candidates only after local and remote |
| 412 | // descriptions are set. |
| 413 | EXPECT_FALSE(transport_controller_->ReadyForRemoteCandidates("audio")); |
| 414 | |
| 415 | std::string err; |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 416 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1, |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 417 | kIcePwd1, ICEMODE_FULL, |
| 418 | CONNECTIONROLE_ACTPASS, nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 419 | EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription( |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 420 | "audio", remote_desc, CA_OFFER, &err)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 421 | EXPECT_FALSE(transport_controller_->ReadyForRemoteCandidates("audio")); |
| 422 | |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 423 | TransportDescription local_desc(std::vector<std::string>(), kIceUfrag2, |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 424 | kIcePwd2, ICEMODE_FULL, |
| 425 | CONNECTIONROLE_ACTPASS, nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 426 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 427 | "audio", local_desc, CA_ANSWER, &err)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 428 | EXPECT_TRUE(transport_controller_->ReadyForRemoteCandidates("audio")); |
| 429 | } |
| 430 | |
| 431 | TEST_F(TransportControllerTest, TestGetStats) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 432 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 433 | ASSERT_NE(nullptr, transport1); |
| 434 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("audio", 2); |
| 435 | ASSERT_NE(nullptr, transport2); |
| 436 | FakeDtlsTransport* transport3 = CreateFakeDtlsTransport("video", 1); |
| 437 | ASSERT_NE(nullptr, transport3); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 438 | |
| 439 | TransportStats stats; |
| 440 | EXPECT_TRUE(transport_controller_->GetStats("audio", &stats)); |
| 441 | EXPECT_EQ("audio", stats.transport_name); |
| 442 | EXPECT_EQ(2U, stats.channel_stats.size()); |
| 443 | } |
| 444 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 445 | // Test that a "transport" from a stats perspective (combination of RTP/RTCP |
| 446 | // transports) goes away when all references to its transports are gone. |
| 447 | TEST_F(TransportControllerTest, TestCreateAndDestroyFakeDtlsTransport) { |
| 448 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 449 | ASSERT_NE(nullptr, transport1); |
| 450 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("audio", 1); |
| 451 | ASSERT_NE(nullptr, transport2); |
| 452 | ASSERT_EQ(transport1, transport2); |
| 453 | FakeDtlsTransport* transport3 = CreateFakeDtlsTransport("audio", 2); |
| 454 | ASSERT_NE(nullptr, transport3); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 455 | |
| 456 | // Using GetStats to check if transport is destroyed from an outside class's |
| 457 | // perspective. |
| 458 | TransportStats stats; |
| 459 | EXPECT_TRUE(transport_controller_->GetStats("audio", &stats)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 460 | DestroyFakeDtlsTransport("audio", 2); |
| 461 | DestroyFakeDtlsTransport("audio", 1); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 462 | EXPECT_TRUE(transport_controller_->GetStats("audio", &stats)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 463 | DestroyFakeDtlsTransport("audio", 1); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 464 | EXPECT_FALSE(transport_controller_->GetStats("audio", &stats)); |
| 465 | } |
| 466 | |
| 467 | TEST_F(TransportControllerTest, TestSignalConnectionStateFailed) { |
| 468 | // Need controlling ICE role to get in failed state. |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 469 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 470 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 471 | ASSERT_NE(nullptr, transport1); |
| 472 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 473 | ASSERT_NE(nullptr, transport2); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 474 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 475 | // Should signal "failed" if any transport failed; transport is considered |
| 476 | // failed |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 477 | // if it previously had a connection but now has none, and gathering is |
| 478 | // complete. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 479 | transport1->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 480 | transport1->fake_ice_transport()->SetConnectionCount(1); |
| 481 | transport1->fake_ice_transport()->SetConnectionCount(0); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 482 | EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 483 | EXPECT_EQ(1, connection_state_signal_count_); |
| 484 | } |
| 485 | |
| 486 | TEST_F(TransportControllerTest, TestSignalConnectionStateConnected) { |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 487 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 488 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 489 | ASSERT_NE(nullptr, transport1); |
| 490 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 491 | ASSERT_NE(nullptr, transport2); |
| 492 | FakeDtlsTransport* transport3 = CreateFakeDtlsTransport("video", 2); |
| 493 | ASSERT_NE(nullptr, transport3); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 494 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 495 | // First, have one transport connect, and another fail, to ensure that |
| 496 | // the first transport connecting didn't trigger a "connected" state signal. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 497 | // We should only get a signal when all are connected. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 498 | transport1->fake_ice_transport()->SetConnectionCount(2); |
| 499 | transport1->SetWritable(true); |
| 500 | transport3->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 501 | transport3->fake_ice_transport()->SetConnectionCount(1); |
| 502 | transport3->fake_ice_transport()->SetConnectionCount(0); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 503 | EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 504 | // Signal count of 1 means that the only signal emitted was "failed". |
| 505 | EXPECT_EQ(1, connection_state_signal_count_); |
| 506 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 507 | // Destroy the failed transport to return to "connecting" state. |
| 508 | DestroyFakeDtlsTransport("video", 2); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 509 | EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 510 | EXPECT_EQ(2, connection_state_signal_count_); |
| 511 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 512 | // Make the remaining transport reach a connected state. |
| 513 | transport2->fake_ice_transport()->SetConnectionCount(2); |
| 514 | transport2->SetWritable(true); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 515 | EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 516 | EXPECT_EQ(3, connection_state_signal_count_); |
| 517 | } |
| 518 | |
| 519 | TEST_F(TransportControllerTest, TestSignalConnectionStateComplete) { |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 520 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 521 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 522 | ASSERT_NE(nullptr, transport1); |
| 523 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 524 | ASSERT_NE(nullptr, transport2); |
| 525 | FakeDtlsTransport* transport3 = CreateFakeDtlsTransport("video", 2); |
| 526 | ASSERT_NE(nullptr, transport3); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 527 | |
| 528 | // Similar to above test, but we're now reaching the completed state, which |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 529 | // means only one connection per FakeDtlsTransport. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 530 | transport1->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 531 | transport1->fake_ice_transport()->SetConnectionCount(1); |
| 532 | transport1->SetWritable(true); |
| 533 | transport3->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 534 | transport3->fake_ice_transport()->SetConnectionCount(1); |
| 535 | transport3->fake_ice_transport()->SetConnectionCount(0); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 536 | EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 537 | // Signal count of 1 means that the only signal emitted was "failed". |
| 538 | EXPECT_EQ(1, connection_state_signal_count_); |
| 539 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 540 | // Destroy the failed transport to return to "connecting" state. |
| 541 | DestroyFakeDtlsTransport("video", 2); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 542 | EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 543 | EXPECT_EQ(2, connection_state_signal_count_); |
| 544 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 545 | // Make the remaining transport reach a connected state. |
| 546 | transport2->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 547 | transport2->fake_ice_transport()->SetConnectionCount(2); |
| 548 | transport2->SetWritable(true); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 549 | EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 550 | EXPECT_EQ(3, connection_state_signal_count_); |
| 551 | |
| 552 | // Finally, transition to completed state. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 553 | transport2->fake_ice_transport()->SetConnectionCount(1); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 554 | EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 555 | EXPECT_EQ(4, connection_state_signal_count_); |
| 556 | } |
| 557 | |
| 558 | // Make sure that if we're "connected" and remove a transport, we stay in the |
| 559 | // "connected" state. |
| 560 | TEST_F(TransportControllerTest, TestDestroyTransportAndStayConnected) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 561 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 562 | ASSERT_NE(nullptr, transport1); |
| 563 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 564 | ASSERT_NE(nullptr, transport2); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 565 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 566 | transport1->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 567 | transport1->fake_ice_transport()->SetConnectionCount(2); |
| 568 | transport1->SetWritable(true); |
| 569 | transport2->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 570 | transport2->fake_ice_transport()->SetConnectionCount(2); |
| 571 | transport2->SetWritable(true); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 572 | EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 573 | EXPECT_EQ(1, connection_state_signal_count_); |
| 574 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 575 | // Destroy one transport, then "complete" the other one, so we reach |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 576 | // a known state. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 577 | DestroyFakeDtlsTransport("video", 1); |
| 578 | transport1->fake_ice_transport()->SetConnectionCount(1); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 579 | EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 580 | // Signal count of 2 means the deletion didn't cause any unexpected signals |
| 581 | EXPECT_EQ(2, connection_state_signal_count_); |
| 582 | } |
| 583 | |
| 584 | // If we destroy the last/only transport, we should simply transition to |
| 585 | // "connecting". |
| 586 | TEST_F(TransportControllerTest, TestDestroyLastTransportWhileConnected) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 587 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 588 | ASSERT_NE(nullptr, transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 589 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 590 | transport->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 591 | transport->fake_ice_transport()->SetConnectionCount(2); |
| 592 | transport->SetWritable(true); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 593 | EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 594 | EXPECT_EQ(1, connection_state_signal_count_); |
| 595 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 596 | DestroyFakeDtlsTransport("audio", 1); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 597 | EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 598 | // Signal count of 2 means the deletion didn't cause any unexpected signals |
| 599 | EXPECT_EQ(2, connection_state_signal_count_); |
| 600 | } |
| 601 | |
| 602 | TEST_F(TransportControllerTest, TestSignalReceiving) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 603 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 604 | ASSERT_NE(nullptr, transport1); |
| 605 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 606 | ASSERT_NE(nullptr, transport2); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 607 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 608 | // Should signal receiving as soon as any transport is receiving. |
| 609 | transport1->SetReceiving(true); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 610 | EXPECT_TRUE_WAIT(receiving_, kTimeout); |
| 611 | EXPECT_EQ(1, receiving_signal_count_); |
| 612 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 613 | transport2->SetReceiving(true); |
| 614 | transport1->SetReceiving(false); |
| 615 | transport2->SetReceiving(false); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 616 | EXPECT_TRUE_WAIT(!receiving_, kTimeout); |
| 617 | EXPECT_EQ(2, receiving_signal_count_); |
| 618 | } |
| 619 | |
| 620 | TEST_F(TransportControllerTest, TestSignalGatheringStateGathering) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 621 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 622 | ASSERT_NE(nullptr, transport); |
| 623 | transport->fake_ice_transport()->MaybeStartGathering(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 624 | // Should be in the gathering state as soon as any transport starts gathering. |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 625 | EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 626 | EXPECT_EQ(1, gathering_state_signal_count_); |
| 627 | } |
| 628 | |
| 629 | TEST_F(TransportControllerTest, TestSignalGatheringStateComplete) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 630 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 631 | ASSERT_NE(nullptr, transport1); |
| 632 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 633 | ASSERT_NE(nullptr, transport2); |
| 634 | FakeDtlsTransport* transport3 = CreateFakeDtlsTransport("data", 1); |
| 635 | ASSERT_NE(nullptr, transport3); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 636 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 637 | transport3->fake_ice_transport()->MaybeStartGathering(); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 638 | EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 639 | EXPECT_EQ(1, gathering_state_signal_count_); |
| 640 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 641 | // Have one transport finish gathering, then destroy it, to make sure |
| 642 | // gathering |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 643 | // completion wasn't signalled if only one transport finished gathering. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 644 | transport3->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 645 | DestroyFakeDtlsTransport("data", 1); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 646 | EXPECT_EQ_WAIT(kIceGatheringNew, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 647 | EXPECT_EQ(2, gathering_state_signal_count_); |
| 648 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 649 | // Make remaining transports start and then finish gathering. |
| 650 | transport1->fake_ice_transport()->MaybeStartGathering(); |
| 651 | transport2->fake_ice_transport()->MaybeStartGathering(); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 652 | EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 653 | EXPECT_EQ(3, gathering_state_signal_count_); |
| 654 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 655 | transport1->fake_ice_transport()->SetCandidatesGatheringComplete(); |
| 656 | transport2->fake_ice_transport()->SetCandidatesGatheringComplete(); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 657 | EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 658 | EXPECT_EQ(4, gathering_state_signal_count_); |
| 659 | } |
| 660 | |
| 661 | // Test that when the last transport that hasn't finished connecting and/or |
| 662 | // gathering is destroyed, the aggregate state jumps to "completed". This can |
| 663 | // happen if, for example, we have an audio and video transport, the audio |
| 664 | // transport completes, then we start bundling video on the audio transport. |
| 665 | TEST_F(TransportControllerTest, |
| 666 | TestSignalingWhenLastIncompleteTransportDestroyed) { |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 667 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 668 | FakeDtlsTransport* transport1 = CreateFakeDtlsTransport("audio", 1); |
| 669 | ASSERT_NE(nullptr, transport1); |
| 670 | FakeDtlsTransport* transport2 = CreateFakeDtlsTransport("video", 1); |
| 671 | ASSERT_NE(nullptr, transport2); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 672 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 673 | transport1->fake_ice_transport()->SetCandidatesGatheringComplete(); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 674 | EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 675 | EXPECT_EQ(1, gathering_state_signal_count_); |
| 676 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 677 | transport1->fake_ice_transport()->SetConnectionCount(1); |
| 678 | transport1->SetWritable(true); |
| 679 | DestroyFakeDtlsTransport("video", 1); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 680 | EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 681 | EXPECT_EQ(1, connection_state_signal_count_); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 682 | EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 683 | EXPECT_EQ(2, gathering_state_signal_count_); |
| 684 | } |
| 685 | |
| 686 | TEST_F(TransportControllerTest, TestSignalCandidatesGathered) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 687 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 688 | ASSERT_NE(nullptr, transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 689 | |
| 690 | // Transport won't signal candidates until it has a local description. |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 691 | TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1, |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 692 | kIcePwd1, ICEMODE_FULL, |
| 693 | CONNECTIONROLE_ACTPASS, nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 694 | std::string err; |
| 695 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 696 | "audio", local_desc, CA_OFFER, &err)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 697 | transport_controller_->MaybeStartGathering(); |
| 698 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 699 | transport->fake_ice_transport()->SignalCandidateGathered( |
| 700 | transport->fake_ice_transport(), CreateCandidate(1)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 701 | EXPECT_EQ_WAIT(1, candidates_signal_count_, kTimeout); |
| 702 | EXPECT_EQ(1U, candidates_["audio"].size()); |
| 703 | } |
| 704 | |
| 705 | TEST_F(TransportControllerTest, TestSignalingOccursOnSignalingThread) { |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 706 | CreateTransportControllerWithNetworkThread(); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 707 | CreateFakeDtlsTransportsAndCompleteConnectionOnNetworkThread(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 708 | |
| 709 | // connecting --> connected --> completed |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 710 | EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 711 | EXPECT_EQ(2, connection_state_signal_count_); |
| 712 | |
| 713 | EXPECT_TRUE_WAIT(receiving_, kTimeout); |
| 714 | EXPECT_EQ(1, receiving_signal_count_); |
| 715 | |
| 716 | // new --> gathering --> complete |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 717 | EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 718 | EXPECT_EQ(2, gathering_state_signal_count_); |
| 719 | |
| 720 | EXPECT_EQ_WAIT(1U, candidates_["audio"].size(), kTimeout); |
| 721 | EXPECT_EQ_WAIT(1U, candidates_["video"].size(), kTimeout); |
| 722 | EXPECT_EQ(2, candidates_signal_count_); |
| 723 | |
| 724 | EXPECT_TRUE(!signaled_on_non_signaling_thread_); |
| 725 | } |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 726 | |
| 727 | // Older versions of Chrome expect the ICE role to be re-determined when an |
| 728 | // ICE restart occurs, and also don't perform conflict resolution correctly, |
| 729 | // so for now we can't safely stop doing this. |
| 730 | // See: https://bugs.chromium.org/p/chromium/issues/detail?id=628676 |
| 731 | // TODO(deadbeef): Remove this when these old versions of Chrome reach a low |
| 732 | // enough population. |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 733 | TEST_F(TransportControllerTest, IceRoleRedeterminedOnIceRestartByDefault) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 734 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 735 | ASSERT_NE(nullptr, transport); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 736 | std::string err; |
| 737 | // Do an initial offer answer, so that the next offer is an ICE restart. |
| 738 | transport_controller_->SetIceRole(ICEROLE_CONTROLLED); |
| 739 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1, |
| 740 | kIcePwd1, ICEMODE_FULL, |
| 741 | CONNECTIONROLE_ACTPASS, nullptr); |
| 742 | EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 743 | "audio", remote_desc, CA_OFFER, &err)); |
| 744 | TransportDescription local_desc(std::vector<std::string>(), kIceUfrag2, |
| 745 | kIcePwd2, ICEMODE_FULL, |
| 746 | CONNECTIONROLE_ACTPASS, nullptr); |
| 747 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 748 | "audio", local_desc, CA_ANSWER, &err)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 749 | EXPECT_EQ(ICEROLE_CONTROLLED, transport->fake_ice_transport()->GetIceRole()); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 750 | |
| 751 | // The endpoint that initiated an ICE restart should take the controlling |
| 752 | // role. |
| 753 | TransportDescription ice_restart_desc(std::vector<std::string>(), kIceUfrag3, |
| 754 | kIcePwd3, ICEMODE_FULL, |
| 755 | CONNECTIONROLE_ACTPASS, nullptr); |
| 756 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 757 | "audio", ice_restart_desc, CA_OFFER, &err)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 758 | EXPECT_EQ(ICEROLE_CONTROLLING, transport->fake_ice_transport()->GetIceRole()); |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 761 | // Test that if the TransportController was created with the |
| 762 | // |redetermine_role_on_ice_restart| parameter set to false, the role is *not* |
| 763 | // redetermined on an ICE restart. |
| 764 | TEST_F(TransportControllerTest, IceRoleNotRedetermined) { |
| 765 | bool redetermine_role = false; |
| 766 | transport_controller_.reset(new TransportControllerForTest(redetermine_role)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 767 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 768 | ASSERT_NE(nullptr, transport); |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 769 | std::string err; |
| 770 | // Do an initial offer answer, so that the next offer is an ICE restart. |
| 771 | transport_controller_->SetIceRole(ICEROLE_CONTROLLED); |
| 772 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1, |
| 773 | kIcePwd1, ICEMODE_FULL, |
| 774 | CONNECTIONROLE_ACTPASS, nullptr); |
| 775 | EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 776 | "audio", remote_desc, CA_OFFER, &err)); |
| 777 | TransportDescription local_desc(std::vector<std::string>(), kIceUfrag2, |
| 778 | kIcePwd2, ICEMODE_FULL, |
| 779 | CONNECTIONROLE_ACTPASS, nullptr); |
| 780 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 781 | "audio", local_desc, CA_ANSWER, &err)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 782 | EXPECT_EQ(ICEROLE_CONTROLLED, transport->fake_ice_transport()->GetIceRole()); |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 783 | |
| 784 | // The endpoint that initiated an ICE restart should keep the existing role. |
| 785 | TransportDescription ice_restart_desc(std::vector<std::string>(), kIceUfrag3, |
| 786 | kIcePwd3, ICEMODE_FULL, |
| 787 | CONNECTIONROLE_ACTPASS, nullptr); |
| 788 | EXPECT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 789 | "audio", ice_restart_desc, CA_OFFER, &err)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 790 | EXPECT_EQ(ICEROLE_CONTROLLED, transport->fake_ice_transport()->GetIceRole()); |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 791 | } |
| 792 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 793 | // Tests ICE role is reversed after receiving ice-lite from remote. |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 794 | TEST_F(TransportControllerTest, TestSetRemoteIceLiteInOffer) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 795 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 796 | ASSERT_NE(nullptr, transport); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 797 | std::string err; |
| 798 | |
| 799 | transport_controller_->SetIceRole(ICEROLE_CONTROLLED); |
| 800 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1, |
| 801 | kIcePwd1, ICEMODE_LITE, |
| 802 | CONNECTIONROLE_ACTPASS, nullptr); |
| 803 | EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 804 | "audio", remote_desc, CA_OFFER, &err)); |
| 805 | TransportDescription local_desc(kIceUfrag1, kIcePwd1); |
| 806 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 807 | "audio", local_desc, CA_ANSWER, nullptr)); |
| 808 | |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 809 | EXPECT_EQ(ICEROLE_CONTROLLING, transport->fake_ice_transport()->GetIceRole()); |
| 810 | EXPECT_EQ(ICEMODE_LITE, transport->fake_ice_transport()->remote_ice_mode()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | // Tests ice-lite in remote answer. |
| 814 | TEST_F(TransportControllerTest, TestSetRemoteIceLiteInAnswer) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 815 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 816 | ASSERT_NE(nullptr, transport); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 817 | std::string err; |
| 818 | |
| 819 | transport_controller_->SetIceRole(ICEROLE_CONTROLLING); |
| 820 | TransportDescription local_desc(kIceUfrag1, kIcePwd1); |
| 821 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 822 | "audio", local_desc, CA_OFFER, nullptr)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 823 | EXPECT_EQ(ICEROLE_CONTROLLING, transport->fake_ice_transport()->GetIceRole()); |
| 824 | // Transports will be created in ICEFULL_MODE. |
| 825 | EXPECT_EQ(ICEMODE_FULL, transport->fake_ice_transport()->remote_ice_mode()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 826 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1, |
| 827 | kIcePwd1, ICEMODE_LITE, CONNECTIONROLE_NONE, |
| 828 | nullptr); |
| 829 | ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 830 | "audio", remote_desc, CA_ANSWER, nullptr)); |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 831 | EXPECT_EQ(ICEROLE_CONTROLLING, transport->fake_ice_transport()->GetIceRole()); |
| 832 | // After receiving remote description with ICEMODE_LITE, transport should |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 833 | // have mode set to ICEMODE_LITE. |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 834 | EXPECT_EQ(ICEMODE_LITE, transport->fake_ice_transport()->remote_ice_mode()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 835 | } |
| 836 | |
deadbeef | 897d08e | 2017-04-20 00:57:25 -0700 | [diff] [blame] | 837 | // Tests that the ICE role remains "controlling" if a subsequent offer that |
| 838 | // does an ICE restart is received from an ICE lite endpoint. Regression test |
| 839 | // for: https://crbug.com/710760 |
| 840 | TEST_F(TransportControllerTest, |
| 841 | IceRoleIsControllingAfterIceRestartFromIceLiteEndpoint) { |
| 842 | FakeDtlsTransport* transport = CreateFakeDtlsTransport("audio", 1); |
| 843 | ASSERT_NE(nullptr, transport); |
| 844 | std::string err; |
| 845 | |
| 846 | // Initial offer/answer. |
| 847 | TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1, |
| 848 | kIcePwd1, ICEMODE_LITE, |
| 849 | CONNECTIONROLE_ACTPASS, nullptr); |
| 850 | TransportDescription local_desc(kIceUfrag1, kIcePwd1); |
| 851 | ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 852 | "audio", remote_desc, CA_OFFER, &err)); |
| 853 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 854 | "audio", local_desc, CA_ANSWER, nullptr)); |
| 855 | // Subsequent ICE restart offer/answer. |
| 856 | remote_desc.ice_ufrag = kIceUfrag2; |
| 857 | remote_desc.ice_pwd = kIcePwd2; |
| 858 | local_desc.ice_ufrag = kIceUfrag2; |
| 859 | local_desc.ice_pwd = kIcePwd2; |
| 860 | ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 861 | "audio", remote_desc, CA_OFFER, &err)); |
| 862 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 863 | "audio", local_desc, CA_ANSWER, nullptr)); |
| 864 | |
| 865 | EXPECT_EQ(ICEROLE_CONTROLLING, transport->fake_ice_transport()->GetIceRole()); |
| 866 | } |
| 867 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 868 | // Tests SetNeedsIceRestartFlag and NeedsIceRestart, setting the flag and then |
| 869 | // initiating an ICE restart for one of the transports. |
| 870 | TEST_F(TransportControllerTest, NeedsIceRestart) { |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 871 | CreateFakeDtlsTransport("audio", 1); |
| 872 | CreateFakeDtlsTransport("video", 1); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 873 | |
| 874 | // Do initial offer/answer so there's something to restart. |
| 875 | TransportDescription local_desc(kIceUfrag1, kIcePwd1); |
| 876 | TransportDescription remote_desc(kIceUfrag1, kIcePwd1); |
| 877 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 878 | "audio", local_desc, CA_OFFER, nullptr)); |
| 879 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 880 | "video", local_desc, CA_OFFER, nullptr)); |
| 881 | ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 882 | "audio", remote_desc, CA_ANSWER, nullptr)); |
| 883 | ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription( |
| 884 | "video", remote_desc, CA_ANSWER, nullptr)); |
| 885 | |
| 886 | // Initially NeedsIceRestart should return false. |
| 887 | EXPECT_FALSE(transport_controller_->NeedsIceRestart("audio")); |
| 888 | EXPECT_FALSE(transport_controller_->NeedsIceRestart("video")); |
| 889 | |
| 890 | // Set the needs-ice-restart flag and verify NeedsIceRestart starts returning |
| 891 | // true. |
| 892 | transport_controller_->SetNeedsIceRestartFlag(); |
| 893 | EXPECT_TRUE(transport_controller_->NeedsIceRestart("audio")); |
| 894 | EXPECT_TRUE(transport_controller_->NeedsIceRestart("video")); |
| 895 | // For a nonexistent transport, false should be returned. |
| 896 | EXPECT_FALSE(transport_controller_->NeedsIceRestart("deadbeef")); |
| 897 | |
| 898 | // Do ICE restart but only for audio. |
| 899 | TransportDescription ice_restart_local_desc(kIceUfrag2, kIcePwd2); |
| 900 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 901 | "audio", ice_restart_local_desc, CA_OFFER, nullptr)); |
| 902 | ASSERT_TRUE(transport_controller_->SetLocalTransportDescription( |
| 903 | "video", local_desc, CA_OFFER, nullptr)); |
| 904 | // NeedsIceRestart should still be true for video. |
| 905 | EXPECT_FALSE(transport_controller_->NeedsIceRestart("audio")); |
| 906 | EXPECT_TRUE(transport_controller_->NeedsIceRestart("video")); |
| 907 | } |
| 908 | |
Zhi Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 909 | } // namespace cricket |