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"}, |
| 57 |
| 58 // Note that a non-boolean includeSubdomains field is *not* invalid, per |
| 59 // the spec. |
| 60 |
| 61 {"[{\"url\":\"https://a/\",\"max-age\":1}," |
| 62 "{\"url\":\"https://b/\",\"max-age\":1}]", |
| 63 "wrapped in list"}}; |
| 64 |
| 65 for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) { |
| 66 auto& test_case = kInvalidHeaderTestCases[i]; |
| 67 ParseHeader(kUrl_, test_case.header_value); |
| 68 |
| 69 std::vector<const ReportingClient*> clients; |
| 70 cache_.GetClients(&clients); |
| 71 EXPECT_TRUE(clients.empty()) |
| 72 << "Invalid Report-To header (" << test_case.description << ": \"" |
| 73 << test_case.header_value << "\") parsed as valid."; |
| 74 } |
| 75 } |
| 76 |
| 77 TEST_F(ReportingHeaderParserTest, Valid) { |
| 78 ParseHeader(kUrl_, |
| 79 "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":86400}"); |
| 80 |
| 81 const ReportingClient* client = |
| 82 FindClientInCache(&cache_, kOrigin_, kEndpoint_); |
| 83 ASSERT_TRUE(client); |
| 84 EXPECT_EQ(kOrigin_, client->origin); |
| 85 EXPECT_EQ(kEndpoint_, client->endpoint); |
| 86 EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, client->subdomains); |
| 87 EXPECT_EQ(86400, (client->expires - clock_.NowTicks()).InSeconds()); |
| 88 } |
| 89 |
| 90 TEST_F(ReportingHeaderParserTest, Subdomains) { |
| 91 ParseHeader(kUrl_, "{\"url\":\"" + kEndpoint_.spec() + |
| 92 "\",\"max-age\":86400," |
| 93 "\"includeSubdomains\":true}"); |
| 94 |
| 95 const ReportingClient* client = |
| 96 FindClientInCache(&cache_, kOrigin_, kEndpoint_); |
| 97 ASSERT_TRUE(client); |
| 98 EXPECT_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains); |
| 99 } |
| 100 |
| 101 TEST_F(ReportingHeaderParserTest, ZeroMaxAge) { |
| 102 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 103 kGroup_, clock_.NowTicks() + base::TimeDelta::FromDays(1)); |
| 104 |
| 105 ParseHeader(kUrl_, "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":0}"); |
| 106 |
| 107 EXPECT_EQ(nullptr, FindClientInCache(&cache_, kOrigin_, kEndpoint_)); |
| 108 } |
| 109 |
| 110 } // namespace |
| 111 } // namespace net |
OLD | NEW |