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

Unified Diff: net/reporting/reporting_delegate_impl_unittest.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: DISALLOW_COPY_AND_ASSIGN Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: net/reporting/reporting_delegate_impl_unittest.cc
diff --git a/net/reporting/reporting_delegate_impl_unittest.cc b/net/reporting/reporting_delegate_impl_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d57273eca033d3f0bab7dbebd467c02895e5eaf2
--- /dev/null
+++ b/net/reporting/reporting_delegate_impl_unittest.cc
@@ -0,0 +1,149 @@
+// Copyright 2017 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 "net/reporting/reporting_delegate_impl.h"
+
+#include <string>
+#include <vector>
+
+#include "base/memory/ptr_util.h"
+#include "base/test/simple_test_tick_clock.h"
+#include "base/time/time.h"
+#include "base/values.h"
+#include "net/reporting/reporting_cache.h"
+#include "net/reporting/reporting_test_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+#include "url/origin.h"
+
+namespace net {
+namespace {
+
+using Client = ReportingCache::Client;
+using Report = ReportingCache::Report;
+
+const GURL kUrl("https://origin/path");
+const GURL kSensitiveUrl("https://username:password@origin/path#ref");
+const url::Origin kOrigin(GURL("https://origin/"));
+const GURL kEndpoint("https://endpoint/");
+const std::string kGroup1("group1");
+const std::string kGroup2("group2");
+const std::string kType("type");
+
+class ReportingDelegateImplTest : public ::testing::Test {
+ protected:
+ ReportingDelegateImplTest() : delegate_(&clock_, &cache_) {}
+
+ base::SimpleTestTickClock clock_;
+ ReportingCache cache_;
+ ReportingDelegateImpl delegate_;
+};
+
+TEST_F(ReportingDelegateImplTest, OnReportGenerated) {
+ delegate_.OnReportGenerated(kUrl, kGroup1, kType,
+ base::MakeUnique<base::DictionaryValue>());
+
+ std::vector<const Report*> reports;
+ cache_.GetReports(&reports);
+ ASSERT_EQ(1u, reports.size());
+ const Report* report = reports[0];
+ ASSERT_TRUE(report);
+ EXPECT_EQ(kUrl, report->url);
+ EXPECT_EQ(kGroup1, report->group);
+ EXPECT_EQ(kType, report->type);
+ EXPECT_EQ(clock_.NowTicks(), report->queued);
+ EXPECT_EQ(0, report->attempts);
+ EXPECT_FALSE(report->pending);
+ EXPECT_FALSE(report->doomed);
+}
+
+TEST_F(ReportingDelegateImplTest, SanitizeUrl) {
+ ASSERT_TRUE(kSensitiveUrl.has_username());
+ ASSERT_TRUE(kSensitiveUrl.has_password());
+ ASSERT_TRUE(kSensitiveUrl.has_ref());
+
+ delegate_.OnReportGenerated(kSensitiveUrl, kGroup1, kType,
+ base::MakeUnique<base::DictionaryValue>());
+
+ std::vector<const Report*> reports;
+ cache_.GetReports(&reports);
+ ASSERT_EQ(1u, reports.size());
+ const Report* report = reports[0];
+ ASSERT_TRUE(report);
+ EXPECT_FALSE(report->url.has_username());
+ EXPECT_FALSE(report->url.has_password());
+ EXPECT_FALSE(report->url.has_ref());
+}
+
+TEST_F(ReportingDelegateImplTest, OnHeaderReceivedInvalid) {
+ static const struct {
+ const char* header_value;
+ const char* description;
+ } kInvalidHeaderTestCases[] = {
+ {"{\"max-age\":1}", "missing url"},
+ {"{\"url\":0,\"max-age\":1}", "non-string url"},
+ {"{\"url\":\"http://insecure/\",\"max-age\":1}", "insecure url"},
+
+ {"{\"url\":\"https://endpoint/\"}", "missing max-age"},
+ {"{\"url\":\"https://endpoint/\",\"max-age\":\"\"}",
+ "non-integer max-age"},
+ {"{\"url\":\"https://endpoint/\",\"max-age\":-1}", "negative max-age"},
+
+ {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"group\":0}",
+ "non-string group"},
+
+ {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"includeSubdomains\":0}",
+ "non-boolean includeSubdomains"},
+ };
+
+ for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) {
+ auto& test_case = kInvalidHeaderTestCases[i];
+ delegate_.OnHeaderReceived(kUrl, test_case.header_value);
+
+ std::vector<const Client*> clients;
+ cache_.GetClients(&clients);
+ EXPECT_TRUE(clients.empty())
+ << "Invalid Report-To header (" << test_case.description << ": \""
+ << test_case.header_value << "\") parsed as valid.";
+ }
+}
+
+TEST_F(ReportingDelegateImplTest, OnHeaderReceivedValid) {
+ delegate_.OnHeaderReceived(
+ kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":86400}");
+
+ const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint);
+ ASSERT_TRUE(client);
+ EXPECT_EQ(kOrigin, client->origin);
+ EXPECT_EQ(kEndpoint, client->endpoint);
+ EXPECT_FALSE(client->subdomains);
+ EXPECT_EQ(86400, (client->expires - clock_.NowTicks()).InSeconds());
+}
+
+TEST_F(ReportingDelegateImplTest, OnHeaderReceivedValidSubdomains) {
+ delegate_.OnHeaderReceived(
+ kUrl, "{\"url\":\"" + kEndpoint.spec() +
+ "\",\"max-age\":86400,\"includeSubdomains\":true}");
+
+ const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint);
+ ASSERT_TRUE(client);
+ EXPECT_TRUE(client->subdomains);
+}
+
+TEST_F(ReportingDelegateImplTest, OnHeaderReceivedValidZeroMaxAge) {
+ delegate_.OnHeaderReceived(
+ kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":86400}");
+
+ const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint);
+ ASSERT_TRUE(client);
+
+ delegate_.OnHeaderReceived(
+ kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":0}");
+
+ client = FindClientInCache(&cache_, kOrigin, kEndpoint);
+ EXPECT_FALSE(client);
+}
+
+} // namespace
+} // namespace net
« net/reporting/reporting_delegate_impl.h ('K') | « net/reporting/reporting_delegate_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698