OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 | 752 |
753 void OnDestroyed(PortInterface* port) { | 753 void OnDestroyed(PortInterface* port) { |
754 destroyed_ = true; | 754 destroyed_ = true; |
755 } | 755 } |
756 bool destroyed() const { return destroyed_; } | 756 bool destroyed() const { return destroyed_; } |
757 | 757 |
758 rtc::BasicPacketSocketFactory* nat_socket_factory1() { | 758 rtc::BasicPacketSocketFactory* nat_socket_factory1() { |
759 return &nat_socket_factory1_; | 759 return &nat_socket_factory1_; |
760 } | 760 } |
761 | 761 |
762 protected: | |
763 rtc::VirtualSocketServer* vss() { return ss_.get(); } | 762 rtc::VirtualSocketServer* vss() { return ss_.get(); } |
764 | 763 |
765 private: | 764 private: |
766 rtc::Thread* main_; | 765 rtc::Thread* main_; |
767 std::unique_ptr<rtc::PhysicalSocketServer> pss_; | 766 std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
768 std::unique_ptr<rtc::VirtualSocketServer> ss_; | 767 std::unique_ptr<rtc::VirtualSocketServer> ss_; |
769 rtc::SocketServerScope ss_scope_; | 768 rtc::SocketServerScope ss_scope_; |
770 rtc::Network network_; | 769 rtc::Network network_; |
771 rtc::BasicPacketSocketFactory socket_factory_; | 770 rtc::BasicPacketSocketFactory socket_factory_; |
772 std::unique_ptr<rtc::NATServer> nat_server1_; | 771 std::unique_ptr<rtc::NATServer> nat_server1_; |
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1755 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); | 1754 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
1756 IceMessage* msg = lport->last_stun_msg(); | 1755 IceMessage* msg = lport->last_stun_msg(); |
1757 const StunUInt64Attribute* ice_controlling_attr = | 1756 const StunUInt64Attribute* ice_controlling_attr = |
1758 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); | 1757 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
1759 ASSERT_TRUE(ice_controlling_attr != NULL); | 1758 ASSERT_TRUE(ice_controlling_attr != NULL); |
1760 const StunByteStringAttribute* use_candidate_attr = msg->GetByteString( | 1759 const StunByteStringAttribute* use_candidate_attr = msg->GetByteString( |
1761 STUN_ATTR_USE_CANDIDATE); | 1760 STUN_ATTR_USE_CANDIDATE); |
1762 ASSERT_TRUE(use_candidate_attr != NULL); | 1761 ASSERT_TRUE(use_candidate_attr != NULL); |
1763 } | 1762 } |
1764 | 1763 |
| 1764 // Tests that when the network type changes, the network cost of the port will |
| 1765 // change, the network cost of the local candidates will change. Also tests that |
| 1766 // the remote network costs are updated with the stun binding requests. |
| 1767 TEST_F(PortTest, TestNetworkCostChange) { |
| 1768 std::unique_ptr<TestPort> lport( |
| 1769 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
| 1770 std::unique_ptr<TestPort> rport( |
| 1771 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
| 1772 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1773 lport->SetIceTiebreaker(kTiebreaker1); |
| 1774 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1775 rport->SetIceTiebreaker(kTiebreaker2); |
| 1776 lport->PrepareAddress(); |
| 1777 rport->PrepareAddress(); |
| 1778 |
| 1779 // Default local port cost is rtc::kNetworkCostUnknown. |
| 1780 EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost()); |
| 1781 ASSERT_TRUE(!lport->Candidates().empty()); |
| 1782 for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1783 EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost()); |
| 1784 } |
| 1785 |
| 1786 // Change the network type to wifi. |
| 1787 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
| 1788 EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); |
| 1789 for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1790 EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); |
| 1791 } |
| 1792 |
| 1793 // Add a connection and then change the network type. |
| 1794 Connection* lconn = |
| 1795 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1796 // Change the network type to cellular. |
| 1797 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
| 1798 EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); |
| 1799 for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1800 EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); |
| 1801 } |
| 1802 |
| 1803 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
| 1804 Connection* rconn = |
| 1805 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1806 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
| 1807 lconn->Ping(0); |
| 1808 // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping |
| 1809 // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the |
| 1810 // message is handled in rconn, The rconn's remote candidate will have cost |
| 1811 // rtc::kNetworkCostHigh; |
| 1812 EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); |
| 1813 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1814 IceMessage* msg = lport->last_stun_msg(); |
| 1815 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1816 // Pass the binding request to rport. |
| 1817 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
| 1818 lport->last_stun_buf()->size(), rtc::PacketTime()); |
| 1819 // Wait until rport sends the response and then check the remote network cost. |
| 1820 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); |
| 1821 EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); |
| 1822 } |
| 1823 |
1765 TEST_F(PortTest, TestNetworkInfoAttribute) { | 1824 TEST_F(PortTest, TestNetworkInfoAttribute) { |
1766 std::unique_ptr<TestPort> lport( | 1825 std::unique_ptr<TestPort> lport( |
1767 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); | 1826 CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
1768 // Set the network type for rport to be cellular so its cost will be 999. | |
1769 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); | |
1770 std::unique_ptr<TestPort> rport( | 1827 std::unique_ptr<TestPort> rport( |
1771 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); | 1828 CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
1772 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); | 1829 lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
1773 lport->SetIceTiebreaker(kTiebreaker1); | 1830 lport->SetIceTiebreaker(kTiebreaker1); |
1774 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); | 1831 rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
1775 rport->SetIceTiebreaker(kTiebreaker2); | 1832 rport->SetIceTiebreaker(kTiebreaker2); |
1776 | 1833 |
1777 uint16_t lnetwork_id = 9; | 1834 uint16_t lnetwork_id = 9; |
1778 lport->Network()->set_id(lnetwork_id); | 1835 lport->Network()->set_id(lnetwork_id); |
1779 // Send a fake ping from lport to rport. | 1836 // Send a fake ping from lport to rport. |
1780 lport->PrepareAddress(); | 1837 lport->PrepareAddress(); |
1781 rport->PrepareAddress(); | 1838 rport->PrepareAddress(); |
1782 Connection* lconn = | 1839 Connection* lconn = |
1783 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); | 1840 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
1784 lconn->Ping(0); | 1841 lconn->Ping(0); |
1785 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); | 1842 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
1786 IceMessage* msg = lport->last_stun_msg(); | 1843 IceMessage* msg = lport->last_stun_msg(); |
1787 const StunUInt32Attribute* network_info_attr = | 1844 const StunUInt32Attribute* network_info_attr = |
1788 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); | 1845 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
1789 ASSERT_TRUE(network_info_attr != NULL); | 1846 ASSERT_TRUE(network_info_attr != NULL); |
1790 uint32_t network_info = network_info_attr->value(); | 1847 uint32_t network_info = network_info_attr->value(); |
1791 EXPECT_EQ(lnetwork_id, network_info >> 16); | 1848 EXPECT_EQ(lnetwork_id, network_info >> 16); |
1792 // Default network cost is 0. | 1849 // Default network has unknown type and cost kNetworkCostUnknown. |
1793 EXPECT_EQ(0U, network_info & 0xFFFF); | 1850 EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); |
1794 | 1851 |
| 1852 // Set the network type to be cellular so its cost will be kNetworkCostHigh. |
1795 // Send a fake ping from rport to lport. | 1853 // Send a fake ping from rport to lport. |
| 1854 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
1796 uint16_t rnetwork_id = 8; | 1855 uint16_t rnetwork_id = 8; |
1797 rport->Network()->set_id(rnetwork_id); | 1856 rport->Network()->set_id(rnetwork_id); |
1798 Connection* rconn = | 1857 Connection* rconn = |
1799 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); | 1858 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
1800 rconn->Ping(0); | 1859 rconn->Ping(0); |
1801 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); | 1860 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); |
1802 msg = rport->last_stun_msg(); | 1861 msg = rport->last_stun_msg(); |
1803 network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO); | 1862 network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
1804 ASSERT_TRUE(network_info_attr != NULL); | 1863 ASSERT_TRUE(network_info_attr != NULL); |
1805 network_info = network_info_attr->value(); | 1864 network_info = network_info_attr->value(); |
1806 EXPECT_EQ(rnetwork_id, network_info >> 16); | 1865 EXPECT_EQ(rnetwork_id, network_info >> 16); |
1807 EXPECT_EQ(cricket::kMaxNetworkCost, network_info & 0xFFFF); | 1866 EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF); |
1808 } | 1867 } |
1809 | 1868 |
1810 // Test handling STUN messages. | 1869 // Test handling STUN messages. |
1811 TEST_F(PortTest, TestHandleStunMessage) { | 1870 TEST_F(PortTest, TestHandleStunMessage) { |
1812 // Our port will act as the "remote" port. | 1871 // Our port will act as the "remote" port. |
1813 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); | 1872 std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
1814 | 1873 |
1815 std::unique_ptr<IceMessage> in_msg, out_msg; | 1874 std::unique_ptr<IceMessage> in_msg, out_msg; |
1816 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); | 1875 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
1817 rtc::SocketAddress addr(kLocalAddr1); | 1876 rtc::SocketAddress addr(kLocalAddr1); |
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2596 EXPECT_EQ(1UL, port->Candidates().size()); | 2655 EXPECT_EQ(1UL, port->Candidates().size()); |
2597 port->SetIceParameters(1, "ufrag2", "password2"); | 2656 port->SetIceParameters(1, "ufrag2", "password2"); |
2598 EXPECT_EQ(1, port->component()); | 2657 EXPECT_EQ(1, port->component()); |
2599 EXPECT_EQ("ufrag2", port->username_fragment()); | 2658 EXPECT_EQ("ufrag2", port->username_fragment()); |
2600 EXPECT_EQ("password2", port->password()); | 2659 EXPECT_EQ("password2", port->password()); |
2601 const Candidate& candidate = port->Candidates()[0]; | 2660 const Candidate& candidate = port->Candidates()[0]; |
2602 EXPECT_EQ(1, candidate.component()); | 2661 EXPECT_EQ(1, candidate.component()); |
2603 EXPECT_EQ("ufrag2", candidate.username()); | 2662 EXPECT_EQ("ufrag2", candidate.username()); |
2604 EXPECT_EQ("password2", candidate.password()); | 2663 EXPECT_EQ("password2", candidate.password()); |
2605 } | 2664 } |
OLD | NEW |