OLD | NEW |
---|---|
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 "chrome/browser/permissions/permission_decision_auto_blocker.h" | 5 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" |
6 | 6 |
7 #include <map> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/run_loop.h" | |
8 #include "base/test/scoped_feature_list.h" | 11 #include "base/test/scoped_feature_list.h" |
9 #include "base/time/time.h" | 12 #include "base/test/simple_test_clock.h" |
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
14 #include "chrome/browser/permissions/permission_util.h" | |
11 #include "chrome/common/chrome_features.h" | 15 #include "chrome/common/chrome_features.h" |
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
13 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
14 #include "components/safe_browsing_db/test_database_manager.h" | 18 #include "components/safe_browsing_db/test_database_manager.h" |
15 #include "content/public/browser/permission_type.h" | 19 #include "content/public/browser/permission_type.h" |
16 | 20 |
17 namespace { | 21 namespace { |
18 | 22 |
19 bool FilterGoogle(const GURL& url) { | 23 bool FilterGoogle(const GURL& url) { |
20 return url == "https://www.google.com/"; | 24 return url == "https://www.google.com/"; |
21 } | 25 } |
22 | 26 |
23 bool FilterAll(const GURL& url) { | 27 bool FilterAll(const GURL& url) { |
24 return true; | 28 return true; |
25 } | 29 } |
26 | 30 |
27 void AutoBlockerCallback(bool expected, bool result) { | |
28 EXPECT_EQ(expected, result); | |
29 } | |
30 | |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 // TODO(meredithl): Write unit tests to simulate entering Permissions | 33 class MockSafeBrowsingDatabaseManager |
34 // Blacklisting embargo status via the public API. | 34 : public safe_browsing::TestSafeBrowsingDatabaseManager { |
35 public: | |
36 explicit MockSafeBrowsingDatabaseManager(bool perform_callback) | |
37 : perform_callback_(perform_callback) {} | |
38 | |
39 bool CheckApiBlacklistUrl( | |
40 const GURL& url, | |
41 safe_browsing::SafeBrowsingDatabaseManager::Client* client) override { | |
42 if (perform_callback_) { | |
43 safe_browsing::ThreatMetadata metadata; | |
44 const auto& blacklisted_permissions = permissions_blacklist_.find(url); | |
45 if (blacklisted_permissions != permissions_blacklist_.end()) | |
46 metadata.api_permissions = blacklisted_permissions->second; | |
47 client->OnCheckApiBlacklistUrlResult(url, metadata); | |
48 } | |
49 return false; | |
50 } | |
51 | |
52 bool CancelApiCheck(Client* client) override { | |
53 DCHECK(!perform_callback_); | |
54 // Returns true when client check could be stopped. | |
55 return true; | |
56 } | |
57 | |
58 void BlacklistUrlPermissions(const GURL& url, | |
59 const std::set<std::string> permissions) { | |
60 permissions_blacklist_[url] = permissions; | |
61 } | |
62 | |
63 void SetPerformCallback(bool perform_callback) { | |
64 perform_callback_ = perform_callback; | |
65 } | |
66 | |
67 protected: | |
68 ~MockSafeBrowsingDatabaseManager() override {} | |
69 | |
70 private: | |
71 bool perform_callback_; | |
72 std::map<GURL, std::set<std::string>> permissions_blacklist_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); | |
75 }; | |
76 | |
35 class PermissionDecisionAutoBlockerUnitTest | 77 class PermissionDecisionAutoBlockerUnitTest |
36 : public ChromeRenderViewHostTestHarness { | 78 : public ChromeRenderViewHostTestHarness { |
37 protected: | 79 protected: |
38 int GetDismissalCount(const GURL& url, content::PermissionType permission) { | 80 void SetUp() override { |
39 return PermissionDecisionAutoBlocker::GetDismissCount(url, permission, | 81 ChromeRenderViewHostTestHarness::SetUp(); |
40 profile()); | 82 autoblocker_ = PermissionDecisionAutoBlocker::GetForProfile(profile()); |
41 } | 83 feature_list_.InitWithFeatures({features::kBlockPromptsIfDismissedOften, |
42 | 84 features::kPermissionsBlacklist}, |
43 int GetIgnoreCount(const GURL& url, content::PermissionType permission) { | 85 {}); |
44 return PermissionDecisionAutoBlocker::GetIgnoreCount(url, permission, | 86 last_embargoed_status_ = false; |
45 profile()); | 87 std::unique_ptr<base::SimpleTestClock> clock = |
46 } | 88 base::MakeUnique<base::SimpleTestClock>(); |
47 | 89 clock_ = clock.get(); |
48 int RecordDismissAndEmbargo(const GURL& url, | 90 autoblocker_->SetClockForTesting(std::move(clock)); |
49 content::PermissionType permission, | 91 } |
50 base::Time current_time) { | 92 |
51 return PermissionDecisionAutoBlocker::RecordDismissAndEmbargo( | 93 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting( |
52 url, permission, profile(), current_time); | 94 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager, |
53 } | 95 int timeout) { |
54 | 96 autoblocker_->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
55 int RecordIgnore(const GURL& url, content::PermissionType permission) { | 97 timeout); |
56 return PermissionDecisionAutoBlocker::RecordIgnore(url, permission, | |
57 profile()); | |
58 } | 98 } |
59 | 99 |
60 void UpdateEmbargoedStatus(content::PermissionType permission, | 100 void UpdateEmbargoedStatus(content::PermissionType permission, |
61 const GURL& url, | 101 const GURL& url) { |
62 base::Time current_time, | 102 base::RunLoop run_loop; |
63 bool expected_result) { | 103 autoblocker_->UpdateEmbargoedStatus( |
64 PermissionDecisionAutoBlocker::UpdateEmbargoedStatus( | 104 permission, url, nullptr, |
65 nullptr /* db manager */, permission, url, nullptr /* web contents */, | 105 base::Bind(&PermissionDecisionAutoBlockerUnitTest::SetLastEmbargoStatus, |
66 2000 /* timeout in ms */, profile(), current_time, | 106 base::Unretained(this), run_loop.QuitClosure())); |
67 base::Bind(&AutoBlockerCallback, expected_result)); | 107 run_loop.Run(); |
68 } | 108 } |
69 | 109 |
70 // Manually placing an origin, permission pair under embargo for blacklisting. | 110 // Manually placing an (origin, permission) pair under embargo for |
71 // To embargo on dismissals, RecordDismissAndEmbargo can be used. | 111 // blacklisting. To embargo on dismissals, RecordDismissAndEmbargo can be |
112 // used. | |
72 void PlaceUnderBlacklistEmbargo(content::PermissionType permission, | 113 void PlaceUnderBlacklistEmbargo(content::PermissionType permission, |
73 const GURL& url, | 114 const GURL& url) { |
74 HostContentSettingsMap* map, | 115 autoblocker_->PlaceUnderEmbargo( |
75 base::Time current_time) { | 116 permission, url, |
76 PermissionDecisionAutoBlocker::PlaceUnderEmbargo( | |
77 permission, url, map, current_time, | |
78 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey); | 117 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey); |
79 } | 118 } |
119 | |
120 PermissionDecisionAutoBlocker* autoblocker() { return autoblocker_; } | |
121 | |
122 void SetLastEmbargoStatus(base::Closure quit_closure, bool status) { | |
123 last_embargoed_status_ = status; | |
124 if (quit_closure) { | |
125 quit_closure.Run(); | |
126 quit_closure.Reset(); | |
127 } | |
128 } | |
129 | |
130 bool last_embargoed_status() { return last_embargoed_status_; } | |
131 | |
132 base::SimpleTestClock* clock() { return clock_; } | |
133 | |
134 private: | |
135 PermissionDecisionAutoBlocker* autoblocker_; | |
136 base::test::ScopedFeatureList feature_list_; | |
137 base::SimpleTestClock* clock_; | |
138 bool last_embargoed_status_; | |
80 }; | 139 }; |
81 | 140 |
82 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { | 141 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { |
142 PermissionDecisionAutoBlocker* autoblocker_ptr = autoblocker(); | |
raymes
2017/01/24 05:15:17
nit: I would just inline autoblocker_ptr to be aut
meredithl
2017/01/24 23:20:21
Done.
| |
83 GURL url1("https://www.google.com"); | 143 GURL url1("https://www.google.com"); |
84 GURL url2("https://www.example.com"); | 144 GURL url2("https://www.example.com"); |
85 base::test::ScopedFeatureList feature_list; | |
86 feature_list.InitAndEnableFeature(features::kBlockPromptsIfDismissedOften); | |
87 | 145 |
88 // Record some dismissals. | 146 // Record some dismissals. |
89 EXPECT_FALSE(RecordDismissAndEmbargo( | 147 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
90 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); | 148 url1, content::PermissionType::GEOLOCATION)); |
91 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); | 149 EXPECT_EQ(1, autoblocker_ptr->GetDismissCount( |
92 | 150 url1, content::PermissionType::GEOLOCATION)); |
93 EXPECT_FALSE(RecordDismissAndEmbargo( | 151 |
94 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); | 152 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
95 EXPECT_EQ(2, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); | 153 url1, content::PermissionType::GEOLOCATION)); |
96 | 154 EXPECT_EQ(2, autoblocker_ptr->GetDismissCount( |
97 EXPECT_TRUE(RecordDismissAndEmbargo( | 155 url1, content::PermissionType::GEOLOCATION)); |
98 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); | 156 |
99 EXPECT_EQ(3, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); | 157 EXPECT_TRUE(autoblocker_ptr->RecordDismissAndEmbargo( |
100 | 158 url1, content::PermissionType::GEOLOCATION)); |
101 EXPECT_FALSE(RecordDismissAndEmbargo( | 159 EXPECT_EQ(3, autoblocker_ptr->GetDismissCount( |
102 url2, content::PermissionType::GEOLOCATION, base::Time::Now())); | 160 url1, content::PermissionType::GEOLOCATION)); |
103 EXPECT_EQ(1, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); | 161 |
104 | 162 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
105 EXPECT_FALSE(RecordDismissAndEmbargo( | 163 url2, content::PermissionType::GEOLOCATION)); |
106 url1, content::PermissionType::NOTIFICATIONS, base::Time::Now())); | 164 EXPECT_EQ(1, autoblocker_ptr->GetDismissCount( |
107 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); | 165 url2, content::PermissionType::GEOLOCATION)); |
166 | |
167 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( | |
168 url1, content::PermissionType::NOTIFICATIONS)); | |
169 EXPECT_EQ(1, autoblocker_ptr->GetDismissCount( | |
170 url1, content::PermissionType::NOTIFICATIONS)); | |
108 | 171 |
109 // Record some ignores. | 172 // Record some ignores. |
110 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::MIDI_SYSEX)); | 173 EXPECT_EQ(1, autoblocker_ptr->RecordIgnore( |
111 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::DURABLE_STORAGE)); | 174 url1, content::PermissionType::MIDI_SYSEX)); |
112 EXPECT_EQ(1, RecordIgnore(url2, content::PermissionType::GEOLOCATION)); | 175 EXPECT_EQ(1, autoblocker_ptr->RecordIgnore( |
113 EXPECT_EQ(2, RecordIgnore(url2, content::PermissionType::GEOLOCATION)); | 176 url1, content::PermissionType::DURABLE_STORAGE)); |
114 | 177 EXPECT_EQ(1, autoblocker_ptr->RecordIgnore( |
115 PermissionDecisionAutoBlocker::RemoveCountsByUrl(profile(), | 178 url2, content::PermissionType::GEOLOCATION)); |
116 base::Bind(&FilterGoogle)); | 179 EXPECT_EQ(2, autoblocker_ptr->RecordIgnore( |
180 url2, content::PermissionType::GEOLOCATION)); | |
181 | |
182 autoblocker_ptr->RemoveCountsByUrl(base::Bind(&FilterGoogle)); | |
117 | 183 |
118 // Expect that url1's actions are gone, but url2's remain. | 184 // Expect that url1's actions are gone, but url2's remain. |
119 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); | 185 EXPECT_EQ(0, autoblocker_ptr->GetDismissCount( |
120 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); | 186 url1, content::PermissionType::GEOLOCATION)); |
121 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::MIDI_SYSEX)); | 187 EXPECT_EQ(0, autoblocker_ptr->GetDismissCount( |
122 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::DURABLE_STORAGE)); | 188 url1, content::PermissionType::NOTIFICATIONS)); |
123 | 189 EXPECT_EQ(0, autoblocker_ptr->GetIgnoreCount( |
124 EXPECT_EQ(1, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); | 190 url1, content::PermissionType::MIDI_SYSEX)); |
125 EXPECT_EQ(2, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION)); | 191 EXPECT_EQ(0, autoblocker_ptr->GetIgnoreCount( |
192 url1, content::PermissionType::DURABLE_STORAGE)); | |
193 | |
194 EXPECT_EQ(1, autoblocker_ptr->GetDismissCount( | |
195 url2, content::PermissionType::GEOLOCATION)); | |
196 EXPECT_EQ(2, autoblocker_ptr->GetIgnoreCount( | |
197 url2, content::PermissionType::GEOLOCATION)); | |
126 | 198 |
127 // Add some more actions. | 199 // Add some more actions. |
128 EXPECT_FALSE(RecordDismissAndEmbargo( | 200 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
129 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); | 201 url1, content::PermissionType::GEOLOCATION)); |
130 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); | 202 EXPECT_EQ(1, autoblocker_ptr->GetDismissCount( |
131 | 203 url1, content::PermissionType::GEOLOCATION)); |
132 EXPECT_FALSE(RecordDismissAndEmbargo( | 204 |
133 url1, content::PermissionType::NOTIFICATIONS, base::Time::Now())); | 205 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
134 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); | 206 url1, content::PermissionType::NOTIFICATIONS)); |
135 | 207 EXPECT_EQ(1, autoblocker_ptr->GetDismissCount( |
136 EXPECT_FALSE(RecordDismissAndEmbargo( | 208 url1, content::PermissionType::NOTIFICATIONS)); |
137 url2, content::PermissionType::GEOLOCATION, base::Time::Now())); | 209 |
138 EXPECT_EQ(2, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); | 210 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
139 | 211 url2, content::PermissionType::GEOLOCATION)); |
140 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::GEOLOCATION)); | 212 EXPECT_EQ(2, autoblocker_ptr->GetDismissCount( |
141 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::NOTIFICATIONS)); | 213 url2, content::PermissionType::GEOLOCATION)); |
142 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::DURABLE_STORAGE)); | 214 |
143 EXPECT_EQ(1, RecordIgnore(url2, content::PermissionType::MIDI_SYSEX)); | 215 EXPECT_EQ(1, autoblocker_ptr->RecordIgnore( |
216 url1, content::PermissionType::GEOLOCATION)); | |
217 EXPECT_EQ(1, autoblocker_ptr->RecordIgnore( | |
218 url1, content::PermissionType::NOTIFICATIONS)); | |
219 EXPECT_EQ(1, autoblocker_ptr->RecordIgnore( | |
220 url1, content::PermissionType::DURABLE_STORAGE)); | |
221 EXPECT_EQ(1, autoblocker_ptr->RecordIgnore( | |
222 url2, content::PermissionType::MIDI_SYSEX)); | |
144 | 223 |
145 // Remove everything and expect that it's all gone. | 224 // Remove everything and expect that it's all gone. |
146 PermissionDecisionAutoBlocker::RemoveCountsByUrl(profile(), | 225 autoblocker_ptr->RemoveCountsByUrl(base::Bind(&FilterAll)); |
147 base::Bind(&FilterAll)); | 226 |
148 | 227 EXPECT_EQ(0, autoblocker_ptr->GetDismissCount( |
149 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); | 228 url1, content::PermissionType::GEOLOCATION)); |
150 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); | 229 EXPECT_EQ(0, autoblocker_ptr->GetDismissCount( |
151 EXPECT_EQ(0, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); | 230 url1, content::PermissionType::NOTIFICATIONS)); |
152 | 231 EXPECT_EQ(0, autoblocker_ptr->GetDismissCount( |
153 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::GEOLOCATION)); | 232 url2, content::PermissionType::GEOLOCATION)); |
154 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::NOTIFICATIONS)); | 233 |
155 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION)); | 234 EXPECT_EQ(0, autoblocker_ptr->GetIgnoreCount( |
156 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::DURABLE_STORAGE)); | 235 url1, content::PermissionType::GEOLOCATION)); |
157 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::MIDI_SYSEX)); | 236 EXPECT_EQ(0, autoblocker_ptr->GetIgnoreCount( |
237 url1, content::PermissionType::NOTIFICATIONS)); | |
238 EXPECT_EQ(0, autoblocker_ptr->GetIgnoreCount( | |
239 url2, content::PermissionType::GEOLOCATION)); | |
240 EXPECT_EQ(0, autoblocker_ptr->GetIgnoreCount( | |
241 url2, content::PermissionType::DURABLE_STORAGE)); | |
242 EXPECT_EQ(0, autoblocker_ptr->GetIgnoreCount( | |
243 url2, content::PermissionType::MIDI_SYSEX)); | |
244 } | |
245 | |
246 // Test that an origin that has been blacklisted for a permission is embargoed. | |
247 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { | |
248 GURL url("https://www.google.com"); | |
249 | |
250 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
251 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | |
252 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | |
253 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
254 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | |
255 2000 /* timeout in ms */); | |
256 | |
257 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | |
258 EXPECT_TRUE(last_embargoed_status()); | |
158 } | 259 } |
159 | 260 |
160 // Check that IsUnderEmbargo returns the correct value when the embargo is set | 261 // Check that IsUnderEmbargo returns the correct value when the embargo is set |
161 // and expires. | 262 // and expires. |
162 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { | 263 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { |
264 PermissionDecisionAutoBlocker* autoblocker_ptr = autoblocker(); | |
163 GURL url("https://www.google.com"); | 265 GURL url("https://www.google.com"); |
164 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 266 clock()->SetNow(base::Time::Now()); |
165 base::Time time_now = base::Time::Now(); | 267 |
166 base::test::ScopedFeatureList feature_list; | 268 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); |
167 | 269 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( |
168 feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); | 270 content::PermissionType::GEOLOCATION, url)); |
169 EXPECT_TRUE(base::FeatureList::IsEnabled(features::kPermissionsBlacklist)); | 271 |
170 | 272 // Check that the origin is not under embargo for a different permission. |
171 // Manually place url under embargo and confirm embargo status. | 273 EXPECT_FALSE(autoblocker_ptr->IsUnderEmbargo( |
172 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url, map, | 274 content::PermissionType::NOTIFICATIONS, url)); |
173 time_now); | |
174 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | |
175 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | |
176 | |
177 // Check that the origin is not under embargo for another permission. | |
178 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | |
179 content::PermissionType::NOTIFICATIONS, profile(), url, time_now)); | |
180 | 275 |
181 // Confirm embargo status during the embargo period. | 276 // Confirm embargo status during the embargo period. |
182 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 277 clock()->Advance(base::TimeDelta::FromDays(5)); |
183 content::PermissionType::GEOLOCATION, profile(), url, | 278 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( |
184 time_now + base::TimeDelta::FromDays(5))); | 279 content::PermissionType::GEOLOCATION, url)); |
185 | 280 |
186 // Check embargo is lifted on expiry day. A small offset after the exact | 281 // Check embargo is lifted on expiry day. A small offset after the exact |
187 // embargo expiration date has been added to account for any precision errors | 282 // embargo expiration date has been added to account for any precision errors |
188 // when removing the date stored as a double from the permission dictionary. | 283 // when removing the date stored as a double from the permission dictionary. |
189 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 284 clock()->Advance(base::TimeDelta::FromHours(3 * 24 + 1)); |
190 content::PermissionType::GEOLOCATION, profile(), url, | 285 EXPECT_FALSE(autoblocker_ptr->IsUnderEmbargo( |
191 time_now + base::TimeDelta::FromHours(7 * 24 + 1))); | 286 content::PermissionType::GEOLOCATION, url)); |
192 | 287 |
193 // Check embargo is lifted well after the expiry day. | 288 // Check embargo is lifted well after the expiry day. |
194 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 289 clock()->Advance(base::TimeDelta::FromDays(1)); |
195 content::PermissionType::GEOLOCATION, profile(), url, | 290 EXPECT_FALSE(autoblocker_ptr->IsUnderEmbargo( |
196 time_now + base::TimeDelta::FromDays(8))); | 291 content::PermissionType::GEOLOCATION, url)); |
197 | 292 |
198 // Place under embargo again and verify the embargo status. | 293 // Place under embargo again and verify the embargo status. |
199 time_now = base::Time::Now(); | 294 PlaceUnderBlacklistEmbargo(content::PermissionType::NOTIFICATIONS, url); |
200 PlaceUnderBlacklistEmbargo(content::PermissionType::NOTIFICATIONS, url, map, | 295 clock()->Advance(base::TimeDelta::FromDays(1)); |
201 time_now); | 296 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( |
202 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 297 content::PermissionType::NOTIFICATIONS, url)); |
203 content::PermissionType::NOTIFICATIONS, profile(), url, time_now)); | |
204 } | 298 } |
205 | 299 |
206 // Tests the alternating pattern of the block on multiple dismiss behaviour. On | 300 // Tests the alternating pattern of the block on multiple dismiss behaviour. On |
207 // N dismissals, the origin to be embargoed for the requested permission and | 301 // N dismissals, the origin to be embargoed for the requested permission and |
208 // automatically blocked. Each time the embargo is lifted, the site gets another | 302 // automatically blocked. Each time the embargo is lifted, the site gets another |
209 // chance to request the permission, but if it is again dismissed it is placed | 303 // chance to request the permission, but if it is again dismissed it is placed |
210 // under embargo again and its permission requests blocked. | 304 // under embargo again and its permission requests blocked. |
211 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargo) { | 305 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { |
306 PermissionDecisionAutoBlocker* autoblocker_ptr = autoblocker(); | |
212 GURL url("https://www.google.com"); | 307 GURL url("https://www.google.com"); |
213 base::Time time_now = base::Time::Now(); | 308 clock()->SetNow(base::Time::Now()); |
214 // Enable the autoblocking feature, which is disabled by default. | |
215 base::test::ScopedFeatureList feature_list; | |
216 feature_list.InitAndEnableFeature(features::kBlockPromptsIfDismissedOften); | |
217 | |
218 EXPECT_TRUE( | |
219 base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)); | |
220 | 309 |
221 // Record some dismisses. | 310 // Record some dismisses. |
222 EXPECT_FALSE(RecordDismissAndEmbargo( | 311 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
223 url, content::PermissionType::GEOLOCATION, time_now)); | 312 url, content::PermissionType::GEOLOCATION)); |
224 EXPECT_FALSE(RecordDismissAndEmbargo( | 313 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
225 url, content::PermissionType::GEOLOCATION, time_now)); | 314 url, content::PermissionType::GEOLOCATION)); |
226 | 315 |
227 // A request with < 3 prior dismisses should not be automatically blocked. | 316 // A request with < 3 prior dismisses should not be automatically blocked. |
228 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 317 EXPECT_FALSE(autoblocker_ptr->IsUnderEmbargo( |
229 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | 318 content::PermissionType::GEOLOCATION, url)); |
230 | 319 |
231 // After the 3rd dismiss subsequent permission requests should be autoblocked. | 320 // After the 3rd dismiss subsequent permission requests should be autoblocked. |
232 EXPECT_TRUE(RecordDismissAndEmbargo(url, content::PermissionType::GEOLOCATION, | 321 EXPECT_TRUE(autoblocker_ptr->RecordDismissAndEmbargo( |
233 time_now)); | 322 url, content::PermissionType::GEOLOCATION)); |
234 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 323 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( |
235 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | 324 content::PermissionType::GEOLOCATION, url)); |
236 | 325 |
237 // Accelerate time forward, check that the embargo status is lifted and the | 326 // Accelerate time forward, check that the embargo status is lifted and the |
238 // request won't be automatically blocked. | 327 // request won't be automatically blocked. |
239 time_now += base::TimeDelta::FromDays(8); | 328 clock()->Advance(base::TimeDelta::FromDays(8)); |
240 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 329 EXPECT_FALSE(autoblocker_ptr->IsUnderEmbargo( |
241 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | 330 content::PermissionType::GEOLOCATION, url)); |
242 | 331 |
243 // Record another dismiss, subsequent requests should be autoblocked again. | 332 // Record another dismiss, subsequent requests should be autoblocked again. |
244 EXPECT_TRUE(RecordDismissAndEmbargo(url, content::PermissionType::GEOLOCATION, | 333 EXPECT_TRUE(autoblocker_ptr->RecordDismissAndEmbargo( |
245 time_now)); | 334 url, content::PermissionType::GEOLOCATION)); |
246 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 335 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( |
247 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | 336 content::PermissionType::GEOLOCATION, url)); |
248 | 337 |
249 // Accelerate time again, check embargo is lifted and another permission | 338 // Accelerate time again, check embargo is lifted and another permission |
250 // request is let through. | 339 // request is let through. |
251 time_now += base::TimeDelta::FromDays(8); | 340 clock()->Advance(base::TimeDelta::FromDays(8)); |
252 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 341 EXPECT_FALSE(autoblocker_ptr->IsUnderEmbargo( |
253 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | 342 content::PermissionType::GEOLOCATION, url)); |
254 } | 343 } |
255 | 344 |
256 // Test the logic for a combination of blacklisting and dismissal embargo. | 345 // Test the logic for a combination of blacklisting and dismissal embargo. |
257 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { | 346 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { |
258 GURL url("https://www.google.com"); | 347 GURL url("https://www.google.com"); |
259 base::Time time_now = base::Time::Now(); | 348 clock()->SetNow(base::Time::Now()); |
260 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 349 PermissionDecisionAutoBlocker* autoblocker_ptr = autoblocker(); |
261 | |
262 // Enable both dismissals and permissions blacklisting features. | |
263 base::test::ScopedFeatureList feature_list; | |
264 feature_list.InitWithFeatures({features::kBlockPromptsIfDismissedOften, | |
265 features::kPermissionsBlacklist}, | |
266 {}); | |
267 EXPECT_TRUE( | |
268 base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)); | |
269 EXPECT_TRUE(base::FeatureList::IsEnabled(features::kPermissionsBlacklist)); | |
270 | 350 |
271 // Place under blacklist embargo and check the status. | 351 // Place under blacklist embargo and check the status. |
272 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url, map, | 352 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); |
273 base::Time::Now()); | 353 clock()->Advance(base::TimeDelta::FromDays(5)); |
274 | 354 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( |
275 time_now += base::TimeDelta::FromDays(5); | 355 content::PermissionType::GEOLOCATION, url)); |
276 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | |
277 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | |
278 | 356 |
279 // Record dismisses to place it under dismissal embargo. | 357 // Record dismisses to place it under dismissal embargo. |
280 EXPECT_FALSE(RecordDismissAndEmbargo( | 358 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
281 url, content::PermissionType::GEOLOCATION, time_now)); | 359 url, content::PermissionType::GEOLOCATION)); |
282 EXPECT_FALSE(RecordDismissAndEmbargo( | 360 EXPECT_FALSE(autoblocker_ptr->RecordDismissAndEmbargo( |
283 url, content::PermissionType::GEOLOCATION, time_now)); | 361 url, content::PermissionType::GEOLOCATION)); |
284 EXPECT_TRUE(RecordDismissAndEmbargo(url, content::PermissionType::GEOLOCATION, | 362 EXPECT_TRUE(autoblocker_ptr->RecordDismissAndEmbargo( |
285 time_now)); | 363 url, content::PermissionType::GEOLOCATION)); |
286 | 364 |
287 // Accelerate time to a point where the blacklist embargo should be expired. | 365 // Accelerate time to a point where the blacklist embargo should be expired |
288 time_now += base::TimeDelta::FromDays(3); | 366 // and check that dismissal embargo is still set. |
289 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 367 clock()->Advance(base::TimeDelta::FromDays(3)); |
290 content::PermissionType::GEOLOCATION, profile(), url, | 368 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( |
291 time_now + base::TimeDelta::FromDays(5))); | 369 content::PermissionType::GEOLOCATION, url)); |
370 } | |
292 | 371 |
293 // Check that dismissal embargo is still set, even though the blacklisting | 372 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { |
294 // embargo has expired. | 373 PermissionDecisionAutoBlocker* autoblocker_ptr = autoblocker(); |
295 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( | 374 GURL url("https://www.google.com"); |
296 content::PermissionType::GEOLOCATION, profile(), url, time_now)); | 375 clock()->SetNow(base::Time::Now()); |
376 | |
377 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
378 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | |
379 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | |
380 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
381 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | |
382 0 /* timeout in ms */); | |
383 | |
384 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | |
385 EXPECT_FALSE(last_embargoed_status()); | |
386 EXPECT_FALSE(autoblocker_ptr->IsUnderEmbargo( | |
387 content::PermissionType::GEOLOCATION, url)); | |
388 db_manager->SetPerformCallback(true); | |
389 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | |
390 2000 /* timeout in ms */); | |
391 | |
392 clock()->Advance(base::TimeDelta::FromDays(1)); | |
393 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | |
394 EXPECT_TRUE(last_embargoed_status()); | |
395 | |
396 clock()->Advance(base::TimeDelta::FromDays(1)); | |
397 EXPECT_TRUE(autoblocker_ptr->IsUnderEmbargo( | |
398 content::PermissionType::GEOLOCATION, url)); | |
297 } | 399 } |
OLD | NEW |