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

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

Issue 2684523002: Check result of ApiBlacklist query in client. (Closed)
Patch Set: Check callback. 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
« no previous file with comments | « chrome/browser/permissions/permission_decision_auto_blocker.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 11 matching lines...) Expand all
22 namespace { 22 namespace {
23 23
24 bool FilterGoogle(const GURL& url) { 24 bool FilterGoogle(const GURL& url) {
25 return url == "https://www.google.com/"; 25 return url == "https://www.google.com/";
26 } 26 }
27 27
28 bool FilterAll(const GURL& url) { 28 bool FilterAll(const GURL& url) {
29 return true; 29 return true;
30 } 30 }
31 31
32 } // namespace
33
34 class MockSafeBrowsingDatabaseManager 32 class MockSafeBrowsingDatabaseManager
35 : public safe_browsing::TestSafeBrowsingDatabaseManager { 33 : public safe_browsing::TestSafeBrowsingDatabaseManager {
36 public: 34 public:
37 explicit MockSafeBrowsingDatabaseManager(bool perform_callback) 35 explicit MockSafeBrowsingDatabaseManager(bool perform_callback, bool enabled)
38 : perform_callback_(perform_callback) {} 36 : perform_callback_(perform_callback), enabled_(enabled) {}
39 37
40 bool CheckApiBlacklistUrl( 38 bool CheckApiBlacklistUrl(
41 const GURL& url, 39 const GURL& url,
42 safe_browsing::SafeBrowsingDatabaseManager::Client* client) override { 40 safe_browsing::SafeBrowsingDatabaseManager::Client* client) override {
41 // Return true when able to synchronously determine that the url is safe.
42 if (!enabled_) {
43 return true;
44 }
45
43 if (perform_callback_) { 46 if (perform_callback_) {
44 safe_browsing::ThreatMetadata metadata; 47 safe_browsing::ThreatMetadata metadata;
45 const auto& blacklisted_permissions = permissions_blacklist_.find(url); 48 const auto& blacklisted_permissions = permissions_blacklist_.find(url);
46 if (blacklisted_permissions != permissions_blacklist_.end()) 49 if (blacklisted_permissions != permissions_blacklist_.end())
47 metadata.api_permissions = blacklisted_permissions->second; 50 metadata.api_permissions = blacklisted_permissions->second;
48 client->OnCheckApiBlacklistUrlResult(url, metadata); 51 client->OnCheckApiBlacklistUrlResult(url, metadata);
49 } 52 }
50 return false; 53 return false;
51 } 54 }
52 55
(...skipping 10 matching lines...) Expand all
63 66
64 void SetPerformCallback(bool perform_callback) { 67 void SetPerformCallback(bool perform_callback) {
65 perform_callback_ = perform_callback; 68 perform_callback_ = perform_callback;
66 } 69 }
67 70
68 protected: 71 protected:
69 ~MockSafeBrowsingDatabaseManager() override {} 72 ~MockSafeBrowsingDatabaseManager() override {}
70 73
71 private: 74 private:
72 bool perform_callback_; 75 bool perform_callback_;
76 bool enabled_;
73 std::map<GURL, std::set<std::string>> permissions_blacklist_; 77 std::map<GURL, std::set<std::string>> permissions_blacklist_;
74 78
75 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); 79 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager);
76 }; 80 };
77 81
82 } // namespace
83
78 class PermissionDecisionAutoBlockerUnitTest 84 class PermissionDecisionAutoBlockerUnitTest
79 : public ChromeRenderViewHostTestHarness { 85 : public ChromeRenderViewHostTestHarness {
80 protected: 86 protected:
81 void SetUp() override { 87 void SetUp() override {
82 ChromeRenderViewHostTestHarness::SetUp(); 88 ChromeRenderViewHostTestHarness::SetUp();
83 autoblocker_ = PermissionDecisionAutoBlocker::GetForProfile(profile()); 89 autoblocker_ = PermissionDecisionAutoBlocker::GetForProfile(profile());
84 feature_list_.InitWithFeatures({features::kBlockPromptsIfDismissedOften, 90 feature_list_.InitWithFeatures({features::kBlockPromptsIfDismissedOften,
85 features::kPermissionsBlacklist}, 91 features::kPermissionsBlacklist},
86 {}); 92 {});
87 last_embargoed_status_ = false; 93 last_embargoed_status_ = false;
88 std::unique_ptr<base::SimpleTestClock> clock = 94 std::unique_ptr<base::SimpleTestClock> clock =
89 base::MakeUnique<base::SimpleTestClock>(); 95 base::MakeUnique<base::SimpleTestClock>();
90 clock_ = clock.get(); 96 clock_ = clock.get();
91 autoblocker_->SetClockForTesting(std::move(clock)); 97 autoblocker_->SetClockForTesting(std::move(clock));
98 callback_was_run_ = false;
92 } 99 }
93 100
94 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting( 101 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(
95 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager, 102 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager,
96 int timeout) { 103 int timeout) {
97 autoblocker_->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 104 autoblocker_->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
98 timeout); 105 timeout);
99 } 106 }
100 107
101 void UpdateEmbargoedStatus(content::PermissionType permission, 108 void UpdateEmbargoedStatus(content::PermissionType permission,
(...skipping 12 matching lines...) Expand all
114 void PlaceUnderBlacklistEmbargo(content::PermissionType permission, 121 void PlaceUnderBlacklistEmbargo(content::PermissionType permission,
115 const GURL& url) { 122 const GURL& url) {
116 autoblocker_->PlaceUnderEmbargo( 123 autoblocker_->PlaceUnderEmbargo(
117 permission, url, 124 permission, url,
118 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey); 125 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey);
119 } 126 }
120 127
121 PermissionDecisionAutoBlocker* autoblocker() { return autoblocker_; } 128 PermissionDecisionAutoBlocker* autoblocker() { return autoblocker_; }
122 129
123 void SetLastEmbargoStatus(base::Closure quit_closure, bool status) { 130 void SetLastEmbargoStatus(base::Closure quit_closure, bool status) {
131 callback_was_run_ = true;
124 last_embargoed_status_ = status; 132 last_embargoed_status_ = status;
125 if (quit_closure) { 133 if (quit_closure) {
126 quit_closure.Run(); 134 quit_closure.Run();
127 quit_closure.Reset(); 135 quit_closure.Reset();
128 } 136 }
129 } 137 }
130 138
131 bool last_embargoed_status() { return last_embargoed_status_; } 139 bool last_embargoed_status() { return last_embargoed_status_; }
132 140
141 bool callback_was_run() { return callback_was_run_; }
142
133 base::SimpleTestClock* clock() { return clock_; } 143 base::SimpleTestClock* clock() { return clock_; }
134 144
135 const char* GetDismissKey() { 145 const char* GetDismissKey() {
136 return PermissionDecisionAutoBlocker::kPromptDismissCountKey; 146 return PermissionDecisionAutoBlocker::kPromptDismissCountKey;
137 } 147 }
138 148
139 const char* GetIgnoreKey() { 149 const char* GetIgnoreKey() {
140 return PermissionDecisionAutoBlocker::kPromptIgnoreCountKey; 150 return PermissionDecisionAutoBlocker::kPromptIgnoreCountKey;
141 } 151 }
142 152
143 private: 153 private:
144 PermissionDecisionAutoBlocker* autoblocker_; 154 PermissionDecisionAutoBlocker* autoblocker_;
145 base::test::ScopedFeatureList feature_list_; 155 base::test::ScopedFeatureList feature_list_;
146 base::SimpleTestClock* clock_; 156 base::SimpleTestClock* clock_;
147 bool last_embargoed_status_; 157 bool last_embargoed_status_;
158 bool callback_was_run_;
148 }; 159 };
149 160
150 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { 161 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) {
151 GURL url1("https://www.google.com"); 162 GURL url1("https://www.google.com");
152 GURL url2("https://www.example.com"); 163 GURL url2("https://www.example.com");
153 164
154 // Record some dismissals. 165 // Record some dismissals.
155 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 166 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
156 url1, content::PermissionType::GEOLOCATION)); 167 url1, content::PermissionType::GEOLOCATION));
157 EXPECT_EQ(1, autoblocker()->GetDismissCount( 168 EXPECT_EQ(1, autoblocker()->GetDismissCount(
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 url2, content::PermissionType::DURABLE_STORAGE)); 260 url2, content::PermissionType::DURABLE_STORAGE));
250 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 261 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
251 url2, content::PermissionType::MIDI_SYSEX)); 262 url2, content::PermissionType::MIDI_SYSEX));
252 } 263 }
253 264
254 // Test that an origin that has been blacklisted for a permission is embargoed. 265 // Test that an origin that has been blacklisted for a permission is embargoed.
255 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { 266 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) {
256 GURL url("https://www.google.com"); 267 GURL url("https://www.google.com");
257 268
258 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 269 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
259 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); 270 new MockSafeBrowsingDatabaseManager(true /* perform_callback */,
271 true /* enabled */);
260 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 272 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
261 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 273 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
262 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 274 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
263 2000 /* timeout in ms */); 275 2000 /* timeout in ms */);
264 276
265 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 277 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
278 EXPECT_TRUE(callback_was_run());
266 EXPECT_TRUE(last_embargoed_status()); 279 EXPECT_TRUE(last_embargoed_status());
267 } 280 }
268 281
269 // Check that IsUnderEmbargo returns the correct value when the embargo is set 282 // Check that IsUnderEmbargo returns the correct value when the embargo is set
270 // and expires. 283 // and expires.
271 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { 284 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) {
272 GURL url("https://www.google.com"); 285 GURL url("https://www.google.com");
273 clock()->SetNow(base::Time::Now()); 286 clock()->SetNow(base::Time::Now());
274 287
275 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); 288 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 clock()->Advance(base::TimeDelta::FromDays(3)); 385 clock()->Advance(base::TimeDelta::FromDays(3));
373 EXPECT_TRUE( 386 EXPECT_TRUE(
374 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 387 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
375 } 388 }
376 389
377 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { 390 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) {
378 GURL url("https://www.google.com"); 391 GURL url("https://www.google.com");
379 clock()->SetNow(base::Time::Now()); 392 clock()->SetNow(base::Time::Now());
380 393
381 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 394 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
382 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); 395 new MockSafeBrowsingDatabaseManager(false /* perform_callback */,
396 true /* enabled */);
383 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 397 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
384 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 398 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
385 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 399 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
386 0 /* timeout in ms */); 400 0 /* timeout in ms */);
387 401
388 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 402 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
403 EXPECT_TRUE(callback_was_run());
389 EXPECT_FALSE(last_embargoed_status()); 404 EXPECT_FALSE(last_embargoed_status());
390 EXPECT_FALSE( 405 EXPECT_FALSE(
391 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 406 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
392 db_manager->SetPerformCallback(true); 407 db_manager->SetPerformCallback(true);
393 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 408 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
394 2000 /* timeout in ms */); 409 2000 /* timeout in ms */);
395 410
396 clock()->Advance(base::TimeDelta::FromDays(1)); 411 clock()->Advance(base::TimeDelta::FromDays(1));
397 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 412 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
413 EXPECT_TRUE(callback_was_run());
398 EXPECT_TRUE(last_embargoed_status()); 414 EXPECT_TRUE(last_embargoed_status());
399 415
400 clock()->Advance(base::TimeDelta::FromDays(1)); 416 clock()->Advance(base::TimeDelta::FromDays(1));
401 EXPECT_TRUE( 417 EXPECT_TRUE(
402 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 418 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url));
403 } 419 }
404 420
405 // TODO(raymes): See crbug.com/681709. Remove after M60. 421 // TODO(raymes): See crbug.com/681709. Remove after M60.
406 TEST_F(PermissionDecisionAutoBlockerUnitTest, 422 TEST_F(PermissionDecisionAutoBlockerUnitTest,
407 MigrateNoDecisionCountToPermissionAutoBlockerData) { 423 MigrateNoDecisionCountToPermissionAutoBlockerData) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 profile()->GetPrefs(), false /* is_incognito_profile */, 482 profile()->GetPrefs(), false /* is_incognito_profile */,
467 false /* is_guest_profile */)); 483 false /* is_guest_profile */));
468 temp_map->ShutdownOnUIThread(); 484 temp_map->ShutdownOnUIThread();
469 } 485 }
470 486
471 EXPECT_EQ(100, autoblocker()->GetDismissCount( 487 EXPECT_EQ(100, autoblocker()->GetDismissCount(
472 url, content::PermissionType::GEOLOCATION)); 488 url, content::PermissionType::GEOLOCATION));
473 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( 489 EXPECT_EQ(50, autoblocker()->GetIgnoreCount(
474 url, content::PermissionType::GEOLOCATION)); 490 url, content::PermissionType::GEOLOCATION));
475 } 491 }
492
493 // Test that a blacklisted permission should not be autoblocked if the database
494 // manager is disabled.
495 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDisabledDatabaseManager) {
496 GURL url("https://www.google.com");
497 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
498 new MockSafeBrowsingDatabaseManager(true /* perform_callback */,
499 false /* enabled */);
500 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
501 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
502 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
503 2000 /* timeout in ms */);
504 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url);
505 EXPECT_TRUE(callback_was_run());
506 EXPECT_FALSE(last_embargoed_status());
507 }
OLDNEW
« no previous file with comments | « chrome/browser/permissions/permission_decision_auto_blocker.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698