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

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

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

Powered by Google App Engine
This is Rietveld 408576698