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

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

Issue 1577233006: Implement Turn/Turn first logic for connection selection. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: missed to rename a var Created 4 years, 9 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
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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 // The addresses for the public relay server. 74 // The addresses for the public relay server.
75 static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000); 75 static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
76 static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001); 76 static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
77 static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002); 77 static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
78 static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003); 78 static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
79 static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004); 79 static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
80 static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005); 80 static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
81 // The addresses for the public turn server. 81 // The addresses for the public turn server.
82 static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 82 static const SocketAddress kTurnUdpIntAddr("99.99.99.4",
83 cricket::STUN_SERVER_PORT); 83 cricket::STUN_SERVER_PORT);
84 static const SocketAddress kTurnTcpIntAddr("99.99.99.4",
85 cricket::STUN_SERVER_PORT + 1);
84 static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); 86 static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0);
85 static const cricket::RelayCredentials kRelayCredentials("test", "test"); 87 static const cricket::RelayCredentials kRelayCredentials("test", "test");
86 88
87 // Based on ICE_UFRAG_LENGTH 89 // Based on ICE_UFRAG_LENGTH
88 static const char* kIceUfrag[4] = {"TESTICEUFRAG0000", "TESTICEUFRAG0001", 90 static const char* kIceUfrag[4] = {"TESTICEUFRAG0000", "TESTICEUFRAG0001",
89 "TESTICEUFRAG0002", "TESTICEUFRAG0003"}; 91 "TESTICEUFRAG0002", "TESTICEUFRAG0003"};
90 // Based on ICE_PWD_LENGTH 92 // Based on ICE_PWD_LENGTH
91 static const char* kIcePwd[4] = {"TESTICEPWD00000000000000", 93 static const char* kIcePwd[4] = {"TESTICEPWD00000000000000",
92 "TESTICEPWD00000000000001", 94 "TESTICEPWD00000000000001",
93 "TESTICEPWD00000000000002", 95 "TESTICEPWD00000000000002",
(...skipping 1764 matching lines...) Expand 10 before | Expand all | Expand 10 after
1858 vss_(new rtc::VirtualSocketServer(pss_.get())), 1860 vss_(new rtc::VirtualSocketServer(pss_.get())),
1859 ss_scope_(vss_.get()) {} 1861 ss_scope_(vss_.get()) {}
1860 1862
1861 protected: 1863 protected:
1862 void PrepareChannel(cricket::P2PTransportChannel* ch) { 1864 void PrepareChannel(cricket::P2PTransportChannel* ch) {
1863 ch->SetIceRole(cricket::ICEROLE_CONTROLLING); 1865 ch->SetIceRole(cricket::ICEROLE_CONTROLLING);
1864 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]); 1866 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]);
1865 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]); 1867 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]);
1866 } 1868 }
1867 1869
1868 cricket::Candidate CreateCandidate(const std::string& ip, 1870 cricket::Candidate CreateHostCandidate(const std::string& ip,
1869 int port, 1871 int port,
1870 int priority, 1872 int priority,
1871 const std::string& ufrag = "") { 1873 const std::string& ufrag = "") {
1872 cricket::Candidate c; 1874 cricket::Candidate c;
1873 c.set_address(rtc::SocketAddress(ip, port)); 1875 c.set_address(rtc::SocketAddress(ip, port));
1874 c.set_component(1); 1876 c.set_component(1);
1875 c.set_protocol(cricket::UDP_PROTOCOL_NAME); 1877 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
1876 c.set_priority(priority); 1878 c.set_priority(priority);
1877 c.set_username(ufrag); 1879 c.set_username(ufrag);
1880 c.set_type(cricket::LOCAL_PORT_TYPE);
1878 return c; 1881 return c;
1879 } 1882 }
1880 1883
1881 cricket::Connection* WaitForConnectionTo(cricket::P2PTransportChannel* ch, 1884 cricket::Connection* WaitForConnectionTo(cricket::P2PTransportChannel* ch,
1882 const std::string& ip, 1885 const std::string& ip,
1883 int port_num) { 1886 int port_num) {
1884 EXPECT_TRUE_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, 3000); 1887 EXPECT_TRUE_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, 3000);
1885 return GetConnectionTo(ch, ip, port_num); 1888 return GetConnectionTo(ch, ip, port_num);
1886 } 1889 }
1887 1890
(...skipping 19 matching lines...) Expand all
1907 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_; 1910 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
1908 rtc::SocketServerScope ss_scope_; 1911 rtc::SocketServerScope ss_scope_;
1909 }; 1912 };
1910 1913
1911 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { 1914 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) {
1912 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 1915 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
1913 cricket::P2PTransportChannel ch("trigger checks", 1, &pa); 1916 cricket::P2PTransportChannel ch("trigger checks", 1, &pa);
1914 PrepareChannel(&ch); 1917 PrepareChannel(&ch);
1915 ch.Connect(); 1918 ch.Connect();
1916 ch.MaybeStartGathering(); 1919 ch.MaybeStartGathering();
1917 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 1920 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
1918 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2)); 1921 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
1919 1922
1920 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1923 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1921 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 1924 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
1922 ASSERT_TRUE(conn1 != nullptr); 1925 ASSERT_TRUE(conn1 != nullptr);
1923 ASSERT_TRUE(conn2 != nullptr); 1926 ASSERT_TRUE(conn2 != nullptr);
1924 1927
1925 // Before a triggered check, the first connection to ping is the 1928 // Before a triggered check, the first connection to ping is the
1926 // highest priority one. 1929 // highest priority one.
1927 EXPECT_EQ(conn2, ch.FindNextPingableConnection()); 1930 EXPECT_EQ(conn2, ch.FindNextPingableConnection());
1928 1931
1929 // Receiving a ping causes a triggered check which should make conn1 1932 // Receiving a ping causes a triggered check which should make conn1
1930 // be pinged first instead of conn2, even though conn2 has a higher 1933 // be pinged first instead of conn2, even though conn2 has a higher
1931 // priority. 1934 // priority.
1932 conn1->ReceivedPing(); 1935 conn1->ReceivedPing();
1933 EXPECT_EQ(conn1, ch.FindNextPingableConnection()); 1936 EXPECT_EQ(conn1, ch.FindNextPingableConnection());
1934 } 1937 }
1935 1938
1936 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { 1939 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) {
1937 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 1940 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
1938 cricket::P2PTransportChannel ch("trigger checks", 1, &pa); 1941 cricket::P2PTransportChannel ch("trigger checks", 1, &pa);
1939 PrepareChannel(&ch); 1942 PrepareChannel(&ch);
1940 ch.Connect(); 1943 ch.Connect();
1941 ch.MaybeStartGathering(); 1944 ch.MaybeStartGathering();
1942 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 1945 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
1943 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2)); 1946 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
1944 1947
1945 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1948 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1946 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 1949 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
1947 ASSERT_TRUE(conn1 != nullptr); 1950 ASSERT_TRUE(conn1 != nullptr);
1948 ASSERT_TRUE(conn2 != nullptr); 1951 ASSERT_TRUE(conn2 != nullptr);
1949 1952
1950 EXPECT_EQ(conn2, ch.FindNextPingableConnection()); 1953 EXPECT_EQ(conn2, ch.FindNextPingableConnection());
1954 EXPECT_EQ(conn1, ch.FindNextPingableConnection());
1951 conn1->ReceivedPingResponse(); 1955 conn1->ReceivedPingResponse();
1952 ASSERT_TRUE(conn1->writable()); 1956 ASSERT_TRUE(conn1->writable());
1953 conn1->ReceivedPing(); 1957 conn1->ReceivedPing();
1954 1958
1955 // Ping received, but the connection is already writable, so no 1959 // Ping received, but the connection is already writable, so no
1956 // "triggered check" and conn2 is pinged before conn1 because it has 1960 // "triggered check" and conn2 is pinged before conn1 because it has
1957 // a higher priority. 1961 // a higher priority.
1958 EXPECT_EQ(conn2, ch.FindNextPingableConnection()); 1962 EXPECT_EQ(conn2, ch.FindNextPingableConnection());
1959 } 1963 }
1960 1964
1961 // Test adding remote candidates with different ufrags. If a remote candidate 1965 // Test adding remote candidates with different ufrags. If a remote candidate
1962 // is added with an old ufrag, it will be discarded. If it is added with a 1966 // is added with an old ufrag, it will be discarded. If it is added with a
1963 // ufrag that was not seen before, it will be used to create connections 1967 // ufrag that was not seen before, it will be used to create connections
1964 // although the ICE pwd in the remote candidate will be set when the ICE 1968 // although the ICE pwd in the remote candidate will be set when the ICE
1965 // credentials arrive. If a remote candidate is added with the current ICE 1969 // credentials arrive. If a remote candidate is added with the current ICE
1966 // ufrag, its pwd and generation will be set properly. 1970 // ufrag, its pwd and generation will be set properly.
1967 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { 1971 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) {
1968 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 1972 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
1969 cricket::P2PTransportChannel ch("add candidate", 1, &pa); 1973 cricket::P2PTransportChannel ch("add candidate", 1, &pa);
1970 PrepareChannel(&ch); 1974 PrepareChannel(&ch);
1971 ch.Connect(); 1975 ch.Connect();
1972 ch.MaybeStartGathering(); 1976 ch.MaybeStartGathering();
1973 // Add a candidate with a future ufrag. 1977 // Add a candidate with a future ufrag.
1974 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1, kIceUfrag[2])); 1978 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1, kIceUfrag[2]));
1975 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1979 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1976 ASSERT_TRUE(conn1 != nullptr); 1980 ASSERT_TRUE(conn1 != nullptr);
1977 const cricket::Candidate& candidate = conn1->remote_candidate(); 1981 const cricket::Candidate& candidate = conn1->remote_candidate();
1978 EXPECT_EQ(kIceUfrag[2], candidate.username()); 1982 EXPECT_EQ(kIceUfrag[2], candidate.username());
1979 EXPECT_TRUE(candidate.password().empty()); 1983 EXPECT_TRUE(candidate.password().empty());
1980 EXPECT_TRUE(ch.FindNextPingableConnection() == nullptr); 1984 EXPECT_TRUE(ch.FindNextPingableConnection() == nullptr);
1981 1985
1982 // Set the remote credentials with the "future" ufrag. 1986 // Set the remote credentials with the "future" ufrag.
1983 // This should set the ICE pwd in the remote candidate of |conn1|, making 1987 // This should set the ICE pwd in the remote candidate of |conn1|, making
1984 // it pingable. 1988 // it pingable.
1985 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]); 1989 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]);
1986 EXPECT_EQ(kIceUfrag[2], candidate.username()); 1990 EXPECT_EQ(kIceUfrag[2], candidate.username());
1987 EXPECT_EQ(kIcePwd[2], candidate.password()); 1991 EXPECT_EQ(kIcePwd[2], candidate.password());
1988 EXPECT_EQ(conn1, ch.FindNextPingableConnection()); 1992 EXPECT_EQ(conn1, ch.FindNextPingableConnection());
1989 1993
1990 // Add a candidate with an old ufrag. No connection will be created. 1994 // Add a candidate with an old ufrag. No connection will be created.
1991 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2, kIceUfrag[1])); 1995 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2, kIceUfrag[1]));
1992 rtc::Thread::Current()->ProcessMessages(500); 1996 rtc::Thread::Current()->ProcessMessages(500);
1993 EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr); 1997 EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr);
1994 1998
1995 // Add a candidate with the current ufrag, its pwd and generation will be 1999 // Add a candidate with the current ufrag, its pwd and generation will be
1996 // assigned, even if the generation is not set. 2000 // assigned, even if the generation is not set.
1997 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 0, kIceUfrag[2])); 2001 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 0, kIceUfrag[2]));
1998 cricket::Connection* conn3 = nullptr; 2002 cricket::Connection* conn3 = nullptr;
1999 ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr, 2003 ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr,
2000 3000); 2004 3000);
2001 const cricket::Candidate& new_candidate = conn3->remote_candidate(); 2005 const cricket::Candidate& new_candidate = conn3->remote_candidate();
2002 EXPECT_EQ(kIcePwd[2], new_candidate.password()); 2006 EXPECT_EQ(kIcePwd[2], new_candidate.password());
2003 EXPECT_EQ(1U, new_candidate.generation()); 2007 EXPECT_EQ(1U, new_candidate.generation());
2004 2008
2005 // Check that the pwd of all remote candidates are properly assigned. 2009 // Check that the pwd of all remote candidates are properly assigned.
2006 for (const cricket::RemoteCandidate& candidate : ch.remote_candidates()) { 2010 for (const cricket::RemoteCandidate& candidate : ch.remote_candidates()) {
2007 EXPECT_TRUE(candidate.username() == kIceUfrag[1] || 2011 EXPECT_TRUE(candidate.username() == kIceUfrag[1] ||
2008 candidate.username() == kIceUfrag[2]); 2012 candidate.username() == kIceUfrag[2]);
2009 if (candidate.username() == kIceUfrag[1]) { 2013 if (candidate.username() == kIceUfrag[1]) {
2010 EXPECT_EQ(kIcePwd[1], candidate.password()); 2014 EXPECT_EQ(kIcePwd[1], candidate.password());
2011 } else if (candidate.username() == kIceUfrag[2]) { 2015 } else if (candidate.username() == kIceUfrag[2]) {
2012 EXPECT_EQ(kIcePwd[2], candidate.password()); 2016 EXPECT_EQ(kIcePwd[2], candidate.password());
2013 } 2017 }
2014 } 2018 }
2015 } 2019 }
2016 2020
2017 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { 2021 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) {
2018 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2022 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2019 cricket::P2PTransportChannel ch("connection resurrection", 1, &pa); 2023 cricket::P2PTransportChannel ch("connection resurrection", 1, &pa);
2020 PrepareChannel(&ch); 2024 PrepareChannel(&ch);
2021 ch.Connect(); 2025 ch.Connect();
2022 ch.MaybeStartGathering(); 2026 ch.MaybeStartGathering();
2023 2027
2024 // Create conn1 and keep track of original candidate priority. 2028 // Create conn1 and keep track of original candidate priority.
2025 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 2029 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2026 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2030 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2027 ASSERT_TRUE(conn1 != nullptr); 2031 ASSERT_TRUE(conn1 != nullptr);
2028 uint32_t remote_priority = conn1->remote_candidate().priority(); 2032 uint32_t remote_priority = conn1->remote_candidate().priority();
2029 2033
2030 // Create a higher priority candidate and make the connection 2034 // Create a higher priority candidate and make the connection
2031 // receiving/writable. This will prune conn1. 2035 // receiving/writable. This will prune conn1.
2032 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2)); 2036 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2033 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2037 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2034 ASSERT_TRUE(conn2 != nullptr); 2038 ASSERT_TRUE(conn2 != nullptr);
2035 conn2->ReceivedPing(); 2039 conn2->ReceivedPing();
2036 conn2->ReceivedPingResponse(); 2040 conn2->ReceivedPingResponse();
2037 2041
2038 // Wait for conn1 to be pruned. 2042 // Wait for conn1 to be pruned.
2039 EXPECT_TRUE_WAIT(conn1->pruned(), 3000); 2043 EXPECT_TRUE_WAIT(conn1->pruned(), 3000);
2040 // Destroy the connection to test SignalUnknownAddress. 2044 // Destroy the connection to test SignalUnknownAddress.
2041 conn1->Destroy(); 2045 conn1->Destroy();
2042 EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, 1000); 2046 EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, 1000);
(...skipping 30 matching lines...) Expand all
2073 PrepareChannel(&ch); 2077 PrepareChannel(&ch);
2074 // Default receiving timeout and checking receiving delay should not be too 2078 // Default receiving timeout and checking receiving delay should not be too
2075 // small. 2079 // small.
2076 EXPECT_LE(1000, ch.receiving_timeout()); 2080 EXPECT_LE(1000, ch.receiving_timeout());
2077 EXPECT_LE(200, ch.check_receiving_delay()); 2081 EXPECT_LE(200, ch.check_receiving_delay());
2078 ch.SetIceConfig(CreateIceConfig(500, false)); 2082 ch.SetIceConfig(CreateIceConfig(500, false));
2079 EXPECT_EQ(500, ch.receiving_timeout()); 2083 EXPECT_EQ(500, ch.receiving_timeout());
2080 EXPECT_EQ(50, ch.check_receiving_delay()); 2084 EXPECT_EQ(50, ch.check_receiving_delay());
2081 ch.Connect(); 2085 ch.Connect();
2082 ch.MaybeStartGathering(); 2086 ch.MaybeStartGathering();
2083 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 2087 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2084 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2088 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2085 ASSERT_TRUE(conn1 != nullptr); 2089 ASSERT_TRUE(conn1 != nullptr);
2086 2090
2087 conn1->ReceivedPing(); 2091 conn1->ReceivedPing();
2088 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); 2092 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0));
2089 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000); 2093 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000);
2090 EXPECT_TRUE_WAIT(ch.receiving(), 1000); 2094 EXPECT_TRUE_WAIT(ch.receiving(), 1000);
2091 EXPECT_TRUE_WAIT(!ch.receiving(), 1000); 2095 EXPECT_TRUE_WAIT(!ch.receiving(), 1000);
2092 } 2096 }
2093 2097
2094 // The controlled side will select a connection as the "best connection" based 2098 // The controlled side will select a connection as the "best connection" based
2095 // on priority until the controlling side nominates a connection, at which 2099 // on priority until the controlling side nominates a connection, at which
2096 // point the controlled side will select that connection as the 2100 // point the controlled side will select that connection as the
2097 // "best connection". 2101 // "best connection".
2098 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { 2102 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) {
2099 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2103 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2100 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); 2104 cricket::P2PTransportChannel ch("receiving state change", 1, &pa);
2101 PrepareChannel(&ch); 2105 PrepareChannel(&ch);
2102 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2106 ch.SetIceRole(cricket::ICEROLE_CONTROLLED);
2103 ch.Connect(); 2107 ch.Connect();
2104 ch.MaybeStartGathering(); 2108 ch.MaybeStartGathering();
2105 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 2109 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2106 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2110 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2107 ASSERT_TRUE(conn1 != nullptr); 2111 ASSERT_TRUE(conn1 != nullptr);
2108 EXPECT_EQ(conn1, ch.best_connection()); 2112 EXPECT_EQ(conn1, ch.best_connection());
2109 2113
2110 // When a higher priority candidate comes in, the new connection is chosen 2114 // When a higher priority candidate comes in, the new connection is chosen
2111 // as the best connection. 2115 // as the best connection.
2112 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 10)); 2116 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10));
2113 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2117 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2114 ASSERT_TRUE(conn2 != nullptr); 2118 ASSERT_TRUE(conn2 != nullptr);
2115 EXPECT_EQ(conn2, ch.best_connection()); 2119 EXPECT_EQ(conn2, ch.best_connection());
2116 2120
2117 // If a stun request with use-candidate attribute arrives, the receiving 2121 // If a stun request with use-candidate attribute arrives, the receiving
2118 // connection will be set as the best connection, even though 2122 // connection will be set as the best connection, even though
2119 // its priority is lower. 2123 // its priority is lower.
2120 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 1)); 2124 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1));
2121 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2125 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2122 ASSERT_TRUE(conn3 != nullptr); 2126 ASSERT_TRUE(conn3 != nullptr);
2123 // Because it has a lower priority, the best connection is still conn2. 2127 // Because it has a lower priority, the best connection is still conn2.
2124 EXPECT_EQ(conn2, ch.best_connection()); 2128 EXPECT_EQ(conn2, ch.best_connection());
2125 conn3->ReceivedPingResponse(); // Become writable. 2129 conn3->ReceivedPingResponse(); // Become writable.
2126 // But if it is nominated via use_candidate, it is chosen as the best 2130 // But if it is nominated via use_candidate, it is chosen as the best
2127 // connection. 2131 // connection.
2128 conn3->set_nominated(true); 2132 conn3->set_nominated(true);
2129 conn3->SignalNominated(conn3); 2133 conn3->SignalNominated(conn3);
2130 EXPECT_EQ(conn3, ch.best_connection()); 2134 EXPECT_EQ(conn3, ch.best_connection());
2131 2135
2132 // Even if another higher priority candidate arrives, 2136 // Even if another higher priority candidate arrives,
2133 // it will not be set as the best connection because the best connection 2137 // it will not be set as the best connection because the best connection
2134 // is nominated by the controlling side. 2138 // is nominated by the controlling side.
2135 ch.AddRemoteCandidate(CreateCandidate("4.4.4.4", 4, 100)); 2139 ch.AddRemoteCandidate(CreateHostCandidate("4.4.4.4", 4, 100));
2136 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); 2140 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4);
2137 ASSERT_TRUE(conn4 != nullptr); 2141 ASSERT_TRUE(conn4 != nullptr);
2138 EXPECT_EQ(conn3, ch.best_connection()); 2142 EXPECT_EQ(conn3, ch.best_connection());
2139 // But if it is nominated via use_candidate and writable, it will be set as 2143 // But if it is nominated via use_candidate and writable, it will be set as
2140 // the best connection. 2144 // the best connection.
2141 conn4->set_nominated(true); 2145 conn4->set_nominated(true);
2142 conn4->SignalNominated(conn4); 2146 conn4->SignalNominated(conn4);
2143 // Not switched yet because conn4 is not writable. 2147 // Not switched yet because conn4 is not writable.
2144 EXPECT_EQ(conn3, ch.best_connection()); 2148 EXPECT_EQ(conn3, ch.best_connection());
2145 // The best connection switches after conn4 becomes writable. 2149 // The best connection switches after conn4 becomes writable.
(...skipping 26 matching lines...) Expand all
2172 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2176 cricket::PROTO_UDP, &request, kIceUfrag[1], false);
2173 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2177 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2174 ASSERT_TRUE(conn1 != nullptr); 2178 ASSERT_TRUE(conn1 != nullptr);
2175 EXPECT_TRUE(port->sent_binding_response()); 2179 EXPECT_TRUE(port->sent_binding_response());
2176 EXPECT_EQ(conn1, ch.best_connection()); 2180 EXPECT_EQ(conn1, ch.best_connection());
2177 conn1->ReceivedPingResponse(); 2181 conn1->ReceivedPingResponse();
2178 EXPECT_EQ(conn1, ch.best_connection()); 2182 EXPECT_EQ(conn1, ch.best_connection());
2179 port->set_sent_binding_response(false); 2183 port->set_sent_binding_response(false);
2180 2184
2181 // Another connection is nominated via use_candidate. 2185 // Another connection is nominated via use_candidate.
2182 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2186 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2183 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2187 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2184 ASSERT_TRUE(conn2 != nullptr); 2188 ASSERT_TRUE(conn2 != nullptr);
2185 // Because it has a lower priority, the best connection is still conn1. 2189 // Because it has a lower priority, the best connection is still conn1.
2186 EXPECT_EQ(conn1, ch.best_connection()); 2190 EXPECT_EQ(conn1, ch.best_connection());
2187 // When it is nominated via use_candidate and writable, it is chosen as the 2191 // When it is nominated via use_candidate and writable, it is chosen as the
2188 // best connection. 2192 // best connection.
2189 conn2->ReceivedPingResponse(); // Become writable. 2193 conn2->ReceivedPingResponse(); // Become writable.
2190 conn2->set_nominated(true); 2194 conn2->set_nominated(true);
2191 conn2->SignalNominated(conn2); 2195 conn2->SignalNominated(conn2);
2192 EXPECT_EQ(conn2, ch.best_connection()); 2196 EXPECT_EQ(conn2, ch.best_connection());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
2234 // based on media received until the controlling side nominates a connection, 2238 // based on media received until the controlling side nominates a connection,
2235 // at which point the controlled side will select that connection as 2239 // at which point the controlled side will select that connection as
2236 // the "best connection". 2240 // the "best connection".
2237 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { 2241 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) {
2238 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2242 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2239 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); 2243 cricket::P2PTransportChannel ch("receiving state change", 1, &pa);
2240 PrepareChannel(&ch); 2244 PrepareChannel(&ch);
2241 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2245 ch.SetIceRole(cricket::ICEROLE_CONTROLLED);
2242 ch.Connect(); 2246 ch.Connect();
2243 ch.MaybeStartGathering(); 2247 ch.MaybeStartGathering();
2244 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 10)); 2248 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 10));
2245 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2249 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2246 ASSERT_TRUE(conn1 != nullptr); 2250 ASSERT_TRUE(conn1 != nullptr);
2247 EXPECT_EQ(conn1, ch.best_connection()); 2251 EXPECT_EQ(conn1, ch.best_connection());
2248 2252
2249 // If a data packet is received on conn2, the best connection should 2253 // If a data packet is received on conn2, the best connection should
2250 // switch to conn2 because the controlled side must mirror the media path 2254 // switch to conn2 because the controlled side must mirror the media path
2251 // chosen by the controlling side. 2255 // chosen by the controlling side.
2252 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2256 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2253 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2257 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2254 ASSERT_TRUE(conn2 != nullptr); 2258 ASSERT_TRUE(conn2 != nullptr);
2255 conn2->ReceivedPing(); // Start receiving. 2259 conn2->ReceivedPing(); // Start receiving.
2256 // Do not switch because it is not writable. 2260 // Do not switch because it is not writable.
2257 conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); 2261 conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0));
2258 EXPECT_EQ(conn1, ch.best_connection()); 2262 EXPECT_EQ(conn1, ch.best_connection());
2259 2263
2260 conn2->ReceivedPingResponse(); // Become writable. 2264 conn2->ReceivedPingResponse(); // Become writable.
2261 // Switch because it is writable. 2265 // Switch because it is writable.
2262 conn2->OnReadPacket("DEF", 3, rtc::CreatePacketTime(0)); 2266 conn2->OnReadPacket("DEF", 3, rtc::CreatePacketTime(0));
(...skipping 29 matching lines...) Expand all
2292 2296
2293 // When the current best connection is strong, lower-priority connections will 2297 // When the current best connection is strong, lower-priority connections will
2294 // be pruned. Otherwise, lower-priority connections are kept. 2298 // be pruned. Otherwise, lower-priority connections are kept.
2295 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { 2299 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) {
2296 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2300 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2297 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2301 cricket::P2PTransportChannel ch("test channel", 1, &pa);
2298 PrepareChannel(&ch); 2302 PrepareChannel(&ch);
2299 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2303 ch.SetIceRole(cricket::ICEROLE_CONTROLLED);
2300 ch.Connect(); 2304 ch.Connect();
2301 ch.MaybeStartGathering(); 2305 ch.MaybeStartGathering();
2302 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 2306 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2303 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2307 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2304 ASSERT_TRUE(conn1 != nullptr); 2308 ASSERT_TRUE(conn1 != nullptr);
2305 EXPECT_EQ(conn1, ch.best_connection()); 2309 EXPECT_EQ(conn1, ch.best_connection());
2306 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2310 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2307 2311
2308 // When a higher-priority, nominated candidate comes in, the connections with 2312 // When a higher-priority, nominated candidate comes in, the connections with
2309 // lower-priority are pruned. 2313 // lower-priority are pruned.
2310 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 10)); 2314 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10));
2311 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2315 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2312 ASSERT_TRUE(conn2 != nullptr); 2316 ASSERT_TRUE(conn2 != nullptr);
2313 conn2->ReceivedPingResponse(); // Becomes writable and receiving 2317 conn2->ReceivedPingResponse(); // Becomes writable and receiving
2314 conn2->set_nominated(true); 2318 conn2->set_nominated(true);
2315 conn2->SignalNominated(conn2); 2319 conn2->SignalNominated(conn2);
2316 EXPECT_TRUE_WAIT(conn1->pruned(), 3000); 2320 EXPECT_TRUE_WAIT(conn1->pruned(), 3000);
2317 2321
2318 ch.SetIceConfig(CreateIceConfig(500, false)); 2322 ch.SetIceConfig(CreateIceConfig(500, false));
2319 // Wait until conn2 becomes not receiving. 2323 // Wait until conn2 becomes not receiving.
2320 EXPECT_TRUE_WAIT(!conn2->receiving(), 3000); 2324 EXPECT_TRUE_WAIT(!conn2->receiving(), 3000);
2321 2325
2322 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 1)); 2326 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1));
2323 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2327 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2324 ASSERT_TRUE(conn3 != nullptr); 2328 ASSERT_TRUE(conn3 != nullptr);
2325 // The best connection should still be conn2. Even through conn3 has lower 2329 // The best connection should still be conn2. Even through conn3 has lower
2326 // priority and is not receiving/writable, it is not pruned because the best 2330 // priority and is not receiving/writable, it is not pruned because the best
2327 // connection is not receiving. 2331 // connection is not receiving.
2328 WAIT(conn3->pruned(), 1000); 2332 WAIT(conn3->pruned(), 1000);
2329 EXPECT_FALSE(conn3->pruned()); 2333 EXPECT_FALSE(conn3->pruned());
2330 } 2334 }
2331 2335
2332 // Test that GetState returns the state correctly. 2336 // Test that GetState returns the state correctly.
2333 TEST_F(P2PTransportChannelPingTest, TestGetState) { 2337 TEST_F(P2PTransportChannelPingTest, TestGetState) {
2334 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2338 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2335 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2339 cricket::P2PTransportChannel ch("test channel", 1, &pa);
2336 PrepareChannel(&ch); 2340 PrepareChannel(&ch);
2337 ch.Connect(); 2341 ch.Connect();
2338 ch.MaybeStartGathering(); 2342 ch.MaybeStartGathering();
2339 EXPECT_EQ(cricket::TransportChannelState::STATE_INIT, ch.GetState()); 2343 EXPECT_EQ(cricket::TransportChannelState::STATE_INIT, ch.GetState());
2340 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2344 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2341 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2345 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2342 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2346 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2343 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2347 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2344 ASSERT_TRUE(conn1 != nullptr); 2348 ASSERT_TRUE(conn1 != nullptr);
2345 ASSERT_TRUE(conn2 != nullptr); 2349 ASSERT_TRUE(conn2 != nullptr);
2346 // Now there are two connections, so the transport channel is connecting. 2350 // Now there are two connections, so the transport channel is connecting.
2347 EXPECT_EQ(cricket::TransportChannelState::STATE_CONNECTING, ch.GetState()); 2351 EXPECT_EQ(cricket::TransportChannelState::STATE_CONNECTING, ch.GetState());
2348 // |conn1| becomes writable and receiving; it then should prune |conn2|. 2352 // |conn1| becomes writable and receiving; it then should prune |conn2|.
2349 conn1->ReceivedPingResponse(); 2353 conn1->ReceivedPingResponse();
2350 EXPECT_TRUE_WAIT(conn2->pruned(), 1000); 2354 EXPECT_TRUE_WAIT(conn2->pruned(), 1000);
2351 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState()); 2355 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState());
2352 conn1->Prune(); // All connections are pruned. 2356 conn1->Prune(); // All connections are pruned.
2353 // Need to wait until the channel state is updated. 2357 // Need to wait until the channel state is updated.
2354 EXPECT_EQ_WAIT(cricket::TransportChannelState::STATE_FAILED, ch.GetState(), 2358 EXPECT_EQ_WAIT(cricket::TransportChannelState::STATE_FAILED, ch.GetState(),
2355 1000); 2359 1000);
2356 } 2360 }
2357 2361
2358 // Test that when a low-priority connection is pruned, it is not deleted 2362 // Test that when a low-priority connection is pruned, it is not deleted
2359 // right away, and it can become active and be pruned again. 2363 // right away, and it can become active and be pruned again.
2360 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { 2364 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) {
2361 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2365 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2362 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2366 cricket::P2PTransportChannel ch("test channel", 1, &pa);
2363 PrepareChannel(&ch); 2367 PrepareChannel(&ch);
2364 ch.SetIceConfig(CreateIceConfig(1000, false)); 2368 ch.SetIceConfig(CreateIceConfig(1000, false));
2365 ch.Connect(); 2369 ch.Connect();
2366 ch.MaybeStartGathering(); 2370 ch.MaybeStartGathering();
2367 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2371 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2368 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2372 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2369 ASSERT_TRUE(conn1 != nullptr); 2373 ASSERT_TRUE(conn1 != nullptr);
2370 EXPECT_EQ(conn1, ch.best_connection()); 2374 EXPECT_EQ(conn1, ch.best_connection());
2371 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2375 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2372 2376
2373 // Add a low-priority connection |conn2|, which will be pruned, but it will 2377 // Add a low-priority connection |conn2|, which will be pruned, but it will
2374 // not be deleted right away. Once the current best connection becomes not 2378 // not be deleted right away. Once the current best connection becomes not
2375 // receiving, |conn2| will start to ping and upon receiving the ping response, 2379 // receiving, |conn2| will start to ping and upon receiving the ping response,
2376 // it will become the best connection. 2380 // it will become the best connection.
2377 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2381 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2378 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2382 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2379 ASSERT_TRUE(conn2 != nullptr); 2383 ASSERT_TRUE(conn2 != nullptr);
2380 EXPECT_TRUE_WAIT(!conn2->active(), 1000); 2384 EXPECT_TRUE_WAIT(!conn2->active(), 1000);
2381 // |conn2| should not send a ping yet. 2385 // |conn2| should not send a ping yet.
2382 EXPECT_EQ(cricket::Connection::STATE_WAITING, conn2->state()); 2386 EXPECT_EQ(cricket::Connection::STATE_WAITING, conn2->state());
2383 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState()); 2387 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState());
2384 // Wait for |conn1| becoming not receiving. 2388 // Wait for |conn1| becoming not receiving.
2385 EXPECT_TRUE_WAIT(!conn1->receiving(), 3000); 2389 EXPECT_TRUE_WAIT(!conn1->receiving(), 3000);
2386 // Make sure conn2 is not deleted. 2390 // Make sure conn2 is not deleted.
2387 conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2391 conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
(...skipping 12 matching lines...) Expand all
2400 2404
2401 // Test that if all connections in a channel has timed out on writing, they 2405 // Test that if all connections in a channel has timed out on writing, they
2402 // will all be deleted. We use Prune to simulate write_time_out. 2406 // will all be deleted. We use Prune to simulate write_time_out.
2403 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { 2407 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) {
2404 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2408 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2405 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2409 cricket::P2PTransportChannel ch("test channel", 1, &pa);
2406 PrepareChannel(&ch); 2410 PrepareChannel(&ch);
2407 ch.Connect(); 2411 ch.Connect();
2408 ch.MaybeStartGathering(); 2412 ch.MaybeStartGathering();
2409 // Have one connection only but later becomes write-time-out. 2413 // Have one connection only but later becomes write-time-out.
2410 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2414 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2411 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2415 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2412 ASSERT_TRUE(conn1 != nullptr); 2416 ASSERT_TRUE(conn1 != nullptr);
2413 conn1->ReceivedPing(); // Becomes receiving 2417 conn1->ReceivedPing(); // Becomes receiving
2414 conn1->Prune(); 2418 conn1->Prune();
2415 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); 2419 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000);
2416 2420
2417 // Have two connections but both become write-time-out later. 2421 // Have two connections but both become write-time-out later.
2418 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2422 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2419 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2423 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2420 ASSERT_TRUE(conn2 != nullptr); 2424 ASSERT_TRUE(conn2 != nullptr);
2421 conn2->ReceivedPing(); // Becomes receiving 2425 conn2->ReceivedPing(); // Becomes receiving
2422 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 2)); 2426 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 2));
2423 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2427 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2424 ASSERT_TRUE(conn3 != nullptr); 2428 ASSERT_TRUE(conn3 != nullptr);
2425 conn3->ReceivedPing(); // Becomes receiving 2429 conn3->ReceivedPing(); // Becomes receiving
2426 // Now prune both conn2 and conn3; they will be deleted soon. 2430 // Now prune both conn2 and conn3; they will be deleted soon.
2427 conn2->Prune(); 2431 conn2->Prune();
2428 conn3->Prune(); 2432 conn3->Prune();
2429 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); 2433 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000);
2430 } 2434 }
2431 2435
2432 // Test that after a port allocator session is started, it will be stopped 2436 // Test that after a port allocator session is started, it will be stopped
2433 // when a new connection becomes writable and receiving. Also test that this 2437 // when a new connection becomes writable and receiving. Also test that this
2434 // holds even if the transport channel did not lose the writability. 2438 // holds even if the transport channel did not lose the writability.
2435 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { 2439 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) {
2436 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2440 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2437 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2441 cricket::P2PTransportChannel ch("test channel", 1, &pa);
2438 PrepareChannel(&ch); 2442 PrepareChannel(&ch);
2439 ch.SetIceConfig(CreateIceConfig(2000, false)); 2443 ch.SetIceConfig(CreateIceConfig(2000, false));
2440 ch.Connect(); 2444 ch.Connect();
2441 ch.MaybeStartGathering(); 2445 ch.MaybeStartGathering();
2442 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2446 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2443 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2447 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2444 ASSERT_TRUE(conn1 != nullptr); 2448 ASSERT_TRUE(conn1 != nullptr);
2445 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2449 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2446 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); 2450 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts());
2447 2451
2448 // Restart gathering even if the transport channel is still writable. 2452 // Restart gathering even if the transport channel is still writable.
2449 // It should stop getting ports after a new connection becomes strongly 2453 // It should stop getting ports after a new connection becomes strongly
2450 // connected. 2454 // connected.
2451 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]); 2455 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]);
2452 ch.MaybeStartGathering(); 2456 ch.MaybeStartGathering();
2453 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 100)); 2457 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 100));
2454 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2458 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2455 ASSERT_TRUE(conn2 != nullptr); 2459 ASSERT_TRUE(conn2 != nullptr);
2456 conn2->ReceivedPingResponse(); // Becomes writable and receiving 2460 conn2->ReceivedPingResponse(); // Becomes writable and receiving
2457 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); 2461 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts());
2458 } 2462 }
2463
2464 class P2PTransportChannelMostLikelyToWorkFirstTest
2465 : public P2PTransportChannelPingTest {
2466 public:
2467 P2PTransportChannelMostLikelyToWorkFirstTest()
2468 : turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr) {
2469 network_manager_.AddInterface(kPublicAddrs[0]);
2470 allocator_.reset(new cricket::BasicPortAllocator(
2471 &network_manager_, ServerAddresses(), rtc::SocketAddress(),
2472 rtc::SocketAddress(), rtc::SocketAddress()));
2473 allocator_->set_flags(allocator_->flags() |
2474 cricket::PORTALLOCATOR_DISABLE_STUN |
2475 cricket::PORTALLOCATOR_DISABLE_TCP);
2476 cricket::RelayServerConfig config(cricket::RELAY_TURN);
2477 config.credentials = kRelayCredentials;
2478 config.ports.push_back(
2479 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
2480 allocator_->AddTurnServer(config);
2481 allocator_->set_step_delay(kMinimumStepDelay);
2482 }
2483
2484 cricket::P2PTransportChannel& StartTransportChannel(
2485 bool prioritize_most_likely_to_work,
2486 uint32_t max_strong_delay) {
2487 channel_.reset(
2488 new cricket::P2PTransportChannel("checks", 1, nullptr, allocator()));
2489 cricket::IceConfig config = channel_->GetIceConfig();
2490 config.prioritize_most_likely_candidate_pairs =
2491 prioritize_most_likely_to_work;
2492 config.max_strong_delay = max_strong_delay;
2493 channel_->SetIceConfig(config);
2494 PrepareChannel(channel_.get());
2495 channel_->Connect();
2496 channel_->MaybeStartGathering();
2497 return *channel_.get();
2498 }
2499
2500 cricket::BasicPortAllocator* allocator() { return allocator_.get(); }
2501 cricket::TestTurnServer* turn_server() { return &turn_server_; }
2502
2503 // This verifies the next pingable connection has the expected candidates'
2504 // types and, for relay local candidate, the expected relay protocol and ping
2505 // it.
2506 void VerifyNextPingableConnection(
2507 const std::string& local_candidate_type,
2508 const std::string& remote_candidate_type,
2509 const std::string& relay_protocol_type = cricket::UDP_PROTOCOL_NAME) {
2510 cricket::Connection* conn = channel_->FindNextPingableConnection();
2511 EXPECT_EQ(conn->local_candidate().type(), local_candidate_type);
2512 if (conn->local_candidate().type() == cricket::RELAY_PORT_TYPE) {
2513 EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type);
2514 }
2515 EXPECT_EQ(conn->remote_candidate().type(), remote_candidate_type);
2516 }
2517
2518 cricket::Candidate CreateRelayCandidate(const std::string& ip,
2519 int port,
2520 int priority,
2521 const std::string& ufrag = "") {
2522 cricket::Candidate c = CreateHostCandidate(ip, port, priority, ufrag);
2523 c.set_type(cricket::RELAY_PORT_TYPE);
2524 return c;
2525 }
2526
2527 private:
2528 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
2529 rtc::FakeNetworkManager network_manager_;
2530 cricket::TestTurnServer turn_server_;
2531 rtc::scoped_ptr<cricket::P2PTransportChannel> channel_;
2532 };
2533
2534 // Test that Relay/Relay connections will be pinged first when no other
2535 // connections have been pinged yet, unless we need to ping a trigger check or
2536 // we have a best connection.
2537 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest,
2538 TestRelayRelayFirstWhenNothingPingedYet) {
2539 const uint32_t max_strong_delay = 100;
2540 cricket::P2PTransportChannel& ch =
2541 StartTransportChannel(true, max_strong_delay);
2542 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2543 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE);
2544 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2545
2546 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2547 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2548
2549 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2550
2551 // Relay/Relay should be the first pingable connection.
2552 cricket::Connection* conn = ch.FindNextPingableConnection();
2553 EXPECT_EQ(conn->local_candidate().type(), cricket::RELAY_PORT_TYPE);
2554 EXPECT_EQ(conn->remote_candidate().type(), cricket::RELAY_PORT_TYPE);
2555
2556 // Unless that we have a trigger check waiting to be pinged.
2557 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2558 EXPECT_EQ(conn2->local_candidate().type(), cricket::LOCAL_PORT_TYPE);
2559 EXPECT_EQ(conn2->remote_candidate().type(), cricket::LOCAL_PORT_TYPE);
2560 conn2->ReceivedPing();
2561 EXPECT_EQ(conn2, ch.FindNextPingableConnection());
2562
2563 // Make conn3 the best connection.
2564 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2565 EXPECT_EQ(conn3->local_candidate().type(), cricket::LOCAL_PORT_TYPE);
2566 EXPECT_EQ(conn3->remote_candidate().type(), cricket::RELAY_PORT_TYPE);
2567 conn3->ReceivedPingResponse();
2568 ASSERT_TRUE(conn3->writable());
2569 conn3->ReceivedPing();
2570
2571 // Verify that conn3 will be the "best connection" since it is readable and
2572 // writable. After |MAX_CURRENT_STRONG_DELAY|, it should be the next pingable
2573 // connection.
2574 EXPECT_TRUE_WAIT(conn3 == ch.best_connection(), 5000);
2575 WAIT(false, max_strong_delay + 100);
2576 conn3->ReceivedPingResponse();
2577 ASSERT_TRUE(conn3->writable());
2578 EXPECT_EQ(conn3, ch.FindNextPingableConnection());
2579 }
2580
2581 // Test that Relay/Relay connections will be pinged first when everything has
2582 // been pinged even if the Relay/Relay connection wasn't the first to be pinged
2583 // in the first round.
2584 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest,
2585 TestRelayRelayFirstWhenEverythingPinged) {
2586 cricket::P2PTransportChannel& ch = StartTransportChannel(true, 100);
2587 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2588 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE);
2589 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2590
2591 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2592 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000);
2593
2594 // Initially, only have Local/Local and Local/Relay.
2595 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE,
2596 cricket::LOCAL_PORT_TYPE);
2597 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2598 cricket::LOCAL_PORT_TYPE);
2599
2600 // Remote Relay candidate arrives.
2601 ch.AddRemoteCandidate(CreateRelayCandidate("2.2.2.2", 2, 2));
2602 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2603
2604 // Relay/Relay should be the first since it hasn't been pinged before.
2605 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2606 cricket::RELAY_PORT_TYPE);
2607
2608 // Local/Relay is the final one.
2609 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE,
2610 cricket::RELAY_PORT_TYPE);
2611
2612 // Now, every connection has been pinged once. The next one should be
2613 // Relay/Relay.
2614 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2615 cricket::RELAY_PORT_TYPE);
2616 }
2617
2618 // Test that when we receive a new remote candidate, they will be tried first
2619 // before we re-ping Relay/Relay connections again.
2620 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest,
2621 TestNoStarvationOnNonRelayConnection) {
2622 cricket::P2PTransportChannel& ch = StartTransportChannel(true, 100);
2623 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2624 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE);
2625 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2626
2627 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2628 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000);
2629
2630 // Initially, only have Relay/Relay and Local/Relay. Ping Relay/Relay first.
2631 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2632 cricket::RELAY_PORT_TYPE);
2633
2634 // Next, ping Local/Relay.
2635 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE,
2636 cricket::RELAY_PORT_TYPE);
2637
2638 // Remote Local candidate arrives.
2639 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2640 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2641
2642 // Local/Local should be the first since it hasn't been pinged before.
2643 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE,
2644 cricket::LOCAL_PORT_TYPE);
2645
2646 // Relay/Local is the final one.
2647 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2648 cricket::LOCAL_PORT_TYPE);
2649
2650 // Now, every connection has been pinged once. The next one should be
2651 // Relay/Relay.
2652 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2653 cricket::RELAY_PORT_TYPE);
2654 }
2655
2656 // Test the ping sequence is UDP Relay/Relay followed by TCP Relay/Relay,
2657 // followed by the rest.
2658 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, TestTcpTurn) {
2659 // Add a Tcp Turn server.
2660 turn_server()->AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
2661 cricket::RelayServerConfig config(cricket::RELAY_TURN);
2662 config.credentials = kRelayCredentials;
2663 config.ports.push_back(
2664 cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false));
2665 allocator()->AddTurnServer(config);
2666
2667 cricket::P2PTransportChannel& ch = StartTransportChannel(true, 100);
2668 EXPECT_TRUE_WAIT(ch.ports().size() == 3, 5000);
2669 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE);
2670 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2671 EXPECT_EQ(ch.ports()[2]->Type(), cricket::RELAY_PORT_TYPE);
2672
2673 // Remote Relay candidate arrives.
2674 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2675 EXPECT_TRUE_WAIT(ch.connections().size() == 3, 5000);
2676
2677 // UDP Relay/Relay should be pinged first.
2678 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2679 cricket::RELAY_PORT_TYPE);
2680
2681 // TCP Relay/Relay is the next.
2682 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2683 cricket::RELAY_PORT_TYPE,
2684 cricket::TCP_PROTOCOL_NAME);
2685
2686 // Finally, Local/Relay will be pinged.
2687 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE,
2688 cricket::RELAY_PORT_TYPE);
2689 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698