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

Side by Side Diff: chrome/browser/safe_browsing/permission_reporter_unittest.cc

Issue 2089383005: Add throttling to permission reporter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@add-hooks-to-permission-layer
Patch Set: Add comment Created 4 years, 5 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/safe_browsing/permission_reporter.h" 5 #include "chrome/browser/safe_browsing/permission_reporter.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/test/simple_test_clock.h"
9 #include "base/time/time.h"
8 #include "chrome/common/safe_browsing/permission_report.pb.h" 10 #include "chrome/common/safe_browsing/permission_report.pb.h"
9 #include "content/public/browser/permission_type.h" 11 #include "content/public/browser/permission_type.h"
10 #include "net/url_request/report_sender.h" 12 #include "net/url_request/report_sender.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 14
13 using content::PermissionType; 15 using content::PermissionType;
14 16
15 namespace safe_browsing { 17 namespace safe_browsing {
16 18
17 namespace { 19 namespace {
18 // URL to upload permission action reports. 20 // URL to upload permission action reports.
19 const char kPermissionActionReportingUploadUrl[] = 21 const char kPermissionActionReportingUploadUrl[] =
20 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" 22 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/"
21 "permission-action"; 23 "permission-action";
22 24
23 const char kDummyOrigin[] = "http://example.test/"; 25 const int kMaximumReportsPerOriginPerPermissionPerMinute = 5;
24 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; 26
27 const char kDummyOriginOne[] = "http://example.test/";
28 const char kDummyOriginTwo[] = "http://example2.test/";
29 const PermissionType kDummyPermissionOne = PermissionType::GEOLOCATION;
30 const PermissionType kDummyPermissionTwo = PermissionType::NOTIFICATIONS;
25 const PermissionAction kDummyAction = GRANTED; 31 const PermissionAction kDummyAction = GRANTED;
26 const PermissionReport::PermissionType kDummyPermissionReportPermission = 32 const PermissionReport::PermissionType kDummyPermissionReportPermissionOne =
raymes 2016/06/29 07:09:17 nit: maybe we just inline this? Sometimes it's a b
stefanocs 2016/06/30 00:24:51 Done.
27 PermissionReport::GEOLOCATION; 33 PermissionReport::GEOLOCATION;
28 const PermissionReport::Action kDummyPermissionReportAction = 34 const PermissionReport::Action kDummyPermissionReportAction =
raymes 2016/06/29 07:09:17 nit: same here.
stefanocs 2016/06/30 00:24:51 Done.
29 PermissionReport::GRANTED; 35 PermissionReport::GRANTED;
30 36
31 // A mock ReportSender that keeps track of the last report sent. 37 // A mock ReportSender that keeps track of the last report sent.
32 class MockReportSender : public net::ReportSender { 38 class MockReportSender : public net::ReportSender {
33 public: 39 public:
34 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} 40 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {}
35 ~MockReportSender() override {} 41 ~MockReportSender() override {}
36 42
37 void Send(const GURL& report_uri, const std::string& report) override { 43 void Send(const GURL& report_uri, const std::string& report) override {
38 latest_report_uri_ = report_uri; 44 latest_report_uri_ = report_uri;
39 latest_report_ = report; 45 latest_report_ = report;
40 } 46 }
41 47
42 const GURL& latest_report_uri() { return latest_report_uri_; } 48 const GURL& latest_report_uri() { return latest_report_uri_; }
43 49
44 const std::string& latest_report() { return latest_report_; } 50 const std::string& latest_report() { return latest_report_; }
45 51
46 private: 52 private:
47 GURL latest_report_uri_; 53 GURL latest_report_uri_;
48 std::string latest_report_; 54 std::string latest_report_;
49 55
50 DISALLOW_COPY_AND_ASSIGN(MockReportSender); 56 DISALLOW_COPY_AND_ASSIGN(MockReportSender);
51 }; 57 };
52 58
53 } // namespace 59 } // namespace
54 60
55 class PermissionReporterTest : public ::testing::Test { 61 class PermissionReporterTest : public ::testing::Test {
56 protected: 62 protected:
57 PermissionReporterTest() 63 PermissionReporterTest()
58 : mock_report_sender_(new MockReportSender()), 64 : mock_report_sender_(new MockReportSender),
65 clock_(new base::SimpleTestClock),
59 permission_reporter_( 66 permission_reporter_(
60 new PermissionReporter(base::WrapUnique(mock_report_sender_))) {} 67 new PermissionReporter(base::WrapUnique(mock_report_sender_),
68 base::WrapUnique(clock_))) {}
69
70 bool IsAllowedToSend(content::PermissionType permission, const GURL& origin) {
71 return permission_reporter_->IsAllowedToSend(permission, origin);
72 }
61 73
62 // Owned by |permission_reporter_|. 74 // Owned by |permission_reporter_|.
63 MockReportSender* mock_report_sender_; 75 MockReportSender* mock_report_sender_;
64 76
77 // Owned by |permission_reporter_|.
78 base::SimpleTestClock* clock_;
79
65 std::unique_ptr<PermissionReporter> permission_reporter_; 80 std::unique_ptr<PermissionReporter> permission_reporter_;
66 }; 81 };
67 82
68 // Test that PermissionReporter::SendReport sends a serialized report string to 83 // Test that PermissionReporter::SendReport sends a serialized report string to
69 // SafeBrowsing CSD servers. 84 // SafeBrowsing CSD servers.
70 TEST_F(PermissionReporterTest, SendReport) { 85 TEST_F(PermissionReporterTest, SendReport) {
71 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, 86 permission_reporter_->SendReport(GURL(kDummyOriginOne), kDummyPermissionOne,
72 kDummyAction); 87 kDummyAction);
73 88
74 PermissionReport permission_report; 89 PermissionReport permission_report;
75 ASSERT_TRUE( 90 ASSERT_TRUE(
76 permission_report.ParseFromString(mock_report_sender_->latest_report())); 91 permission_report.ParseFromString(mock_report_sender_->latest_report()));
77 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); 92 EXPECT_EQ(kDummyPermissionReportPermissionOne,
93 permission_report.permission());
78 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); 94 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action());
79 EXPECT_EQ(kDummyOrigin, permission_report.origin()); 95 EXPECT_EQ(kDummyOriginOne, permission_report.origin());
80 96
81 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), 97 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl),
82 mock_report_sender_->latest_report_uri()); 98 mock_report_sender_->latest_report_uri());
83 } 99 }
84 100
101 // Test that PermissionReporter::IsAllowedToSend returns true only when the
102 // number of reports sent in the last one minute per origin per permission is
103 // under a threshold.
104 TEST_F(PermissionReporterTest, IsAllowedToSend) {
105 EXPECT_TRUE(IsAllowedToSend(kDummyPermissionOne, GURL(kDummyOriginOne)));
106
107 int reports_to_send = kMaximumReportsPerOriginPerPermissionPerMinute;
108 while (reports_to_send--)
109 permission_reporter_->SendReport(GURL(kDummyOriginOne), kDummyPermissionOne,
110 kDummyAction);
111
112 EXPECT_FALSE(IsAllowedToSend(kDummyPermissionOne, GURL(kDummyOriginOne)));
113 EXPECT_TRUE(IsAllowedToSend(kDummyPermissionTwo, GURL(kDummyOriginOne)));
114 EXPECT_TRUE(IsAllowedToSend(kDummyPermissionOne, GURL(kDummyOriginTwo)));
115
116 clock_->Advance(base::TimeDelta::FromMinutes(1));
117 EXPECT_FALSE(IsAllowedToSend(kDummyPermissionOne, GURL(kDummyOriginOne)));
118
119 clock_->Advance(base::TimeDelta::FromMicroseconds(1));
120 EXPECT_TRUE(IsAllowedToSend(kDummyPermissionOne, GURL(kDummyOriginOne)));
raymes 2016/06/29 07:09:17 Instead of calling IsAllowedToSend directly, I thi
stefanocs 2016/06/30 00:24:51 Done.
121 }
122
85 } // namespace safe_browsing 123 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698