OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/dns/async_host_resolver.h" | 5 #include "net/dns/async_host_resolver.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "net/base/host_cache.h" | 9 #include "net/base/host_cache.h" |
10 #include "net/base/net_log.h" | 10 #include "net/base/net_log.h" |
11 #include "net/base/rand_callback.h" | 11 #include "net/base/rand_callback.h" |
12 #include "net/base/sys_addrinfo.h" | 12 #include "net/base/sys_addrinfo.h" |
13 #include "net/base/test_host_resolver_observer.h" | |
14 #include "net/dns/dns_test_util.h" | 13 #include "net/dns/dns_test_util.h" |
15 #include "net/socket/socket_test_util.h" | 14 #include "net/socket/socket_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace net { | 17 namespace net { |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 void VerifyAddressList(const std::vector<const char*>& ip_addresses, | 21 void VerifyAddressList(const std::vector<const char*>& ip_addresses, |
23 int port, | 22 int port, |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 | 479 |
481 rv_fail = callback_fail.WaitForResult(); | 480 rv_fail = callback_fail.WaitForResult(); |
482 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv_fail); | 481 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv_fail); |
483 EXPECT_EQ(static_cast<addrinfo*>(NULL), addrlist_fail.head()); | 482 EXPECT_EQ(static_cast<addrinfo*>(NULL), addrlist_fail.head()); |
484 | 483 |
485 rv2 = callback2_.WaitForResult(); | 484 rv2 = callback2_.WaitForResult(); |
486 EXPECT_EQ(OK, rv2); | 485 EXPECT_EQ(OK, rv2); |
487 VerifyAddressList(ip_addresses2_, kPortNum, addrlist2_); | 486 VerifyAddressList(ip_addresses2_, kPortNum, addrlist2_); |
488 } | 487 } |
489 | 488 |
490 // Test that registering, unregistering, and notifying of observers of | |
491 // resolution start, completion and cancellation (both due to CancelRequest | |
492 // and resolver destruction) work. | |
493 TEST_F(AsyncHostResolverTest, Observers) { | |
494 TestHostResolverObserver observer; | |
495 resolver_->AddObserver(&observer); | |
496 | |
497 int rv0 = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | |
498 BoundNetLog()); | |
499 int rv1 = resolver_->Resolve(info1_, &addrlist1_, &callback1_, NULL, | |
500 BoundNetLog()); | |
501 // We will cancel this one. | |
502 HostResolver::RequestHandle req2 = NULL; | |
503 int rv2 = resolver_->Resolve(info2_, &addrlist2_, &callback2_, &req2, | |
504 BoundNetLog()); | |
505 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
506 EXPECT_EQ(ERR_IO_PENDING, rv1); | |
507 EXPECT_EQ(ERR_IO_PENDING, rv2); | |
508 | |
509 // Cancel lookup 2. | |
510 resolver_->CancelRequest(req2); | |
511 | |
512 // Lookup 0 and 1 should succeed. | |
513 rv0 = callback0_.WaitForResult(); | |
514 EXPECT_EQ(OK, rv0); | |
515 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); | |
516 | |
517 rv1 = callback1_.WaitForResult(); | |
518 EXPECT_EQ(OK, rv1); | |
519 VerifyAddressList(ip_addresses1_, kPortNum, addrlist1_); | |
520 | |
521 // Next lookup should not have finished. | |
522 MessageLoop::current()->RunAllPending(); | |
523 EXPECT_FALSE(callback2_.have_result()); | |
524 | |
525 // Verify observer calls. | |
526 EXPECT_EQ(3u, observer.start_log.size()); | |
527 EXPECT_EQ(2u, observer.finish_log.size()); | |
528 EXPECT_EQ(1u, observer.cancel_log.size()); | |
529 | |
530 // Lookup 0 started and finished. | |
531 EXPECT_TRUE(observer.start_log[0] == | |
532 TestHostResolverObserver::StartOrCancelEntry(0, info0_)); | |
533 EXPECT_TRUE(observer.finish_log[0] == | |
534 TestHostResolverObserver::FinishEntry(0, true, info0_)); | |
535 | |
536 // Ditto for lookup 1. | |
537 EXPECT_TRUE(observer.start_log[1] == | |
538 TestHostResolverObserver::StartOrCancelEntry(1, info1_)); | |
539 EXPECT_TRUE(observer.finish_log[1] == | |
540 TestHostResolverObserver::FinishEntry(1, true, info1_)); | |
541 | |
542 // Lookup 2 was cancelled, hence, failed to finish. | |
543 EXPECT_TRUE(observer.start_log[2] == | |
544 TestHostResolverObserver::StartOrCancelEntry(2, info2_)); | |
545 EXPECT_TRUE(observer.cancel_log[0] == | |
546 TestHostResolverObserver::StartOrCancelEntry(2, info2_)); | |
547 | |
548 // Unregister observer. | |
549 resolver_->RemoveObserver(&observer); | |
550 | |
551 // We will do lookup 2 again but will not cancel it this time. | |
552 rv2 = resolver_->Resolve(info2_, &addrlist2_, &callback2_, NULL, | |
553 BoundNetLog()); | |
554 EXPECT_EQ(ERR_IO_PENDING, rv2); | |
555 | |
556 // Run lookup 2 to completion. | |
557 rv2 = callback2_.WaitForResult(); | |
558 EXPECT_EQ(OK, rv2); | |
559 VerifyAddressList(ip_addresses2_, kPortNum, addrlist2_); | |
560 | |
561 // Observer log should stay the same. | |
562 EXPECT_EQ(3u, observer.start_log.size()); | |
563 EXPECT_EQ(2u, observer.finish_log.size()); | |
564 EXPECT_EQ(1u, observer.cancel_log.size()); | |
565 | |
566 // Re-register observer. | |
567 resolver_->AddObserver(&observer); | |
568 | |
569 // Start lookup 3. | |
570 int rv3 = resolver_->Resolve(info3_, &addrlist3_, &callback3_, NULL, | |
571 BoundNetLog()); | |
572 EXPECT_EQ(ERR_IO_PENDING, rv3); | |
573 | |
574 // Destroy the resolver and make sure that observer was notified of just | |
575 // the resolution start. | |
576 resolver_.reset(); | |
577 | |
578 EXPECT_EQ(4u, observer.start_log.size()); // Was incremented by 1. | |
579 EXPECT_EQ(2u, observer.finish_log.size()); | |
580 EXPECT_EQ(2u, observer.cancel_log.size()); | |
581 | |
582 EXPECT_TRUE(observer.start_log[3] == | |
583 TestHostResolverObserver::StartOrCancelEntry(4, info3_)); | |
584 } | |
585 | |
586 } // namespace net | 489 } // namespace net |
OLD | NEW |