Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: webrtc/p2p/base/stunport_unittest.cc

Issue 2680233002: Use fake clock in some more networks tests. (Closed)
Patch Set: Review comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 ss_(new rtc::VirtualSocketServer(pss_.get())), 51 ss_(new rtc::VirtualSocketServer(pss_.get())),
52 ss_scope_(ss_.get()), 52 ss_scope_(ss_.get()),
53 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), 53 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32),
54 socket_factory_(rtc::Thread::Current()), 54 socket_factory_(rtc::Thread::Current()),
55 stun_server_1_(cricket::TestStunServer::Create(rtc::Thread::Current(), 55 stun_server_1_(cricket::TestStunServer::Create(rtc::Thread::Current(),
56 kStunAddr1)), 56 kStunAddr1)),
57 stun_server_2_(cricket::TestStunServer::Create(rtc::Thread::Current(), 57 stun_server_2_(cricket::TestStunServer::Create(rtc::Thread::Current(),
58 kStunAddr2)), 58 kStunAddr2)),
59 done_(false), 59 done_(false),
60 error_(false), 60 error_(false),
61 stun_keepalive_delay_(0), 61 stun_keepalive_delay_(1),
62 stun_keepalive_lifetime_(-1) {} 62 stun_keepalive_lifetime_(-1) {}
63 63
64 cricket::UDPPort* port() const { return stun_port_.get(); } 64 cricket::UDPPort* port() const { return stun_port_.get(); }
65 bool done() const { return done_; } 65 bool done() const { return done_; }
66 bool error() const { return error_; } 66 bool error() const { return error_; }
67 67
68 void SetNetworkType(rtc::AdapterType adapter_type) { 68 void SetNetworkType(rtc::AdapterType adapter_type) {
69 network_.set_type(adapter_type); 69 network_.set_type(adapter_type);
70 } 70 }
71 71
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 stun_keepalive_lifetime_ = lifetime; 153 stun_keepalive_lifetime_ = lifetime;
154 } 154 }
155 155
156 cricket::TestStunServer* stun_server_1() { 156 cricket::TestStunServer* stun_server_1() {
157 return stun_server_1_.get(); 157 return stun_server_1_.get();
158 } 158 }
159 cricket::TestStunServer* stun_server_2() { 159 cricket::TestStunServer* stun_server_2() {
160 return stun_server_2_.get(); 160 return stun_server_2_.get();
161 } 161 }
162 162
163 rtc::FakeClock& fake_clock() { return fake_clock_; }
164
163 private: 165 private:
166 rtc::ScopedFakeClock fake_clock_;
164 std::unique_ptr<rtc::PhysicalSocketServer> pss_; 167 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
165 std::unique_ptr<rtc::VirtualSocketServer> ss_; 168 std::unique_ptr<rtc::VirtualSocketServer> ss_;
166 rtc::SocketServerScope ss_scope_; 169 rtc::SocketServerScope ss_scope_;
167 rtc::Network network_; 170 rtc::Network network_;
168 rtc::BasicPacketSocketFactory socket_factory_; 171 rtc::BasicPacketSocketFactory socket_factory_;
169 std::unique_ptr<cricket::UDPPort> stun_port_; 172 std::unique_ptr<cricket::UDPPort> stun_port_;
170 std::unique_ptr<cricket::TestStunServer> stun_server_1_; 173 std::unique_ptr<cricket::TestStunServer> stun_server_1_;
171 std::unique_ptr<cricket::TestStunServer> stun_server_2_; 174 std::unique_ptr<cricket::TestStunServer> stun_server_2_;
172 std::unique_ptr<rtc::AsyncPacketSocket> socket_; 175 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
173 bool done_; 176 bool done_;
(...skipping 13 matching lines...) Expand all
187 TEST_F(StunPortTest, TestCreateUdpPort) { 190 TEST_F(StunPortTest, TestCreateUdpPort) {
188 CreateSharedUdpPort(kStunAddr1); 191 CreateSharedUdpPort(kStunAddr1);
189 EXPECT_EQ("local", port()->Type()); 192 EXPECT_EQ("local", port()->Type());
190 EXPECT_EQ(0U, port()->Candidates().size()); 193 EXPECT_EQ(0U, port()->Candidates().size());
191 } 194 }
192 195
193 // Test that we can get an address from a STUN server. 196 // Test that we can get an address from a STUN server.
194 TEST_F(StunPortTest, TestPrepareAddress) { 197 TEST_F(StunPortTest, TestPrepareAddress) {
195 CreateStunPort(kStunAddr1); 198 CreateStunPort(kStunAddr1);
196 PrepareAddress(); 199 PrepareAddress();
197 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 200 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
198 ASSERT_EQ(1U, port()->Candidates().size()); 201 ASSERT_EQ(1U, port()->Candidates().size());
199 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); 202 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
200 203
201 // TODO: Add IPv6 tests here, once either physicalsocketserver supports 204 // TODO: Add IPv6 tests here, once either physicalsocketserver supports
202 // IPv6, or this test is changed to use VirtualSocketServer. 205 // IPv6, or this test is changed to use VirtualSocketServer.
203 } 206 }
204 207
205 // Test that we fail properly if we can't get an address. 208 // Test that we fail properly if we can't get an address.
206 TEST_F(StunPortTest, TestPrepareAddressFail) { 209 TEST_F(StunPortTest, TestPrepareAddressFail) {
207 CreateStunPort(kBadAddr); 210 CreateStunPort(kBadAddr);
208 PrepareAddress(); 211 PrepareAddress();
209 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 212 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
210 EXPECT_TRUE(error()); 213 EXPECT_TRUE(error());
211 EXPECT_EQ(0U, port()->Candidates().size()); 214 EXPECT_EQ(0U, port()->Candidates().size());
212 } 215 }
213 216
214 // Test that we can get an address from a STUN server specified by a hostname. 217 // Test that we can get an address from a STUN server specified by a hostname.
215 TEST_F(StunPortTest, TestPrepareAddressHostname) { 218 TEST_F(StunPortTest, TestPrepareAddressHostname) {
216 CreateStunPort(kStunHostnameAddr); 219 CreateStunPort(kStunHostnameAddr);
217 PrepareAddress(); 220 PrepareAddress();
218 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 221 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
219 ASSERT_EQ(1U, port()->Candidates().size()); 222 ASSERT_EQ(1U, port()->Candidates().size());
220 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); 223 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
221 EXPECT_EQ(kStunCandidatePriority, port()->Candidates()[0].priority()); 224 EXPECT_EQ(kStunCandidatePriority, port()->Candidates()[0].priority());
222 } 225 }
223 226
224 // Test that we handle hostname lookup failures properly. 227 // Test that we handle hostname lookup failures properly.
225 TEST_F(StunPortTest, TestPrepareAddressHostnameFail) { 228 TEST_F(StunPortTest, TestPrepareAddressHostnameFail) {
226 CreateStunPort(kBadHostnameAddr); 229 CreateStunPort(kBadHostnameAddr);
227 PrepareAddress(); 230 PrepareAddress();
228 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 231 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
229 EXPECT_TRUE(error()); 232 EXPECT_TRUE(error());
230 EXPECT_EQ(0U, port()->Candidates().size()); 233 EXPECT_EQ(0U, port()->Candidates().size());
231 } 234 }
232 235
233 // This test verifies keepalive response messages don't result in 236 // This test verifies keepalive response messages don't result in
234 // additional candidate generation. 237 // additional candidate generation.
235 TEST_F(StunPortTest, TestKeepAliveResponse) { 238 TEST_F(StunPortTest, TestKeepAliveResponse) {
236 SetKeepaliveDelay(500); // 500ms of keepalive delay. 239 SetKeepaliveDelay(500); // 500ms of keepalive delay.
237 CreateStunPort(kStunHostnameAddr); 240 CreateStunPort(kStunHostnameAddr);
238 PrepareAddress(); 241 PrepareAddress();
239 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 242 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
240 ASSERT_EQ(1U, port()->Candidates().size()); 243 ASSERT_EQ(1U, port()->Candidates().size());
241 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); 244 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
242 // Waiting for 1 seond, which will allow us to process 245 SIMULATED_WAIT(false, 1000, fake_clock());
243 // response for keepalive binding request. 500 ms is the keepalive delay. 246 EXPECT_EQ(1U, port()->Candidates().size());
244 rtc::Thread::Current()->ProcessMessages(1000);
245 ASSERT_EQ(1U, port()->Candidates().size());
246 } 247 }
247 248
248 // Test that a local candidate can be generated using a shared socket. 249 // Test that a local candidate can be generated using a shared socket.
249 TEST_F(StunPortTest, TestSharedSocketPrepareAddress) { 250 TEST_F(StunPortTest, TestSharedSocketPrepareAddress) {
250 CreateSharedUdpPort(kStunAddr1); 251 CreateSharedUdpPort(kStunAddr1);
251 PrepareAddress(); 252 PrepareAddress();
252 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 253 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
253 ASSERT_EQ(1U, port()->Candidates().size()); 254 ASSERT_EQ(1U, port()->Candidates().size());
254 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); 255 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
255 } 256 }
256 257
257 // Test that we still a get a local candidate with invalid stun server hostname. 258 // Test that we still a get a local candidate with invalid stun server hostname.
258 // Also verifing that UDPPort can receive packets when stun address can't be 259 // Also verifing that UDPPort can receive packets when stun address can't be
259 // resolved. 260 // resolved.
260 TEST_F(StunPortTest, TestSharedSocketPrepareAddressInvalidHostname) { 261 TEST_F(StunPortTest, TestSharedSocketPrepareAddressInvalidHostname) {
261 CreateSharedUdpPort(kBadHostnameAddr); 262 CreateSharedUdpPort(kBadHostnameAddr);
262 PrepareAddress(); 263 PrepareAddress();
263 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 264 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
264 ASSERT_EQ(1U, port()->Candidates().size()); 265 ASSERT_EQ(1U, port()->Candidates().size());
265 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address())); 266 EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
266 267
267 // Send data to port after it's ready. This is to make sure, UDP port can 268 // Send data to port after it's ready. This is to make sure, UDP port can
268 // handle data with unresolved stun server address. 269 // handle data with unresolved stun server address.
269 std::string data = "some random data, sending to cricket::Port."; 270 std::string data = "some random data, sending to cricket::Port.";
270 SendData(data.c_str(), data.length()); 271 SendData(data.c_str(), data.length());
271 // No crash is success. 272 // No crash is success.
272 } 273 }
273 274
274 // Test that the same address is added only once if two STUN servers are in use. 275 // Test that the same address is added only once if two STUN servers are in use.
275 TEST_F(StunPortTest, TestNoDuplicatedAddressWithTwoStunServers) { 276 TEST_F(StunPortTest, TestNoDuplicatedAddressWithTwoStunServers) {
276 ServerAddresses stun_servers; 277 ServerAddresses stun_servers;
277 stun_servers.insert(kStunAddr1); 278 stun_servers.insert(kStunAddr1);
278 stun_servers.insert(kStunAddr2); 279 stun_servers.insert(kStunAddr2);
279 CreateStunPort(stun_servers); 280 CreateStunPort(stun_servers);
280 EXPECT_EQ("stun", port()->Type()); 281 EXPECT_EQ("stun", port()->Type());
281 PrepareAddress(); 282 PrepareAddress();
282 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 283 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
283 EXPECT_EQ(1U, port()->Candidates().size()); 284 EXPECT_EQ(1U, port()->Candidates().size());
284 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), ""); 285 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
285 } 286 }
286 287
287 // Test that candidates can be allocated for multiple STUN servers, one of which 288 // Test that candidates can be allocated for multiple STUN servers, one of which
288 // is not reachable. 289 // is not reachable.
289 TEST_F(StunPortTest, TestMultipleStunServersWithBadServer) { 290 TEST_F(StunPortTest, TestMultipleStunServersWithBadServer) {
290 ServerAddresses stun_servers; 291 ServerAddresses stun_servers;
291 stun_servers.insert(kStunAddr1); 292 stun_servers.insert(kStunAddr1);
292 stun_servers.insert(kBadAddr); 293 stun_servers.insert(kBadAddr);
293 CreateStunPort(stun_servers); 294 CreateStunPort(stun_servers);
294 EXPECT_EQ("stun", port()->Type()); 295 EXPECT_EQ("stun", port()->Type());
295 PrepareAddress(); 296 PrepareAddress();
296 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 297 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
297 EXPECT_EQ(1U, port()->Candidates().size()); 298 EXPECT_EQ(1U, port()->Candidates().size());
298 } 299 }
299 300
300 // Test that two candidates are allocated if the two STUN servers return 301 // Test that two candidates are allocated if the two STUN servers return
301 // different mapped addresses. 302 // different mapped addresses.
302 TEST_F(StunPortTest, TestTwoCandidatesWithTwoStunServersAcrossNat) { 303 TEST_F(StunPortTest, TestTwoCandidatesWithTwoStunServersAcrossNat) {
303 const SocketAddress kStunMappedAddr1("77.77.77.77", 0); 304 const SocketAddress kStunMappedAddr1("77.77.77.77", 0);
304 const SocketAddress kStunMappedAddr2("88.77.77.77", 0); 305 const SocketAddress kStunMappedAddr2("88.77.77.77", 0);
305 stun_server_1()->set_fake_stun_addr(kStunMappedAddr1); 306 stun_server_1()->set_fake_stun_addr(kStunMappedAddr1);
306 stun_server_2()->set_fake_stun_addr(kStunMappedAddr2); 307 stun_server_2()->set_fake_stun_addr(kStunMappedAddr2);
307 308
308 ServerAddresses stun_servers; 309 ServerAddresses stun_servers;
309 stun_servers.insert(kStunAddr1); 310 stun_servers.insert(kStunAddr1);
310 stun_servers.insert(kStunAddr2); 311 stun_servers.insert(kStunAddr2);
311 CreateStunPort(stun_servers); 312 CreateStunPort(stun_servers);
312 EXPECT_EQ("stun", port()->Type()); 313 EXPECT_EQ("stun", port()->Type());
313 PrepareAddress(); 314 PrepareAddress();
314 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 315 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
315 EXPECT_EQ(2U, port()->Candidates().size()); 316 EXPECT_EQ(2U, port()->Candidates().size());
316 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), ""); 317 EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
317 EXPECT_EQ(port()->Candidates()[1].relay_protocol(), ""); 318 EXPECT_EQ(port()->Candidates()[1].relay_protocol(), "");
318 } 319 }
319 320
320 // Test that the stun_keepalive_lifetime is set correctly based on the network 321 // Test that the stun_keepalive_lifetime is set correctly based on the network
321 // type on a STUN port. Also test that it will be updated if the network type 322 // type on a STUN port. Also test that it will be updated if the network type
322 // changes. 323 // changes.
323 TEST_F(StunPortTest, TestStunPortGetStunKeepaliveLifetime) { 324 TEST_F(StunPortTest, TestStunPortGetStunKeepaliveLifetime) {
324 // Lifetime for the default (unknown) network type is |kInfiniteLifetime|. 325 // Lifetime for the default (unknown) network type is |kInfiniteLifetime|.
(...skipping 28 matching lines...) Expand all
353 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime()); 354 EXPECT_EQ(kInfiniteLifetime, port()->stun_keepalive_lifetime());
354 } 355 }
355 356
356 // Test that STUN binding requests will be stopped shortly if the keep-alive 357 // Test that STUN binding requests will be stopped shortly if the keep-alive
357 // lifetime is short. 358 // lifetime is short.
358 TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) { 359 TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) {
359 SetKeepaliveDelay(101); 360 SetKeepaliveDelay(101);
360 SetKeepaliveLifetime(100); 361 SetKeepaliveLifetime(100);
361 CreateStunPort(kStunAddr1); 362 CreateStunPort(kStunAddr1);
362 PrepareAddress(); 363 PrepareAddress();
363 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 364 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
364 EXPECT_TRUE_WAIT(!port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), 365 EXPECT_TRUE_SIMULATED_WAIT(
365 2000); 366 !port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), 2000,
367 fake_clock());
366 } 368 }
367 369
368 // Test that by default, the STUN binding requests will last for a long time. 370 // Test that by default, the STUN binding requests will last for a long time.
369 TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) { 371 TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) {
370 SetKeepaliveDelay(101); 372 SetKeepaliveDelay(101);
371 CreateStunPort(kStunAddr1); 373 CreateStunPort(kStunAddr1);
372 PrepareAddress(); 374 PrepareAddress();
373 EXPECT_TRUE_WAIT(done(), kTimeoutMs); 375 EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock());
374 rtc::Thread::Current()->ProcessMessages(1000); 376 EXPECT_TRUE_SIMULATED_WAIT(
375 EXPECT_TRUE(port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST)); 377 port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), 1000,
378 fake_clock());
376 } 379 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698