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..b6528d0570e15cf8962f1f2ebd286e1c22174bdd |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/permission_reporter_unittest.cc |
| @@ -0,0 +1,84 @@ |
| +// 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 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); |
| +}; |
| + |
| +} // namespace |
| + |
| +class PermissionReporterTest : public ::testing::Test { |
| + protected: |
| + PermissionReporterTest() |
| + : mock_report_sender_(new MockReportSender()), |
| + permission_reporter_( |
| + new PermissionReporter(base::WrapUnique(mock_report_sender_))) {} |
|
raymes
2016/06/09 06:03:47
What I mean is, can we replace mock_report_sender_
stefanocs
2016/06/09 06:41:29
I was trying to do this earlier but I figured the
raymes
2016/06/09 06:43:41
Oh sorry, I understand now. This seems ok to me. S
stefanocs
2016/06/09 07:50:45
Acknowledged.
|
| + |
|
raymes
2016/06/09 06:43:41
Add a comment about the ownership of this:
// Owne
stefanocs
2016/06/09 07:50:45
Done.
|
| + MockReportSender* mock_report_sender_; |
| + |
| + std::unique_ptr<PermissionReporter> permission_reporter_; |
| +}; |
| + |
| +// Test that PermissionReporter::SendReport sends a serialized report string to |
| +// SafeBrowsing CSD servers. |
| +TEST_F(PermissionReporterTest, SendReport) { |
| + 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 safe_browsing |