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

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

Issue 2651163002: Add UMA for autoblocking and embargoing. (Closed)
Patch Set: Review comments, add histogram checks Created 3 years, 10 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> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/test/histogram_tester.h"
11 #include "base/test/scoped_feature_list.h" 12 #include "base/test/scoped_feature_list.h"
12 #include "base/test/simple_test_clock.h" 13 #include "base/test/simple_test_clock.h"
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
15 #include "chrome/browser/permissions/permission_uma_util.h"
14 #include "chrome/browser/permissions/permission_util.h" 16 #include "chrome/browser/permissions/permission_util.h"
15 #include "chrome/common/chrome_features.h" 17 #include "chrome/common/chrome_features.h"
16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
17 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
18 #include "components/safe_browsing_db/test_database_manager.h" 20 #include "components/safe_browsing_db/test_database_manager.h"
19 #include "content/public/browser/permission_type.h" 21 #include "content/public/browser/permission_type.h"
20 22
21 namespace { 23 namespace {
22 24
23 bool FilterGoogle(const GURL& url) { 25 bool FilterGoogle(const GURL& url) {
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 url2, content::PermissionType::GEOLOCATION)); 240 url2, content::PermissionType::GEOLOCATION));
239 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 241 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
240 url2, content::PermissionType::DURABLE_STORAGE)); 242 url2, content::PermissionType::DURABLE_STORAGE));
241 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 243 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
242 url2, content::PermissionType::MIDI_SYSEX)); 244 url2, content::PermissionType::MIDI_SYSEX));
243 } 245 }
244 246
245 // Test that an origin that has been blacklisted for a permission is embargoed. 247 // Test that an origin that has been blacklisted for a permission is embargoed.
246 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { 248 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) {
247 GURL url("https://www.google.com"); 249 GURL url("https://www.google.com");
250 base::HistogramTester histograms;
248 251
249 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 252 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
250 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); 253 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
251 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 254 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
252 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 255 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
253 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 256 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
254 2000 /* timeout in ms */); 257 2000 /* timeout in ms */);
255
256 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 258 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
257 EXPECT_TRUE(last_embargoed_status()); 259 EXPECT_TRUE(last_embargoed_status());
260 histograms.ExpectTotalCount("Permissions.AutoBlocker.EmbargoReason", 1);
dominickn 2017/01/30 04:55:26 You can collapse the TotalCount and BucketCount in
meredithl 2017/01/30 05:37:40 Done.
261 histograms.ExpectBucketCount(
262 "Permissions.AutoBlocker.EmbargoReason",
263 PermissionEmbargoReason::PERMISSIONS_BLACKLISTING, 1);
264 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse",
265 1);
266 histograms.ExpectBucketCount("Permissions.AutoBlocker.SafeBrowsingResponse",
267 SafeBrowsingResponse::BLACKLISTED, 1);
258 } 268 }
259 269
260 // Check that IsUnderEmbargo returns the correct value when the embargo is set 270 // Check that IsUnderEmbargo returns the correct value when the embargo is set
261 // and expires. 271 // and expires.
262 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { 272 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) {
263 GURL url("https://www.google.com"); 273 GURL url("https://www.google.com");
264 clock()->SetNow(base::Time::Now()); 274 clock()->SetNow(base::Time::Now());
265 275
266 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); 276 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url);
267 EXPECT_TRUE( 277 EXPECT_TRUE(
(...skipping 28 matching lines...) Expand all
296 } 306 }
297 307
298 // Tests the alternating pattern of the block on multiple dismiss behaviour. On 308 // Tests the alternating pattern of the block on multiple dismiss behaviour. On
299 // N dismissals, the origin to be embargoed for the requested permission and 309 // N dismissals, the origin to be embargoed for the requested permission and
300 // automatically blocked. Each time the embargo is lifted, the site gets another 310 // automatically blocked. Each time the embargo is lifted, the site gets another
301 // chance to request the permission, but if it is again dismissed it is placed 311 // chance to request the permission, but if it is again dismissed it is placed
302 // under embargo again and its permission requests blocked. 312 // under embargo again and its permission requests blocked.
303 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { 313 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) {
304 GURL url("https://www.google.com"); 314 GURL url("https://www.google.com");
305 clock()->SetNow(base::Time::Now()); 315 clock()->SetNow(base::Time::Now());
316 base::HistogramTester histograms;
306 317
307 // Record some dismisses. 318 // Record some dismisses.
308 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 319 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
309 url, content::PermissionType::GEOLOCATION)); 320 url, content::PermissionType::GEOLOCATION));
310 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 321 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
311 url, content::PermissionType::GEOLOCATION)); 322 url, content::PermissionType::GEOLOCATION));
312 323
313 // A request with < 3 prior dismisses should not be automatically blocked. 324 // A request with < 3 prior dismisses should not be automatically blocked.
314 EXPECT_FALSE( 325 EXPECT_FALSE(
315 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 326 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
316 327
317 // After the 3rd dismiss subsequent permission requests should be autoblocked. 328 // After the 3rd dismiss subsequent permission requests should be autoblocked.
318 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 329 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
319 url, content::PermissionType::GEOLOCATION)); 330 url, content::PermissionType::GEOLOCATION));
320 EXPECT_TRUE( 331 EXPECT_TRUE(
321 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 332 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
333 histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason",
334 PermissionEmbargoReason::REPEATED_DISMISSALS, 1);
322 335
323 // Accelerate time forward, check that the embargo status is lifted and the 336 // Accelerate time forward, check that the embargo status is lifted and the
324 // request won't be automatically blocked. 337 // request won't be automatically blocked.
325 clock()->Advance(base::TimeDelta::FromDays(8)); 338 clock()->Advance(base::TimeDelta::FromDays(8));
326 EXPECT_FALSE( 339 EXPECT_FALSE(
327 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 340 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
328 341
329 // Record another dismiss, subsequent requests should be autoblocked again. 342 // Record another dismiss, subsequent requests should be autoblocked again.
330 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 343 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
331 url, content::PermissionType::GEOLOCATION)); 344 url, content::PermissionType::GEOLOCATION));
332 EXPECT_TRUE( 345 EXPECT_TRUE(
333 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 346 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
347 histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason",
348 PermissionEmbargoReason::REPEATED_DISMISSALS, 2);
349 histograms.ExpectBucketCount("Permissions.AutoBlocker.RepeatedEmbargo",
350 PermissionEmbargoReason::REPEATED_DISMISSALS, 1);
334 351
335 // Accelerate time again, check embargo is lifted and another permission 352 // Accelerate time again, check embargo is lifted and another permission
336 // request is let through. 353 // request is let through.
337 clock()->Advance(base::TimeDelta::FromDays(8)); 354 clock()->Advance(base::TimeDelta::FromDays(8));
338 EXPECT_FALSE( 355 EXPECT_FALSE(
339 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 356 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
357
358 // Record another dismiss, subsequent requests should be autoblocked again.
359 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
360 url, content::PermissionType::GEOLOCATION));
361 EXPECT_TRUE(
362 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
363 histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason",
364 PermissionEmbargoReason::REPEATED_DISMISSALS, 3);
365 histograms.ExpectBucketCount("Permissions.AutoBlocker.RepeatedEmbargo",
366 PermissionEmbargoReason::REPEATED_DISMISSALS, 2);
340 } 367 }
341 368
342 // Test the logic for a combination of blacklisting and dismissal embargo. 369 // Test the logic for a combination of blacklisting and dismissal embargo.
343 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { 370 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) {
344 GURL url("https://www.google.com"); 371 GURL url("https://www.google.com");
345 clock()->SetNow(base::Time::Now()); 372 clock()->SetNow(base::Time::Now());
346 373
347 // Place under blacklist embargo and check the status. 374 // Place under blacklist embargo and check the status.
348 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); 375 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url);
349 clock()->Advance(base::TimeDelta::FromDays(5)); 376 clock()->Advance(base::TimeDelta::FromDays(5));
(...skipping 11 matching lines...) Expand all
361 // Accelerate time to a point where the blacklist embargo should be expired 388 // Accelerate time to a point where the blacklist embargo should be expired
362 // and check that dismissal embargo is still set. 389 // and check that dismissal embargo is still set.
363 clock()->Advance(base::TimeDelta::FromDays(3)); 390 clock()->Advance(base::TimeDelta::FromDays(3));
364 EXPECT_TRUE( 391 EXPECT_TRUE(
365 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 392 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
366 } 393 }
367 394
368 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { 395 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) {
369 GURL url("https://www.google.com"); 396 GURL url("https://www.google.com");
370 clock()->SetNow(base::Time::Now()); 397 clock()->SetNow(base::Time::Now());
398 base::HistogramTester histograms;
371 399
372 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 400 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
373 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); 401 new MockSafeBrowsingDatabaseManager(false /* perform_callback */);
374 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 402 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
375 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 403 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
376 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 404 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
377 0 /* timeout in ms */); 405 0 /* timeout in ms */);
378 406
379 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 407 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
380 EXPECT_FALSE(last_embargoed_status()); 408 EXPECT_FALSE(last_embargoed_status());
381 EXPECT_FALSE( 409 EXPECT_FALSE(
382 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 410 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
411 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse",
412 SafeBrowsingResponse::TIMEOUT, 1);
383 db_manager->SetPerformCallback(true); 413 db_manager->SetPerformCallback(true);
384 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 414 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
385 2000 /* timeout in ms */); 415 2000 /* timeout in ms */);
386 416
387 clock()->Advance(base::TimeDelta::FromDays(1)); 417 clock()->Advance(base::TimeDelta::FromDays(1));
388 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 418 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
389 EXPECT_TRUE(last_embargoed_status()); 419 EXPECT_TRUE(last_embargoed_status());
420 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse",
421 2);
422 histograms.ExpectBucketCount(
423 "Permissions.AutoBlocker.EmbargoReason",
424 PermissionEmbargoReason::PERMISSIONS_BLACKLISTING, 1);
390 425
391 clock()->Advance(base::TimeDelta::FromDays(1)); 426 clock()->Advance(base::TimeDelta::FromDays(1));
392 EXPECT_TRUE( 427 EXPECT_TRUE(
393 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 428 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
394 } 429 }
430
431 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingResponse) {
432 GURL url("https://www.google.com");
433 clock()->SetNow(base::Time::Now());
434 base::HistogramTester histograms;
435
436 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
437 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
438 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
439 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
440 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
441 0 /* timeout in ms */);
442
443 UpdateEmbargoedStatus(content::PermissionType::NOTIFICATIONS, url);
444 EXPECT_FALSE(last_embargoed_status());
445 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse",
446 SafeBrowsingResponse::NOT_BLACKLISTED, 1);
447 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698