OLD | NEW |
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 1902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1913 P2PTransportChannelPingTest() | 1913 P2PTransportChannelPingTest() |
1914 : pss_(new rtc::PhysicalSocketServer), | 1914 : pss_(new rtc::PhysicalSocketServer), |
1915 vss_(new rtc::VirtualSocketServer(pss_.get())), | 1915 vss_(new rtc::VirtualSocketServer(pss_.get())), |
1916 ss_scope_(vss_.get()) {} | 1916 ss_scope_(vss_.get()) {} |
1917 | 1917 |
1918 protected: | 1918 protected: |
1919 void PrepareChannel(cricket::P2PTransportChannel* ch) { | 1919 void PrepareChannel(cricket::P2PTransportChannel* ch) { |
1920 ch->SetIceRole(cricket::ICEROLE_CONTROLLING); | 1920 ch->SetIceRole(cricket::ICEROLE_CONTROLLING); |
1921 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]); | 1921 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]); |
1922 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]); | 1922 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]); |
| 1923 ch->SignalReadyToSend.connect(this, |
| 1924 &P2PTransportChannelPingTest::OnReadyToSend); |
1923 } | 1925 } |
1924 | 1926 |
1925 cricket::Candidate CreateHostCandidate(const std::string& ip, | 1927 cricket::Candidate CreateHostCandidate(const std::string& ip, |
1926 int port, | 1928 int port, |
1927 int priority, | 1929 int priority, |
1928 const std::string& ufrag = "") { | 1930 const std::string& ufrag = "") { |
1929 cricket::Candidate c; | 1931 cricket::Candidate c; |
1930 c.set_address(rtc::SocketAddress(ip, port)); | 1932 c.set_address(rtc::SocketAddress(ip, port)); |
1931 c.set_component(1); | 1933 c.set_component(1); |
1932 c.set_protocol(cricket::UDP_PROTOCOL_NAME); | 1934 c.set_protocol(cricket::UDP_PROTOCOL_NAME); |
(...skipping 29 matching lines...) Expand all Loading... |
1962 | 1964 |
1963 cricket::Connection* FindNextPingableConnectionAndPingIt( | 1965 cricket::Connection* FindNextPingableConnectionAndPingIt( |
1964 cricket::P2PTransportChannel* ch) { | 1966 cricket::P2PTransportChannel* ch) { |
1965 cricket::Connection* conn = ch->FindNextPingableConnection(); | 1967 cricket::Connection* conn = ch->FindNextPingableConnection(); |
1966 if (conn) { | 1968 if (conn) { |
1967 ch->MarkConnectionPinged(conn); | 1969 ch->MarkConnectionPinged(conn); |
1968 } | 1970 } |
1969 return conn; | 1971 return conn; |
1970 } | 1972 } |
1971 | 1973 |
| 1974 void OnReadyToSend(cricket::TransportChannel* channel) { |
| 1975 channel_ready_to_send_ = true; |
| 1976 } |
| 1977 |
| 1978 bool channel_ready_to_send() { return channel_ready_to_send_; } |
| 1979 void reset_channel_ready_to_send() { channel_ready_to_send_ = false; } |
| 1980 |
1972 private: | 1981 private: |
1973 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; | 1982 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; |
1974 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_; | 1983 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_; |
1975 rtc::SocketServerScope ss_scope_; | 1984 rtc::SocketServerScope ss_scope_; |
| 1985 bool channel_ready_to_send_ = false; |
1976 }; | 1986 }; |
1977 | 1987 |
1978 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { | 1988 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { |
1979 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); | 1989 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
1980 cricket::P2PTransportChannel ch("trigger checks", 1, &pa); | 1990 cricket::P2PTransportChannel ch("trigger checks", 1, &pa); |
1981 PrepareChannel(&ch); | 1991 PrepareChannel(&ch); |
1982 ch.Connect(); | 1992 ch.Connect(); |
1983 ch.MaybeStartGathering(); | 1993 ch.MaybeStartGathering(); |
1984 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); | 1994 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); |
1985 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); | 1995 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2155 conn1->ReceivedPing(); | 2165 conn1->ReceivedPing(); |
2156 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); | 2166 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); |
2157 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000); | 2167 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000); |
2158 EXPECT_TRUE_WAIT(ch.receiving(), 1000); | 2168 EXPECT_TRUE_WAIT(ch.receiving(), 1000); |
2159 EXPECT_TRUE_WAIT(!ch.receiving(), 1000); | 2169 EXPECT_TRUE_WAIT(!ch.receiving(), 1000); |
2160 } | 2170 } |
2161 | 2171 |
2162 // The controlled side will select a connection as the "best connection" based | 2172 // The controlled side will select a connection as the "best connection" based |
2163 // on priority until the controlling side nominates a connection, at which | 2173 // on priority until the controlling side nominates a connection, at which |
2164 // point the controlled side will select that connection as the | 2174 // point the controlled side will select that connection as the |
2165 // "best connection". | 2175 // "best connection". Plus, the channel will fire SignalReadyToSend if the new |
| 2176 // best connection is writable. |
2166 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { | 2177 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { |
2167 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); | 2178 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
2168 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); | 2179 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); |
2169 PrepareChannel(&ch); | 2180 PrepareChannel(&ch); |
2170 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); | 2181 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); |
2171 ch.Connect(); | 2182 ch.Connect(); |
2172 ch.MaybeStartGathering(); | 2183 ch.MaybeStartGathering(); |
2173 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); | 2184 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); |
2174 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); | 2185 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
2175 ASSERT_TRUE(conn1 != nullptr); | 2186 ASSERT_TRUE(conn1 != nullptr); |
2176 EXPECT_EQ(conn1, ch.best_connection()); | 2187 EXPECT_EQ(conn1, ch.best_connection()); |
| 2188 // Channel is not ready because it is not writable. |
| 2189 EXPECT_FALSE(channel_ready_to_send()); |
2177 | 2190 |
2178 // When a higher priority candidate comes in, the new connection is chosen | 2191 // When a higher priority candidate comes in, the new connection is chosen |
2179 // as the best connection. | 2192 // as the best connection. |
2180 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10)); | 2193 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10)); |
2181 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); | 2194 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
2182 ASSERT_TRUE(conn2 != nullptr); | 2195 ASSERT_TRUE(conn2 != nullptr); |
2183 EXPECT_EQ(conn2, ch.best_connection()); | 2196 EXPECT_EQ(conn2, ch.best_connection()); |
| 2197 EXPECT_FALSE(channel_ready_to_send()); |
2184 | 2198 |
2185 // If a stun request with use-candidate attribute arrives, the receiving | 2199 // If a stun request with use-candidate attribute arrives, the receiving |
2186 // connection will be set as the best connection, even though | 2200 // connection will be set as the best connection, even though |
2187 // its priority is lower. | 2201 // its priority is lower. |
2188 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1)); | 2202 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1)); |
2189 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); | 2203 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
2190 ASSERT_TRUE(conn3 != nullptr); | 2204 ASSERT_TRUE(conn3 != nullptr); |
2191 // Because it has a lower priority, the best connection is still conn2. | 2205 // Because it has a lower priority, the best connection is still conn2. |
2192 EXPECT_EQ(conn2, ch.best_connection()); | 2206 EXPECT_EQ(conn2, ch.best_connection()); |
2193 conn3->ReceivedPingResponse(); // Become writable. | 2207 conn3->ReceivedPingResponse(); // Become writable. |
2194 // But if it is nominated via use_candidate, it is chosen as the best | 2208 // But if it is nominated via use_candidate, it is chosen as the best |
2195 // connection. | 2209 // connection. |
2196 conn3->set_nominated(true); | 2210 conn3->set_nominated(true); |
2197 conn3->SignalNominated(conn3); | 2211 conn3->SignalNominated(conn3); |
2198 EXPECT_EQ(conn3, ch.best_connection()); | 2212 EXPECT_EQ(conn3, ch.best_connection()); |
| 2213 EXPECT_TRUE(channel_ready_to_send()); |
2199 | 2214 |
2200 // Even if another higher priority candidate arrives, | 2215 // Even if another higher priority candidate arrives, |
2201 // it will not be set as the best connection because the best connection | 2216 // it will not be set as the best connection because the best connection |
2202 // is nominated by the controlling side. | 2217 // is nominated by the controlling side. |
2203 ch.AddRemoteCandidate(CreateHostCandidate("4.4.4.4", 4, 100)); | 2218 ch.AddRemoteCandidate(CreateHostCandidate("4.4.4.4", 4, 100)); |
2204 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); | 2219 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); |
2205 ASSERT_TRUE(conn4 != nullptr); | 2220 ASSERT_TRUE(conn4 != nullptr); |
2206 EXPECT_EQ(conn3, ch.best_connection()); | 2221 EXPECT_EQ(conn3, ch.best_connection()); |
2207 // But if it is nominated via use_candidate and writable, it will be set as | 2222 // But if it is nominated via use_candidate and writable, it will be set as |
2208 // the best connection. | 2223 // the best connection. |
2209 conn4->set_nominated(true); | 2224 conn4->set_nominated(true); |
2210 conn4->SignalNominated(conn4); | 2225 conn4->SignalNominated(conn4); |
2211 // Not switched yet because conn4 is not writable. | 2226 // Not switched yet because conn4 is not writable. |
2212 EXPECT_EQ(conn3, ch.best_connection()); | 2227 EXPECT_EQ(conn3, ch.best_connection()); |
| 2228 reset_channel_ready_to_send(); |
2213 // The best connection switches after conn4 becomes writable. | 2229 // The best connection switches after conn4 becomes writable. |
2214 conn4->ReceivedPingResponse(); | 2230 conn4->ReceivedPingResponse(); |
2215 EXPECT_EQ(conn4, ch.best_connection()); | 2231 EXPECT_EQ(conn4, ch.best_connection()); |
| 2232 // SignalReadyToSend is fired again because conn4 is writable. |
| 2233 EXPECT_TRUE(channel_ready_to_send()); |
2216 } | 2234 } |
2217 | 2235 |
2218 // The controlled side will select a connection as the "best connection" based | 2236 // The controlled side will select a connection as the "best connection" based |
2219 // on requests from an unknown address before the controlling side nominates | 2237 // on requests from an unknown address before the controlling side nominates |
2220 // a connection, and will nominate a connection from an unknown address if the | 2238 // a connection, and will nominate a connection from an unknown address if the |
2221 // request contains the use_candidate attribute. Plus, it will also sends back | 2239 // request contains the use_candidate attribute. Plus, it will also sends back |
2222 // a ping response and set the ICE pwd in the remote candidate appropriately. | 2240 // a ping response and set the ICE pwd in the remote candidate appropriately. |
2223 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { | 2241 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { |
2224 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); | 2242 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
2225 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); | 2243 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2745 | 2763 |
2746 // TCP Relay/Relay is the next. | 2764 // TCP Relay/Relay is the next. |
2747 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, | 2765 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, |
2748 cricket::RELAY_PORT_TYPE, | 2766 cricket::RELAY_PORT_TYPE, |
2749 cricket::TCP_PROTOCOL_NAME); | 2767 cricket::TCP_PROTOCOL_NAME); |
2750 | 2768 |
2751 // Finally, Local/Relay will be pinged. | 2769 // Finally, Local/Relay will be pinged. |
2752 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, | 2770 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, |
2753 cricket::RELAY_PORT_TYPE); | 2771 cricket::RELAY_PORT_TYPE); |
2754 } | 2772 } |
OLD | NEW |