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

Side by Side Diff: chrome/browser/permissions/permission_decision_auto_blocker_unittest.cc

Issue 2640033006: Convert AutoBlocker static class to KeyedService. (Closed)
Patch Set: Git surgery. Created 3 years, 11 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 "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) {
83 GURL url1("https://www.google.com"); 142 GURL url1("https://www.google.com");
84 GURL url2("https://www.example.com"); 143 GURL url2("https://www.example.com");
85 base::test::ScopedFeatureList feature_list;
86 feature_list.InitAndEnableFeature(features::kBlockPromptsIfDismissedOften);
87 144
88 // Record some dismissals. 145 // Record some dismissals.
89 EXPECT_FALSE(RecordDismissAndEmbargo( 146 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
90 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); 147 url1, content::PermissionType::GEOLOCATION));
91 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); 148 EXPECT_EQ(1, autoblocker()->GetDismissCount(
92 149 url1, content::PermissionType::GEOLOCATION));
93 EXPECT_FALSE(RecordDismissAndEmbargo( 150
94 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); 151 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
95 EXPECT_EQ(2, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); 152 url1, content::PermissionType::GEOLOCATION));
96 153 EXPECT_EQ(2, autoblocker()->GetDismissCount(
97 EXPECT_TRUE(RecordDismissAndEmbargo( 154 url1, content::PermissionType::GEOLOCATION));
98 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); 155
99 EXPECT_EQ(3, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); 156 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
100 157 url1, content::PermissionType::GEOLOCATION));
101 EXPECT_FALSE(RecordDismissAndEmbargo( 158 EXPECT_EQ(3, autoblocker()->GetDismissCount(
102 url2, content::PermissionType::GEOLOCATION, base::Time::Now())); 159 url1, content::PermissionType::GEOLOCATION));
103 EXPECT_EQ(1, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); 160
104 161 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
105 EXPECT_FALSE(RecordDismissAndEmbargo( 162 url2, content::PermissionType::GEOLOCATION));
106 url1, content::PermissionType::NOTIFICATIONS, base::Time::Now())); 163 EXPECT_EQ(1, autoblocker()->GetDismissCount(
107 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); 164 url2, content::PermissionType::GEOLOCATION));
165
166 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
167 url1, content::PermissionType::NOTIFICATIONS));
168 EXPECT_EQ(1, autoblocker()->GetDismissCount(
169 url1, content::PermissionType::NOTIFICATIONS));
108 170
109 // Record some ignores. 171 // Record some ignores.
110 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::MIDI_SYSEX)); 172 EXPECT_EQ(1, autoblocker()->RecordIgnore(
111 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::DURABLE_STORAGE)); 173 url1, content::PermissionType::MIDI_SYSEX));
112 EXPECT_EQ(1, RecordIgnore(url2, content::PermissionType::GEOLOCATION)); 174 EXPECT_EQ(1, autoblocker()->RecordIgnore(
113 EXPECT_EQ(2, RecordIgnore(url2, content::PermissionType::GEOLOCATION)); 175 url1, content::PermissionType::DURABLE_STORAGE));
114 176 EXPECT_EQ(1, autoblocker()->RecordIgnore(
115 PermissionDecisionAutoBlocker::RemoveCountsByUrl(profile(), 177 url2, content::PermissionType::GEOLOCATION));
116 base::Bind(&FilterGoogle)); 178 EXPECT_EQ(2, autoblocker()->RecordIgnore(
179 url2, content::PermissionType::GEOLOCATION));
180
181 autoblocker()->RemoveCountsByUrl(base::Bind(&FilterGoogle));
117 182
118 // Expect that url1's actions are gone, but url2's remain. 183 // Expect that url1's actions are gone, but url2's remain.
119 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); 184 EXPECT_EQ(0, autoblocker()->GetDismissCount(
120 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); 185 url1, content::PermissionType::GEOLOCATION));
121 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::MIDI_SYSEX)); 186 EXPECT_EQ(0, autoblocker()->GetDismissCount(
122 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::DURABLE_STORAGE)); 187 url1, content::PermissionType::NOTIFICATIONS));
123 188 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
124 EXPECT_EQ(1, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); 189 url1, content::PermissionType::MIDI_SYSEX));
125 EXPECT_EQ(2, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION)); 190 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
191 url1, content::PermissionType::DURABLE_STORAGE));
192
193 EXPECT_EQ(1, autoblocker()->GetDismissCount(
194 url2, content::PermissionType::GEOLOCATION));
195 EXPECT_EQ(2, autoblocker()->GetIgnoreCount(
196 url2, content::PermissionType::GEOLOCATION));
126 197
127 // Add some more actions. 198 // Add some more actions.
128 EXPECT_FALSE(RecordDismissAndEmbargo( 199 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
129 url1, content::PermissionType::GEOLOCATION, base::Time::Now())); 200 url1, content::PermissionType::GEOLOCATION));
130 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); 201 EXPECT_EQ(1, autoblocker()->GetDismissCount(
131 202 url1, content::PermissionType::GEOLOCATION));
132 EXPECT_FALSE(RecordDismissAndEmbargo( 203
133 url1, content::PermissionType::NOTIFICATIONS, base::Time::Now())); 204 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
134 EXPECT_EQ(1, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); 205 url1, content::PermissionType::NOTIFICATIONS));
135 206 EXPECT_EQ(1, autoblocker()->GetDismissCount(
136 EXPECT_FALSE(RecordDismissAndEmbargo( 207 url1, content::PermissionType::NOTIFICATIONS));
137 url2, content::PermissionType::GEOLOCATION, base::Time::Now())); 208
138 EXPECT_EQ(2, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); 209 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
139 210 url2, content::PermissionType::GEOLOCATION));
140 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::GEOLOCATION)); 211 EXPECT_EQ(2, autoblocker()->GetDismissCount(
141 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::NOTIFICATIONS)); 212 url2, content::PermissionType::GEOLOCATION));
142 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::DURABLE_STORAGE)); 213
143 EXPECT_EQ(1, RecordIgnore(url2, content::PermissionType::MIDI_SYSEX)); 214 EXPECT_EQ(1, autoblocker()->RecordIgnore(
215 url1, content::PermissionType::GEOLOCATION));
216 EXPECT_EQ(1, autoblocker()->RecordIgnore(
217 url1, content::PermissionType::NOTIFICATIONS));
218 EXPECT_EQ(1, autoblocker()->RecordIgnore(
219 url1, content::PermissionType::DURABLE_STORAGE));
220 EXPECT_EQ(1, autoblocker()->RecordIgnore(
221 url2, content::PermissionType::MIDI_SYSEX));
144 222
145 // Remove everything and expect that it's all gone. 223 // Remove everything and expect that it's all gone.
146 PermissionDecisionAutoBlocker::RemoveCountsByUrl(profile(), 224 autoblocker()->RemoveCountsByUrl(base::Bind(&FilterAll));
147 base::Bind(&FilterAll)); 225
148 226 EXPECT_EQ(0, autoblocker()->GetDismissCount(
149 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); 227 url1, content::PermissionType::GEOLOCATION));
150 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); 228 EXPECT_EQ(0, autoblocker()->GetDismissCount(
151 EXPECT_EQ(0, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); 229 url1, content::PermissionType::NOTIFICATIONS));
152 230 EXPECT_EQ(0, autoblocker()->GetDismissCount(
153 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::GEOLOCATION)); 231 url2, content::PermissionType::GEOLOCATION));
154 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::NOTIFICATIONS)); 232
155 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION)); 233 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
156 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::DURABLE_STORAGE)); 234 url1, content::PermissionType::GEOLOCATION));
157 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::MIDI_SYSEX)); 235 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
236 url1, content::PermissionType::NOTIFICATIONS));
237 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
238 url2, content::PermissionType::GEOLOCATION));
239 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
240 url2, content::PermissionType::DURABLE_STORAGE));
241 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
242 url2, content::PermissionType::MIDI_SYSEX));
243 }
244
245 // Test that an origin that has been blacklisted for a permission is embargoed.
246 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) {
247 GURL url("https://www.google.com");
248
249 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
250 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
251 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
252 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
253 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
254 2000 /* timeout in ms */);
255
256 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
257 EXPECT_TRUE(last_embargoed_status());
158 } 258 }
159 259
160 // Check that IsUnderEmbargo returns the correct value when the embargo is set 260 // Check that IsUnderEmbargo returns the correct value when the embargo is set
161 // and expires. 261 // and expires.
162 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { 262 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) {
163 GURL url("https://www.google.com"); 263 GURL url("https://www.google.com");
164 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); 264 clock()->SetNow(base::Time::Now());
165 base::Time time_now = base::Time::Now(); 265
166 base::test::ScopedFeatureList feature_list; 266 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url);
167 267 EXPECT_TRUE(
168 feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); 268 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
169 EXPECT_TRUE(base::FeatureList::IsEnabled(features::kPermissionsBlacklist)); 269
170 270 // Check that the origin is not under embargo for a different permission.
171 // Manually place url under embargo and confirm embargo status. 271 EXPECT_FALSE(autoblocker()->IsUnderEmbargo(
172 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url, map, 272 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 273
181 // Confirm embargo status during the embargo period. 274 // Confirm embargo status during the embargo period.
182 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 275 clock()->Advance(base::TimeDelta::FromDays(5));
183 content::PermissionType::GEOLOCATION, profile(), url, 276 EXPECT_TRUE(
184 time_now + base::TimeDelta::FromDays(5))); 277 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
185 278
186 // Check embargo is lifted on expiry day. A small offset after the exact 279 // 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 280 // 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. 281 // when removing the date stored as a double from the permission dictionary.
189 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 282 clock()->Advance(base::TimeDelta::FromHours(3 * 24 + 1));
190 content::PermissionType::GEOLOCATION, profile(), url, 283 EXPECT_FALSE(
191 time_now + base::TimeDelta::FromHours(7 * 24 + 1))); 284 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
192 285
193 // Check embargo is lifted well after the expiry day. 286 // Check embargo is lifted well after the expiry day.
194 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 287 clock()->Advance(base::TimeDelta::FromDays(1));
195 content::PermissionType::GEOLOCATION, profile(), url, 288 EXPECT_FALSE(
196 time_now + base::TimeDelta::FromDays(8))); 289 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
197 290
198 // Place under embargo again and verify the embargo status. 291 // Place under embargo again and verify the embargo status.
199 time_now = base::Time::Now(); 292 PlaceUnderBlacklistEmbargo(content::PermissionType::NOTIFICATIONS, url);
200 PlaceUnderBlacklistEmbargo(content::PermissionType::NOTIFICATIONS, url, map, 293 clock()->Advance(base::TimeDelta::FromDays(1));
201 time_now); 294 EXPECT_TRUE(autoblocker()->IsUnderEmbargo(
202 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 295 content::PermissionType::NOTIFICATIONS, url));
203 content::PermissionType::NOTIFICATIONS, profile(), url, time_now));
204 } 296 }
205 297
206 // Tests the alternating pattern of the block on multiple dismiss behaviour. On 298 // 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 299 // 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 300 // 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 301 // chance to request the permission, but if it is again dismissed it is placed
210 // under embargo again and its permission requests blocked. 302 // under embargo again and its permission requests blocked.
211 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargo) { 303 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) {
212 GURL url("https://www.google.com"); 304 GURL url("https://www.google.com");
213 base::Time time_now = base::Time::Now(); 305 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 306
221 // Record some dismisses. 307 // Record some dismisses.
222 EXPECT_FALSE(RecordDismissAndEmbargo( 308 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
223 url, content::PermissionType::GEOLOCATION, time_now)); 309 url, content::PermissionType::GEOLOCATION));
224 EXPECT_FALSE(RecordDismissAndEmbargo( 310 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
225 url, content::PermissionType::GEOLOCATION, time_now)); 311 url, content::PermissionType::GEOLOCATION));
226 312
227 // A request with < 3 prior dismisses should not be automatically blocked. 313 // A request with < 3 prior dismisses should not be automatically blocked.
228 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 314 EXPECT_FALSE(
229 content::PermissionType::GEOLOCATION, profile(), url, time_now)); 315 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
230 316
231 // After the 3rd dismiss subsequent permission requests should be autoblocked. 317 // After the 3rd dismiss subsequent permission requests should be autoblocked.
232 EXPECT_TRUE(RecordDismissAndEmbargo(url, content::PermissionType::GEOLOCATION, 318 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
233 time_now)); 319 url, content::PermissionType::GEOLOCATION));
234 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 320 EXPECT_TRUE(
235 content::PermissionType::GEOLOCATION, profile(), url, time_now)); 321 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
236 322
237 // Accelerate time forward, check that the embargo status is lifted and the 323 // Accelerate time forward, check that the embargo status is lifted and the
238 // request won't be automatically blocked. 324 // request won't be automatically blocked.
239 time_now += base::TimeDelta::FromDays(8); 325 clock()->Advance(base::TimeDelta::FromDays(8));
240 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 326 EXPECT_FALSE(
241 content::PermissionType::GEOLOCATION, profile(), url, time_now)); 327 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
242 328
243 // Record another dismiss, subsequent requests should be autoblocked again. 329 // Record another dismiss, subsequent requests should be autoblocked again.
244 EXPECT_TRUE(RecordDismissAndEmbargo(url, content::PermissionType::GEOLOCATION, 330 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
245 time_now)); 331 url, content::PermissionType::GEOLOCATION));
246 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 332 EXPECT_TRUE(
247 content::PermissionType::GEOLOCATION, profile(), url, time_now)); 333 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
248 334
249 // Accelerate time again, check embargo is lifted and another permission 335 // Accelerate time again, check embargo is lifted and another permission
250 // request is let through. 336 // request is let through.
251 time_now += base::TimeDelta::FromDays(8); 337 clock()->Advance(base::TimeDelta::FromDays(8));
252 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 338 EXPECT_FALSE(
253 content::PermissionType::GEOLOCATION, profile(), url, time_now)); 339 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
254 } 340 }
255 341
256 // Test the logic for a combination of blacklisting and dismissal embargo. 342 // Test the logic for a combination of blacklisting and dismissal embargo.
257 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { 343 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) {
258 GURL url("https://www.google.com"); 344 GURL url("https://www.google.com");
259 base::Time time_now = base::Time::Now(); 345 clock()->SetNow(base::Time::Now());
260 auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
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 346
271 // Place under blacklist embargo and check the status. 347 // Place under blacklist embargo and check the status.
272 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url, map, 348 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url);
273 base::Time::Now()); 349 clock()->Advance(base::TimeDelta::FromDays(5));
274 350 EXPECT_TRUE(
275 time_now += base::TimeDelta::FromDays(5); 351 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
276 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo(
277 content::PermissionType::GEOLOCATION, profile(), url, time_now));
278 352
279 // Record dismisses to place it under dismissal embargo. 353 // Record dismisses to place it under dismissal embargo.
280 EXPECT_FALSE(RecordDismissAndEmbargo( 354 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
281 url, content::PermissionType::GEOLOCATION, time_now)); 355 url, content::PermissionType::GEOLOCATION));
282 EXPECT_FALSE(RecordDismissAndEmbargo( 356 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
283 url, content::PermissionType::GEOLOCATION, time_now)); 357 url, content::PermissionType::GEOLOCATION));
284 EXPECT_TRUE(RecordDismissAndEmbargo(url, content::PermissionType::GEOLOCATION, 358 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
285 time_now)); 359 url, content::PermissionType::GEOLOCATION));
286 360
287 // Accelerate time to a point where the blacklist embargo should be expired. 361 // Accelerate time to a point where the blacklist embargo should be expired
288 time_now += base::TimeDelta::FromDays(3); 362 // and check that dismissal embargo is still set.
289 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 363 clock()->Advance(base::TimeDelta::FromDays(3));
290 content::PermissionType::GEOLOCATION, profile(), url, 364 EXPECT_TRUE(
291 time_now + base::TimeDelta::FromDays(5))); 365 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
366 }
292 367
293 // Check that dismissal embargo is still set, even though the blacklisting 368 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) {
294 // embargo has expired. 369 GURL url("https://www.google.com");
295 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo( 370 clock()->SetNow(base::Time::Now());
296 content::PermissionType::GEOLOCATION, profile(), url, time_now)); 371
372 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
373 new MockSafeBrowsingDatabaseManager(false /* perform_callback */);
374 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
375 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
376 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
377 0 /* timeout in ms */);
378
379 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
380 EXPECT_FALSE(last_embargoed_status());
381 EXPECT_FALSE(
382 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
383 db_manager->SetPerformCallback(true);
384 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
385 2000 /* timeout in ms */);
386
387 clock()->Advance(base::TimeDelta::FromDays(1));
388 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
389 EXPECT_TRUE(last_embargoed_status());
390
391 clock()->Advance(base::TimeDelta::FromDays(1));
392 EXPECT_TRUE(
393 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
297 } 394 }
OLDNEW
« no previous file with comments | « chrome/browser/permissions/permission_decision_auto_blocker.cc ('k') | chrome/browser/permissions/permission_uma_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698