OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <memory> |
| 6 #include <utility> |
| 7 |
| 8 #include "base/feature_list.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/test/scoped_feature_list.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/after_startup_task_utils.h" |
| 17 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 18 #include "chrome/browser/infobars/infobar_service.h" |
| 19 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 20 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| 21 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" |
| 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 23 #include "chrome/test/base/testing_browser_process.h" |
| 24 #include "components/prefs/testing_pref_service.h" |
| 25 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 26 #include "components/subresource_filter/content/browser/content_ruleset_service.
h" |
| 27 #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" |
| 29 #include "components/subresource_filter/core/browser/ruleset_service.h" |
| 30 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
| 31 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
| 32 #include "components/subresource_filter/core/common/test_ruleset_creator.h" |
| 33 #include "content/public/browser/navigation_throttle.h" |
| 34 #include "content/public/test/navigation_simulator.h" |
| 35 #include "content/public/test/test_renderer_host.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" |
| 37 #include "url/gurl.h" |
| 38 |
| 39 // End to end unit test harness of (most of) the browser process portions of the |
| 40 // subresource filtering code. |
| 41 class SubresourceFilterTest : public ChromeRenderViewHostTestHarness { |
| 42 public: |
| 43 SubresourceFilterTest() : field_trial_list_(nullptr) {} |
| 44 ~SubresourceFilterTest() override {} |
| 45 |
| 46 // ChromeRenderViewHostTestHarness: |
| 47 void SetUp() override { |
| 48 ChromeRenderViewHostTestHarness::SetUp(); |
| 49 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); |
| 50 |
| 51 // Ensure correct features. |
| 52 scoped_feature_list_.InitFromCommandLine("SafeBrowsingV4OnlyEnabled", ""); |
| 53 scoped_feature_toggle_.reset( |
| 54 new subresource_filter::testing::ScopedSubresourceFilterFeatureToggle( |
| 55 base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 56 subresource_filter::kActivationLevelEnabled, |
| 57 subresource_filter::kActivationScopeActivationList, |
| 58 subresource_filter::kActivationListSubresourceFilter)); |
| 59 NavigateAndCommit(GURL("https://example.first")); |
| 60 |
| 61 // Set up safe browsing service with the fake database manager. |
| 62 // |
| 63 // 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. |
| 65 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; |
| 66 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); |
| 67 sb_service_factory.SetTestDatabaseManager( |
| 68 fake_safe_browsing_database_.get()); |
| 69 safe_browsing::SafeBrowsingService::RegisterFactory(&sb_service_factory); |
| 70 auto* safe_browsing_service = |
| 71 sb_service_factory.CreateSafeBrowsingService(); |
| 72 safe_browsing::SafeBrowsingService::RegisterFactory(nullptr); |
| 73 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( |
| 74 safe_browsing_service); |
| 75 g_browser_process->safe_browsing_service()->Initialize(); |
| 76 |
| 77 // Set up the ruleset service. |
| 78 ASSERT_TRUE(ruleset_service_dir_.CreateUniqueTempDir()); |
| 79 subresource_filter::IndexedRulesetVersion::RegisterPrefs( |
| 80 pref_service_.registry()); |
| 81 auto content_service = |
| 82 base::MakeUnique<subresource_filter::ContentRulesetService>( |
| 83 base::ThreadTaskRunnerHandle::Get()); |
| 84 auto ruleset_service = base::MakeUnique<subresource_filter::RulesetService>( |
| 85 &pref_service_, base::ThreadTaskRunnerHandle::Get(), |
| 86 content_service.get(), ruleset_service_dir_.GetPath()); |
| 87 content_service->set_ruleset_service(std::move(ruleset_service)); |
| 88 TestingBrowserProcess::GetGlobal()->SetRulesetService( |
| 89 std::move(content_service)); |
| 90 |
| 91 // Publish the test ruleset. |
| 92 subresource_filter::testing::TestRulesetCreator ruleset_creator; |
| 93 subresource_filter::testing::TestRulesetPair test_ruleset_pair; |
| 94 ruleset_creator.CreateRulesetToDisallowURLsWithPathSuffix( |
| 95 "disallowed.html", &test_ruleset_pair); |
| 96 subresource_filter::testing::TestRulesetPublisher test_ruleset_publisher; |
| 97 ASSERT_NO_FATAL_FAILURE( |
| 98 test_ruleset_publisher.SetRuleset(test_ruleset_pair.unindexed)); |
| 99 |
| 100 // Set up the tab helpers. |
| 101 InfoBarService::CreateForWebContents(web_contents()); |
| 102 TabSpecificContentSettings::CreateForWebContents(web_contents()); |
| 103 |
| 104 std::unique_ptr<ChromeSubresourceFilterClient> subresource_filter_client( |
| 105 new ChromeSubresourceFilterClient(web_contents())); |
| 106 client_ = subresource_filter_client.get(); |
| 107 subresource_filter::ContentSubresourceFilterDriverFactory:: |
| 108 CreateForWebContents(web_contents(), |
| 109 std::move(subresource_filter_client)); |
| 110 base::RunLoop().RunUntilIdle(); |
| 111 } |
| 112 |
| 113 void TearDown() override { |
| 114 fake_safe_browsing_database_ = nullptr; |
| 115 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); |
| 116 |
| 117 // Must explicitly set these to null and pump the run loop to ensure that |
| 118 // all cleanup related to these classes actually happens. |
| 119 TestingBrowserProcess::GetGlobal()->SetRulesetService(nullptr); |
| 120 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); |
| 121 base::RunLoop().RunUntilIdle(); |
| 122 |
| 123 ChromeRenderViewHostTestHarness::TearDown(); |
| 124 } |
| 125 |
| 126 // Will return nullptr if the navigation fails. |
| 127 content::RenderFrameHost* SimulateNavigateAndCommit( |
| 128 const GURL& url, |
| 129 content::RenderFrameHost* rfh) { |
| 130 auto simulator = |
| 131 content::NavigationSimulator::CreateRendererInitiated(url, rfh); |
| 132 simulator->Commit(); |
| 133 return simulator->GetLastThrottleCheckResult() == |
| 134 content::NavigationThrottle::PROCEED |
| 135 ? simulator->GetFinalRenderFrameHost() |
| 136 : nullptr; |
| 137 } |
| 138 |
| 139 // Returns the frame host the navigation commit in, or nullptr if it did not |
| 140 // succeed. |
| 141 content::RenderFrameHost* CreateAndNavigateDisallowedSubframe( |
| 142 content::RenderFrameHost* parent) { |
| 143 auto* subframe = |
| 144 content::RenderFrameHostTester::For(parent)->AppendChild("subframe"); |
| 145 return SimulateNavigateAndCommit( |
| 146 GURL("https://example.test/disallowed.html"), subframe); |
| 147 } |
| 148 |
| 149 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { |
| 150 fake_safe_browsing_database_->AddBlacklistedUrl( |
| 151 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); |
| 152 } |
| 153 |
| 154 ChromeSubresourceFilterClient* client() { return client_; } |
| 155 |
| 156 private: |
| 157 base::ScopedTempDir ruleset_service_dir_; |
| 158 base::FieldTrialList field_trial_list_; |
| 159 base::test::ScopedFeatureList scoped_feature_list_; |
| 160 std::unique_ptr< |
| 161 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle> |
| 162 scoped_feature_toggle_; |
| 163 TestingPrefServiceSimple pref_service_; |
| 164 |
| 165 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; |
| 166 |
| 167 ChromeSubresourceFilterClient* client_ = nullptr; |
| 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterTest); |
| 170 }; |
| 171 |
| 172 TEST_F(SubresourceFilterTest, SimpleAllowedLoad) { |
| 173 GURL url("https://example.test"); |
| 174 SimulateNavigateAndCommit(url, main_rfh()); |
| 175 EXPECT_TRUE(CreateAndNavigateDisallowedSubframe(main_rfh())); |
| 176 EXPECT_FALSE(client()->did_show_ui_for_navigation()); |
| 177 } |
| 178 |
| 179 TEST_F(SubresourceFilterTest, SimpleDisallowedLoad) { |
| 180 GURL url("https://example.test"); |
| 181 ConfigureAsSubresourceFilterOnlyURL(url); |
| 182 SimulateNavigateAndCommit(url, main_rfh()); |
| 183 EXPECT_FALSE(CreateAndNavigateDisallowedSubframe(main_rfh())); |
| 184 EXPECT_TRUE(client()->did_show_ui_for_navigation()); |
| 185 } |
OLD | NEW |