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

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

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

Powered by Google App Engine
This is Rietveld 408576698