Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: webrtc/p2p/base/p2ptransportchannel_unittest.cc

Issue 2099563004: Start ICE connectivity checks as soon as the first pair is pingable. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Removing debug log message, adding missing UpdateState. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/p2p/base/p2ptransportchannel.cc ('k') | webrtc/p2p/base/port.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 channel->SignalReadPacket.connect( 348 channel->SignalReadPacket.connect(
349 this, &P2PTransportChannelTestBase::OnReadPacket); 349 this, &P2PTransportChannelTestBase::OnReadPacket);
350 channel->SignalRoleConflict.connect( 350 channel->SignalRoleConflict.connect(
351 this, &P2PTransportChannelTestBase::OnRoleConflict); 351 this, &P2PTransportChannelTestBase::OnRoleConflict);
352 channel->SetIceCredentials(local_ice_ufrag, local_ice_pwd); 352 channel->SetIceCredentials(local_ice_ufrag, local_ice_pwd);
353 if (remote_ice_credential_source_ == FROM_SETICECREDENTIALS) { 353 if (remote_ice_credential_source_ == FROM_SETICECREDENTIALS) {
354 channel->SetRemoteIceCredentials(remote_ice_ufrag, remote_ice_pwd); 354 channel->SetRemoteIceCredentials(remote_ice_ufrag, remote_ice_pwd);
355 } 355 }
356 channel->SetIceRole(GetEndpoint(endpoint)->ice_role()); 356 channel->SetIceRole(GetEndpoint(endpoint)->ice_role());
357 channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker()); 357 channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker());
358 channel->Connect();
359 return channel; 358 return channel;
360 } 359 }
361 void DestroyChannels() { 360 void DestroyChannels() {
362 ep1_.cd1_.ch_.reset(); 361 ep1_.cd1_.ch_.reset();
363 ep2_.cd1_.ch_.reset(); 362 ep2_.cd1_.ch_.reset();
364 ep1_.cd2_.ch_.reset(); 363 ep1_.cd2_.ch_.reset();
365 ep2_.cd2_.ch_.reset(); 364 ep2_.cd2_.ch_.reset();
366 } 365 }
367 P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); } 366 P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); }
368 P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); } 367 P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); }
369 P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); } 368 P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); }
370 P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); } 369 P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); }
371 370
372 TestTurnServer* test_turn_server() { return &turn_server_; } 371 TestTurnServer* test_turn_server() { return &turn_server_; }
372 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
373 373
374 // Common results. 374 // Common results.
375 static const Result kLocalUdpToLocalUdp; 375 static const Result kLocalUdpToLocalUdp;
376 static const Result kLocalUdpToStunUdp; 376 static const Result kLocalUdpToStunUdp;
377 static const Result kLocalUdpToPrflxUdp; 377 static const Result kLocalUdpToPrflxUdp;
378 static const Result kPrflxUdpToLocalUdp; 378 static const Result kPrflxUdpToLocalUdp;
379 static const Result kStunUdpToLocalUdp; 379 static const Result kStunUdpToLocalUdp;
380 static const Result kStunUdpToStunUdp; 380 static const Result kStunUdpToStunUdp;
381 static const Result kPrflxUdpToStunUdp; 381 static const Result kPrflxUdpToStunUdp;
382 static const Result kLocalUdpToRelayUdp; 382 static const Result kLocalUdpToRelayUdp;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 EXPECT_EQ(expected.local_type, local_type); 480 EXPECT_EQ(expected.local_type, local_type);
481 EXPECT_EQ(expected.remote_type, remote_type); 481 EXPECT_EQ(expected.remote_type, remote_type);
482 EXPECT_EQ(expected.local_proto, local_proto); 482 EXPECT_EQ(expected.local_proto, local_proto);
483 EXPECT_EQ(expected.remote_proto, remote_proto); 483 EXPECT_EQ(expected.remote_proto, remote_proto);
484 } 484 }
485 485
486 // Return true if the approprite parts of the expected Result, based 486 // Return true if the approprite parts of the expected Result, based
487 // on the local and remote candidate of ep2_ch1, match. This can be 487 // on the local and remote candidate of ep2_ch1, match. This can be
488 // used in an EXPECT_TRUE_WAIT. 488 // used in an EXPECT_TRUE_WAIT.
489 bool CheckCandidate2(const Result& expected) { 489 bool CheckCandidate2(const Result& expected) {
490 const std::string& local_type = LocalCandidate(ep2_ch1())->type();
491 // const std::string& remote_type = RemoteCandidate(ep2_ch1())->type();
492 const std::string& local_proto = LocalCandidate(ep2_ch1())->protocol(); 490 const std::string& local_proto = LocalCandidate(ep2_ch1())->protocol();
493 const std::string& remote_proto = RemoteCandidate(ep2_ch1())->protocol(); 491 const std::string& remote_proto = RemoteCandidate(ep2_ch1())->protocol();
494 // Removed remote_type comparision aginst selected connection remote 492 // Removed remote_type comparision aginst selected connection remote
495 // candidate. This is done to handle remote type discrepancy from 493 // candidate. This is done to handle remote type discrepancy from
496 // local to stun based on the test type. 494 // local to stun based on the test type.
497 // For example in case of Open -> NAT, ep2 channels will have LULU 495 // For example in case of Open -> NAT, ep2 channels will have LULU
498 // and in other cases like NAT -> NAT it will be LUSU. To avoid these 496 // and in other cases like NAT -> NAT it will be LUSU. To avoid these
499 // mismatches and we are doing comparision in different way. 497 // mismatches and we are doing comparision in different way.
500 // i.e. when don't match its remote type is either local or stun. 498 // i.e. when don't match its remote type is either local or stun.
499 //
500 // Update(deadbeef): Also had to remove local type comparison. There
501 // is currently an issue where the local type is not updated to stun.
502 // So one side may see local<->relay while the other sees relay<->stun.
503 // This mean the other side may prioritize prflx<->relay, and won't have
504 // a local type of relay no matter how long we wait.
505 // TODO(deadbeef): Fix this!! It's causing us to have very sparse test
506 // coverage and is a very real bug.
507 //
501 // TODO(ronghuawu): Refine the test criteria. 508 // TODO(ronghuawu): Refine the test criteria.
502 // https://code.google.com/p/webrtc/issues/detail?id=1953 509 // https://code.google.com/p/webrtc/issues/detail?id=1953
503 return ((local_proto == expected.local_proto2 && 510 return ((local_proto == expected.local_proto2 &&
504 remote_proto == expected.remote_proto2) && 511 remote_proto == expected.remote_proto2));
505 (local_type == expected.local_type2 ||
506 // Sometimes we expect local -> prflx or prflx -> local
507 // and instead get prflx -> local or local -> prflx, and
508 // that's OK.
509 (IsLocalToPrflxOrTheReverse(expected) &&
510 local_type == expected.remote_type2)));
511 } 512 }
512 513
513 // EXPECT_EQ on the approprite parts of the expected Result, based 514 // EXPECT_EQ on the approprite parts of the expected Result, based
514 // on the local and remote candidate of ep2_ch1. This is like 515 // on the local and remote candidate of ep2_ch1. This is like
515 // CheckCandidate2, except that it will provide more detail about 516 // CheckCandidate2, except that it will provide more detail about
516 // what didn't match. 517 // what didn't match.
517 void ExpectCandidate2(const Result& expected) { 518 void ExpectCandidate2(const Result& expected) {
518 if (CheckCandidate2(expected)) { 519 if (CheckCandidate2(expected)) {
519 return; 520 return;
520 } 521 }
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 FLAKY_P2P_TEST(x, NAT_PORT_RESTRICTED) \ 1056 FLAKY_P2P_TEST(x, NAT_PORT_RESTRICTED) \
1056 P2P_TEST(x, NAT_SYMMETRIC) \ 1057 P2P_TEST(x, NAT_SYMMETRIC) \
1057 FLAKY_P2P_TEST(x, NAT_DOUBLE_CONE) \ 1058 FLAKY_P2P_TEST(x, NAT_DOUBLE_CONE) \
1058 P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \ 1059 P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \
1059 P2P_TEST(x, BLOCK_UDP) \ 1060 P2P_TEST(x, BLOCK_UDP) \
1060 P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \ 1061 P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \
1061 P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \ 1062 P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \
1062 P2P_TEST(x, PROXY_HTTPS) \ 1063 P2P_TEST(x, PROXY_HTTPS) \
1063 P2P_TEST(x, PROXY_SOCKS) 1064 P2P_TEST(x, PROXY_SOCKS)
1064 1065
1065 #define FLAKY_P2P_TEST_SET(x) \
1066 P2P_TEST(x, OPEN) \
1067 P2P_TEST(x, NAT_FULL_CONE) \
1068 P2P_TEST(x, NAT_ADDR_RESTRICTED) \
1069 P2P_TEST(x, NAT_PORT_RESTRICTED) \
1070 P2P_TEST(x, NAT_SYMMETRIC) \
1071 P2P_TEST(x, NAT_DOUBLE_CONE) \
1072 P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \
1073 P2P_TEST(x, BLOCK_UDP) \
1074 P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \
1075 P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \
1076 P2P_TEST(x, PROXY_HTTPS) \
1077 P2P_TEST(x, PROXY_SOCKS)
1078
1079 P2P_TEST_SET(OPEN) 1066 P2P_TEST_SET(OPEN)
1080 P2P_TEST_SET(NAT_FULL_CONE) 1067 P2P_TEST_SET(NAT_FULL_CONE)
1081 P2P_TEST_SET(NAT_ADDR_RESTRICTED) 1068 P2P_TEST_SET(NAT_ADDR_RESTRICTED)
1082 P2P_TEST_SET(NAT_PORT_RESTRICTED) 1069 P2P_TEST_SET(NAT_PORT_RESTRICTED)
1083 P2P_TEST_SET(NAT_SYMMETRIC) 1070 P2P_TEST_SET(NAT_SYMMETRIC)
1084 P2P_TEST_SET(NAT_DOUBLE_CONE) 1071 P2P_TEST_SET(NAT_DOUBLE_CONE)
1085 P2P_TEST_SET(NAT_SYMMETRIC_THEN_CONE) 1072 P2P_TEST_SET(NAT_SYMMETRIC_THEN_CONE)
1086 P2P_TEST_SET(BLOCK_UDP) 1073 P2P_TEST_SET(BLOCK_UDP)
1087 P2P_TEST_SET(BLOCK_UDP_AND_INCOMING_TCP) 1074 P2P_TEST_SET(BLOCK_UDP_AND_INCOMING_TCP)
1088 P2P_TEST_SET(BLOCK_ALL_BUT_OUTGOING_HTTP) 1075 P2P_TEST_SET(BLOCK_ALL_BUT_OUTGOING_HTTP)
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
1443 EXPECT_EQ(ICEROLE_CONTROLLING, ports_before[i]->GetIceRole()); 1430 EXPECT_EQ(ICEROLE_CONTROLLING, ports_before[i]->GetIceRole());
1444 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker()); 1431 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker());
1445 } 1432 }
1446 1433
1447 ep1_ch1()->SetIceRole(ICEROLE_CONTROLLED); 1434 ep1_ch1()->SetIceRole(ICEROLE_CONTROLLED);
1448 ep1_ch1()->SetIceTiebreaker(kTiebreaker2); 1435 ep1_ch1()->SetIceTiebreaker(kTiebreaker2);
1449 1436
1450 const std::vector<PortInterface*> ports_after = ep1_ch1()->ports(); 1437 const std::vector<PortInterface*> ports_after = ep1_ch1()->ports();
1451 for (size_t i = 0; i < ports_after.size(); ++i) { 1438 for (size_t i = 0; i < ports_after.size(); ++i) {
1452 EXPECT_EQ(ICEROLE_CONTROLLED, ports_before[i]->GetIceRole()); 1439 EXPECT_EQ(ICEROLE_CONTROLLED, ports_before[i]->GetIceRole());
1453 // SetIceTiebreaker after Connect() has been called will fail. So expect the 1440 // SetIceTiebreaker after ports have been created will fail. So expect the
1454 // original value. 1441 // original value.
1455 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker()); 1442 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker());
1456 } 1443 }
1457 1444
1458 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && 1445 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() &&
1459 ep1_ch1()->writable() && 1446 ep1_ch1()->writable() &&
1460 ep2_ch1()->receiving() && 1447 ep2_ch1()->receiving() &&
1461 ep2_ch1()->writable(), 1448 ep2_ch1()->writable(),
1462 1000); 1449 1000);
1463 1450
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
1701 // Also make sure we can immediately send packets. 1688 // Also make sure we can immediately send packets.
1702 const char* data = "test"; 1689 const char* data = "test";
1703 int len = static_cast<int>(strlen(data)); 1690 int len = static_cast<int>(strlen(data));
1704 EXPECT_EQ(len, SendData(ep1_ch1(), data, len)); 1691 EXPECT_EQ(len, SendData(ep1_ch1(), data, len));
1705 } 1692 }
1706 1693
1707 // Test that a TURN/peer reflexive candidate pair is also presumed writable. 1694 // Test that a TURN/peer reflexive candidate pair is also presumed writable.
1708 TEST_F(P2PTransportChannelTest, TurnToPrflxPresumedWritable) { 1695 TEST_F(P2PTransportChannelTest, TurnToPrflxPresumedWritable) {
1709 rtc::ScopedFakeClock fake_clock; 1696 rtc::ScopedFakeClock fake_clock;
1710 1697
1698 // We need to add artificial network delay to verify that the connection
1699 // is presumed writable before it's actually writable. Without this delay
1700 // it would become writable instantly.
1701 virtual_socket_server()->set_delay_mean(50);
1702 virtual_socket_server()->UpdateDelayDistribution();
1703
1711 ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, 1704 ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags,
1712 kDefaultPortAllocatorFlags); 1705 kDefaultPortAllocatorFlags);
1713 // We want the remote TURN candidate to show up as prflx. To do this we need 1706 // We want the remote TURN candidate to show up as prflx. To do this we need
1714 // to configure the server to accept packets from an address we haven't 1707 // to configure the server to accept packets from an address we haven't
1715 // explicitly installed permission for. 1708 // explicitly installed permission for.
1716 test_turn_server()->set_enable_permission_checks(false); 1709 test_turn_server()->set_enable_permission_checks(false);
1717 IceConfig config; 1710 IceConfig config;
1718 config.presume_writable_when_fully_relayed = true; 1711 config.presume_writable_when_fully_relayed = true;
1719 GetEndpoint(0)->cd1_.ch_.reset( 1712 GetEndpoint(0)->cd1_.ch_.reset(
1720 CreateChannel(0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceUfrag[0], 1713 CreateChannel(0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceUfrag[0],
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after
2289 int selected_candidate_pair_switches_ = 0; 2282 int selected_candidate_pair_switches_ = 0;
2290 int last_sent_packet_id_ = -1; 2283 int last_sent_packet_id_ = -1;
2291 bool channel_ready_to_send_ = false; 2284 bool channel_ready_to_send_ = false;
2292 TransportChannelState channel_state_ = STATE_INIT; 2285 TransportChannelState channel_state_ = STATE_INIT;
2293 }; 2286 };
2294 2287
2295 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { 2288 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) {
2296 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2289 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2297 P2PTransportChannel ch("trigger checks", 1, &pa); 2290 P2PTransportChannel ch("trigger checks", 1, &pa);
2298 PrepareChannel(&ch); 2291 PrepareChannel(&ch);
2299 ch.Connect();
2300 ch.MaybeStartGathering(); 2292 ch.MaybeStartGathering();
2301 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2293 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2302 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); 2294 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2));
2303 2295
2304 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2296 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2305 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2297 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2306 ASSERT_TRUE(conn1 != nullptr); 2298 ASSERT_TRUE(conn1 != nullptr);
2307 ASSERT_TRUE(conn2 != nullptr); 2299 ASSERT_TRUE(conn2 != nullptr);
2308 2300
2309 // Before a triggered check, the first connection to ping is the 2301 // Before a triggered check, the first connection to ping is the
2310 // highest priority one. 2302 // highest priority one.
2311 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); 2303 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch));
2312 2304
2313 // Receiving a ping causes a triggered check which should make conn1 2305 // Receiving a ping causes a triggered check which should make conn1
2314 // be pinged first instead of conn2, even though conn2 has a higher 2306 // be pinged first instead of conn2, even though conn2 has a higher
2315 // priority. 2307 // priority.
2316 conn1->ReceivedPing(); 2308 conn1->ReceivedPing();
2317 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); 2309 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch));
2318 } 2310 }
2319 2311
2320 TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) { 2312 TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) {
2321 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2313 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2322 P2PTransportChannel ch("ping sufficiently", 1, &pa); 2314 P2PTransportChannel ch("ping sufficiently", 1, &pa);
2323 PrepareChannel(&ch); 2315 PrepareChannel(&ch);
2324 ch.Connect();
2325 ch.MaybeStartGathering(); 2316 ch.MaybeStartGathering();
2326 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2317 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2327 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); 2318 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2));
2328 2319
2329 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2320 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2330 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2321 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2331 ASSERT_TRUE(conn1 != nullptr); 2322 ASSERT_TRUE(conn1 != nullptr);
2332 ASSERT_TRUE(conn2 != nullptr); 2323 ASSERT_TRUE(conn2 != nullptr);
2333 2324
2334 // Low-priority connection becomes writable so that the other connection 2325 // Low-priority connection becomes writable so that the other connection
2335 // is not pruned. 2326 // is not pruned.
2336 conn1->ReceivedPingResponse(LOW_RTT); 2327 conn1->ReceivedPingResponse(LOW_RTT);
2337 EXPECT_TRUE_WAIT( 2328 EXPECT_TRUE_WAIT(
2338 conn1->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL && 2329 conn1->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL &&
2339 conn2->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, 2330 conn2->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL,
2340 kDefaultTimeout); 2331 kDefaultTimeout);
2341 } 2332 }
2342 2333
2343 // Verify that the connections are pinged at the right time. 2334 // Verify that the connections are pinged at the right time.
2344 TEST_F(P2PTransportChannelPingTest, TestStunPingIntervals) { 2335 TEST_F(P2PTransportChannelPingTest, TestStunPingIntervals) {
2345 rtc::ScopedFakeClock clock; 2336 rtc::ScopedFakeClock clock;
2346 int RTT_RATIO = 4; 2337 int RTT_RATIO = 4;
2347 int SCHEDULING_RANGE = 200; 2338 int SCHEDULING_RANGE = 200;
2348 int RTT_RANGE = 10; 2339 int RTT_RANGE = 10;
2349 2340
2350 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2341 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2351 P2PTransportChannel ch("TestChannel", 1, &pa); 2342 P2PTransportChannel ch("TestChannel", 1, &pa);
2352 PrepareChannel(&ch); 2343 PrepareChannel(&ch);
2353 ch.Connect();
2354 ch.MaybeStartGathering(); 2344 ch.MaybeStartGathering();
2355 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2345 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2356 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2346 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2357 2347
2358 ASSERT_TRUE(conn != nullptr); 2348 ASSERT_TRUE(conn != nullptr);
2359 SIMULATED_WAIT(conn->num_pings_sent() == 1, kDefaultTimeout, clock); 2349 SIMULATED_WAIT(conn->num_pings_sent() == 1, kDefaultTimeout, clock);
2360 2350
2361 // Initializing. 2351 // Initializing.
2362 2352
2363 int64_t start = clock.TimeNanos(); 2353 int64_t start = clock.TimeNanos();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2417 // STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL. 2407 // STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL.
2418 start = clock.TimeNanos(); 2408 start = clock.TimeNanos();
2419 ping_sent_before = conn->num_pings_sent(); 2409 ping_sent_before = conn->num_pings_sent();
2420 SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, 3000, clock); 2410 SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, 3000, clock);
2421 ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; 2411 ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec;
2422 EXPECT_GE(ping_interval_ms, STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); 2412 EXPECT_GE(ping_interval_ms, STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL);
2423 EXPECT_LE(ping_interval_ms, 2413 EXPECT_LE(ping_interval_ms,
2424 STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); 2414 STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE);
2425 } 2415 }
2426 2416
2417 // Test that we start pinging as soon as we have a connection and remote ICE
2418 // credentials.
2419 TEST_F(P2PTransportChannelPingTest, PingingStartedAsSoonAsPossible) {
2420 rtc::ScopedFakeClock clock;
2421
2422 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2423 P2PTransportChannel ch("TestChannel", 1, &pa);
2424 ch.SetIceRole(ICEROLE_CONTROLLING);
2425 ch.SetIceCredentials(kIceUfrag[0], kIcePwd[0]);
2426 ch.MaybeStartGathering();
2427 EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, ch.gathering_state(),
2428 kDefaultTimeout);
2429
2430 // Simulate a binding request being received, creating a peer reflexive
2431 // candidate pair while we still don't have remote ICE credentials.
2432 IceMessage request;
2433 request.SetType(STUN_BINDING_REQUEST);
2434 request.AddAttribute(
2435 new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1]));
2436 uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24;
2437 request.AddAttribute(
2438 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority));
2439 Port* port = GetPort(&ch);
2440 ASSERT_NE(nullptr, port);
2441 port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP,
2442 &request, kIceUfrag[1], false);
2443 Connection* conn = GetConnectionTo(&ch, "1.1.1.1", 1);
2444 ASSERT_NE(nullptr, conn);
2445
2446 // Simulate waiting for a second (and change) and verify that no pings were
2447 // sent, since we don't yet have remote ICE credentials.
2448 SIMULATED_WAIT(conn->num_pings_sent() > 0, 1025, clock);
2449 EXPECT_EQ(0, conn->num_pings_sent());
2450
2451 // Set remote ICE credentials. Now we should be able to ping. Ensure that
2452 // the first ping is sent as soon as possible, within one simulated clock
2453 // tick.
2454 ch.SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]);
2455 EXPECT_TRUE_SIMULATED_WAIT(conn->num_pings_sent() > 0, 1, clock);
2456 }
2457
2427 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { 2458 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) {
2428 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2459 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2429 P2PTransportChannel ch("trigger checks", 1, &pa); 2460 P2PTransportChannel ch("trigger checks", 1, &pa);
2430 PrepareChannel(&ch); 2461 PrepareChannel(&ch);
2431 ch.Connect();
2432 ch.MaybeStartGathering(); 2462 ch.MaybeStartGathering();
2433 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2463 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2434 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); 2464 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2));
2435 2465
2436 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2466 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2437 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2467 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2438 ASSERT_TRUE(conn1 != nullptr); 2468 ASSERT_TRUE(conn1 != nullptr);
2439 ASSERT_TRUE(conn2 != nullptr); 2469 ASSERT_TRUE(conn2 != nullptr);
2440 2470
2441 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); 2471 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch));
2442 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); 2472 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch));
2443 conn1->ReceivedPingResponse(LOW_RTT); 2473 conn1->ReceivedPingResponse(LOW_RTT);
2444 ASSERT_TRUE(conn1->writable()); 2474 ASSERT_TRUE(conn1->writable());
2445 conn1->ReceivedPing(); 2475 conn1->ReceivedPing();
2446 2476
2447 // Ping received, but the connection is already writable, so no 2477 // Ping received, but the connection is already writable, so no
2448 // "triggered check" and conn2 is pinged before conn1 because it has 2478 // "triggered check" and conn2 is pinged before conn1 because it has
2449 // a higher priority. 2479 // a higher priority.
2450 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); 2480 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch));
2451 } 2481 }
2452 2482
2453 TEST_F(P2PTransportChannelPingTest, TestFailedConnectionNotPingable) { 2483 TEST_F(P2PTransportChannelPingTest, TestFailedConnectionNotPingable) {
2454 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2484 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2455 P2PTransportChannel ch("Do not ping failed connections", 1, &pa); 2485 P2PTransportChannel ch("Do not ping failed connections", 1, &pa);
2456 PrepareChannel(&ch); 2486 PrepareChannel(&ch);
2457 ch.Connect();
2458 ch.MaybeStartGathering(); 2487 ch.MaybeStartGathering();
2459 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2488 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2460 2489
2461 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2490 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2462 ASSERT_TRUE(conn1 != nullptr); 2491 ASSERT_TRUE(conn1 != nullptr);
2463 2492
2464 EXPECT_EQ(conn1, ch.FindNextPingableConnection()); 2493 EXPECT_EQ(conn1, ch.FindNextPingableConnection());
2465 conn1->Prune(); // A pruned connection may still be pingable. 2494 conn1->Prune(); // A pruned connection may still be pingable.
2466 EXPECT_EQ(conn1, ch.FindNextPingableConnection()); 2495 EXPECT_EQ(conn1, ch.FindNextPingableConnection());
2467 conn1->FailAndPrune(); 2496 conn1->FailAndPrune();
2468 EXPECT_TRUE(nullptr == ch.FindNextPingableConnection()); 2497 EXPECT_TRUE(nullptr == ch.FindNextPingableConnection());
2469 } 2498 }
2470 2499
2471 TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) { 2500 TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) {
2472 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2501 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2473 P2PTransportChannel ch("state change", 1, &pa); 2502 P2PTransportChannel ch("state change", 1, &pa);
2474 PrepareChannel(&ch); 2503 PrepareChannel(&ch);
2475 ch.Connect();
2476 ch.MaybeStartGathering(); 2504 ch.MaybeStartGathering();
2477 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2505 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2478 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2506 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2479 ASSERT_TRUE(conn1 != nullptr); 2507 ASSERT_TRUE(conn1 != nullptr);
2480 // Pruning the connection reduces the set of active connections and changes 2508 // Pruning the connection reduces the set of active connections and changes
2481 // the channel state. 2509 // the channel state.
2482 conn1->Prune(); 2510 conn1->Prune();
2483 EXPECT_EQ_WAIT(STATE_FAILED, channel_state(), kDefaultTimeout); 2511 EXPECT_EQ_WAIT(STATE_FAILED, channel_state(), kDefaultTimeout);
2484 } 2512 }
2485 2513
2486 // Test adding remote candidates with different ufrags. If a remote candidate 2514 // Test adding remote candidates with different ufrags. If a remote candidate
2487 // is added with an old ufrag, it will be discarded. If it is added with a 2515 // is added with an old ufrag, it will be discarded. If it is added with a
2488 // ufrag that was not seen before, it will be used to create connections 2516 // ufrag that was not seen before, it will be used to create connections
2489 // although the ICE pwd in the remote candidate will be set when the ICE 2517 // although the ICE pwd in the remote candidate will be set when the ICE
2490 // credentials arrive. If a remote candidate is added with the current ICE 2518 // credentials arrive. If a remote candidate is added with the current ICE
2491 // ufrag, its pwd and generation will be set properly. 2519 // ufrag, its pwd and generation will be set properly.
2492 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { 2520 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) {
2493 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2521 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2494 P2PTransportChannel ch("add candidate", 1, &pa); 2522 P2PTransportChannel ch("add candidate", 1, &pa);
2495 PrepareChannel(&ch); 2523 PrepareChannel(&ch);
2496 ch.Connect();
2497 ch.MaybeStartGathering(); 2524 ch.MaybeStartGathering();
2498 // Add a candidate with a future ufrag. 2525 // Add a candidate with a future ufrag.
2499 ch.AddRemoteCandidate( 2526 ch.AddRemoteCandidate(
2500 CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1, kIceUfrag[2])); 2527 CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1, kIceUfrag[2]));
2501 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2528 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2502 ASSERT_TRUE(conn1 != nullptr); 2529 ASSERT_TRUE(conn1 != nullptr);
2503 const Candidate& candidate = conn1->remote_candidate(); 2530 const Candidate& candidate = conn1->remote_candidate();
2504 EXPECT_EQ(kIceUfrag[2], candidate.username()); 2531 EXPECT_EQ(kIceUfrag[2], candidate.username());
2505 EXPECT_TRUE(candidate.password().empty()); 2532 EXPECT_TRUE(candidate.password().empty());
2506 EXPECT_TRUE(FindNextPingableConnectionAndPingIt(&ch) == nullptr); 2533 EXPECT_TRUE(FindNextPingableConnectionAndPingIt(&ch) == nullptr);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2539 } else if (candidate.username() == kIceUfrag[2]) { 2566 } else if (candidate.username() == kIceUfrag[2]) {
2540 EXPECT_EQ(kIcePwd[2], candidate.password()); 2567 EXPECT_EQ(kIcePwd[2], candidate.password());
2541 } 2568 }
2542 } 2569 }
2543 } 2570 }
2544 2571
2545 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { 2572 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) {
2546 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2573 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2547 P2PTransportChannel ch("connection resurrection", 1, &pa); 2574 P2PTransportChannel ch("connection resurrection", 1, &pa);
2548 PrepareChannel(&ch); 2575 PrepareChannel(&ch);
2549 ch.Connect();
2550 ch.MaybeStartGathering(); 2576 ch.MaybeStartGathering();
2551 2577
2552 // Create conn1 and keep track of original candidate priority. 2578 // Create conn1 and keep track of original candidate priority.
2553 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2579 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2554 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2580 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2555 ASSERT_TRUE(conn1 != nullptr); 2581 ASSERT_TRUE(conn1 != nullptr);
2556 uint32_t remote_priority = conn1->remote_candidate().priority(); 2582 uint32_t remote_priority = conn1->remote_candidate().priority();
2557 2583
2558 // Create a higher priority candidate and make the connection 2584 // Create a higher priority candidate and make the connection
2559 // receiving/writable. This will prune conn1. 2585 // receiving/writable. This will prune conn1.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
2599 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2625 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2600 P2PTransportChannel ch("receiving state change", 1, &pa); 2626 P2PTransportChannel ch("receiving state change", 1, &pa);
2601 PrepareChannel(&ch); 2627 PrepareChannel(&ch);
2602 // Default receiving timeout and checking receiving interval should not be too 2628 // Default receiving timeout and checking receiving interval should not be too
2603 // small. 2629 // small.
2604 EXPECT_LE(1000, ch.receiving_timeout()); 2630 EXPECT_LE(1000, ch.receiving_timeout());
2605 EXPECT_LE(200, ch.check_receiving_interval()); 2631 EXPECT_LE(200, ch.check_receiving_interval());
2606 ch.SetIceConfig(CreateIceConfig(500, false)); 2632 ch.SetIceConfig(CreateIceConfig(500, false));
2607 EXPECT_EQ(500, ch.receiving_timeout()); 2633 EXPECT_EQ(500, ch.receiving_timeout());
2608 EXPECT_EQ(50, ch.check_receiving_interval()); 2634 EXPECT_EQ(50, ch.check_receiving_interval());
2609 ch.Connect();
2610 ch.MaybeStartGathering(); 2635 ch.MaybeStartGathering();
2611 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2636 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2612 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2637 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2613 ASSERT_TRUE(conn1 != nullptr); 2638 ASSERT_TRUE(conn1 != nullptr);
2614 2639
2615 conn1->ReceivedPing(); 2640 conn1->ReceivedPing();
2616 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); 2641 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0));
2617 EXPECT_TRUE_WAIT(ch.selected_connection() != nullptr, 1000); 2642 EXPECT_TRUE_WAIT(ch.selected_connection() != nullptr, 1000);
2618 EXPECT_TRUE_WAIT(ch.receiving(), 1000); 2643 EXPECT_TRUE_WAIT(ch.receiving(), 1000);
2619 EXPECT_TRUE_WAIT(!ch.receiving(), 1000); 2644 EXPECT_TRUE_WAIT(!ch.receiving(), 1000);
2620 } 2645 }
2621 2646
2622 // The controlled side will select a connection as the "selected connection" 2647 // The controlled side will select a connection as the "selected connection"
2623 // based on priority until the controlling side nominates a connection, at which 2648 // based on priority until the controlling side nominates a connection, at which
2624 // point the controlled side will select that connection as the 2649 // point the controlled side will select that connection as the
2625 // "selected connection". Plus, SignalSelectedCandidatePair will be fired if the 2650 // "selected connection". Plus, SignalSelectedCandidatePair will be fired if the
2626 // selected connection changes and SignalReadyToSend will be fired if the new 2651 // selected connection changes and SignalReadyToSend will be fired if the new
2627 // selected connection is writable. 2652 // selected connection is writable.
2628 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { 2653 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) {
2629 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2654 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2630 P2PTransportChannel ch("receiving state change", 1, &pa); 2655 P2PTransportChannel ch("receiving state change", 1, &pa);
2631 PrepareChannel(&ch); 2656 PrepareChannel(&ch);
2632 ch.SetIceRole(ICEROLE_CONTROLLED); 2657 ch.SetIceRole(ICEROLE_CONTROLLED);
2633 ch.Connect();
2634 ch.MaybeStartGathering(); 2658 ch.MaybeStartGathering();
2635 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 2659 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
2636 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2660 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2637 ASSERT_TRUE(conn1 != nullptr); 2661 ASSERT_TRUE(conn1 != nullptr);
2638 EXPECT_EQ(conn1, ch.selected_connection()); 2662 EXPECT_EQ(conn1, ch.selected_connection());
2639 EXPECT_EQ(conn1, last_selected_candidate_pair()); 2663 EXPECT_EQ(conn1, last_selected_candidate_pair());
2640 EXPECT_EQ(-1, last_sent_packet_id()); 2664 EXPECT_EQ(-1, last_sent_packet_id());
2641 // Channel is not ready to send because it is not writable. 2665 // Channel is not ready to send because it is not writable.
2642 EXPECT_FALSE(channel_ready_to_send()); 2666 EXPECT_FALSE(channel_ready_to_send());
2643 2667
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2702 // based on requests from an unknown address before the controlling side 2726 // based on requests from an unknown address before the controlling side
2703 // nominates a connection, and will nominate a connection from an unknown 2727 // nominates a connection, and will nominate a connection from an unknown
2704 // address if the request contains the use_candidate attribute. Plus, it will 2728 // address if the request contains the use_candidate attribute. Plus, it will
2705 // also sends back a ping response and set the ICE pwd in the remote candidate 2729 // also sends back a ping response and set the ICE pwd in the remote candidate
2706 // appropriately. 2730 // appropriately.
2707 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { 2731 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) {
2708 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2732 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2709 P2PTransportChannel ch("receiving state change", 1, &pa); 2733 P2PTransportChannel ch("receiving state change", 1, &pa);
2710 PrepareChannel(&ch); 2734 PrepareChannel(&ch);
2711 ch.SetIceRole(ICEROLE_CONTROLLED); 2735 ch.SetIceRole(ICEROLE_CONTROLLED);
2712 ch.Connect();
2713 ch.MaybeStartGathering(); 2736 ch.MaybeStartGathering();
2714 // A minimal STUN message with prflx priority. 2737 // A minimal STUN message with prflx priority.
2715 IceMessage request; 2738 IceMessage request;
2716 request.SetType(STUN_BINDING_REQUEST); 2739 request.SetType(STUN_BINDING_REQUEST);
2717 request.AddAttribute( 2740 request.AddAttribute(
2718 new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1])); 2741 new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1]));
2719 uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; 2742 uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24;
2720 request.AddAttribute( 2743 request.AddAttribute(
2721 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); 2744 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority));
2722 TestUDPPort* port = static_cast<TestUDPPort*>(GetPort(&ch)); 2745 TestUDPPort* port = static_cast<TestUDPPort*>(GetPort(&ch));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
2783 2806
2784 // The controlled side will select a connection as the "selected connection" 2807 // The controlled side will select a connection as the "selected connection"
2785 // based on media received until the controlling side nominates a connection, 2808 // based on media received until the controlling side nominates a connection,
2786 // at which point the controlled side will select that connection as 2809 // at which point the controlled side will select that connection as
2787 // the "selected connection". 2810 // the "selected connection".
2788 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { 2811 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) {
2789 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2812 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2790 P2PTransportChannel ch("receiving state change", 1, &pa); 2813 P2PTransportChannel ch("receiving state change", 1, &pa);
2791 PrepareChannel(&ch); 2814 PrepareChannel(&ch);
2792 ch.SetIceRole(ICEROLE_CONTROLLED); 2815 ch.SetIceRole(ICEROLE_CONTROLLED);
2793 ch.Connect();
2794 ch.MaybeStartGathering(); 2816 ch.MaybeStartGathering();
2795 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 10)); 2817 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 10));
2796 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2818 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2797 ASSERT_TRUE(conn1 != nullptr); 2819 ASSERT_TRUE(conn1 != nullptr);
2798 EXPECT_EQ(conn1, ch.selected_connection()); 2820 EXPECT_EQ(conn1, ch.selected_connection());
2799 2821
2800 // If a data packet is received on conn2, the selected connection should 2822 // If a data packet is received on conn2, the selected connection should
2801 // switch to conn2 because the controlled side must mirror the media path 2823 // switch to conn2 because the controlled side must mirror the media path
2802 // chosen by the controlling side. 2824 // chosen by the controlling side.
2803 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); 2825 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2836 } 2858 }
2837 2859
2838 TEST_F(P2PTransportChannelPingTest, 2860 TEST_F(P2PTransportChannelPingTest,
2839 TestControlledAgentDataReceivingTakesHigherPrecedenceThanPriority) { 2861 TestControlledAgentDataReceivingTakesHigherPrecedenceThanPriority) {
2840 rtc::ScopedFakeClock clock; 2862 rtc::ScopedFakeClock clock;
2841 2863
2842 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2864 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2843 P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); 2865 P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa);
2844 PrepareChannel(&ch); 2866 PrepareChannel(&ch);
2845 ch.SetIceRole(ICEROLE_CONTROLLED); 2867 ch.SetIceRole(ICEROLE_CONTROLLED);
2846 ch.Connect();
2847 ch.MaybeStartGathering(); 2868 ch.MaybeStartGathering();
2848 // The connections have decreasing priority. 2869 // The connections have decreasing priority.
2849 Connection* conn1 = 2870 Connection* conn1 =
2850 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, true); 2871 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, true);
2851 ASSERT_TRUE(conn1 != nullptr); 2872 ASSERT_TRUE(conn1 != nullptr);
2852 Connection* conn2 = 2873 Connection* conn2 =
2853 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, true); 2874 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, true);
2854 ASSERT_TRUE(conn2 != nullptr); 2875 ASSERT_TRUE(conn2 != nullptr);
2855 2876
2856 // Initially, connections are selected based on priority. 2877 // Initially, connections are selected based on priority.
(...skipping 19 matching lines...) Expand all
2876 } 2897 }
2877 2898
2878 TEST_F(P2PTransportChannelPingTest, 2899 TEST_F(P2PTransportChannelPingTest,
2879 TestControlledAgentNominationTakesHigherPrecedenceThanDataReceiving) { 2900 TestControlledAgentNominationTakesHigherPrecedenceThanDataReceiving) {
2880 rtc::ScopedFakeClock clock; 2901 rtc::ScopedFakeClock clock;
2881 2902
2882 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2903 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2883 P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); 2904 P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa);
2884 PrepareChannel(&ch); 2905 PrepareChannel(&ch);
2885 ch.SetIceRole(ICEROLE_CONTROLLED); 2906 ch.SetIceRole(ICEROLE_CONTROLLED);
2886 ch.Connect();
2887 ch.MaybeStartGathering(); 2907 ch.MaybeStartGathering();
2888 // The connections have decreasing priority. 2908 // The connections have decreasing priority.
2889 Connection* conn1 = 2909 Connection* conn1 =
2890 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, false); 2910 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, false);
2891 ASSERT_TRUE(conn1 != nullptr); 2911 ASSERT_TRUE(conn1 != nullptr);
2892 Connection* conn2 = 2912 Connection* conn2 =
2893 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, false); 2913 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, false);
2894 ASSERT_TRUE(conn2 != nullptr); 2914 ASSERT_TRUE(conn2 != nullptr);
2895 2915
2896 // conn1 received data; it is the selected connection. 2916 // conn1 received data; it is the selected connection.
(...skipping 25 matching lines...) Expand all
2922 } 2942 }
2923 2943
2924 TEST_F(P2PTransportChannelPingTest, 2944 TEST_F(P2PTransportChannelPingTest,
2925 TestControlledAgentWriteStateTakesHigherPrecedenceThanNomination) { 2945 TestControlledAgentWriteStateTakesHigherPrecedenceThanNomination) {
2926 rtc::ScopedFakeClock clock; 2946 rtc::ScopedFakeClock clock;
2927 2947
2928 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2948 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2929 P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); 2949 P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa);
2930 PrepareChannel(&ch); 2950 PrepareChannel(&ch);
2931 ch.SetIceRole(ICEROLE_CONTROLLED); 2951 ch.SetIceRole(ICEROLE_CONTROLLED);
2932 ch.Connect();
2933 ch.MaybeStartGathering(); 2952 ch.MaybeStartGathering();
2934 // The connections have decreasing priority. 2953 // The connections have decreasing priority.
2935 Connection* conn1 = 2954 Connection* conn1 =
2936 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, false); 2955 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, false);
2937 ASSERT_TRUE(conn1 != nullptr); 2956 ASSERT_TRUE(conn1 != nullptr);
2938 Connection* conn2 = 2957 Connection* conn2 =
2939 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, false); 2958 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, false);
2940 ASSERT_TRUE(conn2 != nullptr); 2959 ASSERT_TRUE(conn2 != nullptr);
2941 2960
2942 NominateConnection(conn1); 2961 NominateConnection(conn1);
(...skipping 18 matching lines...) Expand all
2961 SIMULATED_WAIT(false, 10, clock); 2980 SIMULATED_WAIT(false, 10, clock);
2962 EXPECT_EQ(0, get_and_reset_selected_candidate_pair_switches()); 2981 EXPECT_EQ(0, get_and_reset_selected_candidate_pair_switches());
2963 } 2982 }
2964 2983
2965 // Test that if a new remote candidate has the same address and port with 2984 // Test that if a new remote candidate has the same address and port with
2966 // an old one, it will be used to create a new connection. 2985 // an old one, it will be used to create a new connection.
2967 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { 2986 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) {
2968 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2987 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2969 P2PTransportChannel ch("candidate reuse", 1, &pa); 2988 P2PTransportChannel ch("candidate reuse", 1, &pa);
2970 PrepareChannel(&ch); 2989 PrepareChannel(&ch);
2971 ch.Connect();
2972 ch.MaybeStartGathering(); 2990 ch.MaybeStartGathering();
2973 const std::string host_address = "1.1.1.1"; 2991 const std::string host_address = "1.1.1.1";
2974 const int port_num = 1; 2992 const int port_num = 1;
2975 2993
2976 // kIceUfrag[1] is the current generation ufrag. 2994 // kIceUfrag[1] is the current generation ufrag.
2977 Candidate candidate = CreateUdpCandidate(LOCAL_PORT_TYPE, host_address, 2995 Candidate candidate = CreateUdpCandidate(LOCAL_PORT_TYPE, host_address,
2978 port_num, 1, kIceUfrag[1]); 2996 port_num, 1, kIceUfrag[1]);
2979 ch.AddRemoteCandidate(candidate); 2997 ch.AddRemoteCandidate(candidate);
2980 Connection* conn1 = WaitForConnectionTo(&ch, host_address, port_num); 2998 Connection* conn1 = WaitForConnectionTo(&ch, host_address, port_num);
2981 ASSERT_TRUE(conn1 != nullptr); 2999 ASSERT_TRUE(conn1 != nullptr);
(...skipping 19 matching lines...) Expand all
3001 EXPECT_TRUE(conn2->last_ping_received() > 0); 3019 EXPECT_TRUE(conn2->last_ping_received() > 0);
3002 } 3020 }
3003 3021
3004 // When the current selected connection is strong, lower-priority connections 3022 // When the current selected connection is strong, lower-priority connections
3005 // will be pruned. Otherwise, lower-priority connections are kept. 3023 // will be pruned. Otherwise, lower-priority connections are kept.
3006 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { 3024 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) {
3007 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3025 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3008 P2PTransportChannel ch("test channel", 1, &pa); 3026 P2PTransportChannel ch("test channel", 1, &pa);
3009 PrepareChannel(&ch); 3027 PrepareChannel(&ch);
3010 ch.SetIceRole(ICEROLE_CONTROLLED); 3028 ch.SetIceRole(ICEROLE_CONTROLLED);
3011 ch.Connect();
3012 ch.MaybeStartGathering(); 3029 ch.MaybeStartGathering();
3013 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 3030 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
3014 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3031 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3015 ASSERT_TRUE(conn1 != nullptr); 3032 ASSERT_TRUE(conn1 != nullptr);
3016 EXPECT_EQ(conn1, ch.selected_connection()); 3033 EXPECT_EQ(conn1, ch.selected_connection());
3017 conn1->ReceivedPingResponse(LOW_RTT); // Becomes writable and receiving 3034 conn1->ReceivedPingResponse(LOW_RTT); // Becomes writable and receiving
3018 3035
3019 // When a higher-priority, nominated candidate comes in, the connections with 3036 // When a higher-priority, nominated candidate comes in, the connections with
3020 // lower-priority are pruned. 3037 // lower-priority are pruned.
3021 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10)); 3038 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10));
(...skipping 17 matching lines...) Expand all
3039 WAIT(conn3->pruned(), 1000); 3056 WAIT(conn3->pruned(), 1000);
3040 EXPECT_FALSE(conn3->pruned()); 3057 EXPECT_FALSE(conn3->pruned());
3041 } 3058 }
3042 3059
3043 TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) { 3060 TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) {
3044 rtc::ScopedFakeClock clock; 3061 rtc::ScopedFakeClock clock;
3045 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3062 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3046 P2PTransportChannel ch("test channel", 1, &pa); 3063 P2PTransportChannel ch("test channel", 1, &pa);
3047 PrepareChannel(&ch); 3064 PrepareChannel(&ch);
3048 ch.SetIceRole(ICEROLE_CONTROLLED); 3065 ch.SetIceRole(ICEROLE_CONTROLLED);
3049 ch.Connect();
3050 ch.MaybeStartGathering(); 3066 ch.MaybeStartGathering();
3051 Connection* conn1 = 3067 Connection* conn1 =
3052 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 100, true); 3068 CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 100, true);
3053 ASSERT_TRUE(conn1 != nullptr); 3069 ASSERT_TRUE(conn1 != nullptr);
3054 Connection* conn2 = 3070 Connection* conn2 =
3055 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 200, false); 3071 CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 200, false);
3056 ASSERT_TRUE(conn2 != nullptr); 3072 ASSERT_TRUE(conn2 != nullptr);
3057 // Even if conn1 is writable, nominated, receiving data, it should not prune 3073 // Even if conn1 is writable, nominated, receiving data, it should not prune
3058 // conn2. 3074 // conn2.
3059 NominateConnection(conn1); 3075 NominateConnection(conn1);
3060 SIMULATED_WAIT(false, 1, clock); 3076 SIMULATED_WAIT(false, 1, clock);
3061 conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); 3077 conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0));
3062 SIMULATED_WAIT(conn2->pruned(), 100, clock); 3078 SIMULATED_WAIT(conn2->pruned(), 100, clock);
3063 EXPECT_FALSE(conn2->pruned()); 3079 EXPECT_FALSE(conn2->pruned());
3064 } 3080 }
3065 3081
3066 // Test that GetState returns the state correctly. 3082 // Test that GetState returns the state correctly.
3067 TEST_F(P2PTransportChannelPingTest, TestGetState) { 3083 TEST_F(P2PTransportChannelPingTest, TestGetState) {
3068 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3084 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3069 P2PTransportChannel ch("test channel", 1, &pa); 3085 P2PTransportChannel ch("test channel", 1, &pa);
3070 PrepareChannel(&ch); 3086 PrepareChannel(&ch);
3071 ch.Connect();
3072 ch.MaybeStartGathering(); 3087 ch.MaybeStartGathering();
3073 EXPECT_EQ(TransportChannelState::STATE_INIT, ch.GetState()); 3088 EXPECT_EQ(TransportChannelState::STATE_INIT, ch.GetState());
3074 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); 3089 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100));
3075 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); 3090 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1));
3076 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3091 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3077 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 3092 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
3078 ASSERT_TRUE(conn1 != nullptr); 3093 ASSERT_TRUE(conn1 != nullptr);
3079 ASSERT_TRUE(conn2 != nullptr); 3094 ASSERT_TRUE(conn2 != nullptr);
3080 // Now there are two connections, so the transport channel is connecting. 3095 // Now there are two connections, so the transport channel is connecting.
3081 EXPECT_EQ(TransportChannelState::STATE_CONNECTING, ch.GetState()); 3096 EXPECT_EQ(TransportChannelState::STATE_CONNECTING, ch.GetState());
3082 // |conn1| becomes writable and receiving; it then should prune |conn2|. 3097 // |conn1| becomes writable and receiving; it then should prune |conn2|.
3083 conn1->ReceivedPingResponse(LOW_RTT); 3098 conn1->ReceivedPingResponse(LOW_RTT);
3084 EXPECT_TRUE_WAIT(conn2->pruned(), 1000); 3099 EXPECT_TRUE_WAIT(conn2->pruned(), 1000);
3085 EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState()); 3100 EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState());
3086 conn1->Prune(); // All connections are pruned. 3101 conn1->Prune(); // All connections are pruned.
3087 // Need to wait until the channel state is updated. 3102 // Need to wait until the channel state is updated.
3088 EXPECT_EQ_WAIT(TransportChannelState::STATE_FAILED, ch.GetState(), 1000); 3103 EXPECT_EQ_WAIT(TransportChannelState::STATE_FAILED, ch.GetState(), 1000);
3089 } 3104 }
3090 3105
3091 // Test that when a low-priority connection is pruned, it is not deleted 3106 // Test that when a low-priority connection is pruned, it is not deleted
3092 // right away, and it can become active and be pruned again. 3107 // right away, and it can become active and be pruned again.
3093 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { 3108 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) {
3094 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3109 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3095 P2PTransportChannel ch("test channel", 1, &pa); 3110 P2PTransportChannel ch("test channel", 1, &pa);
3096 PrepareChannel(&ch); 3111 PrepareChannel(&ch);
3097 ch.SetIceConfig(CreateIceConfig(1000, false)); 3112 ch.SetIceConfig(CreateIceConfig(1000, false));
3098 ch.Connect();
3099 ch.MaybeStartGathering(); 3113 ch.MaybeStartGathering();
3100 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); 3114 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100));
3101 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3115 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3102 ASSERT_TRUE(conn1 != nullptr); 3116 ASSERT_TRUE(conn1 != nullptr);
3103 EXPECT_EQ(conn1, ch.selected_connection()); 3117 EXPECT_EQ(conn1, ch.selected_connection());
3104 conn1->ReceivedPingResponse(LOW_RTT); // Becomes writable and receiving 3118 conn1->ReceivedPingResponse(LOW_RTT); // Becomes writable and receiving
3105 3119
3106 // Add a low-priority connection |conn2|, which will be pruned, but it will 3120 // Add a low-priority connection |conn2|, which will be pruned, but it will
3107 // not be deleted right away. Once the current selected connection becomes not 3121 // not be deleted right away. Once the current selected connection becomes not
3108 // receiving, |conn2| will start to ping and upon receiving the ping response, 3122 // receiving, |conn2| will start to ping and upon receiving the ping response,
(...skipping 21 matching lines...) Expand all
3130 EXPECT_TRUE_WAIT(!conn2->active(), 1000); 3144 EXPECT_TRUE_WAIT(!conn2->active(), 1000);
3131 EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState()); 3145 EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState());
3132 } 3146 }
3133 3147
3134 // Test that if all connections in a channel has timed out on writing, they 3148 // Test that if all connections in a channel has timed out on writing, they
3135 // will all be deleted. We use Prune to simulate write_time_out. 3149 // will all be deleted. We use Prune to simulate write_time_out.
3136 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { 3150 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) {
3137 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3151 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3138 P2PTransportChannel ch("test channel", 1, &pa); 3152 P2PTransportChannel ch("test channel", 1, &pa);
3139 PrepareChannel(&ch); 3153 PrepareChannel(&ch);
3140 ch.Connect();
3141 ch.MaybeStartGathering(); 3154 ch.MaybeStartGathering();
3142 // Have one connection only but later becomes write-time-out. 3155 // Have one connection only but later becomes write-time-out.
3143 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); 3156 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100));
3144 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3157 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3145 ASSERT_TRUE(conn1 != nullptr); 3158 ASSERT_TRUE(conn1 != nullptr);
3146 conn1->ReceivedPing(); // Becomes receiving 3159 conn1->ReceivedPing(); // Becomes receiving
3147 conn1->Prune(); 3160 conn1->Prune();
3148 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); 3161 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000);
3149 3162
3150 // Have two connections but both become write-time-out later. 3163 // Have two connections but both become write-time-out later.
(...skipping 13 matching lines...) Expand all
3164 3177
3165 // Tests that after a port allocator session is started, it will be stopped 3178 // Tests that after a port allocator session is started, it will be stopped
3166 // when a new connection becomes writable and receiving. Also tests that if a 3179 // when a new connection becomes writable and receiving. Also tests that if a
3167 // connection belonging to an old session becomes writable, it won't stop 3180 // connection belonging to an old session becomes writable, it won't stop
3168 // the current port allocator session. 3181 // the current port allocator session.
3169 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { 3182 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) {
3170 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3183 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3171 P2PTransportChannel ch("test channel", 1, &pa); 3184 P2PTransportChannel ch("test channel", 1, &pa);
3172 PrepareChannel(&ch); 3185 PrepareChannel(&ch);
3173 ch.SetIceConfig(CreateIceConfig(2000, false)); 3186 ch.SetIceConfig(CreateIceConfig(2000, false));
3174 ch.Connect();
3175 ch.MaybeStartGathering(); 3187 ch.MaybeStartGathering();
3176 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); 3188 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100));
3177 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3189 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3178 ASSERT_TRUE(conn1 != nullptr); 3190 ASSERT_TRUE(conn1 != nullptr);
3179 conn1->ReceivedPingResponse(LOW_RTT); // Becomes writable and receiving 3191 conn1->ReceivedPingResponse(LOW_RTT); // Becomes writable and receiving
3180 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); 3192 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts());
3181 3193
3182 // Start a new session. Even though conn1, which belongs to an older 3194 // Start a new session. Even though conn1, which belongs to an older
3183 // session, becomes unwritable and writable again, it should not stop the 3195 // session, becomes unwritable and writable again, it should not stop the
3184 // current session. 3196 // current session.
(...skipping 17 matching lines...) Expand all
3202 // a correct role, in case the network becomes active before the connection is 3214 // a correct role, in case the network becomes active before the connection is
3203 // destroyed. 3215 // destroyed.
3204 TEST_F(P2PTransportChannelPingTest, 3216 TEST_F(P2PTransportChannelPingTest,
3205 TestIceRoleUpdatedOnPortAfterSignalNetworkInactive) { 3217 TestIceRoleUpdatedOnPortAfterSignalNetworkInactive) {
3206 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3218 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3207 P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); 3219 P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa);
3208 // Starts with ICEROLE_CONTROLLING. 3220 // Starts with ICEROLE_CONTROLLING.
3209 PrepareChannel(&ch); 3221 PrepareChannel(&ch);
3210 IceConfig config = CreateIceConfig(1000, true); 3222 IceConfig config = CreateIceConfig(1000, true);
3211 ch.SetIceConfig(config); 3223 ch.SetIceConfig(config);
3212 ch.Connect();
3213 ch.MaybeStartGathering(); 3224 ch.MaybeStartGathering();
3214 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 3225 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
3215 3226
3216 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3227 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3217 ASSERT_TRUE(conn != nullptr); 3228 ASSERT_TRUE(conn != nullptr);
3218 3229
3219 // Make the fake port signal that its network is inactive, then change the 3230 // Make the fake port signal that its network is inactive, then change the
3220 // ICE role and expect it to be updated. 3231 // ICE role and expect it to be updated.
3221 conn->port()->SignalNetworkInactive(conn->port()); 3232 conn->port()->SignalNetworkInactive(conn->port());
3222 ch.SetIceRole(ICEROLE_CONTROLLED); 3233 ch.SetIceRole(ICEROLE_CONTROLLED);
3223 EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); 3234 EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole());
3224 } 3235 }
3225 3236
3226 // Test that the ICE role is updated even on ports with inactive networks. 3237 // Test that the ICE role is updated even on ports with inactive networks.
3227 // These ports may still have connections that need a correct role, for the 3238 // These ports may still have connections that need a correct role, for the
3228 // pings sent by those connections until they're replaced by newer-generation 3239 // pings sent by those connections until they're replaced by newer-generation
3229 // connections. 3240 // connections.
3230 TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) { 3241 TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) {
3231 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3242 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3232 P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); 3243 P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa);
3233 // Starts with ICEROLE_CONTROLLING. 3244 // Starts with ICEROLE_CONTROLLING.
3234 PrepareChannel(&ch); 3245 PrepareChannel(&ch);
3235 ch.Connect();
3236 ch.MaybeStartGathering(); 3246 ch.MaybeStartGathering();
3237 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 3247 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
3238 3248
3239 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3249 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3240 ASSERT_TRUE(conn != nullptr); 3250 ASSERT_TRUE(conn != nullptr);
3241 3251
3242 // Do an ICE restart, change the role, and expect the old port to have its 3252 // Do an ICE restart, change the role, and expect the old port to have its
3243 // role updated. 3253 // role updated.
3244 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]); 3254 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]);
3245 ch.MaybeStartGathering(); 3255 ch.MaybeStartGathering();
3246 ch.SetIceRole(ICEROLE_CONTROLLED); 3256 ch.SetIceRole(ICEROLE_CONTROLLED);
3247 EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); 3257 EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole());
3248 } 3258 }
3249 3259
3250 // Test that after some amount of time without receiving data, the connection 3260 // Test that after some amount of time without receiving data, the connection
3251 // and port are destroyed. 3261 // and port are destroyed.
3252 TEST_F(P2PTransportChannelPingTest, TestPortDestroyedAfterTimeout) { 3262 TEST_F(P2PTransportChannelPingTest, TestPortDestroyedAfterTimeout) {
3253 rtc::ScopedFakeClock fake_clock; 3263 rtc::ScopedFakeClock fake_clock;
3254 3264
3255 FakePortAllocator pa(rtc::Thread::Current(), nullptr); 3265 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
3256 P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); 3266 P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa);
3257 PrepareChannel(&ch); 3267 PrepareChannel(&ch);
3258 // Only a controlled channel should expect its ports to be destroyed. 3268 // Only a controlled channel should expect its ports to be destroyed.
3259 ch.SetIceRole(ICEROLE_CONTROLLED); 3269 ch.SetIceRole(ICEROLE_CONTROLLED);
3260 ch.Connect();
3261 ch.MaybeStartGathering(); 3270 ch.MaybeStartGathering();
3262 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); 3271 ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1));
3263 3272
3264 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); 3273 Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1);
3265 ASSERT_TRUE(conn != nullptr); 3274 ASSERT_TRUE(conn != nullptr);
3266 3275
3267 // Simulate 2 minutes going by. This should be enough time for the port to 3276 // Simulate 2 minutes going by. This should be enough time for the port to
3268 // time out. 3277 // time out.
3269 for (int second = 0; second < 120; ++second) { 3278 for (int second = 0; second < 120; ++second) {
3270 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); 3279 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
(...skipping 24 matching lines...) Expand all
3295 bool prioritize_most_likely_to_work, 3304 bool prioritize_most_likely_to_work,
3296 int stable_writable_connection_ping_interval) { 3305 int stable_writable_connection_ping_interval) {
3297 channel_.reset(new P2PTransportChannel("checks", 1, nullptr, allocator())); 3306 channel_.reset(new P2PTransportChannel("checks", 1, nullptr, allocator()));
3298 IceConfig config = channel_->config(); 3307 IceConfig config = channel_->config();
3299 config.prioritize_most_likely_candidate_pairs = 3308 config.prioritize_most_likely_candidate_pairs =
3300 prioritize_most_likely_to_work; 3309 prioritize_most_likely_to_work;
3301 config.stable_writable_connection_ping_interval = 3310 config.stable_writable_connection_ping_interval =
3302 stable_writable_connection_ping_interval; 3311 stable_writable_connection_ping_interval;
3303 channel_->SetIceConfig(config); 3312 channel_->SetIceConfig(config);
3304 PrepareChannel(channel_.get()); 3313 PrepareChannel(channel_.get());
3305 channel_->Connect();
3306 channel_->MaybeStartGathering(); 3314 channel_->MaybeStartGathering();
3307 return *channel_.get(); 3315 return *channel_.get();
3308 } 3316 }
3309 3317
3310 BasicPortAllocator* allocator() { return allocator_.get(); } 3318 BasicPortAllocator* allocator() { return allocator_.get(); }
3311 TestTurnServer* turn_server() { return &turn_server_; } 3319 TestTurnServer* turn_server() { return &turn_server_; }
3312 3320
3313 // This verifies the next pingable connection has the expected candidates' 3321 // This verifies the next pingable connection has the expected candidates'
3314 // types and, for relay local candidate, the expected relay protocol and ping 3322 // types and, for relay local candidate, the expected relay protocol and ping
3315 // it. 3323 // it.
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
3479 3487
3480 // TCP Relay/Relay is the next. 3488 // TCP Relay/Relay is the next.
3481 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE, 3489 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE,
3482 TCP_PROTOCOL_NAME); 3490 TCP_PROTOCOL_NAME);
3483 3491
3484 // Finally, Local/Relay will be pinged. 3492 // Finally, Local/Relay will be pinged.
3485 VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); 3493 VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE);
3486 } 3494 }
3487 3495
3488 } // namespace cricket { 3496 } // namespace cricket {
OLDNEW
« no previous file with comments | « webrtc/p2p/base/p2ptransportchannel.cc ('k') | webrtc/p2p/base/port.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698