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

Side by Side Diff: net/proxy/init_proxy_resolver_unittest.cc

Issue 7044058: Cancel when InitProxyResolver cancels, so we don't use callback later. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add regression test, and fix NULL check (caught by existing test). Created 9 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « net/proxy/init_proxy_resolver.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <vector> 5 #include <vector>
6 6
7 #include "base/message_loop.h"
7 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/time.h"
8 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
9 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
10 #include "net/base/net_log.h" 12 #include "net/base/net_log.h"
11 #include "net/base/net_log_unittest.h" 13 #include "net/base/net_log_unittest.h"
12 #include "net/base/test_completion_callback.h" 14 #include "net/base/test_completion_callback.h"
13 #include "net/proxy/init_proxy_resolver.h" 15 #include "net/proxy/init_proxy_resolver.h"
14 #include "net/proxy/dhcp_proxy_script_fetcher.h" 16 #include "net/proxy/dhcp_proxy_script_fetcher.h"
15 #include "net/proxy/proxy_config.h" 17 #include "net/proxy/proxy_config.h"
16 #include "net/proxy/proxy_resolver.h" 18 #include "net/proxy/proxy_resolver.h"
17 #include "net/proxy/proxy_script_fetcher.h" 19 #include "net/proxy/proxy_script_fetcher.h"
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 return gurl_; 571 return gurl_;
570 } 572 }
571 573
572 const string16& expected_text() const { 574 const string16& expected_text() const {
573 return expected_text_; 575 return expected_text_;
574 } 576 }
575 577
576 private: 578 private:
577 GURL gurl_; 579 GURL gurl_;
578 string16 expected_text_; 580 string16 expected_text_;
581
582 DISALLOW_COPY_AND_ASSIGN(SynchronousSuccessDhcpFetcher);
579 }; 583 };
580 584
581 // All of the tests above that use InitProxyResolver have tested 585 // All of the tests above that use InitProxyResolver have tested
582 // failure to fetch a PAC file via DHCP configuration, so we now test 586 // failure to fetch a PAC file via DHCP configuration, so we now test
583 // success at downloading and parsing, and then success at downloading, 587 // success at downloading and parsing, and then success at downloading,
584 // failure at parsing. 588 // failure at parsing.
585 589
586 TEST(InitProxyResolverTest, AutodetectDhcpSuccess) { 590 TEST(InitProxyResolverTest, AutodetectDhcpSuccess) {
587 Rules rules; 591 Rules rules;
588 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); 592 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL); 630 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
627 // Since there is fallback to DNS-based WPAD, the final error will be that 631 // Since there is fallback to DNS-based WPAD, the final error will be that
628 // it failed downloading, not that it failed parsing. 632 // it failed downloading, not that it failed parsing.
629 EXPECT_EQ(kFailedDownloading, 633 EXPECT_EQ(kFailedDownloading,
630 init.Init(config, base::TimeDelta(), &effective_config, &callback)); 634 init.Init(config, base::TimeDelta(), &effective_config, &callback));
631 EXPECT_EQ(NULL, resolver.script_data()); 635 EXPECT_EQ(NULL, resolver.script_data());
632 636
633 EXPECT_FALSE(effective_config.has_pac_url()); 637 EXPECT_FALSE(effective_config.has_pac_url());
634 } 638 }
635 639
640 class AsyncFailDhcpFetcher
641 : public DhcpProxyScriptFetcher,
642 public base::RefCountedThreadSafe<AsyncFailDhcpFetcher> {
643 public:
644 AsyncFailDhcpFetcher() : callback_(NULL) {
645 }
646
647 int Fetch(string16* utf16_text, CompletionCallback* callback) OVERRIDE {
648 callback_ = callback;
649 MessageLoop::current()->PostTask(
650 FROM_HERE,
651 NewRunnableMethod(this, &AsyncFailDhcpFetcher::CallbackWithFailure));
652 return ERR_IO_PENDING;
653 }
654
655 void Cancel() OVERRIDE {
656 callback_ = NULL;
657 }
658
659 const GURL& GetPacURL() const OVERRIDE {
660 return dummy_gurl_;
661 }
662
663 void CallbackWithFailure() {
664 if (callback_)
665 callback_->Run(ERR_PAC_NOT_IN_DHCP);
666 }
667
668 private:
669 GURL dummy_gurl_;
670 CompletionCallback* callback_;
671 };
672
673 TEST(InitProxyResolverTest, DhcpCancelledByDestructor) {
674 // This regression test would crash before
675 // http://codereview.chromium.org/7044058/
676 // Thus, we don't care much about actual results (hence no EXPECT or ASSERT
677 // macros below), just that it doesn't crash.
678 Rules rules;
679 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
680 RuleBasedProxyScriptFetcher fetcher(&rules);
681
682 scoped_refptr<AsyncFailDhcpFetcher> dhcp_fetcher(new AsyncFailDhcpFetcher());
683
684 ProxyConfig config;
685 config.set_auto_detect(true);
686 rules.AddFailDownloadRule("http://wpad/wpad.dat");
687
688 TestCompletionCallback callback;
689
690 // Scope so InitProxyResolver gets destroyed early.
691 {
692 InitProxyResolver init(&resolver, &fetcher, dhcp_fetcher.get(), NULL);
693 init.Init(config, base::TimeDelta(), NULL, &callback);
694 }
695
696 // Run the message loop to let the DHCP fetch complete and post the results
697 // back. Before the fix linked to above, this would try to invoke on
698 // the callback object provided by InitProxyResolver after it was
699 // no longer valid.
700 MessageLoop::current()->RunAllPending();
701 }
702
636 } // namespace 703 } // namespace
637 } // namespace net 704 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/init_proxy_resolver.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698