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 known- | 5 // This test creates a fake safebrowsing service, where we can inject known- |
6 // threat urls. It then uses a real browser to go to these urls, and sends | 6 // threat urls. It then uses a real browser to go to these urls, and sends |
7 // "goback" or "proceed" commands and verifies they work. | 7 // "goback" or "proceed" commands and verifies they work. |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/interstitials/security_interstitial_page_test_utils.h" | 16 #include "chrome/browser/interstitials/security_interstitial_page_test_utils.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/safe_browsing/database_manager.h" | 18 #include "chrome/browser/safe_browsing/database_manager.h" |
| 19 #include "chrome/browser/safe_browsing/local_database_manager.h" |
19 #include "chrome/browser/safe_browsing/malware_details.h" | 20 #include "chrome/browser/safe_browsing/malware_details.h" |
20 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" | 21 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
21 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 22 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
22 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 23 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 24 #include "chrome/browser/safe_browsing/test_database_manager.h" |
23 #include "chrome/browser/safe_browsing/ui_manager.h" | 25 #include "chrome/browser/safe_browsing/ui_manager.h" |
24 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
25 #include "chrome/browser/ui/browser_tabstrip.h" | 27 #include "chrome/browser/ui/browser_tabstrip.h" |
26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
27 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
29 #include "chrome/common/url_constants.h" | 31 #include "chrome/common/url_constants.h" |
30 #include "chrome/test/base/in_process_browser_test.h" | 32 #include "chrome/test/base/in_process_browser_test.h" |
31 #include "chrome/test/base/test_switches.h" | 33 #include "chrome/test/base/test_switches.h" |
32 #include "chrome/test/base/ui_test_utils.h" | 34 #include "chrome/test/base/ui_test_utils.h" |
(...skipping 15 matching lines...) Expand all Loading... |
48 using content::WebContents; | 50 using content::WebContents; |
49 | 51 |
50 namespace { | 52 namespace { |
51 | 53 |
52 const char kEmptyPage[] = "files/empty.html"; | 54 const char kEmptyPage[] = "files/empty.html"; |
53 const char kMalwarePage[] = "files/safe_browsing/malware.html"; | 55 const char kMalwarePage[] = "files/safe_browsing/malware.html"; |
54 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; | 56 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; |
55 | 57 |
56 // A SafeBrowsingDatabaseManager class that allows us to inject the malicious | 58 // A SafeBrowsingDatabaseManager class that allows us to inject the malicious |
57 // URLs. | 59 // URLs. |
58 class FakeSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { | 60 class FakeSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
59 public: | 61 public: |
60 explicit FakeSafeBrowsingDatabaseManager(SafeBrowsingService* service) | 62 FakeSafeBrowsingDatabaseManager() {} |
61 : SafeBrowsingDatabaseManager(service) { } | |
62 | 63 |
63 // Called on the IO thread to check if the given url is safe or not. If we | 64 // Called on the IO thread to check if the given url is safe or not. If we |
64 // can synchronously determine that the url is safe, CheckUrl returns true. | 65 // can synchronously determine that the url is safe, CheckUrl returns true. |
65 // Otherwise it returns false, and "client" is called asynchronously with the | 66 // Otherwise it returns false, and "client" is called asynchronously with the |
66 // result when it is ready. | 67 // result when it is ready. |
67 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl. | 68 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl. |
68 bool CheckBrowseUrl(const GURL& gurl, Client* client) override { | 69 bool CheckBrowseUrl(const GURL& gurl, Client* client) override { |
69 if (badurls[gurl.spec()] == SB_THREAT_TYPE_SAFE) | 70 if (badurls[gurl.spec()] == SB_THREAT_TYPE_SAFE) |
70 return true; | 71 return true; |
71 | 72 |
72 BrowserThread::PostTask( | 73 BrowserThread::PostTask( |
73 BrowserThread::IO, FROM_HERE, | 74 BrowserThread::IO, FROM_HERE, |
74 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone, | 75 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone, |
75 this, gurl, client)); | 76 this, gurl, client)); |
76 return false; | 77 return false; |
77 } | 78 } |
78 | 79 |
79 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) { | 80 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) { |
80 std::vector<SBThreatType> expected_threats; | 81 std::vector<SBThreatType> expected_threats; |
| 82 // TODO(nparker): Remove ref to LocalSafeBrowsingDatabase by calling |
| 83 // client->OnCheckBrowseUrlResult(..) directly. |
81 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); | 84 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); |
82 expected_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); | 85 expected_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); |
83 expected_threats.push_back(SB_THREAT_TYPE_URL_UNWANTED); | 86 expected_threats.push_back(SB_THREAT_TYPE_URL_UNWANTED); |
84 SafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check( | 87 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check( |
85 std::vector<GURL>(1, gurl), | 88 std::vector<GURL>(1, gurl), |
86 std::vector<SBFullHash>(), | 89 std::vector<SBFullHash>(), |
87 client, | 90 client, |
88 safe_browsing_util::MALWARE, | 91 safe_browsing_util::MALWARE, |
89 expected_threats); | 92 expected_threats); |
90 sb_check.url_results[0] = badurls[gurl.spec()]; | 93 sb_check.url_results[0] = badurls[gurl.spec()]; |
91 client->OnSafeBrowsingResult(sb_check); | 94 sb_check.OnSafeBrowsingResult(); |
92 } | 95 } |
93 | 96 |
94 void SetURLThreatType(const GURL& url, SBThreatType threat_type) { | 97 void SetURLThreatType(const GURL& url, SBThreatType threat_type) { |
95 badurls[url.spec()] = threat_type; | 98 badurls[url.spec()] = threat_type; |
96 } | 99 } |
97 | 100 |
| 101 // Called during startup, so must not check-fail. |
| 102 bool CheckExtensionIDs(const std::set<std::string>& extension_ids, |
| 103 Client* client) override { |
| 104 return true; |
| 105 } |
| 106 |
98 private: | 107 private: |
99 ~FakeSafeBrowsingDatabaseManager() override {} | 108 ~FakeSafeBrowsingDatabaseManager() override {} |
100 | 109 |
101 base::hash_map<std::string, SBThreatType> badurls; | 110 base::hash_map<std::string, SBThreatType> badurls; |
102 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); | 111 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); |
103 }; | 112 }; |
104 | 113 |
105 // A SafeBrowingUIManager class that allows intercepting malware details. | 114 // A SafeBrowingUIManager class that allows intercepting malware details. |
106 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager { | 115 class FakeSafeBrowsingUIManager : public SafeBrowsingUIManager { |
107 public: | 116 public: |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // Returned pointer has the same lifespan as the ui_manager_ refcounted | 174 // Returned pointer has the same lifespan as the ui_manager_ refcounted |
166 // object. | 175 // object. |
167 FakeSafeBrowsingUIManager* fake_ui_manager() { | 176 FakeSafeBrowsingUIManager* fake_ui_manager() { |
168 return fake_ui_manager_; | 177 return fake_ui_manager_; |
169 } | 178 } |
170 | 179 |
171 protected: | 180 protected: |
172 ~FakeSafeBrowsingService() override {} | 181 ~FakeSafeBrowsingService() override {} |
173 | 182 |
174 SafeBrowsingDatabaseManager* CreateDatabaseManager() override { | 183 SafeBrowsingDatabaseManager* CreateDatabaseManager() override { |
175 fake_database_manager_ = new FakeSafeBrowsingDatabaseManager(this); | 184 fake_database_manager_ = new FakeSafeBrowsingDatabaseManager(); |
176 return fake_database_manager_; | 185 return fake_database_manager_; |
177 } | 186 } |
178 | 187 |
179 SafeBrowsingUIManager* CreateUIManager() override { | 188 SafeBrowsingUIManager* CreateUIManager() override { |
180 fake_ui_manager_ = new FakeSafeBrowsingUIManager(this); | 189 fake_ui_manager_ = new FakeSafeBrowsingUIManager(this); |
181 return fake_ui_manager_; | 190 return fake_ui_manager_; |
182 } | 191 } |
183 | 192 |
| 193 SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate() override { |
| 194 // Our SafeBrowsingDatabaseManager doesn't implement this delegate. |
| 195 return NULL; |
| 196 } |
| 197 |
184 private: | 198 private: |
185 FakeSafeBrowsingDatabaseManager* fake_database_manager_; | 199 FakeSafeBrowsingDatabaseManager* fake_database_manager_; |
186 FakeSafeBrowsingUIManager* fake_ui_manager_; | 200 FakeSafeBrowsingUIManager* fake_ui_manager_; |
187 | 201 |
188 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); | 202 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); |
189 }; | 203 }; |
190 | 204 |
191 // Factory that creates FakeSafeBrowsingService instances. | 205 // Factory that creates FakeSafeBrowsingService instances. |
192 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { | 206 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { |
193 public: | 207 public: |
(...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageIDNTest, | 866 IN_PROC_BROWSER_TEST_P(SafeBrowsingBlockingPageIDNTest, |
853 SafeBrowsingBlockingPageDecodesIDN) { | 867 SafeBrowsingBlockingPageDecodesIDN) { |
854 EXPECT_TRUE(VerifyIDNDecoded()); | 868 EXPECT_TRUE(VerifyIDNDecoded()); |
855 } | 869 } |
856 | 870 |
857 INSTANTIATE_TEST_CASE_P(SafeBrowsingBlockingPageIDNTestWithThreatType, | 871 INSTANTIATE_TEST_CASE_P(SafeBrowsingBlockingPageIDNTestWithThreatType, |
858 SafeBrowsingBlockingPageIDNTest, | 872 SafeBrowsingBlockingPageIDNTest, |
859 testing::Values(SB_THREAT_TYPE_URL_MALWARE, | 873 testing::Values(SB_THREAT_TYPE_URL_MALWARE, |
860 SB_THREAT_TYPE_URL_PHISHING, | 874 SB_THREAT_TYPE_URL_PHISHING, |
861 SB_THREAT_TYPE_URL_UNWANTED)); | 875 SB_THREAT_TYPE_URL_UNWANTED)); |
OLD | NEW |