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

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

Issue 2622983003: Implement embargo in PermissionDecisionAutoBlocker (Closed)
Patch Set: Create separate keys for different embargo types. 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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/test/scoped_feature_list.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
11 #include "chrome/common/chrome_features.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
14 #include "components/safe_browsing_db/test_database_manager.h"
10 #include "content/public/browser/permission_type.h" 15 #include "content/public/browser/permission_type.h"
11 16
12 namespace { 17 namespace {
13 18
14 bool FilterGoogle(const GURL& url) { 19 bool FilterGoogle(const GURL& url) {
15 return url == "https://www.google.com/"; 20 return url == "https://www.google.com/";
16 } 21 }
17 22
18 bool FilterAll(const GURL& url) { 23 bool FilterAll(const GURL& url) {
19 return true; 24 return true;
20 } 25 }
21 26
27 void AutoBlockerCallback(bool expected, bool result) {
28 EXPECT_EQ(expected, result);
29 }
30
22 } // namespace 31 } // namespace
23 32
24 class PermissionDecisionAutoBlockerUnitTest 33 class PermissionDecisionAutoBlockerUnitTest
25 : public ChromeRenderViewHostTestHarness { 34 : public ChromeRenderViewHostTestHarness {
26 protected: 35 protected:
27 int GetDismissalCount(const GURL& url, 36 int GetDismissalCount(const GURL& url,
28 content::PermissionType permission) { 37 content::PermissionType permission) {
29 return PermissionDecisionAutoBlocker::GetDismissCount( 38 return PermissionDecisionAutoBlocker::GetDismissCount(
30 url, permission, profile()); 39 url, permission, profile());
31 } 40 }
32 41
33 int GetIgnoreCount(const GURL& url, content::PermissionType permission) { 42 int GetIgnoreCount(const GURL& url, content::PermissionType permission) {
34 return PermissionDecisionAutoBlocker::GetIgnoreCount( 43 return PermissionDecisionAutoBlocker::GetIgnoreCount(
35 url, permission, profile()); 44 url, permission, profile());
36 } 45 }
37 46
38 int RecordDismiss(const GURL& url, content::PermissionType permission) { 47 int RecordDismiss(const GURL& url, content::PermissionType permission) {
39 return PermissionDecisionAutoBlocker::RecordDismiss(url, permission, 48 return PermissionDecisionAutoBlocker::RecordDismiss(url, permission,
40 profile()); 49 profile());
41 } 50 }
42 51
43 int RecordIgnore(const GURL& url, content::PermissionType permission) { 52 int RecordIgnore(const GURL& url, content::PermissionType permission) {
44 return PermissionDecisionAutoBlocker::RecordIgnore(url, permission, 53 return PermissionDecisionAutoBlocker::RecordIgnore(url, permission,
45 profile()); 54 profile());
46 } 55 }
56
57 void PlaceUnderEmbargo(content::PermissionType permission,
58 const GURL& url,
59 HostContentSettingsMap* map,
60 base::Time current_time) {
61 PermissionDecisionAutoBlocker::PlaceUnderEmbargoForTest(permission, url,
62 map, current_time);
63 }
64
65 bool GetEmbargoStatus(content::PermissionType permission,
66 const GURL& url,
67 HostContentSettingsMap* map) {
68 return PermissionDecisionAutoBlocker::GetEmbargoStatusForTest(permission,
69 url, map);
70 }
71
72 void UpdateEmbargoedStatus(content::PermissionType permission,
73 const GURL& url,
74 base::Time current_time,
75 bool expected_result) {
76 PermissionDecisionAutoBlocker::UpdateEmbargoedStatus(
77 nullptr, permission, url, nullptr, 2000 /* timeout in ms */, profile(),
78 current_time, base::Bind(&AutoBlockerCallback, expected_result));
79 }
47 }; 80 };
48 81
49 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { 82 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) {
50 GURL url1("https://www.google.com"); 83 GURL url1("https://www.google.com");
51 GURL url2("https://www.example.com"); 84 GURL url2("https://www.example.com");
52 85
53 // Record some dismissals. 86 // Record some dismissals.
54 EXPECT_EQ(1, RecordDismiss(url1, content::PermissionType::GEOLOCATION)); 87 EXPECT_EQ(1, RecordDismiss(url1, content::PermissionType::GEOLOCATION));
55 EXPECT_EQ(2, RecordDismiss(url1, content::PermissionType::GEOLOCATION)); 88 EXPECT_EQ(2, RecordDismiss(url1, content::PermissionType::GEOLOCATION));
56 EXPECT_EQ(3, RecordDismiss(url1, content::PermissionType::GEOLOCATION)); 89 EXPECT_EQ(3, RecordDismiss(url1, content::PermissionType::GEOLOCATION));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION)); 126 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION));
94 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS)); 127 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS));
95 EXPECT_EQ(0, GetDismissalCount(url2, content::PermissionType::GEOLOCATION)); 128 EXPECT_EQ(0, GetDismissalCount(url2, content::PermissionType::GEOLOCATION));
96 129
97 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::GEOLOCATION)); 130 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::GEOLOCATION));
98 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::NOTIFICATIONS)); 131 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::NOTIFICATIONS));
99 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION)); 132 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION));
100 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::DURABLE_STORAGE)); 133 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::DURABLE_STORAGE));
101 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::MIDI_SYSEX)); 134 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::MIDI_SYSEX));
102 } 135 }
136
137 // Check that IsUnderEmbargo returns the correct value when the embargo status
138 // is set and expires.
139 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) {
140 GURL url("https://www.google.com");
141 auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
142 base::Time time_now = base::Time::Now();
143 PlaceUnderEmbargo(content::PermissionType::GEOLOCATION, url, map, time_now);
144
145 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo(
146 content::PermissionType::GEOLOCATION, profile(), url, time_now));
147
148 // Check that the origin is not under embargo for another permission.
149 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo(
150 content::PermissionType::NOTIFICATIONS, profile(), url, time_now));
151
152 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo(
153 content::PermissionType::GEOLOCATION, profile(), url,
154 time_now + base::TimeDelta::FromDays(5)));
155
156 // Check embargo is lifted on expiry day. A small offset after the exact
157 // embargo expiration date has been added to account for any precision errors
158 // when removing the date stored as a double from the permission dictionary.
159 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo(
160 content::PermissionType::GEOLOCATION, profile(), url,
161 time_now + base::TimeDelta::FromHours(7 * 24 + 1)));
162
163 // Check embargo is lifted well after the expiry day.
164 EXPECT_FALSE(PermissionDecisionAutoBlocker::IsUnderEmbargo(
165 content::PermissionType::GEOLOCATION, profile(), url,
166 time_now + base::TimeDelta::FromDays(8)));
167
168 // Place under embargo again.
169 time_now = base::Time::Now();
170 PlaceUnderEmbargo(content::PermissionType::NOTIFICATIONS, url, map, time_now);
171
172 EXPECT_TRUE(PermissionDecisionAutoBlocker::IsUnderEmbargo(
173 content::PermissionType::NOTIFICATIONS, profile(), url, time_now));
174 }
175
176 // Check that the embargo status is correctly recorded in PlaceUnderEmbargo.
177 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdatingEmbargoStatus) {
raymes 2017/01/18 03:15:20 Does this test anything more than the above test?
meredithl 2017/01/18 08:28:16 Dom and I thought you were looking for a way just
raymes 2017/01/18 23:35:00 I'm not sure I understand completely, but I think
meredithl 2017/01/19 02:11:43 Done (removed).
178 GURL url("https://www.google.com");
179 auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
180
181 EXPECT_FALSE(
182 GetEmbargoStatus(content::PermissionType::GEOLOCATION, url, map));
183
184 PlaceUnderEmbargo(content::PermissionType::GEOLOCATION, url, map,
185 base::Time::Now());
186
187 EXPECT_TRUE(GetEmbargoStatus(content::PermissionType::GEOLOCATION, url, map));
188 }
189
190 // Tests the alternating pattern of the block on multiple dismiss behaviour. On
191 // N prior dismissals, the N+1th request will cause the origin to be embargoed
192 // for the requested permission and automatically blocked. Each time the embargo
193 // is lifted, the site gets another chance to request the permission, but if it
194 // is again dismissed it is placed under embargo again and the request
195 // automatically blocked for that period.
196 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargo) {
197 GURL url("https://www.google.com");
198 base::Time time_now = base::Time::Now();
199 // Enable the autoblocking feature, which is disabled by default.
200 base::test::ScopedFeatureList feature_list;
201 feature_list.InitAndEnableFeature(features::kBlockPromptsIfDismissedOften);
202
203 EXPECT_TRUE(
204 base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften));
205
206 // Record some dismisses.
207 EXPECT_EQ(1, RecordDismiss(url, content::PermissionType::GEOLOCATION));
208 EXPECT_EQ(2, RecordDismiss(url, content::PermissionType::GEOLOCATION));
209
210 // A request with < 3 prior dismisses should not be automatically blocked.
211 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url, time_now,
212 false);
raymes 2017/01/18 03:15:20 nit: document booleans
meredithl 2017/01/18 08:28:16 Done.
213
214 // After the 3rd dismiss subsequent permission requests should be autoblocked.
215 EXPECT_EQ(3, RecordDismiss(url, content::PermissionType::GEOLOCATION));
216 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url, time_now,
217 true);
218
219 // Accelerate time forward, check that the embargo status is lifted and the
220 // request won't be automatically blocked.
221 time_now += base::TimeDelta::FromDays(8);
222 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url, time_now,
223 false);
224
225 // Record another dismiss, subsequent requests should be autoblocked again.
226 EXPECT_EQ(4, RecordDismiss(url, content::PermissionType::GEOLOCATION));
227 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url, time_now,
228 true);
229
230 // Accelerate time again, check embargo is lifted and another permission
231 // request is let through.
232 time_now += base::TimeDelta::FromDays(8);
233 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url, time_now,
234 false);
235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698