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

Side by Side Diff: net/reporting/reporting_header_parser_unittest.cc

Issue 2751103002: Reporting: Wrap existing classes in context. (Closed)
Patch Set: rebase Created 3 years, 8 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 unified diff | Download patch
« no previous file with comments | « net/reporting/reporting_header_parser.cc ('k') | net/reporting/reporting_policy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/reporting/reporting_header_parser.h" 5 #include "net/reporting/reporting_header_parser.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/test/simple_test_tick_clock.h" 11 #include "base/test/simple_test_tick_clock.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "net/reporting/reporting_cache.h" 14 #include "net/reporting/reporting_cache.h"
15 #include "net/reporting/reporting_client.h" 15 #include "net/reporting/reporting_client.h"
16 #include "net/reporting/reporting_test_util.h" 16 #include "net/reporting/reporting_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h" 18 #include "url/gurl.h"
19 #include "url/origin.h" 19 #include "url/origin.h"
20 20
21 namespace net { 21 namespace net {
22 namespace { 22 namespace {
23 23
24 class ReportingHeaderParserTest : public ::testing::Test { 24 class ReportingHeaderParserTest : public ReportingTestBase {
25 protected: 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"); 26 const GURL kUrl_ = GURL("https://origin/path");
32 const url::Origin kOrigin_ = url::Origin(GURL("https://origin/")); 27 const url::Origin kOrigin_ = url::Origin(GURL("https://origin/"));
33 const GURL kEndpoint_ = GURL("https://endpoint/"); 28 const GURL kEndpoint_ = GURL("https://endpoint/");
34 const std::string kGroup_ = "group"; 29 const std::string kGroup_ = "group";
35 const std::string kType_ = "type"; 30 const std::string kType_ = "type";
36
37 base::SimpleTestTickClock clock_;
38 ReportingCache cache_;
39 }; 31 };
40 32
41 TEST_F(ReportingHeaderParserTest, Invalid) { 33 TEST_F(ReportingHeaderParserTest, Invalid) {
42 static const struct { 34 static const struct {
43 const char* header_value; 35 const char* header_value;
44 const char* description; 36 const char* description;
45 } kInvalidHeaderTestCases[] = { 37 } kInvalidHeaderTestCases[] = {
46 {"{\"max-age\":1}", "missing url"}, 38 {"{\"max-age\":1}", "missing url"},
47 {"{\"url\":0,\"max-age\":1}", "non-string url"}, 39 {"{\"url\":0,\"max-age\":1}", "non-string url"},
48 {"{\"url\":\"http://insecure/\",\"max-age\":1}", "insecure url"}, 40 {"{\"url\":\"http://insecure/\",\"max-age\":1}", "insecure url"},
49 41
50 {"{\"url\":\"https://endpoint/\"}", "missing max-age"}, 42 {"{\"url\":\"https://endpoint/\"}", "missing max-age"},
51 {"{\"url\":\"https://endpoint/\",\"max-age\":\"\"}", 43 {"{\"url\":\"https://endpoint/\",\"max-age\":\"\"}",
52 "non-integer max-age"}, 44 "non-integer max-age"},
53 {"{\"url\":\"https://endpoint/\",\"max-age\":-1}", "negative max-age"}, 45 {"{\"url\":\"https://endpoint/\",\"max-age\":-1}", "negative max-age"},
54 46
55 {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"group\":0}", 47 {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"group\":0}",
56 "non-string group"}, 48 "non-string group"},
57 49
58 // Note that a non-boolean includeSubdomains field is *not* invalid, per 50 // Note that a non-boolean includeSubdomains field is *not* invalid, per
59 // the spec. 51 // the spec.
60 52
61 {"[{\"url\":\"https://a/\",\"max-age\":1}," 53 {"[{\"url\":\"https://a/\",\"max-age\":1},"
62 "{\"url\":\"https://b/\",\"max-age\":1}]", 54 "{\"url\":\"https://b/\",\"max-age\":1}]",
63 "wrapped in list"}}; 55 "wrapped in list"}};
64 56
65 for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) { 57 for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) {
66 auto& test_case = kInvalidHeaderTestCases[i]; 58 auto& test_case = kInvalidHeaderTestCases[i];
67 ParseHeader(kUrl_, test_case.header_value); 59 ReportingHeaderParser::ParseHeader(context(), kUrl_,
60 test_case.header_value);
68 61
69 std::vector<const ReportingClient*> clients; 62 std::vector<const ReportingClient*> clients;
70 cache_.GetClients(&clients); 63 cache()->GetClients(&clients);
71 EXPECT_TRUE(clients.empty()) 64 EXPECT_TRUE(clients.empty())
72 << "Invalid Report-To header (" << test_case.description << ": \"" 65 << "Invalid Report-To header (" << test_case.description << ": \""
73 << test_case.header_value << "\") parsed as valid."; 66 << test_case.header_value << "\") parsed as valid.";
74 } 67 }
75 } 68 }
76 69
77 TEST_F(ReportingHeaderParserTest, Valid) { 70 TEST_F(ReportingHeaderParserTest, Valid) {
78 ParseHeader(kUrl_, 71 ReportingHeaderParser::ParseHeader(
79 "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":86400}"); 72 context(), kUrl_,
73 "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":86400}");
80 74
81 const ReportingClient* client = 75 const ReportingClient* client =
82 FindClientInCache(&cache_, kOrigin_, kEndpoint_); 76 FindClientInCache(cache(), kOrigin_, kEndpoint_);
83 ASSERT_TRUE(client); 77 ASSERT_TRUE(client);
84 EXPECT_EQ(kOrigin_, client->origin); 78 EXPECT_EQ(kOrigin_, client->origin);
85 EXPECT_EQ(kEndpoint_, client->endpoint); 79 EXPECT_EQ(kEndpoint_, client->endpoint);
86 EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, client->subdomains); 80 EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, client->subdomains);
87 EXPECT_EQ(86400, (client->expires - clock_.NowTicks()).InSeconds()); 81 EXPECT_EQ(86400, (client->expires - tick_clock()->NowTicks()).InSeconds());
88 } 82 }
89 83
90 TEST_F(ReportingHeaderParserTest, Subdomains) { 84 TEST_F(ReportingHeaderParserTest, Subdomains) {
91 ParseHeader(kUrl_, "{\"url\":\"" + kEndpoint_.spec() + 85 ReportingHeaderParser::ParseHeader(context(), kUrl_,
92 "\",\"max-age\":86400," 86 "{\"url\":\"" + kEndpoint_.spec() +
93 "\"includeSubdomains\":true}"); 87 "\",\"max-age\":86400,"
88 "\"includeSubdomains\":true}");
94 89
95 const ReportingClient* client = 90 const ReportingClient* client =
96 FindClientInCache(&cache_, kOrigin_, kEndpoint_); 91 FindClientInCache(cache(), kOrigin_, kEndpoint_);
97 ASSERT_TRUE(client); 92 ASSERT_TRUE(client);
98 EXPECT_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains); 93 EXPECT_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains);
99 } 94 }
100 95
101 TEST_F(ReportingHeaderParserTest, ZeroMaxAge) { 96 TEST_F(ReportingHeaderParserTest, ZeroMaxAge) {
102 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, 97 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE,
103 kGroup_, clock_.NowTicks() + base::TimeDelta::FromDays(1)); 98 kGroup_,
99 tick_clock()->NowTicks() + base::TimeDelta::FromDays(1));
104 100
105 ParseHeader(kUrl_, "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":0}"); 101 ReportingHeaderParser::ParseHeader(
102 context(), kUrl_,
103 "{\"url\":\"" + kEndpoint_.spec() + "\",\"max-age\":0}");
106 104
107 EXPECT_EQ(nullptr, FindClientInCache(&cache_, kOrigin_, kEndpoint_)); 105 EXPECT_EQ(nullptr, FindClientInCache(cache(), kOrigin_, kEndpoint_));
108 } 106 }
109 107
110 } // namespace 108 } // namespace
111 } // namespace net 109 } // namespace net
OLDNEW
« no previous file with comments | « net/reporting/reporting_header_parser.cc ('k') | net/reporting/reporting_policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698