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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 // choose its lifetime from the network type. | 85 // choose its lifetime from the network type. |
86 if (stun_keepalive_lifetime_ >= 0) { | 86 if (stun_keepalive_lifetime_ >= 0) { |
87 stun_port_->set_stun_keepalive_lifetime(stun_keepalive_lifetime_); | 87 stun_port_->set_stun_keepalive_lifetime(stun_keepalive_lifetime_); |
88 } | 88 } |
89 stun_port_->SignalPortComplete.connect(this, | 89 stun_port_->SignalPortComplete.connect(this, |
90 &StunPortTest::OnPortComplete); | 90 &StunPortTest::OnPortComplete); |
91 stun_port_->SignalPortError.connect(this, | 91 stun_port_->SignalPortError.connect(this, |
92 &StunPortTest::OnPortError); | 92 &StunPortTest::OnPortError); |
93 } | 93 } |
94 | 94 |
95 void CreateSharedStunPort(const rtc::SocketAddress& server_addr) { | 95 void CreateSharedUdpPort(const rtc::SocketAddress& server_addr) { |
96 socket_.reset(socket_factory_.CreateUdpSocket( | 96 socket_.reset(socket_factory_.CreateUdpSocket( |
97 rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0)); | 97 rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0)); |
98 ASSERT_TRUE(socket_ != NULL); | 98 ASSERT_TRUE(socket_ != NULL); |
99 socket_->SignalReadPacket.connect(this, &StunPortTest::OnReadPacket); | 99 socket_->SignalReadPacket.connect(this, &StunPortTest::OnReadPacket); |
100 stun_port_.reset(cricket::UDPPort::Create( | 100 stun_port_.reset(cricket::UDPPort::Create( |
101 rtc::Thread::Current(), &socket_factory_, | 101 rtc::Thread::Current(), &socket_factory_, |
102 &network_, socket_.get(), | 102 &network_, socket_.get(), |
103 rtc::CreateRandomString(16), rtc::CreateRandomString(22), | 103 rtc::CreateRandomString(16), rtc::CreateRandomString(22), |
104 std::string(), false)); | 104 std::string(), false)); |
105 ASSERT_TRUE(stun_port_ != NULL); | 105 ASSERT_TRUE(stun_port_ != NULL); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 std::unique_ptr<cricket::UDPPort> stun_port_; | 169 std::unique_ptr<cricket::UDPPort> stun_port_; |
170 std::unique_ptr<cricket::TestStunServer> stun_server_1_; | 170 std::unique_ptr<cricket::TestStunServer> stun_server_1_; |
171 std::unique_ptr<cricket::TestStunServer> stun_server_2_; | 171 std::unique_ptr<cricket::TestStunServer> stun_server_2_; |
172 std::unique_ptr<rtc::AsyncPacketSocket> socket_; | 172 std::unique_ptr<rtc::AsyncPacketSocket> socket_; |
173 bool done_; | 173 bool done_; |
174 bool error_; | 174 bool error_; |
175 int stun_keepalive_delay_; | 175 int stun_keepalive_delay_; |
176 int stun_keepalive_lifetime_; | 176 int stun_keepalive_lifetime_; |
177 }; | 177 }; |
178 | 178 |
179 // Test that we can create a STUN port | 179 // Test that we can create a STUN port. |
180 TEST_F(StunPortTest, TestBasic) { | 180 TEST_F(StunPortTest, TestCreateStunPort) { |
181 CreateStunPort(kStunAddr1); | 181 CreateStunPort(kStunAddr1); |
182 EXPECT_EQ("stun", port()->Type()); | 182 EXPECT_EQ("stun", port()->Type()); |
183 EXPECT_EQ(0U, port()->Candidates().size()); | 183 EXPECT_EQ(0U, port()->Candidates().size()); |
184 } | 184 } |
185 | 185 |
| 186 // Test that we can create a UDP port. |
| 187 TEST_F(StunPortTest, TestCreateUdpPort) { |
| 188 CreateSharedUdpPort(kStunAddr1); |
| 189 EXPECT_EQ("local", port()->Type()); |
| 190 EXPECT_EQ(0U, port()->Candidates().size()); |
| 191 } |
| 192 |
186 // Test that we can get an address from a STUN server. | 193 // Test that we can get an address from a STUN server. |
187 TEST_F(StunPortTest, TestPrepareAddress) { | 194 TEST_F(StunPortTest, TestPrepareAddress) { |
188 CreateStunPort(kStunAddr1); | 195 CreateStunPort(kStunAddr1); |
189 PrepareAddress(); | 196 PrepareAddress(); |
190 EXPECT_TRUE_WAIT(done(), kTimeoutMs); | 197 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
191 ASSERT_EQ(1U, port()->Candidates().size()); | 198 ASSERT_EQ(1U, port()->Candidates().size()); |
192 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); | 199 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); |
193 | 200 |
194 // TODO: Add IPv6 tests here, once either physicalsocketserver supports | 201 // TODO: Add IPv6 tests here, once either physicalsocketserver supports |
195 // IPv6, or this test is changed to use VirtualSocketServer. | 202 // IPv6, or this test is changed to use VirtualSocketServer. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 ASSERT_EQ(1U, port()->Candidates().size()); | 240 ASSERT_EQ(1U, port()->Candidates().size()); |
234 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); | 241 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); |
235 // Waiting for 1 seond, which will allow us to process | 242 // Waiting for 1 seond, which will allow us to process |
236 // response for keepalive binding request. 500 ms is the keepalive delay. | 243 // response for keepalive binding request. 500 ms is the keepalive delay. |
237 rtc::Thread::Current()->ProcessMessages(1000); | 244 rtc::Thread::Current()->ProcessMessages(1000); |
238 ASSERT_EQ(1U, port()->Candidates().size()); | 245 ASSERT_EQ(1U, port()->Candidates().size()); |
239 } | 246 } |
240 | 247 |
241 // Test that a local candidate can be generated using a shared socket. | 248 // Test that a local candidate can be generated using a shared socket. |
242 TEST_F(StunPortTest, TestSharedSocketPrepareAddress) { | 249 TEST_F(StunPortTest, TestSharedSocketPrepareAddress) { |
243 CreateSharedStunPort(kStunAddr1); | 250 CreateSharedUdpPort(kStunAddr1); |
244 PrepareAddress(); | 251 PrepareAddress(); |
245 EXPECT_TRUE_WAIT(done(), kTimeoutMs); | 252 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
246 ASSERT_EQ(1U, port()->Candidates().size()); | 253 ASSERT_EQ(1U, port()->Candidates().size()); |
247 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); | 254 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); |
248 } | 255 } |
249 | 256 |
250 // Test that we still a get a local candidate with invalid stun server hostname. | 257 // Test that we still a get a local candidate with invalid stun server hostname. |
251 // Also verifing that UDPPort can receive packets when stun address can't be | 258 // Also verifing that UDPPort can receive packets when stun address can't be |
252 // resolved. | 259 // resolved. |
253 TEST_F(StunPortTest, TestSharedSocketPrepareAddressInvalidHostname) { | 260 TEST_F(StunPortTest, TestSharedSocketPrepareAddressInvalidHostname) { |
254 CreateSharedStunPort(kBadHostnameAddr); | 261 CreateSharedUdpPort(kBadHostnameAddr); |
255 PrepareAddress(); | 262 PrepareAddress(); |
256 EXPECT_TRUE_WAIT(done(), kTimeoutMs); | 263 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
257 ASSERT_EQ(1U, port()->Candidates().size()); | 264 ASSERT_EQ(1U, port()->Candidates().size()); |
258 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); | 265 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); |
259 | 266 |
260 // Send data to port after it's ready. This is to make sure, UDP port can | 267 // Send data to port after it's ready. This is to make sure, UDP port can |
261 // handle data with unresolved stun server address. | 268 // handle data with unresolved stun server address. |
262 std::string data = "some random data, sending to cricket::Port."; | 269 std::string data = "some random data, sending to cricket::Port."; |
263 SendData(data.c_str(), data.length()); | 270 SendData(data.c_str(), data.length()); |
264 // No crash is success. | 271 // No crash is success. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); | 333 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
327 CreateStunPort(kStunAddr2); | 334 CreateStunPort(kStunAddr2); |
328 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); | 335 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); |
329 } | 336 } |
330 | 337 |
331 // Test that the stun_keepalive_lifetime is set correctly based on the network | 338 // Test that the stun_keepalive_lifetime is set correctly based on the network |
332 // type on a shared STUN port (UDPPort). Also test that it will be updated | 339 // type on a shared STUN port (UDPPort). Also test that it will be updated |
333 // if the network type changes. | 340 // if the network type changes. |
334 TEST_F(StunPortTest, TestUdpPortGetStunKeepaliveLifetime) { | 341 TEST_F(StunPortTest, TestUdpPortGetStunKeepaliveLifetime) { |
335 // Lifetime for the default (unknown) network type is |kInfiniteLifetime|. | 342 // Lifetime for the default (unknown) network type is |kInfiniteLifetime|. |
336 CreateSharedStunPort(kStunAddr1); | 343 CreateSharedUdpPort(kStunAddr1); |
337 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); | 344 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); |
338 // Lifetime for the cellular network is |kHighCostPortKeepaliveLifetimeMs|. | 345 // Lifetime for the cellular network is |kHighCostPortKeepaliveLifetimeMs|. |
339 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); | 346 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
340 EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs, | 347 EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs, |
341 port()->stun_keepalive_lifetime()); | 348 port()->stun_keepalive_lifetime()); |
342 | 349 |
343 // Lifetime for the wifi network type is |kInfiniteLifetime|. | 350 // Lifetime for the wifi network type is |kInfiniteLifetime|. |
344 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); | 351 SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
345 CreateSharedStunPort(kStunAddr2); | 352 CreateSharedUdpPort(kStunAddr2); |
346 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); | 353 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); |
347 } | 354 } |
348 | 355 |
349 // Test that STUN binding requests will be stopped shortly if the keep-alive | 356 // Test that STUN binding requests will be stopped shortly if the keep-alive |
350 // lifetime is short. | 357 // lifetime is short. |
351 TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) { | 358 TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) { |
352 SetKeepaliveDelay(101); | 359 SetKeepaliveDelay(101); |
353 SetKeepaliveLifetime(100); | 360 SetKeepaliveLifetime(100); |
354 CreateStunPort(kStunAddr1); | 361 CreateStunPort(kStunAddr1); |
355 PrepareAddress(); | 362 PrepareAddress(); |
356 EXPECT_TRUE_WAIT(done(), kTimeoutMs); | 363 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
357 EXPECT_TRUE_WAIT(!port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), | 364 EXPECT_TRUE_WAIT(!port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), |
358 2000); | 365 2000); |
359 } | 366 } |
360 | 367 |
361 // Test that by default, the STUN binding requests will last for a long time. | 368 // Test that by default, the STUN binding requests will last for a long time. |
362 TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) { | 369 TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) { |
363 SetKeepaliveDelay(101); | 370 SetKeepaliveDelay(101); |
364 CreateStunPort(kStunAddr1); | 371 CreateStunPort(kStunAddr1); |
365 PrepareAddress(); | 372 PrepareAddress(); |
366 EXPECT_TRUE_WAIT(done(), kTimeoutMs); | 373 EXPECT_TRUE_WAIT(done(), kTimeoutMs); |
367 rtc::Thread::Current()->ProcessMessages(1000); | 374 rtc::Thread::Current()->ProcessMessages(1000); |
368 EXPECT_TRUE(port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST)); | 375 EXPECT_TRUE(port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST)); |
369 } | 376 } |
OLD | NEW |