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

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

Powered by Google App Engine
This is Rietveld 408576698