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

Side by Side Diff: chrome/browser/subresource_filter/subresource_filter_browsertest.cc

Issue 2645283007: Add the client for accessing Subresource Filter only list. (Closed)
Patch Set: feature Created 3 years, 8 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <map> 5 #include <map>
6 #include <memory> 6 #include <memory>
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/strings/string_piece.h" 17 #include "base/strings/string_piece.h"
18 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
19 #include "base/test/histogram_tester.h" 20 #include "base/test/histogram_tester.h"
20 #include "chrome/browser/browser_process.h" 21 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 22 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
22 #include "chrome/browser/metrics/subprocess_metrics_provider.h" 23 #include "chrome/browser/metrics/subprocess_metrics_provider.h"
23 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_ observer.h" 24 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_ observer.h"
24 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" 25 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
26 #include "chrome/browser/safe_browsing/v4_test_utils.h"
25 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" 27 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h"
26 #include "chrome/browser/ui/browser.h" 28 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_commands.h" 29 #include "chrome/browser/ui/browser_commands.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h" 30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/chrome_paths.h" 31 #include "chrome/common/chrome_paths.h"
30 #include "chrome/common/url_constants.h" 32 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h" 33 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h" 34 #include "chrome/test/base/ui_test_utils.h"
33 #include "components/content_settings/core/browser/host_content_settings_map.h" 35 #include "components/content_settings/core/browser/host_content_settings_map.h"
34 #include "components/content_settings/core/common/content_settings.h" 36 #include "components/content_settings/core/common/content_settings.h"
35 #include "components/safe_browsing_db/test_database_manager.h" 37 #include "components/safe_browsing_db/test_database_manager.h"
36 #include "components/safe_browsing_db/util.h" 38 #include "components/safe_browsing_db/util.h"
39 #include "components/safe_browsing_db/v4_database.h"
37 #include "components/security_interstitials/content/unsafe_resource.h" 40 #include "components/security_interstitials/content/unsafe_resource.h"
38 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter.h" 41 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter.h"
39 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter_test_utils.h" 42 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter_test_utils.h"
40 #include "components/subresource_filter/content/browser/content_ruleset_service. h" 43 #include "components/subresource_filter/content/browser/content_ruleset_service. h"
41 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" 44 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
42 #include "components/subresource_filter/core/browser/subresource_filter_features .h" 45 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
43 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" 46 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
44 #include "components/subresource_filter/core/common/activation_level.h" 47 #include "components/subresource_filter/core/common/activation_level.h"
45 #include "components/subresource_filter/core/common/activation_state.h" 48 #include "components/subresource_filter/core/common/activation_state.h"
46 #include "components/subresource_filter/core/common/scoped_timers.h" 49 #include "components/subresource_filter/core/common/scoped_timers.h"
(...skipping 13 matching lines...) Expand all
60 #include "net/test/spawned_test_server/spawned_test_server.h" 63 #include "net/test/spawned_test_server/spawned_test_server.h"
61 #include "net/test/test_data_directory.h" 64 #include "net/test/test_data_directory.h"
62 #include "testing/gmock/include/gmock/gmock.h" 65 #include "testing/gmock/include/gmock/gmock.h"
63 #include "testing/gtest/include/gtest/gtest.h" 66 #include "testing/gtest/include/gtest/gtest.h"
64 #include "url/gurl.h" 67 #include "url/gurl.h"
65 68
66 namespace { 69 namespace {
67 70
68 // The path to a multi-frame document used for tests. 71 // The path to a multi-frame document used for tests.
69 static constexpr const char kTestFrameSetPath[] = 72 static constexpr const char kTestFrameSetPath[] =
70 "subresource_filter/frame_set.html"; 73 "/subresource_filter/frame_set.html";
71 74
72 // Names of DocumentLoad histograms. 75 // Names of DocumentLoad histograms.
73 constexpr const char kDocumentLoadActivationLevel[] = 76 constexpr const char kDocumentLoadActivationLevel[] =
74 "SubresourceFilter.DocumentLoad.ActivationState"; 77 "SubresourceFilter.DocumentLoad.ActivationState";
75 constexpr const char kSubresourceLoadsTotal[] = 78 constexpr const char kSubresourceLoadsTotal[] =
76 "SubresourceFilter.DocumentLoad.NumSubresourceLoads.Total"; 79 "SubresourceFilter.DocumentLoad.NumSubresourceLoads.Total";
77 constexpr const char kSubresourceLoadsEvaluated[] = 80 constexpr const char kSubresourceLoadsEvaluated[] =
78 "SubresourceFilter.DocumentLoad.NumSubresourceLoads.Evaluated"; 81 "SubresourceFilter.DocumentLoad.NumSubresourceLoads.Evaluated";
79 constexpr const char kSubresourceLoadsMatchedRules[] = 82 constexpr const char kSubresourceLoadsMatchedRules[] =
80 "SubresourceFilter.DocumentLoad.NumSubresourceLoads.MatchedRules"; 83 "SubresourceFilter.DocumentLoad.NumSubresourceLoads.MatchedRules";
(...skipping 20 matching lines...) Expand all
101 "SubresourceFilter.PageLoad.SubresourceEvaluation.TotalCPUDuration"; 104 "SubresourceFilter.PageLoad.SubresourceEvaluation.TotalCPUDuration";
102 constexpr const char kEvaluationTotalWallDurationForDocument[] = 105 constexpr const char kEvaluationTotalWallDurationForDocument[] =
103 "SubresourceFilter.DocumentLoad.SubresourceEvaluation.TotalWallDuration"; 106 "SubresourceFilter.DocumentLoad.SubresourceEvaluation.TotalWallDuration";
104 constexpr const char kEvaluationTotalCPUDurationForDocument[] = 107 constexpr const char kEvaluationTotalCPUDurationForDocument[] =
105 "SubresourceFilter.DocumentLoad.SubresourceEvaluation.TotalCPUDuration"; 108 "SubresourceFilter.DocumentLoad.SubresourceEvaluation.TotalCPUDuration";
106 constexpr const char kEvaluationWallDuration[] = 109 constexpr const char kEvaluationWallDuration[] =
107 "SubresourceFilter.SubresourceLoad.Evaluation.WallDuration"; 110 "SubresourceFilter.SubresourceLoad.Evaluation.WallDuration";
108 constexpr const char kEvaluationCPUDuration[] = 111 constexpr const char kEvaluationCPUDuration[] =
109 "SubresourceFilter.SubresourceLoad.Evaluation.CPUDuration"; 112 "SubresourceFilter.SubresourceLoad.Evaluation.CPUDuration";
110 113
114 #if defined(GOOGLE_CHROME_BUILD)
115 // Names of navigation chain patterns histogram.
116 const char kMatchesPatternHistogramName[] =
117 "SubresourceFilter.PageLoad.RedirectChainMatchPattern";
118 const char kNavigationChainSize[] =
119 "SubresourceFilter.PageLoad.RedirectChainLength";
120 const char kSubresourceFilterOnlySuffix[] = ".SubresourceFilterOnly";
121 #endif
122
111 // Other histograms. 123 // Other histograms.
112 const char kSubresourceFilterPromptHistogram[] = 124 const char kSubresourceFilterPromptHistogram[] =
113 "SubresourceFilter.Prompt.NumVisibility"; 125 "SubresourceFilter.Prompt.NumVisibility";
114 126
115 // Database manager that allows any URL to be configured as blacklisted for 127 // Human readable representation of expected redirect chain match patterns.
116 // testing. 128 // The explanations for the buckets given for the following redirect chain:
117 class FakeSafeBrowsingDatabaseManager 129 // A->B->C->D, where A is initial URL and D is a final URL.
118 : public safe_browsing::TestSafeBrowsingDatabaseManager { 130 enum RedirectChainMatchPattern {
119 public: 131 EMPTY, // No histograms were recorded.
120 FakeSafeBrowsingDatabaseManager() {} 132 F0M0L1, // D is a Safe Browsing match.
121 133 F0M1L0, // B or C, or both are Safe Browsing matches.
122 void AddBlacklistedURL(const GURL& url, 134 F0M1L1, // B or C, or both and D are Safe Browsing matches.
123 safe_browsing::SBThreatType threat_type) { 135 F1M0L0, // A is Safe Browsing match
124 url_to_threat_type_[url] = threat_type; 136 F1M0L1, // A and D are Safe Browsing matches.
125 } 137 F1M1L0, // B and/or C and A are Safe Browsing matches.
126 138 F1M1L1, // B and/or C and A and D are Safe Browsing matches.
127 protected: 139 NO_REDIRECTS_HIT, // Redirect chain consists of single URL, aka no redirects
128 ~FakeSafeBrowsingDatabaseManager() override {} 140 // has happened, and this URL was a Safe Browsing hit.
129 141 NUM_HIT_PATTERNS,
130 bool CheckBrowseUrl(const GURL& url, Client* client) override {
131 if (!url_to_threat_type_.count(url))
132 return true;
133
134 content::BrowserThread::PostTask(
135 content::BrowserThread::IO, FROM_HERE,
136 base::Bind(&Client::OnCheckBrowseUrlResult, base::Unretained(client),
137 url, url_to_threat_type_[url],
138 safe_browsing::ThreatMetadata()));
139 return false;
140 }
141
142 bool CheckResourceUrl(const GURL& url, Client* client) override {
143 return true;
144 }
145
146 bool IsSupported() const override { return true; }
147 bool ChecksAreAlwaysAsync() const override { return false; }
148 bool CanCheckResourceType(
149 content::ResourceType /* resource_type */) const override {
150 return true;
151 }
152
153 safe_browsing::ThreatSource GetThreatSource() const override {
154 return safe_browsing::ThreatSource::LOCAL_PVER4;
155 }
156
157 bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
158 Client* client) override {
159 return true;
160 }
161
162 private:
163 std::map<GURL, safe_browsing::SBThreatType> url_to_threat_type_;
164
165 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
166 }; 142 };
167 143
168 // UI manager that never actually shows any interstitials, but emulates as if 144 // UI manager that never actually shows any interstitials, but emulates as if
169 // the user chose to proceed through them. 145 // the user chose to proceed through them.
170 class FakeSafeBrowsingUIManager 146 class FakeSafeBrowsingUIManager
171 : public safe_browsing::TestSafeBrowsingUIManager { 147 : public safe_browsing::TestSafeBrowsingUIManager {
172 public: 148 public:
173 FakeSafeBrowsingUIManager() {} 149 FakeSafeBrowsingUIManager() {}
174 150
175 protected: 151 protected:
176 ~FakeSafeBrowsingUIManager() override {} 152 ~FakeSafeBrowsingUIManager() override {}
177 153
178 void DisplayBlockingPage(const UnsafeResource& resource) override { 154 void DisplayBlockingPage(const UnsafeResource& resource) override {
179 resource.callback_thread->PostTask( 155 resource.callback_thread->PostTask(
180 FROM_HERE, base::Bind(resource.callback, true /* proceed */)); 156 FROM_HERE, base::Bind(resource.callback, true /* proceed */));
181 } 157 }
182 158
183 private: 159 private:
184 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingUIManager); 160 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingUIManager);
185 }; 161 };
186 162
187 GURL GetURLWithFragment(const GURL& url, base::StringPiece fragment) { 163 GURL GetURLWithFragment(const GURL& url, base::StringPiece fragment) {
188 GURL::Replacements replacements; 164 GURL::Replacements replacements;
189 replacements.SetRefStr(fragment); 165 replacements.SetRefStr(fragment);
190 return url.ReplaceComponents(replacements); 166 return url.ReplaceComponents(replacements);
191 } 167 }
192 168
193 GURL GetURLWithQuery(const GURL& url, base::StringPiece query) {
194 GURL::Replacements replacements;
195 replacements.SetQueryStr(query);
196 return url.ReplaceComponents(replacements);
197 }
198
199 } // namespace 169 } // namespace
200 170
201 namespace subresource_filter { 171 namespace subresource_filter {
202 172
203 using subresource_filter::testing::ScopedSubresourceFilterFeatureToggle; 173 using subresource_filter::testing::ScopedSubresourceFilterFeatureToggle;
204 using subresource_filter::testing::TestRulesetPublisher; 174 using subresource_filter::testing::TestRulesetPublisher;
205 using subresource_filter::testing::TestRulesetCreator; 175 using subresource_filter::testing::TestRulesetCreator;
206 using subresource_filter::testing::TestRulesetPair; 176 using subresource_filter::testing::TestRulesetPair;
207 using ActivationDecision = 177 using ActivationDecision =
208 ContentSubresourceFilterDriverFactory::ActivationDecision; 178 ContentSubresourceFilterDriverFactory::ActivationDecision;
(...skipping 23 matching lines...) Expand all
232 // called after ChromeBrowserMainParts::PreBrowserStart(), which instantiates 202 // called after ChromeBrowserMainParts::PreBrowserStart(), which instantiates
233 // the RulesetService. 203 // the RulesetService.
234 // 204 //
235 // On the other hand, setting up field trials in this method would be too 205 // On the other hand, setting up field trials in this method would be too
236 // early, as it is called before BrowserMain, which expects no FieldTrialList 206 // early, as it is called before BrowserMain, which expects no FieldTrialList
237 // singleton to exist. There are no other hooks we could use either. 207 // singleton to exist. There are no other hooks we could use either.
238 // 208 //
239 // As a workaround, enable the feature here, then enable the feature once 209 // As a workaround, enable the feature here, then enable the feature once
240 // again + set up the field trials later. 210 // again + set up the field trials later.
241 void SetUpCommandLine(base::CommandLine* command_line) override { 211 void SetUpCommandLine(base::CommandLine* command_line) override {
242 command_line->AppendSwitchASCII(switches::kEnableFeatures, 212 command_line->AppendSwitchASCII(
243 kSafeBrowsingSubresourceFilter.name); 213 switches::kEnableFeatures,
214 base::JoinString(
215 {kSafeBrowsingSubresourceFilter.name, "SafeBrowsingV4OnlyEnabled",
216 kSubresourceFilterSafeBrowsingActivationThrottle.name},
217 ","));
244 } 218 }
245 219
246 void SetUpInProcessBrowserTestFixture() override { 220 void SetUp() override {
247 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); 221 sb_factory_ =
248 test_safe_browsing_factory_.SetTestDatabaseManager( 222 base::MakeUnique<safe_browsing::TestSafeBrowsingServiceFactory>(
249 fake_safe_browsing_database_.get()); 223 safe_browsing::V4FeatureList::V4UsageStatus::V4_ONLY);
250 test_safe_browsing_factory_.SetTestUIManager(new FakeSafeBrowsingUIManager); 224 sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager());
251 safe_browsing::SafeBrowsingService::RegisterFactory( 225 safe_browsing::SafeBrowsingService::RegisterFactory(sb_factory_.get());
252 &test_safe_browsing_factory_); 226
227 safe_browsing::V4Database::RegisterStoreFactoryForTest(
228 base::WrapUnique(new safe_browsing::TestV4StoreFactory()));
229
230 v4_db_factory_ = new safe_browsing::TestV4DatabaseFactory();
231 safe_browsing::V4Database::RegisterDatabaseFactoryForTest(
232 base::WrapUnique(v4_db_factory_));
233
234 v4_get_hash_factory_ =
235 new safe_browsing::TestV4GetHashProtocolManagerFactory();
236 safe_browsing::V4GetHashProtocolManager::RegisterFactory(
237 base::WrapUnique(v4_get_hash_factory_));
238 InProcessBrowserTest::SetUp();
239 }
240
241 void TearDown() override {
242 InProcessBrowserTest::TearDown();
243 // Unregister test factories after InProcessBrowserTest::TearDown
244 // (which destructs SafeBrowsingService).
245 safe_browsing::V4GetHashProtocolManager::RegisterFactory(nullptr);
246 safe_browsing::V4Database::RegisterDatabaseFactoryForTest(nullptr);
247 safe_browsing::V4Database::RegisterStoreFactoryForTest(nullptr);
248 safe_browsing::SafeBrowsingService::RegisterFactory(nullptr);
253 } 249 }
254 250
255 void SetUpOnMainThread() override { 251 void SetUpOnMainThread() override {
256 scoped_feature_toggle_.reset(new ScopedSubresourceFilterFeatureToggle( 252 SetUpActivationFeature();
257 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
258 kActivationScopeActivationList, kActivationListPhishingInterstitial,
259 measure_performance_ ? "1" : "0", "" /* suppress_notifications */,
260 whitelist_site_on_reload_ ? "true" : "false"));
261
262 base::FilePath test_data_dir; 253 base::FilePath test_data_dir;
263 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); 254 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
264 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); 255 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
265 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure"); 256 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure");
266 host_resolver()->AddRule("*", "127.0.0.1"); 257 host_resolver()->AddRule("*", "127.0.0.1");
267 content::SetupCrossSiteRedirector(embedded_test_server()); 258 content::SetupCrossSiteRedirector(embedded_test_server());
268 ASSERT_TRUE(embedded_test_server()->Start()); 259 ASSERT_TRUE(embedded_test_server()->Start());
269 } 260 }
270 261
262 virtual void SetUpActivationFeature() {
263 scoped_feature_toggle_.reset(new ScopedSubresourceFilterFeatureToggle(
264 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
265 kActivationScopeActivationList, kActivationListPhishingInterstitial,
266 measure_performance_ ? "1" : "0", "" /* suppress_notifications */,
267 whitelist_site_on_reload_ ? "true" : "false"));
268 }
269
271 GURL GetTestUrl(const std::string& relative_url) { 270 GURL GetTestUrl(const std::string& relative_url) {
272 return embedded_test_server()->base_url().Resolve(relative_url); 271 return embedded_test_server()->base_url().Resolve(relative_url);
273 } 272 }
274 273
274 void MarkUrlAsMatchingListWithId(
275 const GURL& bad_url,
276 const safe_browsing::ListIdentifier& list_id,
277 safe_browsing::ThreatPatternType threat_pattern_type) {
278 safe_browsing::FullHashInfo full_hash_info =
279 GetFullHashInfoWithMetadata(bad_url, list_id, threat_pattern_type);
280 v4_db_factory_->MarkPrefixAsBad(list_id, full_hash_info.full_hash);
281 v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
282 }
283
275 void ConfigureAsPhishingURL(const GURL& url) { 284 void ConfigureAsPhishingURL(const GURL& url) {
276 fake_safe_browsing_database_->AddBlacklistedURL( 285 MarkUrlAsMatchingListWithId(url, safe_browsing::GetUrlSocEngId(),
277 url, safe_browsing::SB_THREAT_TYPE_URL_PHISHING); 286 safe_browsing::ThreatPatternType::NONE);
287 }
288
289 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) {
290 MarkUrlAsMatchingListWithId(url, safe_browsing::GetUrlSubresourceFilterId(),
291 safe_browsing::ThreatPatternType::NONE);
278 } 292 }
279 293
280 content::WebContents* web_contents() { 294 content::WebContents* web_contents() {
281 return browser()->tab_strip_model()->GetActiveWebContents(); 295 return browser()->tab_strip_model()->GetActiveWebContents();
282 } 296 }
283 297
284 content::RenderFrameHost* FindFrameByName(const std::string& name) { 298 content::RenderFrameHost* FindFrameByName(const std::string& name) {
285 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) { 299 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) {
286 if (frame->GetFrameName() == name) 300 if (frame->GetFrameName() == name)
287 return frame; 301 return frame;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 test_ruleset_publisher_.SetRuleset(test_ruleset_pair.unindexed)); 355 test_ruleset_publisher_.SetRuleset(test_ruleset_pair.unindexed));
342 } 356 }
343 357
344 void SetRulesetWithRules(const std::vector<proto::UrlRule>& rules) { 358 void SetRulesetWithRules(const std::vector<proto::UrlRule>& rules) {
345 TestRulesetPair test_ruleset_pair; 359 TestRulesetPair test_ruleset_pair;
346 ruleset_creator_.CreateRulesetWithRules(rules, &test_ruleset_pair); 360 ruleset_creator_.CreateRulesetWithRules(rules, &test_ruleset_pair);
347 ASSERT_NO_FATAL_FAILURE( 361 ASSERT_NO_FATAL_FAILURE(
348 test_ruleset_publisher_.SetRuleset(test_ruleset_pair.unindexed)); 362 test_ruleset_publisher_.SetRuleset(test_ruleset_pair.unindexed));
349 } 363 }
350 364
365 void set_scoped_feature_toggle(ScopedSubresourceFilterFeatureToggle* toggle) {
366 scoped_feature_toggle_.reset(toggle);
367 }
368
351 private: 369 private:
352 TestRulesetCreator ruleset_creator_; 370 TestRulesetCreator ruleset_creator_;
353 safe_browsing::TestSafeBrowsingServiceFactory test_safe_browsing_factory_;
354 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_;
355 371
356 std::unique_ptr<ScopedSubresourceFilterFeatureToggle> scoped_feature_toggle_; 372 std::unique_ptr<ScopedSubresourceFilterFeatureToggle> scoped_feature_toggle_;
357 TestRulesetPublisher test_ruleset_publisher_; 373 TestRulesetPublisher test_ruleset_publisher_;
358 const bool measure_performance_; 374 const bool measure_performance_;
359 const bool whitelist_site_on_reload_; 375 const bool whitelist_site_on_reload_;
360 376
377 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> sb_factory_;
378 // Owned by the V4Database.
379 safe_browsing::TestV4DatabaseFactory* v4_db_factory_;
380 // Owned by the V4GetHashProtocolManager.
381 safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_;
382
361 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTestImpl); 383 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTestImpl);
362 }; 384 };
363 385
364 class SubresourceFilterBrowserTest : public SubresourceFilterBrowserTestImpl { 386 class SubresourceFilterBrowserTest : public SubresourceFilterBrowserTestImpl {
365 public: 387 public:
366 SubresourceFilterBrowserTest() 388 SubresourceFilterBrowserTest()
367 : SubresourceFilterBrowserTestImpl(false, false) {} 389 : SubresourceFilterBrowserTestImpl(false, false) {}
368 }; 390 };
369 391
370 // TODO(pkalinnikov): It should be possible to have only one fixture, i.e., 392 // TODO(pkalinnikov): It should be possible to have only one fixture, i.e.,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 browser()->tab_strip_model()->GetActiveWebContents(), 444 browser()->tab_strip_model()->GetActiveWebContents(),
423 base::StringPrintf("connectWebSocket('%s');", url.spec().c_str()), 445 base::StringPrintf("connectWebSocket('%s');", url.spec().c_str()),
424 &websocket_connection_succeeded)); 446 &websocket_connection_succeeded));
425 EXPECT_EQ(expect_connection_success, websocket_connection_succeeded); 447 EXPECT_EQ(expect_connection_success, websocket_connection_succeeded);
426 } 448 }
427 449
428 private: 450 private:
429 std::unique_ptr<net::SpawnedTestServer> websocket_test_server_; 451 std::unique_ptr<net::SpawnedTestServer> websocket_test_server_;
430 }; 452 };
431 453
454 #if defined(GOOGLE_CHROME_BUILD)
455 class SubresourceFilterListBrowserTest
456 : public SubresourceFilterBrowserTestImpl {
457 public:
458 SubresourceFilterListBrowserTest()
459 : SubresourceFilterBrowserTestImpl(false, false) {}
460
461 void SetUpActivationFeature() override {
462 set_scoped_feature_toggle(new ScopedSubresourceFilterFeatureToggle(
463 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
464 kActivationScopeActivationList, kActivationListSubresourceFilter, "0",
465 "" /* suppress_notifications */, "false"));
466 }
467 };
468 #endif
469
432 // Tests ----------------------------------------------------------------------- 470 // Tests -----------------------------------------------------------------------
433 471
434 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, MainFrameActivation) { 472 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, MainFrameActivation) {
435 GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html")); 473 GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html"));
436 ConfigureAsPhishingURL(url); 474 ConfigureAsPhishingURL(url);
437 ASSERT_NO_FATAL_FAILURE(SetRulesetToDisallowURLsWithPathSuffix( 475 ASSERT_NO_FATAL_FAILURE(SetRulesetToDisallowURLsWithPathSuffix(
438 "suffix-that-does-not-match-anything")); 476 "suffix-that-does-not-match-anything"));
439 ui_test_utils::NavigateToURL(browser(), url); 477 ui_test_utils::NavigateToURL(browser(), url);
440 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); 478 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
441 479
442 ASSERT_NO_FATAL_FAILURE( 480 ASSERT_NO_FATAL_FAILURE(
443 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); 481 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
444 ui_test_utils::NavigateToURL(browser(), url); 482 ui_test_utils::NavigateToURL(browser(), url);
445 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); 483 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
446 484
447 // The main frame document should never be filtered. 485 // The main frame document should never be filtered.
448 SetRulesetToDisallowURLsWithPathSuffix("frame_with_included_script.html"); 486 SetRulesetToDisallowURLsWithPathSuffix("frame_with_included_script.html");
449 ui_test_utils::NavigateToURL(browser(), url); 487 ui_test_utils::NavigateToURL(browser(), url);
450 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); 488 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
451 } 489 }
452 490
491 #if defined(GOOGLE_CHROME_BUILD)
492 IN_PROC_BROWSER_TEST_F(SubresourceFilterListBrowserTest, MainFrameActivation) {
493 GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html"));
494 ConfigureAsSubresourceFilterOnlyURL(url);
495 ASSERT_NO_FATAL_FAILURE(SetRulesetToDisallowURLsWithPathSuffix(
496 "suffix-that-does-not-match-anything"));
497 ui_test_utils::NavigateToURL(browser(), url);
498 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
499
500 ASSERT_NO_FATAL_FAILURE(
501 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
502 ui_test_utils::NavigateToURL(browser(), url);
503 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
504
505 // The main frame document should never be filtered.
506 SetRulesetToDisallowURLsWithPathSuffix("frame_with_included_script.html");
507 ui_test_utils::NavigateToURL(browser(), url);
508 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
509 }
510 #endif
511
453 // There should be no document-level de-/reactivation happening on the renderer 512 // There should be no document-level de-/reactivation happening on the renderer
454 // side as a result of an in-page navigation. 513 // side as a result of an in-page navigation.
455 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, 514 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest,
456 DocumentActivationOutlivesSamePageNavigation) { 515 DocumentActivationOutlivesSamePageNavigation) {
457 GURL url(GetTestUrl("subresource_filter/frame_with_delayed_script.html")); 516 GURL url(GetTestUrl("subresource_filter/frame_with_delayed_script.html"));
458 ConfigureAsPhishingURL(url); 517 ConfigureAsPhishingURL(url);
459 ASSERT_NO_FATAL_FAILURE( 518 ASSERT_NO_FATAL_FAILURE(
460 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); 519 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
461 ui_test_utils::NavigateToURL(browser(), url); 520 ui_test_utils::NavigateToURL(browser(), url);
462 521
(...skipping 19 matching lines...) Expand all
482 const std::vector<const char*> kSubframeNames{"one", "two", "three"}; 541 const std::vector<const char*> kSubframeNames{"one", "two", "three"};
483 const std::vector<bool> kExpectScriptInFrameToLoad{false, true, false}; 542 const std::vector<bool> kExpectScriptInFrameToLoad{false, true, false};
484 ASSERT_NO_FATAL_FAILURE(ExpectParsedScriptElementLoadedStatusInFrames( 543 ASSERT_NO_FATAL_FAILURE(ExpectParsedScriptElementLoadedStatusInFrames(
485 kSubframeNames, kExpectScriptInFrameToLoad)); 544 kSubframeNames, kExpectScriptInFrameToLoad));
486 545
487 tester.ExpectBucketCount(kSubresourceFilterPromptHistogram, true, 1); 546 tester.ExpectBucketCount(kSubresourceFilterPromptHistogram, true, 1);
488 } 547 }
489 548
490 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, 549 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest,
491 HistoryNavigationActivation) { 550 HistoryNavigationActivation) {
492 GURL url_without_activation(GetTestUrl(kTestFrameSetPath)); 551 GURL url_with_activation(GetTestUrl(kTestFrameSetPath));
493 GURL url_with_activation( 552 GURL url_without_activation(
494 GetURLWithQuery(url_without_activation, "activation")); 553 embedded_test_server()->GetURL("a.com", kTestFrameSetPath));
495 ConfigureAsPhishingURL(url_with_activation); 554 ConfigureAsPhishingURL(url_with_activation);
496 ASSERT_NO_FATAL_FAILURE( 555 ASSERT_NO_FATAL_FAILURE(
497 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); 556 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
498 557
499 const std::vector<const char*> kSubframeNames{"one", "two", "three"}; 558 const std::vector<const char*> kSubframeNames{"one", "two", "three"};
500 const std::vector<bool> kExpectScriptInFrameToLoadWithoutActivation{ 559 const std::vector<bool> kExpectScriptInFrameToLoadWithoutActivation{
501 true, true, true}; 560 true, true, true};
502 const std::vector<bool> kExpectScriptInFrameToLoadWithActivation{false, true, 561 const std::vector<bool> kExpectScriptInFrameToLoadWithActivation{false, true,
503 false}; 562 false};
504 563
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 internal::kHistogramSubresourceFilterActivationDecision, 1180 internal::kHistogramSubresourceFilterActivationDecision,
1122 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1); 1181 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1);
1123 1182
1124 tester.ExpectTotalCount( 1183 tester.ExpectTotalCount(
1125 internal::kHistogramSubresourceFilterActivationDecisionReload, 1); 1184 internal::kHistogramSubresourceFilterActivationDecisionReload, 1);
1126 tester.ExpectBucketCount( 1185 tester.ExpectBucketCount(
1127 internal::kHistogramSubresourceFilterActivationDecisionReload, 1186 internal::kHistogramSubresourceFilterActivationDecisionReload,
1128 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1); 1187 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1);
1129 } 1188 }
1130 1189
1190 #if defined(GOOGLE_CHROME_BUILD)
1191 // This test is only enabled when GOOGLE_CHROME_BUILD is true because the store
1192 // that this test uses is only populated on GOOGLE_CHROME_BUILD builds.
1193 IN_PROC_BROWSER_TEST_F(
1194 SubresourceFilterBrowserTest,
1195 ExpectRedirectPatternHistogramsAreRecordedForSubresourceFilterOnlyMatch) {
1196 ASSERT_NO_FATAL_FAILURE(SetRulesetToDisallowURLsWithPathSuffix(
1197 "suffix-that-does-not-match-anything"));
1198
1199 GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html"));
1200 ConfigureAsSubresourceFilterOnlyURL(url);
1201
1202 base::HistogramTester tester;
1203 ui_test_utils::NavigateToURL(browser(), url);
1204
1205 EXPECT_THAT(tester.GetAllSamples(std::string(kMatchesPatternHistogramName) +
1206 std::string(kSubresourceFilterOnlySuffix)),
1207 ::testing::ElementsAre(base::Bucket(NO_REDIRECTS_HIT, 1)));
1208 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) +
1209 std::string(kSubresourceFilterOnlySuffix)),
1210 ::testing::ElementsAre(base::Bucket(1, 1)));
1211 }
1212
1213 IN_PROC_BROWSER_TEST_F(
1214 SubresourceFilterBrowserTest,
1215 ExpectRedirectPatternHistogramsAreRecordedForSubresourceFilterOnlyRedirectMa tch) {
1216 ASSERT_NO_FATAL_FAILURE(
1217 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
1218 const std::string initial_host("a.com");
1219 const std::string redirected_host("b.com");
1220
1221 GURL redirect_url(embedded_test_server()->GetURL(
1222 redirected_host, "/subresource_filter/frame_with_included_script.html"));
1223 GURL url(embedded_test_server()->GetURL(
1224 initial_host, "/server-redirect?" + redirect_url.spec()));
1225
1226 ConfigureAsSubresourceFilterOnlyURL(url.GetOrigin());
1227 base::HistogramTester tester;
1228 ui_test_utils::NavigateToURL(browser(), url);
1229 EXPECT_THAT(tester.GetAllSamples(std::string(kMatchesPatternHistogramName) +
1230 std::string(kSubresourceFilterOnlySuffix)),
1231 ::testing::IsEmpty());
1232
1233 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) +
1234 std::string(kSubresourceFilterOnlySuffix)),
1235 ::testing::IsEmpty());
1236 }
1237 #endif
1238
1131 } // namespace subresource_filter 1239 } // namespace subresource_filter
OLDNEW
« no previous file with comments | « chrome/browser/subresource_filter/navigation_throttle_util.cc ('k') | components/safe_browsing_db/v4_database.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698