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_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" |
| 18 #include "url/origin.h" |
| 19 |
| 20 namespace net { |
| 21 namespace { |
| 22 |
| 23 using Client = ReportingCache::Client; |
| 24 |
| 25 const GURL kUrl("https://origin/path"); |
| 26 const url::Origin kOrigin(GURL("https://origin/")); |
| 27 const GURL kEndpoint("https://endpoint/"); |
| 28 const std::string kGroup("group"); |
| 29 const std::string kType("type"); |
| 30 |
| 31 class ReportingHeaderParserTest : public ::testing::Test { |
| 32 protected: |
| 33 void ParseHeader(const GURL& url, const std::string& header_value) { |
| 34 ReportingHeaderParser::ParseHeader(&cache_, clock_.NowTicks(), url, |
| 35 header_value); |
| 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 {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"includeSubdomains\":0}", |
| 59 "non-boolean includeSubdomains"}, |
| 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 Client*> 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, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":86400}"); |
| 76 |
| 77 const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint); |
| 78 ASSERT_TRUE(client); |
| 79 EXPECT_EQ(kOrigin, client->origin); |
| 80 EXPECT_EQ(kEndpoint, client->endpoint); |
| 81 EXPECT_FALSE(client->subdomains); |
| 82 EXPECT_EQ(86400, (client->expires - clock_.NowTicks()).InSeconds()); |
| 83 } |
| 84 |
| 85 TEST_F(ReportingHeaderParserTest, Subdomains) { |
| 86 ParseHeader(kUrl, "{\"url\":\"" + kEndpoint.spec() + |
| 87 "\",\"max-age\":86400," |
| 88 "\"includeSubdomains\":true}"); |
| 89 |
| 90 const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint); |
| 91 ASSERT_TRUE(client); |
| 92 EXPECT_TRUE(client->subdomains); |
| 93 } |
| 94 |
| 95 TEST_F(ReportingHeaderParserTest, ZeroMaxAge) { |
| 96 cache_.SetClient(kOrigin, kEndpoint, false, kGroup, |
| 97 clock_.NowTicks() + base::TimeDelta::FromDays(1)); |
| 98 |
| 99 ParseHeader(kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":0}"); |
| 100 |
| 101 EXPECT_FALSE(FindClientInCache(&cache_, kOrigin, kEndpoint) != nullptr); |
| 102 } |
| 103 |
| 104 } // namespace |
| 105 } // namespace net |
OLD | NEW |