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

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

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: DISALLOW_COPY_AND_ASSIGN Created 3 years, 9 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
OLDNEW
(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_delegate_impl.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 using Report = ReportingCache::Report;
25
26 const GURL kUrl("https://origin/path");
27 const GURL kSensitiveUrl("https://username:password@origin/path#ref");
28 const url::Origin kOrigin(GURL("https://origin/"));
29 const GURL kEndpoint("https://endpoint/");
30 const std::string kGroup1("group1");
31 const std::string kGroup2("group2");
32 const std::string kType("type");
33
34 class ReportingDelegateImplTest : public ::testing::Test {
35 protected:
36 ReportingDelegateImplTest() : delegate_(&clock_, &cache_) {}
37
38 base::SimpleTestTickClock clock_;
39 ReportingCache cache_;
40 ReportingDelegateImpl delegate_;
41 };
42
43 TEST_F(ReportingDelegateImplTest, OnReportGenerated) {
44 delegate_.OnReportGenerated(kUrl, kGroup1, kType,
45 base::MakeUnique<base::DictionaryValue>());
46
47 std::vector<const Report*> reports;
48 cache_.GetReports(&reports);
49 ASSERT_EQ(1u, reports.size());
50 const Report* report = reports[0];
51 ASSERT_TRUE(report);
52 EXPECT_EQ(kUrl, report->url);
53 EXPECT_EQ(kGroup1, report->group);
54 EXPECT_EQ(kType, report->type);
55 EXPECT_EQ(clock_.NowTicks(), report->queued);
56 EXPECT_EQ(0, report->attempts);
57 EXPECT_FALSE(report->pending);
58 EXPECT_FALSE(report->doomed);
59 }
60
61 TEST_F(ReportingDelegateImplTest, SanitizeUrl) {
62 ASSERT_TRUE(kSensitiveUrl.has_username());
63 ASSERT_TRUE(kSensitiveUrl.has_password());
64 ASSERT_TRUE(kSensitiveUrl.has_ref());
65
66 delegate_.OnReportGenerated(kSensitiveUrl, kGroup1, kType,
67 base::MakeUnique<base::DictionaryValue>());
68
69 std::vector<const Report*> reports;
70 cache_.GetReports(&reports);
71 ASSERT_EQ(1u, reports.size());
72 const Report* report = reports[0];
73 ASSERT_TRUE(report);
74 EXPECT_FALSE(report->url.has_username());
75 EXPECT_FALSE(report->url.has_password());
76 EXPECT_FALSE(report->url.has_ref());
77 }
78
79 TEST_F(ReportingDelegateImplTest, OnHeaderReceivedInvalid) {
80 static const struct {
81 const char* header_value;
82 const char* description;
83 } kInvalidHeaderTestCases[] = {
84 {"{\"max-age\":1}", "missing url"},
85 {"{\"url\":0,\"max-age\":1}", "non-string url"},
86 {"{\"url\":\"http://insecure/\",\"max-age\":1}", "insecure url"},
87
88 {"{\"url\":\"https://endpoint/\"}", "missing max-age"},
89 {"{\"url\":\"https://endpoint/\",\"max-age\":\"\"}",
90 "non-integer max-age"},
91 {"{\"url\":\"https://endpoint/\",\"max-age\":-1}", "negative max-age"},
92
93 {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"group\":0}",
94 "non-string group"},
95
96 {"{\"url\":\"https://endpoint/\",\"max-age\":1,\"includeSubdomains\":0}",
97 "non-boolean includeSubdomains"},
98 };
99
100 for (size_t i = 0; i < arraysize(kInvalidHeaderTestCases); ++i) {
101 auto& test_case = kInvalidHeaderTestCases[i];
102 delegate_.OnHeaderReceived(kUrl, test_case.header_value);
103
104 std::vector<const Client*> clients;
105 cache_.GetClients(&clients);
106 EXPECT_TRUE(clients.empty())
107 << "Invalid Report-To header (" << test_case.description << ": \""
108 << test_case.header_value << "\") parsed as valid.";
109 }
110 }
111
112 TEST_F(ReportingDelegateImplTest, OnHeaderReceivedValid) {
113 delegate_.OnHeaderReceived(
114 kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":86400}");
115
116 const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint);
117 ASSERT_TRUE(client);
118 EXPECT_EQ(kOrigin, client->origin);
119 EXPECT_EQ(kEndpoint, client->endpoint);
120 EXPECT_FALSE(client->subdomains);
121 EXPECT_EQ(86400, (client->expires - clock_.NowTicks()).InSeconds());
122 }
123
124 TEST_F(ReportingDelegateImplTest, OnHeaderReceivedValidSubdomains) {
125 delegate_.OnHeaderReceived(
126 kUrl, "{\"url\":\"" + kEndpoint.spec() +
127 "\",\"max-age\":86400,\"includeSubdomains\":true}");
128
129 const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint);
130 ASSERT_TRUE(client);
131 EXPECT_TRUE(client->subdomains);
132 }
133
134 TEST_F(ReportingDelegateImplTest, OnHeaderReceivedValidZeroMaxAge) {
135 delegate_.OnHeaderReceived(
136 kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":86400}");
137
138 const Client* client = FindClientInCache(&cache_, kOrigin, kEndpoint);
139 ASSERT_TRUE(client);
140
141 delegate_.OnHeaderReceived(
142 kUrl, "{\"url\":\"" + kEndpoint.spec() + "\",\"max-age\":0}");
143
144 client = FindClientInCache(&cache_, kOrigin, kEndpoint);
145 EXPECT_FALSE(client);
146 }
147
148 } // namespace
149 } // namespace net
OLDNEW
« net/reporting/reporting_delegate_impl.h ('K') | « net/reporting/reporting_delegate_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698