| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/permission_reporter.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/common/safe_browsing/permission_report.pb.h" |
| 9 #include "content/public/browser/permission_type.h" |
| 10 #include "net/url_request/report_sender.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using content::PermissionType; |
| 14 |
| 15 namespace safe_browsing { |
| 16 |
| 17 namespace { |
| 18 // URL to upload permission action reports. |
| 19 const char kPermissionActionReportingUploadUrl[] = |
| 20 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
| 21 "permission-action"; |
| 22 |
| 23 const char kDummyOrigin[] = "http://example.test/"; |
| 24 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; |
| 25 const PermissionAction kDummyAction = GRANTED; |
| 26 const PermissionReport::PermissionType kDummyPermissionReportPermission = |
| 27 PermissionReport::GEOLOCATION; |
| 28 const PermissionReport::Action kDummyPermissionReportAction = |
| 29 PermissionReport::GRANTED; |
| 30 |
| 31 // A mock ReportSender that keeps track of the last report sent. |
| 32 class MockReportSender : public net::ReportSender { |
| 33 public: |
| 34 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} |
| 35 ~MockReportSender() override {} |
| 36 |
| 37 void Send(const GURL& report_uri, const std::string& report) override { |
| 38 latest_report_uri_ = report_uri; |
| 39 latest_report_ = report; |
| 40 } |
| 41 |
| 42 const GURL& latest_report_uri() { return latest_report_uri_; } |
| 43 |
| 44 const std::string& latest_report() { return latest_report_; } |
| 45 |
| 46 private: |
| 47 GURL latest_report_uri_; |
| 48 std::string latest_report_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(MockReportSender); |
| 51 }; |
| 52 |
| 53 } // namespace |
| 54 |
| 55 class PermissionReporterTest : public ::testing::Test { |
| 56 protected: |
| 57 PermissionReporterTest() |
| 58 : mock_report_sender_(new MockReportSender()), |
| 59 permission_reporter_( |
| 60 new PermissionReporter(base::WrapUnique(mock_report_sender_))) {} |
| 61 |
| 62 // Owned by |permission_reporter_|. |
| 63 MockReportSender* mock_report_sender_; |
| 64 |
| 65 std::unique_ptr<PermissionReporter> permission_reporter_; |
| 66 }; |
| 67 |
| 68 // Test that PermissionReporter::SendReport sends a serialized report string to |
| 69 // SafeBrowsing CSD servers. |
| 70 TEST_F(PermissionReporterTest, SendReport) { |
| 71 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, |
| 72 kDummyAction); |
| 73 |
| 74 PermissionReport permission_report; |
| 75 ASSERT_TRUE( |
| 76 permission_report.ParseFromString(mock_report_sender_->latest_report())); |
| 77 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); |
| 78 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); |
| 79 EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
| 80 |
| 81 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), |
| 82 mock_report_sender_->latest_report_uri()); |
| 83 } |
| 84 |
| 85 } // namespace safe_browsing |
| OLD | NEW |