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" | |
engedy
2017/04/20 12:34:33
Nice, I didn't find a single missing or superfluou
Charlie Harrison
2017/04/20 15:55:20
If only this were automated :)
| |
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 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; | |
63 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); | |
64 sb_service_factory.SetTestDatabaseManager( | |
65 fake_safe_browsing_database_.get()); | |
66 safe_browsing::SafeBrowsingService::RegisterFactory(&sb_service_factory); | |
67 auto* safe_browsing_service = | |
engedy
2017/04/20 12:34:33
This seems a bit hacky. :) Is there any nicer solu
Charlie Harrison
2017/04/20 15:55:20
This is cribbed from notification_image_reporter_u
engedy
2017/04/20 16:36:53
Acknowledged.
| |
68 sb_service_factory.CreateSafeBrowsingService(); | |
69 safe_browsing::SafeBrowsingService::RegisterFactory(nullptr); | |
70 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( | |
71 safe_browsing_service); | |
72 g_browser_process->safe_browsing_service()->Initialize(); | |
73 | |
74 // Set up the ruleset service. | |
75 ASSERT_TRUE(ruleset_service_dir_.CreateUniqueTempDir()); | |
76 subresource_filter::IndexedRulesetVersion::RegisterPrefs( | |
77 pref_service_.registry()); | |
78 auto content_service = | |
79 base::MakeUnique<subresource_filter::ContentRulesetService>( | |
80 base::ThreadTaskRunnerHandle::Get()); | |
81 auto ruleset_service = base::MakeUnique<subresource_filter::RulesetService>( | |
82 &pref_service_, base::ThreadTaskRunnerHandle::Get(), | |
83 content_service.get(), ruleset_service_dir_.GetPath()); | |
84 content_service->set_ruleset_service(std::move(ruleset_service)); | |
85 TestingBrowserProcess::GetGlobal()->SetRulesetService( | |
86 std::move(content_service)); | |
87 | |
88 // Publish the test ruleset. | |
89 subresource_filter::testing::TestRulesetCreator ruleset_creator; | |
90 subresource_filter::testing::TestRulesetPair test_ruleset_pair; | |
91 ruleset_creator.CreateRulesetToDisallowURLsWithPathSuffix( | |
92 "disallowed.html", &test_ruleset_pair); | |
93 subresource_filter::testing::TestRulesetPublisher test_ruleset_publisher; | |
94 ASSERT_NO_FATAL_FAILURE( | |
95 test_ruleset_publisher.SetRuleset(test_ruleset_pair.unindexed)); | |
96 | |
97 // Set up the tab helpers. | |
98 InfoBarService::CreateForWebContents(web_contents()); | |
99 TabSpecificContentSettings::CreateForWebContents(web_contents()); | |
100 | |
101 std::unique_ptr<ChromeSubresourceFilterClient> subresource_filter_client( | |
102 new ChromeSubresourceFilterClient(web_contents())); | |
103 client_ = subresource_filter_client.get(); | |
104 subresource_filter::ContentSubresourceFilterDriverFactory:: | |
105 CreateForWebContents(web_contents(), | |
106 std::move(subresource_filter_client)); | |
107 base::RunLoop().RunUntilIdle(); | |
108 } | |
109 | |
110 void TearDown() override { | |
111 fake_safe_browsing_database_ = nullptr; | |
112 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); | |
engedy
2017/04/20 12:34:33
nit: ChromeUnitTestSuiteInitializer::OnTestEnd des
Charlie Harrison
2017/04/20 15:55:20
The Shutdown() and run loop is needed, to ensure t
engedy
2017/04/20 16:36:53
Acknowledged.
Charlie Harrison
2017/04/20 19:32:48
Actually, they are needed. We need to reset the ob
engedy
2017/04/20 19:35:20
Ack, sorry for the noise.
| |
113 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); | |
114 TestingBrowserProcess::GetGlobal()->SetRulesetService(nullptr); | |
115 base::RunLoop().RunUntilIdle(); | |
116 ChromeRenderViewHostTestHarness::TearDown(); | |
117 } | |
118 | |
119 // Will return nullptr if the navigation fails. | |
120 content::RenderFrameHost* SimulateNavigateAndCommit( | |
121 const GURL& url, | |
122 content::RenderFrameHost* rfh) { | |
123 auto simulator = | |
124 content::NavigationSimulator::CreateRendererInitiated(url, rfh); | |
125 simulator->Commit(); | |
126 return simulator->GetLastThrottleCheckResult() == | |
127 content::NavigationThrottle::PROCEED | |
128 ? simulator->GetFinalRenderFrameHost() | |
129 : nullptr; | |
130 } | |
131 | |
132 // Returns the frame host the navigation commit in, or nullptr if it did not | |
133 // succeed. | |
134 content::RenderFrameHost* CreateAndNavigateDisallowedSubframe( | |
135 content::RenderFrameHost* parent) { | |
136 auto* subframe = | |
137 content::RenderFrameHostTester::For(parent)->AppendChild("subframe"); | |
138 return SimulateNavigateAndCommit( | |
139 GURL("https://example.test/disallowed.html"), subframe); | |
140 } | |
141 | |
142 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { | |
143 fake_safe_browsing_database_->AddBlacklistedUrl( | |
144 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); | |
145 } | |
146 | |
147 ChromeSubresourceFilterClient* client() { return client_; } | |
148 | |
149 private: | |
150 base::ScopedTempDir ruleset_service_dir_; | |
151 base::FieldTrialList field_trial_list_; | |
152 std::unique_ptr< | |
153 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle> | |
154 scoped_feature_toggle_; | |
155 base::test::ScopedFeatureList scoped_feature_list_; | |
engedy
2017/04/20 12:34:33
Let's move this up by one, so that they get destro
Charlie Harrison
2017/04/20 15:55:20
Done.
| |
156 TestingPrefServiceSimple pref_service_; | |
157 | |
158 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; | |
159 | |
160 ChromeSubresourceFilterClient* client_ = nullptr; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterTest); | |
163 }; | |
164 | |
165 TEST_F(SubresourceFilterTest, SimpleAllowedLoadLoad) { | |
166 GURL url("https://example.test"); | |
167 SimulateNavigateAndCommit(url, main_rfh()); | |
168 EXPECT_TRUE(CreateAndNavigateDisallowedSubframe(main_rfh())); | |
169 EXPECT_FALSE(client()->shown_for_navigation()); | |
170 } | |
171 | |
172 TEST_F(SubresourceFilterTest, SimpleDisallowedLoadLoad) { | |
173 GURL url("https://example.test"); | |
174 ConfigureAsSubresourceFilterOnlyURL(url); | |
175 SimulateNavigateAndCommit(url, main_rfh()); | |
176 EXPECT_FALSE(CreateAndNavigateDisallowedSubframe(main_rfh())); | |
177 EXPECT_TRUE(client()->shown_for_navigation()); | |
178 } | |
OLD | NEW |