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 13 matching lines...) Expand all Loading... |
24 | 24 |
25 static const SocketAddress kLocalAddr("127.0.0.1", 0); | 25 static const SocketAddress kLocalAddr("127.0.0.1", 0); |
26 static const SocketAddress kStunAddr1("127.0.0.1", 5000); | 26 static const SocketAddress kStunAddr1("127.0.0.1", 5000); |
27 static const SocketAddress kStunAddr2("127.0.0.1", 4000); | 27 static const SocketAddress kStunAddr2("127.0.0.1", 4000); |
28 static const SocketAddress kBadAddr("0.0.0.1", 5000); | 28 static const SocketAddress kBadAddr("0.0.0.1", 5000); |
29 static const SocketAddress kStunHostnameAddr("localhost", 5000); | 29 static const SocketAddress kStunHostnameAddr("localhost", 5000); |
30 static const SocketAddress kBadHostnameAddr("not-a-real-hostname", 5000); | 30 static const SocketAddress kBadHostnameAddr("not-a-real-hostname", 5000); |
31 static const int kTimeoutMs = 10000; | 31 static const int kTimeoutMs = 10000; |
32 // stun prio = 100 << 24 | 30 (IPV4) << 8 | 256 - 0 | 32 // stun prio = 100 << 24 | 30 (IPV4) << 8 | 256 - 0 |
33 static const uint32_t kStunCandidatePriority = 1677729535; | 33 static const uint32_t kStunCandidatePriority = 1677729535; |
| 34 static const int kInfiniteLifetime = -1; |
| 35 static const int kHighCostPortKeepaliveLifetimeMs = 2 * 60 * 1000; |
34 | 36 |
35 // Tests connecting a StunPort to a fake STUN server (cricket::StunServer) | 37 // Tests connecting a StunPort to a fake STUN server (cricket::StunServer) |
36 // TODO: Use a VirtualSocketServer here. We have to use a | 38 // TODO: Use a VirtualSocketServer here. We have to use a |
37 // PhysicalSocketServer right now since DNS is not part of SocketServer yet. | 39 // PhysicalSocketServer right now since DNS is not part of SocketServer yet. |
38 class StunPortTest : public testing::Test, | 40 class StunPortTest : public testing::Test, |
39 public sigslot::has_slots<> { | 41 public sigslot::has_slots<> { |
40 public: | 42 public: |
41 StunPortTest() | 43 StunPortTest() |
42 : pss_(new rtc::PhysicalSocketServer), | 44 : pss_(new rtc::PhysicalSocketServer), |
43 ss_(new rtc::VirtualSocketServer(pss_.get())), | 45 ss_(new rtc::VirtualSocketServer(pss_.get())), |
44 ss_scope_(ss_.get()), | 46 ss_scope_(ss_.get()), |
45 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), | 47 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), |
46 socket_factory_(rtc::Thread::Current()), | 48 socket_factory_(rtc::Thread::Current()), |
47 stun_server_1_(cricket::TestStunServer::Create( | 49 stun_server_1_(cricket::TestStunServer::Create(rtc::Thread::Current(), |
48 rtc::Thread::Current(), kStunAddr1)), | 50 kStunAddr1)), |
49 stun_server_2_(cricket::TestStunServer::Create( | 51 stun_server_2_(cricket::TestStunServer::Create(rtc::Thread::Current(), |
50 rtc::Thread::Current(), kStunAddr2)), | 52 kStunAddr2)), |
51 done_(false), error_(false), stun_keepalive_delay_(0) { | 53 done_(false), |
52 } | 54 error_(false), |
| 55 stun_keepalive_delay_(0), |
| 56 stun_keepalive_lifetime_(-1) {} |
53 | 57 |
54 const cricket::Port* port() const { return stun_port_.get(); } | 58 cricket::UDPPort* port() const { return stun_port_.get(); } |
55 bool done() const { return done_; } | 59 bool done() const { return done_; } |
56 bool error() const { return error_; } | 60 bool error() const { return error_; } |
57 | 61 |
| 62 void SetNetworkType(rtc::AdapterType adapter_type) { |
| 63 network_.set_type(adapter_type); |
| 64 } |
| 65 |
58 void CreateStunPort(const rtc::SocketAddress& server_addr) { | 66 void CreateStunPort(const rtc::SocketAddress& server_addr) { |
59 ServerAddresses stun_servers; | 67 ServerAddresses stun_servers; |
60 stun_servers.insert(server_addr); | 68 stun_servers.insert(server_addr); |
61 CreateStunPort(stun_servers); | 69 CreateStunPort(stun_servers); |
62 } | 70 } |
63 | 71 |
64 void CreateStunPort(const ServerAddresses& stun_servers) { | 72 void CreateStunPort(const ServerAddresses& stun_servers) { |
65 stun_port_.reset(cricket::StunPort::Create( | 73 stun_port_.reset(cricket::StunPort::Create( |
66 rtc::Thread::Current(), &socket_factory_, &network_, | 74 rtc::Thread::Current(), &socket_factory_, &network_, |
67 kLocalAddr.ipaddr(), 0, 0, rtc::CreateRandomString(16), | 75 kLocalAddr.ipaddr(), 0, 0, rtc::CreateRandomString(16), |
68 rtc::CreateRandomString(22), stun_servers, std::string())); | 76 rtc::CreateRandomString(22), stun_servers, std::string())); |
69 stun_port_->set_stun_keepalive_delay(stun_keepalive_delay_); | 77 stun_port_->set_stun_keepalive_delay(stun_keepalive_delay_); |
| 78 // If |stun_keepalive_lifetime_| is negative, let the stun port |
| 79 // choose its lifetime from the network type. |
| 80 if (stun_keepalive_lifetime_ >= 0) { |
| 81 stun_port_->set_stun_keepalive_lifetime(stun_keepalive_lifetime_); |
| 82 } |
70 stun_port_->SignalPortComplete.connect(this, | 83 stun_port_->SignalPortComplete.connect(this, |
71 &StunPortTest::OnPortComplete); | 84 &StunPortTest::OnPortComplete); |
72 stun_port_->SignalPortError.connect(this, | 85 stun_port_->SignalPortError.connect(this, |
73 &StunPortTest::OnPortError); | 86 &StunPortTest::OnPortError); |
74 } | 87 } |
75 | 88 |
76 void CreateSharedStunPort(const rtc::SocketAddress& server_addr) { | 89 void CreateSharedStunPort(const rtc::SocketAddress& server_addr) { |
77 socket_.reset(socket_factory_.CreateUdpSocket( | 90 socket_.reset(socket_factory_.CreateUdpSocket( |
78 rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0)); | 91 rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0)); |
79 ASSERT_TRUE(socket_ != NULL); | 92 ASSERT_TRUE(socket_ != NULL); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 error_ = false; | 136 error_ = false; |
124 } | 137 } |
125 void OnPortError(cricket::Port* port) { | 138 void OnPortError(cricket::Port* port) { |
126 done_ = true; | 139 done_ = true; |
127 error_ = true; | 140 error_ = true; |
128 } | 141 } |
129 void SetKeepaliveDelay(int delay) { | 142 void SetKeepaliveDelay(int delay) { |
130 stun_keepalive_delay_ = delay; | 143 stun_keepalive_delay_ = delay; |
131 } | 144 } |
132 | 145 |
| 146 void SetKeepaliveLifetime(int lifetime) { |
| 147 stun_keepalive_lifetime_ = lifetime; |
| 148 } |
| 149 |
133 cricket::TestStunServer* stun_server_1() { | 150 cricket::TestStunServer* stun_server_1() { |
134 return stun_server_1_.get(); | 151 return stun_server_1_.get(); |
135 } | 152 } |
136 cricket::TestStunServer* stun_server_2() { | 153 cricket::TestStunServer* stun_server_2() { |
137 return stun_server_2_.get(); | 154 return stun_server_2_.get(); |
138 } | 155 } |
139 | 156 |
140 private: | 157 private: |
141 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; | 158 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; |
142 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_; | 159 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_; |
143 rtc::SocketServerScope ss_scope_; | 160 rtc::SocketServerScope ss_scope_; |
144 rtc::Network network_; | 161 rtc::Network network_; |
145 rtc::BasicPacketSocketFactory socket_factory_; | 162 rtc::BasicPacketSocketFactory socket_factory_; |
146 rtc::scoped_ptr<cricket::UDPPort> stun_port_; | 163 rtc::scoped_ptr<cricket::UDPPort> stun_port_; |
147 rtc::scoped_ptr<cricket::TestStunServer> stun_server_1_; | 164 rtc::scoped_ptr<cricket::TestStunServer> stun_server_1_; |
148 rtc::scoped_ptr<cricket::TestStunServer> stun_server_2_; | 165 rtc::scoped_ptr<cricket::TestStunServer> stun_server_2_; |
149 rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_; | 166 rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_; |
150 bool done_; | 167 bool done_; |
151 bool error_; | 168 bool error_; |
152 int stun_keepalive_delay_; | 169 int stun_keepalive_delay_; |
| 170 int stun_keepalive_lifetime_; |
153 }; | 171 }; |
154 | 172 |
155 // Test that we can create a STUN port | 173 // Test that we can create a STUN port |
156 TEST_F(StunPortTest, TestBasic) { | 174 TEST_F(StunPortTest, TestBasic) { |
157 CreateStunPort(kStunAddr1); | 175 CreateStunPort(kStunAddr1); |
158 EXPECT_EQ("stun", port()->Type()); | 176 EXPECT_EQ("stun", port()->Type()); |
159 EXPECT_EQ(0U, port()->Candidates().size()); | 177 EXPECT_EQ(0U, port()->Candidates().size()); |
160 } | 178 } |
161 | 179 |
162 // Test that we can get an address from a STUN server. | 180 // Test that we can get an address from a STUN server. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 stun_servers.insert(kStunAddr1); | 296 stun_servers.insert(kStunAddr1); |
279 stun_servers.insert(kStunAddr2); | 297 stun_servers.insert(kStunAddr2); |
280 CreateStunPort(stun_servers); | 298 CreateStunPort(stun_servers); |
281 EXPECT_EQ("stun", port()->Type()); | 299 EXPECT_EQ("stun", port()->Type()); |
282 PrepareAddress(); | 300 PrepareAddress(); |
283 EXPECT_TRUE_WAIT(done(), kTimeoutMs); | 301 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
284 EXPECT_EQ(2U, port()->Candidates().size()); | 302 EXPECT_EQ(2U, port()->Candidates().size()); |
285 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), ""); | 303 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), ""); |
286 EXPECT_EQ(port()->Candidates()[1].relay_protocol(), ""); | 304 EXPECT_EQ(port()->Candidates()[1].relay_protocol(), ""); |
287 } | 305 } |
| 306 |
| 307 // Test that the stun_keepalive_lifetime is set correctly based on the network |
| 308 // type on a STUN port. |
| 309 TEST_F(StunPortTest, TestStunPortGetStunKeepaliveLifetime) { |
| 310 // Lifetime for the default network type is |kInfiniteLifetime|. |
| 311 CreateStunPort(kStunAddr1); |
| 312 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); |
| 313 |
| 314 // Lifetime for the cellular network is |kHighCostPortKeepaliveLifetimeMs| |
| 315 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
| 316 CreateStunPort(kStunAddr2); |
| 317 EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs, |
| 318 port()->stun_keepalive_lifetime()); |
| 319 } |
| 320 |
| 321 // Test that the stun_keepalive_lifetime is set correctly based on the network |
| 322 // type on a shared STUN port (UDPPort). |
| 323 TEST_F(StunPortTest, TestUdpPortGetStunKeepaliveLifetime) { |
| 324 // Lifetime for the default network type is |kInfiniteLifetime|. |
| 325 CreateSharedStunPort(kStunAddr1); |
| 326 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); |
| 327 |
| 328 // Lifetime for the cellular network is |kHighCostPortKeepaliveLifetimeMs| |
| 329 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
| 330 CreateSharedStunPort(kStunAddr2); |
| 331 EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs, |
| 332 port()->stun_keepalive_lifetime()); |
| 333 } |
| 334 |
| 335 // Test that STUN binding requests will be stopped shortly if the keep-alive |
| 336 // lifetime is short. |
| 337 TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) { |
| 338 SetKeepaliveDelay(101); |
| 339 SetKeepaliveLifetime(100); |
| 340 CreateStunPort(kStunAddr1); |
| 341 PrepareAddress(); |
| 342 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
| 343 EXPECT_TRUE_WAIT(!port()->RequestExists(cricket::STUN_BINDING_REQUEST), 2000); |
| 344 } |
| 345 |
| 346 // Test that by default, the STUN binding requests will last for a long time. |
| 347 TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) { |
| 348 SetKeepaliveDelay(101); |
| 349 CreateStunPort(kStunAddr1); |
| 350 PrepareAddress(); |
| 351 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
| 352 rtc::Thread::Current()->ProcessMessages(1000); |
| 353 EXPECT_TRUE(port()->RequestExists(cricket::STUN_BINDING_REQUEST)); |
| 354 } |
OLD | NEW |