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

Unified Diff: net/reporting/reporting_header_parser_unittest.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: Extract header parser and make static 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_header_parser_unittest.cc
diff --git a/net/reporting/reporting_header_parser_unittest.cc b/net/reporting/reporting_header_parser_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f3f991dcc782216b3278476f839d1b10f88f8b8b
--- /dev/null
+++ b/net/reporting/reporting_header_parser_unittest.cc
@@ -0,0 +1,105 @@
+// 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_header_parser.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;
+
+const GURL kUrl("https://origin/path");
+const url::Origin kOrigin(GURL("https://origin/"));
+const GURL kEndpoint("https://endpoint/");
+const std::string kGroup("group");
+const std::string kType("type");
+
+class ReportingHeaderParserTest : public ::testing::Test {
+ protected:
+ void ParseHeader(const GURL& url, const std::string& header_value) {
+ ReportingHeaderParser::ParseHeader(&cache_, clock_.NowTicks(), url,
+ header_value);
+ }
+ base::SimpleTestTickClock clock_;
+ ReportingCache cache_;
+};
+
+TEST_F(ReportingHeaderParserTest, Invalid) {
+ 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];
+ ParseHeader(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(ReportingHeaderParserTest, Valid) {
+ ParseHeader(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(ReportingHeaderParserTest, Subdomains) {
+ ParseHeader(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(ReportingHeaderParserTest, ZeroMaxAge) {
+ cache_.SetClient(kOrigin, kEndpoint, false, kGroup,
+ clock_.NowTicks() + base::TimeDelta::FromDays(1));
+
+ ParseHeader(kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":0}");
+
+ EXPECT_FALSE(FindClientInCache(&cache_, kOrigin, kEndpoint) != nullptr);
+}
+
+} // namespace
+} // namespace net
« net/reporting/reporting_header_parser.h ('K') | « net/reporting/reporting_header_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698