blob: d1f3816af9245821c3ba272eb474e475298afc27 [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
74 FakeTransportChannel* CreateChannel(const std::string& content,
75 int component) {
76 TransportChannel* channel =
Danil Chapovalov7f216b72016-05-12 09:20:31 +020077 transport_controller_->CreateTransportChannel_n(content, component);
deadbeefcbecd352015-09-23 11:50:27 -070078 return static_cast<FakeTransportChannel*>(channel);
79 }
80
81 void DestroyChannel(const std::string& content, int component) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +020082 transport_controller_->DestroyTransportChannel_n(content, component);
deadbeefcbecd352015-09-23 11:50:27 -070083 }
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);
deadbeef91042f82016-07-15 17:48:13 -070089 c.set_protocol(UDP_PROTOCOL_NAME);
deadbeefcbecd352015-09-23 11:50:27 -070090 c.set_priority(1);
91 return c;
92 }
93
94 // Used for thread hopping test.
johan27c3d5b2016-10-17 00:54:57 -070095 void CreateChannelsAndCompleteConnectionOnNetworkThread() {
96 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070097 RTC_FROM_HERE,
98 rtc::Bind(
99 &TransportControllerTest::CreateChannelsAndCompleteConnection_w,
100 this));
deadbeefcbecd352015-09-23 11:50:27 -0700101 }
102
103 void CreateChannelsAndCompleteConnection_w() {
deadbeef91042f82016-07-15 17:48:13 -0700104 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
deadbeefcbecd352015-09-23 11:50:27 -0700105 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
106 ASSERT_NE(nullptr, channel1);
107 FakeTransportChannel* channel2 = CreateChannel("video", 1);
108 ASSERT_NE(nullptr, channel2);
109
deadbeef46eed762016-01-28 13:24:37 -0800110 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700111 kIcePwd1, ICEMODE_FULL,
112 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700113 std::string err;
deadbeef91042f82016-07-15 17:48:13 -0700114 transport_controller_->SetLocalTransportDescription("audio", local_desc,
115 CA_OFFER, &err);
116 transport_controller_->SetLocalTransportDescription("video", local_desc,
117 CA_OFFER, &err);
deadbeefcbecd352015-09-23 11:50:27 -0700118 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
deadbeef91042f82016-07-15 17:48:13 -0700133 IceConfig CreateIceConfig(
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700134 int receiving_timeout,
deadbeef91042f82016-07-15 17:48:13 -0700135 ContinualGatheringPolicy continual_gathering_policy) {
136 IceConfig config;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800137 config.receiving_timeout = receiving_timeout;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700138 config.continual_gathering_policy = continual_gathering_policy;
honghaiz1f429e32015-09-28 07:57:34 -0700139 return config;
140 }
141
deadbeefcbecd352015-09-23 11:50:27 -0700142 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
johan27c3d5b2016-10-17 00:54:57 -0700177 std::unique_ptr<rtc::Thread> network_thread_; // Not used for most tests.
jbauch555604a2016-04-26 03:13:22 -0700178 std::unique_ptr<TransportControllerForTest> transport_controller_;
deadbeefcbecd352015-09-23 11:50:27 -0700179
180 // Information received from signals from transport controller.
deadbeef91042f82016-07-15 17:48:13 -0700181 IceConnectionState connection_state_ = kIceConnectionConnecting;
deadbeefcbecd352015-09-23 11:50:27 -0700182 bool receiving_ = false;
deadbeef91042f82016-07-15 17:48:13 -0700183 IceGatheringState gathering_state_ = kIceGatheringNew;
deadbeefcbecd352015-09-23 11:50:27 -0700184 // 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
honghaiz1f429e32015-09-28 07:57:34 -0700197TEST_F(TransportControllerTest, TestSetIceConfig) {
deadbeefcbecd352015-09-23 11:50:27 -0700198 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
199 ASSERT_NE(nullptr, channel1);
200
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700201 transport_controller_->SetIceConfig(
deadbeef91042f82016-07-15 17:48:13 -0700202 CreateIceConfig(1000, GATHER_CONTINUALLY));
deadbeefcbecd352015-09-23 11:50:27 -0700203 EXPECT_EQ(1000, channel1->receiving_timeout());
honghaiz1f429e32015-09-28 07:57:34 -0700204 EXPECT_TRUE(channel1->gather_continually());
deadbeefcbecd352015-09-23 11:50:27 -0700205
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700206 transport_controller_->SetIceConfig(
deadbeef91042f82016-07-15 17:48:13 -0700207 CreateIceConfig(1000, GATHER_CONTINUALLY_AND_RECOVER));
deadbeefcbecd352015-09-23 11:50:27 -0700208 // 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());
honghaiz1f429e32015-09-28 07:57:34 -0700212 EXPECT_TRUE(channel2->gather_continually());
deadbeefcbecd352015-09-23 11:50:27 -0700213}
214
215TEST_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
228TEST_F(TransportControllerTest, TestSetIceRole) {
229 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
230 ASSERT_NE(nullptr, channel1);
231
deadbeef91042f82016-07-15 17:48:13 -0700232 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());
deadbeefcbecd352015-09-23 11:50:27 -0700236
237 // Test that value stored in controller is applied to new channels.
238 FakeTransportChannel* channel2 = CreateChannel("video", 1);
239 ASSERT_NE(nullptr, channel2);
deadbeef91042f82016-07-15 17:48:13 -0700240 EXPECT_EQ(ICEROLE_CONTROLLED, channel2->GetIceRole());
deadbeefcbecd352015-09-23 11:50:27 -0700241}
242
243// Test that when one channel encounters a role conflict, the ICE role is
244// swapped on every channel.
245TEST_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
deadbeef91042f82016-07-15 17:48:13 -0700251 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
252 EXPECT_EQ(ICEROLE_CONTROLLING, channel1->GetIceRole());
253 EXPECT_EQ(ICEROLE_CONTROLLING, channel2->GetIceRole());
deadbeefcbecd352015-09-23 11:50:27 -0700254
255 channel1->SignalRoleConflict(channel1);
deadbeef91042f82016-07-15 17:48:13 -0700256 EXPECT_EQ(ICEROLE_CONTROLLED, channel1->GetIceRole());
257 EXPECT_EQ(ICEROLE_CONTROLLED, channel2->GetIceRole());
deadbeef1c206102016-05-27 13:34:37 -0700258
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);
deadbeef91042f82016-07-15 17:48:13 -0700262 EXPECT_EQ(ICEROLE_CONTROLLING, channel1->GetIceRole());
263 EXPECT_EQ(ICEROLE_CONTROLLING, channel2->GetIceRole());
deadbeefcbecd352015-09-23 11:50:27 -0700264}
265
266TEST_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 Brandstetterf475d362016-01-08 15:35:57 -0800271 EXPECT_FALSE(transport_controller_->GetSslRole("video", &role));
272 EXPECT_TRUE(transport_controller_->GetSslRole("audio", &role));
deadbeefcbecd352015-09-23 11:50:27 -0700273 EXPECT_EQ(rtc::SSL_CLIENT, role);
274}
275
276TEST_F(TransportControllerTest, TestSetAndGetLocalCertificate) {
277 rtc::scoped_refptr<rtc::RTCCertificate> certificate1 =
jbauch555604a2016-04-26 03:13:22 -0700278 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
kwiberg0eb15ed2015-12-17 03:04:15 -0800279 rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT)));
deadbeefcbecd352015-09-23 11:50:27 -0700280 rtc::scoped_refptr<rtc::RTCCertificate> certificate2 =
jbauch555604a2016-04-26 03:13:22 -0700281 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
kwiberg0eb15ed2015-12-17 03:04:15 -0800282 rtc::SSLIdentity::Generate("session2", rtc::KT_DEFAULT)));
deadbeefcbecd352015-09-23 11:50:27 -0700283 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
310TEST_F(TransportControllerTest, TestGetRemoteSSLCertificate) {
311 rtc::FakeSSLCertificate fake_certificate("fake_data");
deadbeefcbecd352015-09-23 11:50:27 -0700312
313 FakeTransportChannel* channel = CreateChannel("audio", 1);
314 ASSERT_NE(nullptr, channel);
315
316 channel->SetRemoteSSLCertificate(&fake_certificate);
jbauch555604a2016-04-26 03:13:22 -0700317 std::unique_ptr<rtc::SSLCertificate> returned_certificate =
kwibergb4d01c42016-04-06 05:15:06 -0700318 transport_controller_->GetRemoteSSLCertificate("audio");
319 EXPECT_TRUE(returned_certificate);
deadbeefcbecd352015-09-23 11:50:27 -0700320 EXPECT_EQ(fake_certificate.ToPEMString(),
321 returned_certificate->ToPEMString());
322
323 // Should fail if called for a nonexistant transport.
kwibergb4d01c42016-04-06 05:15:06 -0700324 EXPECT_FALSE(transport_controller_->GetRemoteSSLCertificate("video"));
deadbeefcbecd352015-09-23 11:50:27 -0700325}
326
327TEST_F(TransportControllerTest, TestSetLocalTransportDescription) {
328 FakeTransportChannel* channel = CreateChannel("audio", 1);
329 ASSERT_NE(nullptr, channel);
deadbeef46eed762016-01-28 13:24:37 -0800330 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700331 kIcePwd1, ICEMODE_FULL,
332 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700333 std::string err;
334 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700335 "audio", local_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700336 // 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();
deadbeef91042f82016-07-15 17:48:13 -0700342 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700343 EXPECT_EQ(1, gathering_state_signal_count_);
344}
345
346TEST_F(TransportControllerTest, TestSetRemoteTransportDescription) {
347 FakeTransportChannel* channel = CreateChannel("audio", 1);
348 ASSERT_NE(nullptr, channel);
deadbeef46eed762016-01-28 13:24:37 -0800349 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700350 kIcePwd1, ICEMODE_FULL,
351 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700352 std::string err;
353 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700354 "audio", remote_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700355 // 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
360TEST_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
371TEST_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;
deadbeef46eed762016-01-28 13:24:37 -0800379 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700380 kIcePwd1, ICEMODE_FULL,
381 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700382 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700383 "audio", remote_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700384 EXPECT_FALSE(transport_controller_->ReadyForRemoteCandidates("audio"));
385
deadbeef46eed762016-01-28 13:24:37 -0800386 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag2,
deadbeef91042f82016-07-15 17:48:13 -0700387 kIcePwd2, ICEMODE_FULL,
388 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700389 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700390 "audio", local_desc, CA_ANSWER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700391 EXPECT_TRUE(transport_controller_->ReadyForRemoteCandidates("audio"));
392}
393
394TEST_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.
409TEST_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
429TEST_F(TransportControllerTest, TestSignalConnectionStateFailed) {
430 // Need controlling ICE role to get in failed state.
deadbeef91042f82016-07-15 17:48:13 -0700431 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
deadbeefcbecd352015-09-23 11:50:27 -0700432 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);
deadbeef91042f82016-07-15 17:48:13 -0700443 EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700444 EXPECT_EQ(1, connection_state_signal_count_);
445}
446
447TEST_F(TransportControllerTest, TestSignalConnectionStateConnected) {
deadbeef91042f82016-07-15 17:48:13 -0700448 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
deadbeefcbecd352015-09-23 11:50:27 -0700449 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);
deadbeef91042f82016-07-15 17:48:13 -0700464 EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700465 // 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);
deadbeef91042f82016-07-15 17:48:13 -0700470 EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700471 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);
deadbeef91042f82016-07-15 17:48:13 -0700476 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700477 EXPECT_EQ(3, connection_state_signal_count_);
478}
479
480TEST_F(TransportControllerTest, TestSignalConnectionStateComplete) {
deadbeef91042f82016-07-15 17:48:13 -0700481 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
deadbeefcbecd352015-09-23 11:50:27 -0700482 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);
deadbeef91042f82016-07-15 17:48:13 -0700497 EXPECT_EQ_WAIT(kIceConnectionFailed, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700498 // 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);
deadbeef91042f82016-07-15 17:48:13 -0700503 EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700504 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);
deadbeef91042f82016-07-15 17:48:13 -0700510 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700511 EXPECT_EQ(3, connection_state_signal_count_);
512
513 // Finally, transition to completed state.
514 channel2->SetConnectionCount(1);
deadbeef91042f82016-07-15 17:48:13 -0700515 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700516 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.
521TEST_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);
deadbeef91042f82016-07-15 17:48:13 -0700533 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700534 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);
deadbeef91042f82016-07-15 17:48:13 -0700540 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700541 // 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".
547TEST_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);
deadbeef91042f82016-07-15 17:48:13 -0700554 EXPECT_EQ_WAIT(kIceConnectionConnected, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700555 EXPECT_EQ(1, connection_state_signal_count_);
556
557 DestroyChannel("audio", 1);
deadbeef91042f82016-07-15 17:48:13 -0700558 EXPECT_EQ_WAIT(kIceConnectionConnecting, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700559 // Signal count of 2 means the deletion didn't cause any unexpected signals
560 EXPECT_EQ(2, connection_state_signal_count_);
561}
562
563TEST_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
581TEST_F(TransportControllerTest, TestSignalGatheringStateGathering) {
582 FakeTransportChannel* channel = CreateChannel("audio", 1);
583 ASSERT_NE(nullptr, channel);
deadbeefcbecd352015-09-23 11:50:27 -0700584 channel->MaybeStartGathering();
585 // Should be in the gathering state as soon as any transport starts gathering.
deadbeef91042f82016-07-15 17:48:13 -0700586 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700587 EXPECT_EQ(1, gathering_state_signal_count_);
588}
589
590TEST_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
deadbeefcbecd352015-09-23 11:50:27 -0700598 channel3->MaybeStartGathering();
deadbeef91042f82016-07-15 17:48:13 -0700599 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700600 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);
deadbeef91042f82016-07-15 17:48:13 -0700606 EXPECT_EQ_WAIT(kIceGatheringNew, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700607 EXPECT_EQ(2, gathering_state_signal_count_);
608
609 // Make remaining channels start and then finish gathering.
deadbeefcbecd352015-09-23 11:50:27 -0700610 channel1->MaybeStartGathering();
deadbeefcbecd352015-09-23 11:50:27 -0700611 channel2->MaybeStartGathering();
deadbeef91042f82016-07-15 17:48:13 -0700612 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700613 EXPECT_EQ(3, gathering_state_signal_count_);
614
615 channel1->SetCandidatesGatheringComplete();
616 channel2->SetCandidatesGatheringComplete();
deadbeef91042f82016-07-15 17:48:13 -0700617 EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700618 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.
625TEST_F(TransportControllerTest,
626 TestSignalingWhenLastIncompleteTransportDestroyed) {
deadbeef91042f82016-07-15 17:48:13 -0700627 transport_controller_->SetIceRole(ICEROLE_CONTROLLING);
deadbeefcbecd352015-09-23 11:50:27 -0700628 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();
deadbeef91042f82016-07-15 17:48:13 -0700634 EXPECT_EQ_WAIT(kIceGatheringGathering, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700635 EXPECT_EQ(1, gathering_state_signal_count_);
636
637 channel1->SetConnectionCount(1);
638 channel1->SetWritable(true);
639 DestroyChannel("video", 1);
deadbeef91042f82016-07-15 17:48:13 -0700640 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700641 EXPECT_EQ(1, connection_state_signal_count_);
deadbeef91042f82016-07-15 17:48:13 -0700642 EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700643 EXPECT_EQ(2, gathering_state_signal_count_);
644}
645
646TEST_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.
deadbeef46eed762016-01-28 13:24:37 -0800651 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1,
deadbeef91042f82016-07-15 17:48:13 -0700652 kIcePwd1, ICEMODE_FULL,
653 CONNECTIONROLE_ACTPASS, nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700654 std::string err;
655 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
deadbeef91042f82016-07-15 17:48:13 -0700656 "audio", local_desc, CA_OFFER, &err));
deadbeefcbecd352015-09-23 11:50:27 -0700657 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
664TEST_F(TransportControllerTest, TestSignalingOccursOnSignalingThread) {
johan27c3d5b2016-10-17 00:54:57 -0700665 CreateTransportControllerWithNetworkThread();
666 CreateChannelsAndCompleteConnectionOnNetworkThread();
deadbeefcbecd352015-09-23 11:50:27 -0700667
668 // connecting --> connected --> completed
deadbeef91042f82016-07-15 17:48:13 -0700669 EXPECT_EQ_WAIT(kIceConnectionCompleted, connection_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700670 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
deadbeef91042f82016-07-15 17:48:13 -0700676 EXPECT_EQ_WAIT(kIceGatheringComplete, gathering_state_, kTimeout);
deadbeefcbecd352015-09-23 11:50:27 -0700677 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}
deadbeef91042f82016-07-15 17:48:13 -0700685
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 Brandstetterf0bb3602016-08-26 20:59:24 -0700692TEST_F(TransportControllerTest, IceRoleRedeterminedOnIceRestartByDefault) {
deadbeef91042f82016-07-15 17:48:13 -0700693 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 Brandstetterf0bb3602016-08-26 20:59:24 -0700720// 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.
723TEST_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
deadbeef91042f82016-07-15 17:48:13 -0700752} // namespace cricket {