Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "net/reporting/reporting_header_parser.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/test/simple_test_tick_clock.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "base/values.h" | |
| 14 #include "net/reporting/reporting_cache.h" | |
| 15 #include "net/reporting/reporting_client.h" | |
| 16 #include "net/reporting/reporting_test_util.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 #include "url/gurl.h" | |
| 19 #include "url/origin.h" | |
| 20 | |
| 21 namespace net { | |
| 22 namespace { | |
| 23 | |
| 24 class ReportingHeaderParserTest : public ::testing::Test { | |
| 25 protected: | |
| 26 void ParseHeader(const GURL& url, const std::string& header_value) { | |
| 27 ReportingHeaderParser::ParseHeader(&cache_, clock_.NowTicks(), url, | |
| 28 header_value); | |
| 29 } | |
| 30 | |
| 31 const GURL kUrl_ = GURL("https://origin/path"); | |
| 32 const url::Origin kOrigin_ = url::Origin(GURL("https://origin/")); | |
| 33 const GURL kEndpoint_ = GURL("https://endpoint/"); | |
| 34 const std::string kGroup_ = "group"; | |
| 35 const std::string kType_ = "type"; | |
| 36 | |
| 37 base::SimpleTestTickClock clock_; | |
| 38 ReportingCache cache_; | |
| 39 }; | |
| 40 | |
| 41 TEST_F(ReportingHeaderParserTest, Invalid) { | |
| 42 static const struct { | |
| 43 const char* header_value; | |
| 44 const char* description; | |
| 45 } kInvalidHeaderTestCases[] = { | |
| 46 {"{\"max-age\":1}", "missing url"}, | |
| 47 {"{\"url\":0,\"max-age\":1}", "non-string url"}, | |
| 48 {"{\"url\":\"http://insecure/\",\"max-age\":1}", "insecure url"}, | |
| 49 | |
| 50 {"{\"url\":\"https://endpoint/\"}", "missing max-age"}, | |
| 51 {"{\"url\":\"https://endpoint/\",\"max-age\":\"\"}", | |
| 52 "non-integer max-age"}, | |
| 53 {"{\"url\":\"https://endpoint/\",\"max-age\":-1}", "negative max-age"}, | |
| 54 | |
| 55 {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"group\":0}", | |
| 56 "non-string group"}, | |
|
jkarlin
2017/03/29 11:30:05
I think we should check the invalid case that the
Julia Tuttle
2017/03/29 16:14:53
Oh, good call.
| |
| 57 | |
| 58 // Note that a non-boolean includeSubdomains field is *not* invalid, per | |
| 59 // the spec. | |
| 60 }; | |
| 61 | |
| 62 for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) { | |
| 63 auto& test_case = kInvalidHeaderTestCases[i]; | |
| 64 ParseHeader(kUrl_, test_case.header_value); | |
| 65 | |
| 66 std::vector<const ReportingClient*> clients; | |
| 67 cache_.GetClients(&clients); | |
| 68 EXPECT_TRUE(clients.empty()) | |
| 69 << "Invalid Report-To header (" << test_case.description << ": \"" | |
| 70 << test_case.header_value << "\") parsed as valid."; | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 TEST_F(ReportingHeaderParserTest, Valid) { | |
| 75 ParseHeader(kUrl_, | |
| 76 "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":86400}"); | |
| 77 | |
| 78 const ReportingClient* client = | |
| 79 FindClientInCache(&cache_, kOrigin_, kEndpoint_); | |
| 80 ASSERT_TRUE(client); | |
| 81 EXPECT_EQ(kOrigin_, client->origin); | |
| 82 EXPECT_EQ(kEndpoint_, client->endpoint); | |
| 83 EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, client->subdomains); | |
| 84 EXPECT_EQ(86400, (client->expires - clock_.NowTicks()).InSeconds()); | |
| 85 } | |
| 86 | |
| 87 TEST_F(ReportingHeaderParserTest, Subdomains) { | |
| 88 ParseHeader(kUrl_, "{\"url\":\"" + kEndpoint_.spec() + | |
| 89 "\",\"max-age\":86400," | |
| 90 "\"includeSubdomains\":true}"); | |
| 91 | |
| 92 const ReportingClient* client = | |
| 93 FindClientInCache(&cache_, kOrigin_, kEndpoint_); | |
| 94 ASSERT_TRUE(client); | |
| 95 EXPECT_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains); | |
| 96 } | |
| 97 | |
| 98 TEST_F(ReportingHeaderParserTest, ZeroMaxAge) { | |
| 99 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 100 kGroup_, clock_.NowTicks() + base::TimeDelta::FromDays(1)); | |
| 101 | |
| 102 ParseHeader(kUrl_, "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":0}"); | |
| 103 | |
| 104 EXPECT_EQ(nullptr, FindClientInCache(&cache_, kOrigin_, kEndpoint_)); | |
| 105 } | |
| 106 | |
| 107 } // namespace | |
| 108 } // namespace net | |
| OLD | NEW |