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