Chromium Code Reviews| Index: chrome/browser/safe_browsing/permission_reporter_unittest.cc |
| diff --git a/chrome/browser/safe_browsing/permission_reporter_unittest.cc b/chrome/browser/safe_browsing/permission_reporter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ca4abf072269b577ae6c9b1d8b97695fc838b4d4 |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/permission_reporter_unittest.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/safe_browsing/permission_reporter.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "chrome/common/safe_browsing/permission_report.pb.h" |
| +#include "content/public/browser/permission_type.h" |
| +#include "net/url_request/report_sender.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using content::PermissionType; |
| + |
| +namespace safe_browsing { |
| + |
| +namespace { |
| +// URL to upload permission action reports. |
| +static const char kPermissionActionReportingUploadUrl[] = |
| + "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
| + "permission-action"; |
| + |
| +static const char kDummyOrigin[] = "http://example.test/"; |
| +static const PermissionType kDummyPermission = PermissionType::GEOLOCATION; |
| +static const PermissionAction kDummyAction = GRANTED; |
| +static const PermissionReport::PermissionType kDummyProtobufPermission = |
| + PermissionReport::GEOLOCATION; |
| +static const PermissionReport::Action kDummyProtobufAction = |
| + PermissionReport::GRANTED; |
| + |
| +// 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.
|
| +// sent. |
| +class MockReportSender : public net::ReportSender { |
| + public: |
| + MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} |
| + ~MockReportSender() override {} |
| + |
| + void Send(const GURL& report_uri, const std::string& report) override { |
| + latest_report_uri_ = report_uri; |
| + latest_report_ = report; |
| + } |
| + |
| + const GURL& latest_report_uri() { return latest_report_uri_; } |
| + |
| + const std::string& latest_report() { return latest_report_; } |
| + |
| + private: |
| + GURL latest_report_uri_; |
| + std::string latest_report_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockReportSender); |
| +}; |
| + |
| +class PermissionReporterTest : public ::testing::Test { |
| + public: |
| + PermissionReporterTest() {} |
| + |
| + ~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.
|
| +}; |
| + |
| +// Test that PermissionReporter::BuildReport returns a serialized permission |
| +// report string |
|
raymes
2016/06/09 01:30:41
nit: "." at end of line
stefanocs
2016/06/09 05:56:11
Done.
|
| +TEST_F(PermissionReporterTest, BuildReport) { |
| + std::string serialized_report; |
| + PermissionReporter::BuildReport(GURL(kDummyOrigin), kDummyPermission, |
| + kDummyAction, &serialized_report); |
| + |
| + PermissionReport permission_report; |
| + ASSERT_TRUE(permission_report.ParseFromString(serialized_report)); |
| + EXPECT_EQ(kDummyProtobufPermission, permission_report.permission()); |
| + EXPECT_EQ(kDummyProtobufAction, permission_report.action()); |
| + EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
| +} |
|
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.
|
| + |
| +// Test that PermissionReporter::SendReport sends a serialized report string to |
| +// SafeBrowsing CSD servers. |
| +TEST_F(PermissionReporterTest, SendReport) { |
| + 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?
|
| + PermissionReporter permission_reporter(base::WrapUnique(mock_report_sender)); |
| + permission_reporter.SendReport(GURL(kDummyOrigin), kDummyPermission, |
| + kDummyAction); |
| + |
| + PermissionReport permission_report; |
| + ASSERT_TRUE( |
| + permission_report.ParseFromString(mock_report_sender->latest_report())); |
| + EXPECT_EQ(kDummyProtobufPermission, permission_report.permission()); |
| + EXPECT_EQ(kDummyProtobufAction, permission_report.action()); |
| + EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
| + |
| + EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), |
| + mock_report_sender->latest_report_uri()); |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace safe_browsing |