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

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

Issue 2678253002: Add metrics for Safe Browsing blacklist response. (Closed)
Patch Set: Histogram sanity check. 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/content_settings/core/browser/host_content_settings_map.h" 20 #include "components/content_settings/core/browser/host_content_settings_map.h"
19 #include "components/safe_browsing_db/test_database_manager.h" 21 #include "components/safe_browsing_db/test_database_manager.h"
20 #include "content/public/browser/permission_type.h" 22 #include "content/public/browser/permission_type.h"
21 23
22 namespace { 24 namespace {
23 25
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 url2, content::PermissionType::GEOLOCATION)); 249 url2, content::PermissionType::GEOLOCATION));
248 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 250 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
249 url2, content::PermissionType::DURABLE_STORAGE)); 251 url2, content::PermissionType::DURABLE_STORAGE));
250 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 252 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
251 url2, content::PermissionType::MIDI_SYSEX)); 253 url2, content::PermissionType::MIDI_SYSEX));
252 } 254 }
253 255
254 // Test that an origin that has been blacklisted for a permission is embargoed. 256 // Test that an origin that has been blacklisted for a permission is embargoed.
255 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { 257 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) {
256 GURL url("https://www.google.com"); 258 GURL url("https://www.google.com");
259 base::HistogramTester histograms;
257 260
258 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 261 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
259 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); 262 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
260 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 263 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
261 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 264 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
262 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 265 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
263 2000 /* timeout in ms */); 266 2000 /* timeout in ms */);
264 267
265 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 268 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
266 EXPECT_TRUE(last_embargoed_status()); 269 EXPECT_TRUE(last_embargoed_status());
270 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse",
271 SafeBrowsingResponse::BLACKLISTED, 1);
267 } 272 }
268 273
269 // Check that IsUnderEmbargo returns the correct value when the embargo is set 274 // Check that IsUnderEmbargo returns the correct value when the embargo is set
270 // and expires. 275 // and expires.
271 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { 276 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) {
272 GURL url("https://www.google.com"); 277 GURL url("https://www.google.com");
273 clock()->SetNow(base::Time::Now()); 278 clock()->SetNow(base::Time::Now());
274 279
275 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); 280 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url);
276 EXPECT_TRUE( 281 EXPECT_TRUE(
(...skipping 28 matching lines...) Expand all
305 } 310 }
306 311
307 // Tests the alternating pattern of the block on multiple dismiss behaviour. On 312 // Tests the alternating pattern of the block on multiple dismiss behaviour. On
308 // N dismissals, the origin to be embargoed for the requested permission and 313 // N dismissals, the origin to be embargoed for the requested permission and
309 // automatically blocked. Each time the embargo is lifted, the site gets another 314 // automatically blocked. Each time the embargo is lifted, the site gets another
310 // chance to request the permission, but if it is again dismissed it is placed 315 // chance to request the permission, but if it is again dismissed it is placed
311 // under embargo again and its permission requests blocked. 316 // under embargo again and its permission requests blocked.
312 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { 317 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) {
313 GURL url("https://www.google.com"); 318 GURL url("https://www.google.com");
314 clock()->SetNow(base::Time::Now()); 319 clock()->SetNow(base::Time::Now());
320 base::HistogramTester histograms;
315 321
316 // Record some dismisses. 322 // Record some dismisses.
317 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 323 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
318 url, content::PermissionType::GEOLOCATION)); 324 url, content::PermissionType::GEOLOCATION));
319 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 325 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
320 url, content::PermissionType::GEOLOCATION)); 326 url, content::PermissionType::GEOLOCATION));
321 327
322 // A request with < 3 prior dismisses should not be automatically blocked. 328 // A request with < 3 prior dismisses should not be automatically blocked.
323 EXPECT_FALSE( 329 EXPECT_FALSE(
324 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 330 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
325 331
326 // After the 3rd dismiss subsequent permission requests should be autoblocked. 332 // After the 3rd dismiss subsequent permission requests should be autoblocked.
327 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 333 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
328 url, content::PermissionType::GEOLOCATION)); 334 url, content::PermissionType::GEOLOCATION));
329 EXPECT_TRUE( 335 EXPECT_TRUE(
330 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 336 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
331 337
338 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse",
339 0);
340 histograms.ExpectTotalCount(
341 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 0);
332 // Accelerate time forward, check that the embargo status is lifted and the 342 // Accelerate time forward, check that the embargo status is lifted and the
333 // request won't be automatically blocked. 343 // request won't be automatically blocked.
334 clock()->Advance(base::TimeDelta::FromDays(8)); 344 clock()->Advance(base::TimeDelta::FromDays(8));
335 EXPECT_FALSE( 345 EXPECT_FALSE(
336 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 346 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
337 347
338 // Record another dismiss, subsequent requests should be autoblocked again. 348 // Record another dismiss, subsequent requests should be autoblocked again.
339 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 349 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
340 url, content::PermissionType::GEOLOCATION)); 350 url, content::PermissionType::GEOLOCATION));
341 EXPECT_TRUE( 351 EXPECT_TRUE(
342 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 352 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
343 353
344 // Accelerate time again, check embargo is lifted and another permission 354 // Accelerate time again, check embargo is lifted and another permission
345 // request is let through. 355 // request is let through.
346 clock()->Advance(base::TimeDelta::FromDays(8)); 356 clock()->Advance(base::TimeDelta::FromDays(8));
347 EXPECT_FALSE( 357 EXPECT_FALSE(
348 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 358 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
359
360 // Record another dismiss, subsequent requests should be autoblocked again.
361 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
362 url, content::PermissionType::GEOLOCATION));
363 EXPECT_TRUE(
364 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
365 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse",
366 0);
367 histograms.ExpectTotalCount(
368 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 0);
349 } 369 }
350 370
351 // Test the logic for a combination of blacklisting and dismissal embargo. 371 // Test the logic for a combination of blacklisting and dismissal embargo.
352 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { 372 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) {
353 GURL url("https://www.google.com"); 373 GURL url("https://www.google.com");
354 clock()->SetNow(base::Time::Now()); 374 clock()->SetNow(base::Time::Now());
355 375
356 // Place under blacklist embargo and check the status. 376 // Place under blacklist embargo and check the status.
357 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); 377 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url);
358 clock()->Advance(base::TimeDelta::FromDays(5)); 378 clock()->Advance(base::TimeDelta::FromDays(5));
(...skipping 11 matching lines...) Expand all
370 // Accelerate time to a point where the blacklist embargo should be expired 390 // Accelerate time to a point where the blacklist embargo should be expired
371 // and check that dismissal embargo is still set. 391 // and check that dismissal embargo is still set.
372 clock()->Advance(base::TimeDelta::FromDays(3)); 392 clock()->Advance(base::TimeDelta::FromDays(3));
373 EXPECT_TRUE( 393 EXPECT_TRUE(
374 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 394 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
375 } 395 }
376 396
377 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { 397 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) {
378 GURL url("https://www.google.com"); 398 GURL url("https://www.google.com");
379 clock()->SetNow(base::Time::Now()); 399 clock()->SetNow(base::Time::Now());
400 base::HistogramTester histograms;
380 401
381 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 402 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
382 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); 403 new MockSafeBrowsingDatabaseManager(false /* perform_callback */);
383 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 404 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
384 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 405 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
385 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 406 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
386 0 /* timeout in ms */); 407 0 /* timeout in ms */);
387 408
388 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 409 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
389 EXPECT_FALSE(last_embargoed_status()); 410 EXPECT_FALSE(last_embargoed_status());
390 EXPECT_FALSE( 411 EXPECT_FALSE(
391 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 412 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
413 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse",
414 SafeBrowsingResponse::TIMEOUT, 1);
415 histograms.ExpectTotalCount(
416 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1);
392 db_manager->SetPerformCallback(true); 417 db_manager->SetPerformCallback(true);
393 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 418 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
394 2000 /* timeout in ms */); 419 2000 /* timeout in ms */);
395 420
396 clock()->Advance(base::TimeDelta::FromDays(1)); 421 clock()->Advance(base::TimeDelta::FromDays(1));
397 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 422 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
398 EXPECT_TRUE(last_embargoed_status()); 423 EXPECT_TRUE(last_embargoed_status());
399 424 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse",
425 2);
426 histograms.ExpectTotalCount(
427 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 2);
428 histograms.ExpectBucketCount("Permissions.AutoBlocker.SafeBrowsingResponse",
429 SafeBrowsingResponse::BLACKLISTED, 1);
400 clock()->Advance(base::TimeDelta::FromDays(1)); 430 clock()->Advance(base::TimeDelta::FromDays(1));
401 EXPECT_TRUE( 431 EXPECT_TRUE(
402 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 432 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
403 } 433 }
404 434
405 // TODO(raymes): See crbug.com/681709. Remove after M60. 435 // TODO(raymes): See crbug.com/681709. Remove after M60.
406 TEST_F(PermissionDecisionAutoBlockerUnitTest, 436 TEST_F(PermissionDecisionAutoBlockerUnitTest,
407 MigrateNoDecisionCountToPermissionAutoBlockerData) { 437 MigrateNoDecisionCountToPermissionAutoBlockerData) {
408 GURL url("https://www.google.com"); 438 GURL url("https://www.google.com");
409 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); 439 auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 profile()->GetPrefs(), false /* is_incognito_profile */, 496 profile()->GetPrefs(), false /* is_incognito_profile */,
467 false /* is_guest_profile */)); 497 false /* is_guest_profile */));
468 temp_map->ShutdownOnUIThread(); 498 temp_map->ShutdownOnUIThread();
469 } 499 }
470 500
471 EXPECT_EQ(100, autoblocker()->GetDismissCount( 501 EXPECT_EQ(100, autoblocker()->GetDismissCount(
472 url, content::PermissionType::GEOLOCATION)); 502 url, content::PermissionType::GEOLOCATION));
473 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( 503 EXPECT_EQ(50, autoblocker()->GetIgnoreCount(
474 url, content::PermissionType::GEOLOCATION)); 504 url, content::PermissionType::GEOLOCATION));
475 } 505 }
506
507 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingResponse) {
raymes 2017/02/08 02:08:00 nit: maybe this should just be called TestRequestN
meredithl 2017/02/08 02:40:32 Done. I've also moved it upwards to underneath Tes
508 GURL url("https://www.google.com");
509 clock()->SetNow(base::Time::Now());
510 base::HistogramTester histograms;
511
512 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
513 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
514 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
515 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
516 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
517 0 /* timeout in ms */);
518
519 UpdateEmbargoedStatus(content::PermissionType::NOTIFICATIONS, url);
520 EXPECT_FALSE(last_embargoed_status());
521 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse",
522 SafeBrowsingResponse::NOT_BLACKLISTED, 1);
523 histograms.ExpectTotalCount(
524 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1);
525 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698