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 const GURL kUrl("https://origin/path"); | |
| 25 const url::Origin kOrigin(GURL("https://origin/")); | |
| 26 const GURL kEndpoint("https://endpoint/"); | |
| 27 const std::string kGroup("group"); | |
| 28 const std::string kType("type"); | |
|
jkarlin
2017/03/28 18:29:50
All of above need to be POD
Julia Tuttle
2017/03/28 20:28:55
Moved into class instead.
| |
| 29 | |
| 30 class ReportingHeaderParserTest : public ::testing::Test { | |
| 31 protected: | |
| 32 void ParseHeader(const GURL& url, const std::string& header_value) { | |
| 33 ReportingHeaderParser::ParseHeader(&cache_, clock_.NowTicks(), url, | |
| 34 header_value); | |
| 35 } | |
| 36 base::SimpleTestTickClock clock_; | |
| 37 ReportingCache cache_; | |
| 38 }; | |
| 39 | |
| 40 TEST_F(ReportingHeaderParserTest, Invalid) { | |
| 41 static const struct { | |
| 42 const char* header_value; | |
| 43 const char* description; | |
| 44 } kInvalidHeaderTestCases[] = { | |
| 45 {"{\"max-age\":1}", "missing url"}, | |
| 46 {"{\"url\":0,\"max-age\":1}", "non-string url"}, | |
| 47 {"{\"url\":\"http://insecure/\",\"max-age\":1}", "insecure url"}, | |
| 48 | |
| 49 {"{\"url\":\"https://endpoint/\"}", "missing max-age"}, | |
| 50 {"{\"url\":\"https://endpoint/\",\"max-age\":\"\"}", | |
| 51 "non-integer max-age"}, | |
| 52 {"{\"url\":\"https://endpoint/\",\"max-age\":-1}", "negative max-age"}, | |
| 53 | |
| 54 {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"group\":0}", | |
| 55 "non-string group"}, | |
| 56 | |
| 57 // Note that a non-boolean includeSubdomains field is *not* invalid, per | |
| 58 // the spec. | |
| 59 }; | |
| 60 | |
| 61 for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) { | |
| 62 auto& test_case = kInvalidHeaderTestCases[i]; | |
| 63 ParseHeader(kUrl, test_case.header_value); | |
| 64 | |
| 65 std::vector<const ReportingClient*> clients; | |
| 66 cache_.GetClients(&clients); | |
| 67 EXPECT_TRUE(clients.empty()) | |
| 68 << "Invalid Report-To header (" << test_case.description << ": \"" | |
| 69 << test_case.header_value << "\") parsed as valid."; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 TEST_F(ReportingHeaderParserTest, Valid) { | |
| 74 ParseHeader(kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":86400}"); | |
| 75 | |
| 76 const ReportingClient* client = | |
| 77 FindClientInCache(&cache_, kOrigin, kEndpoint); | |
| 78 ASSERT_TRUE(client); | |
| 79 EXPECT_EQ(kOrigin, client->origin); | |
| 80 EXPECT_EQ(kEndpoint, client->endpoint); | |
| 81 EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, 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 ReportingClient* client = | |
| 91 FindClientInCache(&cache_, kOrigin, kEndpoint); | |
| 92 ASSERT_TRUE(client); | |
| 93 EXPECT_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains); | |
| 94 } | |
| 95 | |
| 96 TEST_F(ReportingHeaderParserTest, ZeroMaxAge) { | |
| 97 cache_.SetClient(kOrigin, kEndpoint, ReportingClient::Subdomains::EXCLUDE, | |
| 98 kGroup, clock_.NowTicks() + base::TimeDelta::FromDays(1)); | |
| 99 | |
| 100 ParseHeader(kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":0}"); | |
| 101 | |
| 102 EXPECT_FALSE(FindClientInCache(&cache_, kOrigin, kEndpoint) != nullptr); | |
|
jkarlin
2017/03/28 18:29:50
EXPECT_EQ(nullptr, FindClientInCache(&cache_, kOri
Julia Tuttle
2017/03/28 20:28:55
Done.
| |
| 103 } | |
| 104 | |
| 105 } // namespace | |
| 106 } // namespace net | |
| OLD | NEW |