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

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: fix a bug Created 4 years, 11 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') | no next file » | 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 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 1664 matching lines...) Expand 10 before | Expand all | Expand 10 after
1758 vss_(new rtc::VirtualSocketServer(pss_.get())), 1760 vss_(new rtc::VirtualSocketServer(pss_.get())),
1759 ss_scope_(vss_.get()) {} 1761 ss_scope_(vss_.get()) {}
1760 1762
1761 protected: 1763 protected:
1762 void PrepareChannel(cricket::P2PTransportChannel* ch) { 1764 void PrepareChannel(cricket::P2PTransportChannel* ch) {
1763 ch->SetIceRole(cricket::ICEROLE_CONTROLLING); 1765 ch->SetIceRole(cricket::ICEROLE_CONTROLLING);
1764 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]); 1766 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]);
1765 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]); 1767 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]);
1766 } 1768 }
1767 1769
1768 cricket::Candidate CreateCandidate(const std::string& ip, 1770 cricket::Candidate CreateLocalCandidate(const std::string& ip,
1769 int port, 1771 int port,
1770 int priority, 1772 int priority,
1771 const std::string& ufrag = "") { 1773 const std::string& ufrag = "") {
1772 cricket::Candidate c; 1774 cricket::Candidate c;
1773 c.set_address(rtc::SocketAddress(ip, port)); 1775 c.set_address(rtc::SocketAddress(ip, port));
1774 c.set_component(1); 1776 c.set_component(1);
1775 c.set_protocol(cricket::UDP_PROTOCOL_NAME); 1777 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
1776 c.set_priority(priority); 1778 c.set_priority(priority);
1777 c.set_username(ufrag); 1779 c.set_username(ufrag);
1778 return c; 1780 return c;
1779 } 1781 }
1780 1782
1781 cricket::Connection* WaitForConnectionTo(cricket::P2PTransportChannel* ch, 1783 cricket::Connection* WaitForConnectionTo(cricket::P2PTransportChannel* ch,
(...skipping 25 matching lines...) Expand all
1807 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_; 1809 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
1808 rtc::SocketServerScope ss_scope_; 1810 rtc::SocketServerScope ss_scope_;
1809 }; 1811 };
1810 1812
1811 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { 1813 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) {
1812 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 1814 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
1813 cricket::P2PTransportChannel ch("trigger checks", 1, nullptr, &pa); 1815 cricket::P2PTransportChannel ch("trigger checks", 1, nullptr, &pa);
1814 PrepareChannel(&ch); 1816 PrepareChannel(&ch);
1815 ch.Connect(); 1817 ch.Connect();
1816 ch.MaybeStartGathering(); 1818 ch.MaybeStartGathering();
1817 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 1819 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1));
1818 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2)); 1820 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 2));
1819 1821
1820 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1822 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1821 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 1823 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
1822 ASSERT_TRUE(conn1 != nullptr); 1824 ASSERT_TRUE(conn1 != nullptr);
1823 ASSERT_TRUE(conn2 != nullptr); 1825 ASSERT_TRUE(conn2 != nullptr);
1824 1826
1825 // Before a triggered check, the first connection to ping is the 1827 // Before a triggered check, the first connection to ping is the
1826 // highest priority one. 1828 // highest priority one.
1827 EXPECT_EQ(conn2, ch.FindNextPingableConnection()); 1829 EXPECT_EQ(conn2, ch.FindNextPingableConnection());
1828 1830
1829 // Receiving a ping causes a triggered check which should make conn1 1831 // Receiving a ping causes a triggered check which should make conn1
1830 // be pinged first instead of conn2, even though conn2 has a higher 1832 // be pinged first instead of conn2, even though conn2 has a higher
1831 // priority. 1833 // priority.
1832 conn1->ReceivedPing(); 1834 conn1->ReceivedPing();
1833 EXPECT_EQ(conn1, ch.FindNextPingableConnection()); 1835 EXPECT_EQ(conn1, ch.FindNextPingableConnection());
1834 } 1836 }
1835 1837
1836 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { 1838 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) {
1837 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 1839 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
1838 cricket::P2PTransportChannel ch("trigger checks", 1, nullptr, &pa); 1840 cricket::P2PTransportChannel ch("trigger checks", 1, nullptr, &pa);
1839 PrepareChannel(&ch); 1841 PrepareChannel(&ch);
1840 ch.Connect(); 1842 ch.Connect();
1841 ch.MaybeStartGathering(); 1843 ch.MaybeStartGathering();
1842 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 1844 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1));
1843 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2)); 1845 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 2));
1844 1846
1845 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1847 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1846 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 1848 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
1847 ASSERT_TRUE(conn1 != nullptr); 1849 ASSERT_TRUE(conn1 != nullptr);
1848 ASSERT_TRUE(conn2 != nullptr); 1850 ASSERT_TRUE(conn2 != nullptr);
1849 1851
1850 EXPECT_EQ(conn2, ch.FindNextPingableConnection()); 1852 EXPECT_EQ(conn2, ch.FindNextPingableConnection());
1851 conn1->ReceivedPingResponse(); 1853 conn1->ReceivedPingResponse();
1852 ASSERT_TRUE(conn1->writable()); 1854 ASSERT_TRUE(conn1->writable());
1853 conn1->ReceivedPing(); 1855 conn1->ReceivedPing();
(...skipping 10 matching lines...) Expand all
1864 // although the ICE pwd in the remote candidate will be set when the ICE 1866 // although the ICE pwd in the remote candidate will be set when the ICE
1865 // credentials arrive. If a remote candidate is added with the current ICE 1867 // credentials arrive. If a remote candidate is added with the current ICE
1866 // ufrag, its pwd and generation will be set properly. 1868 // ufrag, its pwd and generation will be set properly.
1867 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { 1869 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) {
1868 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 1870 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
1869 cricket::P2PTransportChannel ch("add candidate", 1, nullptr, &pa); 1871 cricket::P2PTransportChannel ch("add candidate", 1, nullptr, &pa);
1870 PrepareChannel(&ch); 1872 PrepareChannel(&ch);
1871 ch.Connect(); 1873 ch.Connect();
1872 ch.MaybeStartGathering(); 1874 ch.MaybeStartGathering();
1873 // Add a candidate with a future ufrag. 1875 // Add a candidate with a future ufrag.
1874 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1, kIceUfrag[2])); 1876 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1, kIceUfrag[2]));
1875 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1877 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1876 ASSERT_TRUE(conn1 != nullptr); 1878 ASSERT_TRUE(conn1 != nullptr);
1877 const cricket::Candidate& candidate = conn1->remote_candidate(); 1879 const cricket::Candidate& candidate = conn1->remote_candidate();
1878 EXPECT_EQ(kIceUfrag[2], candidate.username()); 1880 EXPECT_EQ(kIceUfrag[2], candidate.username());
1879 EXPECT_TRUE(candidate.password().empty()); 1881 EXPECT_TRUE(candidate.password().empty());
1880 EXPECT_TRUE(ch.FindNextPingableConnection() == nullptr); 1882 EXPECT_TRUE(ch.FindNextPingableConnection() == nullptr);
1881 1883
1882 // Set the remote credentials with the "future" ufrag. 1884 // Set the remote credentials with the "future" ufrag.
1883 // This should set the ICE pwd in the remote candidate of |conn1|, making 1885 // This should set the ICE pwd in the remote candidate of |conn1|, making
1884 // it pingable. 1886 // it pingable.
1885 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]); 1887 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]);
1886 EXPECT_EQ(kIceUfrag[2], candidate.username()); 1888 EXPECT_EQ(kIceUfrag[2], candidate.username());
1887 EXPECT_EQ(kIcePwd[2], candidate.password()); 1889 EXPECT_EQ(kIcePwd[2], candidate.password());
1888 EXPECT_EQ(conn1, ch.FindNextPingableConnection()); 1890 EXPECT_EQ(conn1, ch.FindNextPingableConnection());
1889 1891
1890 // Add a candidate with an old ufrag. No connection will be created. 1892 // Add a candidate with an old ufrag. No connection will be created.
1891 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2, kIceUfrag[1])); 1893 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 2, kIceUfrag[1]));
1892 rtc::Thread::Current()->ProcessMessages(500); 1894 rtc::Thread::Current()->ProcessMessages(500);
1893 EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr); 1895 EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr);
1894 1896
1895 // Add a candidate with the current ufrag, its pwd and generation will be 1897 // Add a candidate with the current ufrag, its pwd and generation will be
1896 // assigned, even if the generation is not set. 1898 // assigned, even if the generation is not set.
1897 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 0, kIceUfrag[2])); 1899 ch.AddRemoteCandidate(CreateLocalCandidate("3.3.3.3", 3, 0, kIceUfrag[2]));
1898 cricket::Connection* conn3 = nullptr; 1900 cricket::Connection* conn3 = nullptr;
1899 ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr, 1901 ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr,
1900 3000); 1902 3000);
1901 const cricket::Candidate& new_candidate = conn3->remote_candidate(); 1903 const cricket::Candidate& new_candidate = conn3->remote_candidate();
1902 EXPECT_EQ(kIcePwd[2], new_candidate.password()); 1904 EXPECT_EQ(kIcePwd[2], new_candidate.password());
1903 EXPECT_EQ(1U, new_candidate.generation()); 1905 EXPECT_EQ(1U, new_candidate.generation());
1904 1906
1905 // Check that the pwd of all remote candidates are properly assigned. 1907 // Check that the pwd of all remote candidates are properly assigned.
1906 for (const cricket::RemoteCandidate& candidate : ch.remote_candidates()) { 1908 for (const cricket::RemoteCandidate& candidate : ch.remote_candidates()) {
1907 EXPECT_TRUE(candidate.username() == kIceUfrag[1] || 1909 EXPECT_TRUE(candidate.username() == kIceUfrag[1] ||
1908 candidate.username() == kIceUfrag[2]); 1910 candidate.username() == kIceUfrag[2]);
1909 if (candidate.username() == kIceUfrag[1]) { 1911 if (candidate.username() == kIceUfrag[1]) {
1910 EXPECT_EQ(kIcePwd[1], candidate.password()); 1912 EXPECT_EQ(kIcePwd[1], candidate.password());
1911 } else if (candidate.username() == kIceUfrag[2]) { 1913 } else if (candidate.username() == kIceUfrag[2]) {
1912 EXPECT_EQ(kIcePwd[2], candidate.password()); 1914 EXPECT_EQ(kIcePwd[2], candidate.password());
1913 } 1915 }
1914 } 1916 }
1915 } 1917 }
1916 1918
1917 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { 1919 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) {
1918 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 1920 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
1919 cricket::P2PTransportChannel ch("connection resurrection", 1, nullptr, &pa); 1921 cricket::P2PTransportChannel ch("connection resurrection", 1, nullptr, &pa);
1920 PrepareChannel(&ch); 1922 PrepareChannel(&ch);
1921 ch.Connect(); 1923 ch.Connect();
1922 ch.MaybeStartGathering(); 1924 ch.MaybeStartGathering();
1923 1925
1924 // Create conn1 and keep track of original candidate priority. 1926 // Create conn1 and keep track of original candidate priority.
1925 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 1927 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1));
1926 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1928 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1927 ASSERT_TRUE(conn1 != nullptr); 1929 ASSERT_TRUE(conn1 != nullptr);
1928 uint32_t remote_priority = conn1->remote_candidate().priority(); 1930 uint32_t remote_priority = conn1->remote_candidate().priority();
1929 1931
1930 // Create a higher priority candidate and make the connection 1932 // Create a higher priority candidate and make the connection
1931 // receiving/writable. This will prune conn1. 1933 // receiving/writable. This will prune conn1.
1932 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 2)); 1934 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 2));
1933 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 1935 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
1934 ASSERT_TRUE(conn2 != nullptr); 1936 ASSERT_TRUE(conn2 != nullptr);
1935 conn2->ReceivedPing(); 1937 conn2->ReceivedPing();
1936 conn2->ReceivedPingResponse(); 1938 conn2->ReceivedPingResponse();
1937 1939
1938 // Wait for conn1 to be pruned. 1940 // Wait for conn1 to be pruned.
1939 EXPECT_TRUE_WAIT(conn1->pruned(), 3000); 1941 EXPECT_TRUE_WAIT(conn1->pruned(), 3000);
1940 // Destroy the connection to test SignalUnknownAddress. 1942 // Destroy the connection to test SignalUnknownAddress.
1941 conn1->Destroy(); 1943 conn1->Destroy();
1942 EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, 1000); 1944 EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, 1000);
(...skipping 30 matching lines...) Expand all
1973 PrepareChannel(&ch); 1975 PrepareChannel(&ch);
1974 // Default receiving timeout and checking receiving delay should not be too 1976 // Default receiving timeout and checking receiving delay should not be too
1975 // small. 1977 // small.
1976 EXPECT_LE(1000, ch.receiving_timeout()); 1978 EXPECT_LE(1000, ch.receiving_timeout());
1977 EXPECT_LE(200, ch.check_receiving_delay()); 1979 EXPECT_LE(200, ch.check_receiving_delay());
1978 ch.SetIceConfig(CreateIceConfig(500, false)); 1980 ch.SetIceConfig(CreateIceConfig(500, false));
1979 EXPECT_EQ(500, ch.receiving_timeout()); 1981 EXPECT_EQ(500, ch.receiving_timeout());
1980 EXPECT_EQ(50, ch.check_receiving_delay()); 1982 EXPECT_EQ(50, ch.check_receiving_delay());
1981 ch.Connect(); 1983 ch.Connect();
1982 ch.MaybeStartGathering(); 1984 ch.MaybeStartGathering();
1983 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 1985 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1));
1984 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 1986 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
1985 ASSERT_TRUE(conn1 != nullptr); 1987 ASSERT_TRUE(conn1 != nullptr);
1986 1988
1987 conn1->ReceivedPing(); 1989 conn1->ReceivedPing();
1988 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); 1990 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0));
1989 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000); 1991 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000);
1990 EXPECT_TRUE_WAIT(ch.receiving(), 1000); 1992 EXPECT_TRUE_WAIT(ch.receiving(), 1000);
1991 EXPECT_TRUE_WAIT(!ch.receiving(), 1000); 1993 EXPECT_TRUE_WAIT(!ch.receiving(), 1000);
1992 } 1994 }
1993 1995
1994 // The controlled side will select a connection as the "best connection" based 1996 // The controlled side will select a connection as the "best connection" based
1995 // on priority until the controlling side nominates a connection, at which 1997 // on priority until the controlling side nominates a connection, at which
1996 // point the controlled side will select that connection as the 1998 // point the controlled side will select that connection as the
1997 // "best connection". 1999 // "best connection".
1998 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { 2000 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) {
1999 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2001 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2000 cricket::P2PTransportChannel ch("receiving state change", 1, nullptr, &pa); 2002 cricket::P2PTransportChannel ch("receiving state change", 1, nullptr, &pa);
2001 PrepareChannel(&ch); 2003 PrepareChannel(&ch);
2002 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2004 ch.SetIceRole(cricket::ICEROLE_CONTROLLED);
2003 ch.Connect(); 2005 ch.Connect();
2004 ch.MaybeStartGathering(); 2006 ch.MaybeStartGathering();
2005 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 2007 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1));
2006 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2008 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2007 ASSERT_TRUE(conn1 != nullptr); 2009 ASSERT_TRUE(conn1 != nullptr);
2008 EXPECT_EQ(conn1, ch.best_connection()); 2010 EXPECT_EQ(conn1, ch.best_connection());
2009 2011
2010 // When a higher priority candidate comes in, the new connection is chosen 2012 // When a higher priority candidate comes in, the new connection is chosen
2011 // as the best connection. 2013 // as the best connection.
2012 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 10)); 2014 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 10));
2013 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2015 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2014 ASSERT_TRUE(conn2 != nullptr); 2016 ASSERT_TRUE(conn2 != nullptr);
2015 EXPECT_EQ(conn2, ch.best_connection()); 2017 EXPECT_EQ(conn2, ch.best_connection());
2016 2018
2017 // If a stun request with use-candidate attribute arrives, the receiving 2019 // If a stun request with use-candidate attribute arrives, the receiving
2018 // connection will be set as the best connection, even though 2020 // connection will be set as the best connection, even though
2019 // its priority is lower. 2021 // its priority is lower.
2020 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 1)); 2022 ch.AddRemoteCandidate(CreateLocalCandidate("3.3.3.3", 3, 1));
2021 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2023 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2022 ASSERT_TRUE(conn3 != nullptr); 2024 ASSERT_TRUE(conn3 != nullptr);
2023 // Because it has a lower priority, the best connection is still conn2. 2025 // Because it has a lower priority, the best connection is still conn2.
2024 EXPECT_EQ(conn2, ch.best_connection()); 2026 EXPECT_EQ(conn2, ch.best_connection());
2025 conn3->ReceivedPingResponse(); // Become writable. 2027 conn3->ReceivedPingResponse(); // Become writable.
2026 // But if it is nominated via use_candidate, it is chosen as the best 2028 // But if it is nominated via use_candidate, it is chosen as the best
2027 // connection. 2029 // connection.
2028 conn3->set_nominated(true); 2030 conn3->set_nominated(true);
2029 conn3->SignalNominated(conn3); 2031 conn3->SignalNominated(conn3);
2030 EXPECT_EQ(conn3, ch.best_connection()); 2032 EXPECT_EQ(conn3, ch.best_connection());
2031 2033
2032 // Even if another higher priority candidate arrives, 2034 // Even if another higher priority candidate arrives,
2033 // it will not be set as the best connection because the best connection 2035 // it will not be set as the best connection because the best connection
2034 // is nominated by the controlling side. 2036 // is nominated by the controlling side.
2035 ch.AddRemoteCandidate(CreateCandidate("4.4.4.4", 4, 100)); 2037 ch.AddRemoteCandidate(CreateLocalCandidate("4.4.4.4", 4, 100));
2036 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); 2038 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4);
2037 ASSERT_TRUE(conn4 != nullptr); 2039 ASSERT_TRUE(conn4 != nullptr);
2038 EXPECT_EQ(conn3, ch.best_connection()); 2040 EXPECT_EQ(conn3, ch.best_connection());
2039 // But if it is nominated via use_candidate and writable, it will be set as 2041 // But if it is nominated via use_candidate and writable, it will be set as
2040 // the best connection. 2042 // the best connection.
2041 conn4->set_nominated(true); 2043 conn4->set_nominated(true);
2042 conn4->SignalNominated(conn4); 2044 conn4->SignalNominated(conn4);
2043 // Not switched yet because conn4 is not writable. 2045 // Not switched yet because conn4 is not writable.
2044 EXPECT_EQ(conn3, ch.best_connection()); 2046 EXPECT_EQ(conn3, ch.best_connection());
2045 // The best connection switches after conn4 becomes writable. 2047 // The best connection switches after conn4 becomes writable.
(...skipping 26 matching lines...) Expand all
2072 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2074 cricket::PROTO_UDP, &request, kIceUfrag[1], false);
2073 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2075 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2074 ASSERT_TRUE(conn1 != nullptr); 2076 ASSERT_TRUE(conn1 != nullptr);
2075 EXPECT_TRUE(port->sent_binding_response()); 2077 EXPECT_TRUE(port->sent_binding_response());
2076 EXPECT_EQ(conn1, ch.best_connection()); 2078 EXPECT_EQ(conn1, ch.best_connection());
2077 conn1->ReceivedPingResponse(); 2079 conn1->ReceivedPingResponse();
2078 EXPECT_EQ(conn1, ch.best_connection()); 2080 EXPECT_EQ(conn1, ch.best_connection());
2079 port->set_sent_binding_response(false); 2081 port->set_sent_binding_response(false);
2080 2082
2081 // Another connection is nominated via use_candidate. 2083 // Another connection is nominated via use_candidate.
2082 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2084 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 1));
2083 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2085 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2084 ASSERT_TRUE(conn2 != nullptr); 2086 ASSERT_TRUE(conn2 != nullptr);
2085 // Because it has a lower priority, the best connection is still conn1. 2087 // Because it has a lower priority, the best connection is still conn1.
2086 EXPECT_EQ(conn1, ch.best_connection()); 2088 EXPECT_EQ(conn1, ch.best_connection());
2087 // When it is nominated via use_candidate and writable, it is chosen as the 2089 // When it is nominated via use_candidate and writable, it is chosen as the
2088 // best connection. 2090 // best connection.
2089 conn2->ReceivedPingResponse(); // Become writable. 2091 conn2->ReceivedPingResponse(); // Become writable.
2090 conn2->set_nominated(true); 2092 conn2->set_nominated(true);
2091 conn2->SignalNominated(conn2); 2093 conn2->SignalNominated(conn2);
2092 EXPECT_EQ(conn2, ch.best_connection()); 2094 EXPECT_EQ(conn2, ch.best_connection());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
2134 // based on media received until the controlling side nominates a connection, 2136 // based on media received until the controlling side nominates a connection,
2135 // at which point the controlled side will select that connection as 2137 // at which point the controlled side will select that connection as
2136 // the "best connection". 2138 // the "best connection".
2137 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { 2139 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) {
2138 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2140 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2139 cricket::P2PTransportChannel ch("receiving state change", 1, nullptr, &pa); 2141 cricket::P2PTransportChannel ch("receiving state change", 1, nullptr, &pa);
2140 PrepareChannel(&ch); 2142 PrepareChannel(&ch);
2141 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2143 ch.SetIceRole(cricket::ICEROLE_CONTROLLED);
2142 ch.Connect(); 2144 ch.Connect();
2143 ch.MaybeStartGathering(); 2145 ch.MaybeStartGathering();
2144 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 10)); 2146 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 10));
2145 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2147 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2146 ASSERT_TRUE(conn1 != nullptr); 2148 ASSERT_TRUE(conn1 != nullptr);
2147 EXPECT_EQ(conn1, ch.best_connection()); 2149 EXPECT_EQ(conn1, ch.best_connection());
2148 2150
2149 // If a data packet is received on conn2, the best connection should 2151 // If a data packet is received on conn2, the best connection should
2150 // switch to conn2 because the controlled side must mirror the media path 2152 // switch to conn2 because the controlled side must mirror the media path
2151 // chosen by the controlling side. 2153 // chosen by the controlling side.
2152 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2154 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 1));
2153 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2155 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2154 ASSERT_TRUE(conn2 != nullptr); 2156 ASSERT_TRUE(conn2 != nullptr);
2155 conn2->ReceivedPing(); // Start receiving. 2157 conn2->ReceivedPing(); // Start receiving.
2156 // Do not switch because it is not writable. 2158 // Do not switch because it is not writable.
2157 conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); 2159 conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0));
2158 EXPECT_EQ(conn1, ch.best_connection()); 2160 EXPECT_EQ(conn1, ch.best_connection());
2159 2161
2160 conn2->ReceivedPingResponse(); // Become writable. 2162 conn2->ReceivedPingResponse(); // Become writable.
2161 // Switch because it is writable. 2163 // Switch because it is writable.
2162 conn2->OnReadPacket("DEF", 3, rtc::CreatePacketTime(0)); 2164 conn2->OnReadPacket("DEF", 3, rtc::CreatePacketTime(0));
(...skipping 29 matching lines...) Expand all
2192 2194
2193 // When the current best connection is strong, lower-priority connections will 2195 // When the current best connection is strong, lower-priority connections will
2194 // be pruned. Otherwise, lower-priority connections are kept. 2196 // be pruned. Otherwise, lower-priority connections are kept.
2195 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { 2197 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) {
2196 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2198 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2197 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa); 2199 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa);
2198 PrepareChannel(&ch); 2200 PrepareChannel(&ch);
2199 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2201 ch.SetIceRole(cricket::ICEROLE_CONTROLLED);
2200 ch.Connect(); 2202 ch.Connect();
2201 ch.MaybeStartGathering(); 2203 ch.MaybeStartGathering();
2202 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 1)); 2204 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1));
2203 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2205 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2204 ASSERT_TRUE(conn1 != nullptr); 2206 ASSERT_TRUE(conn1 != nullptr);
2205 EXPECT_EQ(conn1, ch.best_connection()); 2207 EXPECT_EQ(conn1, ch.best_connection());
2206 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2208 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2207 2209
2208 // When a higher-priority, nominated candidate comes in, the connections with 2210 // When a higher-priority, nominated candidate comes in, the connections with
2209 // lower-priority are pruned. 2211 // lower-priority are pruned.
2210 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 10)); 2212 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 10));
2211 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2213 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2212 ASSERT_TRUE(conn2 != nullptr); 2214 ASSERT_TRUE(conn2 != nullptr);
2213 conn2->ReceivedPingResponse(); // Becomes writable and receiving 2215 conn2->ReceivedPingResponse(); // Becomes writable and receiving
2214 conn2->set_nominated(true); 2216 conn2->set_nominated(true);
2215 conn2->SignalNominated(conn2); 2217 conn2->SignalNominated(conn2);
2216 EXPECT_TRUE_WAIT(conn1->pruned(), 3000); 2218 EXPECT_TRUE_WAIT(conn1->pruned(), 3000);
2217 2219
2218 ch.SetIceConfig(CreateIceConfig(500, false)); 2220 ch.SetIceConfig(CreateIceConfig(500, false));
2219 // Wait until conn2 becomes not receiving. 2221 // Wait until conn2 becomes not receiving.
2220 EXPECT_TRUE_WAIT(!conn2->receiving(), 3000); 2222 EXPECT_TRUE_WAIT(!conn2->receiving(), 3000);
2221 2223
2222 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 1)); 2224 ch.AddRemoteCandidate(CreateLocalCandidate("3.3.3.3", 3, 1));
2223 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2225 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2224 ASSERT_TRUE(conn3 != nullptr); 2226 ASSERT_TRUE(conn3 != nullptr);
2225 // The best connection should still be conn2. Even through conn3 has lower 2227 // The best connection should still be conn2. Even through conn3 has lower
2226 // priority and is not receiving/writable, it is not pruned because the best 2228 // priority and is not receiving/writable, it is not pruned because the best
2227 // connection is not receiving. 2229 // connection is not receiving.
2228 WAIT(conn3->pruned(), 1000); 2230 WAIT(conn3->pruned(), 1000);
2229 EXPECT_FALSE(conn3->pruned()); 2231 EXPECT_FALSE(conn3->pruned());
2230 } 2232 }
2231 2233
2232 // Test that GetState returns the state correctly. 2234 // Test that GetState returns the state correctly.
2233 TEST_F(P2PTransportChannelPingTest, TestGetState) { 2235 TEST_F(P2PTransportChannelPingTest, TestGetState) {
2234 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2236 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2235 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa); 2237 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa);
2236 PrepareChannel(&ch); 2238 PrepareChannel(&ch);
2237 ch.Connect(); 2239 ch.Connect();
2238 ch.MaybeStartGathering(); 2240 ch.MaybeStartGathering();
2239 EXPECT_EQ(cricket::TransportChannelState::STATE_INIT, ch.GetState()); 2241 EXPECT_EQ(cricket::TransportChannelState::STATE_INIT, ch.GetState());
2240 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2242 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 100));
2241 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2243 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 1));
2242 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2244 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2243 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2245 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2244 ASSERT_TRUE(conn1 != nullptr); 2246 ASSERT_TRUE(conn1 != nullptr);
2245 ASSERT_TRUE(conn2 != nullptr); 2247 ASSERT_TRUE(conn2 != nullptr);
2246 // Now there are two connections, so the transport channel is connecting. 2248 // Now there are two connections, so the transport channel is connecting.
2247 EXPECT_EQ(cricket::TransportChannelState::STATE_CONNECTING, ch.GetState()); 2249 EXPECT_EQ(cricket::TransportChannelState::STATE_CONNECTING, ch.GetState());
2248 // |conn1| becomes writable and receiving; it then should prune |conn2|. 2250 // |conn1| becomes writable and receiving; it then should prune |conn2|.
2249 conn1->ReceivedPingResponse(); 2251 conn1->ReceivedPingResponse();
2250 EXPECT_TRUE_WAIT(conn2->pruned(), 1000); 2252 EXPECT_TRUE_WAIT(conn2->pruned(), 1000);
2251 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState()); 2253 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState());
2252 conn1->Prune(); // All connections are pruned. 2254 conn1->Prune(); // All connections are pruned.
2253 // Need to wait until the channel state is updated. 2255 // Need to wait until the channel state is updated.
2254 EXPECT_EQ_WAIT(cricket::TransportChannelState::STATE_FAILED, ch.GetState(), 2256 EXPECT_EQ_WAIT(cricket::TransportChannelState::STATE_FAILED, ch.GetState(),
2255 1000); 2257 1000);
2256 } 2258 }
2257 2259
2258 // Test that when a low-priority connection is pruned, it is not deleted 2260 // Test that when a low-priority connection is pruned, it is not deleted
2259 // right away, and it can become active and be pruned again. 2261 // right away, and it can become active and be pruned again.
2260 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { 2262 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) {
2261 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2263 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2262 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa); 2264 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa);
2263 PrepareChannel(&ch); 2265 PrepareChannel(&ch);
2264 ch.SetIceConfig(CreateIceConfig(1000, false)); 2266 ch.SetIceConfig(CreateIceConfig(1000, false));
2265 ch.Connect(); 2267 ch.Connect();
2266 ch.MaybeStartGathering(); 2268 ch.MaybeStartGathering();
2267 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2269 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 100));
2268 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2270 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2269 ASSERT_TRUE(conn1 != nullptr); 2271 ASSERT_TRUE(conn1 != nullptr);
2270 EXPECT_EQ(conn1, ch.best_connection()); 2272 EXPECT_EQ(conn1, ch.best_connection());
2271 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2273 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2272 2274
2273 // Add a low-priority connection |conn2|, which will be pruned, but it will 2275 // Add a low-priority connection |conn2|, which will be pruned, but it will
2274 // not be deleted right away. Once the current best connection becomes not 2276 // not be deleted right away. Once the current best connection becomes not
2275 // receiving, |conn2| will start to ping and upon receiving the ping response, 2277 // receiving, |conn2| will start to ping and upon receiving the ping response,
2276 // it will become the best connection. 2278 // it will become the best connection.
2277 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2279 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 1));
2278 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2280 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2279 ASSERT_TRUE(conn2 != nullptr); 2281 ASSERT_TRUE(conn2 != nullptr);
2280 EXPECT_TRUE_WAIT(!conn2->active(), 1000); 2282 EXPECT_TRUE_WAIT(!conn2->active(), 1000);
2281 // |conn2| should not send a ping yet. 2283 // |conn2| should not send a ping yet.
2282 EXPECT_EQ(cricket::Connection::STATE_WAITING, conn2->state()); 2284 EXPECT_EQ(cricket::Connection::STATE_WAITING, conn2->state());
2283 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState()); 2285 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState());
2284 // Wait for |conn1| becoming not receiving. 2286 // Wait for |conn1| becoming not receiving.
2285 EXPECT_TRUE_WAIT(!conn1->receiving(), 3000); 2287 EXPECT_TRUE_WAIT(!conn1->receiving(), 3000);
2286 // Make sure conn2 is not deleted. 2288 // Make sure conn2 is not deleted.
2287 conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2289 conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
(...skipping 12 matching lines...) Expand all
2300 2302
2301 // Test that if all connections in a channel has timed out on writing, they 2303 // Test that if all connections in a channel has timed out on writing, they
2302 // will all be deleted. We use Prune to simulate write_time_out. 2304 // will all be deleted. We use Prune to simulate write_time_out.
2303 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { 2305 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) {
2304 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2306 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2305 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa); 2307 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa);
2306 PrepareChannel(&ch); 2308 PrepareChannel(&ch);
2307 ch.Connect(); 2309 ch.Connect();
2308 ch.MaybeStartGathering(); 2310 ch.MaybeStartGathering();
2309 // Have one connection only but later becomes write-time-out. 2311 // Have one connection only but later becomes write-time-out.
2310 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2312 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 100));
2311 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2313 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2312 ASSERT_TRUE(conn1 != nullptr); 2314 ASSERT_TRUE(conn1 != nullptr);
2313 conn1->ReceivedPing(); // Becomes receiving 2315 conn1->ReceivedPing(); // Becomes receiving
2314 conn1->Prune(); 2316 conn1->Prune();
2315 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); 2317 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000);
2316 2318
2317 // Have two connections but both become write-time-out later. 2319 // Have two connections but both become write-time-out later.
2318 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 1)); 2320 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 1));
2319 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2321 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2320 ASSERT_TRUE(conn2 != nullptr); 2322 ASSERT_TRUE(conn2 != nullptr);
2321 conn2->ReceivedPing(); // Becomes receiving 2323 conn2->ReceivedPing(); // Becomes receiving
2322 ch.AddRemoteCandidate(CreateCandidate("3.3.3.3", 3, 2)); 2324 ch.AddRemoteCandidate(CreateLocalCandidate("3.3.3.3", 3, 2));
2323 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2325 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2324 ASSERT_TRUE(conn3 != nullptr); 2326 ASSERT_TRUE(conn3 != nullptr);
2325 conn3->ReceivedPing(); // Becomes receiving 2327 conn3->ReceivedPing(); // Becomes receiving
2326 // Now prune both conn2 and conn3; they will be deleted soon. 2328 // Now prune both conn2 and conn3; they will be deleted soon.
2327 conn2->Prune(); 2329 conn2->Prune();
2328 conn3->Prune(); 2330 conn3->Prune();
2329 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); 2331 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000);
2330 } 2332 }
2331 2333
2332 // Test that after a port allocator session is started, it will be stopped 2334 // Test that after a port allocator session is started, it will be stopped
2333 // when a new connection becomes writable and receiving. Also test that this 2335 // when a new connection becomes writable and receiving. Also test that this
2334 // holds even if the transport channel did not lose the writability. 2336 // holds even if the transport channel did not lose the writability.
2335 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { 2337 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) {
2336 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2338 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2337 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa); 2339 cricket::P2PTransportChannel ch("test channel", 1, nullptr, &pa);
2338 PrepareChannel(&ch); 2340 PrepareChannel(&ch);
2339 ch.SetIceConfig(CreateIceConfig(2000, false)); 2341 ch.SetIceConfig(CreateIceConfig(2000, false));
2340 ch.Connect(); 2342 ch.Connect();
2341 ch.MaybeStartGathering(); 2343 ch.MaybeStartGathering();
2342 ch.AddRemoteCandidate(CreateCandidate("1.1.1.1", 1, 100)); 2344 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 100));
2343 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2345 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2344 ASSERT_TRUE(conn1 != nullptr); 2346 ASSERT_TRUE(conn1 != nullptr);
2345 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2347 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2346 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); 2348 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts());
2347 2349
2348 // Restart gathering even if the transport channel is still writable. 2350 // Restart gathering even if the transport channel is still writable.
2349 // It should stop getting ports after a new connection becomes strongly 2351 // It should stop getting ports after a new connection becomes strongly
2350 // connected. 2352 // connected.
2351 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]); 2353 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]);
2352 ch.MaybeStartGathering(); 2354 ch.MaybeStartGathering();
2353 ch.AddRemoteCandidate(CreateCandidate("2.2.2.2", 2, 100)); 2355 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 100));
2354 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2356 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2355 ASSERT_TRUE(conn2 != nullptr); 2357 ASSERT_TRUE(conn2 != nullptr);
2356 conn2->ReceivedPingResponse(); // Becomes writable and receiving 2358 conn2->ReceivedPingResponse(); // Becomes writable and receiving
2357 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); 2359 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts());
2358 } 2360 }
2361
2362 #define P2PTransportChannelTurnTurnFirstTest \
2363 DISABLED_P2PTransportChannelTurnTurnFirstTest
2364
2365 class P2PTransportChannelTurnTurnFirstTest
2366 : public P2PTransportChannelPingTest {
2367 public:
2368 P2PTransportChannelTurnTurnFirstTest()
2369 : turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr) {
2370 network_manager_.AddInterface(kPublicAddrs[0]);
2371 allocator_.reset(new cricket::BasicPortAllocator(
2372 &network_manager_, ServerAddresses(), rtc::SocketAddress(),
2373 rtc::SocketAddress(), rtc::SocketAddress()));
2374 allocator_->set_flags(allocator_->flags() |
2375 cricket::PORTALLOCATOR_DISABLE_STUN |
2376 cricket::PORTALLOCATOR_DISABLE_TCP);
2377 cricket::RelayServerConfig config(cricket::RELAY_TURN);
2378 config.credentials = kRelayCredentials;
2379 config.ports.push_back(
2380 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
2381 allocator_->AddTurnServer(config);
2382 }
2383
2384 cricket::BasicPortAllocator* allocator() { return allocator_.get(); }
2385 cricket::TestTurnServer* turn_server() { return &turn_server_; }
2386
2387 // This verifies the next pingable connection has the expected candidates'
2388 // types and, for relay local candidate, the expected relay protocol and ping
2389 // it.
2390 void VerifyAndPingNextPingableConnection(
2391 cricket::P2PTransportChannel& ch,
2392 const std::string& local_candidate_type,
2393 const std::string& remote_candidate_type,
2394 const std::string& relay_protocol_type = cricket::UDP_PROTOCOL_NAME) {
2395 cricket::Connection* conn = ch.FindNextPingableConnection();
2396 EXPECT_EQ(conn->local_candidate().type(), local_candidate_type);
2397 if (conn->local_candidate().type() == cricket::RELAY_PORT_TYPE) {
2398 EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type);
2399 }
2400 EXPECT_EQ(conn->remote_candidate().type(), remote_candidate_type);
2401 conn->Ping(rtc::Time());
2402 }
2403
2404 cricket::Candidate CreateRelayCandidate(const std::string& ip,
2405 int port,
2406 int priority,
2407 const std::string& ufrag = "") {
2408 cricket::Candidate c = CreateLocalCandidate(ip, port, priority, ufrag);
2409 c.set_type(cricket::RELAY_PORT_TYPE);
2410 return c;
2411 }
2412
2413 private:
2414 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
2415 rtc::FakeNetworkManager network_manager_;
2416 cricket::TestTurnServer turn_server_;
2417 };
2418
2419 // Test that Relay/Relay candidate pair will be pinged when no any other pairs
pthatcher1 2016/01/14 22:40:01 no any other => no other
pthatcher1 2016/01/14 22:40:01 Please use either "candidate pair" or "connection"
guoweis_webrtc 2016/01/15 17:47:52 Done.
guoweis_webrtc 2016/01/15 17:47:53 Done.
2420 // have been pinged yet, unless we need to ping a trigger check or we have a
2421 // best connection.
2422 TEST_F(P2PTransportChannelTurnTurnFirstTest,
2423 TestTurnTurnFirstWhenNothingPingedYet) {
2424 cricket::P2PTransportChannel ch("checks", 1, nullptr, allocator());
2425 PrepareChannel(&ch);
2426 ch.Connect();
2427 ch.MaybeStartGathering();
2428 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2429 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE);
2430 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2431 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2432 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 2));
2433
2434 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2435
2436 // Relay/Relay should be the first pingable connection.
2437 cricket::Connection* conn = ch.FindNextPingableConnection();
2438 EXPECT_EQ(conn->local_candidate().type(), cricket::RELAY_PORT_TYPE);
2439 EXPECT_EQ(conn->remote_candidate().type(), cricket::RELAY_PORT_TYPE);
2440
2441 // Unless that we have a trigger check waiting to be pinged.
2442 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2443 EXPECT_EQ(conn2->local_candidate().type(), cricket::LOCAL_PORT_TYPE);
2444 EXPECT_EQ(conn2->remote_candidate().type(), cricket::LOCAL_PORT_TYPE);
2445 conn2->ReceivedPing();
2446 EXPECT_EQ(conn2, ch.FindNextPingableConnection());
2447
2448 // Make conn3 the best connection.
2449 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 2);
2450 EXPECT_EQ(conn3->local_candidate().type(), cricket::LOCAL_PORT_TYPE);
2451 EXPECT_EQ(conn3->remote_candidate().type(), cricket::RELAY_PORT_TYPE);
2452 conn3->ReceivedPing();
2453 conn3->Ping(rtc::Time());
2454 conn3->ReceivedPingResponse();
2455
2456 // Verify that conn3 will be the "best connection" since it is readable and
2457 // writable. After |MAX_CURRENT_STRONG_DELAY|, it should be the next pingable
2458 // connection.
2459 EXPECT_TRUE_WAIT(conn3 == ch.best_connection(), 5000);
2460 WAIT(false, cricket::MAX_CURRENT_STRONG_DELAY);
2461 EXPECT_EQ(conn3, ch.FindNextPingableConnection());
2462 }
2463
2464 // Test that Relay/Relay connection will be pinged first when everything has
2465 // been pinged even if the connection didn't form the earliest.
pthatcher1 2016/01/14 22:40:01 if the connection didn't form the earliest => even
guoweis_webrtc 2016/01/15 17:47:53 Done.
2466 TEST_F(P2PTransportChannelTurnTurnFirstTest,
2467 TestTurnTurnFirstWhenEverythingPinged) {
2468 cricket::P2PTransportChannel ch("checks", 1, nullptr, allocator());
2469 PrepareChannel(&ch);
2470 ch.Connect();
2471 ch.MaybeStartGathering();
2472 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2473 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE);
2474 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2475
2476 ch.AddRemoteCandidate(CreateLocalCandidate("1.1.1.1", 1, 1));
2477 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000);
2478
2479 // Initially, only have Local/Local and Local/Relay.
2480 VerifyAndPingNextPingableConnection(ch, cricket::LOCAL_PORT_TYPE,
2481 cricket::LOCAL_PORT_TYPE);
2482 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2483 cricket::LOCAL_PORT_TYPE);
2484
2485 // Remote Relay candidate arrives.
2486 ch.AddRemoteCandidate(CreateRelayCandidate("2.2.2.2", 2, 2));
2487 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2488
2489 // Relay/Relay should be the first since it's not pinged before.
pthatcher1 2016/01/14 22:40:01 it's not => it hasn't been
guoweis_webrtc 2016/01/15 17:47:53 Done.
2490 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2491 cricket::RELAY_PORT_TYPE);
2492
2493 // Local/Relay is the final one.
2494 VerifyAndPingNextPingableConnection(ch, cricket::LOCAL_PORT_TYPE,
2495 cricket::RELAY_PORT_TYPE);
2496
2497 // Now, every connection has been pinged once. The next one should be
2498 // Relay/Relay.
2499 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2500 cricket::RELAY_PORT_TYPE);
2501 }
2502
2503 // Test that when we receive a new remote candidate, they will be tried first
2504 // before we re-ping Relay/Relay connection.
2505 TEST_F(P2PTransportChannelTurnTurnFirstTest,
2506 TestNoStarvationOnNonRelayConnection) {
2507 cricket::P2PTransportChannel ch("checks", 1, nullptr, allocator());
2508 PrepareChannel(&ch);
2509 ch.Connect();
2510 ch.MaybeStartGathering();
2511 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2512 EXPECT_EQ(ch.ports()[0]->Type(), cricket::RELAY_PORT_TYPE);
2513 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2514
2515 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2516 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000);
2517
2518 // Initially, only have Relay/Relay and Local/Relay. Ping Relay/Relay first.
2519 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2520 cricket::RELAY_PORT_TYPE);
2521
2522 // Next, ping Local/Relay.
2523 VerifyAndPingNextPingableConnection(ch, cricket::LOCAL_PORT_TYPE,
2524 cricket::RELAY_PORT_TYPE);
2525
2526 // Remote Local candidate arrives.
2527 ch.AddRemoteCandidate(CreateLocalCandidate("2.2.2.2", 2, 2));
2528 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2529
2530 // Local/Local should be the first since it's not pinged before.
pthatcher1 2016/01/14 22:40:01 it's not => it hasn't been
guoweis_webrtc 2016/01/15 17:47:52 Done.
2531 VerifyAndPingNextPingableConnection(ch, cricket::LOCAL_PORT_TYPE,
2532 cricket::LOCAL_PORT_TYPE);
2533
2534 // Relay/Local is the final one.
2535 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2536 cricket::LOCAL_PORT_TYPE);
2537
2538 // Now, every connection has been pinged once. The next one should be
2539 // Relay/Relay.
2540 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2541 cricket::RELAY_PORT_TYPE);
2542 }
2543
2544 // Test the ping sequence is UDP Relay/Relay followed by TCP Relay/Relay,
pthatcher1 2016/01/14 22:40:01 Hmm... that's a good question. Do we really want
2545 // followed by the rest.
2546 TEST_F(P2PTransportChannelTurnTurnFirstTest, TestTcpTurn) {
2547 // Add a Tcp Turn server.
2548 turn_server()->AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
2549 cricket::RelayServerConfig config(cricket::RELAY_TURN);
2550 config.credentials = kRelayCredentials;
2551 config.ports.push_back(
2552 cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false));
2553 allocator()->AddTurnServer(config);
2554
2555 cricket::P2PTransportChannel ch("checks", 1, nullptr, allocator());
2556 PrepareChannel(&ch);
2557 ch.Connect();
2558 ch.MaybeStartGathering();
2559 EXPECT_TRUE_WAIT(ch.ports().size() == 3, 5000);
2560 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE);
2561 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE);
2562 EXPECT_EQ(ch.ports()[2]->Type(), cricket::RELAY_PORT_TYPE);
2563
2564 // Remote Relay candidate arrives.
2565 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
pthatcher1 2016/01/14 22:40:01 Can you add a non-relay candidate also, to be sure
guoweis_webrtc 2016/01/15 17:47:52 This was tested in the first test case, isn't it?
pthatcher1 2016/01/15 20:10:34 Yes, good point.
2566 EXPECT_TRUE_WAIT(ch.connections().size() == 3, 5000);
2567
2568 // UDP Relay/Relay should be pinged first.
2569 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2570 cricket::RELAY_PORT_TYPE);
2571
2572 // TCP Relay/Relay is the next.
2573 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2574 cricket::RELAY_PORT_TYPE,
2575 cricket::TCP_PROTOCOL_NAME);
2576
2577 // Finally, Local/Relay will be pinged.
2578 VerifyAndPingNextPingableConnection(ch, cricket::RELAY_PORT_TYPE,
2579 cricket::LOCAL_PORT_TYPE);
2580 }
OLDNEW
« no previous file with comments | « webrtc/p2p/base/p2ptransportchannel.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698