Chromium Code Reviews| 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 static const char kPermissionActionReportingUploadUrl[] = | |
| 20 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | |
| 21 "permission-action"; | |
| 22 | |
| 23 static const char kDummyOrigin[] = "http://example.test/"; | |
| 24 static const PermissionType kDummyPermission = PermissionType::GEOLOCATION; | |
| 25 static const PermissionAction kDummyAction = GRANTED; | |
| 26 static const PermissionReport::PermissionType kDummyProtobufPermission = | |
| 27 PermissionReport::GEOLOCATION; | |
| 28 static const PermissionReport::Action kDummyProtobufAction = | |
| 29 PermissionReport::GRANTED; | |
| 30 | |
| 31 // A mock ReportSender that keeps track of the last report | |
|
raymes
2016/06/09 01:30:41
nit : fill 80 chars
stefanocs
2016/06/09 05:56:12
Done.
| |
| 32 // sent. | |
| 33 class MockReportSender : public net::ReportSender { | |
| 34 public: | |
| 35 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | |
| 36 ~MockReportSender() override {} | |
| 37 | |
| 38 void Send(const GURL& report_uri, const std::string& report) override { | |
| 39 latest_report_uri_ = report_uri; | |
| 40 latest_report_ = report; | |
| 41 } | |
| 42 | |
| 43 const GURL& latest_report_uri() { return latest_report_uri_; } | |
| 44 | |
| 45 const std::string& latest_report() { return latest_report_; } | |
| 46 | |
| 47 private: | |
| 48 GURL latest_report_uri_; | |
| 49 std::string latest_report_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(MockReportSender); | |
| 52 }; | |
| 53 | |
| 54 class PermissionReporterTest : public ::testing::Test { | |
| 55 public: | |
| 56 PermissionReporterTest() {} | |
| 57 | |
| 58 ~PermissionReporterTest() override {} | |
|
raymes
2016/06/09 01:30:41
nit: You shouldn't need to define the constructor/
stefanocs
2016/06/09 05:56:12
Done.
| |
| 59 }; | |
| 60 | |
| 61 // Test that PermissionReporter::BuildReport returns a serialized permission | |
| 62 // report string | |
|
raymes
2016/06/09 01:30:41
nit: "." at end of line
stefanocs
2016/06/09 05:56:11
Done.
| |
| 63 TEST_F(PermissionReporterTest, BuildReport) { | |
| 64 std::string serialized_report; | |
| 65 PermissionReporter::BuildReport(GURL(kDummyOrigin), kDummyPermission, | |
| 66 kDummyAction, &serialized_report); | |
| 67 | |
| 68 PermissionReport permission_report; | |
| 69 ASSERT_TRUE(permission_report.ParseFromString(serialized_report)); | |
| 70 EXPECT_EQ(kDummyProtobufPermission, permission_report.permission()); | |
| 71 EXPECT_EQ(kDummyProtobufAction, permission_report.action()); | |
| 72 EXPECT_EQ(kDummyOrigin, permission_report.origin()); | |
| 73 } | |
|
raymes
2016/06/09 01:30:41
I actually don't think we need to test BuildReport
stefanocs
2016/06/09 05:56:11
Done.
| |
| 74 | |
| 75 // Test that PermissionReporter::SendReport sends a serialized report string to | |
| 76 // SafeBrowsing CSD servers. | |
| 77 TEST_F(PermissionReporterTest, SendReport) { | |
| 78 MockReportSender* mock_report_sender = new MockReportSender(); | |
|
raymes
2016/06/09 01:30:41
nit: inline this to avoid dealing with a raw point
stefanocs
2016/06/09 05:56:12
Do you mean it should be wrapped with unique_ptr?
| |
| 79 PermissionReporter permission_reporter(base::WrapUnique(mock_report_sender)); | |
| 80 permission_reporter.SendReport(GURL(kDummyOrigin), kDummyPermission, | |
| 81 kDummyAction); | |
| 82 | |
| 83 PermissionReport permission_report; | |
| 84 ASSERT_TRUE( | |
| 85 permission_report.ParseFromString(mock_report_sender->latest_report())); | |
| 86 EXPECT_EQ(kDummyProtobufPermission, permission_report.permission()); | |
| 87 EXPECT_EQ(kDummyProtobufAction, permission_report.action()); | |
| 88 EXPECT_EQ(kDummyOrigin, permission_report.origin()); | |
| 89 | |
| 90 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), | |
| 91 mock_report_sender->latest_report_uri()); | |
| 92 } | |
| 93 | |
| 94 } // namespace | |
| 95 | |
| 96 } // namespace safe_browsing | |
| OLD | NEW |