xref: /aosp_15_r20/external/cronet/net/network_error_logging/mock_persistent_nel_store.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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/network_error_logging/mock_persistent_nel_store.h"
6 
7 #include <sstream>
8 
9 namespace net {
10 
Command(Type type,NelPoliciesLoadedCallback loaded_callback)11 MockPersistentNelStore::Command::Command(
12     Type type,
13     NelPoliciesLoadedCallback loaded_callback)
14     : type(type), loaded_callback(std::move(loaded_callback)) {}
15 
Command(Type type,const NetworkErrorLoggingService::NelPolicy & policy)16 MockPersistentNelStore::Command::Command(
17     Type type,
18     const NetworkErrorLoggingService::NelPolicy& policy)
19     : type(type), key(policy.key) {}
20 
Command(Type type)21 MockPersistentNelStore::Command::Command(Type type) : type(type) {}
22 
Command(const Command & other)23 MockPersistentNelStore::Command::Command(const Command& other)
24     : type(other.type), key(other.key) {}
25 
26 MockPersistentNelStore::Command::Command(Command&& other) = default;
27 
28 MockPersistentNelStore::Command::~Command() = default;
29 
operator ==(const MockPersistentNelStore::Command & lhs,const MockPersistentNelStore::Command & rhs)30 bool operator==(const MockPersistentNelStore::Command& lhs,
31                 const MockPersistentNelStore::Command& rhs) {
32   if (lhs.type != rhs.type)
33     return false;
34   switch (lhs.type) {
35     // For LOAD_NEL_POLICIES and FLUSH, just check the type.
36     case MockPersistentNelStore::Command::Type::LOAD_NEL_POLICIES:
37     case MockPersistentNelStore::Command::Type::FLUSH:
38       return true;
39     // For ADD_NEL_POLICY, UPDATE_NEL_POLICY, and DELETE_NEL_POLICY,
40     // additionally check the policy's key.
41     case MockPersistentNelStore::Command::Type::ADD_NEL_POLICY:
42     case MockPersistentNelStore::Command::Type::UPDATE_NEL_POLICY:
43     case MockPersistentNelStore::Command::Type::DELETE_NEL_POLICY:
44       return (lhs.key == rhs.key);
45   }
46 }
47 
operator !=(const MockPersistentNelStore::Command & lhs,const MockPersistentNelStore::Command & rhs)48 bool operator!=(const MockPersistentNelStore::Command& lhs,
49                 const MockPersistentNelStore::Command& rhs) {
50   return !(lhs == rhs);
51 }
52 
53 MockPersistentNelStore::MockPersistentNelStore() = default;
54 
55 MockPersistentNelStore::~MockPersistentNelStore() = default;
56 
LoadNelPolicies(NelPoliciesLoadedCallback loaded_callback)57 void MockPersistentNelStore::LoadNelPolicies(
58     NelPoliciesLoadedCallback loaded_callback) {
59   DCHECK(!load_started_);
60   command_list_.emplace_back(Command::Type::LOAD_NEL_POLICIES,
61                              std::move(loaded_callback));
62   load_started_ = true;
63 }
64 
AddNelPolicy(const NetworkErrorLoggingService::NelPolicy & policy)65 void MockPersistentNelStore::AddNelPolicy(
66     const NetworkErrorLoggingService::NelPolicy& policy) {
67   DCHECK(load_started_);
68   command_list_.emplace_back(Command::Type::ADD_NEL_POLICY, policy);
69   ++queued_policy_count_delta_;
70 }
71 
UpdateNelPolicyAccessTime(const NetworkErrorLoggingService::NelPolicy & policy)72 void MockPersistentNelStore::UpdateNelPolicyAccessTime(
73     const NetworkErrorLoggingService::NelPolicy& policy) {
74   DCHECK(load_started_);
75   command_list_.emplace_back(Command::Type::UPDATE_NEL_POLICY, policy);
76 }
77 
DeleteNelPolicy(const NetworkErrorLoggingService::NelPolicy & policy)78 void MockPersistentNelStore::DeleteNelPolicy(
79     const NetworkErrorLoggingService::NelPolicy& policy) {
80   DCHECK(load_started_);
81   command_list_.emplace_back(Command::Type::DELETE_NEL_POLICY, policy);
82   --queued_policy_count_delta_;
83 }
84 
Flush()85 void MockPersistentNelStore::Flush() {
86   // Can be called before |load_started_| is true, if the
87   // NetworkErrorLoggingService is destroyed before getting a chance to load.
88   command_list_.emplace_back(Command::Type::FLUSH);
89   policy_count_ += queued_policy_count_delta_;
90   queued_policy_count_delta_ = 0;
91 }
92 
SetPrestoredPolicies(std::vector<NetworkErrorLoggingService::NelPolicy> policies)93 void MockPersistentNelStore::SetPrestoredPolicies(
94     std::vector<NetworkErrorLoggingService::NelPolicy> policies) {
95   DCHECK(!load_started_);
96   DCHECK_EQ(0, policy_count_);
97   policy_count_ += policies.size();
98   prestored_policies_.swap(policies);
99 }
100 
FinishLoading(bool load_success)101 void MockPersistentNelStore::FinishLoading(bool load_success) {
102   DCHECK(load_started_);
103   for (size_t i = 0; i < command_list_.size(); ++i) {
104     Command& command = command_list_[i];
105     if (command.type == Command::Type::LOAD_NEL_POLICIES) {
106       // If LOAD_NEL_POLICIES has been initiated, it should be the first
107       // operation.
108       DCHECK_EQ(0u, i);
109       DCHECK(!command.loaded_callback.is_null());
110       if (load_success) {
111         std::move(command.loaded_callback).Run(std::move(prestored_policies_));
112       } else {
113         std::move(command.loaded_callback)
114             .Run(std::vector<NetworkErrorLoggingService::NelPolicy>());
115       }
116     }
117     if (i > 0) {
118       // LOAD_NEL_POLICIES should not have been called twice.
119       DCHECK(command.type != Command::Type::LOAD_NEL_POLICIES);
120     }
121   }
122 }
123 
VerifyCommands(const CommandList & expected_commands) const124 bool MockPersistentNelStore::VerifyCommands(
125     const CommandList& expected_commands) const {
126   return command_list_ == expected_commands;
127 }
128 
GetAllCommands() const129 MockPersistentNelStore::CommandList MockPersistentNelStore::GetAllCommands()
130     const {
131   return command_list_;
132 }
133 
134 }  // namespace net
135