• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors
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/mock_persistent_reporting_store.h"
6 
7 #include <algorithm>
8 #include <memory>
9 
10 namespace net {
11 
Command(Type type,ReportingClientsLoadedCallback loaded_callback)12 MockPersistentReportingStore::Command::Command(
13     Type type,
14     ReportingClientsLoadedCallback loaded_callback)
15     : type(type), loaded_callback(std::move(loaded_callback)) {
16   DCHECK(type == Type::LOAD_REPORTING_CLIENTS);
17 }
18 
Command(Type type,const ReportingEndpoint & endpoint)19 MockPersistentReportingStore::Command::Command(
20     Type type,
21     const ReportingEndpoint& endpoint)
22     : Command(type, endpoint.group_key, endpoint.info.url) {}
23 
Command(Type type,const ReportingEndpointGroupKey & group_key,const GURL & endpoint_url)24 MockPersistentReportingStore::Command::Command(
25     Type type,
26     const ReportingEndpointGroupKey& group_key,
27     const GURL& endpoint_url)
28     : type(type), group_key(group_key), url(endpoint_url) {
29   DCHECK(type == Type::ADD_REPORTING_ENDPOINT ||
30          type == Type::UPDATE_REPORTING_ENDPOINT_DETAILS ||
31          type == Type::DELETE_REPORTING_ENDPOINT);
32 }
33 
Command(Type type,const CachedReportingEndpointGroup & group)34 MockPersistentReportingStore::Command::Command(
35     Type type,
36     const CachedReportingEndpointGroup& group)
37     : Command(type, group.group_key) {}
38 
Command(Type type,const ReportingEndpointGroupKey & group_key)39 MockPersistentReportingStore::Command::Command(
40     Type type,
41     const ReportingEndpointGroupKey& group_key)
42     : type(type), group_key(group_key) {
43   DCHECK(type == Type::ADD_REPORTING_ENDPOINT_GROUP ||
44          type == Type::UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS ||
45          type == Type::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME ||
46          type == Type::DELETE_REPORTING_ENDPOINT_GROUP);
47 }
48 
Command(Type type)49 MockPersistentReportingStore::Command::Command(Type type) : type(type) {
50   DCHECK(type == Type::FLUSH || type == Type::LOAD_REPORTING_CLIENTS);
51 }
52 
Command(const Command & other)53 MockPersistentReportingStore::Command::Command(const Command& other)
54     : type(other.type), group_key(other.group_key), url(other.url) {}
55 
56 MockPersistentReportingStore::Command::Command(Command&& other) = default;
57 
58 MockPersistentReportingStore::Command::~Command() = default;
59 
operator ==(const MockPersistentReportingStore::Command & lhs,const MockPersistentReportingStore::Command & rhs)60 bool operator==(const MockPersistentReportingStore::Command& lhs,
61                 const MockPersistentReportingStore::Command& rhs) {
62   if (lhs.type != rhs.type)
63     return false;
64   bool equal = true;
65   switch (lhs.type) {
66     // For load and flush, just check the type.
67     case MockPersistentReportingStore::Command::Type::LOAD_REPORTING_CLIENTS:
68     case MockPersistentReportingStore::Command::Type::FLUSH:
69       return true;
70     // For endpoint operations, check the url and group key.
71     case MockPersistentReportingStore::Command::Type::ADD_REPORTING_ENDPOINT:
72     case MockPersistentReportingStore::Command::Type::
73         UPDATE_REPORTING_ENDPOINT_DETAILS:
74     case MockPersistentReportingStore::Command::Type::DELETE_REPORTING_ENDPOINT:
75       equal &= (lhs.url == rhs.url);
76       [[fallthrough]];
77     // For endpoint group operations, check the group key only.
78     case MockPersistentReportingStore::Command::Type::
79         ADD_REPORTING_ENDPOINT_GROUP:
80     case MockPersistentReportingStore::Command::Type::
81         UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME:
82     case MockPersistentReportingStore::Command::Type::
83         UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS:
84     case MockPersistentReportingStore::Command::Type::
85         DELETE_REPORTING_ENDPOINT_GROUP:
86       equal &= (lhs.group_key == rhs.group_key);
87   }
88   return equal;
89 }
90 
operator !=(const MockPersistentReportingStore::Command & lhs,const MockPersistentReportingStore::Command & rhs)91 bool operator!=(const MockPersistentReportingStore::Command& lhs,
92                 const MockPersistentReportingStore::Command& rhs) {
93   return !(lhs == rhs);
94 }
95 
operator <<(std::ostream & out,const MockPersistentReportingStore::Command & cmd)96 std::ostream& operator<<(std::ostream& out,
97                          const MockPersistentReportingStore::Command& cmd) {
98   switch (cmd.type) {
99     case MockPersistentReportingStore::Command::Type::LOAD_REPORTING_CLIENTS:
100       return out << "LOAD_REPORTING_CLIENTS()";
101     case MockPersistentReportingStore::Command::Type::FLUSH:
102       return out << "FLUSH()";
103     case MockPersistentReportingStore::Command::Type::ADD_REPORTING_ENDPOINT:
104       return out << "ADD_REPORTING_ENDPOINT("
105                  << "NAK="
106                  << cmd.group_key.network_anonymization_key.ToDebugString()
107                  << ", "
108                  << "origin=" << cmd.group_key.origin << ", "
109                  << "group=" << cmd.group_key.group_name << ", "
110                  << "endpoint=" << cmd.url << ")";
111     case MockPersistentReportingStore::Command::Type::
112         UPDATE_REPORTING_ENDPOINT_DETAILS:
113       return out << "UPDATE_REPORTING_ENDPOINT_DETAILS("
114                  << "NAK="
115                  << cmd.group_key.network_anonymization_key.ToDebugString()
116                  << ", "
117                  << "origin=" << cmd.group_key.origin << ", "
118                  << "group=" << cmd.group_key.group_name << ", "
119                  << "endpoint=" << cmd.url << ")";
120     case MockPersistentReportingStore::Command::Type::DELETE_REPORTING_ENDPOINT:
121       return out << "DELETE_REPORTING_ENDPOINT("
122                  << "NAK="
123                  << cmd.group_key.network_anonymization_key.ToDebugString()
124                  << ", "
125                  << "origin=" << cmd.group_key.origin << ", "
126                  << "group=" << cmd.group_key.group_name << ", "
127                  << "endpoint=" << cmd.url << ")";
128     case MockPersistentReportingStore::Command::Type::
129         ADD_REPORTING_ENDPOINT_GROUP:
130       return out << "ADD_REPORTING_ENDPOINT_GROUP("
131                  << "NAK="
132                  << cmd.group_key.network_anonymization_key.ToDebugString()
133                  << ", "
134                  << "origin=" << cmd.group_key.origin << ", "
135                  << "group=" << cmd.group_key.group_name << ")";
136     case MockPersistentReportingStore::Command::Type::
137         UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME:
138       return out << "UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME("
139                  << "NAK="
140                  << cmd.group_key.network_anonymization_key.ToDebugString()
141                  << ", "
142                  << "origin=" << cmd.group_key.origin << ", "
143                  << "group=" << cmd.group_key.group_name << ")";
144     case MockPersistentReportingStore::Command::Type::
145         UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS:
146       return out << "UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS("
147                  << "NAK="
148                  << cmd.group_key.network_anonymization_key.ToDebugString()
149                  << ", "
150                  << "origin=" << cmd.group_key.origin << ", "
151                  << "group=" << cmd.group_key.group_name << ")";
152     case MockPersistentReportingStore::Command::Type::
153         DELETE_REPORTING_ENDPOINT_GROUP:
154       return out << "DELETE_REPORTING_ENDPOINT_GROUP("
155                  << "NAK="
156                  << cmd.group_key.network_anonymization_key.ToDebugString()
157                  << ", "
158                  << "origin=" << cmd.group_key.origin << ", "
159                  << "group=" << cmd.group_key.group_name << ")";
160   }
161 }
162 
163 MockPersistentReportingStore::MockPersistentReportingStore() = default;
164 MockPersistentReportingStore::~MockPersistentReportingStore() = default;
165 
LoadReportingClients(ReportingClientsLoadedCallback loaded_callback)166 void MockPersistentReportingStore::LoadReportingClients(
167     ReportingClientsLoadedCallback loaded_callback) {
168   DCHECK(!load_started_);
169   command_list_.emplace_back(Command::Type::LOAD_REPORTING_CLIENTS,
170                              std::move(loaded_callback));
171   load_started_ = true;
172 }
173 
AddReportingEndpoint(const ReportingEndpoint & endpoint)174 void MockPersistentReportingStore::AddReportingEndpoint(
175     const ReportingEndpoint& endpoint) {
176   DCHECK(load_started_);
177   command_list_.emplace_back(Command::Type::ADD_REPORTING_ENDPOINT, endpoint);
178   ++queued_endpoint_count_delta_;
179 }
180 
AddReportingEndpointGroup(const CachedReportingEndpointGroup & group)181 void MockPersistentReportingStore::AddReportingEndpointGroup(
182     const CachedReportingEndpointGroup& group) {
183   DCHECK(load_started_);
184   command_list_.emplace_back(Command::Type::ADD_REPORTING_ENDPOINT_GROUP,
185                              group);
186   ++queued_endpoint_group_count_delta_;
187 }
188 
UpdateReportingEndpointGroupAccessTime(const CachedReportingEndpointGroup & group)189 void MockPersistentReportingStore::UpdateReportingEndpointGroupAccessTime(
190     const CachedReportingEndpointGroup& group) {
191   DCHECK(load_started_);
192   command_list_.emplace_back(
193       Command::Type::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME, group);
194 }
195 
UpdateReportingEndpointDetails(const ReportingEndpoint & endpoint)196 void MockPersistentReportingStore::UpdateReportingEndpointDetails(
197     const ReportingEndpoint& endpoint) {
198   DCHECK(load_started_);
199   command_list_.emplace_back(Command::Type::UPDATE_REPORTING_ENDPOINT_DETAILS,
200                              endpoint);
201 }
202 
UpdateReportingEndpointGroupDetails(const CachedReportingEndpointGroup & group)203 void MockPersistentReportingStore::UpdateReportingEndpointGroupDetails(
204     const CachedReportingEndpointGroup& group) {
205   DCHECK(load_started_);
206   command_list_.emplace_back(
207       Command::Type::UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS, group);
208 }
209 
DeleteReportingEndpoint(const ReportingEndpoint & endpoint)210 void MockPersistentReportingStore::DeleteReportingEndpoint(
211     const ReportingEndpoint& endpoint) {
212   DCHECK(load_started_);
213   command_list_.emplace_back(Command::Type::DELETE_REPORTING_ENDPOINT,
214                              endpoint);
215   --queued_endpoint_count_delta_;
216 }
217 
DeleteReportingEndpointGroup(const CachedReportingEndpointGroup & group)218 void MockPersistentReportingStore::DeleteReportingEndpointGroup(
219     const CachedReportingEndpointGroup& group) {
220   DCHECK(load_started_);
221   command_list_.emplace_back(Command::Type::DELETE_REPORTING_ENDPOINT_GROUP,
222                              group);
223   --queued_endpoint_group_count_delta_;
224 }
225 
Flush()226 void MockPersistentReportingStore::Flush() {
227   // Can be called before |load_started_| is true, if the ReportingCache is
228   // destroyed before getting a chance to load.
229   command_list_.emplace_back(Command::Type::FLUSH);
230   endpoint_count_ += queued_endpoint_count_delta_;
231   queued_endpoint_count_delta_ = 0;
232   endpoint_group_count_ += queued_endpoint_group_count_delta_;
233   queued_endpoint_group_count_delta_ = 0;
234 }
235 
SetPrestoredClients(std::vector<ReportingEndpoint> endpoints,std::vector<CachedReportingEndpointGroup> groups)236 void MockPersistentReportingStore::SetPrestoredClients(
237     std::vector<ReportingEndpoint> endpoints,
238     std::vector<CachedReportingEndpointGroup> groups) {
239   DCHECK(!load_started_);
240   DCHECK_EQ(0, endpoint_count_);
241   DCHECK_EQ(0, endpoint_group_count_);
242   endpoint_count_ += endpoints.size();
243   prestored_endpoints_.swap(endpoints);
244   endpoint_group_count_ += groups.size();
245   prestored_endpoint_groups_.swap(groups);
246 }
247 
FinishLoading(bool load_success)248 void MockPersistentReportingStore::FinishLoading(bool load_success) {
249   DCHECK(load_started_);
250   for (size_t i = 0; i < command_list_.size(); ++i) {
251     Command& command = command_list_[i];
252     if (command.type == Command::Type::LOAD_REPORTING_CLIENTS) {
253       // If load has been initiated, it should be the first operation.
254       DCHECK_EQ(0u, i);
255       DCHECK(!command.loaded_callback.is_null());
256       if (load_success) {
257         std::move(command.loaded_callback)
258             .Run(std::move(prestored_endpoints_),
259                  std::move(prestored_endpoint_groups_));
260       } else {
261         std::move(command.loaded_callback)
262             .Run(std::vector<ReportingEndpoint>(),
263                  std::vector<CachedReportingEndpointGroup>());
264       }
265     }
266     if (i > 0) {
267       // Load should not have been called twice.
268       DCHECK(command.type != Command::Type::LOAD_REPORTING_CLIENTS);
269     }
270   }
271 }
272 
VerifyCommands(const CommandList & expected_commands) const273 bool MockPersistentReportingStore::VerifyCommands(
274     const CommandList& expected_commands) const {
275   return command_list_ == expected_commands;
276 }
277 
CountCommands(Command::Type t)278 int MockPersistentReportingStore::CountCommands(Command::Type t) {
279   int c = 0;
280   for (const auto& cmd : command_list_) {
281     if (cmd.type == t)
282       ++c;
283   }
284   return c;
285 }
286 
ClearCommands()287 void MockPersistentReportingStore::ClearCommands() {
288   command_list_.clear();
289 }
290 
291 MockPersistentReportingStore::CommandList
GetAllCommands() const292 MockPersistentReportingStore::GetAllCommands() const {
293   return command_list_;
294 }
295 
296 }  // namespace net
297