| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This test creates a fake safebrowsing service, where we can inject | 5 // This test creates a fake safebrowsing service, where we can inject |
| 6 // malware and phishing urls. It then uses a real browser to go to | 6 // malware and phishing urls. It then uses a real browser to go to |
| 7 // these urls, and sends "goback" or "proceed" commands and verifies | 7 // these urls, and sends "goback" or "proceed" commands and verifies |
| 8 // they work. | 8 // they work. |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using content::InterstitialPage; | 31 using content::InterstitialPage; |
| 32 using content::NavigationController; | 32 using content::NavigationController; |
| 33 using content::WebContents; | 33 using content::WebContents; |
| 34 | 34 |
| 35 // A SafeBrowingService class that allows us to inject the malicious URLs. | 35 // A SafeBrowingService class that allows us to inject the malicious URLs. |
| 36 class FakeSafeBrowsingService : public SafeBrowsingService { | 36 class FakeSafeBrowsingService : public SafeBrowsingService { |
| 37 public: | 37 public: |
| 38 FakeSafeBrowsingService() {} | 38 FakeSafeBrowsingService() {} |
| 39 | 39 |
| 40 virtual ~FakeSafeBrowsingService() {} | |
| 41 | |
| 42 // Called on the IO thread to check if the given url is safe or not. If we | 40 // Called on the IO thread to check if the given url is safe or not. If we |
| 43 // can synchronously determine that the url is safe, CheckUrl returns true. | 41 // can synchronously determine that the url is safe, CheckUrl returns true. |
| 44 // Otherwise it returns false, and "client" is called asynchronously with the | 42 // Otherwise it returns false, and "client" is called asynchronously with the |
| 45 // result when it is ready. | 43 // result when it is ready. |
| 46 // Overrides SafeBrowsingService::CheckBrowseUrl. | 44 // Overrides SafeBrowsingService::CheckBrowseUrl. |
| 47 virtual bool CheckBrowseUrl(const GURL& gurl, Client* client) { | 45 virtual bool CheckBrowseUrl(const GURL& gurl, Client* client) { |
| 48 if (badurls[gurl.spec()] == SAFE) | 46 if (badurls[gurl.spec()] == SAFE) |
| 49 return true; | 47 return true; |
| 50 | 48 |
| 51 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 82 } | 80 } |
| 83 | 81 |
| 84 std::string GetReport() { | 82 std::string GetReport() { |
| 85 EXPECT_TRUE(reports_.size() == 1); | 83 EXPECT_TRUE(reports_.size() == 1); |
| 86 return reports_[0]; | 84 return reports_[0]; |
| 87 } | 85 } |
| 88 | 86 |
| 89 std::vector<std::string> reports_; | 87 std::vector<std::string> reports_; |
| 90 | 88 |
| 91 private: | 89 private: |
| 90 virtual ~FakeSafeBrowsingService() {} |
| 91 |
| 92 base::hash_map<std::string, UrlCheckResult> badurls; | 92 base::hash_map<std::string, UrlCheckResult> badurls; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 // Factory that creates FakeSafeBrowsingService instances. | 95 // Factory that creates FakeSafeBrowsingService instances. |
| 96 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { | 96 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { |
| 97 public: | 97 public: |
| 98 TestSafeBrowsingServiceFactory() { } | 98 TestSafeBrowsingServiceFactory() { } |
| 99 virtual ~TestSafeBrowsingServiceFactory() { } | 99 virtual ~TestSafeBrowsingServiceFactory() { } |
| 100 | 100 |
| 101 virtual SafeBrowsingService* CreateSafeBrowsingService() { | 101 virtual SafeBrowsingService* CreateSafeBrowsingService() { |
| 102 return new FakeSafeBrowsingService(); | 102 return new FakeSafeBrowsingService(); |
| 103 } | 103 } |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 // A MalwareDetails class lets us intercept calls from the renderer. | 106 // A MalwareDetails class lets us intercept calls from the renderer. |
| 107 class FakeMalwareDetails : public MalwareDetails { | 107 class FakeMalwareDetails : public MalwareDetails { |
| 108 public: | 108 public: |
| 109 FakeMalwareDetails(SafeBrowsingService* sb_service, | 109 FakeMalwareDetails(SafeBrowsingService* sb_service, |
| 110 WebContents* web_contents, | 110 WebContents* web_contents, |
| 111 const SafeBrowsingService::UnsafeResource& unsafe_resource) | 111 const SafeBrowsingService::UnsafeResource& unsafe_resource) |
| 112 : MalwareDetails(sb_service, web_contents, unsafe_resource) { } | 112 : MalwareDetails(sb_service, web_contents, unsafe_resource) { } |
| 113 | 113 |
| 114 virtual ~FakeMalwareDetails() {} | |
| 115 | |
| 116 virtual void AddDOMDetails( | 114 virtual void AddDOMDetails( |
| 117 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { | 115 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { |
| 118 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 116 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 119 MalwareDetails::AddDOMDetails(params); | 117 MalwareDetails::AddDOMDetails(params); |
| 120 | 118 |
| 121 // Notify the UI thread that we got the dom details. | 119 // Notify the UI thread that we got the dom details. |
| 122 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 120 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 123 base::Bind(&FakeMalwareDetails::OnDOMDetailsDone, | 121 base::Bind(&FakeMalwareDetails::OnDOMDetailsDone, |
| 124 this)); | 122 this)); |
| 125 } | 123 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 145 | 143 |
| 146 void set_waiting(bool waiting) { | 144 void set_waiting(bool waiting) { |
| 147 waiting_ = waiting; | 145 waiting_ = waiting; |
| 148 } | 146 } |
| 149 | 147 |
| 150 safe_browsing::ClientMalwareReportRequest* get_report() { | 148 safe_browsing::ClientMalwareReportRequest* get_report() { |
| 151 return report_.get(); | 149 return report_.get(); |
| 152 } | 150 } |
| 153 | 151 |
| 154 private: | 152 private: |
| 153 virtual ~FakeMalwareDetails() {} |
| 154 |
| 155 // Some logic to figure out if we should wait for the dom details or not. | 155 // Some logic to figure out if we should wait for the dom details or not. |
| 156 // These variables should only be accessed in the UI thread. | 156 // These variables should only be accessed in the UI thread. |
| 157 bool got_dom_; | 157 bool got_dom_; |
| 158 bool waiting_; | 158 bool waiting_; |
| 159 | 159 |
| 160 }; | 160 }; |
| 161 | 161 |
| 162 class TestMalwareDetailsFactory : public MalwareDetailsFactory { | 162 class TestMalwareDetailsFactory : public MalwareDetailsFactory { |
| 163 public: | 163 public: |
| 164 TestMalwareDetailsFactory() { } | 164 TestMalwareDetailsFactory() { } |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 SendCommand("\"proceed\""); // Simulate the user clicking "back" | 600 SendCommand("\"proceed\""); // Simulate the user clicking "back" |
| 601 AssertNoInterstitial(true); // Assert the interstitial is gone | 601 AssertNoInterstitial(true); // Assert the interstitial is gone |
| 602 | 602 |
| 603 EXPECT_EQ( | 603 EXPECT_EQ( |
| 604 url, | 604 url, |
| 605 browser()->GetSelectedTabContentsWrapper()->web_contents()->GetURL()); | 605 browser()->GetSelectedTabContentsWrapper()->web_contents()->GetURL()); |
| 606 AssertReportSent(); | 606 AssertReportSent(); |
| 607 } | 607 } |
| 608 | 608 |
| 609 } // namespace | 609 } // namespace |
| OLD | NEW |