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_cache.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/time/time.h" | |
11 #include "base/values.h" | |
12 #include "net/reporting/reporting_test_util.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "url/gurl.h" | |
15 #include "url/origin.h" | |
16 | |
17 namespace net { | |
18 namespace { | |
19 | |
20 using Client = ReportingCache::Client; | |
21 using Report = ReportingCache::Report; | |
22 | |
23 const GURL kUrl1("https://origin1/path"); | |
jkarlin
2017/03/15 18:54:32
Globals need to be PODs or arrays/structs of PODs,
Julia Tuttle
2017/03/16 14:50:38
But it's a unittest x.x
I suppose static members
jkarlin
2017/03/17 15:07:01
Yes even tests :p It's important that test code is
| |
24 const url::Origin kOrigin1(GURL("https://origin1/")); | |
25 const url::Origin kOrigin2(GURL("https://origin2/")); | |
26 const GURL kEndpoint1("https://endpoint1/"); | |
27 const GURL kEndpoint2("https://endpoint2/"); | |
28 const std::string kGroup1("group1"); | |
29 const std::string kGroup2("group2"); | |
30 const std::string kType("default"); | |
31 const base::TimeTicks kNow(base::TimeTicks::Now()); | |
32 const base::TimeTicks kExpires1(kNow + base::TimeDelta::FromDays(7)); | |
33 const base::TimeTicks kExpires2(kExpires1 + base::TimeDelta::FromDays(7)); | |
34 | |
35 class ReportingCacheTest : public ::testing::Test { | |
36 protected: | |
37 ReportingCache cache_; | |
38 }; | |
39 | |
40 TEST_F(ReportingCacheTest, Reports) { | |
41 std::vector<const Report*> reports; | |
42 cache_.GetReports(&reports); | |
43 EXPECT_TRUE(reports.empty()); | |
44 | |
45 cache_.AddReport(kUrl1, kGroup1, kType, | |
46 base::MakeUnique<base::DictionaryValue>(), kNow, 0); | |
47 | |
48 cache_.GetReports(&reports); | |
49 ASSERT_EQ(1u, reports.size()); | |
50 const Report* report = reports[0]; | |
51 ASSERT_TRUE(report); | |
52 EXPECT_EQ(kUrl1, report->url); | |
53 EXPECT_EQ(kGroup1, report->group); | |
54 EXPECT_EQ(kType, report->type); | |
55 // TODO(juliatuttle): Check body? | |
56 EXPECT_EQ(kNow, report->queued); | |
57 EXPECT_EQ(0, report->attempts); | |
58 EXPECT_FALSE(report->pending); | |
59 EXPECT_FALSE(report->doomed); | |
60 | |
61 cache_.IncrementReportsAttempts(reports); | |
62 | |
63 cache_.GetReports(&reports); | |
64 ASSERT_EQ(1u, reports.size()); | |
65 report = reports[0]; | |
66 ASSERT_TRUE(report); | |
67 EXPECT_EQ(1, report->attempts); | |
68 | |
69 cache_.RemoveReports(reports); | |
70 | |
71 cache_.GetReports(&reports); | |
72 EXPECT_TRUE(reports.empty()); | |
73 } | |
74 | |
75 TEST_F(ReportingCacheTest, PendingReports) { | |
76 cache_.AddReport(kUrl1, kGroup1, kType, | |
77 base::MakeUnique<base::DictionaryValue>(), kNow, 0); | |
78 | |
79 std::vector<const Report*> reports; | |
80 cache_.GetReports(&reports); | |
81 ASSERT_EQ(1u, reports.size()); | |
82 EXPECT_FALSE(reports[0]->pending); | |
83 EXPECT_FALSE(reports[0]->doomed); | |
84 | |
85 cache_.SetReportsPending(reports); | |
86 EXPECT_TRUE(reports[0]->pending); | |
87 EXPECT_FALSE(reports[0]->doomed); | |
88 | |
89 cache_.RemoveReports(reports); | |
90 EXPECT_TRUE(reports[0]->pending); | |
91 EXPECT_TRUE(reports[0]->doomed); | |
92 | |
93 // After removing report, future calls to GetReports should not return it. | |
94 std::vector<const Report*> visible_reports; | |
95 cache_.GetReports(&visible_reports); | |
96 EXPECT_TRUE(visible_reports.empty()); | |
97 EXPECT_EQ(1u, cache_.GetFullReportCountForTesting()); | |
98 | |
99 // After clearing pending flag, report should be deleted. | |
100 cache_.ClearReportsPending(reports); | |
101 EXPECT_EQ(0u, cache_.GetFullReportCountForTesting()); | |
102 } | |
103 | |
104 TEST_F(ReportingCacheTest, Endpoints) { | |
105 cache_.SetClient(kOrigin1, kEndpoint1, false, kGroup1, kExpires1); | |
jkarlin
2017/03/15 18:54:32
, false /* sub-domains */, kGroup1, ...
Julia Tuttle
2017/03/16 14:50:38
Obsolete.
| |
106 | |
107 const Client* client = FindClientInCache(&cache_, kOrigin1, kEndpoint1); | |
108 ASSERT_TRUE(client); | |
109 EXPECT_EQ(kOrigin1, client->origin); | |
110 EXPECT_EQ(kEndpoint1, client->endpoint); | |
111 EXPECT_FALSE(client->subdomains); | |
112 EXPECT_EQ(kGroup1, client->group); | |
113 EXPECT_EQ(kExpires1, client->expires); | |
114 | |
115 cache_.SetClient(kOrigin1, kEndpoint1, true, kGroup2, kExpires2); | |
jkarlin
2017/03/15 18:54:32
Add a comment that this replaces the origin client
jkarlin
2017/03/15 18:54:33
ditto with the bool coomment
Julia Tuttle
2017/03/16 14:50:38
Done.
Julia Tuttle
2017/03/16 14:50:38
Obsolete.
| |
116 | |
117 client = FindClientInCache(&cache_, kOrigin1, kEndpoint1); | |
118 ASSERT_TRUE(client); | |
119 EXPECT_EQ(kOrigin1, client->origin); | |
120 EXPECT_EQ(kEndpoint1, client->endpoint); | |
121 EXPECT_TRUE(client->subdomains); | |
122 EXPECT_EQ(kGroup2, client->group); | |
123 EXPECT_EQ(kExpires2, client->expires); | |
124 | |
125 std::vector<const Client*> clients; | |
jkarlin
2017/03/15 18:54:33
std::vector<const Client*> clients {client};
Julia Tuttle
2017/03/16 14:50:38
Whoa.
Better yet, all three lines as:
cache_.R
| |
126 clients.push_back(client); | |
127 cache_.RemoveClients(clients); | |
128 | |
129 client = FindClientInCache(&cache_, kOrigin1, kEndpoint1); | |
130 EXPECT_FALSE(client); | |
131 } | |
132 | |
133 TEST_F(ReportingCacheTest, GetClientsForOriginAndGroup) { | |
134 cache_.SetClient(kOrigin1, kEndpoint1, false, kGroup1, kExpires1); | |
jkarlin
2017/03/15 18:54:32
In fact, you use this enough, why not make that bo
Julia Tuttle
2017/03/16 14:50:38
You are irritatingly correct. Done.
| |
135 cache_.SetClient(kOrigin1, kEndpoint2, false, kGroup2, kExpires1); | |
136 cache_.SetClient(kOrigin2, kEndpoint1, false, kGroup1, kExpires1); | |
137 | |
138 std::vector<const Client*> clients; | |
139 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup1, &clients); | |
140 ASSERT_EQ(1u, clients.size()); | |
141 const Client* client = clients[0]; | |
142 ASSERT_TRUE(client); | |
143 EXPECT_EQ(kOrigin1, client->origin); | |
144 EXPECT_EQ(kGroup1, client->group); | |
145 } | |
146 | |
147 TEST_F(ReportingCacheTest, RemoveClientForOriginAndEndpoint) { | |
148 cache_.SetClient(kOrigin1, kEndpoint1, false, kGroup1, kExpires1); | |
149 cache_.SetClient(kOrigin1, kEndpoint2, false, kGroup2, kExpires1); | |
150 cache_.SetClient(kOrigin2, kEndpoint1, false, kGroup1, kExpires1); | |
151 | |
152 cache_.RemoveClientForOriginAndEndpoint(kOrigin1, kEndpoint1); | |
153 | |
154 std::vector<const Client*> clients; | |
155 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup1, &clients); | |
156 EXPECT_TRUE(clients.empty()); | |
157 | |
158 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup2, &clients); | |
159 EXPECT_EQ(1u, clients.size()); | |
160 | |
161 cache_.GetClientsForOriginAndGroup(kOrigin2, kGroup1, &clients); | |
162 EXPECT_EQ(1u, clients.size()); | |
163 } | |
164 | |
165 TEST_F(ReportingCacheTest, RemoveClientsForEndpoint) { | |
166 cache_.SetClient(kOrigin1, kEndpoint1, false, kGroup1, kExpires1); | |
167 cache_.SetClient(kOrigin1, kEndpoint2, false, kGroup2, kExpires1); | |
168 cache_.SetClient(kOrigin2, kEndpoint1, false, kGroup1, kExpires1); | |
169 | |
170 cache_.RemoveClientsForEndpoint(kEndpoint1); | |
171 | |
172 std::vector<const Client*> clients; | |
173 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup1, &clients); | |
174 EXPECT_TRUE(clients.empty()); | |
175 | |
176 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup2, &clients); | |
177 EXPECT_EQ(1u, clients.size()); | |
178 | |
179 cache_.GetClientsForOriginAndGroup(kOrigin2, kGroup1, &clients); | |
180 EXPECT_TRUE(clients.empty()); | |
181 } | |
182 | |
183 } // namespace | |
184 } // namespace net | |
OLD | NEW |