OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 public: | 209 public: |
210 ServiceDiscoveryTest() | 210 ServiceDiscoveryTest() |
211 : service_discovery_client_(&mdns_client_) { | 211 : service_discovery_client_(&mdns_client_) { |
212 mdns_client_.StartListening(&socket_factory_); | 212 mdns_client_.StartListening(&socket_factory_); |
213 } | 213 } |
214 | 214 |
215 ~ServiceDiscoveryTest() override {} | 215 ~ServiceDiscoveryTest() override {} |
216 | 216 |
217 protected: | 217 protected: |
218 void RunFor(base::TimeDelta time_period) { | 218 void RunFor(base::TimeDelta time_period) { |
219 base::CancelableCallback<void()> callback(base::Bind( | 219 base::RunLoop run_loop; |
220 &ServiceDiscoveryTest::Stop, base::Unretained(this))); | 220 base::CancelableCallback<void()> callback(run_loop.QuitWhenIdleClosure()); |
221 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 221 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
222 FROM_HERE, callback.callback(), time_period); | 222 FROM_HERE, callback.callback(), time_period); |
223 | 223 run_loop.Run(); |
224 base::MessageLoop::current()->Run(); | |
225 callback.Cancel(); | 224 callback.Cancel(); |
226 } | 225 } |
227 | 226 |
228 void Stop() { base::MessageLoop::current()->QuitWhenIdle(); } | 227 void Stop() { base::MessageLoop::current()->QuitWhenIdle(); } |
229 | 228 |
230 net::MockMDnsSocketFactory socket_factory_; | 229 net::MockMDnsSocketFactory socket_factory_; |
231 net::MDnsClientImpl mdns_client_; | 230 net::MDnsClientImpl mdns_client_; |
232 ServiceDiscoveryClientImpl service_discovery_client_; | 231 ServiceDiscoveryClientImpl service_discovery_client_; |
233 base::MessageLoop loop_; | 232 base::MessageLoop loop_; |
234 }; | 233 }; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 std::unique_ptr<ServiceWatcher> watcher( | 280 std::unique_ptr<ServiceWatcher> watcher( |
282 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", | 281 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", |
283 delegate.GetCallback())); | 282 delegate.GetCallback())); |
284 | 283 |
285 watcher->Start(); | 284 watcher->Start(); |
286 | 285 |
287 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, | 286 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, |
288 "hello._privet._tcp.local")) | 287 "hello._privet._tcp.local")) |
289 .Times(Exactly(1)); | 288 .Times(Exactly(1)); |
290 | 289 |
291 base::MessageLoop::current()->RunUntilIdle(); | 290 base::RunLoop().RunUntilIdle(); |
292 } | 291 } |
293 | 292 |
294 | 293 |
295 TEST_F(ServiceDiscoveryTest, ReadCachedServicesMultiple) { | 294 TEST_F(ServiceDiscoveryTest, ReadCachedServicesMultiple) { |
296 socket_factory_.SimulateReceive(kSamplePacketPTR2, sizeof(kSamplePacketPTR2)); | 295 socket_factory_.SimulateReceive(kSamplePacketPTR2, sizeof(kSamplePacketPTR2)); |
297 | 296 |
298 StrictMock<MockServiceWatcherClient> delegate; | 297 StrictMock<MockServiceWatcherClient> delegate; |
299 std::unique_ptr<ServiceWatcher> watcher = | 298 std::unique_ptr<ServiceWatcher> watcher = |
300 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", | 299 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", |
301 delegate.GetCallback()); | 300 delegate.GetCallback()); |
302 | 301 |
303 watcher->Start(); | 302 watcher->Start(); |
304 | 303 |
305 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, | 304 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, |
306 "hello._privet._tcp.local")) | 305 "hello._privet._tcp.local")) |
307 .Times(Exactly(1)); | 306 .Times(Exactly(1)); |
308 | 307 |
309 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, | 308 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, |
310 "gdbye._privet._tcp.local")) | 309 "gdbye._privet._tcp.local")) |
311 .Times(Exactly(1)); | 310 .Times(Exactly(1)); |
312 | 311 |
313 base::MessageLoop::current()->RunUntilIdle(); | 312 base::RunLoop().RunUntilIdle(); |
314 } | 313 } |
315 | 314 |
316 | 315 |
317 TEST_F(ServiceDiscoveryTest, OnServiceChanged) { | 316 TEST_F(ServiceDiscoveryTest, OnServiceChanged) { |
318 StrictMock<MockServiceWatcherClient> delegate; | 317 StrictMock<MockServiceWatcherClient> delegate; |
319 std::unique_ptr<ServiceWatcher> watcher( | 318 std::unique_ptr<ServiceWatcher> watcher( |
320 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", | 319 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", |
321 delegate.GetCallback())); | 320 delegate.GetCallback())); |
322 | 321 |
323 watcher->Start(); | 322 watcher->Start(); |
324 | 323 |
325 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, | 324 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, |
326 "hello._privet._tcp.local")) | 325 "hello._privet._tcp.local")) |
327 .Times(Exactly(1)); | 326 .Times(Exactly(1)); |
328 | 327 |
329 socket_factory_.SimulateReceive(kSamplePacketPTR, sizeof(kSamplePacketPTR)); | 328 socket_factory_.SimulateReceive(kSamplePacketPTR, sizeof(kSamplePacketPTR)); |
330 | 329 |
331 base::MessageLoop::current()->RunUntilIdle(); | 330 base::RunLoop().RunUntilIdle(); |
332 | 331 |
333 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_CHANGED, | 332 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_CHANGED, |
334 "hello._privet._tcp.local")) | 333 "hello._privet._tcp.local")) |
335 .Times(Exactly(1)); | 334 .Times(Exactly(1)); |
336 | 335 |
337 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); | 336 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); |
338 | 337 |
339 socket_factory_.SimulateReceive(kSamplePacketTXT, sizeof(kSamplePacketTXT)); | 338 socket_factory_.SimulateReceive(kSamplePacketTXT, sizeof(kSamplePacketTXT)); |
340 | 339 |
341 base::MessageLoop::current()->RunUntilIdle(); | 340 base::RunLoop().RunUntilIdle(); |
342 } | 341 } |
343 | 342 |
344 TEST_F(ServiceDiscoveryTest, SinglePacket) { | 343 TEST_F(ServiceDiscoveryTest, SinglePacket) { |
345 StrictMock<MockServiceWatcherClient> delegate; | 344 StrictMock<MockServiceWatcherClient> delegate; |
346 std::unique_ptr<ServiceWatcher> watcher( | 345 std::unique_ptr<ServiceWatcher> watcher( |
347 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", | 346 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", |
348 delegate.GetCallback())); | 347 delegate.GetCallback())); |
349 | 348 |
350 watcher->Start(); | 349 watcher->Start(); |
351 | 350 |
352 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, | 351 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, |
353 "hello._privet._tcp.local")) | 352 "hello._privet._tcp.local")) |
354 .Times(Exactly(1)); | 353 .Times(Exactly(1)); |
355 | 354 |
356 socket_factory_.SimulateReceive(kSamplePacketPTR, sizeof(kSamplePacketPTR)); | 355 socket_factory_.SimulateReceive(kSamplePacketPTR, sizeof(kSamplePacketPTR)); |
357 | 356 |
358 // Reset the "already updated" flag. | 357 // Reset the "already updated" flag. |
359 base::MessageLoop::current()->RunUntilIdle(); | 358 base::RunLoop().RunUntilIdle(); |
360 | 359 |
361 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_CHANGED, | 360 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_CHANGED, |
362 "hello._privet._tcp.local")) | 361 "hello._privet._tcp.local")) |
363 .Times(Exactly(1)); | 362 .Times(Exactly(1)); |
364 | 363 |
365 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); | 364 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); |
366 | 365 |
367 socket_factory_.SimulateReceive(kSamplePacketTXT, sizeof(kSamplePacketTXT)); | 366 socket_factory_.SimulateReceive(kSamplePacketTXT, sizeof(kSamplePacketTXT)); |
368 | 367 |
369 base::MessageLoop::current()->RunUntilIdle(); | 368 base::RunLoop().RunUntilIdle(); |
370 } | 369 } |
371 | 370 |
372 TEST_F(ServiceDiscoveryTest, ActivelyRefreshServices) { | 371 TEST_F(ServiceDiscoveryTest, ActivelyRefreshServices) { |
373 StrictMock<MockServiceWatcherClient> delegate; | 372 StrictMock<MockServiceWatcherClient> delegate; |
374 std::unique_ptr<ServiceWatcher> watcher( | 373 std::unique_ptr<ServiceWatcher> watcher( |
375 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", | 374 service_discovery_client_.CreateServiceWatcher("_privet._tcp.local", |
376 delegate.GetCallback())); | 375 delegate.GetCallback())); |
377 | 376 |
378 watcher->Start(); | 377 watcher->Start(); |
379 watcher->SetActivelyRefreshServices(true); | 378 watcher->SetActivelyRefreshServices(true); |
380 | 379 |
381 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, | 380 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_ADDED, |
382 "hello._privet._tcp.local")) | 381 "hello._privet._tcp.local")) |
383 .Times(Exactly(1)); | 382 .Times(Exactly(1)); |
384 | 383 |
385 std::string query_packet = std::string((const char*)(kSamplePacketQuerySRV), | 384 std::string query_packet = std::string((const char*)(kSamplePacketQuerySRV), |
386 sizeof(kSamplePacketQuerySRV)); | 385 sizeof(kSamplePacketQuerySRV)); |
387 | 386 |
388 EXPECT_CALL(socket_factory_, OnSendTo(query_packet)) | 387 EXPECT_CALL(socket_factory_, OnSendTo(query_packet)) |
389 .Times(2); | 388 .Times(2); |
390 | 389 |
391 socket_factory_.SimulateReceive(kSamplePacketPTR, sizeof(kSamplePacketPTR)); | 390 socket_factory_.SimulateReceive(kSamplePacketPTR, sizeof(kSamplePacketPTR)); |
392 | 391 |
393 base::MessageLoop::current()->RunUntilIdle(); | 392 base::RunLoop().RunUntilIdle(); |
394 | 393 |
395 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); | 394 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); |
396 | 395 |
397 EXPECT_CALL(socket_factory_, OnSendTo(query_packet)) | 396 EXPECT_CALL(socket_factory_, OnSendTo(query_packet)) |
398 .Times(4); // IPv4 and IPv6 at 85% and 95% | 397 .Times(4); // IPv4 and IPv6 at 85% and 95% |
399 | 398 |
400 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_REMOVED, | 399 EXPECT_CALL(delegate, OnServiceUpdated(ServiceWatcher::UPDATE_REMOVED, |
401 "hello._privet._tcp.local")) | 400 "hello._privet._tcp.local")) |
402 .Times(Exactly(1)); | 401 .Times(Exactly(1)); |
403 | 402 |
404 RunFor(base::TimeDelta::FromSeconds(2)); | 403 RunFor(base::TimeDelta::FromSeconds(2)); |
405 | 404 |
406 base::MessageLoop::current()->RunUntilIdle(); | 405 base::RunLoop().RunUntilIdle(); |
407 } | 406 } |
408 | 407 |
409 | 408 |
410 class ServiceResolverTest : public ServiceDiscoveryTest { | 409 class ServiceResolverTest : public ServiceDiscoveryTest { |
411 public: | 410 public: |
412 ServiceResolverTest() { | 411 ServiceResolverTest() { |
413 metadata_expected_.push_back("hello"); | 412 metadata_expected_.push_back("hello"); |
414 address_expected_ = net::HostPortPair("myhello.local", 8888); | 413 address_expected_ = net::HostPortPair("myhello.local", 8888); |
415 EXPECT_TRUE(ip_address_expected_.AssignFromIPLiteral("1.2.3.4")); | 414 EXPECT_TRUE(ip_address_expected_.AssignFromIPLiteral("1.2.3.4")); |
416 } | 415 } |
(...skipping 30 matching lines...) Expand all Loading... |
447 net::IPAddress ip_address_expected_; | 446 net::IPAddress ip_address_expected_; |
448 }; | 447 }; |
449 | 448 |
450 TEST_F(ServiceResolverTest, TxtAndSrvButNoA) { | 449 TEST_F(ServiceResolverTest, TxtAndSrvButNoA) { |
451 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(4); | 450 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(4); |
452 | 451 |
453 resolver_->StartResolving(); | 452 resolver_->StartResolving(); |
454 | 453 |
455 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); | 454 socket_factory_.SimulateReceive(kSamplePacketSRV, sizeof(kSamplePacketSRV)); |
456 | 455 |
457 base::MessageLoop::current()->RunUntilIdle(); | 456 base::RunLoop().RunUntilIdle(); |
458 | 457 |
459 EXPECT_CALL( | 458 EXPECT_CALL( |
460 *this, OnFinishedResolvingInternal(ServiceResolver::STATUS_SUCCESS, | 459 *this, OnFinishedResolvingInternal(ServiceResolver::STATUS_SUCCESS, |
461 address_expected_.ToString(), | 460 address_expected_.ToString(), |
462 metadata_expected_, net::IPAddress())); | 461 metadata_expected_, net::IPAddress())); |
463 | 462 |
464 socket_factory_.SimulateReceive(kSamplePacketTXT, sizeof(kSamplePacketTXT)); | 463 socket_factory_.SimulateReceive(kSamplePacketTXT, sizeof(kSamplePacketTXT)); |
465 } | 464 } |
466 | 465 |
467 TEST_F(ServiceResolverTest, TxtSrvAndA) { | 466 TEST_F(ServiceResolverTest, TxtSrvAndA) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 ServiceResolver::STATUS_REQUEST_TIMEOUT, _, _, _)); | 506 ServiceResolver::STATUS_REQUEST_TIMEOUT, _, _, _)); |
508 | 507 |
509 // TODO(noamsml): When NSEC record support is added, change this to use an | 508 // TODO(noamsml): When NSEC record support is added, change this to use an |
510 // NSEC record. | 509 // NSEC record. |
511 RunFor(base::TimeDelta::FromSeconds(4)); | 510 RunFor(base::TimeDelta::FromSeconds(4)); |
512 } | 511 } |
513 | 512 |
514 } // namespace | 513 } // namespace |
515 | 514 |
516 } // namespace local_discovery | 515 } // namespace local_discovery |
OLD | NEW |