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