OLD | NEW |
---|---|
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 Loading... | |
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 url, content::PermissionType::GEOLOCATION)); | 325 url, content::PermissionType::GEOLOCATION)); |
321 | 326 |
322 // A request with < 3 prior dismisses should not be automatically blocked. | 327 // A request with < 3 prior dismisses should not be automatically blocked. |
323 EXPECT_FALSE( | 328 EXPECT_FALSE( |
324 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 329 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
325 | 330 |
326 // After the 3rd dismiss subsequent permission requests should be autoblocked. | 331 // After the 3rd dismiss subsequent permission requests should be autoblocked. |
327 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 332 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
328 url, content::PermissionType::GEOLOCATION)); | 333 url, content::PermissionType::GEOLOCATION)); |
329 EXPECT_TRUE( | 334 EXPECT_TRUE( |
330 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 335 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
dominickn
2017/02/07 22:28:25
Just as a sanity check, can you add:
histograms.E
meredithl
2017/02/08 01:37:39
Done.
| |
331 | 336 |
332 // Accelerate time forward, check that the embargo status is lifted and the | 337 // Accelerate time forward, check that the embargo status is lifted and the |
333 // request won't be automatically blocked. | 338 // request won't be automatically blocked. |
334 clock()->Advance(base::TimeDelta::FromDays(8)); | 339 clock()->Advance(base::TimeDelta::FromDays(8)); |
335 EXPECT_FALSE( | 340 EXPECT_FALSE( |
336 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 341 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
337 | 342 |
338 // Record another dismiss, subsequent requests should be autoblocked again. | 343 // Record another dismiss, subsequent requests should be autoblocked again. |
339 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 344 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
340 url, content::PermissionType::GEOLOCATION)); | 345 url, content::PermissionType::GEOLOCATION)); |
341 EXPECT_TRUE( | 346 EXPECT_TRUE( |
342 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 347 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
343 | 348 |
344 // Accelerate time again, check embargo is lifted and another permission | 349 // Accelerate time again, check embargo is lifted and another permission |
345 // request is let through. | 350 // request is let through. |
346 clock()->Advance(base::TimeDelta::FromDays(8)); | 351 clock()->Advance(base::TimeDelta::FromDays(8)); |
347 EXPECT_FALSE( | 352 EXPECT_FALSE( |
348 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 353 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
354 | |
355 // Record another dismiss, subsequent requests should be autoblocked again. | |
356 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | |
357 url, content::PermissionType::GEOLOCATION)); | |
358 EXPECT_TRUE( | |
359 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | |
dominickn
2017/02/07 22:28:25
And here too.
meredithl
2017/02/08 01:37:39
Done.
| |
349 } | 360 } |
350 | 361 |
351 // Test the logic for a combination of blacklisting and dismissal embargo. | 362 // Test the logic for a combination of blacklisting and dismissal embargo. |
352 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { | 363 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { |
353 GURL url("https://www.google.com"); | 364 GURL url("https://www.google.com"); |
354 clock()->SetNow(base::Time::Now()); | 365 clock()->SetNow(base::Time::Now()); |
355 | 366 |
356 // Place under blacklist embargo and check the status. | 367 // Place under blacklist embargo and check the status. |
357 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); | 368 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); |
358 clock()->Advance(base::TimeDelta::FromDays(5)); | 369 clock()->Advance(base::TimeDelta::FromDays(5)); |
(...skipping 11 matching lines...) Expand all Loading... | |
370 // Accelerate time to a point where the blacklist embargo should be expired | 381 // Accelerate time to a point where the blacklist embargo should be expired |
371 // and check that dismissal embargo is still set. | 382 // and check that dismissal embargo is still set. |
372 clock()->Advance(base::TimeDelta::FromDays(3)); | 383 clock()->Advance(base::TimeDelta::FromDays(3)); |
373 EXPECT_TRUE( | 384 EXPECT_TRUE( |
374 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 385 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
375 } | 386 } |
376 | 387 |
377 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { | 388 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { |
378 GURL url("https://www.google.com"); | 389 GURL url("https://www.google.com"); |
379 clock()->SetNow(base::Time::Now()); | 390 clock()->SetNow(base::Time::Now()); |
391 base::HistogramTester histograms; | |
380 | 392 |
381 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 393 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
382 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | 394 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); |
383 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 395 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
384 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 396 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
385 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 397 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
386 0 /* timeout in ms */); | 398 0 /* timeout in ms */); |
387 | 399 |
388 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 400 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); |
389 EXPECT_FALSE(last_embargoed_status()); | 401 EXPECT_FALSE(last_embargoed_status()); |
390 EXPECT_FALSE( | 402 EXPECT_FALSE( |
391 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 403 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
404 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | |
405 SafeBrowsingResponse::TIMEOUT, 1); | |
392 db_manager->SetPerformCallback(true); | 406 db_manager->SetPerformCallback(true); |
dominickn
2017/02/07 22:28:25
Add:
histograms.ExpectTotalCount("Permissions.Aut
meredithl
2017/02/08 01:37:38
Done.
| |
393 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 407 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
394 2000 /* timeout in ms */); | 408 2000 /* timeout in ms */); |
395 | 409 |
396 clock()->Advance(base::TimeDelta::FromDays(1)); | 410 clock()->Advance(base::TimeDelta::FromDays(1)); |
397 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 411 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); |
398 EXPECT_TRUE(last_embargoed_status()); | 412 EXPECT_TRUE(last_embargoed_status()); |
399 | 413 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", |
dominickn
2017/02/07 22:28:25
Add:
histograms.ExpectTotalCount("Permissions.Aut
meredithl
2017/02/08 01:37:39
Done.
| |
414 2); | |
415 histograms.ExpectBucketCount("Permissions.AutoBlocker.SafeBrowsingResponse", | |
416 SafeBrowsingResponse::BLACKLISTED, 1); | |
400 clock()->Advance(base::TimeDelta::FromDays(1)); | 417 clock()->Advance(base::TimeDelta::FromDays(1)); |
401 EXPECT_TRUE( | 418 EXPECT_TRUE( |
402 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 419 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
403 } | 420 } |
404 | 421 |
405 // TODO(raymes): See crbug.com/681709. Remove after M60. | 422 // TODO(raymes): See crbug.com/681709. Remove after M60. |
406 TEST_F(PermissionDecisionAutoBlockerUnitTest, | 423 TEST_F(PermissionDecisionAutoBlockerUnitTest, |
407 MigrateNoDecisionCountToPermissionAutoBlockerData) { | 424 MigrateNoDecisionCountToPermissionAutoBlockerData) { |
408 GURL url("https://www.google.com"); | 425 GURL url("https://www.google.com"); |
409 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 426 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 profile()->GetPrefs(), false /* is_incognito_profile */, | 483 profile()->GetPrefs(), false /* is_incognito_profile */, |
467 false /* is_guest_profile */)); | 484 false /* is_guest_profile */)); |
468 temp_map->ShutdownOnUIThread(); | 485 temp_map->ShutdownOnUIThread(); |
469 } | 486 } |
470 | 487 |
471 EXPECT_EQ(100, autoblocker()->GetDismissCount( | 488 EXPECT_EQ(100, autoblocker()->GetDismissCount( |
472 url, content::PermissionType::GEOLOCATION)); | 489 url, content::PermissionType::GEOLOCATION)); |
473 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( | 490 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( |
474 url, content::PermissionType::GEOLOCATION)); | 491 url, content::PermissionType::GEOLOCATION)); |
475 } | 492 } |
493 | |
494 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingResponse) { | |
495 GURL url("https://www.google.com"); | |
496 clock()->SetNow(base::Time::Now()); | |
497 base::HistogramTester histograms; | |
498 | |
499 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
500 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | |
501 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | |
502 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
503 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | |
504 0 /* timeout in ms */); | |
505 | |
506 UpdateEmbargoedStatus(content::PermissionType::NOTIFICATIONS, url); | |
507 EXPECT_FALSE(last_embargoed_status()); | |
508 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | |
509 SafeBrowsingResponse::NOT_BLACKLISTED, 1); | |
dominickn
2017/02/07 22:28:25
Add:
histograms.ExpectTotalCount("Permissions.Aut
meredithl
2017/02/08 01:37:39
Done.
| |
510 } | |
OLD | NEW |