forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporting_header_parser_unittest.cc
181 lines (147 loc) · 6.55 KB
/
reporting_header_parser_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/reporting/reporting_header_parser.h"
#include <string>
#include <vector>
#include "base/json/json_reader.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_client.h"
#include "net/reporting/reporting_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net {
namespace {
class ReportingHeaderParserTest : public ReportingTestBase {
protected:
void ParseHeader(const GURL& url, const std::string& json) {
std::unique_ptr<base::Value> value =
base::JSONReader::Read("[" + json + "]");
if (value)
ReportingHeaderParser::ParseHeader(context(), url, std::move(value));
}
const GURL kUrl_ = GURL("https://origin/path");
const url::Origin kOrigin_ = url::Origin::Create(GURL("https://origin/"));
const GURL kEndpoint_ = GURL("https://endpoint/");
const std::string kGroup_ = "group";
const std::string kType_ = "type";
};
// TODO(juliatuttle): Ideally these tests should be expecting that JSON parsing
// (and therefore header parsing) may happen asynchronously, but the entire
// pipeline is also tested by NetworkErrorLoggingEndToEndTest.
TEST_F(ReportingHeaderParserTest, Invalid) {
static const struct {
const char* header_value;
const char* description;
} kInvalidHeaderTestCases[] = {
{"{\"max_age\":1, \"endpoints\": [{}]}", "missing url"},
{"{\"max_age\":1, \"endpoints\": [{\"url\":0}]}", "non-string url"},
{"{\"max_age\":1, \"endpoints\": [{\"url\":\"http://insecure/\"}]}",
"insecure url"},
{"{\"endpoints\": [{\"url\":\"https://endpoint/\"}]}", "missing max_age"},
{"{\"max_age\":\"\", \"endpoints\": [{\"url\":\"https://endpoint/\"}]}",
"non-integer max_age"},
{"{\"max_age\":-1, \"endpoints\": [{\"url\":\"https://endpoint/\"}]}",
"negative max_age"},
{"{\"max_age\":1, \"group\":0, "
"\"endpoints\": [{\"url\":\"https://endpoint/\"}]}",
"non-string group"},
// Note that a non-boolean include_subdomains field is *not* invalid, per
// the spec.
{"{\"max_age\":1, "
"\"endpoints\": [{\"url\":\"https://endpoint/\",\"priority\":\"\"}]}",
"non-integer priority"},
{"{\"max_age\":1, "
"\"endpoints\": [{\"url\":\"https://endpoint/\",\"weight\":\"\"}]}",
"non-integer weight"},
{"{\"max_age\":1, "
"\"endpoints\": [{\"url\":\"https://endpoint/\",\"weight\":-1}]}",
"negative weight"},
{"{\"max_age\":1, "
"\"endpoints\": [{\"url\":\"https://endpoint/\",\"weight\":0}]}",
"zero weight"},
{"[{\"max_age\":1, \"endpoints\": [{\"url\":\"https://a/\"}]},"
"{\"max_age\":1, \"endpoints\": [{\"url\":\"https://b/\"}]}]",
"wrapped in list"}};
for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) {
auto& test_case = kInvalidHeaderTestCases[i];
ParseHeader(kUrl_, test_case.header_value);
std::vector<const ReportingClient*> clients;
cache()->GetClients(&clients);
EXPECT_TRUE(clients.empty())
<< "Invalid Report-To header (" << test_case.description << ": \""
<< test_case.header_value << "\") parsed as valid.";
}
}
TEST_F(ReportingHeaderParserTest, Valid) {
ParseHeader(kUrl_, "{\"endpoints\": [{\"url\":\"" + kEndpoint_.spec() +
"\"}],\"max_age\":86400}");
const ReportingClient* client =
FindClientInCache(cache(), kOrigin_, kEndpoint_);
ASSERT_TRUE(client);
EXPECT_EQ(kOrigin_, client->origin);
EXPECT_EQ(kEndpoint_, client->endpoint);
EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, client->subdomains);
EXPECT_EQ(86400, (client->expires - tick_clock()->NowTicks()).InSeconds());
EXPECT_EQ(ReportingClient::kDefaultPriority, client->priority);
EXPECT_EQ(ReportingClient::kDefaultWeight, client->weight);
}
TEST_F(ReportingHeaderParserTest, ZeroMaxAge) {
cache()->SetClient(
kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(1),
ReportingClient::kDefaultPriority, ReportingClient::kDefaultWeight);
ParseHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\"}],\"max_age\":0}");
EXPECT_EQ(nullptr, FindClientInCache(cache(), kOrigin_, kEndpoint_));
}
TEST_F(ReportingHeaderParserTest, Subdomains) {
ParseHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\"}],\"max_age\":86400,"
"\"include_subdomains\":true}");
const ReportingClient* client =
FindClientInCache(cache(), kOrigin_, kEndpoint_);
ASSERT_TRUE(client);
EXPECT_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains);
}
TEST_F(ReportingHeaderParserTest, PriorityPositive) {
ParseHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\",\"priority\":2}],\"max_age\":86400}");
const ReportingClient* client =
FindClientInCache(cache(), kOrigin_, kEndpoint_);
ASSERT_TRUE(client);
EXPECT_EQ(2, client->priority);
}
TEST_F(ReportingHeaderParserTest, PriorityNegative) {
ParseHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\",\"priority\":-2}],\"max_age\":86400}");
const ReportingClient* client =
FindClientInCache(cache(), kOrigin_, kEndpoint_);
ASSERT_TRUE(client);
EXPECT_EQ(-2, client->priority);
}
TEST_F(ReportingHeaderParserTest, Weight) {
ParseHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\",\"weight\":3}],\"max_age\":86400}");
const ReportingClient* client =
FindClientInCache(cache(), kOrigin_, kEndpoint_);
ASSERT_TRUE(client);
EXPECT_EQ(3, client->weight);
}
TEST_F(ReportingHeaderParserTest, RemoveOld) {
static const GURL kDifferentEndpoint_ = GURL("https://endpoint2/");
ParseHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\"}],\"max_age\":86400}");
EXPECT_TRUE(FindClientInCache(cache(), kOrigin_, kEndpoint_));
ParseHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" +
kDifferentEndpoint_.spec() +
"\"}],\"max_age\":86400}");
EXPECT_FALSE(FindClientInCache(cache(), kOrigin_, kEndpoint_));
EXPECT_TRUE(FindClientInCache(cache(), kOrigin_, kDifferentEndpoint_));
}
} // namespace
} // namespace net