OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <memory> | 5 #include <memory> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/feature_list.h" | 8 #include "base/feature_list.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/metrics/field_trial.h" | |
13 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
14 #include "base/test/scoped_feature_list.h" | 13 #include "base/test/scoped_feature_list.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
16 #include "chrome/browser/after_startup_task_utils.h" | 15 #include "chrome/browser/after_startup_task_utils.h" |
17 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 16 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
18 #include "chrome/browser/infobars/infobar_service.h" | 17 #include "chrome/browser/infobars/infobar_service.h" |
19 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | 18 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
20 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 19 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
21 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" | 20 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" |
22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
23 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
24 #include "components/prefs/testing_pref_service.h" | 23 #include "components/prefs/testing_pref_service.h" |
25 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 24 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
26 #include "components/subresource_filter/content/browser/content_ruleset_service.
h" | 25 #include "components/subresource_filter/content/browser/content_ruleset_service.
h" |
27 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" | 26 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" |
28 #include "components/subresource_filter/content/browser/fake_safe_browsing_datab
ase_manager.h" | 27 #include "components/subresource_filter/content/browser/fake_safe_browsing_datab
ase_manager.h" |
29 #include "components/subresource_filter/core/browser/ruleset_service.h" | 28 #include "components/subresource_filter/core/browser/ruleset_service.h" |
30 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" | 29 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
31 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" | 30 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
32 #include "components/subresource_filter/core/common/test_ruleset_creator.h" | 31 #include "components/subresource_filter/core/common/test_ruleset_creator.h" |
33 #include "content/public/browser/navigation_throttle.h" | 32 #include "content/public/browser/navigation_throttle.h" |
34 #include "content/public/test/navigation_simulator.h" | 33 #include "content/public/test/navigation_simulator.h" |
35 #include "content/public/test/test_renderer_host.h" | 34 #include "content/public/test/test_renderer_host.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
37 #include "url/gurl.h" | 36 #include "url/gurl.h" |
38 | 37 |
| 38 namespace { |
| 39 using subresource_filter::testing::ScopedSubresourceFilterConfigurator; |
| 40 } // namespace |
| 41 |
39 // End to end unit test harness of (most of) the browser process portions of the | 42 // End to end unit test harness of (most of) the browser process portions of the |
40 // subresource filtering code. | 43 // subresource filtering code. |
41 class SubresourceFilterTest : public ChromeRenderViewHostTestHarness { | 44 class SubresourceFilterTest : public ChromeRenderViewHostTestHarness { |
42 public: | 45 public: |
43 SubresourceFilterTest() : field_trial_list_(nullptr) {} | 46 SubresourceFilterTest() {} |
44 ~SubresourceFilterTest() override {} | 47 ~SubresourceFilterTest() override {} |
45 | 48 |
46 // ChromeRenderViewHostTestHarness: | 49 // ChromeRenderViewHostTestHarness: |
47 void SetUp() override { | 50 void SetUp() override { |
48 ChromeRenderViewHostTestHarness::SetUp(); | 51 ChromeRenderViewHostTestHarness::SetUp(); |
49 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); | 52 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); |
50 | 53 |
51 // Ensure correct features. | 54 // Ensure correct features. |
52 scoped_feature_list_.InitFromCommandLine("SafeBrowsingV4OnlyEnabled", ""); | 55 scoped_feature_list_.InitFromCommandLine("SafeBrowsingV4OnlyEnabled", ""); |
53 scoped_feature_toggle_.reset( | 56 scoped_configuration_.ResetConfiguration(subresource_filter::Configuration( |
54 new subresource_filter::testing::ScopedSubresourceFilterFeatureToggle( | 57 subresource_filter::ActivationLevel::ENABLED, |
55 base::FeatureList::OVERRIDE_ENABLE_FEATURE, | 58 subresource_filter::ActivationScope::ACTIVATION_LIST, |
56 subresource_filter::kActivationLevelEnabled, | 59 subresource_filter::ActivationList::SUBRESOURCE_FILTER)); |
57 subresource_filter::kActivationScopeActivationList, | 60 |
58 subresource_filter::kActivationListSubresourceFilter)); | |
59 NavigateAndCommit(GURL("https://example.first")); | 61 NavigateAndCommit(GURL("https://example.first")); |
60 | 62 |
61 // Set up safe browsing service with the fake database manager. | 63 // Set up safe browsing service with the fake database manager. |
62 // | 64 // |
63 // TODO(csharrison): This is a bit ugly. See if the instructions in | 65 // TODO(csharrison): This is a bit ugly. See if the instructions in |
64 // test_safe_browsing_service.h can be adapted to be used in unit tests. | 66 // test_safe_browsing_service.h can be adapted to be used in unit tests. |
65 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; | 67 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; |
66 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); | 68 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); |
67 sb_service_factory.SetTestDatabaseManager( | 69 sb_service_factory.SetTestDatabaseManager( |
68 fake_safe_browsing_database_.get()); | 70 fake_safe_browsing_database_.get()); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 150 |
149 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { | 151 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { |
150 fake_safe_browsing_database_->AddBlacklistedUrl( | 152 fake_safe_browsing_database_->AddBlacklistedUrl( |
151 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); | 153 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); |
152 } | 154 } |
153 | 155 |
154 ChromeSubresourceFilterClient* client() { return client_; } | 156 ChromeSubresourceFilterClient* client() { return client_; } |
155 | 157 |
156 private: | 158 private: |
157 base::ScopedTempDir ruleset_service_dir_; | 159 base::ScopedTempDir ruleset_service_dir_; |
158 base::FieldTrialList field_trial_list_; | 160 TestingPrefServiceSimple pref_service_; |
159 base::test::ScopedFeatureList scoped_feature_list_; | 161 base::test::ScopedFeatureList scoped_feature_list_; |
160 std::unique_ptr< | 162 ScopedSubresourceFilterConfigurator scoped_configuration_; |
161 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle> | |
162 scoped_feature_toggle_; | |
163 TestingPrefServiceSimple pref_service_; | |
164 | 163 |
165 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; | 164 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; |
166 | 165 |
167 ChromeSubresourceFilterClient* client_ = nullptr; | 166 ChromeSubresourceFilterClient* client_ = nullptr; |
168 | 167 |
169 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterTest); | 168 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterTest); |
170 }; | 169 }; |
171 | 170 |
172 TEST_F(SubresourceFilterTest, SimpleAllowedLoad) { | 171 TEST_F(SubresourceFilterTest, SimpleAllowedLoad) { |
173 GURL url("https://example.test"); | 172 GURL url("https://example.test"); |
174 SimulateNavigateAndCommit(url, main_rfh()); | 173 SimulateNavigateAndCommit(url, main_rfh()); |
175 EXPECT_TRUE(CreateAndNavigateDisallowedSubframe(main_rfh())); | 174 EXPECT_TRUE(CreateAndNavigateDisallowedSubframe(main_rfh())); |
176 EXPECT_FALSE(client()->did_show_ui_for_navigation()); | 175 EXPECT_FALSE(client()->did_show_ui_for_navigation()); |
177 } | 176 } |
178 | 177 |
179 TEST_F(SubresourceFilterTest, SimpleDisallowedLoad) { | 178 TEST_F(SubresourceFilterTest, SimpleDisallowedLoad) { |
180 GURL url("https://example.test"); | 179 GURL url("https://example.test"); |
181 ConfigureAsSubresourceFilterOnlyURL(url); | 180 ConfigureAsSubresourceFilterOnlyURL(url); |
182 SimulateNavigateAndCommit(url, main_rfh()); | 181 SimulateNavigateAndCommit(url, main_rfh()); |
183 EXPECT_FALSE(CreateAndNavigateDisallowedSubframe(main_rfh())); | 182 EXPECT_FALSE(CreateAndNavigateDisallowedSubframe(main_rfh())); |
184 EXPECT_TRUE(client()->did_show_ui_for_navigation()); | 183 EXPECT_TRUE(client()->did_show_ui_for_navigation()); |
185 } | 184 } |
OLD | NEW |