blob: c7ced3a33e8597a3a8de4a6adae292d51ba9d7fe [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
kwiberg3ec46792016-04-27 07:22:53 -070011#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "p2p/base/basicpacketsocketfactory.h"
14#include "p2p/base/stunport.h"
15#include "p2p/base/teststunserver.h"
16#include "rtc_base/gunit.h"
17#include "rtc_base/helpers.h"
18#include "rtc_base/socketaddress.h"
19#include "rtc_base/ssladapter.h"
20#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021
22using cricket::ServerAddresses;
23using rtc::SocketAddress;
24
25static const SocketAddress kLocalAddr("127.0.0.1", 0);
26static const SocketAddress kStunAddr1("127.0.0.1", 5000);
27static const SocketAddress kStunAddr2("127.0.0.1", 4000);
Honghai Zhang351d77b2016-05-20 15:08:29 -070028static const SocketAddress kStunAddr3("127.0.0.1", 3000);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000029static const SocketAddress kBadAddr("0.0.0.1", 5000);
30static const SocketAddress kStunHostnameAddr("localhost", 5000);
31static const SocketAddress kBadHostnameAddr("not-a-real-hostname", 5000);
pthatcher94a2f212017-02-08 14:42:22 -080032// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070033// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080034static const int kTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035// stun prio = 100 << 24 | 30 (IPV4) << 8 | 256 - 0
Peter Boström0c4e06b2015-10-07 12:23:21 +020036static const uint32_t kStunCandidatePriority = 1677729535;
honghaize2af9ef2016-03-03 08:27:47 -080037static const int kInfiniteLifetime = -1;
38static const int kHighCostPortKeepaliveLifetimeMs = 2 * 60 * 1000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039
40// Tests connecting a StunPort to a fake STUN server (cricket::StunServer)
pthatcher1749bc32017-02-08 13:18:00 -080041class StunPortTestBase : public testing::Test, public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 public:
pthatcher1749bc32017-02-08 13:18:00 -080043 StunPortTestBase()
deadbeef98e186c2017-05-16 18:00:06 -070044 : ss_(new rtc::VirtualSocketServer()),
nisse7eaa4ea2017-05-08 05:25:41 -070045 thread_(ss_.get()),
deadbeef5c3c1042017-08-04 15:01:57 -070046 network_("unittest", "unittest", kLocalAddr.ipaddr(), 32),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 socket_factory_(rtc::Thread::Current()),
honghaize2af9ef2016-03-03 08:27:47 -080048 stun_server_1_(cricket::TestStunServer::Create(rtc::Thread::Current(),
49 kStunAddr1)),
50 stun_server_2_(cricket::TestStunServer::Create(rtc::Thread::Current(),
51 kStunAddr2)),
52 done_(false),
53 error_(false),
pthatcher1749bc32017-02-08 13:18:00 -080054 stun_keepalive_delay_(1),
deadbeef5c3c1042017-08-04 15:01:57 -070055 stun_keepalive_lifetime_(-1) {
56 network_.AddIP(kLocalAddr.ipaddr());
57 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058
honghaize2af9ef2016-03-03 08:27:47 -080059 cricket::UDPPort* port() const { return stun_port_.get(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060 bool done() const { return done_; }
61 bool error() const { return error_; }
62
honghaize2af9ef2016-03-03 08:27:47 -080063 void SetNetworkType(rtc::AdapterType adapter_type) {
64 network_.set_type(adapter_type);
65 }
66
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067 void CreateStunPort(const rtc::SocketAddress& server_addr) {
68 ServerAddresses stun_servers;
69 stun_servers.insert(server_addr);
70 CreateStunPort(stun_servers);
71 }
72
73 void CreateStunPort(const ServerAddresses& stun_servers) {
74 stun_port_.reset(cricket::StunPort::Create(
deadbeef5c3c1042017-08-04 15:01:57 -070075 rtc::Thread::Current(), &socket_factory_, &network_, 0, 0,
76 rtc::CreateRandomString(16), rtc::CreateRandomString(22), stun_servers,
Danil Chapovalov00c71832018-06-15 15:58:38 +020077 std::string(), absl::nullopt));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078 stun_port_->set_stun_keepalive_delay(stun_keepalive_delay_);
honghaize2af9ef2016-03-03 08:27:47 -080079 // If |stun_keepalive_lifetime_| is negative, let the stun port
80 // choose its lifetime from the network type.
81 if (stun_keepalive_lifetime_ >= 0) {
82 stun_port_->set_stun_keepalive_lifetime(stun_keepalive_lifetime_);
83 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084 stun_port_->SignalPortComplete.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -080085 &StunPortTestBase::OnPortComplete);
86 stun_port_->SignalPortError.connect(this, &StunPortTestBase::OnPortError);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087 }
88
Honghai Zhangd00c0572016-06-28 09:44:47 -070089 void CreateSharedUdpPort(const rtc::SocketAddress& server_addr) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090 socket_.reset(socket_factory_.CreateUdpSocket(
91 rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0));
92 ASSERT_TRUE(socket_ != NULL);
pthatcher1749bc32017-02-08 13:18:00 -080093 socket_->SignalReadPacket.connect(this, &StunPortTestBase::OnReadPacket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094 stun_port_.reset(cricket::UDPPort::Create(
Qingsi Wang4ff54432018-03-01 18:25:20 -080095 rtc::Thread::Current(), &socket_factory_, &network_, socket_.get(),
96 rtc::CreateRandomString(16), rtc::CreateRandomString(22), std::string(),
Danil Chapovalov00c71832018-06-15 15:58:38 +020097 false, absl::nullopt));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098 ASSERT_TRUE(stun_port_ != NULL);
99 ServerAddresses stun_servers;
100 stun_servers.insert(server_addr);
101 stun_port_->set_server_addresses(stun_servers);
102 stun_port_->SignalPortComplete.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800103 &StunPortTestBase::OnPortComplete);
104 stun_port_->SignalPortError.connect(this, &StunPortTestBase::OnPortError);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105 }
106
107 void PrepareAddress() {
108 stun_port_->PrepareAddress();
109 }
110
111 void OnReadPacket(rtc::AsyncPacketSocket* socket, const char* data,
112 size_t size, const rtc::SocketAddress& remote_addr,
113 const rtc::PacketTime& packet_time) {
114 stun_port_->HandleIncomingPacket(
115 socket, data, size, remote_addr, rtc::PacketTime());
116 }
117
118 void SendData(const char* data, size_t len) {
119 stun_port_->HandleIncomingPacket(
120 socket_.get(), data, len, rtc::SocketAddress("22.22.22.22", 0),
121 rtc::PacketTime());
122 }
123
124 protected:
125 static void SetUpTestCase() {
126 // Ensure the RNG is inited.
127 rtc::InitRandom(NULL, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 }
129
130 void OnPortComplete(cricket::Port* port) {
131 ASSERT_FALSE(done_);
132 done_ = true;
133 error_ = false;
134 }
135 void OnPortError(cricket::Port* port) {
136 done_ = true;
137 error_ = true;
138 }
139 void SetKeepaliveDelay(int delay) {
140 stun_keepalive_delay_ = delay;
141 }
142
honghaize2af9ef2016-03-03 08:27:47 -0800143 void SetKeepaliveLifetime(int lifetime) {
144 stun_keepalive_lifetime_ = lifetime;
145 }
146
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 cricket::TestStunServer* stun_server_1() {
148 return stun_server_1_.get();
149 }
150 cricket::TestStunServer* stun_server_2() {
151 return stun_server_2_.get();
152 }
153
154 private:
kwiberg3ec46792016-04-27 07:22:53 -0700155 std::unique_ptr<rtc::VirtualSocketServer> ss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700156 rtc::AutoSocketServerThread thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 rtc::Network network_;
158 rtc::BasicPacketSocketFactory socket_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700159 std::unique_ptr<cricket::UDPPort> stun_port_;
160 std::unique_ptr<cricket::TestStunServer> stun_server_1_;
161 std::unique_ptr<cricket::TestStunServer> stun_server_2_;
162 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 bool done_;
164 bool error_;
165 int stun_keepalive_delay_;
honghaize2af9ef2016-03-03 08:27:47 -0800166 int stun_keepalive_lifetime_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167};
168
pthatcher1749bc32017-02-08 13:18:00 -0800169class StunPortTestWithRealClock : public StunPortTestBase {};
170
171class FakeClockBase {
172 public:
173 rtc::ScopedFakeClock fake_clock;
174};
175
176class StunPortTest : public FakeClockBase, public StunPortTestBase {};
177
Honghai Zhangd00c0572016-06-28 09:44:47 -0700178// Test that we can create a STUN port.
179TEST_F(StunPortTest, TestCreateStunPort) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 CreateStunPort(kStunAddr1);
181 EXPECT_EQ("stun", port()->Type());
182 EXPECT_EQ(0U, port()->Candidates().size());
183}
184
Honghai Zhangd00c0572016-06-28 09:44:47 -0700185// Test that we can create a UDP port.
186TEST_F(StunPortTest, TestCreateUdpPort) {
187 CreateSharedUdpPort(kStunAddr1);
188 EXPECT_EQ("local", port()->Type());
189 EXPECT_EQ(0U, port()->Candidates().size());
190}
191
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192// Test that we can get an address from a STUN server.
193TEST_F(StunPortTest, TestPrepareAddress) {
194 CreateStunPort(kStunAddr1);
195 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800196 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 ASSERT_EQ(1U, port()->Candidates().size());
198 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
zhihuang26d99c22017-02-13 12:47:27 -0800199 std::string expected_server_url = "stun:127.0.0.1:5000";
200 EXPECT_EQ(port()->Candidates()[0].url(), expected_server_url);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201
deadbeef98e186c2017-05-16 18:00:06 -0700202 // TODO(deadbeef): Add IPv6 tests here.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203}
204
205// Test that we fail properly if we can't get an address.
206TEST_F(StunPortTest, TestPrepareAddressFail) {
207 CreateStunPort(kBadAddr);
208 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800209 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210 EXPECT_TRUE(error());
211 EXPECT_EQ(0U, port()->Candidates().size());
212}
213
214// Test that we can get an address from a STUN server specified by a hostname.
aleloid49d3872017-03-28 03:00:07 -0700215// Crashes on Linux, see webrtc:7416
216#if defined(WEBRTC_LINUX)
217#define MAYBE_TestPrepareAddressHostname DISABLED_TestPrepareAddressHostname
218#else
219#define MAYBE_TestPrepareAddressHostname TestPrepareAddressHostname
220#endif
221TEST_F(StunPortTest, MAYBE_TestPrepareAddressHostname) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 CreateStunPort(kStunHostnameAddr);
223 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800224 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000225 ASSERT_EQ(1U, port()->Candidates().size());
226 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
227 EXPECT_EQ(kStunCandidatePriority, port()->Candidates()[0].priority());
228}
229
230// Test that we handle hostname lookup failures properly.
pthatcher1749bc32017-02-08 13:18:00 -0800231TEST_F(StunPortTestWithRealClock, TestPrepareAddressHostnameFail) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232 CreateStunPort(kBadHostnameAddr);
233 PrepareAddress();
234 EXPECT_TRUE_WAIT(done(), kTimeoutMs);
235 EXPECT_TRUE(error());
236 EXPECT_EQ(0U, port()->Candidates().size());
237}
238
239// This test verifies keepalive response messages don't result in
240// additional candidate generation.
241TEST_F(StunPortTest, TestKeepAliveResponse) {
242 SetKeepaliveDelay(500); // 500ms of keepalive delay.
deadbeef9a6f4d42017-05-15 19:43:33 -0700243 CreateStunPort(kStunAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000244 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800245 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 ASSERT_EQ(1U, port()->Candidates().size());
247 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
pthatcher1749bc32017-02-08 13:18:00 -0800248 SIMULATED_WAIT(false, 1000, fake_clock);
249 EXPECT_EQ(1U, port()->Candidates().size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000250}
251
252// Test that a local candidate can be generated using a shared socket.
253TEST_F(StunPortTest, TestSharedSocketPrepareAddress) {
Honghai Zhangd00c0572016-06-28 09:44:47 -0700254 CreateSharedUdpPort(kStunAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800256 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257 ASSERT_EQ(1U, port()->Candidates().size());
258 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
259}
260
261// Test that we still a get a local candidate with invalid stun server hostname.
262// Also verifing that UDPPort can receive packets when stun address can't be
263// resolved.
pthatcher1749bc32017-02-08 13:18:00 -0800264TEST_F(StunPortTestWithRealClock,
265 TestSharedSocketPrepareAddressInvalidHostname) {
Honghai Zhangd00c0572016-06-28 09:44:47 -0700266 CreateSharedUdpPort(kBadHostnameAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000267 PrepareAddress();
268 EXPECT_TRUE_WAIT(done(), kTimeoutMs);
269 ASSERT_EQ(1U, port()->Candidates().size());
270 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
271
272 // Send data to port after it's ready. This is to make sure, UDP port can
273 // handle data with unresolved stun server address.
274 std::string data = "some random data, sending to cricket::Port.";
275 SendData(data.c_str(), data.length());
276 // No crash is success.
277}
278
279// Test that the same address is added only once if two STUN servers are in use.
280TEST_F(StunPortTest, TestNoDuplicatedAddressWithTwoStunServers) {
281 ServerAddresses stun_servers;
282 stun_servers.insert(kStunAddr1);
283 stun_servers.insert(kStunAddr2);
284 CreateStunPort(stun_servers);
285 EXPECT_EQ("stun", port()->Type());
286 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800287 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000288 EXPECT_EQ(1U, port()->Candidates().size());
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700289 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000290}
291
292// Test that candidates can be allocated for multiple STUN servers, one of which
293// is not reachable.
294TEST_F(StunPortTest, TestMultipleStunServersWithBadServer) {
295 ServerAddresses stun_servers;
296 stun_servers.insert(kStunAddr1);
297 stun_servers.insert(kBadAddr);
298 CreateStunPort(stun_servers);
299 EXPECT_EQ("stun", port()->Type());
300 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800301 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000302 EXPECT_EQ(1U, port()->Candidates().size());
303}
304
305// Test that two candidates are allocated if the two STUN servers return
306// different mapped addresses.
307TEST_F(StunPortTest, TestTwoCandidatesWithTwoStunServersAcrossNat) {
308 const SocketAddress kStunMappedAddr1("77.77.77.77", 0);
309 const SocketAddress kStunMappedAddr2("88.77.77.77", 0);
310 stun_server_1()->set_fake_stun_addr(kStunMappedAddr1);
311 stun_server_2()->set_fake_stun_addr(kStunMappedAddr2);
312
313 ServerAddresses stun_servers;
314 stun_servers.insert(kStunAddr1);
315 stun_servers.insert(kStunAddr2);
316 CreateStunPort(stun_servers);
317 EXPECT_EQ("stun", port()->Type());
318 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800319 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000320 EXPECT_EQ(2U, port()->Candidates().size());
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700321 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
322 EXPECT_EQ(port()->Candidates()[1].relay_protocol(), "");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323}
honghaize2af9ef2016-03-03 08:27:47 -0800324
325// Test that the stun_keepalive_lifetime is set correctly based on the network
Honghai Zhang351d77b2016-05-20 15:08:29 -0700326// type on a STUN port. Also test that it will be updated if the network type
327// changes.
honghaize2af9ef2016-03-03 08:27:47 -0800328TEST_F(StunPortTest, TestStunPortGetStunKeepaliveLifetime) {
Honghai Zhang351d77b2016-05-20 15:08:29 -0700329 // Lifetime for the default (unknown) network type is |kInfiniteLifetime|.
honghaize2af9ef2016-03-03 08:27:47 -0800330 CreateStunPort(kStunAddr1);
331 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
honghaize2af9ef2016-03-03 08:27:47 -0800332 // Lifetime for the cellular network is |kHighCostPortKeepaliveLifetimeMs|
333 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
honghaize2af9ef2016-03-03 08:27:47 -0800334 EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs,
335 port()->stun_keepalive_lifetime());
Honghai Zhang351d77b2016-05-20 15:08:29 -0700336
337 // Lifetime for the wifi network is |kInfiniteLifetime|.
338 SetNetworkType(rtc::ADAPTER_TYPE_WIFI);
339 CreateStunPort(kStunAddr2);
340 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
honghaize2af9ef2016-03-03 08:27:47 -0800341}
342
343// Test that the stun_keepalive_lifetime is set correctly based on the network
Honghai Zhang351d77b2016-05-20 15:08:29 -0700344// type on a shared STUN port (UDPPort). Also test that it will be updated
345// if the network type changes.
honghaize2af9ef2016-03-03 08:27:47 -0800346TEST_F(StunPortTest, TestUdpPortGetStunKeepaliveLifetime) {
Honghai Zhang351d77b2016-05-20 15:08:29 -0700347 // Lifetime for the default (unknown) network type is |kInfiniteLifetime|.
Honghai Zhangd00c0572016-06-28 09:44:47 -0700348 CreateSharedUdpPort(kStunAddr1);
honghaize2af9ef2016-03-03 08:27:47 -0800349 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
Honghai Zhang351d77b2016-05-20 15:08:29 -0700350 // Lifetime for the cellular network is |kHighCostPortKeepaliveLifetimeMs|.
honghaize2af9ef2016-03-03 08:27:47 -0800351 SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR);
honghaize2af9ef2016-03-03 08:27:47 -0800352 EXPECT_EQ(kHighCostPortKeepaliveLifetimeMs,
353 port()->stun_keepalive_lifetime());
Honghai Zhang351d77b2016-05-20 15:08:29 -0700354
355 // Lifetime for the wifi network type is |kInfiniteLifetime|.
356 SetNetworkType(rtc::ADAPTER_TYPE_WIFI);
Honghai Zhangd00c0572016-06-28 09:44:47 -0700357 CreateSharedUdpPort(kStunAddr2);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700358 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
honghaize2af9ef2016-03-03 08:27:47 -0800359}
360
361// Test that STUN binding requests will be stopped shortly if the keep-alive
362// lifetime is short.
363TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) {
364 SetKeepaliveDelay(101);
365 SetKeepaliveLifetime(100);
366 CreateStunPort(kStunAddr1);
367 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800368 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
369 EXPECT_TRUE_SIMULATED_WAIT(
370 !port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), 2000,
371 fake_clock);
honghaize2af9ef2016-03-03 08:27:47 -0800372}
373
374// Test that by default, the STUN binding requests will last for a long time.
375TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) {
376 SetKeepaliveDelay(101);
377 CreateStunPort(kStunAddr1);
378 PrepareAddress();
pthatcher1749bc32017-02-08 13:18:00 -0800379 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
380 EXPECT_TRUE_SIMULATED_WAIT(
381 port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), 1000,
382 fake_clock);
honghaize2af9ef2016-03-03 08:27:47 -0800383}