blob: 23e4dc80671d2e873f9e213afda28175e2113ce4 [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>
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
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";
31
32using cricket::Candidate;
33using cricket::Candidates;
34using cricket::FakeTransportChannel;
35using cricket::FakeTransportController;
36using cricket::IceConnectionState;
37using cricket::IceGatheringState;
38using cricket::TransportChannel;
39using cricket::TransportController;
40using cricket::TransportDescription;
41using 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.
49typedef FakeTransportController TransportControllerForTest;
50
51class 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
114 TransportDescription local_desc(
115 std::vector<std::string>(), kIceUfrag1, kIcePwd1, cricket::ICEMODE_FULL,
116 cricket::CONNECTIONROLE_ACTPASS, nullptr, Candidates());
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
honghaiz1f429e32015-09-28 07:57:34 -0700137 cricket::IceConfig CreateIceConfig(int receiving_timeout_ms,
138 bool gather_continually) {
139 cricket::IceConfig config;
140 config.receiving_timeout_ms = receiving_timeout_ms;
141 config.gather_continually = gather_continually;
142 return config;
143 }
144
deadbeefcbecd352015-09-23 11:50:27 -0700145 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
honghaiz1f429e32015-09-28 07:57:34 -0700200TEST_F(TransportControllerTest, TestSetIceConfig) {
deadbeefcbecd352015-09-23 11:50:27 -0700201 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
202 ASSERT_NE(nullptr, channel1);
203
honghaiz1f429e32015-09-28 07:57:34 -0700204 transport_controller_->SetIceConfig(CreateIceConfig(1000, true));
deadbeefcbecd352015-09-23 11:50:27 -0700205 EXPECT_EQ(1000, channel1->receiving_timeout());
honghaiz1f429e32015-09-28 07:57:34 -0700206 EXPECT_TRUE(channel1->gather_continually());
deadbeefcbecd352015-09-23 11:50:27 -0700207
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());
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
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.
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
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
260TEST_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;
265 EXPECT_TRUE(transport_controller_->GetSslRole(&role));
266 EXPECT_EQ(rtc::SSL_CLIENT, role);
267}
268
269TEST_F(TransportControllerTest, TestSetAndGetLocalCertificate) {
270 rtc::scoped_refptr<rtc::RTCCertificate> certificate1 =
271 rtc::RTCCertificate::Create(
272 rtc::scoped_ptr<rtc::SSLIdentity>(
273 rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT))
274 .Pass());
275 rtc::scoped_refptr<rtc::RTCCertificate> certificate2 =
276 rtc::RTCCertificate::Create(
277 rtc::scoped_ptr<rtc::SSLIdentity>(
278 rtc::SSLIdentity::Generate("session2", rtc::KT_DEFAULT))
279 .Pass());
280 rtc::scoped_refptr<rtc::RTCCertificate> returned_certificate;
281
282 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
283 ASSERT_NE(nullptr, channel1);
284
285 EXPECT_TRUE(transport_controller_->SetLocalCertificate(certificate1));
286 EXPECT_TRUE(transport_controller_->GetLocalCertificate(
287 "audio", &returned_certificate));
288 EXPECT_EQ(certificate1->identity()->certificate().ToPEMString(),
289 returned_certificate->identity()->certificate().ToPEMString());
290
291 // Should fail if called for a nonexistant transport.
292 EXPECT_FALSE(transport_controller_->GetLocalCertificate(
293 "video", &returned_certificate));
294
295 // Test that identity stored in controller is applied to new channels.
296 FakeTransportChannel* channel2 = CreateChannel("video", 1);
297 ASSERT_NE(nullptr, channel2);
298 EXPECT_TRUE(transport_controller_->GetLocalCertificate(
299 "video", &returned_certificate));
300 EXPECT_EQ(certificate1->identity()->certificate().ToPEMString(),
301 returned_certificate->identity()->certificate().ToPEMString());
302
303 // Shouldn't be able to change the identity once set.
304 EXPECT_FALSE(transport_controller_->SetLocalCertificate(certificate2));
305}
306
307TEST_F(TransportControllerTest, TestGetRemoteSSLCertificate) {
308 rtc::FakeSSLCertificate fake_certificate("fake_data");
309 rtc::scoped_ptr<rtc::SSLCertificate> returned_certificate;
310
311 FakeTransportChannel* channel = CreateChannel("audio", 1);
312 ASSERT_NE(nullptr, channel);
313
314 channel->SetRemoteSSLCertificate(&fake_certificate);
315 EXPECT_TRUE(transport_controller_->GetRemoteSSLCertificate(
316 "audio", returned_certificate.accept()));
317 EXPECT_EQ(fake_certificate.ToPEMString(),
318 returned_certificate->ToPEMString());
319
320 // Should fail if called for a nonexistant transport.
321 EXPECT_FALSE(transport_controller_->GetRemoteSSLCertificate(
322 "video", returned_certificate.accept()));
323}
324
325TEST_F(TransportControllerTest, TestSetLocalTransportDescription) {
326 FakeTransportChannel* channel = CreateChannel("audio", 1);
327 ASSERT_NE(nullptr, channel);
328 TransportDescription local_desc(
329 std::vector<std::string>(), kIceUfrag1, kIcePwd1, cricket::ICEMODE_FULL,
330 cricket::CONNECTIONROLE_ACTPASS, nullptr, Candidates());
331 std::string err;
332 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
333 "audio", local_desc, cricket::CA_OFFER, &err));
334 // Check that ICE ufrag and pwd were propagated to channel.
335 EXPECT_EQ(kIceUfrag1, channel->ice_ufrag());
336 EXPECT_EQ(kIcePwd1, channel->ice_pwd());
337 // After setting local description, we should be able to start gathering
338 // candidates.
339 transport_controller_->MaybeStartGathering();
340 EXPECT_EQ_WAIT(cricket::kIceGatheringGathering, gathering_state_, kTimeout);
341 EXPECT_EQ(1, gathering_state_signal_count_);
342}
343
344TEST_F(TransportControllerTest, TestSetRemoteTransportDescription) {
345 FakeTransportChannel* channel = CreateChannel("audio", 1);
346 ASSERT_NE(nullptr, channel);
347 TransportDescription remote_desc(
348 std::vector<std::string>(), kIceUfrag1, kIcePwd1, cricket::ICEMODE_FULL,
349 cricket::CONNECTIONROLE_ACTPASS, nullptr, Candidates());
350 std::string err;
351 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
352 "audio", remote_desc, cricket::CA_OFFER, &err));
353 // Check that ICE ufrag and pwd were propagated to channel.
354 EXPECT_EQ(kIceUfrag1, channel->remote_ice_ufrag());
355 EXPECT_EQ(kIcePwd1, channel->remote_ice_pwd());
356}
357
358TEST_F(TransportControllerTest, TestAddRemoteCandidates) {
359 FakeTransportChannel* channel = CreateChannel("audio", 1);
360 ASSERT_NE(nullptr, channel);
361 Candidates candidates;
362 candidates.push_back(CreateCandidate(1));
363 std::string err;
364 EXPECT_TRUE(
365 transport_controller_->AddRemoteCandidates("audio", candidates, &err));
366 EXPECT_EQ(1U, channel->remote_candidates().size());
367}
368
369TEST_F(TransportControllerTest, TestReadyForRemoteCandidates) {
370 FakeTransportChannel* channel = CreateChannel("audio", 1);
371 ASSERT_NE(nullptr, channel);
372 // We expect to be ready for remote candidates only after local and remote
373 // descriptions are set.
374 EXPECT_FALSE(transport_controller_->ReadyForRemoteCandidates("audio"));
375
376 std::string err;
377 TransportDescription remote_desc(
378 std::vector<std::string>(), kIceUfrag1, kIcePwd1, cricket::ICEMODE_FULL,
379 cricket::CONNECTIONROLE_ACTPASS, nullptr, Candidates());
380 EXPECT_TRUE(transport_controller_->SetRemoteTransportDescription(
381 "audio", remote_desc, cricket::CA_OFFER, &err));
382 EXPECT_FALSE(transport_controller_->ReadyForRemoteCandidates("audio"));
383
384 TransportDescription local_desc(
385 std::vector<std::string>(), kIceUfrag2, kIcePwd2, cricket::ICEMODE_FULL,
386 cricket::CONNECTIONROLE_ACTPASS, nullptr, Candidates());
387 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
388 "audio", local_desc, cricket::CA_ANSWER, &err));
389 EXPECT_TRUE(transport_controller_->ReadyForRemoteCandidates("audio"));
390}
391
392TEST_F(TransportControllerTest, TestGetStats) {
393 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
394 ASSERT_NE(nullptr, channel1);
395 FakeTransportChannel* channel2 = CreateChannel("audio", 2);
396 ASSERT_NE(nullptr, channel2);
397 FakeTransportChannel* channel3 = CreateChannel("video", 1);
398 ASSERT_NE(nullptr, channel3);
399
400 TransportStats stats;
401 EXPECT_TRUE(transport_controller_->GetStats("audio", &stats));
402 EXPECT_EQ("audio", stats.transport_name);
403 EXPECT_EQ(2U, stats.channel_stats.size());
404}
405
406// Test that transport gets destroyed when it has no more channels.
407TEST_F(TransportControllerTest, TestCreateAndDestroyChannel) {
408 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
409 ASSERT_NE(nullptr, channel1);
410 FakeTransportChannel* channel2 = CreateChannel("audio", 1);
411 ASSERT_NE(nullptr, channel2);
412 ASSERT_EQ(channel1, channel2);
413 FakeTransportChannel* channel3 = CreateChannel("audio", 2);
414 ASSERT_NE(nullptr, channel3);
415
416 // Using GetStats to check if transport is destroyed from an outside class's
417 // perspective.
418 TransportStats stats;
419 EXPECT_TRUE(transport_controller_->GetStats("audio", &stats));
420 DestroyChannel("audio", 2);
421 DestroyChannel("audio", 1);
422 EXPECT_TRUE(transport_controller_->GetStats("audio", &stats));
423 DestroyChannel("audio", 1);
424 EXPECT_FALSE(transport_controller_->GetStats("audio", &stats));
425}
426
427TEST_F(TransportControllerTest, TestSignalConnectionStateFailed) {
428 // Need controlling ICE role to get in failed state.
429 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
430 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
431 ASSERT_NE(nullptr, channel1);
432 FakeTransportChannel* channel2 = CreateChannel("video", 1);
433 ASSERT_NE(nullptr, channel2);
434
435 // Should signal "failed" if any channel failed; channel is considered failed
436 // if it previously had a connection but now has none, and gathering is
437 // complete.
438 channel1->SetCandidatesGatheringComplete();
439 channel1->SetConnectionCount(1);
440 channel1->SetConnectionCount(0);
441 EXPECT_EQ_WAIT(cricket::kIceConnectionFailed, connection_state_, kTimeout);
442 EXPECT_EQ(1, connection_state_signal_count_);
443}
444
445TEST_F(TransportControllerTest, TestSignalConnectionStateConnected) {
446 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
447 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
448 ASSERT_NE(nullptr, channel1);
449 FakeTransportChannel* channel2 = CreateChannel("video", 1);
450 ASSERT_NE(nullptr, channel2);
451 FakeTransportChannel* channel3 = CreateChannel("video", 2);
452 ASSERT_NE(nullptr, channel3);
453
454 // First, have one channel connect, and another fail, to ensure that
455 // the first channel connecting didn't trigger a "connected" state signal.
456 // We should only get a signal when all are connected.
457 channel1->SetConnectionCount(2);
458 channel1->SetWritable(true);
459 channel3->SetCandidatesGatheringComplete();
460 channel3->SetConnectionCount(1);
461 channel3->SetConnectionCount(0);
462 EXPECT_EQ_WAIT(cricket::kIceConnectionFailed, connection_state_, kTimeout);
463 // Signal count of 1 means that the only signal emitted was "failed".
464 EXPECT_EQ(1, connection_state_signal_count_);
465
466 // Destroy the failed channel to return to "connecting" state.
467 DestroyChannel("video", 2);
468 EXPECT_EQ_WAIT(cricket::kIceConnectionConnecting, connection_state_,
469 kTimeout);
470 EXPECT_EQ(2, connection_state_signal_count_);
471
472 // Make the remaining channel reach a connected state.
473 channel2->SetConnectionCount(2);
474 channel2->SetWritable(true);
475 EXPECT_EQ_WAIT(cricket::kIceConnectionConnected, connection_state_, kTimeout);
476 EXPECT_EQ(3, connection_state_signal_count_);
477}
478
479TEST_F(TransportControllerTest, TestSignalConnectionStateComplete) {
480 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
481 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
482 ASSERT_NE(nullptr, channel1);
483 FakeTransportChannel* channel2 = CreateChannel("video", 1);
484 ASSERT_NE(nullptr, channel2);
485 FakeTransportChannel* channel3 = CreateChannel("video", 2);
486 ASSERT_NE(nullptr, channel3);
487
488 // Similar to above test, but we're now reaching the completed state, which
489 // means only one connection per FakeTransportChannel.
490 channel1->SetCandidatesGatheringComplete();
491 channel1->SetConnectionCount(1);
492 channel1->SetWritable(true);
493 channel3->SetCandidatesGatheringComplete();
494 channel3->SetConnectionCount(1);
495 channel3->SetConnectionCount(0);
496 EXPECT_EQ_WAIT(cricket::kIceConnectionFailed, connection_state_, kTimeout);
497 // Signal count of 1 means that the only signal emitted was "failed".
498 EXPECT_EQ(1, connection_state_signal_count_);
499
500 // Destroy the failed channel to return to "connecting" state.
501 DestroyChannel("video", 2);
502 EXPECT_EQ_WAIT(cricket::kIceConnectionConnecting, connection_state_,
503 kTimeout);
504 EXPECT_EQ(2, connection_state_signal_count_);
505
506 // Make the remaining channel reach a connected state.
507 channel2->SetCandidatesGatheringComplete();
508 channel2->SetConnectionCount(2);
509 channel2->SetWritable(true);
510 EXPECT_EQ_WAIT(cricket::kIceConnectionConnected, connection_state_, kTimeout);
511 EXPECT_EQ(3, connection_state_signal_count_);
512
513 // Finally, transition to completed state.
514 channel2->SetConnectionCount(1);
515 EXPECT_EQ_WAIT(cricket::kIceConnectionCompleted, connection_state_, kTimeout);
516 EXPECT_EQ(4, connection_state_signal_count_);
517}
518
519// Make sure that if we're "connected" and remove a transport, we stay in the
520// "connected" state.
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);
533 EXPECT_EQ_WAIT(cricket::kIceConnectionConnected, connection_state_, kTimeout);
534 EXPECT_EQ(1, connection_state_signal_count_);
535
536 // Destroy one channel, then "complete" the other one, so we reach
537 // a known state.
538 DestroyChannel("video", 1);
539 channel1->SetConnectionCount(1);
540 EXPECT_EQ_WAIT(cricket::kIceConnectionCompleted, connection_state_, kTimeout);
541 // Signal count of 2 means the deletion didn't cause any unexpected signals
542 EXPECT_EQ(2, connection_state_signal_count_);
543}
544
545// If we destroy the last/only transport, we should simply transition to
546// "connecting".
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);
554 EXPECT_EQ_WAIT(cricket::kIceConnectionConnected, connection_state_, kTimeout);
555 EXPECT_EQ(1, connection_state_signal_count_);
556
557 DestroyChannel("audio", 1);
558 EXPECT_EQ_WAIT(cricket::kIceConnectionConnecting, connection_state_,
559 kTimeout);
560 // 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) {
565 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
566 ASSERT_NE(nullptr, channel1);
567 FakeTransportChannel* channel2 = CreateChannel("video", 1);
568 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) {
583 FakeTransportChannel* channel = CreateChannel("audio", 1);
584 ASSERT_NE(nullptr, channel);
585 channel->Connect();
586 channel->MaybeStartGathering();
587 // Should be in the gathering state as soon as any transport starts gathering.
588 EXPECT_EQ_WAIT(cricket::kIceGatheringGathering, gathering_state_, kTimeout);
589 EXPECT_EQ(1, gathering_state_signal_count_);
590}
591
592TEST_F(TransportControllerTest, TestSignalGatheringStateComplete) {
593 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
594 ASSERT_NE(nullptr, channel1);
595 FakeTransportChannel* channel2 = CreateChannel("video", 1);
596 ASSERT_NE(nullptr, channel2);
597 FakeTransportChannel* channel3 = CreateChannel("data", 1);
598 ASSERT_NE(nullptr, channel3);
599
600 channel3->Connect();
601 channel3->MaybeStartGathering();
602 EXPECT_EQ_WAIT(cricket::kIceGatheringGathering, gathering_state_, kTimeout);
603 EXPECT_EQ(1, gathering_state_signal_count_);
604
605 // Have one channel finish gathering, then destroy it, to make sure gathering
606 // completion wasn't signalled if only one transport finished gathering.
607 channel3->SetCandidatesGatheringComplete();
608 DestroyChannel("data", 1);
609 EXPECT_EQ_WAIT(cricket::kIceGatheringNew, gathering_state_, kTimeout);
610 EXPECT_EQ(2, gathering_state_signal_count_);
611
612 // Make remaining channels start and then finish gathering.
613 channel1->Connect();
614 channel1->MaybeStartGathering();
615 channel2->Connect();
616 channel2->MaybeStartGathering();
617 EXPECT_EQ_WAIT(cricket::kIceGatheringGathering, gathering_state_, kTimeout);
618 EXPECT_EQ(3, gathering_state_signal_count_);
619
620 channel1->SetCandidatesGatheringComplete();
621 channel2->SetCandidatesGatheringComplete();
622 EXPECT_EQ_WAIT(cricket::kIceGatheringComplete, gathering_state_, kTimeout);
623 EXPECT_EQ(4, gathering_state_signal_count_);
624}
625
626// Test that when the last transport that hasn't finished connecting and/or
627// gathering is destroyed, the aggregate state jumps to "completed". This can
628// happen if, for example, we have an audio and video transport, the audio
629// transport completes, then we start bundling video on the audio transport.
630TEST_F(TransportControllerTest,
631 TestSignalingWhenLastIncompleteTransportDestroyed) {
632 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
633 FakeTransportChannel* channel1 = CreateChannel("audio", 1);
634 ASSERT_NE(nullptr, channel1);
635 FakeTransportChannel* channel2 = CreateChannel("video", 1);
636 ASSERT_NE(nullptr, channel2);
637
638 channel1->SetCandidatesGatheringComplete();
639 EXPECT_EQ_WAIT(cricket::kIceGatheringGathering, gathering_state_, kTimeout);
640 EXPECT_EQ(1, gathering_state_signal_count_);
641
642 channel1->SetConnectionCount(1);
643 channel1->SetWritable(true);
644 DestroyChannel("video", 1);
645 EXPECT_EQ_WAIT(cricket::kIceConnectionCompleted, connection_state_, kTimeout);
646 EXPECT_EQ(1, connection_state_signal_count_);
647 EXPECT_EQ_WAIT(cricket::kIceGatheringComplete, gathering_state_, kTimeout);
648 EXPECT_EQ(2, gathering_state_signal_count_);
649}
650
651TEST_F(TransportControllerTest, TestSignalCandidatesGathered) {
652 FakeTransportChannel* channel = CreateChannel("audio", 1);
653 ASSERT_NE(nullptr, channel);
654
655 // Transport won't signal candidates until it has a local description.
656 TransportDescription local_desc(
657 std::vector<std::string>(), kIceUfrag1, kIcePwd1, cricket::ICEMODE_FULL,
658 cricket::CONNECTIONROLE_ACTPASS, nullptr, Candidates());
659 std::string err;
660 EXPECT_TRUE(transport_controller_->SetLocalTransportDescription(
661 "audio", local_desc, cricket::CA_OFFER, &err));
662 transport_controller_->MaybeStartGathering();
663
664 channel->SignalCandidateGathered(channel, CreateCandidate(1));
665 EXPECT_EQ_WAIT(1, candidates_signal_count_, kTimeout);
666 EXPECT_EQ(1U, candidates_["audio"].size());
667}
668
669TEST_F(TransportControllerTest, TestSignalingOccursOnSignalingThread) {
670 CreateTransportControllerWithWorkerThread();
671 CreateChannelsAndCompleteConnectionOnWorkerThread();
672
673 // connecting --> connected --> completed
674 EXPECT_EQ_WAIT(cricket::kIceConnectionCompleted, connection_state_, kTimeout);
675 EXPECT_EQ(2, connection_state_signal_count_);
676
677 EXPECT_TRUE_WAIT(receiving_, kTimeout);
678 EXPECT_EQ(1, receiving_signal_count_);
679
680 // new --> gathering --> complete
681 EXPECT_EQ_WAIT(cricket::kIceGatheringComplete, gathering_state_, kTimeout);
682 EXPECT_EQ(2, gathering_state_signal_count_);
683
684 EXPECT_EQ_WAIT(1U, candidates_["audio"].size(), kTimeout);
685 EXPECT_EQ_WAIT(1U, candidates_["video"].size(), kTimeout);
686 EXPECT_EQ(2, candidates_signal_count_);
687
688 EXPECT_TRUE(!signaled_on_non_signaling_thread_);
689}