blob: 1fc71f1274c45aa1e81d4635e00373c42471c42c [file] [log] [blame]
deadbeefcbecd352015-09-23 11:50:27 -07001/*
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>
jbauch555604a2016-04-26 03:13:22 -070012#include <memory>
deadbeefcbecd352015-09-23 11:50:27 -070013
14#include "webrtc/base/fakesslidentity.h"
15#include "webrtc/base/gunit.h"
16#include "webrtc/base/helpers.h"
deadbeefcbecd352015-09-23 11:50:27 -070017#include "webrtc/base/sslidentity.h"
18#include "webrtc/base/thread.h"
19#include "webrtc/p2p/base/dtlstransportchannel.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070020#include "webrtc/p2p/base/fakeportallocator.h"
deadbeefcbecd352015-09-23 11:50:27 -070021#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"
deadbeefcbecd352015-09-23 11:50:27 -070025
26static const int kTimeout = 100;
27static const char kIceUfrag1[] = "TESTICEUFRAG0001";
28static const char kIcePwd1[] = "TESTICEPWD00000000000001";
29static const char kIceUfrag2[] = "TESTICEUFRAG0002";
30static const char kIcePwd2[] = "TESTICEPWD00000000000002";
deadbeef91042f82016-07-15 17:48:13 -070031static const char kIceUfrag3[] = "TESTICEUFRAG0003";
32static const char kIcePwd3[] = "TESTICEPWD00000000000003";
deadbeefcbecd352015-09-23 11:50:27 -070033
deadbeef91042f82016-07-15 17:48:13 -070034namespace cricket {
deadbeefcbecd352015-09-23 11:50:27 -070035
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.
42typedef FakeTransportController TransportControllerForTest;
43
44class 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
johan27c3d5b2016-10-17 00:54:57 -070053 void CreateTransportControllerWithNetworkThread() {
54 if (!network_thread_) {
55 network_thread_ = rtc::Thread::CreateWithSocketServer();
56 network_thread_->Start();
deadbeefcbecd352015-09-23 11:50:27 -070057 }
58 transport_controller_.reset(
johan27c3d5b2016-10-17 00:54:57 -070059 new TransportControllerForTest(network_thread_.get()));
deadbeefcbecd352015-09-23 11:50:27 -070060 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
zhihuangb2cdd932017-01-19 16:54:25 -080074 FakeDtlsTransport* CreateChannel(const std::string& content, int component) {
75 DtlsTransportInternal* channel =
76 transport_controller_->CreateDtlsTransport_n(content, component);
77 return static_cast<FakeDtlsTransport*>(channel);
deadbeefcbecd352015-09-23 11:50:27 -070078 }
79
80 void DestroyChannel(const std::string& content, int component) {
zhihuangb2cdd932017-01-19 16:54:25 -080081 transport_controller_->DestroyDtlsTransport_n(content, component);
deadbeefcbecd352015-09-23 11:50:27 -070082 }
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);
deadbeef91042f82016-07-15 17:48:13 -070088 c.set_protocol(UDP_PROTOCOL_NAME);
deadbeefcbecd352015-09-23 11:50:27 -070089 c.set_priority(1);
90 return c;
91 }
92
93 // Used for thread hopping test.
johan27c3d5b2016-10-17 00:54:57 -070094 void CreateChannelsAndCompleteConnectionOnNetworkThread() {
95 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070096 RTC_FROM_HERE,
97 rtc::Bind(
98 &TransportControllerTest::CreateChannelsAndCompleteConnection_w,
99 this));
deadbeefcbecd352015-09-23 11:50:27 -0700100 }
101
102 void CreateChannelsAndCompleteConnection_w() {
deadbeef91042f82016-07-15 17:48:13 -0700103 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
zhihuangb2cdd932017-01-19 16:54:25 -0800104 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700105 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800106 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700107 ASSERT_NE(nullptr, channel2);
108
deadbeef46eed762016-01-28 13:24:37 -0800109 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700110 kIcePwd1, ICEMODE_FULL,
111 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700112 std::string err;
deadbeef91042f82016-07-15 17:48:13 -0700113 transport_controller_->SetLocalTransportDescription("audio", local_desc,
114 CA_OFFER, &err);
115 transport_controller_->SetLocalTransportDescription("video", local_desc,
116 CA_OFFER, &err);
deadbeefcbecd352015-09-23 11:50:27 -0700117 transport_controller_->MaybeStartGathering();
zhihuangb2cdd932017-01-19 16:54:25 -0800118 channel1->ice_transport()->SignalCandidateGathered(
119 channel1->ice_transport(), CreateCandidate(1));
120 channel2->ice_transport()->SignalCandidateGathered(
121 channel2->ice_transport(), CreateCandidate(1));
deadbeefcbecd352015-09-23 11:50:27 -0700122 channel1->SetCandidatesGatheringComplete();
123 channel2->SetCandidatesGatheringComplete();
124 channel1->SetConnectionCount(2);
125 channel2->SetConnectionCount(2);
126 channel1->SetReceiving(true);
127 channel2->SetReceiving(true);
128 channel1->SetWritable(true);
129 channel2->SetWritable(true);
130 channel1->SetConnectionCount(1);
131 channel2->SetConnectionCount(1);
132 }
133
deadbeef91042f82016-07-15 17:48:13 -0700134 IceConfig CreateIceConfig(
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700135 int receiving_timeout,
deadbeef91042f82016-07-15 17:48:13 -0700136 ContinualGatheringPolicy continual_gathering_policy) {
137 IceConfig config;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800138 config.receiving_timeout = receiving_timeout;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700139 config.continual_gathering_policy = continual_gathering_policy;
honghaiz1f429e32015-09-28 07:57:34 -0700140 return config;
141 }
142
deadbeefcbecd352015-09-23 11:50:27 -0700143 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
johan27c3d5b2016-10-17 00:54:57 -0700178 std::unique_ptr<rtc::Thread> network_thread_; // Not used for most tests.
jbauch555604a2016-04-26 03:13:22 -0700179 std::unique_ptr<TransportControllerForTest> transport_controller_;
deadbeefcbecd352015-09-23 11:50:27 -0700180
181 // Information received from signals from transport controller.
deadbeef91042f82016-07-15 17:48:13 -0700182 IceConnectionState connection_state_ = kIceConnectionConnecting;
deadbeefcbecd352015-09-23 11:50:27 -0700183 bool receiving_ = false;
deadbeef91042f82016-07-15 17:48:13 -0700184 IceGatheringState gathering_state_ = kIceGatheringNew;
deadbeefcbecd352015-09-23 11:50:27 -0700185 // 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
honghaiz1f429e32015-09-28 07:57:34 -0700198TEST_F(TransportControllerTest, TestSetIceConfig) {
zhihuangb2cdd932017-01-19 16:54:25 -0800199 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700200 ASSERT_NE(nullptr, channel1);
201
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700202 transport_controller_->SetIceConfig(
deadbeef91042f82016-07-15 17:48:13 -0700203 CreateIceConfig(1000, GATHER_CONTINUALLY));
deadbeefcbecd352015-09-23 11:50:27 -0700204 EXPECT_EQ(1000, channel1->receiving_timeout());
honghaiz1f429e32015-09-28 07:57:34 -0700205 EXPECT_TRUE(channel1->gather_continually());
deadbeefcbecd352015-09-23 11:50:27 -0700206
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700207 transport_controller_->SetIceConfig(
deadbeef91042f82016-07-15 17:48:13 -0700208 CreateIceConfig(1000, GATHER_CONTINUALLY_AND_RECOVER));
deadbeefcbecd352015-09-23 11:50:27 -0700209 // Test that value stored in controller is applied to new channels.
zhihuangb2cdd932017-01-19 16:54:25 -0800210 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700211 ASSERT_NE(nullptr, channel2);
212 EXPECT_EQ(1000, channel2->receiving_timeout());
honghaiz1f429e32015-09-28 07:57:34 -0700213 EXPECT_TRUE(channel2->gather_continually());
deadbeefcbecd352015-09-23 11:50:27 -0700214}
215
216TEST_F(TransportControllerTest, TestSetSslMaxProtocolVersion) {
217 EXPECT_TRUE(transport_controller_->SetSslMaxProtocolVersion(
218 rtc::SSL_PROTOCOL_DTLS_12));
zhihuangb2cdd932017-01-19 16:54:25 -0800219 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700220
221 ASSERT_NE(nullptr, channel);
222 EXPECT_EQ(rtc::SSL_PROTOCOL_DTLS_12, channel->ssl_max_protocol_version());
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
229TEST_F(TransportControllerTest, TestSetIceRole) {
zhihuangb2cdd932017-01-19 16:54:25 -0800230 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700231 ASSERT_NE(nullptr, channel1);
232
deadbeef91042f82016-07-15 17:48:13 -0700233 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
234 EXPECT_EQ(ICEROLE_CONTROLLING, channel1->GetIceRole());
235 transport_controller_->SetIceRole(ICEROLE_CONTROLLED);
236 EXPECT_EQ(ICEROLE_CONTROLLED, channel1->GetIceRole());
deadbeefcbecd352015-09-23 11:50:27 -0700237
238 // Test that value stored in controller is applied to new channels.
zhihuangb2cdd932017-01-19 16:54:25 -0800239 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700240 ASSERT_NE(nullptr, channel2);
deadbeef91042f82016-07-15 17:48:13 -0700241 EXPECT_EQ(ICEROLE_CONTROLLED, channel2->GetIceRole());
deadbeefcbecd352015-09-23 11:50:27 -0700242}
243
244// Test that when one channel encounters a role conflict, the ICE role is
245// swapped on every channel.
246TEST_F(TransportControllerTest, TestIceRoleConflict) {
zhihuangb2cdd932017-01-19 16:54:25 -0800247 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700248 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800249 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700250 ASSERT_NE(nullptr, channel2);
251
deadbeef91042f82016-07-15 17:48:13 -0700252 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
253 EXPECT_EQ(ICEROLE_CONTROLLING, channel1->GetIceRole());
254 EXPECT_EQ(ICEROLE_CONTROLLING, channel2->GetIceRole());
deadbeefcbecd352015-09-23 11:50:27 -0700255
zhihuangb2cdd932017-01-19 16:54:25 -0800256 channel1->ice_transport()->SignalRoleConflict(channel1->ice_transport());
deadbeef91042f82016-07-15 17:48:13 -0700257 EXPECT_EQ(ICEROLE_CONTROLLED, channel1->GetIceRole());
258 EXPECT_EQ(ICEROLE_CONTROLLED, channel2->GetIceRole());
deadbeef1c206102016-05-27 13:34:37 -0700259
260 // Should be able to handle a second role conflict. The remote endpoint can
261 // change its role/tie-breaker when it does an ICE restart.
zhihuangb2cdd932017-01-19 16:54:25 -0800262 channel2->ice_transport()->SignalRoleConflict(channel2->ice_transport());
deadbeef91042f82016-07-15 17:48:13 -0700263 EXPECT_EQ(ICEROLE_CONTROLLING, channel1->GetIceRole());
264 EXPECT_EQ(ICEROLE_CONTROLLING, channel2->GetIceRole());
deadbeefcbecd352015-09-23 11:50:27 -0700265}
266
267TEST_F(TransportControllerTest, TestGetSslRole) {
zhihuangb2cdd932017-01-19 16:54:25 -0800268 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700269 ASSERT_NE(nullptr, channel);
270 ASSERT_TRUE(channel->SetSslRole(rtc::SSL_CLIENT));
271 rtc::SSLRole role;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800272 EXPECT_FALSE(transport_controller_->GetSslRole("video", &role));
273 EXPECT_TRUE(transport_controller_->GetSslRole("audio", &role));
deadbeefcbecd352015-09-23 11:50:27 -0700274 EXPECT_EQ(rtc::SSL_CLIENT, role);
275}
276
277TEST_F(TransportControllerTest, TestSetAndGetLocalCertificate) {
278 rtc::scoped_refptr<rtc::RTCCertificate> certificate1 =
jbauch555604a2016-04-26 03:13:22 -0700279 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
kwiberg0eb15ed2015-12-17 03:04:15 -0800280 rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT)));
deadbeefcbecd352015-09-23 11:50:27 -0700281 rtc::scoped_refptr<rtc::RTCCertificate> certificate2 =
jbauch555604a2016-04-26 03:13:22 -0700282 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
kwiberg0eb15ed2015-12-17 03:04:15 -0800283 rtc::SSLIdentity::Generate("session2", rtc::KT_DEFAULT)));
deadbeefcbecd352015-09-23 11:50:27 -0700284 rtc::scoped_refptr<rtc::RTCCertificate> returned_certificate;
285
zhihuangb2cdd932017-01-19 16:54:25 -0800286 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700287 ASSERT_NE(nullptr, channel1);
288
289 EXPECT_TRUE(transport_controller_->SetLocalCertificate(certificate1));
290 EXPECT_TRUE(transport_controller_->GetLocalCertificate(
291 "audio", &returned_certificate));
292 EXPECT_EQ(certificate1->identity()->certificate().ToPEMString(),
293 returned_certificate->identity()->certificate().ToPEMString());
294
295 // Should fail if called for a nonexistant transport.
296 EXPECT_FALSE(transport_controller_->GetLocalCertificate(
297 "video", &returned_certificate));
298
299 // Test that identity stored in controller is applied to new channels.
zhihuangb2cdd932017-01-19 16:54:25 -0800300 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700301 ASSERT_NE(nullptr, channel2);
302 EXPECT_TRUE(transport_controller_->GetLocalCertificate(
303 "video", &returned_certificate));
304 EXPECT_EQ(certificate1->identity()->certificate().ToPEMString(),
305 returned_certificate->identity()->certificate().ToPEMString());
306
307 // Shouldn't be able to change the identity once set.
308 EXPECT_FALSE(transport_controller_->SetLocalCertificate(certificate2));
309}
310
311TEST_F(TransportControllerTest, TestGetRemoteSSLCertificate) {
312 rtc::FakeSSLCertificate fake_certificate("fake_data");
deadbeefcbecd352015-09-23 11:50:27 -0700313
zhihuangb2cdd932017-01-19 16:54:25 -0800314 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700315 ASSERT_NE(nullptr, channel);
316
317 channel->SetRemoteSSLCertificate(&fake_certificate);
jbauch555604a2016-04-26 03:13:22 -0700318 std::unique_ptr<rtc::SSLCertificate> returned_certificate =
kwibergb4d01c42016-04-06 05:15:06 -0700319 transport_controller_->GetRemoteSSLCertificate("audio");
320 EXPECT_TRUE(returned_certificate);
deadbeefcbecd352015-09-23 11:50:27 -0700321 EXPECT_EQ(fake_certificate.ToPEMString(),
322 returned_certificate->ToPEMString());
323
324 // Should fail if called for a nonexistant transport.
kwibergb4d01c42016-04-06 05:15:06 -0700325 EXPECT_FALSE(transport_controller_->GetRemoteSSLCertificate("video"));
deadbeefcbecd352015-09-23 11:50:27 -0700326}
327
328TEST_F(TransportControllerTest, TestSetLocalTransportDescription) {
zhihuangb2cdd932017-01-19 16:54:25 -0800329 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700330 ASSERT_NE(nullptr, channel);
deadbeef46eed762016-01-28 13:24:37 -0800331 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700332 kIcePwd1, ICEMODE_FULL,
333 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700334 std::string err;
335 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700336 "audio", local_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700337 // Check that ICE ufrag and pwd were propagated to channel.
338 EXPECT_EQ(kIceUfrag1, channel->ice_ufrag());
339 EXPECT_EQ(kIcePwd1, channel->ice_pwd());
340 // After setting local description, we should be able to start gathering
341 // candidates.
342 transport_controller_->MaybeStartGathering();
deadbeef91042f82016-07-15 17:48:13 -0700343 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700344 EXPECT_EQ(1, gathering_state_signal_count_);
345}
346
347TEST_F(TransportControllerTest, TestSetRemoteTransportDescription) {
zhihuangb2cdd932017-01-19 16:54:25 -0800348 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700349 ASSERT_NE(nullptr, channel);
deadbeef46eed762016-01-28 13:24:37 -0800350 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700351 kIcePwd1, ICEMODE_FULL,
352 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700353 std::string err;
354 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700355 "audio", remote_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700356 // Check that ICE ufrag and pwd were propagated to channel.
357 EXPECT_EQ(kIceUfrag1, channel->remote_ice_ufrag());
358 EXPECT_EQ(kIcePwd1, channel->remote_ice_pwd());
359}
360
361TEST_F(TransportControllerTest, TestAddRemoteCandidates) {
zhihuangb2cdd932017-01-19 16:54:25 -0800362 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700363 ASSERT_NE(nullptr, channel);
364 Candidates candidates;
365 candidates.push_back(CreateCandidate(1));
366 std::string err;
367 EXPECT_TRUE(
368 transport_controller_->AddRemoteCandidates("audio", candidates, &err));
369 EXPECT_EQ(1U, channel->remote_candidates().size());
370}
371
372TEST_F(TransportControllerTest, TestReadyForRemoteCandidates) {
zhihuangb2cdd932017-01-19 16:54:25 -0800373 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700374 ASSERT_NE(nullptr, channel);
375 // We expect to be ready for remote candidates only after local and remote
376 // descriptions are set.
377 EXPECT_FALSE(transport_controller_->ReadyForRemoteCandidates("audio"));
378
379 std::string err;
deadbeef46eed762016-01-28 13:24:37 -0800380 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700381 kIcePwd1, ICEMODE_FULL,
382 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700383 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700384 "audio", remote_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700385 EXPECT_FALSE(transport_controller_->ReadyForRemoteCandidates("audio"));
386
deadbeef46eed762016-01-28 13:24:37 -0800387 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag2,
deadbeef91042f82016-07-15 17:48:13 -0700388 kIcePwd2, ICEMODE_FULL,
389 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700390 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700391 "audio", local_desc, CA_ANSWER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700392 EXPECT_TRUE(transport_controller_->ReadyForRemoteCandidates("audio"));
393}
394
395TEST_F(TransportControllerTest, TestGetStats) {
zhihuangb2cdd932017-01-19 16:54:25 -0800396 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700397 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800398 FakeDtlsTransport* channel2 = CreateChannel("audio", 2);
deadbeefcbecd352015-09-23 11:50:27 -0700399 ASSERT_NE(nullptr, channel2);
zhihuangb2cdd932017-01-19 16:54:25 -0800400 FakeDtlsTransport* channel3 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700401 ASSERT_NE(nullptr, channel3);
402
403 TransportStats stats;
404 EXPECT_TRUE(transport_controller_->GetStats("audio", &stats));
405 EXPECT_EQ("audio", stats.transport_name);
406 EXPECT_EQ(2U, stats.channel_stats.size());
407}
408
409// Test that transport gets destroyed when it has no more channels.
410TEST_F(TransportControllerTest, TestCreateAndDestroyChannel) {
zhihuangb2cdd932017-01-19 16:54:25 -0800411 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700412 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800413 FakeDtlsTransport* channel2 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700414 ASSERT_NE(nullptr, channel2);
415 ASSERT_EQ(channel1, channel2);
zhihuangb2cdd932017-01-19 16:54:25 -0800416 FakeDtlsTransport* channel3 = CreateChannel("audio", 2);
deadbeefcbecd352015-09-23 11:50:27 -0700417 ASSERT_NE(nullptr, channel3);
418
419 // Using GetStats to check if transport is destroyed from an outside class's
420 // perspective.
421 TransportStats stats;
422 EXPECT_TRUE(transport_controller_->GetStats("audio", &stats));
423 DestroyChannel("audio", 2);
424 DestroyChannel("audio", 1);
425 EXPECT_TRUE(transport_controller_->GetStats("audio", &stats));
426 DestroyChannel("audio", 1);
427 EXPECT_FALSE(transport_controller_->GetStats("audio", &stats));
428}
429
430TEST_F(TransportControllerTest, TestSignalConnectionStateFailed) {
431 // Need controlling ICE role to get in failed state.
deadbeef91042f82016-07-15 17:48:13 -0700432 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
zhihuangb2cdd932017-01-19 16:54:25 -0800433 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700434 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800435 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700436 ASSERT_NE(nullptr, channel2);
437
438 // Should signal "failed" if any channel failed; channel is considered failed
439 // if it previously had a connection but now has none, and gathering is
440 // complete.
441 channel1->SetCandidatesGatheringComplete();
442 channel1->SetConnectionCount(1);
443 channel1->SetConnectionCount(0);
deadbeef91042f82016-07-15 17:48:13 -0700444 EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700445 EXPECT_EQ(1, connection_state_signal_count_);
446}
447
448TEST_F(TransportControllerTest, TestSignalConnectionStateConnected) {
deadbeef91042f82016-07-15 17:48:13 -0700449 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
zhihuangb2cdd932017-01-19 16:54:25 -0800450 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700451 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800452 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700453 ASSERT_NE(nullptr, channel2);
zhihuangb2cdd932017-01-19 16:54:25 -0800454 FakeDtlsTransport* channel3 = CreateChannel("video", 2);
deadbeefcbecd352015-09-23 11:50:27 -0700455 ASSERT_NE(nullptr, channel3);
456
457 // First, have one channel connect, and another fail, to ensure that
458 // the first channel connecting didn't trigger a "connected" state signal.
459 // We should only get a signal when all are connected.
460 channel1->SetConnectionCount(2);
461 channel1->SetWritable(true);
462 channel3->SetCandidatesGatheringComplete();
463 channel3->SetConnectionCount(1);
464 channel3->SetConnectionCount(0);
deadbeef91042f82016-07-15 17:48:13 -0700465 EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700466 // Signal count of 1 means that the only signal emitted was "failed".
467 EXPECT_EQ(1, connection_state_signal_count_);
468
469 // Destroy the failed channel to return to "connecting" state.
470 DestroyChannel("video", 2);
deadbeef91042f82016-07-15 17:48:13 -0700471 EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700472 EXPECT_EQ(2, connection_state_signal_count_);
473
474 // Make the remaining channel reach a connected state.
475 channel2->SetConnectionCount(2);
476 channel2->SetWritable(true);
deadbeef91042f82016-07-15 17:48:13 -0700477 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700478 EXPECT_EQ(3, connection_state_signal_count_);
479}
480
481TEST_F(TransportControllerTest, TestSignalConnectionStateComplete) {
deadbeef91042f82016-07-15 17:48:13 -0700482 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
zhihuangb2cdd932017-01-19 16:54:25 -0800483 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700484 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800485 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700486 ASSERT_NE(nullptr, channel2);
zhihuangb2cdd932017-01-19 16:54:25 -0800487 FakeDtlsTransport* channel3 = CreateChannel("video", 2);
deadbeefcbecd352015-09-23 11:50:27 -0700488 ASSERT_NE(nullptr, channel3);
489
490 // Similar to above test, but we're now reaching the completed state, which
zhihuangb2cdd932017-01-19 16:54:25 -0800491 // means only one connection per FakeDtlsTransport.
deadbeefcbecd352015-09-23 11:50:27 -0700492 channel1->SetCandidatesGatheringComplete();
493 channel1->SetConnectionCount(1);
494 channel1->SetWritable(true);
495 channel3->SetCandidatesGatheringComplete();
496 channel3->SetConnectionCount(1);
497 channel3->SetConnectionCount(0);
deadbeef91042f82016-07-15 17:48:13 -0700498 EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700499 // Signal count of 1 means that the only signal emitted was "failed".
500 EXPECT_EQ(1, connection_state_signal_count_);
501
502 // Destroy the failed channel to return to "connecting" state.
503 DestroyChannel("video", 2);
deadbeef91042f82016-07-15 17:48:13 -0700504 EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700505 EXPECT_EQ(2, connection_state_signal_count_);
506
507 // Make the remaining channel reach a connected state.
508 channel2->SetCandidatesGatheringComplete();
509 channel2->SetConnectionCount(2);
510 channel2->SetWritable(true);
deadbeef91042f82016-07-15 17:48:13 -0700511 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700512 EXPECT_EQ(3, connection_state_signal_count_);
513
514 // Finally, transition to completed state.
515 channel2->SetConnectionCount(1);
deadbeef91042f82016-07-15 17:48:13 -0700516 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700517 EXPECT_EQ(4, connection_state_signal_count_);
518}
519
520// Make sure that if we're "connected" and remove a transport, we stay in the
521// "connected" state.
522TEST_F(TransportControllerTest, TestDestroyTransportAndStayConnected) {
zhihuangb2cdd932017-01-19 16:54:25 -0800523 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700524 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800525 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700526 ASSERT_NE(nullptr, channel2);
527
528 channel1->SetCandidatesGatheringComplete();
529 channel1->SetConnectionCount(2);
530 channel1->SetWritable(true);
531 channel2->SetCandidatesGatheringComplete();
532 channel2->SetConnectionCount(2);
533 channel2->SetWritable(true);
deadbeef91042f82016-07-15 17:48:13 -0700534 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700535 EXPECT_EQ(1, connection_state_signal_count_);
536
537 // Destroy one channel, then "complete" the other one, so we reach
538 // a known state.
539 DestroyChannel("video", 1);
540 channel1->SetConnectionCount(1);
deadbeef91042f82016-07-15 17:48:13 -0700541 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700542 // Signal count of 2 means the deletion didn't cause any unexpected signals
543 EXPECT_EQ(2, connection_state_signal_count_);
544}
545
546// If we destroy the last/only transport, we should simply transition to
547// "connecting".
548TEST_F(TransportControllerTest, TestDestroyLastTransportWhileConnected) {
zhihuangb2cdd932017-01-19 16:54:25 -0800549 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700550 ASSERT_NE(nullptr, channel);
551
552 channel->SetCandidatesGatheringComplete();
553 channel->SetConnectionCount(2);
554 channel->SetWritable(true);
deadbeef91042f82016-07-15 17:48:13 -0700555 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700556 EXPECT_EQ(1, connection_state_signal_count_);
557
558 DestroyChannel("audio", 1);
deadbeef91042f82016-07-15 17:48:13 -0700559 EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700560 // Signal count of 2 means the deletion didn't cause any unexpected signals
561 EXPECT_EQ(2, connection_state_signal_count_);
562}
563
564TEST_F(TransportControllerTest, TestSignalReceiving) {
zhihuangb2cdd932017-01-19 16:54:25 -0800565 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700566 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800567 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700568 ASSERT_NE(nullptr, channel2);
569
570 // Should signal receiving as soon as any channel is receiving.
571 channel1->SetReceiving(true);
572 EXPECT_TRUE_WAIT(receiving_, kTimeout);
573 EXPECT_EQ(1, receiving_signal_count_);
574
575 channel2->SetReceiving(true);
576 channel1->SetReceiving(false);
577 channel2->SetReceiving(false);
578 EXPECT_TRUE_WAIT(!receiving_, kTimeout);
579 EXPECT_EQ(2, receiving_signal_count_);
580}
581
582TEST_F(TransportControllerTest, TestSignalGatheringStateGathering) {
zhihuangb2cdd932017-01-19 16:54:25 -0800583 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700584 ASSERT_NE(nullptr, channel);
zhihuangb2cdd932017-01-19 16:54:25 -0800585 channel->ice_transport()->MaybeStartGathering();
deadbeefcbecd352015-09-23 11:50:27 -0700586 // Should be in the gathering state as soon as any transport starts gathering.
deadbeef91042f82016-07-15 17:48:13 -0700587 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700588 EXPECT_EQ(1, gathering_state_signal_count_);
589}
590
591TEST_F(TransportControllerTest, TestSignalGatheringStateComplete) {
zhihuangb2cdd932017-01-19 16:54:25 -0800592 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700593 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800594 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700595 ASSERT_NE(nullptr, channel2);
zhihuangb2cdd932017-01-19 16:54:25 -0800596 FakeDtlsTransport* channel3 = CreateChannel("data", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700597 ASSERT_NE(nullptr, channel3);
598
zhihuangb2cdd932017-01-19 16:54:25 -0800599 channel3->ice_transport()->MaybeStartGathering();
deadbeef91042f82016-07-15 17:48:13 -0700600 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700601 EXPECT_EQ(1, gathering_state_signal_count_);
602
603 // Have one channel finish gathering, then destroy it, to make sure gathering
604 // completion wasn't signalled if only one transport finished gathering.
605 channel3->SetCandidatesGatheringComplete();
606 DestroyChannel("data", 1);
deadbeef91042f82016-07-15 17:48:13 -0700607 EXPECT_EQ_WAIT(kIceGatheringNew, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700608 EXPECT_EQ(2, gathering_state_signal_count_);
609
610 // Make remaining channels start and then finish gathering.
zhihuangb2cdd932017-01-19 16:54:25 -0800611 channel1->ice_transport()->MaybeStartGathering();
612 channel2->ice_transport()->MaybeStartGathering();
deadbeef91042f82016-07-15 17:48:13 -0700613 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700614 EXPECT_EQ(3, gathering_state_signal_count_);
615
616 channel1->SetCandidatesGatheringComplete();
617 channel2->SetCandidatesGatheringComplete();
deadbeef91042f82016-07-15 17:48:13 -0700618 EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700619 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.
626TEST_F(TransportControllerTest,
627 TestSignalingWhenLastIncompleteTransportDestroyed) {
deadbeef91042f82016-07-15 17:48:13 -0700628 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
zhihuangb2cdd932017-01-19 16:54:25 -0800629 FakeDtlsTransport* channel1 = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700630 ASSERT_NE(nullptr, channel1);
zhihuangb2cdd932017-01-19 16:54:25 -0800631 FakeDtlsTransport* channel2 = CreateChannel("video", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700632 ASSERT_NE(nullptr, channel2);
633
634 channel1->SetCandidatesGatheringComplete();
deadbeef91042f82016-07-15 17:48:13 -0700635 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700636 EXPECT_EQ(1, gathering_state_signal_count_);
637
638 channel1->SetConnectionCount(1);
639 channel1->SetWritable(true);
640 DestroyChannel("video", 1);
deadbeef91042f82016-07-15 17:48:13 -0700641 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700642 EXPECT_EQ(1, connection_state_signal_count_);
deadbeef91042f82016-07-15 17:48:13 -0700643 EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700644 EXPECT_EQ(2, gathering_state_signal_count_);
645}
646
647TEST_F(TransportControllerTest, TestSignalCandidatesGathered) {
zhihuangb2cdd932017-01-19 16:54:25 -0800648 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeefcbecd352015-09-23 11:50:27 -0700649 ASSERT_NE(nullptr, channel);
650
651 // Transport won't signal candidates until it has a local description.
deadbeef46eed762016-01-28 13:24:37 -0800652 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700653 kIcePwd1, ICEMODE_FULL,
654 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700655 std::string err;
656 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700657 "audio", local_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700658 transport_controller_->MaybeStartGathering();
659
zhihuangb2cdd932017-01-19 16:54:25 -0800660 channel->ice_transport()->SignalCandidateGathered(channel->ice_transport(),
661 CreateCandidate(1));
deadbeefcbecd352015-09-23 11:50:27 -0700662 EXPECT_EQ_WAIT(1, candidates_signal_count_, kTimeout);
663 EXPECT_EQ(1U, candidates_["audio"].size());
664}
665
666TEST_F(TransportControllerTest, TestSignalingOccursOnSignalingThread) {
johan27c3d5b2016-10-17 00:54:57 -0700667 CreateTransportControllerWithNetworkThread();
668 CreateChannelsAndCompleteConnectionOnNetworkThread();
deadbeefcbecd352015-09-23 11:50:27 -0700669
670 // connecting --> connected --> completed
deadbeef91042f82016-07-15 17:48:13 -0700671 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700672 EXPECT_EQ(2, connection_state_signal_count_);
673
674 EXPECT_TRUE_WAIT(receiving_, kTimeout);
675 EXPECT_EQ(1, receiving_signal_count_);
676
677 // new --> gathering --> complete
deadbeef91042f82016-07-15 17:48:13 -0700678 EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700679 EXPECT_EQ(2, gathering_state_signal_count_);
680
681 EXPECT_EQ_WAIT(1U, candidates_["audio"].size(), kTimeout);
682 EXPECT_EQ_WAIT(1U, candidates_["video"].size(), kTimeout);
683 EXPECT_EQ(2, candidates_signal_count_);
684
685 EXPECT_TRUE(!signaled_on_non_signaling_thread_);
686}
deadbeef91042f82016-07-15 17:48:13 -0700687
688// Older versions of Chrome expect the ICE role to be re-determined when an
689// ICE restart occurs, and also don't perform conflict resolution correctly,
690// so for now we can't safely stop doing this.
691// See: https://bugs.chromium.org/p/chromium/issues/detail?id=628676
692// TODO(deadbeef): Remove this when these old versions of Chrome reach a low
693// enough population.
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700694TEST_F(TransportControllerTest, IceRoleRedeterminedOnIceRestartByDefault) {
zhihuangb2cdd932017-01-19 16:54:25 -0800695 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeef91042f82016-07-15 17:48:13 -0700696 ASSERT_NE(nullptr, channel);
697 std::string err;
698 // Do an initial offer answer, so that the next offer is an ICE restart.
699 transport_controller_->SetIceRole(ICEROLE_CONTROLLED);
700 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
701 kIcePwd1, ICEMODE_FULL,
702 CONNECTIONROLE_ACTPASS, nullptr);
703 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
704 "audio", remote_desc, CA_OFFER, &err));
705 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag2,
706 kIcePwd2, ICEMODE_FULL,
707 CONNECTIONROLE_ACTPASS, nullptr);
708 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
709 "audio", local_desc, CA_ANSWER, &err));
710 EXPECT_EQ(ICEROLE_CONTROLLED, channel->GetIceRole());
711
712 // The endpoint that initiated an ICE restart should take the controlling
713 // role.
714 TransportDescription ice_restart_desc(std::vector<std::string>(), kIceUfrag3,
715 kIcePwd3, ICEMODE_FULL,
716 CONNECTIONROLE_ACTPASS, nullptr);
717 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
718 "audio", ice_restart_desc, CA_OFFER, &err));
719 EXPECT_EQ(ICEROLE_CONTROLLING, channel->GetIceRole());
720}
721
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700722// Test that if the TransportController was created with the
723// |redetermine_role_on_ice_restart| parameter set to false, the role is *not*
724// redetermined on an ICE restart.
725TEST_F(TransportControllerTest, IceRoleNotRedetermined) {
726 bool redetermine_role = false;
727 transport_controller_.reset(new TransportControllerForTest(redetermine_role));
zhihuangb2cdd932017-01-19 16:54:25 -0800728 FakeDtlsTransport* channel = CreateChannel("audio", 1);
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700729 ASSERT_NE(nullptr, channel);
730 std::string err;
731 // Do an initial offer answer, so that the next offer is an ICE restart.
732 transport_controller_->SetIceRole(ICEROLE_CONTROLLED);
733 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
734 kIcePwd1, ICEMODE_FULL,
735 CONNECTIONROLE_ACTPASS, nullptr);
736 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
737 "audio", remote_desc, CA_OFFER, &err));
738 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag2,
739 kIcePwd2, ICEMODE_FULL,
740 CONNECTIONROLE_ACTPASS, nullptr);
741 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
742 "audio", local_desc, CA_ANSWER, &err));
743 EXPECT_EQ(ICEROLE_CONTROLLED, channel->GetIceRole());
744
745 // The endpoint that initiated an ICE restart should keep the existing role.
746 TransportDescription ice_restart_desc(std::vector<std::string>(), kIceUfrag3,
747 kIcePwd3, ICEMODE_FULL,
748 CONNECTIONROLE_ACTPASS, nullptr);
749 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
750 "audio", ice_restart_desc, CA_OFFER, &err));
751 EXPECT_EQ(ICEROLE_CONTROLLED, channel->GetIceRole());
752}
753
deadbeef49f34fd2016-12-06 16:22:06 -0800754// Tests channel role is reversed after receiving ice-lite from remote.
755TEST_F(TransportControllerTest, TestSetRemoteIceLiteInOffer) {
zhihuangb2cdd932017-01-19 16:54:25 -0800756 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeef49f34fd2016-12-06 16:22:06 -0800757 ASSERT_NE(nullptr, channel);
758 std::string err;
759
760 transport_controller_->SetIceRole(ICEROLE_CONTROLLED);
761 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
762 kIcePwd1, ICEMODE_LITE,
763 CONNECTIONROLE_ACTPASS, nullptr);
764 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
765 "audio", remote_desc, CA_OFFER, &err));
766 TransportDescription local_desc(kIceUfrag1, kIcePwd1);
767 ASSERT_TRUE(transport_controller_->SetLocalTransportDescription(
768 "audio", local_desc, CA_ANSWER, nullptr));
769
770 EXPECT_EQ(ICEROLE_CONTROLLING, channel->GetIceRole());
771 EXPECT_EQ(ICEMODE_LITE, channel->remote_ice_mode());
772}
773
774// Tests ice-lite in remote answer.
775TEST_F(TransportControllerTest, TestSetRemoteIceLiteInAnswer) {
zhihuangb2cdd932017-01-19 16:54:25 -0800776 FakeDtlsTransport* channel = CreateChannel("audio", 1);
deadbeef49f34fd2016-12-06 16:22:06 -0800777 ASSERT_NE(nullptr, channel);
778 std::string err;
779
780 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
781 TransportDescription local_desc(kIceUfrag1, kIcePwd1);
782 ASSERT_TRUE(transport_controller_->SetLocalTransportDescription(
783 "audio", local_desc, CA_OFFER, nullptr));
784 EXPECT_EQ(ICEROLE_CONTROLLING, channel->GetIceRole());
785 // Channels will be created in ICEFULL_MODE.
786 EXPECT_EQ(ICEMODE_FULL, channel->remote_ice_mode());
787 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
788 kIcePwd1, ICEMODE_LITE, CONNECTIONROLE_NONE,
789 nullptr);
790 ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription(
791 "audio", remote_desc, CA_ANSWER, nullptr));
792 EXPECT_EQ(ICEROLE_CONTROLLING, channel->GetIceRole());
793 // After receiving remote description with ICEMODE_LITE, channel should
794 // have mode set to ICEMODE_LITE.
795 EXPECT_EQ(ICEMODE_LITE, channel->remote_ice_mode());
796}
797
deadbeefd1a38b52016-12-10 13:15:33 -0800798// Tests SetNeedsIceRestartFlag and NeedsIceRestart, setting the flag and then
799// initiating an ICE restart for one of the transports.
800TEST_F(TransportControllerTest, NeedsIceRestart) {
801 CreateChannel("audio", 1);
802 CreateChannel("video", 1);
803
804 // Do initial offer/answer so there's something to restart.
805 TransportDescription local_desc(kIceUfrag1, kIcePwd1);
806 TransportDescription remote_desc(kIceUfrag1, kIcePwd1);
807 ASSERT_TRUE(transport_controller_->SetLocalTransportDescription(
808 "audio", local_desc, CA_OFFER, nullptr));
809 ASSERT_TRUE(transport_controller_->SetLocalTransportDescription(
810 "video", local_desc, CA_OFFER, nullptr));
811 ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription(
812 "audio", remote_desc, CA_ANSWER, nullptr));
813 ASSERT_TRUE(transport_controller_->SetRemoteTransportDescription(
814 "video", remote_desc, CA_ANSWER, nullptr));
815
816 // Initially NeedsIceRestart should return false.
817 EXPECT_FALSE(transport_controller_->NeedsIceRestart("audio"));
818 EXPECT_FALSE(transport_controller_->NeedsIceRestart("video"));
819
820 // Set the needs-ice-restart flag and verify NeedsIceRestart starts returning
821 // true.
822 transport_controller_->SetNeedsIceRestartFlag();
823 EXPECT_TRUE(transport_controller_->NeedsIceRestart("audio"));
824 EXPECT_TRUE(transport_controller_->NeedsIceRestart("video"));
825 // For a nonexistent transport, false should be returned.
826 EXPECT_FALSE(transport_controller_->NeedsIceRestart("deadbeef"));
827
828 // Do ICE restart but only for audio.
829 TransportDescription ice_restart_local_desc(kIceUfrag2, kIcePwd2);
830 ASSERT_TRUE(transport_controller_->SetLocalTransportDescription(
831 "audio", ice_restart_local_desc, CA_OFFER, nullptr));
832 ASSERT_TRUE(transport_controller_->SetLocalTransportDescription(
833 "video", local_desc, CA_OFFER, nullptr));
834 // NeedsIceRestart should still be true for video.
835 EXPECT_FALSE(transport_controller_->NeedsIceRestart("audio"));
836 EXPECT_TRUE(transport_controller_->NeedsIceRestart("video"));
837}
838
deadbeef91042f82016-07-15 17:48:13 -0700839} // namespace cricket {