mock_persistent_reporting_store.cc 11 KB

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